How to use the secp256k1.ffi.buffer function in secp256k1

To help you get started, we’ve selected a few secp256k1 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github bitaps-com / pybtc / pybtc / hdwallet.py View on Github external
def add_public_keys(ext_value, key):
    pubkey_ptr = ffi.new('secp256k1_pubkey *')
    if not secp256k1.secp256k1_ec_pubkey_parse(ECDSA_CONTEXT_VERIFY, pubkey_ptr, ext_value, len(ext_value)):
        raise TypeError("public key format error")
    if secp256k1.secp256k1_ec_pubkey_tweak_add(ECDSA_CONTEXT_ALL, pubkey_ptr, key):
        pubkey = ffi.new('char [%d]' % 33)
        outlen = ffi.new('size_t *', 33)
        if secp256k1.secp256k1_ec_pubkey_serialize(ECDSA_CONTEXT_VERIFY, pubkey, outlen, pubkey_ptr, EC_COMPRESSED):
            return bytes(ffi.buffer(pubkey, 33))
    return None
github bitaps-com / pybtc / pybtc / tools.py View on Github external
if not r:
        raise RuntimeError("secp256k1 error")

    pubkey_ptr = ffi.new('secp256k1_pubkey *')
    t = secp256k1.secp256k1_ecdsa_recover(
                 ECDSA_CONTEXT_ALL, pubkey_ptr, recover_sig, messsage)
    len_key = 33 if compressed else 65
    pubkey = ffi.new('char [%d]' % len_key)
    outlen = ffi.new('size_t *', len_key)
    compflag = EC_COMPRESSED if compressed else EC_UNCOMPRESSED
    if bytes(ffi.buffer(pubkey_ptr.data, 64)) == b"\x00" * 64:
        return None
    r = secp256k1.secp256k1_ec_pubkey_serialize(ECDSA_CONTEXT_VERIFY, pubkey, outlen, pubkey_ptr, compflag)
    if not r:
        raise RuntimeError("secp256k1 error")
    pub = bytes(ffi.buffer(pubkey, len_key))
    return hexlify(pub).decode() if hex else pub
github bitaps-com / pybtc / pybtc / tools.py View on Github external
if private_key[0] in (MAINNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX,
                                      TESTNET_PRIVATE_KEY_UNCOMPRESSED_PREFIX):
                    compressed = False
                private_key = wif_to_private_key(private_key, hex=0)
        else:
            raise TypeError("private key must be a bytes or WIF or hex encoded string")
    pubkey_ptr = ffi.new('secp256k1_pubkey *')
    r = secp256k1.secp256k1_ec_pubkey_create(ECDSA_CONTEXT_ALL, pubkey_ptr, private_key)
    if not r:
        raise RuntimeError("secp256k1 error")
    len_key = 33 if compressed else 65
    pubkey = ffi.new('char [%d]' % len_key)
    outlen = ffi.new('size_t *', len_key)
    compflag = EC_COMPRESSED if compressed else EC_UNCOMPRESSED
    r = secp256k1.secp256k1_ec_pubkey_serialize(ECDSA_CONTEXT_VERIFY, pubkey, outlen, pubkey_ptr, compflag)
    pub = bytes(ffi.buffer(pubkey, len_key))
    if not r:
        raise RuntimeError("secp256k1 error")
    return hexlify(pub).decode() if hex else pub
github bitaps-com / pybtc / pybtc / tools.py View on Github external
if not isinstance(private_key, bytes):
            raise TypeError("private key must be a bytes, hex encoded string or in WIF format")

    raw_sig = ffi.new('secp256k1_ecdsa_signature *')
    signed = secp256k1.secp256k1_ecdsa_sign(ECDSA_CONTEXT_SIGN, raw_sig, msg,
                                            private_key, ffi.NULL, ffi.NULL)
    if not signed:
        raise RuntimeError("secp256k1 error")
    len_sig = 74
    output = ffi.new('unsigned char[%d]' % len_sig)
    outputlen = ffi.new('size_t *', len_sig)
    res = secp256k1.secp256k1_ecdsa_signature_serialize_der(ECDSA_CONTEXT_SIGN,
                                                            output, outputlen, raw_sig)
    if not res:
        raise RuntimeError("secp256k1 error")
    signature = bytes(ffi.buffer(output, outputlen[0]))
    raw_sig = ffi.new('secp256k1_ecdsa_signature *')
    return hexlify(signature).decode() if hex else signature
github HuangFJ / pyeth / pyeth / crypto.py View on Github external
def pubkey_format(pub):
    assert pub.public_key, "No public key defined"

    len_compressed = 65
    res_compressed = ffi.new('unsigned char [%d]' % len_compressed)

    serialized = lib.secp256k1_ec_pubkey_serialize(
        pub.ctx, res_compressed, ffi.new('size_t *', len_compressed), pub.public_key, EC_UNCOMPRESSED)
    assert serialized == 1

    return bytes(ffi.buffer(res_compressed, len_compressed))
github bitaps-com / pybtc / pybtc / tools.py View on Github external
raise RuntimeError("secp256k1 error")

    recover_sig = ffi.new('secp256k1_ecdsa_recoverable_signature *')
    t = secp256k1.secp256k1_ecdsa_recoverable_signature_parse_compact(
                                            ECDSA_CONTEXT_ALL, recover_sig, compact_sig, rec_id)
    if not r:
        raise RuntimeError("secp256k1 error")

    pubkey_ptr = ffi.new('secp256k1_pubkey *')
    t = secp256k1.secp256k1_ecdsa_recover(
                 ECDSA_CONTEXT_ALL, pubkey_ptr, recover_sig, messsage)
    len_key = 33 if compressed else 65
    pubkey = ffi.new('char [%d]' % len_key)
    outlen = ffi.new('size_t *', len_key)
    compflag = EC_COMPRESSED if compressed else EC_UNCOMPRESSED
    if bytes(ffi.buffer(pubkey_ptr.data, 64)) == b"\x00" * 64:
        return None
    r = secp256k1.secp256k1_ec_pubkey_serialize(ECDSA_CONTEXT_VERIFY, pubkey, outlen, pubkey_ptr, compflag)
    if not r:
        raise RuntimeError("secp256k1 error")
    pub = bytes(ffi.buffer(pubkey, len_key))
    return hexlify(pub).decode() if hex else pub