def main(): # all from the same private key prv = ec.PrivateKey.from_wif( "L2e5y14ZD3U1J7Yr62t331RtYe2hRW2TBBP8qNQHB8nSPBNgt6dM") pub = prv.get_public_key() print("Public key:") print(hexlify(pub.serialize())) # we will generate regtest addresses network = NETWORKS['regtest'] print("Legacy (pay to pubkey hash):") sc = script.p2pkh(pub) # default network is main print(sc.address(network)) print("Segwit (pay to witness pubkey hash):") sc = script.p2wpkh(pub) print(sc.address(network)) print("Nested segwit (p2sh-p2wpkh):") sc = script.p2sh(script.p2wpkh(pub)) print(sc.address(network)) print("\nMiltisig address (2 of 3):") # unsorted pubs = [ ec.PublicKey.parse( unhexlify( "02edd7a58d2ff1e483d35f92a32e53607423f936b29bf95613cab24b0b7f92e0f1" )), ec.PublicKey.parse( unhexlify( "03a4a6d360acc45cb281e0022b03218fad6ee93881643488ae39d22b854d9fa261" )), ec.PublicKey.parse( unhexlify( "02e1fdc3b011effbba4b0771eb0f7193dee24cfe101ab7e8b64516d83f7116a615" )), ] # 2 of 3 multisig script sc = script.multisig(2, pubs) print("Legacy, unsorted (p2sh):") redeem_sc = script.p2sh(sc) print(redeem_sc.address(network)) print("Native segwit, sorted (p2wsh):") sc = script.multisig(2, sorted(pubs)) witness_sc = script.p2wsh(sc) print(witness_sc.address(network)) print("Nested segwit, sorted (p2sh-p2wsh):") sc = script.multisig(2, sorted(pubs)) witness_sc = script.p2wsh(sc) redeem_sc = script.p2sh(witness_sc) print(redeem_sc.address(network))
async def showaddr(self, paths: list, script_type: str, redeem_script=None, show_screen=None) -> str: if redeem_script is not None: redeem_script = script.Script(unhexlify(redeem_script)) # first check if we have corresponding wallet: # - just take last 2 indexes of the derivation # and see if redeem script matches address = None if redeem_script is not None: if script_type == b"wsh": address = script.p2wsh(redeem_script).address( NETWORKS[self.network]) elif script_type == b"sh-wsh": address = script.p2sh(script.p2wsh(redeem_script)).address( NETWORKS[self.network]) else: raise HostError("Unsupported script type: %s" % script_type) # in our wallets every key # has the same two last indexes for derivation path = paths[0] if not path.startswith(b"m/"): path = b"m" + path[8:] derivation = bip32.parse_path(path.decode()) # if not multisig: if address is None and len(paths) == 1: pub = self.keystore.get_xpub(derivation) if script_type == b"wpkh": address = script.p2wpkh(pub).address(NETWORKS[self.network]) elif script_type == b"sh-wpkh": address = script.p2sh( script.p2wpkh(pub).address(NETWORKS[self.network])) else: raise WalletError("Unsupported script type: %s" % script_type) if len(derivation) >= 2: derivation = derivation[-2:] else: raise WalletError("Invalid derivation") if address is None: raise WalletError("Can't derive address. Provide redeem script.") try: change = bool(derivation[0]) w = self.find_wallet_from_address(address, derivation[1], change=change) except Exception as e: raise WalletError("%s" % e) if show_screen is not None: await show_screen( WalletScreen(w, self.network, derivation[1], change=change)) return address
async def showaddr(self, paths: list, script_type: str, redeem_script=None, show_screen=None) -> str: net = self.Networks[self.network] if redeem_script is not None: redeem_script = script.Script(unhexlify(redeem_script)) # first check if we have corresponding wallet: address = None if redeem_script is not None: if script_type == b"wsh": address = script.p2wsh(redeem_script).address(net) elif script_type == b"sh-wsh": address = script.p2sh(script.p2wsh(redeem_script)).address(net) elif script_type == b"sh": address = script.p2sh(redeem_script).address(net) else: raise WalletError("Unsupported script type: %s" % script_type) else: if len(paths) != 1: raise WalletError("Invalid number of paths, expected 1") path = paths[0] if not path.startswith("m/"): path = "m" + path[8:] derivation = bip32.parse_path(path) pub = self.keystore.get_xpub(derivation) if script_type == b"wpkh": address = script.p2wpkh(pub).address(net) elif script_type == b"sh-wpkh": address = script.p2sh(script.p2wpkh(pub)).address(net) elif script_type == b"pkh": address = script.p2pkh(pub).address(net) else: raise WalletError("Unsupported script type: %s" % script_type) w, (idx, branch_idx) = self.find_wallet_from_address(address, paths=paths) if show_screen is not None: await show_screen( WalletScreen(w, self.network, idx, branch_index=branch_idx)) addr, _ = w.get_address(idx, self.network, branch_idx) return addr
def scriptpubkey(self, derivation): """Derivation should be an array of two ints: [change 0 or 1, index]""" return script.p2wsh(self.witness_script(derivation))