|
1 | 1 | from graphenebase.account import (
|
2 |
| - PasswordKey as GPHPasswordKey, |
3 |
| - BrainKey as GPHBrainKey, |
4 |
| - Address as GPHAddress, |
5 |
| - PublicKey as GPHPublicKey, |
6 |
| - PrivateKey as GPHPrivateKey |
| 2 | + PasswordKey, |
| 3 | + BrainKey, |
| 4 | + Address, |
| 5 | + PublicKey, |
| 6 | + PrivateKey, |
| 7 | + Prefix |
7 | 8 | )
|
8 | 9 |
|
9 |
| -import sys |
10 |
| -import hashlib |
11 |
| -from binascii import hexlify, unhexlify |
12 |
| - |
13 |
| - |
14 |
| -class PasswordKey(GPHPasswordKey): |
15 |
| - """ This class derives a private key given the account name, the |
16 |
| - role and a password. It leverages the technology of Brainkeys |
17 |
| - and allows people to have a secure private key by providing a |
18 |
| - passphrase only. |
19 |
| - """ |
20 |
| - |
21 |
| - def __init__(self, *args, **kwargs): |
22 |
| - super(PasswordKey, self).__init__(*args, **kwargs) |
23 |
| - |
24 |
| - # overloaded from GHPPasswordKey, JUST to set prefix='BTS' :( |
25 |
| - def get_private(self): |
26 |
| - """ Derive private key from the brain key and the current sequence |
27 |
| - number |
28 |
| - """ |
29 |
| - if sys.version > '3': |
30 |
| - a = bytes(self.account + self.role + self.password, 'utf8') |
31 |
| - else: |
32 |
| - a = bytes(self.account + self.role + self.password).encode('utf8') |
33 |
| - s = hashlib.sha256(a).digest() |
34 |
| - return PrivateKey(hexlify(s).decode('ascii')) |
35 |
| - |
36 |
| - |
37 |
| -class BrainKey(GPHBrainKey): |
38 |
| - """Brainkey implementation similar to the graphene-ui web-wallet. |
39 |
| -
|
40 |
| - :param str brainkey: Brain Key |
41 |
| - :param int sequence: Sequence number for consecutive keys |
42 |
| -
|
43 |
| - Keys in Graphene are derived from a seed brain key which is a string of |
44 |
| - 16 words out of a predefined dictionary with 49744 words. It is a |
45 |
| - simple single-chain key derivation scheme that is not compatible with |
46 |
| - BIP44 but easy to use. |
47 |
| -
|
48 |
| - Given the brain key, a private key is derived as:: |
49 |
| -
|
50 |
| - privkey = SHA256(SHA512(brainkey + " " + sequence)) |
51 |
| -
|
52 |
| - Incrementing the sequence number yields a new key that can be |
53 |
| - regenerated given the brain key. |
54 |
| - """ |
55 |
| - |
56 |
| - def __init__(self, *args, **kwargs): |
57 |
| - super(BrainKey, self).__init__(*args, **kwargs) |
58 |
| - |
59 |
| - # overloaded from GHPBrainKey, JUST to set prefix='BTS' :( |
60 |
| - def get_private(self): |
61 |
| - """ Derive private key from the brain key and the current sequence |
62 |
| - number |
63 |
| - """ |
64 |
| - encoded = "%s %d" % (self.brainkey, self.sequence) |
65 |
| - if sys.version > '3': |
66 |
| - a = bytes(encoded, 'ascii') |
67 |
| - else: |
68 |
| - a = bytes(encoded).encode('ascii') |
69 |
| - s = hashlib.sha256(hashlib.sha512(a).digest()).digest() |
70 |
| - return PrivateKey(hexlify(s).decode('ascii')) |
71 |
| - |
72 |
| - def get_blind_private(self): |
73 |
| - """ Derive private key from the brain key (and no sequence number) |
74 |
| - """ |
75 |
| - if sys.version > '3': |
76 |
| - a = bytes(self.brainkey, 'ascii') |
77 |
| - else: |
78 |
| - a = bytes(self.brainkey).encode('ascii') |
79 |
| - return PrivateKey(hashlib.sha256(a).hexdigest()) |
80 |
| - |
81 |
| - |
82 |
| -class Address(GPHAddress): |
83 |
| - """ Address class |
84 |
| -
|
85 |
| - This class serves as an address representation for Public Keys. |
86 |
| -
|
87 |
| - :param str address: Base58 encoded address (defaults to ``None``) |
88 |
| - :param str pubkey: Base58 encoded pubkey (defaults to ``None``) |
89 |
| - :param str prefix: Network prefix (defaults to ``BTS``) |
90 |
| -
|
91 |
| - Example:: |
92 |
| -
|
93 |
| - Address("BTSFN9r6VYzBK8EKtMewfNbfiGCr56pHDBFi") |
94 |
| -
|
95 |
| - """ |
96 |
| - def __init__(self, *args, **kwargs): |
97 |
| - if "prefix" not in kwargs: |
98 |
| - kwargs["prefix"] = "BTS" # make prefix BTS |
99 |
| - super(Address, self).__init__(*args, **kwargs) |
100 |
| - |
101 |
| - |
102 |
| -class PublicKey(GPHPublicKey): |
103 |
| - """ This class deals with Public Keys and inherits ``Address``. |
104 |
| -
|
105 |
| - :param str pk: Base58 encoded public key |
106 |
| - :param str prefix: Network prefix (defaults to ``BTS``) |
107 |
| -
|
108 |
| - Example::: |
109 |
| -
|
110 |
| - PublicKey("BTS6UtYWWs3rkZGV8JA86qrgkG6tyFksgECefKE1MiH4HkLD8PFGL") |
111 |
| -
|
112 |
| - .. note:: By default, graphene-based networks deal with **compressed** |
113 |
| - public keys. If an **uncompressed** key is required, the |
114 |
| - method ``unCompressed`` can be used:: |
115 |
| -
|
116 |
| - PublicKey("xxxxx").unCompressed() |
117 |
| -
|
118 |
| - """ |
119 |
| - def __init__(self, *args, **kwargs): |
120 |
| - if "prefix" not in kwargs: |
121 |
| - kwargs["prefix"] = "BTS" # make prefix BTS |
122 |
| - super(PublicKey, self).__init__(*args, **kwargs) |
123 |
| - |
124 |
| - |
125 |
| -class PrivateKey(GPHPrivateKey): |
126 |
| - """ Derives the compressed and uncompressed public keys and |
127 |
| - constructs two instances of ``PublicKey``: |
128 |
| -
|
129 |
| - :param str wif: Base58check-encoded wif key |
130 |
| - :param str prefix: Network prefix (defaults to ``BTS``) |
131 |
| -
|
132 |
| - Example::: |
133 |
| -
|
134 |
| - PrivateKey("5HqUkGuo62BfcJU5vNhTXKJRXuUi9QSE6jp8C3uBJ2BVHtB8WSd") |
135 |
| -
|
136 |
| - Compressed vs. Uncompressed: |
137 |
| -
|
138 |
| - * ``PrivateKey("w-i-f").pubkey``: |
139 |
| - Instance of ``PublicKey`` using compressed key. |
140 |
| - * ``PrivateKey("w-i-f").pubkey.address``: |
141 |
| - Instance of ``Address`` using compressed key. |
142 |
| - * ``PrivateKey("w-i-f").uncompressed``: |
143 |
| - Instance of ``PublicKey`` using uncompressed key. |
144 |
| - * ``PrivateKey("w-i-f").uncompressed.address``: |
145 |
| - Instance of ``Address`` using uncompressed key. |
146 |
| -
|
147 |
| - """ |
148 |
| - def __init__(self, *args, **kwargs): |
149 |
| - if "prefix" not in kwargs: |
150 |
| - kwargs["prefix"] = "BTS" # make prefix BTS |
151 |
| - super(PrivateKey, self).__init__(*args, **kwargs) |
| 10 | +# Redefine default prefix |
| 11 | +Prefix.prefix = "BTS" |
0 commit comments