forked from walkbean/vsys-sdk-go
/
transaction.go
168 lines (154 loc) · 5.37 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
package vsys
import (
"time"
)
type Transaction struct {
TxId string `json:"txId,omitempty"`
Timestamp int64 `json:"timestamp"`
Fee int64 `json:"fee"`
FeeScale int16 `json:"feeScale"`
Amount int64 `json:"amount,omitempty"`
SenderPublicKey string `json:"senderPublicKey"`
Attachment string `json:"attachment,omitempty"`
Recipient string `json:"recipient,omitempty"`
Signature string `json:"signature"`
// contract
Contract string `json:"contract,omitempty"`
InitData string `json:"initData,omitempty"`
ContractId string `json:"contractId,omitempty"`
TokenIdx int32 `json:"tokenIdx,omitempty"`
Description string `json:"description,omitempty"`
FunctionIndex int16 `json:"functionIndex"`
FunctionData string `json:"functionData,omitempty"`
txType uint8
}
func NewPaymentTransaction(recipient string, amount int64, attachment string) *Transaction {
return &Transaction{
Timestamp: time.Now().Unix() * 1e9,
Fee: DefaultTxFee,
FeeScale: DefaultFeeScale,
Recipient: recipient,
Amount: amount,
Attachment: Base58Encode([]byte(attachment)),
txType: TxTypePayment,
}
}
func NewLeaseTransaction(recipient string, amount int64) *Transaction {
return &Transaction{
Timestamp: time.Now().Unix() * 1e9,
Fee: DefaultTxFee,
FeeScale: DefaultFeeScale,
Recipient: recipient,
Amount: amount,
txType: TxTypeLease,
}
}
func NewCancelLeaseTransaction(txId string) *Transaction {
return &Transaction{
Timestamp: time.Now().Unix() * 1e9,
Fee: DefaultTxFee,
FeeScale: DefaultFeeScale,
TxId: txId,
txType: TxTypeCancelLease,
}
}
func NewRegisterTransaction(contract string, data string, contractDescription string) *Transaction {
return &Transaction{
txType: TxTypeContractRegister,
Contract: contract,
InitData: data,
Description: contractDescription,
Fee: 100 * VsysPrecision,
FeeScale: DefaultFeeScale,
Timestamp: time.Now().Unix() * 1e9,
}
}
func NewExecuteTransaction(contractId string, funcIdx int16, funcData string, attachment string) *Transaction {
return &Transaction{
txType: TxTypeContractExecute,
ContractId: contractId,
FunctionIndex: funcIdx,
FunctionData: funcData,
Attachment: Base58Encode([]byte(attachment)),
Fee: ContractExecFee,
FeeScale: DefaultFeeScale,
Timestamp: time.Now().Unix() * 1e9,
}
}
func (tx *Transaction) TxType() int {
return int(tx.txType)
}
// BuildTxData generate data which is used to be signed
func (tx *Transaction) BuildTxData() []byte {
data := make([]byte, 0)
data = append(data, tx.txType)
if tx.Timestamp <= 0 {
tx.Timestamp = time.Now().Unix() * 1e9
}
if tx.Fee <= 0 {
tx.Fee = DefaultTxFee
}
if tx.FeeScale <= 0 {
tx.FeeScale = DefaultFeeScale
}
switch tx.txType {
case TxTypePayment:
return tx.buildPaymentData(data)
case TxTypeLease:
return tx.buildLeaseData(data)
case TxTypeCancelLease:
return tx.buildLeaseCancelData(data)
case TxTypeContractRegister:
return tx.buildRegisterContractData(data)
case TxTypeContractExecute:
return tx.buildExecuteContractData(data)
}
return data
}
func (tx *Transaction) buildPaymentData(data []byte) []byte {
data = append(data, uint64ToByte(tx.Timestamp)...)
data = append(data, uint64ToByte(tx.Amount)...)
data = append(data, uint64ToByte(tx.Fee)...)
data = append(data, uint16ToByte(tx.FeeScale)...)
data = append(data, Base58Decode(tx.Recipient)...)
data = append(data, bytesToByteArrayWithSize(Base58Decode(tx.Attachment))...)
//data = append(data, uint16ToByte(int16(len(Base58Decode(tx.Attachment))))...)
//data = append(data, Base58Decode(tx.Attachment)...)
return data
}
func (tx *Transaction) buildLeaseData(data []byte) []byte {
data = append(data, Base58Decode(tx.Recipient)...)
data = append(data, uint64ToByte(tx.Amount)...)
data = append(data, uint64ToByte(tx.Fee)...)
data = append(data, uint16ToByte(tx.FeeScale)...)
data = append(data, uint64ToByte(tx.Timestamp)...)
return data
}
func (tx *Transaction) buildLeaseCancelData(data []byte) []byte {
data = append(data, uint64ToByte(tx.Fee)...)
data = append(data, uint16ToByte(tx.FeeScale)...)
data = append(data, uint64ToByte(tx.Timestamp)...)
data = append(data, Base58Decode(tx.TxId)...)
return data
}
func (tx *Transaction) buildRegisterContractData(data []byte) []byte {
data = append(data, bytesToByteArrayWithSize(Base58Decode(tx.Contract))...)
data = append(data, bytesToByteArrayWithSize(Base58Decode(tx.InitData))...)
data = append(data, bytesToByteArrayWithSize([]byte(tx.Description))...)
data = append(data, uint64ToByte(tx.Fee)...)
data = append(data, uint16ToByte(tx.FeeScale)...)
data = append(data, uint64ToByte(tx.Timestamp)...)
return data
}
func (tx *Transaction) buildExecuteContractData(data []byte) []byte {
data = append(data, Base58Decode(tx.ContractId)...)
data = append(data, uint16ToByte(tx.FunctionIndex)...)
data = append(data, bytesToByteArrayWithSize(Base58Decode(tx.FunctionData))...)
data = append(data, bytesToByteArrayWithSize(Base58Decode(tx.Attachment))...)
//data = append(data, uint16ToByte(int16(len(Base58Decode(tx.Attachment))))...)
//data = append(data, Base58Decode(tx.Attachment)...)
data = append(data, uint64ToByte(tx.Fee)...)
data = append(data, uint16ToByte(tx.FeeScale)...)
data = append(data, uint64ToByte(tx.Timestamp)...)
return data
}