basic examples

This commit is contained in:
4tochka 2018-03-28 13:25:45 +04:00
parent ed01d12876
commit 891826d12d
12 changed files with 97 additions and 13 deletions

View File

@ -1,2 +1,79 @@
# pybtc # pybtc
Python bitcoin library Python bitcoin library
### Basic Examples
#### Create private key
>>> from pybtc import *
>>> create_priv()
b'\xc8\xf5tGf\x00+4\x1c\xe3\xb6\x00\xf4\x14w\x1d\xf0{jiY&4`v\xd4\tmv!\x0f\x1f'
>>> priv = create_priv()
>>> priv
b'_`\xd7@\x9e\xdb\xbbB5O%@\xd6\x92\xb1\x0e*\xcd\xb6\x89!\xa3JE\xb0\xb6:\x8c\x04\x88\xc9\xa5'
>>> priv2WIF(priv)
'KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW'
>>> priv2WIF(priv, compressed = False) # Mainnet compressed WIF format
'5JYHtgBjYbLT3ZkhGHHCivscdMdDKeVTZBgq5ZK51fyKpqKDhYv' # Mainnet uncompressed WIF format
>>> priv2WIF(priv, testnet = True)
'cQn71zxEDWF77m386rfe7PHTshLF3kaJH6KGKY5GD5fcLsCqpPbg'
>>> priv2WIF(priv, compressed = True, testnet = True) # Testnet compressed WIF format
'cQn71zxEDWF77m386rfe7PHTshLF3kaJH6KGKY5GD5fcLsCqpPbg'
>>> priv2WIF(priv, compressed = False, testnet = True) # Testnet uncompressed WIF format
'92JvUR1H8pQb1dFytdB7bXRaH1yvUp2eu8YnABfaMQiNbuKiPVL'
>>>
>>> WIF2priv("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW")
b'_`\xd7@\x9e\xdb\xbbB5O%@\xd6\x92\xb1\x0e*\xcd\xb6\x89!\xa3JE\xb0\xb6:\x8c\x04\x88\xc9\xa5'
#### Public key from private key
>>> from pybtc import *
>>> priv2pub("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW")
b'\x02\xb1-\xc2\x03u\xda\x00*7t\xb9c\xe4A\xdb\x1c\xe0\x89\xb8W\x13\x86\xbe\x82\xee(\x11nrj\xb06'
>>> priv2pub("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW", hex = True)
'02b12dc20375da002a3774b963e441db1ce089b8571386be82ee28116e726ab036'
>>>
>>> priv = WIF2priv("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW")
>>> priv
b'_`\xd7@\x9e\xdb\xbbB5O%@\xd6\x92\xb1\x0e*\xcd\xb6\x89!\xa3JE\xb0\xb6:\x8c\x04\x88\xc9\xa5'
>>>
>>> priv2pub(priv, hex = True)
'02b12dc20375da002a3774b963e441db1ce089b8571386be82ee28116e726ab036'
>>>
#### Address from public key/private key
>>> from pybtc import *
>>> # address in bech32 format
...
>>> pub2address(priv2pub("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW"))
'bc1q3hs6985qftzrvfl7aqcshsf7equapuuxzr2kcv'
>>>
>>> # address in legacy format
...
>>> pub2address(priv2pub("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW"), witness_version = None)
'1DwCaTcMTT5kZmH4wCevDe5nyzffi2Bz9p'
>>>
>>> # uncompressed public key deprecated for bech32 segwit addresses fromat
...
>>> pub2address(priv2pub("5JYHtgBjYbLT3ZkhGHHCivscdMdDKeVTZBgq5ZK51fyKpqKDhYv"))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/site-packages/pybtc/tools.py", line 233, in pub2address
assert len(pubkey) == 33
AssertionError
>>>
>>> # uncompressed public key legacy format
...
>>> pub2address(priv2pub("5JYHtgBjYbLT3ZkhGHHCivscdMdDKeVTZBgq5ZK51fyKpqKDhYv"), witness_version = None)
'1EbTeoa1QgZaSHZFznrhNdKrRbbQupVwuZ'
>>>
>>> # testnet addresses
...
>>> pub2address(priv2pub("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW"), testnet = True)
'tb1q3hs6985qftzrvfl7aqcshsf7equapuuxg939rl'
>>> pub2address(priv2pub("KzR7Z5xNnSYqxKZriSrWk4nQFU2qPJUcD4AoD7ckhy1c68A4zvkW"), witness_version = None, testnet = True)
'mtT9sWhLGUX1LskgemdJ3ZJ7qzGNaygcXP'
>>>

View File

@ -34,7 +34,7 @@ def priv_from_int(k):
return int.to_bytes(k,byteorder="big",length=32) return int.to_bytes(k,byteorder="big",length=32)
def priv2WIF(h, compressed = False, testnet = False): def priv2WIF(h, compressed = True, testnet = False):
#uncompressed: 0x80 + [32-byte secret] + [4 bytes of Hash() of previous 33 bytes], base58 encoded #uncompressed: 0x80 + [32-byte secret] + [4 bytes of Hash() of previous 33 bytes], base58 encoded
#compressed: 0x80 + [32-byte secret] + 0x01 + [4 bytes of Hash() previous 34 bytes], base58 encoded #compressed: 0x80 + [32-byte secret] + 0x01 + [4 bytes of Hash() previous 34 bytes], base58 encoded
if type(h) == str: if type(h) == str:
@ -213,13 +213,19 @@ def address2script(address):
return OPCODE["OP_0"] + bytes([len(h)]) + h return OPCODE["OP_0"] + bytes([len(h)]) + h
raise Exception("Unknown address") raise Exception("Unknown address")
def script_P2SH_P2WPKH(pubkey, hash = False):
assert len(pubkey) == 33
if hash:
return hash160(b'\x00\x14' + hash160(pubkey))
return b'\x00\x14' + hash160(pubkey)
def pub2address(pubkey, testnet = False, def pub2address(pubkey, testnet = False,
inside_p2sh = False, p2sh_p2wpkh = False,
witness_version = 0): witness_version = 0):
if type(pubkey) == str: if type(pubkey) == str:
pubkey = unhexlify(pubkey) pubkey = unhexlify(pubkey)
if inside_p2sh: if p2sh_p2wpkh:
assert len(pubkey) == 33 assert len(pubkey) == 33
h = hash160(b'\x00\x14' + hash160(pubkey)) h = hash160(b'\x00\x14' + hash160(pubkey))
else: else:
@ -227,7 +233,7 @@ def pub2address(pubkey, testnet = False,
assert len(pubkey) == 33 assert len(pubkey) == 33
h = hash160(pubkey) h = hash160(pubkey)
return hash2address(h, testnet = testnet, return hash2address(h, testnet = testnet,
script_hash = inside_p2sh, script_hash = p2sh_p2wpkh,
witness_version = witness_version) witness_version = witness_version)
# def pub2P2SH_P2WPKH_hash(pubkey): # def pub2P2SH_P2WPKH_hash(pubkey):

View File

@ -1,4 +1,5 @@
import unittest import unittest
import test import test
testLoad = unittest.TestLoader() testLoad = unittest.TestLoader()

View File

@ -106,7 +106,7 @@ class AddressFunctionsTests(unittest.TestCase):
self.assertEqual(tools.pub2address(pc, witness_version=None, testnet=1), "mvNyptwisQTmwL3vN8VMaVUrA3swVCX83c") self.assertEqual(tools.pub2address(pc, witness_version=None, testnet=1), "mvNyptwisQTmwL3vN8VMaVUrA3swVCX83c")
p = "L32a8Mo1LgvjrVDbzcc3NkuUfkpoLsf2Y2oEWkV4t1KpQdFzuyff" p = "L32a8Mo1LgvjrVDbzcc3NkuUfkpoLsf2Y2oEWkV4t1KpQdFzuyff"
pk = tools.priv2pub(p) pk = tools.priv2pub(p)
self.assertEqual(tools.pub2address(pk, inside_p2sh=1,witness_version=None), "33am12q3Bncnn3BfvLYHczyv23Sq2Wbwjw") self.assertEqual(tools.pub2address(pk, p2sh_p2wpkh=1,witness_version=None), "33am12q3Bncnn3BfvLYHczyv23Sq2Wbwjw")
def test_is_address_valid(self): def test_is_address_valid(self):
self.assertEqual(tools.is_address_valid("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"), 1) self.assertEqual(tools.is_address_valid("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"), 1)

View File

@ -592,11 +592,11 @@ class BlockDeserializeTests(unittest.TestCase):
block = blockchain.Block.deserialize(block_e) block = blockchain.Block.deserialize(block_e)
print(">>>",block.bits) # print(">>>",block.bits)
print(">>>",block.hash) # print(">>>",block.hash)
print(">>>",block.timestamp) # print(">>>",block.timestamp)
target = int.from_bytes(block.bits[1:], 'big') * (2 ** (8 * (block.bits[0] - 3))) # target = int.from_bytes(block.bits[1:], 'big') * (2 ** (8 * (block.bits[0] - 3)))
print(int.from_bytes(block.hash, 'big')<target) # print(int.from_bytes(block.hash, 'big')<target)
# print(rh2s(block.transactions[0].hash)) # print(rh2s(block.transactions[0].hash))
# cbm = block.coinbase # cbm = block.coinbase

View File

@ -1,7 +1,7 @@
import unittest import unittest
from pybtc import blockchain from pybtc import blockchain
from binascii import unhexlify from binascii import unhexlify
from pybtc import address2hash as address2hash160 from pybtc import address2hash
class ScriptDeserializeTests(unittest.TestCase): class ScriptDeserializeTests(unittest.TestCase):
@ -14,7 +14,7 @@ class ScriptDeserializeTests(unittest.TestCase):
self.assertEqual(s.type, "P2PKH") self.assertEqual(s.type, "P2PKH")
self.assertEqual(s.ntype, 0) self.assertEqual(s.ntype, 0)
self.assertEqual(s.asm, "OP_DUP OP_HASH160 3520dd524f6ca66f63182bb23efff6cc8ee3ee63 OP_EQUALVERIFY OP_CHECKSIG") self.assertEqual(s.asm, "OP_DUP OP_HASH160 3520dd524f6ca66f63182bb23efff6cc8ee3ee63 OP_EQUALVERIFY OP_CHECKSIG")
self.assertEqual(s.address[0], address2hash160("15qvBdqSWQCuLQPXVoWViG2GvjeARmpYPw")) self.assertEqual(s.address[0], address2hash("15qvBdqSWQCuLQPXVoWViG2GvjeARmpYPw"))
self.assertEqual(s.pattern, "OP_DUP OP_HASH160 <20> OP_EQUALVERIFY OP_CHECKSIG") self.assertEqual(s.pattern, "OP_DUP OP_HASH160 <20> OP_EQUALVERIFY OP_CHECKSIG")
self.assertEqual(s.op_sig_count, 1) self.assertEqual(s.op_sig_count, 1)
@ -24,7 +24,7 @@ class ScriptDeserializeTests(unittest.TestCase):
self.assertEqual(s.type, "P2SH") self.assertEqual(s.type, "P2SH")
self.assertEqual(s.ntype, 1) self.assertEqual(s.ntype, 1)
self.assertEqual(s.asm, "OP_HASH160 69f37572ab1b69f304f987b119e2450e0b71bf5c OP_EQUAL") self.assertEqual(s.asm, "OP_HASH160 69f37572ab1b69f304f987b119e2450e0b71bf5c OP_EQUAL")
self.assertEqual(s.address[0], address2hash160("3BMEXVsYyfKB5h3m53XRSFHkqi1zPwsvcK")) self.assertEqual(s.address[0], address2hash("3BMEXVsYyfKB5h3m53XRSFHkqi1zPwsvcK"))
self.assertEqual(s.pattern, "OP_HASH160 <20> OP_EQUAL") self.assertEqual(s.pattern, "OP_HASH160 <20> OP_EQUAL")
self.assertEqual(s.op_sig_count, 0) self.assertEqual(s.op_sig_count, 0)