def from_string(cls, string, net=Net): '''Construct from an address string.''' if len(string) > 35: try: return cls.from_cashaddr_string(string) except ValueError as e: raise AddressError(str(e)) try: raw = base58_decode_check(string) except ValueError as e: raise AddressError(str(e)) # Require version byte(s) plus hash160. if len(raw) != 21: raise AddressError('invalid address: {}'.format(string)) verbyte, hash160_ = raw[0], raw[1:] if verbyte == net.ADDRTYPE_P2PKH: kind = cls.ADDR_P2PKH elif verbyte == net.ADDRTYPE_P2SH: kind = cls.ADDR_P2SH else: raise AddressError('unknown version byte: {}'.format(verbyte)) return cls(hash160_, kind)
def __init__(self, value): if isinstance(value, str): if len(value) == 40: value = bytes.fromhex(value) else: value = base58_decode_check(value)[1:] assert isinstance(value, bytes) assert len(value) == 20 super().__init__(value)
def test_bip32_key_from_string_bad(): # Tests the failure modes of from_extended_key. with pytest.raises(Base58Error): bip32_key_from_string('') with pytest.raises(ValueError): bip32_key_from_string('1Po1oWkD2LmodfkBYiAktwh76vkF93LKnh') with pytest.raises(TypeError): bip32_key_from_string(b'') with pytest.raises(Base58Error): bip32_key_from_string(bytes(78).decode()) # Invalid prefix byte raw = base58_decode_check(MXPRV) bad_string = base58_encode_check(raw[:45] + b'\1' + raw[46:]) with pytest.raises(ValueError): bip32_key_from_string(bad_string)
def get_xpubkey(self, c, i): s = ''.join(int_to_hex(x,2) for x in (c, i)) return XPublicKey('ff' + base58_decode_check(self.xpub).hex() + s)