示例#1
0
def validate_address(addr):
    try:
        assert len(addr) > 2
        if addr[:2].lower() in ['bc', 'tb']:
            # Regtest special case
            if addr[:4] == 'bcrt':
                if btc.bech32addr_decode('bcrt', addr)[1]:
                    return True, 'address validated'
                return False, 'Invalid bech32 regtest address'
            #Else, enforce testnet/mainnet per config
            if get_network() == "testnet":
                hrpreq = 'tb'
            else:
                hrpreq = 'bc'
            if btc.bech32addr_decode(hrpreq, addr)[1]:
                return True, 'address validated'
            return False, 'Invalid bech32 address'
        #Not bech32; assume b58 from here
        ver = btc.get_version_byte(addr)
    except AssertionError:
        return False, 'Checksum wrong. Typo in address?'
    except Exception:
        return False, "Invalid bitcoin 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'
示例#2
0
    def wif_to_privkey(cls, wif):
        raw = btc.b58check_to_bin(wif)
        vbyte = struct.unpack('B', btc.get_version_byte(wif))[0]

        if (struct.unpack('B', btc.BTC_P2PK_VBYTE[get_network()])[0] + struct.unpack('B', cls.WIF_PREFIX)[0]) & 0xff == vbyte:
            key_type = TYPE_P2PKH
        elif (struct.unpack('B', btc.BTC_P2SH_VBYTE[get_network()])[0] + struct.unpack('B', cls.WIF_PREFIX)[0]) & 0xff == vbyte:
            key_type = TYPE_P2SH_P2WPKH
        else:
            key_type = None

        return raw, key_type
def validate_address(addr, nettype):
    """A mock of jmclient.validate_address
    """
    BTC_P2PK_VBYTE = {"mainnet": b'\x00', "testnet": b'\x6f'}
    BTC_P2SH_VBYTE = {"mainnet": b'\x05', "testnet": b'\xc4'}
    try:
        ver = btc.get_version_byte(addr)
    except AssertionError as e:
        return False, 'Checksum wrong. Typo in address?'
    except Exception as e:
        return False, "Invalid bitcoin address"
    if ver not in [BTC_P2PK_VBYTE[nettype], BTC_P2SH_VBYTE[nettype]]:
        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'
示例#4
0
    def wif_to_privkey(cls, wif):
        raw = btc.b58check_to_bin(wif)[1]
        # see note to `privkey_to_wif`; same applies here.
        # We only handle valid private keys, not any byte string.
        btc.read_privkey(raw)

        vbyte = struct.unpack('B', btc.get_version_byte(wif))[0]

        if (struct.unpack('B', btc.BTC_P2PK_VBYTE[get_network()])[0] + \
            struct.unpack('B', cls.WIF_PREFIX)[0]) & 0xff == vbyte:
            key_type = TYPE_P2PKH
        elif (struct.unpack('B', btc.BTC_P2SH_VBYTE[get_network()])[0] + \
              struct.unpack('B', cls.WIF_PREFIX)[0]) & 0xff == vbyte:
            key_type = TYPE_P2SH_P2WPKH
        else:
            key_type = None
        return raw, key_type
示例#5
0
    def wif_to_privkey(cls, wif):
        """ Note July 2020: the `key_type` construction below is
        custom and is not currently used. Future code should
        not use this returned `key_type` variable.
        """
        raw = btc.b58check_to_bin(wif)[1]
        # see note to `privkey_to_wif`; same applies here.
        # We only handle valid private keys, not any byte string.
        btc.read_privkey(raw)

        vbyte = struct.unpack('B', btc.get_version_byte(wif))[0]

        if (struct.unpack('B', btc.BTC_P2PK_VBYTE[get_network()])[0] + \
            struct.unpack('B', cls.WIF_PREFIX)[0]) & 0xff == vbyte:
            key_type = TYPE_P2PKH
        elif (struct.unpack('B', btc.BTC_P2SH_VBYTE[get_network()])[0] + \
              struct.unpack('B', cls.WIF_PREFIX)[0]) & 0xff == vbyte:
            key_type = TYPE_P2SH_P2WPKH
        else:
            key_type = None
        return raw, key_type