本文整理汇总了Python中pycoin.key.BIP32Node.BIP32Node.from_wallet_key方法的典型用法代码示例。如果您正苦于以下问题:Python BIP32Node.from_wallet_key方法的具体用法?Python BIP32Node.from_wallet_key怎么用?Python BIP32Node.from_wallet_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pycoin.key.BIP32Node.BIP32Node
的用法示例。
在下文中一共展示了BIP32Node.from_wallet_key方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_streams
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
def test_streams(self):
m0 = BIP32Node.from_master_secret("foo bar baz".encode("utf8"))
pm0 = m0.public_copy()
self.assertEqual(m0.wallet_key(), pm0.wallet_key())
m1 = m0.subkey()
pm1 = pm0.subkey()
for i in range(4):
m = m1.subkey(i=i)
pm = pm1.subkey(i=i)
self.assertEqual(m.wallet_key(), pm.wallet_key())
self.assertEqual(m.bitcoin_address(), pm.bitcoin_address())
m2 = BIP32Node.from_wallet_key(m.wallet_key(as_private=True))
m3 = m2.public_copy()
self.assertEqual(m.wallet_key(as_private=True), m2.wallet_key(as_private=True))
self.assertEqual(m.wallet_key(), m3.wallet_key())
print(m.wallet_key(as_private=True))
for j in range(2):
k = m.subkey(i=j)
k2 = BIP32Node.from_wallet_key(k.wallet_key(as_private=True))
k3 = BIP32Node.from_wallet_key(k.wallet_key())
k4 = k.public_copy()
self.assertEqual(k.wallet_key(as_private=True), k2.wallet_key(as_private=True))
self.assertEqual(k.wallet_key(), k2.wallet_key())
self.assertEqual(k.wallet_key(), k3.wallet_key())
self.assertEqual(k.wallet_key(), k4.wallet_key())
print(" %s %s" % (k.bitcoin_address(), k.wif()))
示例2: from_text
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
def from_text(class_, text, is_compressed=True):
"""
This function will accept a BIP0032 wallet string, a WIF, or a bitcoin address.
The "is_compressed" parameter is ignored unless a public address is passed in.
"""
data = a2b_hashed_base58(text)
netcode, key_type = netcode_and_type_for_data(data)
data = data[1:]
if key_type in ("pub32", "prv32"):
# TODO: fix this... it doesn't belong here
from pycoin.key.BIP32Node import BIP32Node
return BIP32Node.from_wallet_key(text)
if key_type == 'wif':
is_compressed = (len(data) > 32)
if is_compressed:
data = data[:-1]
return Key(
secret_exponent=from_bytes_32(data),
prefer_uncompressed=not is_compressed, netcode=netcode)
if key_type == 'address':
return Key(hash160=data, is_compressed=is_compressed, netcode=netcode)
raise EncodingError("unknown text: %s" % text)
示例3: cosign_spend_request
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
def cosign_spend_request(xprvkey_or_wallet, req_keys, inputs, xpub_check):
"""
Sign the inputs of a transaction, given the sighashs and subkey paths for each input
Args:
xprvkey_or_wallet = 111-char base58 encoded serialization of BIP32 wallet
or pycoin.key.BIP32Node object (w/ private key)
req_keys = dictionary: key is subpath ('a/b/c', but only 'a' for now as a string)
value is tuple: (address, public pair) ... optional checking data
inputs = list of by transaction input: (subpath, sighash_all value)
Returns:
list of 3-tuples: (der-encoded signature, sighash, subpath)
"""
# We need just these features from pycoin <https://github.com/richardkiss/pycoin>
from pycoin import ecdsa
from pycoin.key.BIP32Node import BIP32Node
from pycoin.tx.script import der
# We need a BIP32 "wallet" for the root of all keys.
if isinstance(xprvkey_or_wallet, basestring):
wallet = BIP32Node.from_wallet_key(xprvkey_or_wallet.strip())
else:
wallet = xprvkey_or_wallet
# Verify we are looking at the right extended private key
check = wallet.hwif(as_private=False)[-len(xpub_check) :]
if check != xpub_check:
raise ValueError("This private key isn't the right one for xpub...%s" % xpub_check)
# Make the right subkey for each inputs
wallets = {}
for sp, (addr_check, ppair) in req_keys.items():
w = wallet.subkey_for_path(sp)
assert w.bitcoin_address() == addr_check
assert w.public_pair() == tuple(ppair)
wallets[sp] = w
# Generate a signature for each input required
sigs = []
SIGHASH_ALL = 1
order = ecdsa.generator_secp256k1.order()
for sp, sighash in inputs:
sighash_int = int(sighash, 16)
r, s = ecdsa.sign(ecdsa.generator_secp256k1, wallets[sp].secret_exponent(), sighash_int)
if s + s > order:
s = order - s
sig = der.sigencode_der(r, s) + chr(SIGHASH_ALL)
sigs.append((sig.encode("hex"), sighash, sp))
return sigs
示例4: olsign
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
def olsign(key, proposal, url, upload, html, output):
if not url and not proposal:
raise click.BadParameter(
"Need a URL to fetch proposal from (--url), or the file itself (-i file.json)")
# get the proposal JSON
try:
if url:
proposal = requests.get(url).json()
else:
proposal = simplejson.load(proposal)
except JSONDecodeError:
raise click.UsageError("Does not contain valid JSON")
# unwrap signature, checking it as we go
proposal = check_sig_and_unwrap(proposal)
click.echo('''
Co-signing as: {cosigner}
Required xpubkey: ...{xpubkey_check}
'''.format(**proposal))
# unpack their private key (to test if suitable)
wallet = BIP32Node.from_wallet_key(key.read().strip())
check = wallet.hwif(as_private = False)[-8:]
if check != proposal['xpubkey_check']:
raise click.UsageError('This private key is not the one we need as this co-signer.')
#pprint(proposal.keys())
# present a summary of what will happen
if html: show_page(proposal)
sigs = do_signing(wallet, proposal['req_keys'], proposal['inputs'])
package = package_for_ck(wallet, proposal, sigs)
if output:
output.write(package)
click.echo("Wrote result to: %s" % output.name)
if upload:
upload_to_ck(package)
if not output and not upload:
click.echo("JSON response:\n\n%s" % package)
示例5: main
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
def main():
parser = argparse.ArgumentParser(description="Generate a private wallet key. WARNING: obsolete. Use ku instead.")
parser.add_argument('-a', "--address", help='show as Bitcoin address', action='store_true')
parser.add_argument('-i', "--info", help='show metadata', action='store_true')
parser.add_argument('-j', "--json", help='output metadata as JSON', action='store_true')
parser.add_argument('-w', "--wif", help='show as Bitcoin WIF', action='store_true')
parser.add_argument('-f', "--wallet-key-file", help='initial wallet key', type=argparse.FileType('r'))
parser.add_argument('-k', "--wallet-key", help='initial wallet key')
parser.add_argument('-g', "--gpg", help='use gpg --gen-random to get additional entropy', action='store_true')
parser.add_argument('-u', "--dev-random", help='use /dev/random to get additional entropy', action='store_true')
parser.add_argument('-n', "--uncompressed", help='show in uncompressed form', action='store_true')
parser.add_argument('-p', help='generate wallet key from passphrase. NOT RECOMMENDED', metavar='passphrase')
parser.add_argument('-s', "--subkey", help='subkey path (example: 0p/2/1)')
parser.add_argument('-t', help='generate test key', action="store_true")
parser.add_argument('inputfile', help='source of entropy. stdin by default', type=argparse.FileType(mode='r+b'), nargs='?')
args = parser.parse_args()
# args.inputfile doesn't like binary when "-" is passed in. Deal with this.
if args.inputfile == sys.stdin and hasattr(sys.stdin, "buffer"):
args.inputfile = sys.stdin.buffer
network = 'XTN' if args.t else 'BTC'
entropy = bytearray()
if args.gpg:
entropy.extend(gpg_entropy())
if args.dev_random:
entropy.extend(dev_random_entropy())
if args.inputfile:
entropy.extend(args.inputfile.read())
if args.p:
entropy.extend(args.p.encode("utf8"))
if len(entropy) == 0 and not args.wallet_key and not args.wallet_key_file:
parser.error("you must specify at least one source of entropy")
if args.wallet_key and len(entropy) > 0:
parser.error("don't specify both entropy and a wallet key")
if args.wallet_key_file:
wallet = BIP32Node.from_wallet_key(args.wallet_key_file.readline()[:-1])
elif args.wallet_key:
wallet = BIP32Node.from_wallet_key(args.wallet_key)
else:
wallet = BIP32Node.from_master_secret(bytes(entropy), netcode=network)
try:
if args.subkey:
wallet = wallet.subkey_for_path(args.subkey)
if wallet.child_index() >= 0x80000000:
wc = wallet.child_index() - 0x80000000
child_index = "%dp (%d)" % (wc, wallet.child_index())
else:
child_index = "%d" % wallet.child_index()
if args.json:
d = dict(
wallet_key=wallet.wallet_key(as_private=wallet.is_private()),
public_pair_x=wallet.public_pair[0],
public_pair_y=wallet.public_pair[1],
tree_depth=wallet.depth,
fingerprint=b2h(wallet.fingerprint()),
parent_fingerprint=b2h(wallet.parent_fingerprint),
child_index=child_index,
chain_code=b2h(wallet.chain_code),
bitcoin_addr=wallet.bitcoin_address(),
bitcoin_addr_uncompressed=wallet.bitcoin_address(compressed=False),
network="test" if wallet.is_test else "main",
)
if wallet.is_private():
d.update(dict(
key="private",
secret_exponent=wallet.secret_exponent,
WIF=wallet.wif(),
WIF_uncompressed=wallet.wif(compressed=False)
))
else:
d.update(dict(key="public"))
print(json.dumps(d, indent=3))
elif args.info:
print(wallet.wallet_key(as_private=wallet.is_private()))
print(full_network_name_for_netcode(wallet.netcode()))
if wallet.is_private():
print("private key")
print("secret exponent: %d" % wallet.secret_exponent())
else:
print("public key only")
print("public pair x: %d\npublic pair y: %d" % wallet.public_pair())
print("tree depth: %d" % wallet.tree_depth())
print("fingerprint: %s" % b2h(wallet.fingerprint()))
print("parent f'print: %s" % b2h(wallet.parent_fingerprint()))
print("child index: %s" % child_index)
print("chain code: %s" % b2h(wallet.chain_code()))
if wallet.is_private():
print("WIF: %s" % wallet.wif())
print(" uncompressed: %s" % wallet.wif(use_uncompressed=True))
print("Bitcoin address: %s" % wallet.bitcoin_address())
print(" uncompressed: %s" % wallet.bitcoin_address(use_uncompressed=True))
elif args.address:
print(wallet.bitcoin_address(use_uncompressed=args.uncompressed))
elif args.wif:
print(wallet.wif(compressed=not args.uncompressed))
else:
print(wallet.wallet_key(as_private=wallet.is_private()))
#.........这里部分代码省略.........
示例6: generate_bip32_address_from_extended_pubkey
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
def generate_bip32_address_from_extended_pubkey(extended_pubkey, branch, index):
ext_key = BIP32Node.from_wallet_key(extended_pubkey)
return ext_key.subkey_for_path('%d/%d' % (branch, index)).address()
示例7: Wallet
# 需要导入模块: from pycoin.key.BIP32Node import BIP32Node [as 别名]
# 或者: from pycoin.key.BIP32Node.BIP32Node import from_wallet_key [as 别名]
#!/usr/bin/env python
import unittest
from pycoin.key import bip32
from pycoin.serialize import h2b
from pycoin.key.BIP32Node import BIP32Node
def Wallet(*args, **kwargs):
return BIP32Node(*args, **kwargs)
Wallet.from_master_secret = lambda *args, **kwargs: BIP32Node.from_master_secret(*args, **kwargs)
Wallet.from_wallet_key = lambda *args, **kwargs: BIP32Node.from_wallet_key(*args, **kwargs)
bip32.Wallet = Wallet
class Bip0032TestCase(unittest.TestCase):
def test_vector_1(self):
master = bip32.Wallet.from_master_secret(h2b("000102030405060708090a0b0c0d0e0f"))
self.assertEqual(master.wallet_key(as_private=True), "xprv9s21ZrQH143K3QTDL4LXw2F7HEK3wJUD2nW2nRk4stbPy6cq3jPPqjiChkVvvNKmPGJxWUtg6LnF5kejMRNNU3TGtRBeJgk33yuGBxrMPHi")
self.assertEqual(master.bitcoin_address(), "15mKKb2eos1hWa6tisdPwwDC1a5J1y9nma")
self.assertEqual(master.wif(), "L52XzL2cMkHxqxBXRyEpnPQZGUs3uKiL3R11XbAdHigRzDozKZeW")
self.assertEqual(master.wallet_key(), "xpub661MyMwAqRbcFtXgS5sYJABqqG9YLmC4Q1Rdap9gSE8NqtwybGhePY2gZ29ESFjqJoCu1Rupje8YtGqsefD265TMg7usUDFdp6W1EGMcet8")
m0p = master.subkey(is_hardened=True)
self.assertEqual(m0p.wallet_key(), "xpub68Gmy5EdvgibQVfPdqkBBCHxA5htiqg55crXYuXoQRKfDBFA1WEjWgP6LHhwBZeNK1VTsfTFUHCdrfp1bgwQ9xv5ski8PX9rL2dZXvgGDnw")
self.assertEqual(m0p.wallet_key(as_private=True), "xprv9uHRZZhk6KAJC1avXpDAp4MDc3sQKNxDiPvvkX8Br5ngLNv1TxvUxt4cV1rGL5hj6KCesnDYUhd7oWgT11eZG7XnxHrnYeSvkzY7d2bhkJ7")
self.assertEqual(master.subkey_for_path("0p").wallet_key(), m0p.wallet_key())
pub_mp0 = master.subkey(is_hardened=True, as_private=False)