def __init__(self, pointer): logging.debug('Derive keys for subaccount={}, pointer={}'.format( subaccount, pointer)) self.subaccount = subaccount self.pointer = pointer # Derive the GreenAddress public key for this pointer value ga_key = gacommon.derive_hd_key(ga_xpub, [pointer], wally.BIP32_FLAG_KEY_PUBLIC) self.ga_key = wally.bip32_key_get_pub_key(ga_key) logging.debug("ga_key = {}".format( wally.hex_from_bytes(self.ga_key))) # Derive the user private keys for this pointer value flags = wally.BIP32_FLAG_KEY_PRIVATE user_key_paths = [(key, [pointer]) for key in user_keys] private_keys = [ gacommon.derive_hd_key(*path, flags=flags) for path in user_key_paths ] self.private_keys = [ wally.bip32_key_get_priv_key(k) for k in private_keys ] # Derive the user public keys from the private keys user_public_keys = [ wally.ec_public_key_from_private_key(k) for k in self.private_keys ] public_keys = [self.ga_key] + user_public_keys # Script could be segwit or not - generate both segwit and non-segwit addresses self.witnesses = { cls.type_: cls(public_keys, network) for cls in (P2SH, P2WSH) } logging.debug('p2sh address: {}'.format( self.witnesses['p2sh'].address)) logging.debug('p2wsh address: {}'.format( self.witnesses['p2wsh'].address))
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)
def get_privkey(self, path: List[int]) -> bytearray: return wally.bip32_key_get_priv_key(self.derive_key(path))
def countersign(txdata, private_key): GreenAddress = PassiveSignatory( wally.hex_to_bytes(txdata['prevout_signatures'][0])) user = ActiveSignatory(wally.bip32_key_get_priv_key(private_key)) return sign(txdata, [GreenAddress, user])
def prv(self): return wally.bip32_key_get_priv_key(self.extkey)
output_tx, prev_txid, vin, 0xffffffff, None, # scriptSig None, # witness None, # nonce None, # entropy None, # issuance amount None, # inflation keys None, # issuance amount rangeproof None, # inflation keys rangeproof None, # pegin witness vout) privkey = wally.bip32_key_get_priv_key(wallet_derived_key) pubkey = wally.ec_public_key_from_private_key(privkey) sighash = wally.tx_get_btc_signature_hash(output_tx, vout, script_pubkey, 0, wally.WALLY_SIGHASH_ALL, 0) signature = wally.ec_sig_from_bytes(privkey, sighash, wally.EC_FLAG_ECDSA) scriptsig = wally.scriptsig_p2pkh_from_sig(pubkey, signature, wally.WALLY_SIGHASH_ALL) wally.tx_set_input_script(output_tx, vout, scriptsig) # end-sign # start-to_hex tx_hex = wally.tx_to_hex(output_tx, wally.WALLY_TX_FLAG_USE_WITNESS)
def privkey(self) -> PrivateKey: return PrivateKey(wally.bip32_key_get_priv_key(self.extkey))
try: # Desktop: make test vectors from hashlib import sha256 import wallycore as w 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)
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)