Skip to content

Commit

Permalink
Fix docstring examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryp Toon committed Jun 10, 2024
1 parent 51cdbee commit 7c01000
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
16 changes: 8 additions & 8 deletions bitcoinlib/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def get_key_format(key, is_private=None):
This method does not validate if a key is valid.
>>> get_key_format('L4dTuJf2ceEdWDvCPsLhYf8GiiuYqXtqfbcKdC21BPDvEM1ykJRC')
{'format': 'wif_compressed', 'networks': ['bitcoin', 'regtest'], 'is_private': True, 'script_types': [], 'witness_types': ['legacy'], 'multisig': [False]}
{'format': 'wif_compressed', 'networks': ['bitcoin', 'regtest'], 'is_private': True, 'script_types': [], 'witness_types': ['segwit'], 'multisig': [False]}
>>> get_key_format('becc7ac3b383cd609bd644aa5f102a811bac49b6a34bbd8afe706e32a9ac5c5e')
{'format': 'hex', 'networks': None, 'is_private': True, 'script_types': [], 'witness_types': ['legacy'], 'multisig': [False]}
{'format': 'hex', 'networks': None, 'is_private': True, 'script_types': [], 'witness_types': ['segwit'], 'multisig': [False]}
>>> get_key_format('Zpub6vZyhw1ShkEwNxtqfjk7jiwoEbZYMJdbWLHvEwo6Ns2fFc9rdQn3SerYFQXYxtZYbA8a1d83shW3g4WbsnVsymy2L8m7wpeApiuPxug3ARu')
{'format': 'hdkey_public', 'networks': ['bitcoin', 'regtest'], 'is_private': False, 'script_types': ['p2wsh'], 'witness_types': ['segwit'], 'multisig': [True]}
Expand Down Expand Up @@ -242,7 +242,7 @@ def deserialize_address(address, encoding=None, network=None):
If more networks and or script types are found you can find these in the 'networks' field.
>>> deserialize_address('1Khyc5eUddbhYZ8bEZi9wiN8TrmQ8uND4j')
{'address': '1Khyc5eUddbhYZ8bEZi9wiN8TrmQ8uND4j', 'encoding': 'base58', 'public_key_hash': 'cd322766c02e7c37c3e3f9b825cd41ffbdcd17d7', 'public_key_hash_bytes': b"\\xcd2'f\\xc0.|7\\xc3\\xe3\\xf9\\xb8%\\xcdA\\xff\\xbd\\xcd\\x17\\xd7", 'prefix': b'\\x00', 'network': 'bitcoin', 'script_type': 'p2pkh', 'witness_type': 'legacy', 'networks': ['bitcoin', 'regtest'], 'witver': None}
{'address': '1Khyc5eUddbhYZ8bEZi9wiN8TrmQ8uND4j', 'encoding': 'base58', 'public_key_hash': 'cd322766c02e7c37c3e3f9b825cd41ffbdcd17d7', 'public_key_hash_bytes': b"\\xcd2'f\\xc0.|7\\xc3\\xe3\\xf9\\xb8%\\xcdA\\xff\\xbd\\xcd\\x17\\xd7", 'prefix': b'\\x00', 'network': 'bitcoin', 'script_type': 'p2pkh', 'witness_type': 'legacy', 'networks': ['bitcoin', 'regtest'], 'checksum': b'\xcf\xa4I0', 'witver': None}
:param address: A base58 or bech32 encoded address
:type address: str
Expand Down Expand Up @@ -1730,7 +1730,7 @@ def __init__(self, import_key=None, key=None, chain=None, depth=0, parent_finger
>>> private_hex = '221ff330268a9bb5549a02c801764cffbc79d5c26f4041b26293a425fd5b557c'
>>> k = HDKey(private_hex)
>>> k
<HDKey(public_hex=0363c152144dcd5253c1216b733fdc6eb8a94ab2cd5caa8ead5e59ab456ff99927, wif_public=xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6SmypHzZG2cYrwpGkWJqRxS6EAW77gd7CHFoXNpBd3LN8xjAyCW, network=bitcoin)>
<HDKey(public_hex=0363c152144dcd5253c1216b733fdc6eb8a94ab2cd5caa8ead5e59ab456ff99927, wif_public=zpub6jftahH18ngZw8pNbq6arfqFD7przPySpW3jhhzQiBKYpzJxqwhBEpCk8rh9p7JQ5JrAMu1Pfq1wCXfDfZL8i9zJvxeehCTAAVTev5oZKCn, network=bitcoin)>
:param import_key: HD Key to import in WIF format or as byte with key (32 bytes) and chain (32 bytes)
:type import_key: str, bytes, int, tuple
Expand Down Expand Up @@ -1980,7 +1980,7 @@ def wif(self, is_private=None, child_index=None, prefix=None, witness_type=None,
>>> private_hex = '221ff330268a9bb5549a02c801764cffbc79d5c26f4041b26293a425fd5b557c'
>>> k = HDKey(private_hex)
>>> k.wif()
'xpub661MyMwAqRbcEYS8w7XLSVeEsBXy79zSzH1J8vCdxAZningWLdN3zgtU6SmypHzZG2cYrwpGkWJqRxS6EAW77gd7CHFoXNpBd3LN8xjAyCW'
'zpub6jftahH18ngZw8pNbq6arfqFD7przPySpW3jhhzQiBKYpzJxqwhBEpCk8rh9p7JQ5JrAMu1Pfq1wCXfDfZL8i9zJvxeehCTAAVTev5oZKCn'
:param is_private: Return public or private key
:type is_private: bool
Expand Down Expand Up @@ -2149,7 +2149,7 @@ def public_master(self, account_id=0, purpose=None, multisig=None, witness_type=
>>> k = HDKey(private_hex)
>>> pm = k.public_master()
>>> pm.wif()
'xpub6CjFexgdDZEtHdW7V4LT8wS9rtG3m187pM9qhTpoZdViFhSv3tW9sWonQNtFN1TCkRGAQGKj1UC2ViHTqb7vJV3X67xSKuCDzv14tBHR3Y7'
'zpub6qN4WhSaerCBXv28QdRXwY4EvnAUbCSLCWUF5ZNSsyrupWwBRv4irjjQ6vpz5WFM7Z7zyy3eQBzijfszpvqdoiyAVLc44MsL4mVQDTwSHpT'
:param account_id: Account ID. Leave empty for account 0
:type account_id: int
Expand Down Expand Up @@ -2221,7 +2221,7 @@ def child_private(self, index=0, hardened=False, network=None):
>>> k = HDKey(private_hex)
>>> ck = k.child_private(10)
>>> ck.address()
'1FgHK5JUa87ASxz5mz3ypeaUV23z9yW654'
'bc1q5rlenzyn95pt4rur6jcnrgx5s3u6kw0nnwjrtt'
>>> ck.depth
1
>>> ck.child_index
Expand Down Expand Up @@ -2272,7 +2272,7 @@ def child_public(self, index=0, network=None):
>>> k = HDKey(private_hex)
>>> ck = k.child_public(15)
>>> ck.address()
'1PfLJJgKs8nUbMPpaQUucbGmr8qyNSMGeK'
'bc1qlzf2a6tskk8g6g55nsda7akmwsevasv5p4muuf'
>>> ck.depth
1
>>> ck.child_index
Expand Down
4 changes: 2 additions & 2 deletions bitcoinlib/networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def network_values_for(field):
Return all prefixes for field, i.e.: prefix_wif, prefix_address_p2sh, etc
>>> network_values_for('prefix_wif')
[b'\\x99', b'\\x80', b'\\xef', b'\\xb0', b'\\xcc', b'\\x9e', b'\\xf1']
[b'\\x99', b'\\x80', b'\\xef', b'\\xb0', b'\\x9e', b'\\xf1']
>>> network_values_for('prefix_address_p2sh')
[b'\\x95', b'\\x05', b'\\xc4', b'2', b':', b'\\x10', b'\\x13', b'\\x16']
[b'\\x95', b'\\x05', b'\\xc4', b'2', b':', b'\\x16']
:param field: Prefix name from networks definitions (networks.json)
:type field: str
Expand Down
12 changes: 6 additions & 6 deletions bitcoinlib/scripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def __init__(self, commands=None, message=None, script_types='', is_locking=True
>>> s = Script([op.op_2, op.op_4, op.op_add])
>>> s
<Script([op.op_2, op.op_4, op.op_add])>
<Script([op_2, op_4, op_add])>
>>> s.blueprint
[82, 84, 147]
>>> s.evaluate()
Expand Down Expand Up @@ -259,7 +259,7 @@ def parse(cls, script, message=None, env_data=None, is_locking=None, strict=True
Wrapper for the :func:`parse_bytesio` method. Convert hexadecimal string or bytes script to BytesIO.
>>> Script.parse('76a914af8e14a2cecd715c363b3a72b55b59a31e2acac988ac')
<Script([op.op_dup, op.op_hash160, data-20, op.op_equalverify, op.op_checksig])>
<Script([op_dup, op_hash160, data-20, op_equalverify, op_checksig])>
:param script: Raw script to parse in bytes, BytesIO or hexadecimal string format
:type script: BytesIO, bytes, str
Expand Down Expand Up @@ -449,7 +449,7 @@ def parse_hex(cls, script, message=None, env_data=None, is_locking=None, strict=
Wrapper for the :func:`parse_bytesio` method. Convert hexadecimal string script to BytesIO.
>>> Script.parse_hex('76a914af8e14a2cecd715c363b3a72b55b59a31e2acac988ac')
<Script([op.op_dup, op.op_hash160, data-20, op.op_equalverify, op.op_checksig])>
<Script([op_dup, op_hash160, data-20, op_equalverify, op_checksig])>
:param script: Raw script to parse in hexadecimal string format
:type script: str
Expand Down Expand Up @@ -503,7 +503,7 @@ def parse_str(cls, script, message=None, env_data=None, is_locking=None, strict=
>>> s = Script.parse_str("1 98 OP_ADD 99 OP_EQUAL")
>>> s
data-1 data-1 OP_ADD data-1 OP_EQUAL
<Script([data-1, data-1, op_add, data-1, op_equal])>
>>> s.evaluate()
True
Expand Down Expand Up @@ -539,7 +539,7 @@ def parse_str(cls, script, message=None, env_data=None, is_locking=None, strict=

def __repr__(self):
s_items = self.view(blueprint=True, as_list=True)
return '<Script([' + ', '.join(s_items) + '])>'
return '<Script([' + ', '.join(s_items).lower() + '])>'

def __str__(self):
return self.view(blueprint=True)
Expand Down Expand Up @@ -671,7 +671,7 @@ def evaluate(self, message=None, env_data=None, trace=False):
>>> s = Script([op.op_2, op.op_4, op.op_add])
>>> s
<Script([op.op_2, op.op_4, op.op_add])>
<Script([op_2, op_4, op_add])>
>>> s.blueprint
[82, 84, 147]
>>> s.evaluate()
Expand Down
23 changes: 10 additions & 13 deletions bitcoinlib/wallets.py
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,9 @@ def from_key(name, wallet_id, session, key, account_id=0, network=None, change=0
>>> wif = 'xprv9s21ZrQH143K2mcs9jcK4EjALbu2z1N9qsMTUG1frmnXM3NNCSGR57yLhwTccfNCwdSQEDftgjCGm96P29wGGcbBsPqZH85iqpoHA7LrqVy'
>>> wk = WalletKey.from_key('import_key', w.wallet_id, w.session, wif)
>>> wk.address
'1MwVEhGq6gg1eeSrEdZom5bHyPqXtJSnPg'
'bc1qukcgc3guzt0a27j7vdegtgwxsrv4khc4688ycs'
>>> wk # doctest:+ELLIPSIS
<WalletKey(key_id=..., name=import_key, wif=xprv9s21ZrQH143K2mcs9jcK4EjALbu2z1N9qsMTUG1frmnXM3NNCSGR57yLhwTccfNCwdSQEDftgjCGm96P29wGGcbBsPqZH85iqpoHA7LrqVy, path=m)>
<WalletKey(key_id=..., name=import_key, wif=zprvAWgYBBk7JR8GjN16pTBZUQvAgYBvsFM9g6Pu33oScnYHTEzphkbYKFHckMNncUg3kug1jAs1c3uNXiKWTYmHs5xPc5EQSwihPGvZwGiZeKD, path=m)>
:param name: New key name
:type name: str
Expand Down Expand Up @@ -1886,7 +1886,7 @@ def new_keys(self, name='', account_id=None, change=0, cosigner_id=None, witness
>>> w = Wallet('create_legacy_wallet_test')
>>> w.new_key('my key') # doctest:+ELLIPSIS
<WalletKey(key_id=..., name=my key, wif=..., path=m/44'/0'/0'/0/...)>
<WalletKey(key_id=..., name=my key, wif=..., path=m/84'/0'/0'/0/...)>
:param name: Key name. Does not have to be unique but if you use it at reference you might chooce to enforce this. If not specified 'Key #' with a unique sequence number will be used
:type name: str
Expand Down Expand Up @@ -2109,7 +2109,7 @@ def get_key(self, account_id=None, witness_type=None, network=None, cosigner_id=
>>> w = Wallet('create_legacy_wallet_test')
>>> w.get_key() # doctest:+ELLIPSIS
<WalletKey(key_id=..., name=..., wif=..., path=m/44'/0'/0'/0/...)>
<WalletKey(key_id=..., name=..., wif=..., path=m/84'/0'/0'/0/...)>
:param account_id: Account ID. Default is last used or created account ID.
:type account_id: int
Expand Down Expand Up @@ -2253,11 +2253,11 @@ def path_expand(self, path, level_offset=None, account_id=None, cosigner_id=0, a
>>> w = Wallet('create_legacy_wallet_test')
>>> w.path_expand([0,1200])
['m', "44'", "0'", "0'", '0', '1200']
['m', "84'", "0'", "0'", '0', '1200']
>>> w = Wallet('create_legacy_multisig_wallet_test')
>>> w.path_expand([0,2], cosigner_id=1)
['m', "45'", '1', '0', '2']
['m', "48'", "0'", "0'", "2'", '0', '2']
:param path: Part of path, for example [0, 2] for change=0 and address_index=2
:type path: list, str
Expand Down Expand Up @@ -2321,18 +2321,18 @@ def keys_for_path(self, path, level_offset=None, name=None, account_id=None, cos
>>> w = wallet_create_or_open('key_for_path_example')
>>> key = w.key_for_path([0, 0])
>>> key.path
"m/44'/0'/0'/0/0"
"m/84'/0'/0'/0/0"
>>> w.key_for_path([], level_offset=-2).path
"m/44'/0'/0'"
"m/84'/0'/0'"
>>> w.key_for_path([], w.depth_public_master + 1).path
"m/44'/0'/0'"
"m/84'/0'/0'"
Arguments provided in 'path' take precedence over other arguments. The address_index argument is ignored:
>>> key = w.key_for_path([0, 10], address_index=1000)
>>> key.path
"m/44'/0'/0'/0/10"
"m/84'/0'/0'/0/10"
>>> key.address_index
10
Expand Down Expand Up @@ -2589,9 +2589,6 @@ def keys_networks(self, used=None, as_dict=False):
>>> w = Wallet('bitcoinlib_legacy_wallet_test')
>>> network_key = w.keys_networks()
>>> # Address index of hardened key 0' is 2147483648
>>> network_key[0].address_index
2147483648
>>> network_key[0].path
"m/44'/0'"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -759,7 +759,7 @@ def test_script_add(self):
def test_script_create_simple(self):
script = Script([op.op_2, op.op_5, op.op_sub, op.op_1])
self.assertEqual(str(script), 'OP_2 OP_5 OP_SUB OP_1')
self.assertEqual(repr(script), '<Script([OP_2, OP_5, OP_SUB, OP_1])>')
self.assertEqual(repr(script), '<Script([op_2, op_5, op_sub, op_1])>')
self.assertEqual(script.serialize().hex(), '52559451')
self.assertEqual(script.serialize_list(), [b'R', b'U', b'\x94', b'Q'])
self.assertTrue(script.evaluate())
Expand Down

0 comments on commit 7c01000

Please sign in to comment.