Skip to content

Commit 2618c2f

Browse files
Seongjae ChoiSeongjae Choi
authored andcommitted
Assign Pay to TransactionInterface
1 parent e30881e commit 2618c2f

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

core/transaction/pay.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package transaction
22

33
import (
4+
"github.com/CodeChain-io/codechain-rpc-go/crypto"
45
"github.com/CodeChain-io/codechain-rpc-go/primitives"
56
"github.com/ethereum/go-ethereum/rlp"
67
)
@@ -36,7 +37,7 @@ func (p Pay) ToEncodeObject() []interface{} {
3637
return []interface{}{byte(p.Seq()), p.Fee().ToEncodeObject(), p.NetworkID(), p.ActionToEncodeObject()}
3738
}
3839

39-
func (p Pay) ActionToJSON() PayAction {
40+
func (p Pay) ActionToJSON() interface{} {
4041
return PayAction{
4142
p.Receiver.Value,
4243
p.Quantity.ToJSON()}
@@ -46,3 +47,25 @@ func (p Pay) RlpBytes() []byte {
4647
x, _ := rlp.EncodeToBytes(p.ToEncodeObject())
4748
return x
4849
}
50+
51+
func (p Pay) Hash() []byte {
52+
x, _ := crypto.Blake256(p.RlpBytes())
53+
return x
54+
}
55+
56+
func (p Pay) UnsignedHash() primitives.H256 {
57+
hash, _ := crypto.Blake256(p.RlpBytes())
58+
59+
var value [32]byte
60+
copy(value[:], hash[:32])
61+
return primitives.H256(value)
62+
}
63+
64+
func (p *Pay) Sign(secret primitives.H256, seq uint, fee primitives.U64) SignedTransaction {
65+
// Handle error
66+
p.SetSeq(seq)
67+
p.SetFee(fee)
68+
sig := crypto.SignEcdsa(p.UnsignedHash().Bytes(), secret.Bytes())
69+
70+
return NewSignedTransaction(p, sig, nil, nil, nil)
71+
}

core/transaction/signedTransaction.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package transaction
22

33
import (
4+
"encoding/hex"
5+
46
"github.com/CodeChain-io/codechain-rpc-go/crypto"
57
"github.com/CodeChain-io/codechain-rpc-go/primitives"
6-
// "github.com/ethereum/go-ethereum/rlp"
8+
"github.com/ethereum/go-ethereum/rlp"
79
)
810

911
type SignedTransactionJSON struct {
@@ -15,34 +17,34 @@ type SignedTransactionJSON struct {
1517
}
1618

1719
type SignedTransaction struct {
18-
Unsigned transaction
20+
Unsigned TransactionInterface
1921
BlockNumber *int
2022
BlockHash *primitives.H256
2123
TransactionIndex *int
22-
signature string
24+
signature []byte
2325
}
2426

2527
func NewSignedTransaction(
26-
unsigned transaction,
27-
signature string,
28+
unsigned TransactionInterface,
29+
signature []byte,
2830
blockNumber *int,
2931
blockHash *primitives.H256,
3032
transactionIndex *int) SignedTransaction {
3133
return SignedTransaction{unsigned, blockNumber, blockHash, transactionIndex, signature}
3234
}
3335

34-
func (t SignedTransaction) Signature() string {
36+
func (t SignedTransaction) Signature() []byte {
3537
return t.signature
3638
}
3739

38-
/*func (t SignedTransaction) ToEncodeObject() []interface{} {
39-
return []interface{}{t.Unsigned.ToEncodeObject(), t.Signature()}
40-
}*/
40+
func (t SignedTransaction) ToEncodeObject() []interface{} {
41+
return append(t.Unsigned.ToEncodeObject(), t.Signature())
42+
}
4143

42-
/*func (t SignedTransaction) RlpBytes() []byte {
44+
func (t SignedTransaction) RlpBytes() []byte {
4345
x, _ := rlp.EncodeToBytes(t.ToEncodeObject())
4446
return x
45-
}*/
47+
}
4648

4749
func (t SignedTransaction) Hash() primitives.H256 {
4850
hash, _ := crypto.Blake256([]byte{0}) //t.RlpBytes())
@@ -73,6 +75,6 @@ func (t SignedTransaction) ToJSON() SignedTransactionJSON {
7375
*t.BlockNumber,
7476
t.BlockHash.ToString(),
7577
*t.TransactionIndex,
76-
t.signature,
78+
hex.EncodeToString(t.signature),
7779
t.Hash().ToJSON()}
7880
}

core/transaction/transaction.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package transaction
22

33
import (
4-
"encoding/hex"
5-
64
"github.com/CodeChain-io/codechain-rpc-go/crypto"
75
"github.com/CodeChain-io/codechain-rpc-go/primitives"
86
)
@@ -20,16 +18,16 @@ type TransactionJSON struct {
2018
}
2119

2220
type TransactionInterface interface {
23-
Seq() int
21+
Seq() uint
2422
Fee() primitives.U64
25-
SetSeq(int)
23+
SetSeq(uint)
2624
SetFee(primitives.U64)
2725
NetworkID() string
28-
ToEncodeObject() string
26+
ToEncodeObject() []interface{}
2927
RlpBytes() []byte
3028
UnsignedHash() primitives.H256
31-
Sign(primitives.H256, int, primitives.U64) interface{} // FIXME
32-
ToJSON() string
29+
Sign(primitives.H256, uint, primitives.U64) SignedTransaction
30+
ToJSON() TransactionJSON
3331
GetType() string
3432
ActionToJSON() interface{}
3533
ActionToEncodeObject() []interface{}
@@ -73,11 +71,13 @@ func (t transaction) UnsignedHash() primitives.H256 {
7371
return primitives.H256(value) // byte
7472
}
7573

76-
func (t transaction) Sign(secret primitives.H256, seq *int, fee *primitives.U64) SignedTransaction {
74+
func (t *transaction) Sign(secret primitives.H256, seq uint, fee primitives.U64) SignedTransaction {
7775
// Handle error
78-
sig := crypto.SignEcdsa([]byte(t.UnsignedHash().ToString()), []byte(secret.ToString()))
7976

80-
return NewSignedTransaction(t, hex.EncodeToString(sig), nil, nil, nil) // nil
77+
sig := crypto.SignEcdsa(t.UnsignedHash().Bytes(), secret.Bytes())
78+
t.SetSeq(seq)
79+
t.SetFee(fee)
80+
return NewSignedTransaction(t, sig, nil, nil, nil) // nil
8181
}
8282

8383
func (t transaction) ToJSON() TransactionJSON {
@@ -89,5 +89,21 @@ func (t transaction) ToJSON() TransactionJSON {
8989
}
9090

9191
func (t transaction) GetType() string {
92-
return "Transaction"
92+
return ""
93+
}
94+
95+
func (t transaction) ActionToEncodeObject() []interface{} {
96+
return nil
97+
}
98+
99+
func (t transaction) ActionToJSON() interface{} {
100+
return nil
101+
}
102+
103+
func (t transaction) RlpBytes() []byte {
104+
return nil
105+
}
106+
107+
func (t transaction) ToEncodeObject() []interface{} {
108+
return nil
93109
}

0 commit comments

Comments
 (0)