-
Notifications
You must be signed in to change notification settings - Fork 2
/
spend.go
346 lines (296 loc) · 9.02 KB
/
spend.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
341
342
343
344
345
346
/*
This file is part of go-palletone.
go-palletone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-palletone is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @author PalletOne core developers <[email protected]>
* @date 2018
*/
package ethadaptor
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/rlp"
"github.com/palletone/adaptor"
)
func writeBytes(buf io.Writer, appendBytes []byte) {
lenBytes := len(appendBytes)
if lenBytes == 32 {
buf.Write(appendBytes)
} else {
zeroBytes := make([]byte, 32-lenBytes)
buf.Write(zeroBytes)
buf.Write(appendBytes)
}
}
func SignTransaction(input *adaptor.SignTransactionInput) (*adaptor.SignTransactionOutput, error) {
var tx types.Transaction
err := rlp.DecodeBytes(input.Transaction, &tx)
if err != nil {
return nil, err
}
priKey, err := crypto.ToECDSA(input.PrivateKey)
if err != nil {
return nil, err
}
//
signedTx, err := types.SignTx(&tx, types.HomesteadSigner{}, priKey)
if err != nil {
return nil, err
}
//
rlpTXBytes, err := rlp.EncodeToBytes(signedTx)
if err != nil {
return nil, err
}
//signedTx.WithSignature()
v, r, s := signedTx.RawSignatureValues()
var buf bytes.Buffer
writeBytes(&buf, r.Bytes())
writeBytes(&buf, s.Bytes())
buf.WriteByte(v.Bytes()[0] - 27)
//save result
var result adaptor.SignTransactionOutput
result.Signature = buf.Bytes()
result.Extra = rlpTXBytes
return &result, nil
}
func BindETHTxAndSignature(input *adaptor.BindTxAndSignatureInput) (*adaptor.BindTxAndSignatureOutput, error) {
var tx types.Transaction
err := rlp.DecodeBytes(input.Transaction, &tx)
if err != nil {
return nil, err
}
signedTx, err := tx.WithSignature(types.HomesteadSigner{}, input.Signatures[0])
if err != nil {
return nil, err
}
rlpTXBytes, err := rlp.EncodeToBytes(signedTx)
if err != nil {
return nil, err
}
//save result
var result adaptor.BindTxAndSignatureOutput
result.SignedTx = rlpTXBytes
return &result, nil
}
func BindTxAndSignature(input *adaptor.BindTxAndSignatureInput) (*adaptor.BindTxAndSignatureOutput, error) {
data := make([]byte, 0)
//method ID, example, 0xa9059cbb withdraw(address,uint,string,bytes,bytes,bytes)
hash := crypto.Keccak256Hash(input.Extra)
data = append(data, hash[:4]...)
//receiver amount token extra
data = append(data, input.Transaction[33:]...) //m+from=33
//signatures
for i := range input.Signatures {
sigPadded := common.LeftPadBytes(input.Signatures[i], 32)
data = append(data, sigPadded...)
}
//save result
var result adaptor.BindTxAndSignatureOutput
result.SignedTx = data
return &result, nil
}
func CalcTxHash(input *adaptor.CalcTxHashInput) (*adaptor.CalcTxHashOutput, error) {
var tx types.Transaction
err := rlp.DecodeBytes(input.Transaction, &tx)
if err != nil {
return nil, fmt.Errorf("rlp.DecodeBytes failed : %s", err.Error())
}
//save result
var result adaptor.CalcTxHashOutput
result.Hash = tx.Hash().Bytes()
return &result, nil
}
func SendTransaction(input *adaptor.SendTransactionInput, rpcParams *RPCParams) (
*adaptor.SendTransactionOutput, error) {
//get rpc client
client, err := GetClient(rpcParams)
if err != nil {
return nil, err
}
var tx types.Transaction
err = rlp.DecodeBytes(input.Transaction, &tx)
if err != nil {
return nil, err
}
//
err = client.SendTransaction(context.Background(), &tx)
if err != nil {
//fmt.Println("client.SendTransaction failed:", err)
return nil, err
}
//save result
var result adaptor.SendTransactionOutput
result.TxID = tx.Hash().Bytes()
return &result, nil
}
func CreateETHTx(input *adaptor.CreateTransferTokenTxInput, rpcParams *RPCParams) (
*adaptor.CreateTransferTokenTxOutput, error) {
if input.Amount == nil {
return nil, errors.New("input's Amount is nil")
}
if input.Fee == nil {
return nil, errors.New("input's Fee is nil")
}
//get rpc client
client, err := GetClient(rpcParams)
if err != nil {
return nil, err
}
fromAddress := common.HexToAddress(input.FromAddress)
nonce, err := client.PendingNonceAt(context.Background(), fromAddress)
if err != nil {
return nil, err
}
gasLimitU64 := uint64(2100000)
gasLimit := big.NewInt(2100000)
toAddress := common.HexToAddress(input.ToAddress)
gasPrice := input.Fee.Amount.Div(input.Fee.Amount, gasLimit)
tx := types.NewTransaction(nonce, toAddress,
input.Amount.Amount, //in wei
gasLimitU64,
gasPrice, //in wei
nil) //todo
rlpTXBytes, err := rlp.EncodeToBytes(tx)
if err != nil {
return nil, err
}
//fmt.Printf("unsigned tx: %x\n", rlpTXBytes)
//save result
var result adaptor.CreateTransferTokenTxOutput
result.Transaction = append(result.Transaction, rlpTXBytes...)
return &result, nil
}
//func CreateContractMsg(input *adaptor.CreateTransferTokenTxInput, client *ethclient.Client,
// fromAddress common.Address) (*adaptor.CreateTransferTokenTxOutput, error) {
// gasLimit := uint64(21000) //in units
// toAddress := common.HexToAddress(input.ToAddress)
//
// var extra []byte
// extra = append(extra, fromAddress.Bytes()...)
// //extra = append(extra, input.Extra...)
//
// tx := types.NewTransaction(0, toAddress,
// &input.Amount.Amount, //in wei
// gasLimit,
// &input.Fee.Amount, //in wei
// extra)
//
// rlpTXBytes, err := rlp.EncodeToBytes(tx)
// if err != nil {
// return nil, err
// }
// //fmt.Printf("unsigned tx: %x\n", rlpTXBytes)
// //save result
// var result adaptor.CreateTransferTokenTxOutput
// result.Transaction = append(result.Transaction, 'm')
// result.Transaction = append(result.Transaction, rlpTXBytes...)
//
// return &result, nil
//}
func CreateTx(input *adaptor.CreateTransferTokenTxInput) (*adaptor.CreateTransferTokenTxOutput, error) {
if input.Amount == nil {
return nil, errors.New("input's Amount is nil")
}
//if input.Fee == nil {
// return nil, errors.New("input's Fee is nil")
//}
var data []byte
data = append(data, []byte("msg")...)
//from
fromAddress := common.HexToAddress(input.FromAddress)
fromAddressPadded := common.LeftPadBytes(fromAddress.Bytes(), 32)
data = append(data, fromAddressPadded...)
//to
toAddress := common.HexToAddress(input.ToAddress)
toAddressPadded := common.LeftPadBytes(toAddress.Bytes(), 32)
data = append(data, toAddressPadded...)
//amount
amountPadded := common.LeftPadBytes(input.Amount.Amount.Bytes(), 32)
data = append(data, amountPadded...)
//asset empty is eth, other is erc20
if input.Amount.Asset != "" {
token := common.HexToAddress(input.Amount.Asset)
tokenPadded := common.LeftPadBytes(token.Bytes(), 32)
data = append(data, tokenPadded...)
}
//extra, example: reqid
extraPadded := common.LeftPadBytes(input.Extra, 32)
data = append(data, extraPadded...)
var result adaptor.CreateTransferTokenTxOutput
result.Transaction = data
return &result, nil
////get rpc client
//client, err := GetClient(rpcParams)
//if err != nil {
// return nil, err
//}
//fromAddress := common.HexToAddress(input.FromAddress)
//code, err := client.CodeAt(context.Background(), fromAddress, nil)
//if len(code) > 0 {
// return CreateContractMsg(input, client, fromAddress)
//} else {
// return CreateETHTx(input, client, fromAddress)
//}
}
func HashMessage(input *adaptor.HashMessageInput) (*adaptor.HashMessageOutput, error) {
hash := crypto.Keccak256Hash(input.Message)
//fmt.Printf("%x\n", hash.Bytes())
var result adaptor.HashMessageOutput
result.Hash = hash.Bytes()
return &result, nil
}
func SignMessage(input *adaptor.SignMessageInput) (*adaptor.SignMessageOutput, error) {
priKey, err := crypto.ToECDSA(input.PrivateKey)
if err != nil {
return nil, err
}
hash := crypto.Keccak256Hash(input.Message)
//fmt.Printf("%x\n", hash.Bytes())
sig, err := crypto.Sign(hash.Bytes(), priKey)
if err != nil {
return nil, err
}
var result adaptor.SignMessageOutput
result.Signature = sig
return &result, nil
}
func VerifySignature(input *adaptor.VerifySignatureInput) (*adaptor.VerifySignatureOutput, error) {
//
hash := crypto.Keccak256Hash(input.Message)
//fmt.Printf("%x\n", hash.Bytes())
//
pubkeyByte, err := crypto.Ecrecover(hash.Bytes(), input.Signature)
if err != nil {
return nil, err
}
//fmt.Printf("%x\n", pubkeyByte)
var result adaptor.VerifySignatureOutput
if len(pubkeyByte) == len(input.PublicKey) {
result.Pass = bytes.Equal(pubkeyByte, input.PublicKey)
} else {
pubkey, err := crypto.UnmarshalPubkey(pubkeyByte)
if err != nil {
return nil, err
}
result.Pass = bytes.Equal(crypto.CompressPubkey(pubkey), input.PublicKey)
}
return &result, nil
}