-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibdogecoin.pyx
403 lines (325 loc) · 13.9 KB
/
libdogecoin.pyx
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
cimport cython as cy
from libc.string cimport strncpy, memset
# FUNCTIONS FROM STATIC LIBRARY
#========================================================
cdef extern from "libdogecoin.h":
# ecc.c
void dogecoin_ecc_start()
void dogecoin_ecc_stop()
# address.c
int generatePrivPubKeypair(char* wif_privkey, char* p2pkh_pubkey, bint is_testnet)
int generateHDMasterPubKeypair(char* wif_privkey_master, char* p2pkh_pubkey_master, bint is_testnet)
int generateDerivedHDPubkey(const char* wif_privkey_master, char* p2pkh_pubkey)
int verifyPrivPubKeypair(char* wif_privkey, char* p2pkh_pubkey, bint is_testnet)
int verifyHDMasterPubKeypair(char* wif_privkey_master, char* p2pkh_pubkey_master, bint is_testnet)
int verifyP2pkhAddress(char* p2pkh_pubkey, cy.uchar len)
# transaction.c
int start_transaction()
int add_utxo(int txindex, char* hex_utxo_txid, int vout)
int add_output(int txindex, char* destinationaddress, char* amount)
char* finalize_transaction(int txindex, char* destinationaddress, char* subtractedfee, char* out_dogeamount_for_verification, char* changeaddress)
char* get_raw_transaction(int txindex)
void clear_transaction(int txindex)
int sign_raw_transaction(int inputindex, char* incomingrawtx, char* scripthex, int sighashtype, char* privkey)
int sign_transaction(int txindex, char* script_pubkey, char* privkey)
int store_raw_transaction(char* incomingrawtx)
# PYTHON INTERFACE
#========================================================
# ADDRESS FUNCTIONS
def w_context_start():
"""Start the secp256k1 context necessary for key pair
generation. Must be started before calling any functions
dealing with private or public keys.
"""
dogecoin_ecc_start()
def w_context_stop():
"""Stop the current instance of the secp256k1 context. It is
advised to wait until the session is completely over before
stopping the context.
"""
dogecoin_ecc_stop()
def w_generate_priv_pub_key_pair(chain_code=0):
"""Generate a valid private key paired with the corresponding
p2pkh address.
Keyword arguments:
chain_code -- 0 for mainnet pair, 1 for testnet pair
"""
# verify arguments are valid
assert isinstance(chain_code, int) and chain_code in [0,1]
# prepare arguments
cdef char privkey[53]
cdef char p2pkh_pubkey[35]
cdef bint is_testnet = chain_code
# call c function
generatePrivPubKeypair(privkey, p2pkh_pubkey, chain_code)
# return keys as bytes object
return privkey, p2pkh_pubkey
def w_generate_hd_master_pub_key_pair(chain_code=0):
"""Generate a master private and public key pair for use in
hierarchical deterministic wallets. Public key can be used for
child key derivation using w_generate_derived_hd_pub_key().
Keyword arguments:
chain_code -- 0 for mainnet pair, 1 for testnet pair
"""
# verify arguments are valid
assert isinstance(chain_code, int) and chain_code in [0,1]
# prepare arguments
cdef char master_privkey[128]
cdef char master_p2pkh_pubkey[35]
# call c function
generateHDMasterPubKeypair(master_privkey, master_p2pkh_pubkey, chain_code)
# return keys
# TODO: extra bytes added to end of testnet keys?? truncate after 34 as a temp patch
return master_privkey, master_p2pkh_pubkey[:34]
def w_generate_derived_hd_pub_key(wif_privkey_master):
"""Given a HD master private key, derive a child key from it.
Keyword arguments:
wif_privkey_master -- HD master public key as wif-encoded string
"""
# verify arguments are valid
assert isinstance(wif_privkey_master, (str, bytes))
# prepare arguments
if not isinstance(wif_privkey_master, bytes):
wif_privkey_master = wif_privkey_master.encode('utf-8')
cdef char child_p2pkh_pubkey[128]
# call c function
generateDerivedHDPubkey(wif_privkey_master, child_p2pkh_pubkey)
# return results in bytes
return child_p2pkh_pubkey
def w_verify_priv_pub_keypair(wif_privkey, p2pkh_pubkey, chain_code=0):
"""Given a private/public key pair, verify that the keys are
valid and are associated with each other.
Keyword arguments:
wif_privkey -- string containing wif-encoded private key
p2pkh_pubkey -- string containing address derived from wif_privkey
chain_code -- 0 for mainnet, 1 for testnet
"""
# verify arguments are valid
assert isinstance(wif_privkey, (str, bytes))
assert isinstance(p2pkh_pubkey, (str, bytes))
assert isinstance(chain_code, int) and chain_code in [0,1]
# prepare arguments
if not isinstance(wif_privkey, bytes):
wif_privkey = wif_privkey.encode('utf-8')
if not isinstance(p2pkh_pubkey, bytes):
p2pkh_pubkey = p2pkh_pubkey.encode('utf-8')
# call c function
res = verifyPrivPubKeypair(wif_privkey, p2pkh_pubkey, chain_code)
# return boolean result
return res
def w_verify_master_priv_pub_keypair(wif_privkey_master, p2pkh_pubkey_master, chain_code=0):
"""Given a keypair from generate_hd_master_pub_key_pair, verify that the
keys are valid and are associated with each other.
Keyword arguments:
wif_privkey_master -- string containing wif-encoded private master key
p2pkh_pubkey_master -- string containing address derived from wif_privkey
chain_code -- 0 for mainnet, 1 for testnet
"""
# verify arguments are valid
assert isinstance(wif_privkey_master, (str, bytes))
assert isinstance(p2pkh_pubkey_master, (str, bytes))
assert isinstance(chain_code, int) and chain_code in [0,1]
# prepare arguments
if not isinstance(wif_privkey_master, bytes):
wif_privkey_master = wif_privkey_master.encode('utf-8')
if not isinstance(p2pkh_pubkey_master, bytes):
p2pkh_pubkey_master = p2pkh_pubkey_master.encode('utf-8')
# call c function
res = verifyHDMasterPubKeypair(wif_privkey_master, p2pkh_pubkey_master, chain_code)
# return boolean result
return res
def w_verify_p2pkh_address(p2pkh_pubkey):
"""Given a p2pkh address, confirm address is in correct Dogecoin
format.
Keyword arguments:
p2pkh_pubkey -- string containing basic p2pkh address
"""
# verify arguments are valid
assert isinstance(p2pkh_pubkey, (str, bytes))
# prepare arguments
if not isinstance(p2pkh_pubkey, bytes):
p2pkh_pubkey = p2pkh_pubkey.encode('utf-8')
# call c function
res = verifyP2pkhAddress(p2pkh_pubkey, len(p2pkh_pubkey))
# return boolean result
return res
# TRANSACTION FUNCTIONS
def w_start_transaction():
"""Create a new, empty dogecoin transaction."""
# call c function
res = start_transaction()
# return boolean result
return res
def w_add_utxo(tx_index, hex_utxo_txid, vout):
"""Given the index of a working transaction, add another
input to it.
Keyword arguments:
tx_index -- the index of the working transaction to update
hex_utxo_txid -- the transaction id of the utxo to be spent
vout -- the number of outputs associated with the specified utxo
"""
# verify arguments are valid
assert isinstance(tx_index, int)
assert isinstance(hex_utxo_txid, (str, bytes))
assert isinstance(vout, (int, str))
# prepare arguments
if not isinstance(hex_utxo_txid, bytes):
hex_utxo_txid = hex_utxo_txid.encode('utf-8')
# call c function
res = add_utxo(tx_index, hex_utxo_txid, vout)
# return boolean result
return res
def w_add_output(tx_index, destination_address, amount):
"""Given the index of a working transaction, add another
output to it.
Keyword arguments:
tx_index -- the index of the working transaction to update
destination_address -- the address of the output being added
amount -- the amount of dogecoin to send to the specified address
"""
# verify arguments are valid
assert isinstance(tx_index, int)
assert isinstance(destination_address, (str, bytes))
assert isinstance(amount, (int, str))
# prepare arguments
if not isinstance(destination_address, bytes):
destination_address = destination_address.encode('utf-8')
amount = str(amount).encode('utf-8')
# call c function
res = add_output(tx_index, destination_address, amount)
# return boolean result
return res
def w_finalize_transaction(tx_index, destination_address, subtracted_fee, out_dogeamount_for_verification, changeaddress):
"""Given the index of a working transaction, prepares it
for signing by specifying the recipient and fee to subtract,
directing extra change back to the sender.
Keyword arguments:
tx_index -- the index of the working transaction
destination address -- the address to send coins to
subtracted_fee -- the amount of dogecoin to assign as a fee
out_dogeamount_for_verification -- the total amount of dogecoin being sent (fee included)
changeaddress -- the address of the sender to receive their change
"""
print(tx_index, destination_address, subtracted_fee, out_dogeamount_for_verification, changeaddress)
# verify arguments are valid
assert isinstance(tx_index, int)
assert isinstance(destination_address, (str, bytes))
assert isinstance(subtracted_fee, (int, str))
assert isinstance(out_dogeamount_for_verification, (int, str))
assert isinstance(changeaddress, (str, bytes))
# prepare arguments
if not isinstance(destination_address, bytes):
destination_address = destination_address.encode('utf-8')
if not isinstance(changeaddress, bytes):
changeaddress = changeaddress.encode('utf-8')
subtracted_fee = str(subtracted_fee).encode('utf-8')
out_dogeamount_for_verification = str(out_dogeamount_for_verification).encode('utf-8')
# call c function
cdef void* res
cdef char* finalized_transaction_hex
res = finalize_transaction(tx_index, destination_address, subtracted_fee, out_dogeamount_for_verification, changeaddress)
# return hex result
try:
if (res==<void*>0):
raise TypeError
finalized_transaction_hex = <char*>res
return finalized_transaction_hex.decode('utf-8')
except:
return 0
def w_get_raw_transaction(tx_index):
"""Given the index of a working transaction, returns
the serialized object in hex format.
Keyword arguments:
tx_index -- the index of the working transaction
"""
# verify arguments are valid
assert isinstance(tx_index, int)
# call c function
cdef void* res
cdef char* raw_transaction_hex
res = get_raw_transaction(tx_index)
# return hex result
try:
if (res==<void*>0):
raise TypeError
raw_transaction_hex = <char*>res
return raw_transaction_hex.decode('utf-8')
except:
return 0
def w_clear_transaction(tx_index):
"""Discard a working transaction.
Keyword arguments:
tx_index -- the index of the working transaction
"""
# verify arguments are valid
assert isinstance(tx_index, int)
# call c function
clear_transaction(tx_index)
def w_sign_raw_transaction(tx_index, incoming_raw_tx, script_hex, sig_hash_type, privkey):
"""Sign a finalized raw transaction using the specified
private key.
Keyword arguments:
tx_index -- the index of the working transaction to sign
incoming_raw_tx -- the serialized string of the transaction to sign
script_hex -- the hex of the script to be signed
sig_hash_type -- the type of signature hash to be used
privkey -- the private key to sign with
"""
# verify arguments are valid
assert isinstance(tx_index, int)
assert isinstance(incoming_raw_tx, (str, bytes))
assert isinstance(script_hex, (str, bytes))
assert isinstance(sig_hash_type, int)
assert isinstance(privkey, (str, bytes))
# prepare arguments
if not isinstance(incoming_raw_tx, bytes):
incoming_raw_tx = incoming_raw_tx.encode('utf-8')
if not isinstance(script_hex, bytes):
script_hex = script_hex.encode('utf-8')
if not isinstance(privkey, bytes):
privkey = privkey.encode('utf-8')
# allocate enough mem to cover extension of transaction hex
cdef char c_incoming_raw_tx[1024*100] # max size for a valid signable transaction
memset(c_incoming_raw_tx, 0, (1024*100))
strncpy(c_incoming_raw_tx, incoming_raw_tx, len(incoming_raw_tx))
# call c function and return result
if sign_raw_transaction(tx_index, c_incoming_raw_tx, script_hex, sig_hash_type, privkey):
res = c_incoming_raw_tx.decode('utf-8')
return res
else:
return 0
def w_sign_transaction(tx_index, script_pubkey, privkey):
"""Sign all the inputs of a working transaction using the
specified private key and public key script.
Keyword arguments:
tx_index -- the index of the working transaction to sign
script_pubkey -- the pubkey script associated with the private key
privkey -- the private key used to sign the specified transaction"""
# verify arguments are valid
assert isinstance(tx_index, int)
assert isinstance(script_pubkey, (str, bytes))
assert isinstance(privkey, (str, bytes))
# prepare arguments
if not isinstance(script_pubkey, bytes):
script_pubkey = script_pubkey.encode('utf-8')
if not isinstance(privkey, bytes):
privkey = privkey.encode('utf-8')
# call c function
res = sign_transaction(tx_index, script_pubkey, privkey)
# return result
return res
def w_store_raw_transaction(incoming_raw_tx):
"""Stores a raw transaction at the next available index
in the hash table.
Keyword arguments:
incoming_raw_tx -- the serialized string of the transaction to store.
"""
# verify arguments are valid
assert isinstance(incoming_raw_tx, (str, bytes))
# prepare arguments
if not isinstance(incoming_raw_tx, bytes):
incoming_raw_tx = incoming_raw_tx.encode('utf-8')
# call c function
res = store_raw_transaction(incoming_raw_tx)
# return result
return res