示例#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 Contract_cal_address_from_data(cls, caller_address, index, trx_id):
        datas = cls.ser_string(caller_address)
        datas += cls.ser_string(trx_id)
        datas += struct.pack("<I", index)

        sh_data = sha256(datas).digest()
        hs_data = hash160(sh_data).digest()

        be_bytes = hs_data + sha256(hs_data).digest()[:4]
        return "CON" + Base58.encode(be_bytes)
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")
示例#5
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]
示例#6
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))
示例#7
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.
        '''
        #比特币地址格式为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))
示例#8
0
 def privkey_WIF(cls, privkey_bytes, compressed):
     '''Return the private key encoded in Wallet Import Format.'''
     payload = bytearray(cls.WIF_BYTE) + privkey_bytes
     if compressed:
         payload.append(0x01)
     return Base58.encode_check(payload)
示例#9
0
 def P2SH_address_from_hash160(cls, hash160):
     '''Return a coin address given a hash160.'''
     assert len(hash160) == 20
     return Base58.encode_check(cls.P2SH_VERBYTES[0] + hash160)
示例#10
0
 def P2PKH_address_from_hash160(cls, hash160):
     '''Return a P2PKH address given a public key.'''
     assert len(hash160) == 20
     return Base58.encode_check(cls.P2PKH_VERBYTE + hash160)
示例#11
0
 def extended_key_string(self, ver_bytes):
     '''Return an extended key as a base58 string.'''
     return Base58.encode_check(self.extended_key(ver_bytes))
示例#12
0
 def WIF(self, wif_byte, compressed=True):
     '''Return the private key encoded in Wallet Import Format.'''
     payload = bytearray([wif_byte]) + self.privkey_bytes
     if compressed:
         payload.append(0x01)
     return Base58.encode_check(payload)
示例#13
0
 def address(self, ver_byte):
     "The public key as a P2PKH address"
     return Base58.encode_check(
         bytes([ver_byte]) + hash160(self.pubkey_bytes))
示例#14
0
 def extended_key_string(self, coin):
     '''Return an extended key as a base58 string.'''
     return Base58.encode_check(self.extended_key(coin))