-
Notifications
You must be signed in to change notification settings - Fork 199
/
transaction.go
94 lines (80 loc) · 2.72 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
//go:generate protoc -I=proto -I=$GOPATH/src -I=$GOPATH/src/github.com/gogo/protobuf/protobuf --gogoslick_out=. transaction.proto
package transaction
import (
"math/big"
"github.com/ElrondNetwork/elrond-go/core/check"
"github.com/ElrondNetwork/elrond-go/data"
)
var _ = data.TransactionHandler(&Transaction{})
// IsInterfaceNil verifies if underlying object is nil
func (tx *Transaction) IsInterfaceNil() bool {
return tx == nil
}
// SetValue sets the value of the transaction
func (tx *Transaction) SetValue(value *big.Int) {
tx.Value = value
}
// SetData sets the data of the transaction
func (tx *Transaction) SetData(data []byte) {
tx.Data = data
}
// SetRcvAddr sets the receiver address of the transaction
func (tx *Transaction) SetRcvAddr(addr []byte) {
tx.RcvAddr = addr
}
// SetSndAddr sets the sender address of the transaction
func (tx *Transaction) SetSndAddr(addr []byte) {
tx.SndAddr = addr
}
// TrimSlicePtr creates a copy of the provided slice without the excess capacity
func TrimSlicePtr(in []*Transaction) []*Transaction {
if len(in) == 0 {
return []*Transaction{}
}
ret := make([]*Transaction, len(in))
copy(ret, in)
return ret
}
// TrimSliceHandler creates a copy of the provided slice without the excess capacity
func TrimSliceHandler(in []data.TransactionHandler) []data.TransactionHandler {
if len(in) == 0 {
return []data.TransactionHandler{}
}
ret := make([]data.TransactionHandler, len(in))
copy(ret, in)
return ret
}
// frontendTransaction represents the DTO used in transaction signing/validation.
type frontendTransaction struct {
Nonce uint64 `json:"nonce"`
Value string `json:"value"`
Receiver string `json:"receiver"`
Sender string `json:"sender"`
SenderUsername []byte `json:"senderUsername,omitempty"`
ReceiverUsername []byte `json:"receiverUsername,omitempty"`
GasPrice uint64 `json:"gasPrice"`
GasLimit uint64 `json:"gasLimit"`
Data []byte `json:"data,omitempty"`
Signature string `json:"signature,omitempty"`
}
// GetDataForSigning returns the serialized transaction having an empty signature field
func (tx *Transaction) GetDataForSigning(encoder Encoder, marshalizer Marshalizer) ([]byte, error) {
if check.IfNil(encoder) {
return nil, ErrNilEncoder
}
if check.IfNil(marshalizer) {
return nil, ErrNilMarshalizer
}
ftx := &frontendTransaction{
Nonce: tx.Nonce,
Value: tx.Value.String(),
Receiver: encoder.Encode(tx.RcvAddr),
Sender: encoder.Encode(tx.SndAddr),
GasPrice: tx.GasPrice,
GasLimit: tx.GasLimit,
SenderUsername: tx.SndUserName,
ReceiverUsername: tx.RcvUserName,
Data: tx.Data,
}
return marshalizer.Marshal(ftx)
}