-
Notifications
You must be signed in to change notification settings - Fork 4
/
libdogecoin.go
209 lines (188 loc) · 7.03 KB
/
libdogecoin.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
package libdogecoin
/*
#cgo CFLAGS: -I${SRCDIR}/include -fPIC
#cgo linux,amd64 LDFLAGS: -L${SRCDIR}/build/linux/amd64 -ldogecoin -levent -lm -Wl,-rpath,${SRCDIR}/build/linux/amd64
#cgo linux,arm64 LDFLAGS: -L${SRCDIR}/build/linux/arm64 -ldogecoin -lm -Wl,-rpath,${SRCDIR}/build/linux/arm64
#cgo darwin,amd64 LDFLAGS: -L${SRCDIR}/build/darwin/amd64 -ldogecoin -lm -Wl,-rpath,${SRCDIR}/build/darwin/amd64
#cgo windows,amd64 LDFLAGS: -L${SRCDIR}/build/windows/amd64 -ldogecoin -lm -Wl,-rpath,${SRCDIR}/build/windows/amd64
#cgo windows,386 LDFLAGS: -L${SRCDIR}/build/windows/386 -ldogecoin -lm -Wl,-rpath,${SRCDIR}/build/windows/386
#include "libdogecoin.h"
*/
import "C"
import (
"fmt"
"strconv"
"unsafe"
)
func W_context_start() {
C.dogecoin_ecc_start()
}
func W_context_stop() {
C.dogecoin_ecc_stop()
}
func W_generate_priv_pub_keypair(is_testnet bool) (wif_privkey string, p2pkh_pubkey string) {
c_wif_privkey := [53]C.char{}
c_p2pkh_pubkey := [35]C.char{}
c_is_testnet := C._Bool(is_testnet)
C.generatePrivPubKeypair((*C.char)(&c_wif_privkey[0]), (*C.char)(&c_p2pkh_pubkey[0]), c_is_testnet)
wif_privkey = C.GoString((*C.char)(&c_wif_privkey[0]))
p2pkh_pubkey = C.GoString((*C.char)(&c_p2pkh_pubkey[0]))
return
}
func W_generate_hd_master_pub_keypair(is_testnet bool) (wif_privkey_master string, p2pkh_pubkey_master string) {
c_wif_privkey_master := [128]C.char{}
c_p2pkh_pubkey_master := [35]C.char{}
c_is_testnet := C._Bool(is_testnet)
C.generateHDMasterPubKeypair((*C.char)(&c_wif_privkey_master[0]), (*C.char)(&c_p2pkh_pubkey_master[0]), c_is_testnet)
wif_privkey_master = C.GoString((*C.char)(&c_wif_privkey_master[0]))
p2pkh_pubkey_master = C.GoString((*C.char)(&c_p2pkh_pubkey_master[0]))
return
}
func W_generate_derived_hd_pub_key(wif_privkey_master string) (child_p2pkh_pubkey string) {
c_wif_privkey_master := C.CString(wif_privkey_master)
c_child_p2pkh_pubkey := [35]C.char{}
C.generateDerivedHDPubkey(c_wif_privkey_master, (*C.char)(&c_child_p2pkh_pubkey[0]))
child_p2pkh_pubkey = C.GoString((*C.char)(&c_child_p2pkh_pubkey[0]))
C.free(unsafe.Pointer(c_wif_privkey_master))
return
}
func W_verify_priv_pub_keypair(wif_privkey string, p2pkh_pubkey string, is_testnet bool) (result bool) {
c_wif_privkey := C.CString(wif_privkey)
c_p2pkh_pubkey := C.CString(p2pkh_pubkey)
c_is_testnet := C._Bool(is_testnet)
if C.verifyPrivPubKeypair(c_wif_privkey, c_p2pkh_pubkey, c_is_testnet) == 1 {
result = true
} else {
result = false
}
C.free(unsafe.Pointer(c_wif_privkey))
C.free(unsafe.Pointer(c_p2pkh_pubkey))
return
}
func W_verify_hd_master_pub_keypair(wif_privkey_master string, p2pkh_pubkey_master string, is_testnet bool) (result bool) {
c_wif_privkey_master := C.CString(wif_privkey_master)
c_p2pkh_pubkey_master := C.CString(p2pkh_pubkey_master)
c_is_testnet := C._Bool(is_testnet)
if C.verifyHDMasterPubKeypair(c_wif_privkey_master, c_p2pkh_pubkey_master, c_is_testnet) == 1 {
result = true
} else {
result = false
}
C.free(unsafe.Pointer(c_wif_privkey_master))
C.free(unsafe.Pointer(c_p2pkh_pubkey_master))
return
}
func W_verify_p2pkh_address(p2pkh_pubkey string) (result bool) {
c_p2pkh_pubkey := C.CString(p2pkh_pubkey)
len := len(p2pkh_pubkey)
c_len := C.uchar(len)
if C.verifyP2pkhAddress(c_p2pkh_pubkey, c_len) == 1 {
result = true
} else {
result = false
}
C.free(unsafe.Pointer(c_p2pkh_pubkey))
return
}
func W_start_transaction() (result int) {
result = int(C.start_transaction())
return
}
func W_add_utxo(tx_index int, hex_utxo_txid string, vout int) (result int) {
c_tx_index := C.int(tx_index)
c_hex_utxo_txid := C.CString(hex_utxo_txid)
c_vout := C.int(vout)
result = int(C.add_utxo(c_tx_index, c_hex_utxo_txid, c_vout))
C.free(unsafe.Pointer(c_hex_utxo_txid))
return
}
func W_add_output(tx_index int, destination_address string, amount string) (result int) {
c_tx_index := C.int(tx_index)
c_destination_address := C.CString(destination_address)
_, err := strconv.ParseFloat(amount, 64)
if err != nil {
fmt.Println("Error: amount is not numeric.")
return 0
}
c_amount := C.CString(amount)
result = int(C.add_output(c_tx_index, c_destination_address, c_amount))
C.free(unsafe.Pointer(c_destination_address))
return
}
func W_finalize_transaction(tx_index int, destination_address string, subtracted_fee string, out_doge_amount_for_verification string, public_key string) (result string) {
c_tx_index := C.int(tx_index)
c_destination_address := C.CString(destination_address)
_, err1 := strconv.ParseFloat(subtracted_fee, 64)
if err1 != nil {
fmt.Println("Error: subtracted fee is not numeric.")
return ""
}
c_subtracted_fee := C.CString(subtracted_fee)
_, err2 := strconv.ParseFloat(out_doge_amount_for_verification, 64)
if err2 != nil {
fmt.Println("Error: send amount is not numeric.")
return ""
}
c_out_doge_amount_for_verification := C.CString(out_doge_amount_for_verification)
c_public_key := C.CString(public_key)
result = C.GoString(C.finalize_transaction(c_tx_index, c_destination_address, c_subtracted_fee, c_out_doge_amount_for_verification, c_public_key))
C.free(unsafe.Pointer(c_destination_address))
C.free(unsafe.Pointer(c_public_key))
return
}
func W_get_raw_transaction(tx_index int) (result string) {
c_tx_index := C.int(tx_index)
result = C.GoString(C.get_raw_transaction(c_tx_index))
return
}
func W_clear_transaction(tx_index int) {
c_tx_index := C.int(tx_index)
C.clear_transaction(c_tx_index)
}
func W_sign_raw_transaction(input_index int, incoming_raw_tx string, script_hex string, sig_hash_type int, amount string, privkey string) (result string) {
c_input_index := C.int(input_index)
c_incoming_raw_tx := [1024 * 100]C.char{}
for i := 0; i < len(incoming_raw_tx) && i < 1024; i++ {
c_incoming_raw_tx[i] = C.char(incoming_raw_tx[i])
}
c_script_hex := C.CString(script_hex)
c_sig_hash_type := C.int(sig_hash_type)
_, err := strconv.ParseFloat(amount, 64)
if err != nil {
fmt.Println("Error: amount is not numeric.")
return ""
}
c_amount := C.CString(amount)
c_privkey := C.CString(privkey)
if C.sign_raw_transaction(c_input_index, &c_incoming_raw_tx[0], c_script_hex, c_sig_hash_type, c_amount, c_privkey) == 1 {
result = C.GoString(&c_incoming_raw_tx[0])
} else {
result = ""
}
C.free(unsafe.Pointer(c_script_hex))
C.free(unsafe.Pointer(c_privkey))
return
}
func W_sign_transaction(tx_index int, amounts []string, script_pubkey string, privkey string) (result int) {
c_tx_index := C.int(tx_index)
c_amounts := make([]*C.char, len(amounts))
for i := 0; i < len(amounts); i++ {
_, err := strconv.ParseFloat(amounts[i], 64)
if err != nil {
fmt.Println("Error: amount is not numeric.")
return 0
}
c_amounts[i] = (C.CString(amounts[i]))
}
c_script_pubkey := C.CString(script_pubkey)
c_privkey := C.CString(privkey)
result = int(C.sign_transaction(c_tx_index, &c_amounts[0], c_script_pubkey, c_privkey))
C.free(unsafe.Pointer(c_script_pubkey))
C.free(unsafe.Pointer(c_privkey))
return
}
func W_store_raw_transaction(incoming_raw_tx string) (result int) {
c_incoming_raw_tx := C.CString(incoming_raw_tx)
result = int(C.store_raw_transaction(c_incoming_raw_tx))
return
}