-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcfdgo_api.go
7296 lines (6583 loc) · 239 KB
/
cfdgo_api.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package cfdgo
import (
"encoding/hex"
"fmt"
"strconv"
"strings"
"unsafe"
)
/**
* Convert return code to golang built-in error struct.
* param: retCode return code from cfd
* return: err built-in error struct.
*/
func ConvertCfdErrorCode(retCode int) (err error) {
switch retCode {
case (int)(KCfdSuccess):
return nil
case (int)(KCfdUnknownError):
err = fmt.Errorf("CFD Error: Unknown error occered.: errorCode=[%d]", retCode)
case (int)(KCfdInternalError):
err = fmt.Errorf("CFD Error: Internal error occered.: errorCode=[%d]", retCode)
case (int)(KCfdMemoryFullError):
err = fmt.Errorf("CFD Error: Memory is full.: errorCode=[%d]", retCode)
case (int)(KCfdIllegalArgumentError):
err = fmt.Errorf("CFD Error: Illegal argument passed.: errorCode=[%d]", retCode)
case (int)(KCfdIllegalStateError):
err = fmt.Errorf("CFD Error: Illegal state api call.: errorCode=[%d]", retCode)
case (int)(KCfdOutOfRangeError):
err = fmt.Errorf("CFD Error: Out of range access occered.: errorCode=[%d]", retCode)
case (int)(KCfdInvalidSettingError):
err = fmt.Errorf("CFD Error: Invalid setting api call.: errorCode=[%d]", retCode)
case (int)(KCfdConnectionError):
err = fmt.Errorf("CFD Error: Connection error occered.: errorCode=[%d]", retCode)
case (int)(KCfdDiskAccessError):
err = fmt.Errorf("CFD Error: Disk access error occered.: errorCode=[%d]", retCode)
case (int)(KCfdNotFoundError):
err = fmt.Errorf("CFD Error: NotFound error occered.: errorCode=[%d]", retCode)
}
return
}
/**
* Convert handle and error code to Golang built-in error.
* detail: if handle is nil, return fixed message by the error code.
* param: retCode cfd return code.
* param: handle cfd handle.
* return: err built-in error struct.
*/
func convertCfdError(retCode int, handle uintptr) (err error) {
if retCode == (int)(KCfdSuccess) {
return
}
var errorMsg string
if handle == uintptr(0) {
err = ConvertCfdErrorCode(retCode)
} else if ret := CfdGetLastErrorMessage(handle, &errorMsg); ret != (int)(KCfdSuccess) {
err = ConvertCfdErrorCode(retCode)
} else {
err = fmt.Errorf("CFD Error: message=[%s], code=[%d]", errorMsg, retCode)
}
return
}
/**
* Get supported function.
* return: funcFlag function flag.
* return: err error struct
*/
func CfdGoGetSupportedFunction() (funcFlag uint64, err error) {
funcFlagValue := SwigcptrUint64_t(uintptr(unsafe.Pointer(&funcFlag)))
ret := CfdGetSupportedFunction(funcFlagValue)
err = convertCfdError(ret, uintptr(0))
return funcFlag, err
}
/**
* Create cfd handle.
* return: handle cfd handle. release: CfdGoFreeHandle
* return: err error struct
*/
func CfdGoCreateHandle() (handle uintptr, err error) {
ret := CfdCreateSimpleHandle(&handle)
err = convertCfdError(ret, handle)
return handle, err
}
/**
* Free cfd handle.
* param: handle cfd handle
* return: err error struct
*/
func CfdGoFreeHandle(handle uintptr) (err error) {
ret := CfdFreeHandle(handle)
err = convertCfdError(ret, uintptr(0))
return
}
/**
* Create Address.
* param: hashType hash type (p2pkh, p2sh, etc...)
* param: pubkey pubkey (pubkey hash only)
* param: redeemScript redeem script (script hash only)
* param: networkType network type
* return: address address string
* return: lockingScript locking script
* return: p2shSegwitLockingScript p2sh-segwit witness program
* return: err error
*/
func CfdGoCreateAddress(hashType int, pubkey string, redeemScript string, networkType int) (address string, lockingScript string, p2shSegwitLockingScript string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
ret := CfdCreateAddress(handle, hashType, pubkey, redeemScript, networkType, &address, &lockingScript, &p2shSegwitLockingScript)
err = convertCfdError(ret, handle)
return address, lockingScript, p2shSegwitLockingScript, err
}
/**
* Create multisig script and address.
* param: networkType network type
* param: hashType hash type (p2sh, p2wsh, etc...)
* param: pubkeys pubkey list (max 15 key)
* param: requireNum pubkey require signature
* return: address address string
* return: redeemScript redeem script
* return: witnessScript witness script
* return: err error
*/
func CfdGoCreateMultisigScript(networkType int, hashType int, pubkeys []string, requireNum uint32) (address string, redeemScript string, witnessScript string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
var multisigHandle uintptr
ret := CfdInitializeMultisigScript(handle, networkType, hashType, &multisigHandle)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
defer CfdFreeMultisigScriptHandle(handle, multisigHandle)
for i := 0; i < len(pubkeys); i++ {
ret = CfdAddMultisigScriptData(handle, multisigHandle, pubkeys[i])
if ret != (int)(KCfdSuccess) {
break
}
}
if ret == (int)(KCfdSuccess) {
reqNumPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&requireNum)))
ret = CfdFinalizeMultisigScript(handle, multisigHandle, reqNumPtr, &address, &redeemScript, &witnessScript)
}
if ret == (int)(KCfdSuccess) {
return address, redeemScript, witnessScript, err
} else {
err = convertCfdError(ret, handle)
return "", "", "", err
}
}
/**
* Descriptor data struct.
*/
type CfdDescriptorData struct {
// depth (0 - )
Depth uint32
// script type. (CfdDescriptorScriptType)
ScriptType int
// locking script.
LockingScript string
// address string. (for ScriptType not KCfdDescriptorScriptRaw)
Address string
// hash type. (CfdHashType)
HashType int
// redeem script. (for ScriptType KCfdDescriptorScriptSh or KCfdDescriptorScriptWsh)
RedeemScript string
// key type. (see CfdDescriptorKeyData.KeyType)
KeyType int
// pubkey
Pubkey string
// extend pubkey
ExtPubkey string
// extend privkey
ExtPrivkey string
// schnorr pubkey
SchnorrPubkey string
// has multisig
IsMultisig bool
// number of multisig require signatures
ReqSigNum uint32
// Taproot ScriptTree string
TreeString string
}
/**
* Descriptor key data struct.
*/
type CfdDescriptorKeyData struct {
// key type. (CfdDescriptorKeyType)
// - KCfdDescriptorKeyNull
// - KCfdDescriptorKeyPublic
// - KCfdDescriptorKeyBip32
// - KCfdDescriptorKeyBip32Priv
// - KCfdDescriptorKeySchnorr
KeyType int
// pubkey
Pubkey string
// extend pubkey
ExtPubkey string
// extend privkey
ExtPrivkey string
// schnorr pubkey
SchnorrPubkey string
}
/**
* Parse Output Descriptor.
* param: descriptor output descriptor
* param: networkType network type
* param: bip32DerivationPath derive path
* return: data descriptor data
* return: descriptorDataList descriptor data struct list
* return: multisigList multisig key struct list
* return: err error
*/
func CfdGoParseDescriptorData(descriptor string, networkType int, bip32DerivationPath string) (data CfdDescriptorData, descriptorDataList []CfdDescriptorData, multisigList []CfdDescriptorKeyData, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
var descriptorHandle uintptr
var maxIndex uint32
maxIndexPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&maxIndex)))
ret := CfdParseDescriptor(handle, descriptor, networkType, bip32DerivationPath, &descriptorHandle, maxIndexPtr)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
defer CfdFreeDescriptorHandle(handle, descriptorHandle)
var maxMultisigKeyNum uint32
lastMultisigFlag := false
keyNumPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&maxMultisigKeyNum)))
{
reqSigNumPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&(data.ReqSigNum))))
ret = CfdGetDescriptorRootData(handle, descriptorHandle,
&data.ScriptType, &data.LockingScript,
&data.Address, &data.HashType, &data.RedeemScript,
&data.KeyType, &data.Pubkey, &data.ExtPubkey, &data.ExtPrivkey,
&data.SchnorrPubkey, &data.TreeString,
&data.IsMultisig, keyNumPtr, reqSigNumPtr)
}
if ret == (int)(KCfdSuccess) {
descriptorDataList = make([]CfdDescriptorData, maxIndex+1)
for i := uint32(0); i <= maxIndex; i++ {
var tempData CfdDescriptorData
var maxNum uint32
maxNumPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&maxNum)))
depthPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&(tempData.Depth))))
index := SwigcptrUint32_t(uintptr(unsafe.Pointer(&i)))
reqSigNumPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&(tempData.ReqSigNum))))
ret = CfdGetDescriptorData(handle, descriptorHandle, index, maxNumPtr,
depthPtr, &tempData.ScriptType, &tempData.LockingScript,
&tempData.Address, &tempData.HashType, &tempData.RedeemScript,
&tempData.KeyType, &tempData.Pubkey, &tempData.ExtPubkey, &tempData.ExtPrivkey,
&tempData.IsMultisig, keyNumPtr, reqSigNumPtr)
if ret != (int)(KCfdSuccess) {
break
}
if tempData.KeyType == (int)(KCfdDescriptorKeySchnorr) {
tempData.SchnorrPubkey = tempData.Pubkey
tempData.Pubkey = ""
}
descriptorDataList[i] = tempData
lastMultisigFlag = tempData.IsMultisig
}
}
if lastMultisigFlag && (ret == (int)(KCfdSuccess)) {
multisigList = make([]CfdDescriptorKeyData, maxMultisigKeyNum)
for i := uint32(0); i < maxMultisigKeyNum; i++ {
var keyData CfdDescriptorKeyData
index := SwigcptrUint32_t(uintptr(unsafe.Pointer(&i)))
ret = CfdGetDescriptorMultisigKey(handle, descriptorHandle,
index, &keyData.KeyType, &keyData.Pubkey,
&keyData.ExtPubkey, &keyData.ExtPrivkey)
if ret != (int)(KCfdSuccess) {
break
}
multisigList[i] = keyData
}
}
if ret == (int)(KCfdSuccess) {
return data, descriptorDataList, multisigList, err
} else {
err = convertCfdError(ret, handle)
return data, []CfdDescriptorData{}, []CfdDescriptorKeyData{}, err
}
}
/**
* Parse Output Descriptor.
* param: descriptor output descriptor
* param: networkType network type
* param: bip32DerivationPath derive path
* return: descriptorDataList descriptor data struct list
* return: multisigList multisig key struct list
* return: err error
*/
func CfdGoParseDescriptor(descriptor string, networkType int, bip32DerivationPath string) (descriptorDataList []CfdDescriptorData, multisigList []CfdDescriptorKeyData, err error) {
_, descriptorDataList, multisigList, err = CfdGoParseDescriptorData(descriptor, networkType, bip32DerivationPath)
return descriptorDataList, multisigList, err
}
/**
* Get outputDescriptor's checksum.
* param: networkType network type
* param: descriptor descriptor.
* return: descriptorAddedChecksum descriptor added checksum.
* return: err error
*/
func CfdGoGetDescriptorChecksum(networkType int, descriptor string) (descriptorAddedChecksum string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
ret := CfdGetDescriptorChecksum(handle, networkType, descriptor, &descriptorAddedChecksum)
err = convertCfdError(ret, handle)
return descriptorAddedChecksum, err
}
/**
* Get multisig pubkeys address.
* param: redeemScript multisig script
* param: networkType network type
* param: hashType hash type (p2sh, p2wsh, etc...)
* return: addressList address list
* return: pubkeyList pubkey list
* return: err error
*/
func CfdGoGetAddressesFromMultisig(redeemScript string, networkType int, hashType int) (addressList []string, pubkeyList []string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
var multisigHandle uintptr
var maxKeyNum uint32
maxKeyNumPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&maxKeyNum)))
ret := CfdGetAddressesFromMultisig(handle, redeemScript, networkType,
hashType, &multisigHandle, maxKeyNumPtr)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
defer CfdFreeAddressesMultisigHandle(handle, multisigHandle)
addressList = make([]string, maxKeyNum)
pubkeyList = make([]string, maxKeyNum)
for i := uint32(0); i < maxKeyNum; i++ {
var pubkey string
var address string
index := SwigcptrUint32_t(uintptr(unsafe.Pointer(&i)))
ret = CfdGetAddressFromMultisigKey(handle, multisigHandle,
index, &address, &pubkey)
if ret != (int)(KCfdSuccess) {
break
}
addressList[i] = address
pubkeyList[i] = pubkey
}
if ret == (int)(KCfdSuccess) {
return addressList, pubkeyList, err
} else {
err = convertCfdError(ret, handle)
return []string{}, []string{}, err
}
}
/**
* Get address from locking script.
* param: lockingScript locking script
* param: networkType network type
* param: hashType hash type (p2sh, p2wsh, etc...)
* return: address address
* return: err error
*/
func CfdGoGetAddressFromLockingScript(lockingScript string, networkType int) (address string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
ret := CfdGetAddressFromLockingScript(handle, lockingScript, networkType, &address)
err = convertCfdError(ret, handle)
return address, err
}
/**
* Address information struct.
*/
type CfdAddressInfo struct {
// address
Address string
// network type
NetworkType int
// hash type
HashType int
// witness version (unuse: -1)
WitnessVersion int
// locking script
LockingScript string
// hash
Hash string
}
/**
* Get address information.
* param: address address string
* return: data address data (CfdAddressInfo)
* return: err error
*/
func CfdGoGetAddressInfo(address string) (data CfdAddressInfo, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
ret := CfdGetAddressInfo(handle, address, &data.NetworkType, &data.HashType, &data.WitnessVersion, &data.LockingScript, &data.Hash)
err = convertCfdError(ret, handle)
if err == nil {
data.Address = address
if data.WitnessVersion > 2147483647 {
data.WitnessVersion = (int)(KCfdWitnessVersionNone)
}
}
return data, err
}
/**
* UTXO struct.
*/
type CfdUtxo struct {
// utxo txid
Txid string
// utxo vout
Vout uint32
// amount
Amount int64
// asset
Asset string
// output descriptor
Descriptor string
// is issuance output
IsIssuance bool
// is blind issuance output
IsBlindIssuance bool
// is peg-in output
IsPegin bool
// claim script hex (require when IsPegin is true)
ClaimScript string
// peg-in bitcoin tx size (require when IsPegin is true)
PeginBtcTxSize uint32
// peg-in bitcoin txoutproof size (require when IsPegin is true)
PeginTxOutProofSize uint32
// scriptsig template hex (require script hash estimate fee)
ScriptSigTemplate string
// amount commitment hex
AmountCommitment string
}
/**
* Selection target amount struct.
*/
type CfdTargetAmount struct {
// Amount more than the specified amount is set in txout. default is 0 (disable).
Amount int64
// asset
Asset string
}
/**
* CoinSelection option data struct.
*/
type CfdCoinSelectOption struct {
// fee asset
FeeAsset string
// tx-fee amount
TxFeeAmount int64
// effective feerate
EffectiveFeeRate float64
// longterm feerate
LongTermFeeRate float64
// dust feerate
DustFeeRate float64
// knapsack min change value. knapsack logic's threshold. Recommended value is 1.
KnapsackMinChange int64
// blind exponent. default is 0.
Exponent int64
// blind minimum bits. default is -1 (cfd-go auto).
MinimumBits int64
}
/**
* Create CfdCoinSelectOption struct set default value.
* return: option CoinSelection option
*/
func NewCfdCoinSelectionOption() CfdCoinSelectOption {
option := CfdCoinSelectOption{}
option.EffectiveFeeRate = float64(20.0)
option.LongTermFeeRate = float64(-1.0)
option.DustFeeRate = float64(-1.0)
option.KnapsackMinChange = int64(-1)
option.Exponent = int64(0)
option.MinimumBits = int64(-1)
return option
}
/**
* Select coins.
* param: utxos utxo array
* param: targetAmounts target amount array
* param: option option for coinSelection
* return: selectUtxos select coins
* return: totalAmounts select amount by asset
* return: utxoFee fee by utxo
* return: err error
*/
func CfdGoCoinSelection(utxos []CfdUtxo, targetAmounts []CfdTargetAmount, option CfdCoinSelectOption) (selectUtxos []CfdUtxo, totalAmounts []CfdTargetAmount, utxoFee int64, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
var coinSelectHandle uintptr
utxoCount := (uint32)(len(utxos))
amountCount := (uint32)(len(targetAmounts))
utxoCountBuf := SwigcptrUint32_t(uintptr(unsafe.Pointer(&utxoCount)))
amountCountBuf := SwigcptrUint32_t(uintptr(unsafe.Pointer(&amountCount)))
txFeeAmountBuf := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.TxFeeAmount)))
knapsackMinChangeBuf := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.KnapsackMinChange)))
ret := CfdInitializeCoinSelection(handle, utxoCountBuf,
amountCountBuf, option.FeeAsset, txFeeAmountBuf,
option.EffectiveFeeRate, option.LongTermFeeRate, option.DustFeeRate,
knapsackMinChangeBuf, &coinSelectHandle)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
defer CfdFreeCoinSelectionHandle(handle, coinSelectHandle)
for i := int32(0); i < (int32)(utxoCount); i++ {
indexBuf := SwigcptrInt32_t(uintptr(unsafe.Pointer(&i)))
voutBuf := SwigcptrUint32_t(uintptr(unsafe.Pointer(&utxos[i].Vout)))
amoutBuf := SwigcptrInt64_t(uintptr(unsafe.Pointer(&utxos[i].Amount)))
ret = CfdAddCoinSelectionUtxoTemplate(handle, coinSelectHandle, indexBuf, utxos[i].Txid, voutBuf, amoutBuf, utxos[i].Asset, utxos[i].Descriptor, utxos[i].ScriptSigTemplate)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
}
for i := uint32(0); i < amountCount; i++ {
indexBuf := SwigcptrUint32_t(uintptr(unsafe.Pointer(&i)))
amoutBuf := SwigcptrInt64_t(uintptr(unsafe.Pointer(&targetAmounts[i].Amount)))
ret = CfdAddCoinSelectionAmount(handle, coinSelectHandle, indexBuf, amoutBuf, targetAmounts[i].Asset)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
}
exponentPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.Exponent)))
ret = CfdSetOptionCoinSelection(handle, coinSelectHandle, int(KCfdCoinSelectionExponent), exponentPtr, float64(0), false)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
minimumBitsPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.MinimumBits)))
ret = CfdSetOptionCoinSelection(handle, coinSelectHandle, int(KCfdCoinSelectionMinimumBits), minimumBitsPtr, float64(0), false)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
utxoFeeBuf := SwigcptrInt64_t(uintptr(unsafe.Pointer(&utxoFee)))
ret = CfdFinalizeCoinSelection(handle, coinSelectHandle, utxoFeeBuf)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
selectUtxoCount := 0
tempUtxos := make([]CfdUtxo, utxoCount)
for i := uint32(0); i < utxoCount; i++ {
utxoIndex := int32(0)
indexBuf := SwigcptrUint32_t(uintptr(unsafe.Pointer(&i)))
utxoIndexBuf := SwigcptrInt32_t(uintptr(unsafe.Pointer(&utxoIndex)))
ret = CfdGetSelectedCoinIndex(handle, coinSelectHandle, indexBuf, utxoIndexBuf)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
if utxoIndex < 0 {
break
}
tempUtxos[i] = utxos[utxoIndex]
selectUtxoCount += 1
}
if selectUtxoCount > 0 {
selectUtxos = make([]CfdUtxo, selectUtxoCount)
for i := 0; i < selectUtxoCount; i++ {
selectUtxos[i] = tempUtxos[i]
}
}
totalAmounts = make([]CfdTargetAmount, amountCount)
for i := uint32(0); i < amountCount; i++ {
amount := int64(0)
indexBuf := SwigcptrUint32_t(uintptr(unsafe.Pointer(&i)))
amountBuf := SwigcptrInt64_t(uintptr(unsafe.Pointer(&amount)))
ret = CfdGetSelectedCoinAssetAmount(handle, coinSelectHandle, indexBuf, amountBuf)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
totalAmounts[i] = targetAmounts[i]
totalAmounts[i].Amount = amount
}
return
}
/**
* EstimateFee Input data struct.
* (deprecated) Because it is already integrated with CfdUtxo.
*/
type CfdEstimateFeeInput struct {
// utxo data
Utxo CfdUtxo
// is issuance input
IsIssuance bool
// is blind issuance input
IsBlindIssuance bool
// is peg-in input
IsPegin bool
// claimscript hex (require when IsPegin is true)
ClaimScript string
// peg-in bitcoin tx size (require when IsPegin is true)
PeginBtcTxSize uint32
// peg-in bitcoin txoutproof size (require when IsPegin is true)
PeginTxOutProofSize uint32
}
/**
* EstimateFee option data struct.
*/
type CfdFeeEstimateOption struct {
// effective feerate
EffectiveFeeRate float64
// use elements chain
UseElements bool
// fee asset
FeeAsset string
// Require blinding or not
RequireBlind bool
// blinding exponent value
Exponent int64
// blinding minimum bits value
MinimumBits int64
}
/**
* Create CfdFeeEstimateOption struct set default value.
* return: option EstimateFeeOption
*/
func NewCfdEstimateFeeOption() CfdFeeEstimateOption {
option := CfdFeeEstimateOption{}
option.EffectiveFeeRate = float64(20.0)
option.UseElements = true
option.FeeAsset = ""
option.RequireBlind = true
option.Exponent = int64(0)
option.MinimumBits = int64(-1)
return option
}
/**
* Estimate fee amount.
* param: txHex transaction hex
* param: inputs inputs to set in the transaction
* param: option options for fee estimation
* return: totalFee total fee value when all utxos set to input.
* (totalFee = txoutFee + utxoFee)
* return: txoutFee base transaction fee value.
* return: utxoFee fee value all of input set utxo.
*/
func CfdGoEstimateFee(txHex string, inputs []CfdEstimateFeeInput, option CfdFeeEstimateOption) (totalFee, txoutFee, utxoFee int64, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
var estimateFeeHandle uintptr
if ret := CfdInitializeEstimateFee(
handle,
&estimateFeeHandle,
option.UseElements,
); ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
defer CfdFreeEstimateFeeHandle(handle, estimateFeeHandle)
for _, input := range inputs {
vout := SwigcptrUint32_t(uintptr(unsafe.Pointer(&input.Utxo.Vout)))
peginBtcTxSize := SwigcptrUint32_t(uintptr(unsafe.Pointer(&input.PeginBtcTxSize)))
txOutProofSize := SwigcptrUint32_t(uintptr(unsafe.Pointer(&input.PeginTxOutProofSize)))
if ret := CfdAddTxInputForEstimateFee(
handle,
estimateFeeHandle,
input.Utxo.Txid,
vout,
input.Utxo.Descriptor,
input.Utxo.Asset,
input.IsIssuance,
input.IsBlindIssuance,
input.IsPegin,
input.ClaimScript,
peginBtcTxSize,
txOutProofSize,
input.Utxo.ScriptSigTemplate,
); ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
}
exponentPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.Exponent)))
ret := CfdSetOptionEstimateFee(handle, estimateFeeHandle, int(KCfdEstimateFeeExponent), exponentPtr, float64(0), false)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
minimumBitsPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.MinimumBits)))
ret = CfdSetOptionEstimateFee(handle, estimateFeeHandle, int(KCfdEstimateFeeMinimumBits), minimumBitsPtr, float64(0), false)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
var txFeeWork, inputFeeWork int64
txFeeWorkPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&txFeeWork)))
inputFeeWorkPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&inputFeeWork)))
if ret := CfdFinalizeEstimateFee(
handle,
estimateFeeHandle,
txHex,
option.FeeAsset,
txFeeWorkPtr,
inputFeeWorkPtr,
option.RequireBlind,
option.EffectiveFeeRate,
); ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
totalFee = txFeeWork + inputFeeWork
txoutFee = txFeeWork
utxoFee = inputFeeWork
return
}
/**
* Estimate fee amount.
* param: txHex transaction hex
* param: inputs inputs to set in the transaction
* param: option options for fee estimation
* return: totalFee total fee value when all utxos set to input.
* (totalFee = txoutFee + utxoFee)
* return: txoutFee base transaction fee value.
* return: utxoFee fee value all of input set utxo.
*/
func CfdGoEstimateFeeUsingUtxo(txHex string, inputs []CfdUtxo, option CfdFeeEstimateOption) (totalFee, txoutFee, utxoFee int64, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
var estimateFeeHandle uintptr
if ret := CfdInitializeEstimateFee(
handle,
&estimateFeeHandle,
option.UseElements,
); ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
defer CfdFreeEstimateFeeHandle(handle, estimateFeeHandle)
for _, input := range inputs {
vout := SwigcptrUint32_t(uintptr(unsafe.Pointer(&input.Vout)))
peginBtcTxSize := SwigcptrUint32_t(uintptr(unsafe.Pointer(&input.PeginBtcTxSize)))
txoutproofSize := SwigcptrUint32_t(uintptr(unsafe.Pointer(&input.PeginTxOutProofSize)))
if ret := CfdAddTxInputForEstimateFee(
handle,
estimateFeeHandle,
input.Txid,
vout,
input.Descriptor,
input.Asset,
input.IsIssuance,
input.IsBlindIssuance,
input.IsPegin,
input.ClaimScript,
peginBtcTxSize,
txoutproofSize,
input.ScriptSigTemplate,
); ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
}
exponentPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.Exponent)))
ret := CfdSetOptionEstimateFee(handle, estimateFeeHandle, int(KCfdEstimateFeeExponent), exponentPtr, float64(0), false)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
minimumBitsPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&option.MinimumBits)))
ret = CfdSetOptionEstimateFee(handle, estimateFeeHandle, int(KCfdEstimateFeeMinimumBits), minimumBitsPtr, float64(0), false)
if ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
var txFeeWork, inputFeeWork int64
txFeeWorkPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&txFeeWork)))
inputFeeWorkPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&inputFeeWork)))
if ret := CfdFinalizeEstimateFee(
handle,
estimateFeeHandle,
txHex,
option.FeeAsset,
txFeeWorkPtr,
inputFeeWorkPtr,
option.RequireBlind,
option.EffectiveFeeRate,
); ret != (int)(KCfdSuccess) {
err = convertCfdError(ret, handle)
return
}
totalFee = txFeeWork + inputFeeWork
txoutFee = txFeeWork
utxoFee = inputFeeWork
return
}
/**
* Get initialized confidential transaction.
* param: version transaction version
* param: locktime locktime
* return: txHex transaction hex
* return: err error
*/
func CfdGoInitializeConfidentialTx(version uint32, locktime uint32) (txHex string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
versionPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&version)))
locktimePtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&locktime)))
ret := CfdInitializeConfidentialTx(handle, versionPtr, locktimePtr, &txHex)
err = convertCfdError(ret, handle)
return txHex, err
}
/**
* Add txin to confidential transaction.
* param: txHex transaction hex
* param: txid txid
* param: vout vout
* param: sequence sequence
* return: outputTxHex output transaction hex
* return: err error
*/
func CfdGoAddConfidentialTxIn(txHex string, txid string, vout uint32, sequence uint32) (outputTxHex string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
voutPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&vout)))
sequencePtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&sequence)))
ret := CfdAddConfidentialTxIn(handle, txHex, txid, voutPtr, sequencePtr, &outputTxHex)
err = convertCfdError(ret, handle)
return outputTxHex, err
}
/**
* Add txout to confidential transaction.
* param: txHex transaction hex
* param: asset asset
* param: satoshiAmount amount by satoshi
* param: valueCommitment amount by commitment bytes.
* param: address destination address
* param: directLockingScript locking script for direct insert.
* param: nonce confidential nonce
* return: outputTxHex output transaction hex
* return: err error
*/
func CfdGoAddConfidentialTxOut(txHex string, asset string, satoshiAmount int64, valueCommitment string, address string, directLockingScript string, nonce string) (outputTxHex string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
satoshiPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&satoshiAmount)))
ret := CfdAddConfidentialTxOut(handle, txHex, asset, satoshiPtr, valueCommitment, address, directLockingScript, nonce, &outputTxHex)
err = convertCfdError(ret, handle)
return outputTxHex, err
}
/**
* Update txout of confidential transaction.
* param: txHex transaction hex
* param: index txout index
* param: asset asset
* param: satoshiAmount amount by satoshi
* param: valueCommitment amount by commitment bytes.
* param: address destination address
* param: directLockingScript lockingScript for direct insert.
* param: nonce confidential nonce
* return: outputTxHex output transaction hex
* return: err error
*/
func CfdGoUpdateConfidentialTxOut(txHex string, index uint32, asset string, satoshiAmount int64, valueCommitment string, address string, directLockingScript string, nonce string) (outputTxHex string, err error) {
handle, err := CfdGoCreateHandle()
if err != nil {
return
}
defer CfdGoFreeHandle(handle)
indexPtr := SwigcptrUint32_t(uintptr(unsafe.Pointer(&index)))
satoshiPtr := SwigcptrInt64_t(uintptr(unsafe.Pointer(&satoshiAmount)))
ret := CfdUpdateConfidentialTxOut(handle, txHex, indexPtr, asset, satoshiPtr, valueCommitment, address, directLockingScript, nonce, &outputTxHex)
err = convertCfdError(ret, handle)
return outputTxHex, err
}
/**
* Add output for destroying the specified amount of the specified asset.
* This function is deprecated.
* param: txHex transaction hex
* param: asset asset
* param: satoshiAmount amount by satoshi
* return: outputTxHex output transaction hex
* return: err error
*/
func CfdGoAddDestoryConfidentialTxOut(txHex string, asset string, satoshiAmount int64) (outputTxHex string, err error) {
outputTxHex, err = CfdGoAddDestroyConfidentialTxOut(txHex, asset, satoshiAmount)
return outputTxHex, err
}
/**
* Add output for destroying the specified amount of the specified asset.
* param: txHex transaction hex
* param: asset asset
* param: satoshiAmount amount by satoshi
* return: outputTxHex output transaction hex