/
txdata.go
174 lines (150 loc) · 4.13 KB
/
txdata.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package fiscobcos
import (
"bytes"
"github.com/BSNDA/bsn-sdk-crypto/key"
"golang.org/x/crypto/sha3"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rlp"
"io"
"math/big"
"sync/atomic"
)
type Transaction struct {
data txdata
//sign txsign
// caches
hash atomic.Value
size atomic.Value
from atomic.Value
smcrypto bool
}
type txdata struct {
AccountNonce *big.Int `json:"nonce" gencodec:"required"`
Price *big.Int `json:"gasPrice" gencodec:"required"`
GasLimit *big.Int `json:"gas" gencodec:"required"`
BlockLimit *big.Int `json:"blocklimit" gencodec:"required"`
Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
Amount *big.Int `json:"value" gencodec:"required"`
Payload []byte `json:"input" gencodec:"required"`
// for fisco bcos 2.0
ChainID *big.Int `json:"chainId" gencodec:"required"`
GroupID *big.Int `json:"groupId" gencodec:"required"`
ExtraData []byte `json:"extraData" gencodec:"required"` // rlp:"nil"
V *big.Int `json:"v" gencodec:"required"`
R *big.Int `json:"r" gencodec:"required"`
S *big.Int `json:"s" gencodec:"required"`
}
// NewTransaction returns a new transaction
func NewTransaction(nonce *big.Int, to common.Address, amount *big.Int, gasLimit *big.Int, gasPrice *big.Int, blockLimit *big.Int, data []byte, chainId *big.Int, groupId *big.Int, extraData []byte, smcrypto bool) *Transaction {
return newTransaction(nonce, &to, amount, gasLimit, gasPrice, blockLimit, data, chainId, groupId, extraData, smcrypto)
}
func newTransaction(nonce *big.Int, to *common.Address, amount *big.Int, gasLimit *big.Int, gasPrice *big.Int, blockLimit *big.Int, data []byte, chainId *big.Int, groupId *big.Int, extraData []byte, smcrypto bool) *Transaction {
if len(data) > 0 {
data = common.CopyBytes(data)
}
d := txdata{
AccountNonce: nonce,
Recipient: to,
Payload: data,
Amount: new(big.Int),
GasLimit: gasLimit,
BlockLimit: blockLimit,
Price: new(big.Int),
ChainID: new(big.Int),
GroupID: new(big.Int),
ExtraData: extraData,
V: new(big.Int),
R: new(big.Int),
S: new(big.Int),
}
if amount != nil {
d.Amount.Set(amount)
}
if gasPrice != nil {
d.Price.Set(gasPrice)
}
if chainId != nil {
d.ChainID.Set(chainId)
}
if groupId != nil {
d.GroupID.Set(groupId)
}
if extraData != nil {
d.ExtraData = extraData
}
return &Transaction{data: d, smcrypto: smcrypto}
}
// EncodeRLP implements rlp.Encoder
func (tx *Transaction) EncodeRLP(w io.Writer) error {
return rlp.Encode(w, &tx.data)
}
// DecodeRLP implements rlp.Decoder
func (tx *Transaction) DecodeRLP(s *rlp.Stream) error {
_, size, _ := s.Kind()
err := s.Decode(&tx.data)
if err == nil {
tx.size.Store(common.StorageSize(rlp.ListSize(size)))
}
return err
}
func (tx *Transaction) SignData(priKey key.PrivateKeyProvider) ([]byte, error) {
//txb, _ := rlp.EncodeToBytes(tx.data)
if tx.smcrypto {
txb := priKey.Hash(SM3HashNonSig(tx))
r, s, pub, err := priKey.SignTx(txb)
if err != nil {
return nil, err
}
tx.data.V = pub
tx.data.R = r
tx.data.S = s
} else {
txb := Hash(tx).Bytes()
r, s, v, err := priKey.SignTx(txb)
if err != nil {
return nil, err
}
tx.data.V = v
tx.data.R = r
tx.data.S = s
}
txd, _ := rlp.EncodeToBytes(tx.data)
return txd, nil
}
func rlpHash(x interface{}) (h common.Hash) {
hw := sha3.NewLegacyKeccak256()
rlp.Encode(hw, x)
hw.Sum(h[:0])
return h
}
func Hash(tx *Transaction) common.Hash {
return rlpHash([]interface{}{
tx.data.AccountNonce,
tx.data.Price,
tx.data.GasLimit,
tx.data.BlockLimit,
tx.data.Recipient,
tx.data.Amount,
tx.data.Payload,
tx.data.ChainID,
tx.data.GroupID,
tx.data.ExtraData,
})
}
func SM3HashNonSig(tx *Transaction) []byte {
var src []byte
buf := bytes.NewBuffer(src)
rlp.Encode(buf, []interface{}{
tx.data.AccountNonce,
tx.data.Price,
tx.data.GasLimit,
tx.data.BlockLimit,
tx.data.Recipient,
tx.data.Amount,
tx.data.Payload,
tx.data.ChainID,
tx.data.GroupID,
tx.data.ExtraData,
})
return buf.Bytes()
}