def main(): parser = argparse.ArgumentParser(description="Bitcoin utilities. WARNING: obsolete. Use ku instead.") parser.add_argument('-a', "--address", help='show as Bitcoin address', action='store_true') parser.add_argument('-1', "--hash160", help='show as hash 160', action='store_true') parser.add_argument('-v', "--verbose", help='dump all information available', action='store_true') parser.add_argument('-w', "--wif", help='show as Bitcoin WIF', action='store_true') parser.add_argument('-n', "--uncompressed", help='show in uncompressed form', action='store_true') parser.add_argument('item', help='a WIF, secret exponent, X/Y public pair, SEC (as hex), hash160 (as hex), Bitcoin address', nargs="+") args = parser.parse_args() for c in args.item: # figure out what it is: # - secret exponent # - WIF # - X/Y public key (base 10 or hex) # - sec # - hash160 # - Bitcoin address secret_exponent = parse_as_private_key(c) if secret_exponent: public_pair = ecdsa.public_pair_for_secret_exponent(secp256k1.generator_secp256k1, secret_exponent) print("secret exponent: %d" % secret_exponent) print(" hex: %x" % secret_exponent) print("WIF: %s" % encoding.secret_exponent_to_wif(secret_exponent, compressed=True)) print(" uncompressed: %s" % encoding.secret_exponent_to_wif(secret_exponent, compressed=False)) else: public_pair = parse_as_public_pair(c) if public_pair: bitcoin_address_uncompressed = encoding.public_pair_to_bitcoin_address(public_pair, compressed=False) bitcoin_address_compressed = encoding.public_pair_to_bitcoin_address(public_pair, compressed=True) print("public pair x: %d" % public_pair[0]) print("public pair y: %d" % public_pair[1]) print(" x as hex: %x" % public_pair[0]) print(" y as hex: %x" % public_pair[1]) print("y parity: %s" % "odd" if (public_pair[1] & 1) else "even") print("key pair as sec: %s" % b2h(encoding.public_pair_to_sec(public_pair, compressed=True))) s = b2h(encoding.public_pair_to_sec(public_pair, compressed=False)) print(" uncompressed: %s\\\n %s" % (s[:66], s[66:])) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True) hash160_unc = encoding.public_pair_to_hash160_sec(public_pair, compressed=False) myeccpoint = encoding.public_pair_to_sec(public_pair, compressed=True) myhash = encoding.ripemd160( myeccpoint ).digest( ) print("BTSX PubKey: %s" % BTS_ADDRESS_PREFIX + encoding.b2a_base58(myeccpoint + myhash[ :4 ])) else: hash160 = parse_as_address(c) hash160_unc = None if not hash160: sys.stderr.write("can't decode input %s\n" % c) sys.exit(1) print("hash160: %s" % b2h(hash160)) if hash160_unc: print(" uncompressed: %s" % b2h(hash160_unc)) print("Bitcoin address: %s" % encoding.hash160_sec_to_bitcoin_address(hash160)) if hash160_unc: print(" uncompressed: %s" % encoding.hash160_sec_to_bitcoin_address(hash160_unc))
def test_key(self, keytype, account = 0, chain = 0, address = 0): """ Performs a public key retrieval test, comparing Polly's key against the reference wallet. keytype - Type of key to retrieve, valid values are KEY_MASTER, KEY_ACCOUNT, KEY_CHAIN, or KEY_ADDRESS. account - Account to use for type KEY_ACCOUNT, KEY_CHAIN, KEY_ADDRESS. chain - Chain to use for type KEY_CHAIN, KEY_ADDRESS. address - Index (0 - 0x7FFFFFFF) to use for type KEY_ADDRESS. """ assert address < 0x80000000, "hardened address keys are not supported" if keytype == PollyCom.KEY_MASTER: print(self.PAD.format("Get master key"), end='') refkey = self.wallet check_chaincode = False elif keytype == PollyCom.KEY_ACCOUNT: print(self.PAD.format("Get account key m/" + str(account) + "h"), end='') refkey = self.wallet.subkey(account, is_hardened = True) check_chaincode = True elif keytype == PollyCom.KEY_CHAIN: print(self.PAD.format("Get chain key m/" + str(account) + "h/" + str(chain)), end='') refkey = self.wallet.subkey(account, is_hardened = True).subkey(chain) check_chaincode = True else: # keytype == PollyCom.KEY_ADDRESS print(self.PAD.format("Get address key m/" + str(account) + "h/" + str(chain) + "/" + str(address)), end='') refkey = self.wallet.subkey(account, is_hardened = True).subkey(chain).subkey(address) check_chaincode = False # Get keypair from Polly (pubx, puby, chaincode) = self.polly.send_get_public_key(keytype, account, chain, address) print (self.__outok()) # Check against reference wallet addr = encoding.public_pair_to_hash160_sec((pubx, puby)) addr_check = encoding.public_pair_to_hash160_sec(refkey.public_pair) assert addr == addr_check, "public key mismatch\nexpected: " + self.hexstr(addr_check) + "\nactual: " + self.hexstr(addr) if check_chaincode == True : assert refkey.chain_code == chaincode, "chain code mismatch\nexpected: " + self.hexstr(refkey.chain_code) + "\nactual: " + self.hexstr(chaincode)
def gen_address(private_key, compressed=True): # private_key = codecs.encode(os.urandom(32), 'hex').decode() secret_exponent = int('0x' + private_key, 0) print('WIF: ' + encoding.secret_exponent_to_wif(secret_exponent, compressed=compressed)) public_pair = ecdsa.public_pair_for_secret_exponent(ecdsa.secp256k1.generator_secp256k1, secret_exponent) print('public pair:', public_pair) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=compressed) print("hash160: {}".format(hash160.hex())) return encoding.hash160_sec_to_bitcoin_address(hash160, address_prefix=b'\0')
def __init__(self, secret_exponents): super(OfflineAwareSolver, self).__init__(secret_exponents) STANDARD_SCRIPT_OUT = "OP_DUP OP_HASH160 %s OP_EQUALVERIFY OP_CHECKSIG" self.script_hash160 = [] for se in secret_exponents: pp = public_pair_for_secret_exponent(generator_secp256k1, se) h160sec = public_pair_to_hash160_sec(pp, False) script_text = STANDARD_SCRIPT_OUT % b2h(h160sec) print script_text self.script_hash160.append(compile(script_text))
def do_test(as_public_pair, as_sec, is_compressed, as_hash160_sec, as_bitcoin_address): self.assertEqual(encoding.sec_to_public_pair(as_sec), as_public_pair) self.assertEqual(encoding.public_pair_to_sec(as_public_pair, compressed=is_compressed), as_sec) self.assertEqual(encoding.is_sec_compressed(as_sec), is_compressed) self.assertEqual(encoding.public_pair_to_hash160_sec(as_public_pair, compressed=is_compressed), as_hash160_sec) self.assertEqual(encoding.hash160_sec_to_bitcoin_address(as_hash160_sec), as_bitcoin_address) self.assertEqual(encoding.public_pair_to_bitcoin_address(as_public_pair, compressed=is_compressed), as_bitcoin_address) self.assertTrue(encoding.is_valid_bitcoin_address(as_bitcoin_address)) bad_address = as_bitcoin_address[:17] + chr(ord(as_bitcoin_address[17]) + 1) + as_bitcoin_address[18:] self.assertFalse(encoding.is_valid_bitcoin_address(bad_address))
def do_test(exp_hex, wif, c_wif, public_pair_sec, c_public_pair_sec, address_b58, c_address_b58): secret_exponent = int(exp_hex, 16) sec = h2b(public_pair_sec) c_sec = h2b(c_public_pair_sec) self.assertEqual(secret_exponent_to_wif(secret_exponent, compressed=False), wif) self.assertEqual(secret_exponent_to_wif(secret_exponent, compressed=True), c_wif) exponent, compressed = wif_to_tuple_of_secret_exponent_compressed(wif) self.assertEqual(exponent, secret_exponent) self.assertFalse(compressed) exponent, compressed = wif_to_tuple_of_secret_exponent_compressed(c_wif) self.assertEqual(exponent, secret_exponent) self.assertTrue(compressed) public_pair = secret_exponent * secp256k1_generator pk_public_pair = sec_to_public_pair(sec) compressed = is_sec_compressed(sec) self.assertEqual(pk_public_pair, public_pair) self.assertFalse(is_sec_compressed(sec)) self.assertEqual(public_pair_to_sec(pk_public_pair, compressed=False), sec) pk_public_pair = sec_to_public_pair(c_sec) compressed = is_sec_compressed(c_sec) self.assertEqual(pk_public_pair, public_pair) self.assertTrue(compressed) self.assertEqual(public_pair_to_sec(pk_public_pair, compressed=True), c_sec) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=True) self.assertEqual(bca, c_address_b58) self.assertEqual(bitcoin_address_to_hash160_sec(c_address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=True)) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=False) self.assertEqual(bca, address_b58) self.assertEqual(bitcoin_address_to_hash160_sec(address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=False))
def create_addr(): import hashlib from pycoin import ecdsa, encoding import os import codecs priv = codecs.encode(os.urandom(32), 'hex').decode() secret_exponent = int('0x' + priv, 0) public_pair = ecdsa.public_pair_for_secret_exponent( ecdsa.secp256k1.generator_secp256k1, secret_exponent) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True) addr = encoding.hash160_sec_to_bitcoin_address(hash160) return addr, priv
def do_test(exp_hex, wif, c_wif, public_pair_sec, c_public_pair_sec, address_b58, c_address_b58): secret_exponent = int(exp_hex, 16) sec = h2b(public_pair_sec) c_sec = h2b(c_public_pair_sec) self.assertEqual(secret_exponent_to_wif(secret_exponent, compressed=False), wif) self.assertEqual(secret_exponent_to_wif(secret_exponent, compressed=True), c_wif) exponent, compressed = wif_to_tuple_of_secret_exponent_compressed(wif) self.assertEqual(exponent, secret_exponent) self.assertFalse(compressed) exponent, compressed = wif_to_tuple_of_secret_exponent_compressed(c_wif) self.assertEqual(exponent, secret_exponent) self.assertTrue(compressed) public_pair = public_pair_for_secret_exponent(generator_secp256k1, secret_exponent) pk_public_pair = sec_to_public_pair(sec) compressed = is_sec_compressed(sec) self.assertEqual(pk_public_pair, public_pair) self.assertFalse(is_sec_compressed(sec)) self.assertEqual(public_pair_to_sec(pk_public_pair, compressed=False), sec) pk_public_pair = sec_to_public_pair(c_sec) compressed = is_sec_compressed(c_sec) self.assertEqual(pk_public_pair, public_pair) self.assertTrue(compressed) self.assertEqual(public_pair_to_sec(pk_public_pair, compressed=True), c_sec) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=True) self.assertEqual(bca, c_address_b58) self.assertEqual(bitcoin_address_to_hash160_sec(c_address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=True)) bca = public_pair_to_bitcoin_address(pk_public_pair, compressed=False) self.assertEqual(bca, address_b58) self.assertEqual(bitcoin_address_to_hash160_sec(address_b58), public_pair_to_hash160_sec(pk_public_pair, compressed=False))
def gen_address(private_key, compressed=True): # private_key = codecs.encode(os.urandom(32), 'hex').decode() secret_exponent = int('0x' + private_key, 0) print('WIF: ' + encoding.secret_exponent_to_wif(secret_exponent, compressed=compressed)) public_pair = ecdsa.public_pair_for_secret_exponent( ecdsa.secp256k1.generator_secp256k1, secret_exponent) print('public pair:', public_pair) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=compressed) print("hash160: {}".format(hash160.hex())) return encoding.hash160_sec_to_bitcoin_address(hash160, address_prefix=b'\0')
def verify(self, h, sig): """ Return whether a signature is valid for hash h using this key. """ val = from_bytes_32(h) pubkey = self.public_pair() rs = sigdecode_der(sig) if self.public_pair() is None: # find the pubkey from the signature and see if it matches # our key possible_pubkeys = secp256k1_generator.possible_public_pairs_for_signature(val, rs) hash160 = self.hash160() for candidate in possible_pubkeys: if hash160 == public_pair_to_hash160_sec(candidate, True): pubkey = candidate break if hash160 == public_pair_to_hash160_sec(candidate, False): pubkey = candidate break else: # signature is using a pubkey that's not this key return False return secp256k1_generator.verify(pubkey, val, rs)
def address_h160_from_script (script): s = disassemble(script).split(' ') if 'OP_HASH160' in s: p = s.index('OP_HASH160') if len(s) > p+1: return h2b(s[p+1]) elif 'OP_CHECKSIG' in s: p = s.index('OP_CHECKSIG') if len(s[p-1]) in (66, 130): # public key sec = h2b(s[p-1]) return public_pair_to_hash160_sec(sec_to_public_pair(sec), is_sec_compressed(sec)) else: logger.warn("Can't parse address from script: %s" % (s)) return None
def verify(self, h, sig): """ Return whether a signature is valid for hash h using this key. """ val = from_bytes_32(h) pubkey = self.public_pair() rs = sigdecode_der(sig) if self.public_pair() is None: # find the pubkey from the signature and see if it matches # our key possible_pubkeys = ecdsa.possible_public_pairs_for_signature( ecdsa.generator_secp256k1, val, rs) hash160 = self.hash160() for candidate in possible_pubkeys: if hash160 == public_pair_to_hash160_sec(candidate, True): pubkey = candidate break if hash160 == public_pair_to_hash160_sec(candidate, False): pubkey = candidate break else: # signature is using a pubkey that's not this key return False return ecdsa.verify(ecdsa.generator_secp256k1, pubkey, val, rs)
def search(target_xfp): k = BIP32Node.from_hwif( "tprv8ZgxMBicQKsPeXJHL3vPPgTAEqQ5P2FD9qDeCQT4Cp1EMY5QkwMPWFxHdxHrxZhhcVRJ2m7BNWTz9Xre68y7mX5vCdMJ5qXMUfnrZ2si2X4" ) pid = os.getpid() target_xfp = h2b_rev(target_xfp) # test by going -33 here. #sec_exp = k._secret_exponent - 33 sec_exp = k._secret_exponent + (pid * int(1e40)) i = 0 last_xfp = None while 1: i += 1 sec_exp += 1 public_pair = ecdsa.public_pair_for_secret_exponent( ecdsa.generator_secp256k1, sec_exp) xfp = public_pair_to_hash160_sec(public_pair, compressed=True)[:4] if i <= 5: # checking code (slow) b = BIP32Node(netcode='BTC', chain_code=bytes(32), secret_exponent=sec_exp) chk = b.fingerprint() assert b._secret_exponent == sec_exp assert xfp == chk, (xfp, chk) assert xfp != last_xfp, 'repeat xfp!' last_xfp = xfp if xfp == target_xfp: print(f"\n\nFOUND: sec_exp = {sec_exp}\n") b = BIP32Node(netcode='BTC', chain_code=bytes(32), secret_exponent=sec_exp) chk = b.fingerprint() assert b._secret_exponent == sec_exp assert xfp == chk, (xfp, chk) print(b.hwif(), end='\n\n') return if not (i % 27): print(' %6d %9d' % (pid, i), end='\r')
def pycoin_sign_raw_transaction(tx_hex, private_key_wif): for char in private_key_wif: if char not in script.b58_digits: raise exceptions.TransactionError('invalid private key') if config.TESTNET: allowable_wif_prefixes = [config.PRIVATEKEY_VERSION_TESTNET] else: allowable_wif_prefixes = [config.PRIVATEKEY_VERSION_MAINNET] secret_exponent, compressed = wif_to_tuple_of_secret_exponent_compressed( private_key_wif, allowable_wif_prefixes=allowable_wif_prefixes) public_pair = public_pair_for_secret_exponent(generator_secp256k1, secret_exponent) hash160 = public_pair_to_hash160_sec(public_pair, compressed) hash160_lookup = {hash160: (secret_exponent, public_pair, compressed)} tx = Tx.tx_from_hex(tx_hex) for idx, tx_in in enumerate(tx.txs_in): tx.sign_tx_in(hash160_lookup, idx, tx_in.script, hash_type=SIGHASH_ALL) return tx.as_hex()
(pub_key_x, pub_key_y) = pubkey_pair compressed_indicator = True if (pub_key_y % 2) == 0 else False print("Public key y parity? ", 'even' if compressed_indicator else 'odd') #print("Private key hexlify: ", hexlify(cec_key.get_privkey())) print("Public key hex: ", bitcoin.core.b2x(cec_key.get_pubkey())) cec_key.set_compressed(False) print(" uncompressed: ", bitcoin.core.b2x(cec_key.get_pubkey())) addr_compressed = encoding.public_pair_to_bitcoin_address( pubkey_pair, True, address_prefix=my_pubaddr_prefix) addr_uncompressed = encoding.public_pair_to_bitcoin_address( pubkey_pair, False, address_prefix=my_pubaddr_prefix) # convert public key pair to public key to address pubkey_hashed = encoding.public_pair_to_hash160_sec(pubkey_pair, True) print("Public key hash160: ", bitcoin.core.b2x(pubkey_hashed)) pubkey_addr = encoding.hash160_sec_to_bitcoin_address( pubkey_hashed, address_prefix=my_pubaddr_prefix) assert (addr_compressed == pubkey_addr) pubkey_hashed = encoding.public_pair_to_hash160_sec(pubkey_pair, False) print(" uncompressed: ", bitcoin.core.b2x(pubkey_hashed)) pubkey_addr = encoding.hash160_sec_to_bitcoin_address( pubkey_hashed, address_prefix=my_pubaddr_prefix) assert (addr_uncompressed == pubkey_addr) print("Bitcoin Address: ", addr_compressed) print(" uncompressed: ", addr_uncompressed) assert (encoding.is_valid_bitcoin_address( addr_compressed, allowable_prefixes=my_pubaddr_prefix))
def main(): parser = argparse.ArgumentParser(description="Bitcoin utilities.") parser.add_argument("-a", "--address", help="show as Bitcoin address", action="store_true") parser.add_argument("-1", "--hash160", help="show as hash 160", action="store_true") parser.add_argument("-v", "--verbose", help="dump all information available", action="store_true") parser.add_argument("-w", "--wif", help="show as Bitcoin WIF", action="store_true") parser.add_argument("-n", "--uncompressed", help="show in uncompressed form", action="store_true") parser.add_argument("-j", "--json", help="output in JSON format", action="store_true") parser.add_argument("-t", help="interpret fields as test values", action="store_true") parser.add_argument("-ltc", help="generate LTC address", action="store_true") parser.add_argument("-doge", help="generate DOGE address", action="store_true") parser.add_argument( "item", help="a WIF, secret exponent, X/Y public pair, SEC (as hex), hash160 (as hex), Bitcoin address", nargs="+", ) args = parser.parse_args() if args.ltc: args.t = "litecoin" else: if args.doge: if args.t: args.t = "dogetest" else: args.t = "doge" if args.json: print("{") argcount = len(args.item) i = 0 for c in args.item: i = i + 1 print(' "%s" : { ' % c) secret_exponent = parse_as_private_key(c) if secret_exponent: public_pair = ecdsa.public_pair_for_secret_exponent(secp256k1.generator_secp256k1, secret_exponent) print(' "secret_exponent" : "%d",' % secret_exponent) print(' "secret_exponent_hex" : "%x",' % secret_exponent) print( ' "wif" : "%s",' % encoding.secret_exponent_to_wif(secret_exponent, compressed=True, is_test=args.t) ) print( ' "wif_uncompressed" : "%s",' % encoding.secret_exponent_to_wif(secret_exponent, compressed=False, is_test=args.t) ) else: public_pair = parse_as_public_pair(c) if public_pair: bitcoin_address_uncompressed = encoding.public_pair_to_bitcoin_address( public_pair, compressed=False, is_test=args.t ) bitcoin_address_compressed = encoding.public_pair_to_bitcoin_address( public_pair, compressed=True, is_test=args.t ) print(' "public_pair_x" : "%d",' % public_pair[0]) print(' "public_pair_y" : "%d",' % public_pair[1]) print(' "x_as_hex" : "%x",' % public_pair[0]) print(' "y_as_hex" : "%x",' % public_pair[1]) if public_pair[1] & 1: print(' "y_parity" : "odd",') else: print(' "y_parity" : "even",') print(' "key_pair_as_sec" : "%s",' % b2h(encoding.public_pair_to_sec(public_pair, compressed=True))) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True) hash160_unc = encoding.public_pair_to_hash160_sec(public_pair, compressed=False) else: hash160 = parse_as_address(c) hash160_unc = None if not hash160: print(' "error" : "cannot decode input %s",' % c) print(' "hash160" : "%s",' % b2h(hash160)) if hash160_unc: print(' "hash160_uncompressed" : "%s",' % b2h(hash160_unc)) if hash160_unc: # print(' "bitcoind_addr_uncompressed" : "%s",' % encoding.hash160_sec_to_bitcoin_address(hash160_unc)) print(' "bitcoind_addr_uncompressed" : "%s",' % bitcoin_address_uncompressed) # print(' "bitcoin_addr" : "%s"' % encoding.hash160_sec_to_bitcoin_address(hash160)) print(' "bitcoin_addr" : "%s"' % bitcoin_address_compressed) if i == argcount: print(" }") else: print(" },") print("}") else: for c in args.item: # figure out what it is: # - secret exponent # - WIF # - X/Y public key (base 10 or hex) # - sec # - hash160 # - Bitcoin address secret_exponent = parse_as_private_key(c) if secret_exponent: public_pair = ecdsa.public_pair_for_secret_exponent(secp256k1.generator_secp256k1, secret_exponent) print("secret exponent: %d" % secret_exponent) print(" hex: %x" % secret_exponent) print("WIF: %s" % encoding.secret_exponent_to_wif(secret_exponent, compressed=True)) print(" uncompressed: %s" % encoding.secret_exponent_to_wif(secret_exponent, compressed=False)) else: public_pair = parse_as_public_pair(c) if public_pair: bitcoin_address_uncompressed = encoding.public_pair_to_bitcoin_address(public_pair, compressed=False) bitcoin_address_compressed = encoding.public_pair_to_bitcoin_address(public_pair, compressed=True) print("public pair x: %d" % public_pair[0]) print("public pair y: %d" % public_pair[1]) print(" x as hex: %x" % public_pair[0]) print(" y as hex: %x" % public_pair[1]) print("y parity: %s" % "odd" if (public_pair[1] & 1) else "even") print("key pair as sec: %s" % b2h(encoding.public_pair_to_sec(public_pair, compressed=True))) s = b2h(encoding.public_pair_to_sec(public_pair, compressed=False)) print(" uncompressed: %s\\\n %s" % (s[:66], s[66:])) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True) hash160_unc = encoding.public_pair_to_hash160_sec(public_pair, compressed=False) else: hash160 = parse_as_address(c) hash160_unc = None if not hash160: sys.stderr.write("can't decode input %s\n" % c) sys.exit(1) print("hash160: %s" % b2h(hash160)) if hash160_unc: print(" uncompressed: %s" % b2h(hash160_unc)) print("Bitcoin address: %s" % encoding.hash160_sec_to_bitcoin_address(hash160)) if hash160_unc: print(" uncompressed: %s" % encoding.hash160_sec_to_bitcoin_address(hash160_unc))
def main(): parser = argparse.ArgumentParser( description="Bitcoin utilities. WARNING: obsolete. Use ku instead.") parser.add_argument('-a', "--address", help='show as Bitcoin address', action='store_true') parser.add_argument('-1', "--hash160", help='show as hash 160', action='store_true') parser.add_argument('-v', "--verbose", help='dump all information available', action='store_true') parser.add_argument('-w', "--wif", help='show as Bitcoin WIF', action='store_true') parser.add_argument('-n', "--uncompressed", help='show in uncompressed form', action='store_true') parser.add_argument( 'item', help= 'a WIF, secret exponent, X/Y public pair, SEC (as hex), hash160 (as hex), Bitcoin address', nargs="+") args = parser.parse_args() for c in args.item: # figure out what it is: # - secret exponent # - WIF # - X/Y public key (base 10 or hex) # - sec # - hash160 # - Bitcoin address secret_exponent = parse_as_private_key(c) if secret_exponent: public_pair = ecdsa.public_pair_for_secret_exponent( secp256k1.generator_secp256k1, secret_exponent) print("secret exponent: %d" % secret_exponent) print(" hex: %x" % secret_exponent) print("WIF: %s" % encoding.secret_exponent_to_wif( secret_exponent, compressed=True)) print(" uncompressed: %s" % encoding.secret_exponent_to_wif( secret_exponent, compressed=False)) else: public_pair = parse_as_public_pair(c) if public_pair: bitcoin_address_uncompressed = encoding.public_pair_to_bitcoin_address( public_pair, compressed=False) bitcoin_address_compressed = encoding.public_pair_to_bitcoin_address( public_pair, compressed=True) print("public pair x: %d" % public_pair[0]) print("public pair y: %d" % public_pair[1]) print(" x as hex: %x" % public_pair[0]) print(" y as hex: %x" % public_pair[1]) print("y parity: %s" % "odd" if (public_pair[1] & 1) else "even") print( "key pair as sec: %s" % b2h(encoding.public_pair_to_sec(public_pair, compressed=True))) s = b2h(encoding.public_pair_to_sec(public_pair, compressed=False)) print(" uncompressed: %s\\\n %s" % (s[:66], s[66:])) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True) hash160_unc = encoding.public_pair_to_hash160_sec(public_pair, compressed=False) else: hash160 = parse_as_address(c) hash160_unc = None if not hash160: sys.stderr.write("can't decode input %s\n" % c) sys.exit(1) print("hash160: %s" % b2h(hash160)) if hash160_unc: print(" uncompressed: %s" % b2h(hash160_unc)) print("Bitcoin address: %s" % encoding.hash160_sec_to_bitcoin_address(hash160)) if hash160_unc: print(" uncompressed: %s" % encoding.hash160_sec_to_bitcoin_address(hash160_unc))
def test_raw_to_address(self): privkey = from_bytes_32(a2b_hashed_base58(self.address)[1:]) pubkey = BasePoint * privkey raw = public_pair_to_hash160_sec(pubkey.pair(), False) addr = self.ccc.raw_to_address(raw) self.assertEqual(addr,'1CC3X2gu58d6wXUWMffpuzN9JAfTUWu4Kj')
#!/usr/bin/python import os from pycoin.key.bip32 import Wallet from pycoin.encoding import public_pair_to_hash160_sec, hash160_sec_to_bitcoin_address from pycoin.networks import address_prefix_for_netcode, alternate_hash_for_netcode from binascii import hexlify import bitcoinrpc import socket secret = os.urandom(64) wallet = Wallet.from_master_secret(bytes(secret), 'BLC') sec = public_pair_to_hash160_sec(wallet.public_pair) pubkey = ''.join('{:02x}'.format(ord(x)) for x in sec) addr_blc = hash160_sec_to_bitcoin_address( sec, address_prefix=address_prefix_for_netcode('BLC'), internal_hash=alternate_hash_for_netcode('BLC')) addr_pho = hash160_sec_to_bitcoin_address( sec, address_prefix=address_prefix_for_netcode('PHO'), internal_hash=alternate_hash_for_netcode('PHO')) addr_bbtc = hash160_sec_to_bitcoin_address( sec, address_prefix=address_prefix_for_netcode('BBTC'), internal_hash=alternate_hash_for_netcode('BBTC')) addr_xdq = hash160_sec_to_bitcoin_address( sec, address_prefix=address_prefix_for_netcode('XDQ'),
def public_pair_to_address(testnet, public_pair, compressed): prefix = b'\x6f' if testnet else b"\0" hash160 = public_pair_to_hash160_sec(public_pair, compressed=compressed) return hash160_sec_to_bitcoin_address(hash160, address_prefix=prefix)
def _public_pair_to_address(testnet, public_pair, compressed): hash160 = public_pair_to_hash160_sec(public_pair, compressed=compressed) return _hash160_to_address(testnet, hash160)
from pycoin import ecdsa, encoding import os import codecs if __name__ == '__main__': #rand = codecs.encode(os.urandom(32), 'hex').decode() #secret_exponent= int('0x'+rand, 0) secret_exponent = int(hp, 16) print('WIF: ' + encoding.secret_exponent_to_wif(secret_exponent, compressed=False)) public_pair = ecdsa.public_pair_for_secret_exponent( ecdsa.secp256k1.generator_secp256k1, secret_exponent) hash160 = encoding.public_pair_to_hash160_sec(public_pair, compressed=True) codecs.encode(hash160, 'hex') print('Bitcoin address: %s' % encoding.hash160_sec_to_bitcoin_address(hash160)) with open('../../Mining/dict/UrbanDictionary.txt', 'r') as f: contents = f.readlines() fout = open('privaddr-pair.txt', 'w') i = 0 for l in contents: priv = seed2hpriv(l) secret_exponent = int(priv, 16)
def rawPubkey(self): return public_pair_to_hash160_sec(self.publicPoint.pair(), False)
#!/usr/bin/python import os from pycoin.key.bip32 import Wallet from pycoin.encoding import public_pair_to_hash160_sec, hash160_sec_to_bitcoin_address from pycoin.networks import address_prefix_for_netcode, alternate_hash_for_netcode from binascii import hexlify import bitcoinrpc import socket secret = os.urandom(64) wallet = Wallet.from_master_secret(bytes(secret), 'BLC') sec = public_pair_to_hash160_sec(wallet.public_pair) pubkey = ''.join('{:02x}'.format(ord(x)) for x in sec) addr_blc = hash160_sec_to_bitcoin_address(sec, address_prefix=address_prefix_for_netcode('BLC'), internal_hash=alternate_hash_for_netcode('BLC')) addr_pho = hash160_sec_to_bitcoin_address(sec, address_prefix=address_prefix_for_netcode('PHO'), internal_hash=alternate_hash_for_netcode('PHO')) addr_bbtc = hash160_sec_to_bitcoin_address(sec, address_prefix=address_prefix_for_netcode('BBTC'), internal_hash=alternate_hash_for_netcode('BBTC')) addr_xdq = hash160_sec_to_bitcoin_address(sec, address_prefix=address_prefix_for_netcode('XDQ'), internal_hash=alternate_hash_for_netcode('XDQ')) addr_elt = hash160_sec_to_bitcoin_address(sec, address_prefix=address_prefix_for_netcode('ELT'), internal_hash=alternate_hash_for_netcode('ELT')) addr_umo = hash160_sec_to_bitcoin_address(sec, address_prefix=address_prefix_for_netcode('UMO'), internal_hash=alternate_hash_for_netcode('UMO')) addresses = { 'blakecoin': addr_blc, 'photon': addr_pho, 'blakebitcoin': addr_bbtc, 'dirac': addr_xdq, 'electron': addr_elt, 'universalmolecule': addr_umo }