/
transaction.go
340 lines (280 loc) · 7.64 KB
/
transaction.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package core
import (
"encoding/json"
"fmt"
"github.com/btcsuite/btcutil/base58"
"github.com/ellcrys/elld/crypto"
"github.com/ellcrys/elld/util"
)
var (
// ErrTxVerificationFailed means a transaction signature could not be verified
ErrTxVerificationFailed = fmt.Errorf("signature verification failed")
// ErrTxInsufficientFee means fee is insufficient
ErrTxInsufficientFee = fmt.Errorf("insufficient fee")
// ErrTxLowValue means transaction value is less than or equal to zero
ErrTxLowValue = fmt.Errorf("value must be greater than zero")
//ErrTxTypeUnknown means transaction type is unknown
ErrTxTypeUnknown = fmt.Errorf("unknown transaction type")
)
var (
// TxTypeBalance represents a transaction from an account to another account
TxTypeBalance int64 = 0x1
// TxTypeAlloc represents a transaction to alloc coins to an account
TxTypeAlloc int64 = 0x2
)
// Base58CheckVersionTxPayload is the base58 encode version adopted
// for compressed transaction payload
var Base58CheckVersionTxPayload byte = 95
// InvokeArgs describes a function to be executed by a blockcode
type InvokeArgs struct {
Func string `json:"func" msgpack:"func"`
Params map[string][]byte `json:"params" msgpack:"params"`
}
// Transaction represents a transaction
type Transaction struct {
Type int64 `json:"type" msgpack:"type"`
Nonce uint64 `json:"nonce" msgpack:"nonce"`
To util.String `json:"to" msgpack:"to"`
From util.String `json:"from" msgpack:"from"`
SenderPubKey util.String `json:"senderPubKey" msgpack:"senderPubKey"`
Value util.String `json:"value" msgpack:"value"`
Timestamp int64 `json:"timestamp" msgpack:"timestamp"`
Fee util.String `json:"fee" msgpack:"fee"`
InvokeArgs *InvokeArgs `json:"invokeArgs,omitempty" msgpack:"invokeArgs"`
Sig []byte `json:"sig" msgpack:"sig"`
Hash util.Hash `json:"hash" msgpack:"hash"`
}
// NewTransaction creates a new transaction
func NewTransaction(txType int64, nonce uint64, to util.String,
senderPubKey util.String, value util.String,
fee util.String, timestamp int64) (tx *Transaction) {
tx = new(Transaction)
tx.Type = txType
tx.Nonce = nonce
tx.To = to
tx.SenderPubKey = senderPubKey
tx.Value = value
tx.Timestamp = timestamp
tx.Fee = fee
return
}
// NewTx creates a new, signed transaction
func NewTx(txType int64, nonce uint64, to util.String, senderKey *crypto.Key, value util.String, fee util.String, timestamp int64) (tx *Transaction) {
tx = new(Transaction)
tx.Type = txType
tx.Nonce = nonce
tx.To = to
tx.SenderPubKey = util.String(senderKey.PubKey().Base58())
tx.From = util.String(senderKey.Addr())
tx.Value = value
tx.Timestamp = timestamp
tx.Fee = fee
tx.Hash = tx.ComputeHash()
sig, err := TxSign(tx, senderKey.PrivKey().Base58())
if err != nil {
panic(err)
}
tx.Sig = sig
return
}
// SetFrom sets the sender
func (tx *Transaction) SetFrom(from util.String) {
tx.From = from
}
// GetSignature gets the signature
func (tx *Transaction) GetSignature() []byte {
return tx.Sig
}
// SetSignature sets the signature
func (tx *Transaction) SetSignature(s []byte) {
tx.Sig = s
}
// GetSenderPubKey gets the sender public key
func (tx *Transaction) GetSenderPubKey() util.String {
return tx.SenderPubKey
}
// SetSenderPubKey sets the sender public key
func (tx *Transaction) SetSenderPubKey(pk util.String) {
tx.SenderPubKey = pk
}
// ToBase58 returns base58 encoded equivalent of the transaction
func (tx *Transaction) ToBase58() string {
bs, _ := json.Marshal(tx)
return base58.CheckEncode(bs, Base58CheckVersionTxPayload)
}
// GetTimestamp gets the timestamp
func (tx *Transaction) GetTimestamp() int64 {
return tx.Timestamp
}
// SetTimestamp set the unix timestamp
func (tx *Transaction) SetTimestamp(t int64) {
tx.Timestamp = t
}
// GetNonce gets the nonce
func (tx *Transaction) GetNonce() uint64 {
return tx.Nonce
}
// GetFee gets the value
func (tx *Transaction) GetFee() util.String {
return tx.Fee
}
// GetValue gets the value
func (tx *Transaction) GetValue() util.String {
return tx.Value
}
// SetValue gets the value
func (tx *Transaction) SetValue(v util.String) {
tx.Value = v
}
// GetTo gets the address of receiver
func (tx *Transaction) GetTo() util.String {
return tx.To
}
// GetFrom gets the address of sender
func (tx *Transaction) GetFrom() util.String {
return tx.From
}
// GetHash returns the hash of tx
func (tx *Transaction) GetHash() util.Hash {
return tx.Hash
}
// SetHash sets the hash
func (tx *Transaction) SetHash(h util.Hash) {
tx.Hash = h
}
// GetType gets the transaction type
func (tx *Transaction) GetType() int64 {
return tx.Type
}
// GetBytesNoHashAndSig converts a transaction
// to bytes equivalent but omits the hash and
// signature in the result.
func (tx *Transaction) GetBytesNoHashAndSig() []byte {
var invokeArgsBs []byte
if tx.InvokeArgs != nil {
invokeArgsBs = tx.InvokeArgs.Bytes()
}
data := []interface{}{
tx.Fee,
tx.From,
invokeArgsBs,
tx.Nonce,
tx.SenderPubKey,
tx.Timestamp,
tx.To,
tx.Type,
tx.Value,
}
return getBytes(data)
}
// Bytes converts a transaction
// to bytes equivalent
func (tx *Transaction) Bytes() []byte {
var invokeArgsBs []byte
if tx.InvokeArgs != nil {
invokeArgsBs = tx.InvokeArgs.Bytes()
}
data := []interface{}{
tx.Fee,
tx.From,
tx.Hash,
invokeArgsBs,
tx.Nonce,
tx.SenderPubKey,
tx.Sig,
tx.Timestamp,
tx.To,
tx.Type,
tx.Value,
}
return getBytes(data)
}
// GetSizeNoFee returns the virtual size of the
// transaction by summing up the size of
// field contents except the `fee` field.
// The value does not represent the true size
// of the transaction
// on disk. It is main required for computing
// minimum fees etc.
func (tx *Transaction) GetSizeNoFee() int64 {
var invokeArgsBs []byte
if tx.InvokeArgs != nil {
invokeArgsBs = tx.InvokeArgs.Bytes()
}
data := []interface{}{
tx.From,
tx.Hash,
invokeArgsBs,
tx.Nonce,
tx.SenderPubKey,
tx.Sig,
tx.Timestamp,
tx.To,
tx.Type,
tx.Value,
}
return int64(len(getBytes(data)))
}
// ComputeHash returns the SHA256
// hash of the transaction.
func (tx *Transaction) ComputeHash() util.Hash {
bs := tx.GetBytesNoHashAndSig()
hash := util.Blake2b256(bs)
return util.BytesToHash(hash[:])
}
// GetID returns the hex representation of Hash()
func (tx *Transaction) GetID() string {
return tx.ComputeHash().HexStr()
}
// Sign the transaction
func (tx *Transaction) Sign(privKey string) ([]byte, error) {
return TxSign(tx, privKey)
}
// TxVerify checks whether a transaction's signature is valid.
// Expect tx.SenderPubKey and tx.Sig to be set.
func TxVerify(tx *Transaction) error {
if tx == nil {
return fmt.Errorf("nil tx")
}
if tx.SenderPubKey == "" {
return fieldError("senderPubKey", "sender public not set")
}
if len(tx.Sig) == 0 {
return fieldError("sig", "signature not set")
}
pubKey, err := crypto.PubKeyFromBase58(string(tx.SenderPubKey))
if err != nil {
return fieldError("senderPubKey", err.Error())
}
valid, err := pubKey.Verify(tx.GetBytesNoHashAndSig(), tx.Sig)
if err != nil {
return fieldError("sig", err.Error())
}
if !valid {
return crypto.ErrTxVerificationFailed
}
return nil
}
// TxSign signs a transaction.
// Expects private key in base58Check encoding
func TxSign(tx *Transaction, privKey string) ([]byte, error) {
if tx == nil {
return nil, fmt.Errorf("nil tx")
}
pKey, err := crypto.PrivKeyFromBase58(privKey)
if err != nil {
return nil, err
}
sig, err := pKey.Sign(tx.GetBytesNoHashAndSig())
if err != nil {
return nil, err
}
return sig, nil
}
// Bytes returns the byte equivalent
func (m *InvokeArgs) Bytes() []byte {
return getBytes([]interface{}{
m.Func,
m.Params,
})
}