예제 #1
0
def from_extended_key_string(ekey_str):
    '''Given an extended key string, such as

    xpub6BsnM1W2Y7qLMiuhi7f7dbAwQZ5Cz5gYJCRzTNainXzQXYjFwtuQXHd
    3qfi3t3KJtHxshXezfjft93w4UE7BGMtKwhqEHae3ZA7d823DVrL

    return a (key, coin) pair.  key is either a PubKey or PrivKey.
    '''
    return _from_extended_key(Base58.decode_check(ekey_str))
예제 #2
0
def test_from_extended_key():
    # Tests the failure modes of from_extended_key.
    with pytest.raises(TypeError):
        bip32._from_extended_key('')
    with pytest.raises(ValueError):
        bip32._from_extended_key(b'')
    with pytest.raises(CoinError):
        bip32._from_extended_key(bytes(78))
    # Invalid prefix byte
    raw = Base58.decode_check(MXPRV)
    with pytest.raises(ValueError):
        bip32._from_extended_key(raw[:45] + b'\1' + raw[46:])
def test_address_from_hash160(address):
    coin, addr, hash, _ = address

    raw = Base58.decode_check(addr)
    verlen = len(raw) - 20
    assert verlen > 0
    verbyte, hash_bytes = raw[:verlen], raw[verlen:]
    if coin.P2PKH_VERBYTE == verbyte:
        assert coin.P2PKH_address_from_hash160(bytes.fromhex(hash)) == addr
    elif verbyte in coin.P2SH_VERBYTES:
        assert coin.P2SH_address_from_hash160(bytes.fromhex(hash)) == addr
    else:
        raise Exception("Unknown version byte")
예제 #4
0
def from_extended_key_string(ekey_str):
    '''Given an extended key string, such as

    xpub6BsnM1W2Y7qLMiuhi7f7dbAwQZ5Cz5gYJCRzTNainXzQXYjFwtuQXHd
    3qfi3t3KJtHxshXezfjft93w4UE7BGMtKwhqEHae3ZA7d823DVrL

    return a (key, verbytes) pair.   key is either a MasterPubKey or
    MasterPrivKey.  verbytes is a bytes object of length 4.

    Caller might want to select coin based on the version bytes, and
    check public or private as appropriate.  Sadly version bytes are
    not unique across coins, so this has limited value.
    '''
    ekey = Base58.decode_check(ekey_str)
    key = _from_extended_key(ekey)
    return key, ekey[:4]
예제 #5
0
    def pay_to_address_script(cls, address):
        '''Return a pubkey script that pays to a pubkey hash.

        Pass the address (either P2PKH or P2SH) in base58 form.
        '''
        raw = Base58.decode_check(address)

        # Require version byte plus hash160.
        verbyte = -1
        if len(raw) == 21:
            verbyte, hash_bytes = raw[0], raw[1:]

        if verbyte == cls.P2PKH_VERBYTE:
            return ScriptPubKey.P2PKH_script(hash_bytes)
        if verbyte == cls.P2SH_VERBYTE:
            return ScriptPubKey.P2SH_script(hash_bytes)

        raise CoinError('invalid address: {}'.format(address))
예제 #6
0
파일: coins.py 프로젝트: ZorroII/electrumx
    def pay_to_address_script(cls, address):
        '''Return a pubkey script that pays to a pubkey hash.

        Pass the address (either P2PKH or P2SH) in base58 form.
        '''
        #比特币地址格式为base58(version(1byte)+hash(20bytes)+checksum(4bytes))
        #这里decode_check会对比特币地址解码后,校验最后四字节,然后去掉四字节,返回21字节的地址数据
        raw = Base58.decode_check(address)

        # Require version byte(s) plus hash160.
        verbyte = -1
        verlen = len(raw) - 20
        if verlen > 0:
            verbyte, hash_bytes = raw[:verlen], raw[verlen:]

        if verbyte == cls.P2PKH_VERBYTE:
            return ScriptPubKey.P2PKH_script(hash_bytes)
        if verbyte in cls.P2SH_VERBYTES:
            return ScriptPubKey.P2SH_script(hash_bytes)

        raise CoinError('invalid address: {}'.format(address))