Exemplo n.º 1
0
def get_output_address(tx, i, versions):
    script = wally.tx_get_output_script(tx, i)
    script_type = wally.scriptpubkey_get_type(script)
    if script_type == wally.WALLY_SCRIPT_TYPE_P2PKH:
        return wally.base58check_from_bytes(
            bytearray([versions[0]]) + script[3:23])
    if script_type == wally.WALLY_SCRIPT_TYPE_P2SH:
        return wally.base58check_from_bytes(
            bytearray([versions[1]]) + script[2:22])
    assert False
Exemplo n.º 2
0
def scriptpubkey_to_address(scriptpubkey: bytes) -> str:
    type_ = wally.scriptpubkey_get_type(scriptpubkey)
    if type_ == wally.WALLY_SCRIPT_TYPE_P2PKH:
        pubkeyhash = scriptpubkey[3:23]
        prefix = bytearray([pywally.params.P2PKH_PREFIX])
        return wally.base58check_from_bytes(prefix + pubkeyhash)
    elif type_ == wally.WALLY_SCRIPT_TYPE_P2SH:
        scripthash = scriptpubkey[2:22]
        prefix = bytearray([pywally.params.P2SH_PREFIX])
        return wally.base58check_from_bytes(prefix + scripthash)
    elif type_ == wally.WALLY_SCRIPT_TYPE_P2WPKH or \
            type_ == wally.WALLY_SCRIPT_TYPE_P2WSH:
        return wally.addr_segwit_from_bytes(scriptpubkey,
                                            pywally.params.BECH32_HRP, 0)
    else:
        raise UnknownAddressForScriptpubkey
Exemplo n.º 3
0
    def __init__(self, pubkeys, network):
        self.redeem_script = get_redeem_script(pubkeys)
        self.redeem_script_hex = wally.hex_from_bytes(self.redeem_script)

        script_hash = wally.hash160(self.get_witness_script())
        script_hash_hex = wally.hex_from_bytes(script_hash)
        self.scriptPubKey = get_scriptpubkey_hex(script_hash_hex)

        ver = {'testnet': b'\xc4', 'mainnet': b'\x05'}[network]
        self.address = wally.base58check_from_bytes(ver + script_hash)
Exemplo n.º 4
0
    def __init__(self, pubkeys, testnet):
        self.redeem_script = get_redeem_script(pubkeys)
        self.redeem_script_hex = wally.hex_from_bytes(self.redeem_script)

        script_hash = wally.hash160(self.get_witness_script())
        script_hash_hex = wally.hex_from_bytes(script_hash)
        self.scriptPubKey = get_scriptpubkey_hex(script_hash_hex)

        ver = b'\xc4' if testnet else b'\x05'
        self.address = wally.base58check_from_bytes(ver + script_hash)
Exemplo n.º 5
0
def private_key_to_wif(key, testnet):
    ver = b'\xef' if testnet else b'\x80'
    compressed = b'\x01'
    priv_key = wally.bip32_key_get_priv_key(key)
    return wally.base58check_from_bytes(ver + priv_key + compressed)
Exemplo n.º 6
0
    with open('test_hdnode_gen.py', 'wt') as fd:
        print("import gc, ngu  # auto-gen", file=fd)
        print("HDNode = ngu.hdnode.HDNode", file=fd)

        print("for i in range(3):", file=fd)
        ms = b'1' * 32
        for i in range(10):
            ms = sha256(ms).digest()
            node = w.bip32_key_from_seed(ms, 0x488ade4, 0)
            pub = w.bip32_key_get_pub_key(node)
            priv = w.bip32_key_get_priv_key(node)
            fp = bytearray(4)
            w.bip32_key_get_fingerprint(node, fp)

            cc = w.bip32_key_get_chain_code(node)
            xprv = w.base58check_from_bytes(w.bip32_key_serialize(node, 0))
            xpub = w.base58check_from_bytes(
                w.bip32_key_serialize(node, w.BIP32_FLAG_KEY_PUBLIC))
            addr = w.bip32_key_to_address(node, w.WALLY_ADDRESS_TYPE_P2PKH, 0)
            print("  a = HDNode(); a.from_master(%r)" % ms, file=fd)
            print("  assert a.pubkey() == %r" % pub, file=fd)
            print("  assert a.privkey() == %r" % priv, file=fd)
            print("  assert a.my_fp() == 0x%s" % fp.hex(), file=fd)
            print("  assert a.chain_code() == %r" % bytes(cc), file=fd)
            print("  assert a.serialize(0x488ade4, 1) == %r" % xprv, file=fd)
            print("  assert a.serialize(0x488b21e, 0) == %r" % xpub, file=fd)
            print("  assert a.addr_help(0) == %r" % addr, file=fd)
            print("  ", file=fd)

        print("gc.collect()", file=fd)
        print("print('PASS - %s')" % fd.name, file=fd)
Exemplo n.º 7
0
def private_key_to_wif(key, network):
    ver = {'testnet': b'\xef', 'mainnet': b'\x80'}[network]
    compressed = b'\x01'
    priv_key = wally.bip32_key_get_priv_key(key)
    return wally.base58check_from_bytes(ver + priv_key + compressed)
Exemplo n.º 8
0
 def address(self):
     script_hash = wally.hash160(self.redeem_script)
     # FIXME: be more explicit
     version = bytearray([get_address_versions(self.network)[1]])
     return wally.base58check_from_bytes(version + script_hash)