def tAddress(x): if len(x) > 35: raise ValidationError("Invalid address '%s' (too long)." % x) try: bitcoin.b58check_to_bin(x) except: raise ValidationError("Invalid address '%s'." % x) return x
def becies_encode(ephemeral_pubkey, ciphertext, tag, pubkeys=[], num_to_activate=None, offsets=None): bout = BECIES_MAGIC_BYTES #0xc66b20 3-byte prefix? (encodes to xmsg in base64) isaddresses = bool(pubkeys) isgroup = bool(offsets) #a vli indicating the header contents flags. #offsets,and addresses are first two bits, rest are unused bout += _to_vli( int(isgroup) * BECIES_GROUP_FLAG + int(isaddresses) * BECIES_ADDRESSES_FLAG) if (isaddresses): bout += _to_vli(len(pubkeys)) bout += ''.join( [bitcoin.b58check_to_bin(bitcoin.pubtoaddr(p)) for p in pubkeys]) if (isgroup): bout += _to_vli( num_to_activate) #todo, num_to_activate must be strictly positive bout += _to_vli(len(offsets)) bout += ''.join([bitcoin.encode_privkey(priv) for priv in offsets]) bout += bitcoin.encode_pubkey(ephemeral_pubkey, 'bin_compressed') bout += _to_vli(len(ciphertext)) bout += ciphertext bout += tag #this has to come last for streaming mode too return bout
def validate_address(addr): try: ver = btc.get_version_byte(addr) except AssertionError: return False, 'Checksum wrong. Typo in address?' if ver != get_p2pk_vbyte() and ver != get_p2sh_vbyte(): return False, 'Wrong address version. Testnet/mainnet confused?' if len(btc.b58check_to_bin(addr)) != 20: return False, "Address has correct checksum but wrong length." return True, 'address validated'
def test_wif_privkeys_invalid(setup_keys): #first try to create wif privkey from key of wrong length bad_privs = ['\x01\x02'*17] #some silly private key but > 33 bytes #next try to create wif with correct length but wrong compression byte bad_privs.append('\x07'*32 + '\x02') for priv in bad_privs: with pytest.raises(Exception) as e_info: fake_wif = btc.wif_compressed_privkey(binascii.hexlify(priv)) #Create a wif with wrong length bad_wif1 = btc.bin_to_b58check('\x01\x02'*34, 128) #Create a wif with wrong compression byte bad_wif2 = btc.bin_to_b58check('\x07'*33, 128) for bw in [bad_wif1, bad_wif2]: with pytest.raises(Exception) as e_info: fake_priv = btc.from_wif_privkey(bw) #Some invalid b58 from bitcoin repo; #none of these are valid as any kind of key or address with open("test/base58_keys_invalid.json", "r") as f: json_data = f.read() invalid_key_list = json.loads(json_data) for k in invalid_key_list: bad_key = k[0] for netval in ["mainnet", "testnet"]: jm_single().config.set("BLOCKCHAIN", "network", netval) #if using py.test -s ; sanity check to see what's actually being tested print 'testing this key: ' + bad_key if "decode_privkey" in dir(btc): try: bad_key_format = btc.get_privkey_format(bad_key) print 'has correct format: ' + bad_key_format except: pass #should throw exception with pytest.raises(Exception) as e_info: if "decode_privkey" in dir(btc): from_wif_key = btc.decode_privkey(bad_key) else: from_wif_key = btc.from_wif_compressed_privkey( bad_key, btc.get_version_byte(bad_key)) #in case the b58 check encoding is valid, we should #also check if the leading version byte is in the #expected set, and throw an error if not. if chr(btc.get_version_byte(bad_key)) not in '\x80\xef': raise Exception("Invalid version byte") #the bitcoin library should throw #if the compression byte is not there (test not needed #for secp256k1 branch since the wif_compressed function checks) if "decode_privkey" in dir(btc): if "compressed" in btc.get_privkey_format(bad_key) and \ btc.b58check_to_bin(x)[-1] != '\x01': raise Exception("Invalid compression byte")
def test_wif_privkeys_invalid(setup_keys): #first try to create wif privkey from key of wrong length bad_privs = ['\x01\x02' * 17] #some silly private key but > 33 bytes #next try to create wif with correct length but wrong compression byte bad_privs.append('\x07' * 32 + '\x02') for priv in bad_privs: with pytest.raises(Exception) as e_info: fake_wif = btc.wif_compressed_privkey(binascii.hexlify(priv)) #Create a wif with wrong length bad_wif1 = btc.bin_to_b58check('\x01\x02' * 34, 128) #Create a wif with wrong compression byte bad_wif2 = btc.bin_to_b58check('\x07' * 33, 128) for bw in [bad_wif1, bad_wif2]: with pytest.raises(Exception) as e_info: fake_priv = btc.from_wif_privkey(bw) #Some invalid b58 from bitcoin repo; #none of these are valid as any kind of key or address with open("test/base58_keys_invalid.json", "r") as f: json_data = f.read() invalid_key_list = json.loads(json_data) for k in invalid_key_list: bad_key = k[0] for netval in ["mainnet", "testnet"]: jm_single().config.set("BLOCKCHAIN", "network", netval) #if using py.test -s ; sanity check to see what's actually being tested print 'testing this key: ' + bad_key if "decode_privkey" in dir(btc): try: bad_key_format = btc.get_privkey_format(bad_key) print 'has correct format: ' + bad_key_format except: pass #should throw exception with pytest.raises(Exception) as e_info: if "decode_privkey" in dir(btc): from_wif_key = btc.decode_privkey(bad_key) else: from_wif_key = btc.from_wif_compressed_privkey( bad_key, btc.get_version_byte(bad_key)) #in case the b58 check encoding is valid, we should #also check if the leading version byte is in the #expected set, and throw an error if not. if chr(btc.get_version_byte(bad_key)) not in '\x80\xef': raise Exception("Invalid version byte") #the bitcoin library should throw #if the compression byte is not there (test not needed #for secp256k1 branch since the wif_compressed function checks) if "decode_privkey" in dir(btc): if "compressed" in btc.get_privkey_format(bad_key) and \ btc.b58check_to_bin(x)[-1] != '\x01': raise Exception("Invalid compression byte")
def genesis_commitments(wallets, blind): commitments = [] for pkh_b58, amount in wallets.iteritems(): # Public key hash corresponding to this Tezos address. pkh = bitcoin.b58check_to_bin(pkh_b58)[2:] # The redemption code is unique to the public key hash and deterministically # constructed using a secret blinding value. secret = secret_code(pkh, blind) # The redemption code is used to blind the pkh blinded_pkh = blake2b(pkh, 20, key=secret).digest() commitment = { 'half_pkh': binascii.hexlify(pkh[:10]), 'blinded_pkh': binascii.hexlify(blinded_pkh), 'amount': tez_to_int(amount) } commitments.append(commitment) return commitments
def genesis_commitments(wallets, blind): commitments = [] for pkh_b58, amount in wallets.iteritems(): # Public key hash corresponding to this Tezos address. pkh = bitcoin.b58check_to_bin(pkh_b58)[2:] hpkh_b58 = bitcoin.bin_to_b58check((pkh[:10] + '\000' * 10), magicbyte=434591) # The redemption code is unique to the public key hash and deterministically # constructed using a secret blinding value. secret = secret_code(pkh, blind) # The redemption code is used to blind the pkh blinded_pkh = blake2b(pkh, 20, key=secret).digest() commitment = { 'half_pkh': hpkh_b58, 'blinded_pkh': bitcoin.bin_to_b58check(blinded_pkh, magicbyte=16921055), 'amount': amount } commitments.append(commitment) return commitments
salt = unicodedata.normalize( "NFKD", (email + password).decode("utf8")).encode("utf8") try: seed = bitcoin.mnemonic_to_seed(mnemonic, salt) except: print("Invalid mnemonic") exit(1) pk, sk = pysodium.crypto_sign_seed_keypair(seed[0:32]) pkh = blake2b(pk, 20).digest() print "public key hash: ", tezos_pkh(pkh) elif len(sys.argv) == 2: tz_input = sys.argv[1] assert (tz_input == bitcoin.bin_to_b58check( bitcoin.b58check_to_bin(tz_input)[2:], magicbyte=434591)) try: pkh = bitcoin.b58check_to_bin(tz_input)[2:] except: print "Invalid public key hash" else: print("""Usage: python keychecker.py garage absurd steak ... email password or python keychecker.py tz1YoUrPuBlicKeYhaSh""") exit(1) print "Ethereum data: ", ethereum_data(pkh) print "Bitcoin address: ", bitcoin_address(pkh)
def append(self, addrs): for addr in addrs: self.payload.extend(b58check_to_bin(addr)) pubkeybytes = bfh(self.wallet.get_public_key(addr, tweaked=False)) self.payload.extend(pubkeybytes)
num_to_activate, o = _from_vli(encodedstr) encodedstr = encodedstr[o:] num_offsets, o = _from_vli(encodedstr) offsets = [encodedstr[i:i + n] for i in range(0, num_addresses, 32)] encodedstr = encodedstr[(num_offsets * 32):] ephemeral_pubkey = encodedstr[:33] encodedstr = encodedstr[33:] ephemeral_pubkey = bitcoin.decode_pubkey(ephemeral_pubkey) lcipher, o = _from_vli(encodedstr) encodedstr = encodedstr[o:] ciphertext = encodedstr[:lcipher] encodedstr = encodedstr[o:] tag = encodedstr return ephemeral_pubkey, ciphertext, tag, addresses, num_to_activate, offsets if __name__ == '__main__': k1 = _secure_privkey() K1 = bitcoin.privtopub(k1) k2 = _secure_privkey() K2 = bitcoin.privtopub(k2) R, c, t = becies_encrypt("Hello, World", K2) msg = becies_encode(R, c, t, pubkeys=[K2]) output_expected = (R, c, t, [bitcoin.b58check_to_bin(bitcoin.pubtoaddr(K2)) ], None, []) output = becies_decode(msg) print(output) print(output_expected) print(str(output) == str(output_expected))