Skip to content

Commit 3eb88d8

Browse files
Seongjae ChoiSeongjae Choi
authored andcommitted
Seperate general type files from transaction package
1 parent f605b37 commit 3eb88d8

File tree

5 files changed

+87
-95
lines changed

5 files changed

+87
-95
lines changed

core/transaction/signedTransaction.go renamed to core/signedTransaction.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package transaction
1+
package core
22

33
import (
44
"encoding/hex"

core/transaction/transaction.go renamed to core/transaction.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package transaction
1+
package core
22

33
import (
44
"github.com/CodeChain-io/codechain-sdk-go/crypto"
@@ -33,45 +33,45 @@ type TransactionInterface interface {
3333
ActionToEncodeObject() []interface{}
3434
}
3535

36-
type transaction struct {
36+
type Transaction struct {
3737
seq uint
3838
fee primitives.U64
3939
networkID string
4040
}
4141

42-
func newTransaction(networkID string) transaction {
43-
return transaction{0, primitives.NewU64("0"), networkID}
42+
func NewTransaction(networkID string) Transaction {
43+
return Transaction{0, primitives.NewU64("0"), networkID}
4444
}
4545

46-
func (t transaction) Seq() uint {
46+
func (t Transaction) Seq() uint {
4747
return t.seq
4848
}
4949

50-
func (t transaction) Fee() primitives.U64 {
50+
func (t Transaction) Fee() primitives.U64 {
5151
return t.fee
5252
}
5353

54-
func (t *transaction) SetSeq(seq uint) {
54+
func (t *Transaction) SetSeq(seq uint) {
5555
t.seq = seq
5656
}
5757

58-
func (t *transaction) SetFee(fee primitives.U64) {
58+
func (t *Transaction) SetFee(fee primitives.U64) {
5959
t.fee = fee
6060
}
6161

62-
func (t transaction) NetworkID() string {
62+
func (t Transaction) NetworkID() string {
6363
return t.networkID
6464
}
6565

66-
func (t transaction) UnsignedHash() primitives.H256 {
66+
func (t Transaction) UnsignedHash() primitives.H256 {
6767
hash, _ := crypto.Blake256([]byte{0}) //t.RlpBytes())
6868

6969
var value [32]byte
7070
copy(value[:], hash[:32])
7171
return primitives.H256(value) // byte
7272
}
7373

74-
func (t *transaction) Sign(secret primitives.H256, seq uint, fee primitives.U64) SignedTransaction {
74+
func (t *Transaction) Sign(secret primitives.H256, seq uint, fee primitives.U64) SignedTransaction {
7575
// Handle error
7676

7777
sig := crypto.SignEcdsa(t.UnsignedHash().Bytes(), secret.Bytes())
@@ -80,30 +80,30 @@ func (t *transaction) Sign(secret primitives.H256, seq uint, fee primitives.U64)
8080
return NewSignedTransaction(t, sig, nil, nil, nil) // nil
8181
}
8282

83-
func (t transaction) ToJSON() TransactionJSON {
83+
func (t Transaction) ToJSON() TransactionJSON {
8484
return TransactionJSON{
8585
t.GetType(), // assign action type.
8686
t.networkID,
8787
t.seq,
8888
t.fee.ToJSON()}
8989
}
9090

91-
func (t transaction) GetType() string {
91+
func (t Transaction) GetType() string {
9292
return ""
9393
}
9494

95-
func (t transaction) ActionToEncodeObject() []interface{} {
95+
func (t Transaction) ActionToEncodeObject() []interface{} {
9696
return nil
9797
}
9898

99-
func (t transaction) ActionToJSON() interface{} {
99+
func (t Transaction) ActionToJSON() interface{} {
100100
return nil
101101
}
102102

103-
func (t transaction) RlpBytes() []byte {
103+
func (t Transaction) RlpBytes() []byte {
104104
return nil
105105
}
106106

107-
func (t transaction) ToEncodeObject() []interface{} {
107+
func (t Transaction) ToEncodeObject() []interface{} {
108108
return nil
109109
}

core/transaction/pay.go

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

33
import (
4+
"github.com/CodeChain-io/codechain-sdk-go/core"
45
"github.com/CodeChain-io/codechain-sdk-go/crypto"
56
"github.com/CodeChain-io/codechain-sdk-go/primitives"
67
"github.com/ethereum/go-ethereum/rlp"
@@ -12,7 +13,7 @@ type PayAction struct {
1213
}
1314

1415
type Pay struct {
15-
transaction
16+
core.Transaction
1617
Receiver primitives.PlatformAddress
1718
Quantity primitives.U64
1819
}
@@ -21,7 +22,7 @@ func NewPay(receiver primitives.PlatformAddress,
2122
quantity primitives.U64,
2223
networkID string) Pay {
2324

24-
t := newTransaction(networkID)
25+
t := core.NewTransaction(networkID)
2526
return Pay{t, receiver, quantity}
2627
}
2728

@@ -61,11 +62,11 @@ func (p Pay) UnsignedHash() primitives.H256 {
6162
return primitives.H256(value)
6263
}
6364

64-
func (p *Pay) Sign(secret primitives.H256, seq uint, fee primitives.U64) SignedTransaction {
65+
func (p *Pay) Sign(secret primitives.H256, seq uint, fee primitives.U64) core.SignedTransaction {
6566
// Handle error
6667
p.SetSeq(seq)
6768
p.SetFee(fee)
6869
sig := crypto.SignEcdsa(p.UnsignedHash().Bytes(), secret.Bytes())
6970

70-
return NewSignedTransaction(p, sig, nil, nil, nil)
71+
return core.NewSignedTransaction(p, sig, nil, nil, nil)
7172
}

core/transaction/pay_test.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package transaction
33
import (
44
"bytes"
55
"encoding/hex"
6-
"github.com/CodeChain-io/codechain-sdk-go/primitives"
76
"testing"
7+
8+
"github.com/CodeChain-io/codechain-sdk-go/primitives"
9+
"github.com/CodeChain-io/codechain-sdk-go/rpc"
810
)
911

1012
func TestPay(t *testing.T) {
@@ -28,3 +30,64 @@ func TestPay(t *testing.T) {
2830
}
2931

3032
}
33+
34+
func TestSigning(t *testing.T) {
35+
id, _ := hex.DecodeString("0000000000000000000000000000000000000000")
36+
accountID := primitives.NewH160(id)
37+
38+
p, _ := primitives.PlatformAddressFromAccountID(accountID, "tc")
39+
q := primitives.NewU64("11")
40+
41+
pay := NewPay(p, q, "tc")
42+
43+
secret, _ := primitives.StringToH256("ede1d4ccb4ec9a8bbbae9a13db3f4a7b56ea04189be86ac3a6a439d9a0a1addd")
44+
fee := primitives.NewU64("0")
45+
46+
signed := pay.Sign(secret, uint(0), fee)
47+
48+
if bytes.Compare(
49+
signed.RlpBytes(),
50+
[]byte{248, 96, 128, 128, 130, 116, 99, 215, 2, 148, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 184, 65, 63, 155, 207, 244, 132, 189, 95, 29, 85, 73, 249, 18, 249, 238, 175, 140, 47, 227, 73, 178, 87, 189, 226, 182, 31, 177, 3, 96, 19, 212, 228, 76, 32, 74, 66, 21, 210, 108, 184, 121, 234, 173, 32, 40, 254, 26, 120, 152, 228, 207, 154, 93, 151, 158, 179, 131, 224, 163, 132, 20, 13, 110, 4, 193, 1}) != 0 {
51+
t.Fatal("Signing Pay transaction Error")
52+
}
53+
}
54+
55+
func TestSendSignedTransaction(t *testing.T) {
56+
rpcClient := rpc.NewRPC("https://corgi-rpc.codechain.io/")
57+
58+
recipient, err1 := primitives.PlatformAddressFromString("wccq95dss644nd3xgddje38k2kdldzsy6ns5gmscxu2")
59+
if err1 != nil {
60+
t.Fatal(err1)
61+
}
62+
63+
quantity := primitives.NewU64("12345")
64+
65+
pay := NewPay(recipient, quantity, "wc")
66+
67+
secret, err2 := primitives.StringToH256("6692200de85d2ae6fbb817c656cc9f5a0b8ee1ed82795289aaf4400f1d817d62")
68+
if err2 != nil {
69+
t.Fatal(err2)
70+
}
71+
72+
fee := primitives.NewU64("100")
73+
accountID, err3 := primitives.PlatformAddressFromString("wccq9dddym9sc6rn3jgsnmp8qel57s5mwjq6v5ye68e")
74+
75+
if err3 != nil {
76+
t.Fatal(err3)
77+
}
78+
79+
seq, err4 := rpcClient.Chain.GetLatestSeq(accountID.Value)
80+
81+
if err4 != nil {
82+
t.Fatal(err4)
83+
}
84+
85+
signed := pay.Sign(secret, seq, fee)
86+
87+
_, err5 := rpcClient.Mempool.SendSignedTransaction(hex.EncodeToString(signed.RlpBytes()))
88+
89+
if err5 != nil {
90+
t.Fatal(err5)
91+
}
92+
93+
}

core/transaction/signedTransaction_test.go

Lines changed: 0 additions & 72 deletions
This file was deleted.

0 commit comments

Comments
 (0)