-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateTx.go
429 lines (339 loc) · 11.2 KB
/
createTx.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
package qtumtxsigner
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/decred/dcrd/dcrec/secp256k1/v3"
"github.com/decred/dcrd/dcrec/secp256k1/v3/ecdsa"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/qtumproject/qtumsuite"
"github.com/qtumproject/qtumsuite/chaincfg"
"github.com/qtumproject/qtumsuite/chaincfg/chainhash"
"github.com/qtumproject/qtumsuite/txscript"
"github.com/qtumproject/qtumsuite/wire"
"github.com/shopspring/decimal"
)
type JSONRPCRequest struct {
ID string `json:"id"`
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params []interface{} `json:"params"`
}
type JSONRPCResult struct {
JSONRPC string `json:"jsonrpc"`
RawResult json.RawMessage `json:"result,omitempty"`
Error *JSONRPCError `json:"error,omitempty"`
ID json.RawMessage `json:"id"`
}
type JSONRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
}
type ListUnspentResponse []struct {
Address string `json:"address"`
Txid string `json:"txid"`
Vout uint `json:"vout"`
Amount decimal.Decimal `json:"amount"`
Safe bool `json:"safe"`
Spendable bool `json:"spendable"`
Solvable bool `json:"solvable"`
Label string `json:"label"`
Confirmations int `json:"confirmations"`
ScriptPubKey string `json:"scriptPubKey"`
RedeemScript string `json:"redeemScript"`
}
var qtumTestNetParams = chaincfg.MainNetParams
var (
OP_CREATE = byte(0xc1)
OP_CALL = byte(0xc2)
)
func init() {
//TestnetParams
qtumTestNetParams.PubKeyHashAddrID = 120
qtumTestNetParams.ScriptHashAddrID = 110
}
//Create something like a map where "name"->inputs or Look at ABI gen
func CallContractData(reader io.Reader, arguments map[string][]interface{}) ([]byte, error) {
parsedABI, err := abi.JSON(reader)
if err != nil {
fmt.Println("Error reading abi JSON: ")
return []byte{}, err
}
//Packing the ABI
var bytecode []byte
for name, inputs := range arguments {
var methodInputs []interface{}
//Loop through arguments and create the list of inputs
fmt.Println("Method name: ", name)
for _, input := range inputs {
methodInputs = append(methodInputs, input)
}
//Pack the inputs into the method
fmt.Println("Method Inputs: ", methodInputs)
bytecode, err = parsedABI.Pack(name, methodInputs...)
if err != nil {
fmt.Println("Could not pack input: ", err)
return []byte{}, err
}
}
return bytecode, nil
}
func GatherUTXOs(serilizedPubKey []byte, sourceTx *wire.MsgTx) (*ListUnspentResponse, int64, error) {
//Get UTXOs from network
//Use the UTXOs to figure out the previousTxId as well as the pubKeyScript
/* LOOK INTO JANUS TAKING ADDRESSES WITHOUT THE 0x PREFIX AND STILL RETURNING A BALANCE*/
keyid := qtumsuite.Hash160(serilizedPubKey)
params := []interface{}{"0x" + hex.EncodeToString(keyid), 0.005}
data := JSONRPCRequest{
ID: "10",
Jsonrpc: "2.0",
Method: "qtum_getUTXOs",
Params: params,
}
payloadBytes, err := json.Marshal(data)
if err != nil {
return nil, 0, err
}
body := bytes.NewReader(payloadBytes)
//Link to RPC
req, err := http.NewRequest("POST", "http://localhost:23889", body)
if err != nil {
return nil, 0, err
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, 0, err
}
defer resp.Body.Close()
var cResp JSONRPCResult
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
return nil, 0, err
}
var listUnspentResp *ListUnspentResponse
if err := json.Unmarshal(cResp.RawResult, &listUnspentResp); err != nil {
return nil, 0, err
}
balance := decimal.NewFromFloat(0)
for _, utxo := range *listUnspentResp {
balance = balance.Add(utxo.Amount)
}
balance = balance.Mul(decimal.NewFromFloat(1e8))
floatBalance, exact := balance.Float64()
if exact != true {
return nil, 0, err
}
return listUnspentResp, int64(floatBalance), nil
}
func P2khTx(privKey string, destination string, amount int64) (string, error) {
redeemTx := wire.NewMsgTx(wire.TxVersion)
//Decode WIF
wif, err := qtumsuite.DecodeWIF(privKey)
if err != nil {
return "", err
}
//Gather info extracted from UTXOs related to addrPubKey (prevTxId, balance, pkScript)
utxos, balance, err := GatherUTXOs(wif.SerializePubKey(), redeemTx)
if err != nil {
return "", err
}
//Checking for sufficient balance
if balance < amount {
return "", fmt.Errorf("insufficient balance")
}
//Loop through UTXO to find candidates
var amountIn int64 = 0
var pkScripts [][]byte
for _, v := range *utxos {
utxoHash, err := chainhash.NewHashFromStr(v.Txid)
if err != nil {
fmt.Println("could not get hash from transaction ID; error:", err)
return "", err
}
outPoint := wire.NewOutPoint(utxoHash, uint32(v.Vout))
txIn := wire.NewTxIn(outPoint, nil, nil)
floatAmount := v.Amount.Mul(decimal.NewFromFloat(1e8))
utxoAmount, exact := floatAmount.Float64()
if exact != true {
fmt.Println("could not convert utxoAmount from decimal to float precisely; err:", err)
return "", err
}
amountIn += int64(utxoAmount)
//Append ScriptPubKey to the list of scripts
utxoPkScript, err := hex.DecodeString(v.ScriptPubKey)
if err != nil {
return "", err
}
pkScripts = append(pkScripts, utxoPkScript)
//Append Transaction
redeemTx.AddTxIn(txIn)
//Once we gathered all the UTXOs we need, we stop
if amountIn >= amount {
break
}
}
//Get destination address as []byte from function argument (destination)
destinationAddr, err := qtumsuite.DecodeAddress(destination, &qtumTestNetParams)
if err != nil {
return "", err
}
//Generate PayToAddressScript
destinationScript, err := txscript.PayToAddrScript(destinationAddr)
if err != nil {
return "", err
}
/*
ADD OP CODES FOR CONTRACT CREATION TO THE TX OUTPUT
*/
//Adding the destination address and the amount to the transaction as output
redeemTxOut := wire.NewTxOut(amount, destinationScript)
redeemTx.AddTxOut(redeemTxOut)
//Might want to look into a non hard coded way to calculate this
var change int64 = amountIn - amount - 100000
//Get address
addrPubKey, err := qtumsuite.NewAddressPubKey(wif.SerializePubKey(), &chaincfg.TestNet3Params)
//Generate PayToAddrScript for source address
changeScript, err := txscript.PayToAddrScript(addrPubKey)
if err != nil {
return "", err
}
chanceTxOut := wire.NewTxOut(change, changeScript)
redeemTx.AddTxOut(chanceTxOut)
// Sign the Tx
finalRawTx, err := SignTx(redeemTx, pkScripts, wif)
return finalRawTx, nil
}
func ContractTx(privKey string, from string, contractAddr string, amount int64, data []byte, gas int64, gasPrice int64, opcode byte) (string, error) {
redeemTx := wire.NewMsgTx(wire.TxVersion)
//Decode WIF
wif, err := qtumsuite.DecodeWIF(privKey)
if err != nil {
return "", err
}
//Gather info extracted from UTXOs related to addrPubKey (prevTxId, balance, pkScript)
utxos, balance, err := GatherUTXOs(wif.SerializePubKey(), redeemTx)
if err != nil {
return "", err
}
//Checking for sufficient balance
if balance < amount {
return "", fmt.Errorf("insufficient balance")
}
//Loop through UTXO to find candidates
var amountIn int64 = 0
var pkScripts [][]byte
for _, v := range *utxos {
utxoHash, err := chainhash.NewHashFromStr(v.Txid)
if err != nil {
fmt.Println("could not get hash from transaction ID; error:", err)
return "", err
}
outPoint := wire.NewOutPoint(utxoHash, uint32(v.Vout))
txIn := wire.NewTxIn(outPoint, nil, nil)
floatAmount := v.Amount.Mul(decimal.NewFromFloat(1e8))
utxoAmount, exact := floatAmount.Float64()
if exact != true {
fmt.Println("could not convert utxoAmount from decimal to float precisely; err:", err)
return "", err
}
amountIn += int64(utxoAmount)
//Append ScriptPubKey to the list of scripts
utxoPkScript, err := hex.DecodeString(v.ScriptPubKey)
if err != nil {
return "", err
}
pkScripts = append(pkScripts, utxoPkScript)
//Append Transaction
redeemTx.AddTxIn(txIn)
//Once we gathered all the UTXOs we need, we stop
if amountIn >= amount {
break
}
}
var change int64 = amountIn - amount - gas*gasPrice
//Get address
addrPubKey, err := qtumsuite.NewAddressPubKey(wif.SerializePubKey(), &chaincfg.TestNet3Params)
//Generate PayToAddrScript for source address
changeScript, err := txscript.PayToAddrScript(addrPubKey)
if err != nil {
return "", err
}
changeTxOut := wire.NewTxOut(change, changeScript)
fromAddr, err := qtumsuite.DecodeAddress(from, &qtumTestNetParams)
if err != nil {
return "", err
}
//Generate PayToAddressScript
senderScript, err := txscript.PayToAddrScript(fromAddr)
if err != nil {
return "", err
}
senderTxOut := wire.NewTxOut(amount, senderScript)
redeemTx.AddTxOut(senderTxOut)
contractScript, err := ContractScript(redeemTx, wif, data, contractAddr, opcode)
if err != nil {
fmt.Println("Something went wrong with the contract script: ", err)
return "", err
}
//Build vouts
//Adding the destination address and the amount to the transaction as output
redeemTxOut := wire.NewTxOut(0, contractScript)
redeemTx.AddTxOut(redeemTxOut)
//Add change to tx out
redeemTx.AddTxOut(changeTxOut)
// Sign the Tx
finalRawTx, err := SignTx(redeemTx, pkScripts, wif)
return finalRawTx, nil
}
//Creates pubKeyScript of to create or call a contract with data depending on the byte used for opcode
// a 0xc2 byte (OP_CALL) will call a contract with the data, while a 0xc1 byte (OP_CREATE) will create
// a contract with the data
func ContractScript(redeemTx *wire.MsgTx, wif *qtumsuite.WIF, data []byte, contractAddr string, opcode byte) ([]byte, error) {
//Build scriptPubKey
scriptBuilder := txscript.NewScriptBuilder()
scriptBuilder.AddData([]byte{4}) //EVM Version
scriptBuilder.AddInt64(2500000) //gas limit
scriptBuilder.AddData([]byte{40}) //Gas price
scriptBuilder.AddData(data) //contract data
if opcode == OP_CALL {
hexContractAddr, err := hex.DecodeString(contractAddr)
if err != nil {
fmt.Println("odd length coming from data")
return []byte{0}, err
}
scriptBuilder.AddData(hexContractAddr)
}
//contract address
scriptBuilder.AddOp(opcode) // Add OP_CODE byte (0xc2 -> OP_CALL, 0xc1 -> OP_CREATE)
createScript, err := scriptBuilder.Script()
if err != nil {
return []byte{0}, err
}
return createScript, nil
}
func SignTx(redeemTx *wire.MsgTx, sourcePkScript [][]byte, wif *qtumsuite.WIF) (string, error) {
for i := range redeemTx.TxIn {
//Generate signature script
signatureHash, err := txscript.CalcSignatureHash(sourcePkScript[i], txscript.SigHashAll, redeemTx, i)
if err != nil {
return "", err
}
/*
Sometimes the signing process doesn't work
*/
privKey := secp256k1.PrivKeyFromBytes(wif.PrivKey.Serialize())
signature := ecdsa.Sign(privKey, signatureHash)
signatureScript, err := txscript.NewScriptBuilder().AddData(append(signature.Serialize(), byte(txscript.SigHashAll))).AddData(wif.SerializePubKey()).Script()
if err != nil {
return "", err
}
redeemTx.TxIn[i].SignatureScript = signatureScript
}
buf := bytes.NewBuffer(make([]byte, 0, redeemTx.SerializeSize()))
redeemTx.Serialize(buf)
hexSignedTx := hex.EncodeToString(buf.Bytes())
return hexSignedTx, nil
}