def test_ecdsa_txs(verbose: bool = False):
    signer = SignerFactory.from_private_key(
        "e5b42f3c3fe02e161d42ff4707a174a5715b2badc7d4d3aebbea9081bd9123d5",
        SignerType.ECDSA,
    )
    if verbose:
        print(signer.to_dict())
    assert signer.to_dict(
    )["address"] == "Bis1SAk19HCWpDAThwFiaP9xA6zWjzsga7Hog"

    public_key_hex = signer.to_dict()["public_key"]
    public_key_net = base64.b64encode(
        bytes.fromhex(public_key_hex)).decode("utf-8")

    with open("./vectors/ecdsa_txs.json", "r") as f:
        txs = json.load(f)
        assert txs[0]["public_key"] == public_key_net
        for tx in txs:
            if verbose:
                print(f"Testing tx #{tx['id']}")
            buffer = tx['buffer'].encode()
            # Recalc sig from buffer
            signature = signer.sign_buffer_for_bis(buffer)
            # Make sure sig is same as before
            assert signature == tx["signature"]
            # Make sure it still verifies as well
            SignerFactory.verify_bis_signature(
                signature, public_key_net, buffer,
                "Bis1SAk19HCWpDAThwFiaP9xA6zWjzsga7Hog")
Example #2
0
def test_tx_signature(myserver, verbose=False):
    client = get_client(verbose=verbose)
    client.command(command="regtest_generate", options=[1])  # Mine a block
    sleep(1)
    r = client.command(command="blocklast")
    if verbose:
        print(f"blocklast returns {r}")
    tx = Transaction.from_legacy_params(r[0], r[1], r[2], r[3], r[4], r[5],
                                        r[6], r[7], r[8], r[9], r[10], r[11])
    buffer = tx.to_buffer_for_signing()
    db_signature_enc = r[5]
    db_public_key_b64encoded = r[
        6]  # For rsa, once decoded, this gives a properly formatted ---begin.... pubkey
    db_address = r[2]
    signed = True
    try:
        SignerFactory.verify_bis_signature(db_signature_enc,
                                           db_public_key_b64encoded, buffer,
                                           db_address)
    except Exception:
        signed = False
    assert signed
Example #3
0
def find_it(string: str):
    found = 0
    while True:
        # pk = urandom(32).hex()
        pk = token_hex(32)  # +50% time, but supposed to be cryptographically secure
        signer = SignerFactory.from_seed(pk, signer_type, subtype=subtype)
        address = signer._address
        if not options.case:
            address = address.lower()
        if string in address:
            print(signer._address, pk)
            found += 1
            if found > options.max:
                return
def generic_test(signer_type: SignerType.ECDSA, verbose: bool = False):
    if verbose:
        print("Type", signer_type)
    random.seed(SEED)
    for i in range(100):
        seed = hex(random.getrandbits(32 * 8))[2:]
        if verbose:
            # We can have leading zeros that get discarded. In that case, the seed will be shorter
            # and the lig will use it as a random seed to regenerate a full 32 bytes seed.
            # left as is, not filling here.
            print("seed", seed, len(seed))
        signer = SignerFactory.from_seed(seed, signer_type)
        signer_dict = signer.to_dict()
        if verbose:
            print(signer_dict)
        public_key_hex = signer.to_dict()["public_key"]
        public_key_net = base64.b64encode(
            bytes.fromhex(public_key_hex)).decode("utf-8")

        buffer = hex(random.getrandbits(64 * 8)).encode()
        signature = signer.sign_buffer_for_bis(buffer)
        SignerFactory.verify_bis_signature(signature, public_key_net, buffer,
                                           signer_dict["address"])