from pycoin.key import Key from pycoin.serialize import h2b from pycoin.tx import Tx, TxIn, TxOut, SIGHASH_ALL, tx_utils from pycoin.tx.TxOut import standard_tx_out_script from pycoin.tx.pay_to import ScriptMultisig, ScriptPayToPublicKey from pycoin.tx.pay_to import address_for_pay_to_script, build_hash160_lookup, build_p2sh_lookup from pycoin.tx.pay_to import script_obj_from_address, script_obj_from_script publicaddress = "mkR94WCqr4gpZauH8ieTLTG6jkZ2uBB6SA" wif = "cUDx8gXXBZRePmm46LSZetbWqNwXBmm3WQziGVykAgL8NJKMRjQR" key = Key.from_text(publicaddress) key = Key(secret_exponent=1) print key.address() print key.wif() print key.secret_exponent()
my_prng = util.PRNG(util.randrange(ecdsa.generator_secp256k1.order())) print("PRNG (random generator) 32 bytes: ", b2h(my_prng.__call__(32))) my_netcode = "BTC" # mainnet: BTC, testnet3: XTN my_key = Key(secret_exponent=my_secret, is_compressed=True, netcode=my_netcode) ## netcode list: pycoin.networks.all.py pp = pprint.PrettyPrinter(indent=2) my_network = registry.network_for_netcode(my_netcode) my_addr_prefix = registry._lookup(my_netcode, "address") getattr(my_network, "address") pp.pprint(my_network.__dict__) pprint.pprint(my_network.__dict__.keys(), width=60, depth=2) privkey_hex = b2h(encoding.to_bytes_32(my_key.secret_exponent())) assert(len(privkey_hex) == 64) print("\npycoin.key.Key example - ", my_netcode) #print("Private Key (dec): ", eval('0x' + privkey_hex)) print("Private Key (dec): ", int(privkey_hex, 16)) print("Private Key (hex): ", privkey_hex) privkey_bytes = unhexlify(privkey_hex) # use CBitcoinSecret to compress private key btc_secret = CBitcoinSecret.from_secret_bytes(privkey_bytes, True) print(" compressed: ", hexlify(btc_secret.to_bytes())) assert(btc_secret.is_compressed == True) assert(bitcoin.core.b2x(btc_secret.to_bytes()) == (privkey_hex + '01')) print("Private Key WIF: ", my_key.wif())
def main(): parser = argparse.ArgumentParser( description='ECkey2coin.py by [email protected] for UTXO based Certificates UTXOC.', epilog='Known networks codes:\n ' \ + ', '.join(['%s (%s)'%(i, full_network_name_for_netcode(i)) for i in NETWORK_NAMES]) ) parser.add_argument('-k', '--key', required=False, type=argparse.FileType('r'), help='The EC private key in PEM format') parser.add_argument('-q', '--qrfilename', required=False, help='QR code output filename') parser.add_argument('-n', "--network", help='specify network (default: BTC = Bitcoin)', default='BTC', choices=NETWORK_NAMES) args = parser.parse_args() network = args.network inputprivatekey = '' if args.key: keyfile = args.key while True: line = keyfile.readline().strip() if not line: break inputprivatekey += line + '\n' print 'Loaded EC Key from %s' % keyfile else: print ('Please enter EC KEY in pem format:') inputprivatekey = '' while True: line = raw_input().strip() if not line: break inputprivatekey += line + '\n' if not args.qrfilename: qrfilename = raw_input("Please enter qrcode output filename: ") else: qrfilename = args.qrfilename pkey = decoder.decode(read_pem(inputprivatekey), asn1Spec=ECPrivateKey()) print 'Key loaded' if not isValidECKey(pkey[0]): print "EC Key Supplied cannot be used" exit print "Key Validated OK" inputkey = encoding.to_long(256, pycoin.encoding.byte_to_int, pkey[0][1].asOctets())[0] if inputkey: key = Key(secret_exponent=inputkey, netcode=network) btcsecret = key.secret_exponent() btcpublic = key.public_pair() hash160_c = key.hash160(use_uncompressed=False) hash160_u = key.hash160(use_uncompressed=True) qrimg = qrcode.QRCode ( version=1, error_correction=qrcode.constants.ERROR_CORRECT_L, box_size=10, border=4, ) qrimg.add_data(key.address(use_uncompressed=False)) qrimg.make(fit=True) img = qrimg.make_image() img.save(qrfilename) print"----------------- BEGIN EC PRIVATE KEYS -----------------" print "Secret: %d" % btcsecret print "Secret hex: %x" % btcsecret print "wif: %s" % key.wif(use_uncompressed=False) print "----------------- END EC PRIVATE KEYS -----------------------------" print "----------------- BEGIN PUBLIC KEY -----------------------------" print "Public X: %d" % btcpublic[0] print "Public Y: %d" % btcpublic[1] print "hash160 uncompressed: %s" % b2h(hash160_u) print "Sec: (uncompressed): %s" % b2h(key.sec(use_uncompressed=True)) print "%s address: %s (uncompressed)" % (key._netcode, key.address(use_uncompressed=True)) print "Public X (hex): %x" % btcpublic[0] print "Public Y (hex): %x" % btcpublic[1] print "Sec: %s" % b2h(key.sec(use_uncompressed=False)) print "hash160 compressed: %s" % b2h(hash160_c) print "----------------- END PUBLIC KEYS -----------------------------" print "------------------ BEGIN %s ADDRESSES -------------------------" % key._netcode print "%s address: %s" % (key._netcode, key.address(use_uncompressed=False)) print "------------------ END %s ADDRESSES -------------------------" % key._netcode