-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
246 lines (215 loc) · 6.22 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
package tx
import (
"crypto/ecdsa"
"encoding/binary"
"fmt"
"github.com/TarsCloud/TarsGo/tars/protocol/codec"
"github.com/e-chain-net/echain-go-sdk-721/tars-protocol/bcostars"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/holiman/uint256"
"math/rand"
"strings"
"time"
)
// @todo
func GetBlockLimit(groupId string) (int64, error) {
return 3333, nil
}
/*
1
*/
func CreateTransactionData(groupId string, chainId string, to string, dataHex string, abiJson string, blockLimit int64) (*bcostars.TransactionData, error) {
if blockLimit == 0 {
var err error
blockLimit, err = GetBlockLimit(groupId)
if err != nil {
return nil, err
}
}
if len(abiJson) > 0 {
// 合约部署
return &bcostars.TransactionData{
Version: 0,
ChainID: chainId,
GroupID: groupId,
BlockLimit: blockLimit,
Nonce: Nonce(),
//To: "0x0000000000000000000000000000000000000000",
Input: HexByte2Int8(common.FromHex(dataHex)),
Abi: abiJson,
}, nil
}
// 方法调用
return &bcostars.TransactionData{
Version: 0,
ChainID: chainId,
GroupID: groupId,
BlockLimit: blockLimit,
Nonce: Nonce(),
To: strings.ToLower(to),
//Input: HexByte2Int8(dataHex),
Input: HexByte2Int8(common.FromHex(dataHex)),
Abi: "",
}, nil
}
func hash(buf []byte) string {
// https://github.com/FISCO-BCOS/bcos-crypto/blob/main/bcos-crypto/hash/Keccak256.cpp
return crypto.Keccak256Hash(buf).Hex()
}
func CalculateTransactionDataHash(txData *bcostars.TransactionData) (string, error) {
// Keccak256 hash
buf := codec.NewBuffer()
//if err := txData.WriteTo(buf); err != nil {
// return "", err
//}
versionBigEndian := make([]byte,4)
binary.BigEndian.PutUint32(versionBigEndian,uint32(txData.Version))
buf.WriteBytes(versionBigEndian)
buf.WriteBytes([]byte(txData.ChainID))
buf.WriteBytes([]byte(txData.GroupID))
blockLimitBigEndian := make([]byte,8)
binary.BigEndian.PutUint64(blockLimitBigEndian,uint64(txData.BlockLimit))
buf.WriteBytes(blockLimitBigEndian)
buf.WriteBytes([]byte(txData.Nonce))
buf.WriteBytes([]byte(txData.To))
buf.WriteSliceInt8(txData.Input)
buf.WriteBytes([]byte(txData.Abi))
//return HexStringWithPrefix(hash(buf.ToBytes())), nil
//fmt.Println(buf.ToBytes())
return hash(buf.ToBytes()), nil
}
func Nonce() string {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var buf []byte
for i := 0; i < 32; i++ {
it := r.Intn(255)
buf = append(buf, byte(it))
}
u256 := uint256.NewInt(0).SetBytes(buf)
return fmt.Sprint(u256)
}
func PrivateKeyToAddress(privateKey string) (string, error) {
privKey, err := ParseKeyPairFromPrivateKey(privateKey)
if err != nil {
return "", err
}
//// 获取地址
addr := crypto.PubkeyToAddress(privKey.PublicKey)
//fmt.Printf("地址为: %s\n", addr.Hex())
return strings.ToLower(addr.Hex()), nil
}
func ParseKeyPairFromPrivateKey(privateKey string) (*ecdsa.PrivateKey, error) {
return crypto.HexToECDSA(privateKey)
}
func SignTransactionDataHash(privateKey string, txDataHash string) (string, error) {
privKey, err := ParseKeyPairFromPrivateKey(privateKey)
if err != nil {
return "", err
}
//Sign a message using the private key.
hash,err := hexutil.Decode(txDataHash)
if err != nil {
return "",err
}
signature,err := crypto.Sign(hash,privKey)
if err != nil{
return "",err
}
return hexutil.Encode(signature),nil
}
func CreateTransaction(from string, txData *bcostars.TransactionData, txDataHash string, signedTxDataHash string, attribute int32) (*bcostars.Transaction, error) {
return &bcostars.Transaction{
Data: *txData,
DataHash: HexByte2Int8(common.FromHex(txDataHash)),
Signature: HexByte2Int8(common.FromHex(signedTxDataHash)),
ImportTime: 0,
Attribute: attribute,
ExtraData: "",
//Sender: HexByte2Int8(common.FromHex(strings.ToLower(from))),
}, nil
}
func EncodeTransaction(tx *bcostars.Transaction) (string, error) {
buf := codec.NewBuffer()
if err := tx.WriteTo(buf); err != nil {
return "", err
}
return hexutil.Encode(buf.ToBytes()), nil
}
func CreateSignedTransaction(privateKey string, groupId string, chainId string, to string, dataHex string, abiJson string, blockLimit int64, attribute int32) (txHash string, txHex string, err error) {
txData, err := CreateTransactionData(groupId, chainId, to, dataHex, abiJson, blockLimit)
if err != nil {
return "", "", err
}
//fmt.Println("txData", txData)
txDataHash, err := CalculateTransactionDataHash(txData)
if err != nil {
return "", "", err
}
//fmt.Println("txDataHash", txDataHash)
signedTxDataHash, err := SignTransactionDataHash(privateKey, txDataHash)
if err != nil {
return "", "", err
}
//fmt.Println("signedTxDataHash", signedTxDataHash)
from, err := PrivateKeyToAddress(privateKey)
if err != nil {
return "", "", err
}
tx, err := CreateTransaction(from, txData, txDataHash, signedTxDataHash, attribute)
if err != nil {
return "", "", err
}
_txHex, err := EncodeTransaction(tx)
if err != nil {
return "", "", err
}
//fmt.Println("_txHex", _txHex)
return txDataHash, _txHex, nil
}
func HexByte2Int8(dataHex []byte) []int8 {
var dataHexInt8 []int8
for _, d := range dataHex {
dataHexInt8 = append(dataHexInt8, int8(d))
}
return dataHexInt8
}
func Int82Byte(data []int8) []byte{
var dataHexByte []byte
for _, d := range data {
dataHexByte = append(dataHexByte, byte(d))
}
return dataHexByte
}
func EncodeTransactionDataToHex(tx *bcostars.TransactionData) (string, error) {
buf := codec.NewBuffer()
if err := tx.WriteTo(buf); err != nil {
return "", err
}
return hexutil.Encode(buf.ToBytes()), nil
}
func DecodeTransactionDataFromHex(txDataHex string) (*bcostars.TransactionData, error) {
decode, err := hexutil.Decode(txDataHex)
if err != nil {
return nil, err
}
buf := codec.NewReader(decode)
txData := &bcostars.TransactionData{}
if err := txData.ReadFrom(buf); err != nil {
return nil, err
}
return txData, nil
}
func DecodeTransactionFromHex(txHex string)(*bcostars.Transaction,error){
decode,err := hexutil.Decode(txHex);
if err != nil {
return nil, err
}
buf := codec.NewReader(decode)
tx := &bcostars.Transaction{}
if err := tx.ReadFrom(buf); err != nil {
return nil, err
}
return tx, nil
}