Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions include/wally.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,18 @@ inline int addr_segwit_to_bytes(const ADDR& addr, const ADDR_FAMILY& addr_family
return detail::check_ret(__FUNCTION__, ret);
}

template <class BYTES, class ADDR_FAMILY>
inline int addr_silent_payment_from_bytes(const BYTES& bytes, const ADDR_FAMILY& addr_family, uint32_t flags, char** output) {
int ret = ::wally_addr_silent_payment_from_bytes(bytes.data(), bytes.size(), detail::get_p(addr_family), flags, output);
return detail::check_ret(__FUNCTION__, ret);
}

template <class ADDR, class ADDR_FAMILY, class BYTES_OUT>
inline int addr_silent_payment_to_bytes(const ADDR& addr, const ADDR_FAMILY& addr_family, uint32_t flags, BYTES_OUT& bytes_out) {
int ret = ::wally_addr_silent_payment_to_bytes(detail::get_p(addr), detail::get_p(addr_family), flags, bytes_out.data(), bytes_out.size());
return detail::check_ret(__FUNCTION__, ret);
}

template <class ADDR, class BYTES_OUT>
inline int address_to_scriptpubkey(const ADDR& addr, uint32_t network, BYTES_OUT& bytes_out, size_t* written) {
int ret = ::wally_address_to_scriptpubkey(detail::get_p(addr), network, bytes_out.data(), bytes_out.size(), written);
Expand Down Expand Up @@ -725,6 +737,18 @@ inline int descriptor_to_script_get_maximum_length(const DESCRIPTOR& descriptor,
return detail::check_ret(__FUNCTION__, ret);
}

template <class PRIV_KEY, class PUB_KEY, class AUX_RAND, class GENERATOR, class MSG, class BYTES_OUT>
inline int dleq_proof_generate(const PRIV_KEY& priv_key, const PUB_KEY& pub_key, const AUX_RAND& aux_rand, const GENERATOR& generator, const MSG& msg, uint32_t flags, BYTES_OUT& bytes_out) {
int ret = ::wally_dleq_proof_generate(priv_key.data(), priv_key.size(), pub_key.data(), pub_key.size(), aux_rand.data(), aux_rand.size(), generator.data(), generator.size(), msg.data(), msg.size(), flags, bytes_out.data(), bytes_out.size());
return detail::check_ret(__FUNCTION__, ret);
}

template <class PUB_KEY_A, class PUB_KEY_B, class PUB_KEY_C, class PROOF, class GENERATOR, class MSG>
inline bool dleq_proof_verify(const PUB_KEY_A& pub_key_a, const PUB_KEY_B& pub_key_b, const PUB_KEY_C& pub_key_c, const PROOF& proof, const GENERATOR& generator, const MSG& msg, uint32_t flags) {
int ret = ::wally_dleq_proof_verify(pub_key_a.data(), pub_key_a.size(), pub_key_b.data(), pub_key_b.size(), pub_key_c.data(), pub_key_c.size(), proof.data(), proof.size(), generator.data(), generator.size(), msg.data(), msg.size(), flags);
return ret == WALLY_OK;
}

template <class PRIV_KEY, class MERKLE_ROOT, class BYTES_OUT>
inline int ec_private_key_bip341_tweak(const PRIV_KEY& priv_key, const MERKLE_ROOT& merkle_root, uint32_t flags, BYTES_OUT& bytes_out) {
int ret = ::wally_ec_private_key_bip341_tweak(priv_key.data(), priv_key.size(), merkle_root.data(), merkle_root.size(), flags, bytes_out.data(), bytes_out.size());
Expand Down Expand Up @@ -1282,6 +1306,12 @@ inline int psbt_add_output_taproot_keypath(const PSBT& psbt, uint32_t index, uin
return detail::check_ret(__FUNCTION__, ret);
}

template <class PSBT, class PRIV_KEYS, class AUX_RAND>
inline int psbt_add_sp_ecdh_shares(const PSBT& psbt, const PRIV_KEYS& priv_keys, const AUX_RAND& aux_rand, uint32_t flags) {
int ret = ::wally_psbt_add_sp_ecdh_shares(detail::get_p(psbt), priv_keys.data(), priv_keys.size(), aux_rand.data(), aux_rand.size(), flags);
return detail::check_ret(__FUNCTION__, ret);
}

template <class PSBT>
inline int psbt_add_tx_input_at(const PSBT& psbt, uint32_t index, uint32_t flags, const struct wally_tx_input* input) {
int ret = ::wally_psbt_add_tx_input_at(detail::get_p(psbt), index, flags, input);
Expand Down Expand Up @@ -1317,6 +1347,12 @@ inline int psbt_combine_ex(const PSBT& psbt, uint32_t flags, const struct wally_
return detail::check_ret(__FUNCTION__, ret);
}

template <class PSBT>
inline int psbt_compute_sp_scripts(const PSBT& psbt, uint32_t flags) {
int ret = ::wally_psbt_compute_sp_scripts(detail::get_p(psbt), flags);
return detail::check_ret(__FUNCTION__, ret);
}

template <class PSBT>
inline int psbt_extract(const PSBT& psbt, uint32_t flags, struct wally_tx** output) {
int ret = ::wally_psbt_extract(detail::get_p(psbt), flags, output);
Expand Down Expand Up @@ -1623,6 +1659,11 @@ inline int psbt_output_clear_amount(struct wally_psbt_output* output) {
return detail::check_ret(__FUNCTION__, ret);
}

inline int psbt_output_clear_sp_v0_info(struct wally_psbt_output* output) {
int ret = ::wally_psbt_output_clear_sp_v0_info(output);
return detail::check_ret(__FUNCTION__, ret);
}

template <class OUTPUT, class PUB_KEY>
inline int psbt_output_find_keypath(const OUTPUT& output, const PUB_KEY& pub_key, size_t* written) {
int ret = ::wally_psbt_output_find_keypath(detail::get_p(output), pub_key.data(), pub_key.size(), written);
Expand All @@ -1635,6 +1676,18 @@ inline int psbt_output_find_unknown(const OUTPUT& output, const KEY& key, size_t
return detail::check_ret(__FUNCTION__, ret);
}

template <class OUTPUT, class BYTES_OUT>
inline int psbt_output_get_sp_v0_info(const OUTPUT& output, BYTES_OUT& bytes_out, size_t* written) {
int ret = ::wally_psbt_output_get_sp_v0_info(detail::get_p(output), bytes_out.data(), bytes_out.size(), written);
return detail::check_ret(__FUNCTION__, ret);
}

template <class OUTPUT>
inline int psbt_output_get_sp_v0_info_len(const OUTPUT& output, size_t* written) {
int ret = ::wally_psbt_output_get_sp_v0_info_len(detail::get_p(output), written);
return detail::check_ret(__FUNCTION__, ret);
}

template <class OUTPUT, class PUB_KEY, class FINGERPRINT, class CHILD_PATH>
inline int psbt_output_keypath_add(const OUTPUT& output, const PUB_KEY& pub_key, const FINGERPRINT& fingerprint, const CHILD_PATH& child_path) {
int ret = ::wally_psbt_output_keypath_add(detail::get_p(output), pub_key.data(), pub_key.size(), fingerprint.data(), fingerprint.size(), child_path.data(), child_path.size());
Expand Down Expand Up @@ -1665,6 +1718,12 @@ inline int psbt_output_set_script(const OUTPUT& output, const SCRIPT& script) {
return detail::check_ret(__FUNCTION__, ret);
}

template <class OUTPUT, class KEYS>
inline int psbt_output_set_sp_v0_info(const OUTPUT& output, const KEYS& keys) {
int ret = ::wally_psbt_output_set_sp_v0_info(detail::get_p(output), keys.data(), keys.size());
return detail::check_ret(__FUNCTION__, ret);
}

template <class OUTPUT, class PUB_KEY>
inline int psbt_output_set_taproot_internal_key(const OUTPUT& output, const PUB_KEY& pub_key) {
int ret = ::wally_psbt_output_set_taproot_internal_key(detail::get_p(output), pub_key.data(), pub_key.size());
Expand Down
39 changes: 39 additions & 0 deletions include/wally_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ struct ext_key;
#define WALLY_SEGWIT_V0_ADDRESS_PUBKEY_MAX_LEN 34 /** OP_0 OP_PUSH_{20,32} [20 bytes for wpkh, 32 for wsh] */
#define WALLY_SEGWIT_V1_ADDRESS_PUBKEY_LEN 34 /** OP_1 OP_PUSH_32 [32-bytes x-only pubkey] */

#define WALLY_SILENT_PAYMENT_KEYS_LEN 66 /** BIP-352 scan public key followed by spend public key */

/**
* Create a segwit native address from a v0 or later witness program.
*
Expand Down Expand Up @@ -129,6 +131,43 @@ WALLY_CORE_API int wally_addr_segwit_n_get_version(
uint32_t flags,
size_t *written);

/**
* Create a BIP-352 silent payment address from a scan and spend public key.
*
* :param bytes: The compressed scan public key followed by the compressed
*| spend public key.
* :param bytes_len: Length of ``bytes`` in bytes. Must be `WALLY_SILENT_PAYMENT_KEYS_LEN`.
* :param addr_family: Address family to generate, e.g. "sp" or "tsp".
* :param flags: For future use. Must be 0.
* :param output: Destination for the resulting version 0 silent payment address.
*| The string returned should be freed using `wally_free_string`.
*/
WALLY_CORE_API int wally_addr_silent_payment_from_bytes(
const unsigned char *bytes,
size_t bytes_len,
const char *addr_family,
uint32_t flags,
char **output);

/**
* Get the scan and spend public keys from a BIP-352 silent payment address.
*
* Addresses of versions 1 to 30 are accepted and only their first
* `WALLY_SILENT_PAYMENT_KEYS_LEN` bytes are returned, as BIP-352 requires.
*
* :param addr: Silent payment address to decode.
* :param addr_family: Address family expected, e.g. "sp" or "tsp".
* :param flags: For future use. Must be 0.
* :param bytes_out: Destination for the scan public key followed by the spend public key.
* FIXED_SIZED_OUTPUT(len, bytes_out, WALLY_SILENT_PAYMENT_KEYS_LEN)
*/
WALLY_CORE_API int wally_addr_silent_payment_to_bytes(
const char *addr,
const char *addr_family,
uint32_t flags,
unsigned char *bytes_out,
size_t len);

/**
* Infer a scriptPubKey from an address.
*
Expand Down
71 changes: 71 additions & 0 deletions include/wally_crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,77 @@ WALLY_CORE_API int wally_ecdh(
unsigned char *bytes_out,
size_t len);

/** The length of a BIP-374 DLEQ proof */
#define WALLY_DLEQ_PROOF_LEN 64

/**
* Generate a BIP-374 proof that ``a*G`` and ``a*B`` share the same secret ``a``.
*
* :param priv_key: The secret ``a``.
* :param priv_key_len: The length of ``priv_key`` in bytes. Must be `EC_PRIVATE_KEY_LEN`.
* :param pub_key: The public key ``B``.
* :param pub_key_len: The length of ``pub_key`` in bytes. Must be `EC_PUBLIC_KEY_LEN`.
* :param aux_rand: Fresh random data, as for BIP-340 signing.
* :param aux_rand_len: The length of ``aux_rand`` in bytes. Must be 32.
* :param generator: The generator ``G``, or NULL for the secp256k1 generator.
* :param generator_len: The length of ``generator`` in bytes. Must be
*| `EC_PUBLIC_KEY_LEN`, or 0 if ``generator`` is NULL.
* :param msg: An optional message the proof commits to, or NULL.
* :param msg_len: The length of ``msg`` in bytes. Must be 32, or 0 if ``msg`` is NULL.
* :param flags: For future use. Must be 0.
* :param bytes_out: Destination for the proof.
* FIXED_SIZED_OUTPUT(len, bytes_out, WALLY_DLEQ_PROOF_LEN)
*/
WALLY_CORE_API int wally_dleq_proof_generate(
const unsigned char *priv_key,
size_t priv_key_len,
const unsigned char *pub_key,
size_t pub_key_len,
const unsigned char *aux_rand,
size_t aux_rand_len,
const unsigned char *generator,
size_t generator_len,
const unsigned char *msg,
size_t msg_len,
uint32_t flags,
unsigned char *bytes_out,
size_t len);

/**
* Verify a BIP-374 proof that ``A = a*G`` and ``C = a*B`` for the same ``a``.
*
* :param pub_key_a: The public key ``A``.
* :param pub_key_a_len: The length of ``pub_key_a`` in bytes. Must be `EC_PUBLIC_KEY_LEN`.
* :param pub_key_b: The public key ``B``.
* :param pub_key_b_len: The length of ``pub_key_b`` in bytes. Must be `EC_PUBLIC_KEY_LEN`.
* :param pub_key_c: The public key ``C``.
* :param pub_key_c_len: The length of ``pub_key_c`` in bytes. Must be `EC_PUBLIC_KEY_LEN`.
* :param proof: The proof to verify.
* :param proof_len: The length of ``proof`` in bytes. Must be `WALLY_DLEQ_PROOF_LEN`.
* :param generator: The generator ``G``, or NULL for the secp256k1 generator.
* :param generator_len: The length of ``generator`` in bytes. Must be
*| `EC_PUBLIC_KEY_LEN`, or 0 if ``generator`` is NULL.
* :param msg: The message the proof commits to, or NULL if none.
* :param msg_len: The length of ``msg`` in bytes. Must be 32, or 0 if ``msg`` is NULL.
* :param flags: For future use. Must be 0.
*
* .. note:: Returns `WALLY_EINVAL` if the proof does not verify.
*/
WALLY_CORE_API int wally_dleq_proof_verify(
const unsigned char *pub_key_a,
size_t pub_key_a_len,
const unsigned char *pub_key_b,
size_t pub_key_b_len,
const unsigned char *pub_key_c,
size_t pub_key_c_len,
const unsigned char *proof,
size_t proof_len,
const unsigned char *generator,
size_t generator_len,
const unsigned char *msg,
size_t msg_len,
uint32_t flags);

/** The length of a data committed using sign-to-contract (s2c) */
#define WALLY_S2C_DATA_LEN 32
/** The length of a sign-to-contract (s2c) opening */
Expand Down
Loading
Loading