示例#1
0
 def to_btc(self, type: str) -> str:
     if isinstance(self.key, (Secp256k1Priv, Secp256k1Pub)):
         if self.version == Version.PRIVATE:
             return bitcoin.serialize_privkey(self.key.get_private_bytes(), True, type)
         else:
             return bitcoin.pubkey_to_address(type, self.key.get_public_bytes().hex())
     else:
         raise ValueError("Can only derive BTC from Secp256k1")
示例#2
0
 def test_serialize_privkey(self):
     for priv_details in self.priv_pub_addr:
         txin_type, privkey, compressed = deserialize_privkey(priv_details['priv'])
         priv2 = serialize_privkey(privkey, compressed, txin_type)
         self.assertEqual(priv_details['exported_privkey'], priv2)
示例#3
0
 def test_serialize_privkey(self):
     for priv_details in self.priv_pub_addr:
         txin_type, privkey, compressed = deserialize_privkey(
             priv_details['priv'])
         priv2 = serialize_privkey(privkey, compressed, txin_type)
         self.assertEqual(priv_details['exported_privkey'], priv2)
示例#4
0
def xprv2btc(xprv):
    _xtype, _depth, _fp, _cn, _c, k = bitcoin.deserialize_xprv(xprv)
    privkey = bitcoin.serialize_privkey(k, True, "p2wpkh-p2sh")
    return privkey
示例#5
0
 def to_btc(self, type: str) -> str:
     if self.version == Version.PRIVATE:
         return bitcoin.serialize_privkey(self.key.get_private_bytes(), True, type)
     else:
         return bitcoin.pubkey_to_address(type, self.key.get_public_bytes().hex())
#!./venv/bin/python

from electrum import bitcoin

import sys

lines = [x.strip() for x in sys.stdin.readlines()]

if len(lines) !=1:
    print("wrong input")
    sys.exit(1)

_xtype, _depth, _fp, _cn, _c, k = bitcoin.deserialize_xprv(lines[0])
privkey = bitcoin.serialize_privkey(k, True, "p2pkh")

print(privkey)
示例#7
0
# Set testnet
set_testnet()

# Set mainnet
# set_mainnet()

# Number of addresses to generate:
n = 20

# Type of address
type = 'p2wpkh'

# type = 'p2wpkh-p2sh'
# type = 'p2pkh'


# https://www.secg.org/sec2-v2.pdf section 2.4.1
def get_priv():
    n = 'FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141'
    n = int(n.replace(' ', ''), 16)
    priv = randint(1, n - 1)
    return bytes.fromhex(f"{priv:064x}")


for i in range(0, n):
    priv = get_priv()
    wif = serialize_privkey(priv, True, '').replace(':', '')
    pubk = ECPrivkey(priv).get_public_key_bytes()
    addr = pubkey_to_address('p2wpkh', pubk.hex())
    print(f"WIF: {wif} Address: {addr}")