Пример #1
0
 def check_seed(self, seed):
     secexp = self.stretch_key(seed)
     master_private_key = PrivateKey(int_to_be_bytes(secexp, 32))
     master_public_key = master_private_key.public_key.to_bytes(compressed=False)[1:]
     if master_public_key != bfh(self.mpk):
         logger.error('invalid password (mpk) %s %s', self.mpk, master_public_key.hex())
         raise InvalidPassword()
Пример #2
0
 def get_private_key_from_stretched_exponent(self,
                                             derivation_path: Sequence[int],
                                             secexp) -> bytes:
     assert len(derivation_path) == 2
     secexp = (secexp +
               self.get_sequence(self.mpk, derivation_path)) % CURVE_ORDER
     return int_to_be_bytes(secexp, 32)
Пример #3
0
 def get_pubkey_from_mpk(cls, mpk: str, derivation_path: Sequence[int]) -> PublicKey:
     assert len(derivation_path) == 2
     z = cls.get_sequence(mpk, derivation_path)
     master_public_key = cls._mpk_to_PublicKey(mpk)
     public_key = master_public_key.add(int_to_be_bytes(z, 32))
     assert not public_key.is_compressed()
     return public_key
Пример #4
0
 def _mpk_from_hex_seed(cls, hex_seed) -> str:
     secexp = cls.stretch_key(hex_seed.encode())
     master_private_key = PrivateKey(int_to_be_bytes(secexp, 32))
     return master_private_key.public_key.to_hex(compressed=False)[2:]
Пример #5
0
import pytest

from bitcoinx import (
    int_to_be_bytes,
    Script,
    SigHash,
    double_sha256,
    TxOutput,
    pack_le_uint32,
    OP_CODESEPARATOR,
)

from .utils import random_tx, random_script

ONE = int_to_be_bytes(1, size=32)


# My Python translation of the code in bitcoin-sv/src/test/sighash_tests.cpp that represents
# the original reference signature_hash() code
def ref_sighash(script_code, tx, input_index, hash_type):
    assert input_index < len(tx.inputs)

    # In case concatenating two scripts ends up with two codeseparators, or an
    # extra one at the end, this prevents all those possible incompatibilities.
    script_code = script_code.find_and_delete(Script() << OP_CODESEPARATOR)

    # Blank out other inputs' signatures
    empty_script = Script()
    for tx_in in tx.inputs:
        tx_in.script_sig = empty_script
Пример #6
0
 def test_eq(self):
     address = P2SH_Address(int_to_be_bytes(1, 20), Bitcoin)
     assert address == P2SH_Address(int_to_be_bytes(1, 20), Bitcoin)
     assert address != '31h1vYVSYuKP6AhS86fbRdMw9XHiiQ93Mb'
     assert address != P2PKH_Address(int_to_be_bytes(1, 20), Bitcoin)
Пример #7
0
 def test_to_string(self):
     address = P2SH_Address(int_to_be_bytes(1, 20), Bitcoin)
     assert address.to_string() == '31h1vYVSYuKP6AhS86fbRdMw9XHiiQ93Mb'
     address = P2SH_Address(int_to_be_bytes(1, 20), BitcoinTestnet)
     assert address.to_string() == '2MsFDzHRUAMpjHxKyoEHU3aMCMsVtXMsfu8'
Пример #8
0
class TestAddress:

    @pytest.mark.parametrize("string,kind,network,equal", (
        ('1111111111111111111114oLvT2', P2PKH_Address, Bitcoin, P2PKH_Address(bytes(20), Bitcoin)),
        ('mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX', P2PKH_Address, BitcoinTestnet,
         P2PKH_Address(int_to_be_bytes(1, 20), network=BitcoinTestnet)),
        ('31h1vYVSYuKP6AhS86fbRdMw9XHieotbST', P2SH_Address, Bitcoin,
         P2SH_Address(bytes(20), Bitcoin)),
        ('2MsFDzHRUAMpjHxKyoEHU3aMCMsVtXMsfu8', P2SH_Address, BitcoinTestnet,
         P2SH_Address(int_to_be_bytes(1, 20), network=BitcoinTestnet)),
    ))
    def test_from_string(self, string, kind, network, equal):
        address = Address.from_string(string, network)
        assert isinstance(address, kind)
        assert address.network() is network
        assert address == equal

    def test_from_string_network(self):
        assert Address.from_string('1111111111111111111114oLvT2', Bitcoin).to_string() == \
            '1111111111111111111114oLvT2'
        assert Address.from_string('mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX', BitcoinTestnet) \
                      .to_string() == 'mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX'
        assert Address.from_string(
            'mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX',
            BitcoinScalingTestnet).to_string() == 'mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX'
        with pytest.raises(ValueError):
            Address.from_string('1111111111111111111114oLvT2', BitcoinTestnet)
        with pytest.raises(ValueError):
            Address.from_string('mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX', Bitcoin)

    def test_from_string_bad(self):
        # Too short
        with pytest.raises(ValueError):
            Address.from_string('111111111111111111117K4nzc', Bitcoin)
        # Unknown version byte
        with pytest.raises(ValueError):
            Address.from_string('mm5Yiba1U2odgUskxnXMJGQMV1DSAXVPib', Bitcoin)

    @pytest.mark.parametrize("string,kind,network,equal", (
        ('qp7sl3kxvswe33zmm4mmm2chc22asud3j5g5p6g6u9', P2PKH_Address, Bitcoin,
         '1CQGN9WnzdYeFhT2YDS4xkm94PVzwFByC8'),
        ('pqcnpyfktqzkm9su04empn3ju8e2k4j74q2zzn7h0f', P2SH_Address, Bitcoin,
         '36B7DTHvi58L3rq9Ni3jRVxBkeJa3R5EC1'),
        ('PQCNPYFKTQZKM9SU04EMPN3JU8E2K4J74Q2ZZN7H0F', P2SH_Address, Bitcoin,
         '36B7DTHvi58L3rq9Ni3jRVxBkeJa3R5EC1'),
    ))
    def test_cashaddr(self, string, kind, network, equal):
        address = Address.from_string(string, network)
        assert isinstance(address, kind)
        assert address.network() is network
        assert address.to_string() == equal

    def test_cashaddr_bad(self):
        with pytest.raises(ValueError):
            address = Address.from_string('bitcoinCash:isamaurysbitcoinandtherealbcash', Bitcoin)
        with pytest.raises(ValueError):
            Address.from_string('bcash:qp7sl3kxvswe33zmm4mmm2chc22asud3j5g5p6g6u9', Bitcoin)
        with pytest.raises(ValueError):
            Address.from_string('zvqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqxhap8z55',
                                Bitcoin)
        with pytest.raises(ValueError):
            Address.from_string('qp7sl3kxvswe33zmm4mmm2chc22asud3j5g5p6g6u9', BitcoinTestnet)
Пример #9
0
 def test_eq(self):
     address = P2PKH_Address(int_to_be_bytes(1, 20), Bitcoin)
     assert address == P2PKH_Address(int_to_be_bytes(1, 20), Bitcoin)
     assert address == P2PKH_Address(int_to_be_bytes(1, 20), BitcoinTestnet)
     assert address != '11111111111111111111BZbvjr'
     assert address != P2SH_Address(int_to_be_bytes(1, 20), Bitcoin)
Пример #10
0
 def test_to_string(self):
     address = P2PKH_Address(int_to_be_bytes(1, 20), Bitcoin)
     assert address.to_string() == '11111111111111111111BZbvjr'
     address = P2PKH_Address(int_to_be_bytes(1, 20), BitcoinTestnet)
     assert address.to_string() == 'mfWxJ45yp2SFn7UciZyNpvDKrzbi36LaVX'
Пример #11
0
 def get_private_key_from_stretched_exponent(self, for_change, n, secexp):
     secexp = (secexp + self.get_sequence(self.mpk, for_change, n)) % CURVE_ORDER
     return int_to_be_bytes(secexp, 32)
Пример #12
0
 def get_pubkey_from_mpk(cls, mpk, for_change, n):
     z = cls.get_sequence(mpk, for_change, n)
     master_public_key = cls._mpk_to_PublicKey(mpk)
     public_key2 = master_public_key.add(int_to_be_bytes(z, 32))
     return public_key2.to_hex(compressed=False)
Пример #13
0
import ecdsa
from hashlib import sha256 as sha256_hashlib

secret = b'This is a secret message!'
h_secret = sha256(secret)
k = h_secret

secret_wrong = b'This is the wrong secret message!'
h_secret_wrong = sha256(secret_wrong)
k_wrong = h_secret

G = PublicKey.from_hex(
    '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798')
Q = G.multiply(k)
r, _ = Q.to_point()
r = int_to_be_bytes(r)
if r[0] & 0x80:
    r0 = pack_byte(0) + r
else:
    r0 = r
r_hash = hash160(r0)

# Ephermal key to generate the r signature
key_priv_R = PrivateKey.from_arbitrary_bytes(b'123test')
key_pub_R = key_priv_R.public_key

contract = './test/res/rpuzzle.scrypt'

compiler_result = scryptlib.utils.compile_contract(contract)
desc = compiler_result.to_desc()