def test_encode_decode(self): for exp_bin, exp_bech32 in load_test_vectors('bech32_encode_decode.json'): exp_bin = [_bord(y) for y in unhexlify(exp_bin.encode('utf8'))] witver = self.op_decode(exp_bin[0]) hrp = exp_bech32[:exp_bech32.rindex('1')].lower() self.assertEqual(exp_bin[1], len(exp_bin[2:])) act_bech32 = encode(hrp, witver, exp_bin[2:]) act_bin = decode(hrp, exp_bech32) self.assertEqual(act_bech32.lower(), exp_bech32.lower()) self.assertEqual(to_scriptPubKey(*act_bin), _tobytes(exp_bin))
while (1): privKey = btc.random_key() # 256 bit Random number를 생성한다 dPrivKey = btc.decode_privkey(privKey, 'hex') # 16진수 문자열을 10진수 숫자로 변환한다 if dPrivKey < btc.N: # secp256k1 의 N 보다 작으면 OK break privKey='860ef116221744a5299c99a0ed726c15a2148a21a341fe522399c84a59771cfe01' # 개인키로 공개키를 생성한다. Compressed format. pubKey = btc.privkey_to_pubkey(privKey) cPubKey = btc.compress(pubKey) # 공개키로 160-bit public key hash를 생성한다 witprog = btc.bin_hash160(binascii.unhexlify(cPubKey)) # BIP-173 주소를 생성한다. (Base32 address format for native v0-16 witness outputs) # P2WPKH mainnetAddr = bech32.encode('bc', 0, witprog) testnetAddr = bech32.encode('tb', 0, witprog) # 결과 print("\n\n공개키 :", cPubKey) print("Bech32 주소 (Mainnet P2WPKH) :", mainnetAddr) print("Bech32 주소 (Testnet P2WPKH) :", testnetAddr) print("\n\nBIP-173 문서의 Example 확인") print("==========================") cPubKey = '0279BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798' # 공개키로 160-bit public key hash를 생성한다 witprog = btc.bin_hash160(binascii.unhexlify(cPubKey)) # BIP-173 주소를 생성한다. (Base32 address format for native v0-16 witness outputs)
def __str__(self): """Convert to string""" return encode(bitcoin.params.BECH32_HRP, self.witver, self)
def structure_serialize(self, **kwargs): if 'bech32' in kwargs and kwargs['bech32'] is True: return bech32.encode('sm', 1, self.bytes) else: return self.__str__()
def bech32_id(self) -> str: return bech32.encode('pf', 1, self.GetHash())
# 공개키로 160-bit public key hash를 생성한다 pubHash160 = btc.hash160(binascii.unhexlify(pubKey)) # P2SH용 스크립트를 생성한다. script = OP_0 + length + public key hash script = '00' + '14' + pubHash160 # 160 비트 스크립트 해시를 계산한다 scriptHash = btc.hash160(binascii.unhexlify(script)) # 스크립트 해시로 지갑 주소를 생성한다. addr = btc.hex_to_b58check(scriptHash, 0x05) # BIP-173 주소를 생성한다. # 참조: https://github.com/sipa/bech32/blob/master/ref/python/segwit_addr.py witprog = btc.bin_hash160(binascii.unhexlify(pubKey)) bech32Addr = bech32.encode('bc', 0, witprog) # 결과를 확인한다 print("Decode WIF = ", wifDecode) print("Private Key = ", privKey) print("Public Key = ", pubKey) print("Public Key Hash = ", pubHash160) print("Script = ", script) print("ScriptHash = ", scriptHash) print("Address = ", addr) print("Bech32 Address = ", bech32Addr)