-
Notifications
You must be signed in to change notification settings - Fork 39
/
wallet.py
669 lines (578 loc) · 24.5 KB
/
wallet.py
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
import json
from bitcash.crypto import ECPrivateKey
from bitcash.cashtoken import Unspents
from bitcash.curve import Point
from bitcash.exceptions import InvalidNetwork
from bitcash.format import (
bytes_to_wif,
public_key_to_address,
public_key_to_coords,
wif_to_bytes,
address_to_public_key_hash,
address_to_cashtokenaddress,
)
from bitcash.network import NetworkAPI, satoshi_to_currency_cached
from bitcash.network.meta import Unspent
from bitcash.op import OpCodes
from bitcash.transaction import calc_txid, create_p2pkh_transaction, sanitize_tx_data
NETWORKS = {"main": "mainnet", "test": "testnet", "regtest": "regtest"}
DEFAULT_FEE = 1
def wif_to_key(wif, regtest=False):
private_key_bytes, compressed, version = wif_to_bytes(wif, regtest)
if version == "main":
if compressed:
return PrivateKey.from_bytes(private_key_bytes)
else:
return PrivateKey(wif)
elif version == "test":
if compressed:
return PrivateKeyTestnet.from_bytes(private_key_bytes)
else:
return PrivateKeyTestnet(wif)
else: # Regtest
if compressed:
return PrivateKeyRegtest.from_bytes(private_key_bytes)
else:
return PrivateKeyRegtest(wif)
class BaseKey:
"""This class represents a point on the elliptic curve secp256k1 and
provides all necessary cryptographic functionality. You shouldn't use
this class directly.
:param wif: A private key serialized to the Wallet Import Format. If the
argument is not supplied, a new private key will be created.
The WIF compression flag will be adhered to, but the version
byte is disregarded. Compression will be used by all new keys.
:type wif: ``str``
:raises TypeError: If ``wif`` is not a ``str``.
"""
def __init__(self, wif=None, regtest=False):
if wif:
if isinstance(wif, str):
private_key_bytes, compressed, version = wif_to_bytes(wif, regtest)
self._pk = ECPrivateKey(private_key_bytes)
elif isinstance(wif, ECPrivateKey):
self._pk = wif
compressed = True
else:
raise TypeError("Wallet Import Format must be a string.")
else:
self._pk = ECPrivateKey()
compressed = True
self._public_point = None
self._public_key = self._pk.public_key.format(compressed=compressed)
@property
def public_key(self):
"""The public point serialized to bytes."""
return self._public_key
@property
def public_point(self):
"""The public point (x, y)."""
if self._public_point is None:
self._public_point = Point(*public_key_to_coords(self._public_key))
return self._public_point
def sign(self, data):
"""Signs some data which can be verified later by others using
the public key.
:param data: The message to sign.
:type data: ``bytes``
:returns: A signature compliant with BIP-62.
:rtype: ``bytes``
"""
return self._pk.sign(data)
def verify(self, signature, data):
"""Verifies some data was signed by this private key.
:param signature: The signature to verify.
:type signature: ``bytes``
:param data: The data that was supposedly signed.
:type data: ``bytes``
:rtype: ``bool``
"""
return self._pk.public_key.verify(signature, data)
def to_hex(self):
""":rtype: ``str``"""
return self._pk.to_hex()
def to_bytes(self):
""":rtype: ``bytes``"""
return self._pk.secret
def to_der(self):
""":rtype: ``bytes``"""
return self._pk.to_der()
def to_pem(self):
""":rtype: ``bytes``"""
return self._pk.to_pem()
def to_int(self):
""":rtype: ``int``"""
return self._pk.to_int()
def is_compressed(self):
"""Returns whether or not this private key corresponds to a compressed
public key.
:rtype: ``bool``
"""
return True if len(self.public_key) == 33 else False
def __eq__(self, other):
return self.to_int() == other.to_int()
class PrivateKey(BaseKey):
"""This class represents a BitcoinCash private key. ``Key`` is an alias.
:param wif: A private key serialized to the Wallet Import Format. If the
argument is not supplied, a new private key will be created.
The WIF compression flag will be adhered to, but the version
byte is disregarded. Compression will be used by all new keys.
:type wif: ``str``
:raises TypeError: If ``wif`` is not a ``str``.
"""
def __init__(self, wif=None, network="main"):
super().__init__(wif=wif)
self._address = None
self._scriptcode = None
if network in NETWORKS.keys():
self._network = network
else:
raise InvalidNetwork
self.balance = 0
self.cashtoken_balance = {}
self.unspents = []
self.transactions = []
@property
def address(self):
"""The public address you share with others to receive funds."""
if self._address is None:
self._address = public_key_to_address(
self._public_key, version=self._network
)
return self._address
@property
def cashtoken_address(self):
"""The public address you share with others to receive cashtokens."""
return address_to_cashtokenaddress(self.address)
@property
def scriptcode(self):
self._scriptcode = (
OpCodes.OP_DUP.binary
+ OpCodes.OP_HASH160.binary
+ OpCodes.OP_DATA_20.binary
+ address_to_public_key_hash(self.address)
+ OpCodes.OP_EQUALVERIFY.binary
+ OpCodes.OP_CHECKSIG.binary
)
return self._scriptcode
def to_wif(self):
return bytes_to_wif(
self._pk.secret, version=self._network, compressed=self.is_compressed()
)
def balance_as(self, currency):
"""Returns your balance as a formatted string in a particular currency.
:param currency: One of the :ref:`supported currencies`.
:type currency: ``str``
:rtype: ``str``
"""
return satoshi_to_currency_cached(self.balance, currency)
def get_balance(self, currency="satoshi"):
"""Fetches the current balance by calling
:func:`~bitcash.PrivateKey.get_balance` and returns it using
:func:`~bitcash.PrivateKey.balance_as`.
:param currency: One of the :ref:`supported currencies`.
:type currency: ``str``
:rtype: ``str``
"""
_ = self.get_unspents()
return self.balance_as(currency)
def get_cashtokenbalance(self):
"""Fetches the current cashtoken balance by calling
:func:`~bitcash.PrivateKey.get_balance` and returns it as
a token dictionary.
:rtype: ``dict``
"""
_ = self.get_unspents()
return self.cashtoken_balance
def get_unspents(self):
"""Fetches all available unspent transaction outputs.
:rtype: ``list`` of :class:`~bitcash.network.meta.Unspent`
"""
self.unspents[:] = NetworkAPI.get_unspent(
self.address, network=NETWORKS[self._network]
)
_ = Unspents(self.unspents)
self.balance = _.amount
self.cashtoken_balance = _.tokendata
return self.unspents
def get_transactions(self):
"""Fetches transaction history.
:rtype: ``list`` of ``str`` transaction IDs
"""
self.transactions[:] = NetworkAPI.get_transactions(
self.address, network=NETWORKS[self._network]
)
return self.transactions
def create_transaction(
self,
outputs,
fee=None,
leftover=None,
combine=True,
message=None,
unspents=None,
custom_pushdata=False,
): # pragma: no cover
"""Creates a signed P2PKH transaction.
:param outputs: A sequence of outputs you wish to send in the form
``(destination, amount, currency)``. The amount can
be either an int, float, or string as long as it is
a valid input to ``decimal.Decimal``. The currency
must be :ref:`supported <supported currencies>`.
To send CashToken, the list of output is made in the
form ``(destination, amount, currency, category_id,
nft_capability, nft_commitment, token_amount)``. The category_id
is hex of tx-id as ``str``. The nft_capability is the capability
of non-fungible token in ("none", "mutable", "minting"). The
nft_commitment is the commitment of the non-fungible token in
``bytes``.
The CashToken property nft_capability, nft_commitment, or
the token_amount can be None if not to be sent. If
category_id is tx-id of unspent with tx-index 0, then
tx is treated as a genesis tx.
:type outputs: ``list`` of ``tuple``
:param fee: The number of satoshi per byte to pay to miners. By default
Bitcash will poll `<https://bitcoincashfees.earn.com>`_ and use a fee
that will allow your transaction to be confirmed as soon as
possible.
:type fee: ``int``
:param leftover: The destination that will receive any change from the
transaction. By default Bitcash will send any change to
the same address you sent from.
:type leftover: ``str``
:param combine: Whether or not Bitcash should use all available UTXOs to
make future transactions smaller and therefore reduce
fees. By default Bitcash will consolidate UTXOs.
:type combine: ``bool``
:param message: A message to include in the transaction. This will be
stored in the blockchain forever. Due to size limits,
each message will be stored in chunks of 220 bytes.
:type message: ``str``
:param unspents: The UTXOs to use as the inputs. By default Bitcash will
communicate with the blockchain itself.
:type unspents: ``list`` of :class:`~bitcash.network.meta.Unspent`
:returns: The signed transaction as hex.
:rtype: ``str``
"""
unspents, outputs = sanitize_tx_data(
unspents or self.get_unspents(),
outputs,
fee or DEFAULT_FEE,
leftover or self.cashtoken_address,
combine=combine,
message=message,
compressed=self.is_compressed(),
custom_pushdata=custom_pushdata,
)
return create_p2pkh_transaction(self, unspents, outputs)
def send(
self,
outputs,
fee=None,
leftover=None,
combine=True,
message=None,
unspents=None,
): # pragma: no cover
"""Creates a signed P2PKH transaction and attempts to broadcast it on
the blockchain. This accepts the same arguments as
:func:`~bitcash.PrivateKey.create_transaction`.
:param outputs: A sequence of outputs you wish to send in the form
``(destination, amount, currency)``. The amount can
be either an int, float, or string as long as it is
a valid input to ``decimal.Decimal``. The currency
must be :ref:`supported <supported currencies>`.
To send CashToken, the list of output is made in the
form ``(destination, amount, currency, category_id,
nft_capability, nft_commitment, token_amount)``. The category_id
is hex of tx-id as ``str``. The nft_capability is the capability
of non-fungible token in ("none", "mutable", "minting"). The
nft_commitment is the commitment of the non-fungible token in
``bytes``.
The CashToken property nft_capability, nft_commitment, or
the token_amount can be None if not to be sent. If
category_id is tx-id of unspent with tx-index 0, then
tx is treated as a genesis tx.
:type outputs: ``list`` of ``tuple``
:param fee: The number of satoshi per byte to pay to miners. By default
Bitcash will poll `<https://bitcoincashfees.earn.com>`_ and use a fee
that will allow your transaction to be confirmed as soon as
possible.
:type fee: ``int``
:param leftover: The destination that will receive any change from the
transaction. By default Bitcash will send any change to
the same address you sent from.
:type leftover: ``str``
:param combine: Whether or not Bitcash should use all available UTXOs to
make future transactions smaller and therefore reduce
fees. By default Bitcash will consolidate UTXOs.
:type combine: ``bool``
:param message: A message to include in the transaction. This will be
stored in the blockchain forever. Due to size limits,
each message will be stored in chunks of 220 bytes.
:type message: ``str``
:param unspents: The UTXOs to use as the inputs. By default Bitcash will
communicate with the blockchain itself.
:type unspents: ``list`` of :class:`~bitcash.network.meta.Unspent`
:returns: The transaction ID.
:rtype: ``str``
"""
tx_hex = self.create_transaction(
outputs,
fee=fee,
leftover=leftover,
combine=combine,
message=message,
unspents=unspents,
)
NetworkAPI.broadcast_tx(tx_hex, network=NETWORKS[self._network])
return calc_txid(tx_hex)
@classmethod
def prepare_transaction(
cls,
address,
outputs,
compressed=True,
fee=None,
leftover=None,
combine=True,
message=None,
unspents=None,
): # pragma: no cover
"""Prepares a P2PKH transaction for offline signing.
:param address: The address the funds will be sent from.
:type address: ``str``
:param outputs: A sequence of outputs you wish to send in the form
``(destination, amount, currency)``. The amount can
be either an int, float, or string as long as it is
a valid input to ``decimal.Decimal``. The currency
must be :ref:`supported <supported currencies>`.
To send CashToken, the list of output is made in the
form ``(destination, amount, currency, category_id,
nft_capability, nft_commitment, token_amount)``. The category_id
is hex of tx-id as ``str``. The nft_capability is the capability
of non-fungible token in ("none", "mutable", "minting"). The
nft_commitment is the commitment of the non-fungible token in
``bytes``.
The CashToken property nft_capability, nft_commitment, or
the token_amount can be None if not to be sent. If
category_id is tx-id of unspent with tx-index 0, then
tx is treated as a genesis tx.
:type outputs: ``list`` of ``tuple``
:param compressed: Whether or not the ``address`` corresponds to a
compressed public key. This influences the fee.
:type compressed: ``bool``
:param fee: The number of satoshi per byte to pay to miners. By default
Bitcash will poll `<https://bitcoincashfees.earn.com>`_ and use a fee
that will allow your transaction to be confirmed as soon as
possible.
:type fee: ``int``
:param leftover: The destination that will receive any change from the
transaction. By default Bitcash will send any change to
the same address you sent from.
:type leftover: ``str``
:param combine: Whether or not Bitcash should use all available UTXOs to
make future transactions smaller and therefore reduce
fees. By default Bitcash will consolidate UTXOs.
:type combine: ``bool``
:param message: A message to include in the transaction. This will be
stored in the blockchain forever. Due to size limits,
each message will be stored in chunks of 220 bytes.
:type message: ``str``
:param unspents: The UTXOs to use as the inputs. By default Bitcash will
communicate with the blockchain itself.
:type unspents: ``list`` of :class:`~bitcash.network.meta.Unspent`
:returns: JSON storing data required to create an offline transaction.
:rtype: ``str``
"""
unspents, outputs = sanitize_tx_data(
unspents or NetworkAPI.get_unspent(address),
outputs,
fee or DEFAULT_FEE,
leftover or address,
combine=combine,
message=message,
compressed=compressed,
)
outputs = list(map(list, outputs))
for output in outputs:
# script
output[0] = output[0].hex()
# nft_commitment
if output[4] is not None:
output[4] = output[4].hex()
data = {
"unspents": [unspent.to_dict() for unspent in unspents],
"outputs": outputs,
}
return json.dumps(data, separators=(",", ":"))
def sign_transaction(self, tx_data): # pragma: no cover
"""Creates a signed P2PKH transaction using previously prepared
transaction data.
:param tx_data: Output of :func:`~bitcash.PrivateKey.prepare_transaction`.
:type tx_data: ``str``
:returns: The signed transaction as hex.
:rtype: ``str``
"""
data = json.loads(tx_data)
unspents = [Unspent.from_dict(unspent) for unspent in data["unspents"]]
outputs = data["outputs"]
for output in outputs:
# script
output[0] = bytes.fromhex(output[0])
# nft_commitment
if output[4] is not None:
output[4] = bytes.fromhex(output[4])
outputs = list(map(tuple, outputs))
return create_p2pkh_transaction(self, unspents, outputs)
@classmethod
def from_hex(cls, hexed):
"""
:param hexed: A private key previously encoded as hex.
:type hexed: ``str``
:rtype: :class:`~bitcash.PrivateKey`
"""
return PrivateKey(ECPrivateKey.from_hex(hexed))
@classmethod
def from_bytes(cls, bytestr):
"""
:param bytestr: A private key previously encoded as hex.
:type bytestr: ``bytes``
:rtype: :class:`~bitcash.PrivateKey`
"""
return PrivateKey(ECPrivateKey(bytestr))
@classmethod
def from_der(cls, der):
"""
:param der: A private key previously encoded as DER.
:type der: ``bytes``
:rtype: :class:`~bitcash.PrivateKey`
"""
return PrivateKey(ECPrivateKey.from_der(der))
@classmethod
def from_pem(cls, pem):
"""
:param pem: A private key previously encoded as PEM.
:type pem: ``bytes``
:rtype: :class:`~bitcash.PrivateKey`
"""
return PrivateKey(ECPrivateKey.from_pem(pem))
@classmethod
def from_int(cls, num):
"""
:param num: A private key in raw integer form.
:type num: ``int``
:rtype: :class:`~bitcash.PrivateKey`
"""
return PrivateKey(ECPrivateKey.from_int(num))
def __repr__(self):
return f"<PrivateKey: {self.address}>"
class PrivateKeyTestnet(PrivateKey):
"""This class represents a testnet BitcoinCash private key. **Note:** coins
on the test network have no monetary value!
:param wif: A private key serialized to the Wallet Import Format. If the
argument is not supplied, a new private key will be created.
The WIF compression flag will be adhered to, but the version
byte is disregarded. Compression will be used by all new keys.
:type wif: ``str``
:raises TypeError: If ``wif`` is not a ``str``.
"""
def __init__(self, wif=None, network="test"):
super().__init__(wif=wif, network=network)
@classmethod
def from_hex(cls, hexed):
"""
:param hexed: A private key previously encoded as hex.
:type hexed: ``str``
:rtype: :class:`~bitcash.PrivateKeyTestnet`
"""
return PrivateKeyTestnet(ECPrivateKey.from_hex(hexed))
@classmethod
def from_bytes(cls, bytestr):
"""
:param bytestr: A private key previously encoded as hex.
:type bytestr: ``bytes``
:rtype: :class:`~bitcash.PrivateKeyTestnet`
"""
return PrivateKeyTestnet(ECPrivateKey(bytestr))
@classmethod
def from_der(cls, der):
"""
:param der: A private key previously encoded as DER.
:type der: ``bytes``
:rtype: :class:`~bitcash.PrivateKeyTestnet`
"""
return PrivateKeyTestnet(ECPrivateKey.from_der(der))
@classmethod
def from_pem(cls, pem):
"""
:param pem: A private key previously encoded as PEM.
:type pem: ``bytes``
:rtype: :class:`~bitcash.PrivateKeyTestnet`
"""
return PrivateKeyTestnet(ECPrivateKey.from_pem(pem))
@classmethod
def from_int(cls, num):
"""
:param num: A private key in raw integer form.
:type num: ``int``
:rtype: :class:`~bitcash.PrivateKeyTestnet`
"""
return PrivateKeyTestnet(ECPrivateKey.from_int(num))
def __repr__(self):
return f"<PrivateKeyTestnet: {self.address}>"
class PrivateKeyRegtest(PrivateKey):
"""This class represents a regtest BitcoinCash private key. **Note:** coins
on the regtest network have no monetary value!
:param wif: A private key serialized to the Wallet Import Format. If the
argument is not supplied, a new private key will be created.
The WIF compression flag will be adhered to, but the version
byte is disregarded. Compression will be used by all new keys.
:type wif: ``str``
:raises TypeError: If ``wif`` is not a ``str``.
"""
def __init__(self, wif=None, network="regtest"):
super().__init__(wif, network)
@classmethod
def from_hex(cls, hexed):
"""
:param hexed: A private key previously encoded as hex.
:type hexed: ``str``
:rtype: :class:`~bitcash.PrivateKeyRegtest`
"""
return PrivateKeyRegtest(ECPrivateKey.from_hex(hexed))
@classmethod
def from_bytes(cls, bytestr):
"""
:param bytestr: A private key previously encoded as hex.
:type bytestr: ``bytes``
:rtype: :class:`~bitcash.PrivateKeyRegtest`
"""
return PrivateKeyRegtest(ECPrivateKey(bytestr))
@classmethod
def from_der(cls, der):
"""
:param der: A private key previously encoded as DER.
:type der: ``bytes``
:rtype: :class:`~bitcash.PrivateKeyRegtest`
"""
return PrivateKeyRegtest(ECPrivateKey.from_der(der))
@classmethod
def from_pem(cls, pem):
"""
:param pem: A private key previously encoded as PEM.
:type pem: ``bytes``
:rtype: :class:`~bitcash.PrivateKeyRegtest`
"""
return PrivateKeyRegtest(ECPrivateKey.from_pem(pem))
@classmethod
def from_int(cls, num):
"""
:param num: A private key in raw integer form.
:type num: ``int``
:rtype: :class:`~bitcash.PrivateKeyRegtest`
"""
return PrivateKeyRegtest(ECPrivateKey.from_int(num))
def __repr__(self):
return f"<PrivateKeyRegtest: {self.address}>"
Key = PrivateKey