-
Notifications
You must be signed in to change notification settings - Fork 21
/
omniclient.go
806 lines (706 loc) · 24.8 KB
/
omniclient.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
package rpc
import (
"errors"
"github.com/shopspring/decimal"
"github.com/tidwall/gjson"
"log"
"obd/tool"
"strconv"
"strings"
)
//https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md
//Returns various state information of the client and protocol.
func (client *Client) OmniGetinfo() (result string, err error) {
return client.send("omni_getinfo", nil)
}
//Create and broadcast a simple send transaction.
func (client *Client) OmniSend(fromAddress string, toAddress string, propertyId int, amount float64) (result string, err error) {
_, err = client.ValidateAddress(fromAddress)
if err != nil {
return "", err
}
_, err = client.ValidateAddress(toAddress)
if err != nil {
return "", err
}
return client.send("omni_send", []interface{}{fromAddress, toAddress, propertyId, amount})
}
//Create new tokens with manageable supply. https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md#omni_sendissuancemanaged
func (client *Client) OmniSendIssuanceFixed(fromAddress string, ecosystem int, divisibleType int, name string, data string, amount float64) (result string, err error) {
if tool.CheckIsString(&fromAddress) == false {
return "", errors.New("error fromAddress")
}
if tool.CheckIsString(&name) == false {
return "", errors.New("error name")
}
if ecosystem != 1 && ecosystem != 2 {
return "", errors.New("error ecosystem")
}
if divisibleType != 1 && divisibleType != 2 {
return "", errors.New("error divisibleType")
}
if amount < 0 {
return "", errors.New("error amount")
}
amoutStr := strconv.FormatFloat(amount, 'f', 8, 64)
if divisibleType == 1 {
amoutStr = strconv.FormatFloat(amount, 'f', 0, 64)
}
if tool.CheckIsString(&data) == false {
data = ""
}
_, _ = client.ValidateAddress(fromAddress)
return client.send("omni_sendissuancefixed", []interface{}{fromAddress, ecosystem, divisibleType, 0, "", "", name, "", data, amoutStr})
}
//Create new tokens with manageable supply. https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md#omni_sendissuancemanaged
func (client *Client) OmniSendIssuanceManaged(fromAddress string, ecosystem int, divisibleType int, name string, data string) (result string, err error) {
if tool.CheckIsString(&fromAddress) == false {
return "", errors.New("error fromAddress")
}
if tool.CheckIsString(&name) == false {
return "", errors.New("error name")
}
if ecosystem != 1 && ecosystem != 2 {
return "", errors.New("error ecosystem")
}
if divisibleType != 1 && divisibleType != 2 {
return "", errors.New("error divisibleType")
}
if tool.CheckIsString(&data) == false {
data = ""
}
_, _ = client.ValidateAddress(fromAddress)
return client.send("omni_sendissuancemanaged", []interface{}{fromAddress, ecosystem, divisibleType, 0, "", "", name, "", data})
}
// Issue or grant new units of managed tokens. https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md#omni_sendgrant
func (client *Client) OmniSendGrant(fromAddress string, propertyId int64, amount float64, memo string) (result string, err error) {
if tool.CheckIsString(&fromAddress) == false {
return "", errors.New("error fromAddress")
}
if propertyId < 0 {
return "", errors.New("error propertyId")
}
s, err := client.OmniGetProperty(propertyId)
if err != nil {
return "", err
}
if amount < 0 {
return "", errors.New("error amout")
}
amoutStr := strconv.FormatFloat(amount, 'f', 0, 64)
divisible := gjson.Get(s, "divisible").Bool()
if divisible {
amoutStr = strconv.FormatFloat(amount, 'f', 8, 64)
}
if tool.CheckIsString(&memo) == false {
memo = ""
}
return client.send("omni_sendgrant", []interface{}{fromAddress, "", propertyId, amoutStr, memo})
}
// Revoke units of managed tokens. https://github.com/OmniLayer/omnicore/blob/master/src/omnicore/doc/rpc-api.md#omni_sendrevoke
func (client *Client) OmniSendRevoke(fromAddress string, propertyId int64, amount float64, memo string) (result string, err error) {
if tool.CheckIsString(&fromAddress) == false {
return "", errors.New("error fromAddress")
}
if propertyId < 0 {
return "", errors.New("error propertyId")
}
if amount < 0 {
return "", errors.New("error amout")
}
s, err := client.OmniGetProperty(propertyId)
if err != nil {
return "", err
}
amoutStr := strconv.FormatFloat(amount, 'f', 0, 64)
divisible := gjson.Get(s, "divisible").Bool()
if divisible {
amoutStr = strconv.FormatFloat(amount, 'f', 8, 64)
}
if tool.CheckIsString(&memo) == false {
memo = ""
}
_, _ = client.ValidateAddress(fromAddress)
return client.send("omni_sendrevoke", []interface{}{fromAddress, propertyId, amoutStr, memo})
}
func (client *Client) OmniGetbalance(address string, propertyId int) (result string, err error) {
_, err = client.ValidateAddress(address)
if err != nil {
return "", err
}
return client.send("omni_getbalance", []interface{}{address, propertyId})
}
//Get detailed information about an Omni transaction.
func (client *Client) OmniGettransaction(txid string) (result string, err error) {
return client.send("omni_gettransaction", []interface{}{txid})
}
//Get detailed information about an Omni transaction.
func (client *Client) OmniGetProperty(propertyId int64) (result string, err error) {
return client.send("omni_getproperty", []interface{}{propertyId})
}
//Get detailed information about an Omni transaction.
func (client *Client) OmniListProperties() (result string, err error) {
return client.send("omni_listproperties", []interface{}{})
}
func (client *Client) OmniGetAllBalancesForAddress(address string) (result string, err error) {
_, err = client.ValidateAddress(address)
if err != nil {
return "", err
}
return client.send("omni_getallbalancesforaddress ", []interface{}{address})
}
//List wallet transactions, optionally filtered by an address and block boundaries.
func (client *Client) OmniListTransactions(count int, skip int) (result string, err error) {
if count < 0 {
count = 10
}
if skip < 0 {
skip = 0
}
return client.send("omni_listtransactions", []interface{}{"*", count, skip})
}
func (client *Client) omniCreatePayloadSimpleSend(propertyId int64, amount float64) (result string, err error) {
return client.send("omni_createpayload_simplesend", []interface{}{propertyId, decimal.NewFromFloat(amount).String()})
}
func (client *Client) omniCreateRawtxOpreturn(rawtx string, payload string) (result string, err error) {
return client.send("omni_createrawtx_opreturn", []interface{}{rawtx, payload})
}
func (client *Client) omniCreateRawtxChange(rawtx string, prevtxs []map[string]interface{}, destination string, fee float64) (result string, err error) {
return client.send("omni_createrawtx_change", []interface{}{rawtx, prevtxs, destination, fee})
}
func (client *Client) omniCreateRawtxReference(rawtx string, destination string) (result string, err error) {
return client.send("omni_createrawtx_reference", []interface{}{rawtx, destination})
}
func (client *Client) OmniRawTransaction(fromBitCoinAddress string, privkeys []string, toBitCoinAddress string, propertyId int64, amount float64, minerFee float64, sequence int) (txid string, err error) {
_, hex, err := client.OmniCreateAndSignRawTransaction(fromBitCoinAddress, privkeys, toBitCoinAddress, propertyId, amount, minerFee, sequence)
if err != nil {
return "", err
}
result, err := client.OmniDecodeTransaction(hex)
if err == nil {
log.Println(result)
} else {
log.Println(err)
}
//8 send
txid, err = client.SendRawTransaction(hex)
if err != nil {
return "", err
}
log.Println("8 send", txid)
return txid, nil
}
func (client *Client) OmniCreateAndSignRawTransaction(fromBitCoinAddress string, privkeys []string, toBitCoinAddress string, propertyId int64, amount float64, minerFee float64, sequence int) (txid, hex string, err error) {
if tool.CheckIsString(&fromBitCoinAddress) == false {
return "", "", errors.New("fromBitCoinAddress is empty")
}
if tool.CheckIsString(&toBitCoinAddress) == false {
return "", "", errors.New("toBitCoinAddress is empty")
}
if amount < GetOmniDustBtc() {
return "", "", errors.New("wrong amount")
}
pMoney := GetOmniDustBtc()
if minerFee < GetOmniDustBtc() {
minerFee = 0.00003
}
_, _ = client.ValidateAddress(fromBitCoinAddress)
resultListUnspent, err := client.ListUnspent(fromBitCoinAddress)
if err != nil {
return "", "", err
}
arrayListUnspent := gjson.Parse(resultListUnspent).Array()
if len(arrayListUnspent) == 0 {
return "", "", errors.New("empty balance")
}
log.Println("listunspent", arrayListUnspent)
out, _ := decimal.NewFromFloat(minerFee).Add(decimal.NewFromFloat(pMoney)).Float64()
balance := 0.0
for _, item := range arrayListUnspent {
balance, _ = decimal.NewFromFloat(balance).Add(decimal.NewFromFloat(item.Get("amount").Float())).Float64()
if balance >= out {
break
}
}
log.Println("1 balance", balance)
if balance < out {
return "", "", errors.New("not enough balance")
}
//2.Omni_createpayload_simplesend
payload, err := client.omniCreatePayloadSimpleSend(propertyId, amount)
if err != nil {
return "", "", err
}
log.Println("2 payload " + payload)
inputs := make([]map[string]interface{}, 0, len(arrayListUnspent))
for _, item := range arrayListUnspent {
node := make(map[string]interface{})
node["txid"] = item.Get("txid").String()
node["vout"] = item.Get("vout").Int()
node["confirmations"] = item.Get("confirmations").Int()
node["spendable"] = item.Get("spendable").Bool()
node["solvable"] = item.Get("solvable").Bool()
node["amount"] = item.Get("amount").Float()
node["address"] = item.Get("address").String()
node["account"] = item.Get("account").String()
node["scriptPubKey"] = item.Get("scriptPubKey").String()
inputs = append(inputs, node)
}
outputs := make(map[string]interface{})
//outputs["data"]="e4bda0e5a5bdefbc8ce4b896e7958ce38082"
//3.CreateRawTransaction
createrawtransactionStr, err := client.CreateRawTransaction(inputs, outputs)
if err != nil {
return "", "", err
}
log.Println("3 createrawtransactionStr", createrawtransactionStr)
//4.Omni_createrawtx_opreturn
opreturn, err := client.omniCreateRawtxOpreturn(createrawtransactionStr, payload)
if err != nil {
return "", "", err
}
log.Println("4 opreturn", opreturn)
//5. Omni_createrawtx_reference
reference, err := client.omniCreateRawtxReference(opreturn, toBitCoinAddress)
if err != nil {
return "", "", err
}
log.Println("5 reference", reference)
//6.Omni_createrawtx_change
prevtxs := make([]map[string]interface{}, 0, len(arrayListUnspent))
for _, item := range arrayListUnspent {
node := make(map[string]interface{})
node["txid"] = item.Get("txid").String()
node["vout"] = item.Get("vout").Int()
node["scriptPubKey"] = item.Get("scriptPubKey").String()
node["value"] = item.Get("amount").Float()
prevtxs = append(prevtxs, node)
}
change, err := client.omniCreateRawtxChange(reference, prevtxs, fromBitCoinAddress, minerFee)
if err != nil {
return "", "", err
}
log.Println("6 change", change)
if privkeys == nil || len(privkeys) == 0 {
privkeys = nil
}
//7 sign
signHex, err := client.SignRawTransactionWithKey(change, privkeys, inputs, "ALL")
if err != nil {
return "", "", err
}
hex = gjson.Get(signHex, "hex").String()
log.Println("7 SignRawTransactionWithKey", hex)
decodeHex, _ := client.DecodeRawTransaction(hex)
log.Println("7 DecodeSignRawTransactionWithKey", decodeHex)
txid = gjson.Get(decodeHex, "txid").String()
result, err := client.OmniDecodeTransaction(hex)
if err == nil {
log.Println(result)
} else {
log.Println(err)
}
return txid, hex, nil
}
// From channelAddress to temp multi address, to Create CommitmentTx
func (client *Client) OmniCreateAndSignRawTransactionUseSingleInput(txType int, fromBitCoinAddress string, privkeys []string, toBitCoinAddress string, propertyId int64, amount float64, minerFee float64, sequence int, redeemScript *string, usedTxid string) (txid, hex string, currUseTxid string, err error) {
if tool.CheckIsString(&fromBitCoinAddress) == false {
return "", "", "", errors.New("fromBitCoinAddress is empty")
}
if tool.CheckIsString(&toBitCoinAddress) == false {
return "", "", "", errors.New("toBitCoinAddress is empty")
}
if amount < GetOmniDustBtc() {
return "", "", "", errors.New("wrong amount")
}
pMoney := GetOmniDustBtc()
if minerFee < GetOmniDustBtc() {
minerFee = GetMinerFee()
}
out, _ := decimal.NewFromFloat(pMoney).
Add(decimal.NewFromFloat(minerFee)).
Float64()
_, _ = client.ValidateAddress(fromBitCoinAddress)
_, _ = client.ValidateAddress(toBitCoinAddress)
resultListUnspent, err := client.ListUnspent(fromBitCoinAddress)
if err != nil {
return "", "", "", err
}
arrayListUnspent := gjson.Parse(resultListUnspent).Array()
log.Println("listunspent", arrayListUnspent)
inputCount := 3 + txType
if len(arrayListUnspent) < inputCount {
return "", "", "", errors.New("wrong input num, need " + strconv.Itoa(inputCount) + " input:one for omni token, " + strconv.Itoa(inputCount-1) + " btc inputs for miner fee ")
}
balance := 0.0
inputs := make([]map[string]interface{}, 0, 0)
currUseTxid = ""
for _, item := range arrayListUnspent {
currUseTxid = item.Get("txid").String()
if usedTxid != "" && strings.Contains(usedTxid, currUseTxid) {
continue
}
inputAmount := item.Get("amount").Float()
if inputAmount >= out {
node := make(map[string]interface{})
node["txid"] = item.Get("txid").String()
node["vout"] = item.Get("vout").Int()
node["amount"] = inputAmount
node["scriptPubKey"] = item.Get("scriptPubKey").String()
if redeemScript != nil {
node["redeemScript"] = *redeemScript
}
balance, _ = decimal.NewFromFloat(balance).Add(decimal.NewFromFloat(node["amount"].(float64))).Float64()
inputs = append(inputs, node)
break
}
}
if currUseTxid == "" {
return "", "", "", errors.New("not found the miner fee input")
}
log.Println("1 balance", balance)
if balance < out {
return "", "", "", errors.New("not enough balance")
}
//2.Omni_createpayload_simplesend
payload, err := client.omniCreatePayloadSimpleSend(propertyId, amount)
if err != nil {
return "", "", "", err
}
log.Println("2 payload " + payload)
outputs := make(map[string]interface{})
//3.CreateRawTransaction
createrawtransactionStr, err := client.CreateRawTransaction(inputs, outputs)
if err != nil {
return "", "", "", err
}
log.Println("3 createrawtransactionStr", createrawtransactionStr)
//4.Omni_createrawtx_opreturn
opreturn, err := client.omniCreateRawtxOpreturn(createrawtransactionStr, payload)
if err != nil {
return "", "", "", err
}
log.Println("4 opreturn", opreturn)
//5. Omni_createrawtx_reference
reference, err := client.omniCreateRawtxReference(opreturn, toBitCoinAddress)
if err != nil {
return "", "", "", err
}
log.Println("5 reference", reference)
//6.Omni_createrawtx_change
prevtxs := make([]map[string]interface{}, 0, len(inputs))
for _, item := range inputs {
node := make(map[string]interface{})
node["txid"] = item["txid"]
node["vout"] = item["vout"]
node["scriptPubKey"] = item["scriptPubKey"]
node["value"] = item["amount"]
if redeemScript != nil {
node["redeemScript"] = *redeemScript
}
prevtxs = append(prevtxs, node)
}
change, err := client.omniCreateRawtxChange(reference, prevtxs, toBitCoinAddress, minerFee)
if err != nil {
return "", "", "", err
}
log.Println("6 change", change)
if privkeys == nil || len(privkeys) == 0 {
privkeys = nil
}
//7 sign
signHex, err := client.SignRawTransactionWithKey(change, privkeys, inputs, "ALL")
if err != nil {
return "", "", "", err
}
hex = gjson.Get(signHex, "hex").String()
log.Println("7 SignRawTransactionWithKey", hex)
decodeHex, _ := client.DecodeRawTransaction(hex)
log.Println("7 DecodeSignRawTransactionWithKey", decodeHex)
txid = gjson.Get(decodeHex, "txid").String()
result, err := client.OmniDecodeTransaction(hex)
if err == nil {
log.Println(result)
} else {
log.Println(err)
}
return txid, hex, currUseTxid, nil
}
func (client *Client) OmniCreateAndSignRawTransactionUseRestInput(txType int, fromBitCoinAddress string, usedTxid string, privkeys []string, toBitCoinAddress, changeToAddress string, propertyId int64, amount float64, minerFee float64, sequence int, redeemScript *string) (txid, hex string, err error) {
if tool.CheckIsString(&fromBitCoinAddress) == false {
return "", "", errors.New("fromBitCoinAddress is empty")
}
if tool.CheckIsString(&toBitCoinAddress) == false {
return "", "", errors.New("toBitCoinAddress is empty")
}
if amount < GetOmniDustBtc() {
return "", "", errors.New("wrong amount")
}
pMoney := GetOmniDustBtc()
if minerFee < GetOmniDustBtc() {
minerFee = GetMinerFee()
}
out, _ := decimal.NewFromFloat(minerFee).
Add(decimal.NewFromFloat(pMoney)).
Float64()
_, _ = client.ValidateAddress(fromBitCoinAddress)
_, _ = client.ValidateAddress(toBitCoinAddress)
resultListUnspent, err := client.ListUnspent(fromBitCoinAddress)
if err != nil {
return "", "", err
}
arrayListUnspent := gjson.Parse(resultListUnspent).Array()
log.Println("listunspent", arrayListUnspent)
inputCount := 3 + txType
if len(arrayListUnspent) < inputCount {
return "", "", errors.New("wrong input num, need " + strconv.Itoa(inputCount) + " input:one for omni token, " + strconv.Itoa(inputCount-1) + " btc inputs for miner fee ")
}
inputs := make([]map[string]interface{}, 0, 0)
for _, item := range arrayListUnspent {
txid := item.Get("txid").String()
if usedTxid != "" && strings.Contains(usedTxid, txid) == false {
node := make(map[string]interface{})
node["txid"] = txid
node["vout"] = item.Get("vout").Int()
node["amount"] = item.Get("amount").Float()
node["scriptPubKey"] = item.Get("scriptPubKey").String()
if redeemScript != nil {
node["redeemScript"] = *redeemScript
}
inputs = append(inputs, node)
}
}
balance := 0.0
for _, item := range inputs {
balance, _ = decimal.NewFromFloat(balance).Add(decimal.NewFromFloat(item["amount"].(float64))).Float64()
if balance >= out {
break
}
}
log.Println("1 balance")
if balance < out {
return "", "", errors.New("not enough balance")
}
//2.Omni_createpayload_simplesend
payload, err := client.omniCreatePayloadSimpleSend(propertyId, amount)
if err != nil {
return "", "", err
}
log.Println("2 payload ")
outputs := make(map[string]interface{})
//3.CreateRawTransaction
createrawtransactionStr, err := client.CreateRawTransaction(inputs, outputs)
if err != nil {
return "", "", err
}
log.Println("3 createrawtransactionStr", createrawtransactionStr)
//4.Omni_createrawtx_opreturn
opreturn, err := client.omniCreateRawtxOpreturn(createrawtransactionStr, payload)
if err != nil {
return "", "", err
}
log.Println("4 opreturn")
//5. Omni_createrawtx_reference
reference, err := client.omniCreateRawtxReference(opreturn, toBitCoinAddress)
if err != nil {
return "", "", err
}
log.Println("5 reference")
//6.Omni_createrawtx_change
prevtxs := make([]map[string]interface{}, 0, len(inputs))
for _, item := range inputs {
node := make(map[string]interface{})
node["txid"] = item["txid"]
node["vout"] = item["vout"]
node["scriptPubKey"] = item["scriptPubKey"]
node["value"] = item["amount"]
if redeemScript != nil {
node["redeemScript"] = *redeemScript
}
prevtxs = append(prevtxs, node)
}
change, err := client.omniCreateRawtxChange(reference, prevtxs, changeToAddress, minerFee)
if err != nil {
return "", "", err
}
log.Println("6 change")
if privkeys == nil || len(privkeys) == 0 {
privkeys = nil
}
//7 sign
signHex, err := client.SignRawTransactionWithKey(change, privkeys, inputs, "ALL")
if err != nil {
return "", "", err
}
hex = gjson.Get(signHex, "hex").String()
log.Println("7 SignRawTransactionWithKey")
decodeHex, _ := client.DecodeRawTransaction(hex)
//log.Println("7 DecodeSignRawTransactionWithKey", decodeHex)
txid = gjson.Get(decodeHex, "txid").String()
result, err := client.OmniDecodeTransaction(hex)
if err == nil {
log.Println(result)
} else {
log.Println(err)
}
return txid, hex, nil
}
func (client *Client) OmniCreateAndSignRawTransactionUseUnsendInput(fromBitCoinAddress string, privkeys []string, inputItems []TransactionInputItem, toBitCoinAddress, changeToAddress string, propertyId int64, amount float64, minerFee float64, sequence int, redeemScript *string) (txid string, hex string, err error) {
if tool.CheckIsString(&fromBitCoinAddress) == false {
return "", "", errors.New("fromBitCoinAddress is empty")
}
if tool.CheckIsString(&toBitCoinAddress) == false {
return "", "", errors.New("toBitCoinAddress is empty")
}
if len(inputItems) == 0 {
return "", "", errors.New("inputItems is empty")
}
if amount < GetOmniDustBtc() {
return "", "", errors.New("wrong amount")
}
pMoney := GetOmniDustBtc()
if minerFee < GetOmniDustBtc() {
minerFee = GetMinerFee()
}
_, _ = client.ValidateAddress(fromBitCoinAddress)
_, _ = client.ValidateAddress(toBitCoinAddress)
_, _ = client.ValidateAddress(changeToAddress)
inputs := make([]map[string]interface{}, 0, 0)
for _, item := range inputItems {
node := make(map[string]interface{})
node["txid"] = item.Txid
node["vout"] = item.Vout
node["amount"] = item.Amount
node["scriptPubKey"] = item.ScriptPubKey
if sequence > 0 {
node["sequence"] = sequence
}
if redeemScript != nil {
node["redeemScript"] = *redeemScript
}
inputs = append(inputs, node)
}
out, _ := decimal.NewFromFloat(minerFee).Add(decimal.NewFromFloat(pMoney)).Float64()
balance := 0.0
for _, item := range inputs {
balance, _ = decimal.NewFromFloat(balance).Add(decimal.NewFromFloat(item["amount"].(float64))).Float64()
if balance >= out {
break
}
}
log.Println("1 balance", balance)
if balance < out {
return "", "", errors.New("not enough balance")
}
//2.Omni_createpayload_simplesend
payload, err := client.omniCreatePayloadSimpleSend(propertyId, amount)
if err != nil {
return "", "", err
}
//log.Println("2 payload " + payload)
//3.CreateRawTransaction
outputs := make(map[string]interface{})
//outputs["data"] = "e4bda0e5a5bdefbc8ce4b896e7958ce38082"
createrawtransactionStr, err := client.CreateRawTransaction(inputs, outputs)
if err != nil {
return "", "", err
}
log.Println("3 createrawtransactionStr", createrawtransactionStr)
result, err := client.DecodeRawTransaction(createrawtransactionStr)
if err != nil {
return "", "", err
}
log.Println(result)
//4.Omni_createrawtx_opreturn
opreturn, err := client.omniCreateRawtxOpreturn(createrawtransactionStr, payload)
if err != nil {
return "", "", err
}
//log.Println("4 opreturn", opreturn)
//5. Omni_createrawtx_reference
reference, err := client.omniCreateRawtxReference(opreturn, toBitCoinAddress)
if err != nil {
return "", "", err
}
//log.Println("5 reference", reference)
result, err = client.DecodeRawTransaction(reference)
if err != nil {
return "", "", err
}
//log.Println(result)
//6.Omni_createrawtx_change
prevtxs := make([]map[string]interface{}, 0, 0)
for _, item := range inputItems {
node := make(map[string]interface{})
node["txid"] = item.Txid
node["vout"] = item.Vout
node["scriptPubKey"] = item.ScriptPubKey
node["value"] = item.Amount
if redeemScript != nil {
node["redeemScript"] = *redeemScript
}
prevtxs = append(prevtxs, node)
}
change, err := client.omniCreateRawtxChange(reference, prevtxs, changeToAddress, minerFee)
if err != nil {
return "", "", err
}
//log.Println("6 change", change)
result, err = client.DecodeRawTransaction(change)
if err != nil {
return "", "", err
}
log.Println(result)
if privkeys == nil || len(privkeys) == 0 {
privkeys = nil
}
//7 sign
signHex, err := client.SignRawTransactionWithKey(change, privkeys, inputs, "ALL")
if err != nil {
return "", "", err
}
hex = gjson.Get(signHex, "hex").String()
//log.Println("7 SignRawTransactionWithKey", hex)
decodeHex, _ := client.DecodeRawTransaction(hex)
log.Println("7 DecodeSignRawTransactionWithKey", decodeHex)
txid = gjson.Get(decodeHex, "txid").String()
result, err = client.OmniDecodeTransaction(hex)
if err != nil {
log.Println(err)
}
//log.Println(result)
return txid, hex, nil
}
func (client *Client) OmniSignRawTransactionForUnsend(hex string, inputItems []TransactionInputItem, privKey string) (string, string, error) {
var inputs []map[string]interface{}
for _, item := range inputItems {
node := make(map[string]interface{})
node["txid"] = item.Txid
node["vout"] = item.Vout
node["amount"] = item.Amount
node["scriptPubKey"] = item.ScriptPubKey
inputs = append(inputs, node)
}
signHex, err := client.SignRawTransactionWithKey(hex, []string{privKey}, inputs, "ALL")
if err != nil {
return "", "", err
}
hex = gjson.Get(signHex, "hex").String()
decodeHex, err := client.DecodeRawTransaction(hex)
if err == nil {
log.Println(decodeHex)
} else {
log.Println(err)
}
txId := gjson.Get(decodeHex, "txid").String()
if err != nil {
return "", hex, err
}
result, err := client.OmniDecodeTransaction(hex)
if err == nil {
log.Println(result)
} else {
log.Println(err)
}
return txId, hex, nil
}