From 1fffafa23c67fd2264a27bc5c4352e3b512197d0 Mon Sep 17 00:00:00 2001 From: Jon Griffiths Date: Sat, 11 Jan 2025 22:17:04 +1300 Subject: [PATCH] psbt: expose accessors for outputs taproot internal key --- include/wally.hpp | 6 ++++++ include/wally_psbt.h | 13 +++++++++++++ include/wally_psbt_members.h | 3 +++ src/psbt.c | 2 ++ src/swig_java/swig.i | 4 ++++ src/swig_python/contrib/psbt.py | 3 +++ src/swig_python/python_extra.py_in | 1 + src/test/util.py | 4 ++++ src/wasm_package/src/functions.js | 4 ++++ src/wasm_package/src/index.d.ts | 4 ++++ tools/wasm_exports.sh | 4 ++++ 11 files changed, 48 insertions(+) diff --git a/include/wally.hpp b/include/wally.hpp index 0e384da53..5c49cef21 100644 --- a/include/wally.hpp +++ b/include/wally.hpp @@ -1533,6 +1533,12 @@ inline int psbt_output_set_script(const OUTPUT& output, const SCRIPT& script) { return detail::check_ret(__FUNCTION__, ret); } +template +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()); + return detail::check_ret(__FUNCTION__, ret); +} + template inline int psbt_output_set_unknowns(const OUTPUT& output, const struct wally_map* map_in) { int ret = ::wally_psbt_output_set_unknowns(detail::get_p(output), map_in); diff --git a/include/wally_psbt.h b/include/wally_psbt.h index c261c2a27..18593efdf 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -1434,6 +1434,19 @@ WALLY_CORE_API int wally_psbt_output_set_script( const unsigned char *script, size_t script_len); +/** + * Set the taproot internal public key in an output. + * + * :param output: The output to update. + * :param pub_key: The x-only internal public key for this output. + * :param pub_key_len: The length of ``pub_key`` in bytes. Must be `EC_XONLY_PUBLIC_KEY_LEN`. + */ +WALLY_CORE_API int wally_psbt_output_set_taproot_internal_key( + struct wally_psbt_output *output, + const unsigned char *pub_key, + size_t pub_key_len); + + #ifndef WALLY_ABI_NO_ELEMENTS /** * Set the input blinder index in an output. diff --git a/include/wally_psbt_members.h b/include/wally_psbt_members.h index d426a8b71..6e3965f2f 100644 --- a/include/wally_psbt_members.h +++ b/include/wally_psbt_members.h @@ -186,6 +186,8 @@ WALLY_CORE_API int wally_psbt_get_output_script(const struct wally_psbt *psbt, s WALLY_CORE_API int wally_psbt_get_output_script_len(const struct wally_psbt *psbt, size_t index, size_t *written); WALLY_CORE_API int wally_psbt_get_output_amount(const struct wally_psbt *psbt, size_t index, uint64_t *value_out); WALLY_CORE_API int wally_psbt_has_output_amount(const struct wally_psbt *psbt, size_t index, size_t *written); +WALLY_CORE_API int wally_psbt_get_output_taproot_internal_key(const struct wally_psbt *psbt, size_t index, unsigned char *bytes_out, size_t len, size_t *written); +WALLY_CORE_API int wally_psbt_get_output_taproot_internal_key_len(const struct wally_psbt *psbt, size_t index, size_t *written); WALLY_CORE_API int wally_psbt_set_output_redeem_script(struct wally_psbt *psbt, size_t index, const unsigned char *script, size_t script_len); WALLY_CORE_API int wally_psbt_set_output_witness_script(struct wally_psbt *psbt, size_t index, const unsigned char *script, size_t script_len); @@ -194,6 +196,7 @@ WALLY_CORE_API int wally_psbt_set_output_unknowns(struct wally_psbt *psbt, size_ WALLY_CORE_API int wally_psbt_set_output_script(struct wally_psbt *psbt, size_t index, const unsigned char *script, size_t script_len); WALLY_CORE_API int wally_psbt_set_output_amount(struct wally_psbt *psbt, size_t index, uint64_t amount); WALLY_CORE_API int wally_psbt_clear_output_amount(struct wally_psbt *psbt, size_t index); +WALLY_CORE_API int wally_psbt_set_output_taproot_internal_key(struct wally_psbt *psbt, size_t index, const unsigned char *pub_key, size_t pub_key_len); #ifndef WALLY_ABI_NO_ELEMENTS WALLY_CORE_API int wally_psbt_get_output_blinder_index(const struct wally_psbt *psbt, size_t index, uint32_t *value_out); diff --git a/src/psbt.c b/src/psbt.c index dacdf19d2..ccc26441b 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -871,6 +871,7 @@ static int psbt_input_free(struct wally_psbt_input *input, bool free_parent) MAP_INNER_FIELD(output, redeem_script, PSBT_OUT_REDEEM_SCRIPT, psbt_fields) MAP_INNER_FIELD(output, witness_script, PSBT_OUT_WITNESS_SCRIPT, psbt_fields) +MAP_INNER_FIELD(output, taproot_internal_key, PSBT_OUT_TAP_INTERNAL_KEY, psbt_fields) SET_MAP(wally_psbt_output, keypath,) ADD_KEYPATH(wally_psbt_output) ADD_TAP_KEYPATH(wally_psbt_output) @@ -5712,6 +5713,7 @@ int wally_psbt_clear_input_required_lockheight(struct wally_psbt *psbt, size_t i if (!psbt || psbt->version != PSBT_2) return WALLY_EINVAL; return wally_psbt_input_clear_required_lockheight(psbt_get_input(psbt, index)); } +PSBT_FIELD(output, taproot_internal_key, PSBT_0) #ifndef WALLY_ABI_NO_ELEMENTS PSBT_GET_I_PSET(input, amount, uint64_t, PSBT_2) diff --git a/src/swig_java/swig.i b/src/swig_java/swig.i index c4644fa9e..f9b45595a 100644 --- a/src/swig_java/swig.i +++ b/src/swig_java/swig.i @@ -845,6 +845,8 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %returns_size_t(wally_psbt_get_output_redeem_script_len); %returns_size_t(wally_psbt_get_output_script); %returns_size_t(wally_psbt_get_output_script_len); +%returns_size_t(wally_psbt_get_output_taproot_internal_key); +%returns_size_t(wally_psbt_get_output_taproot_internal_key_len); %returns_size_t(wally_psbt_get_output_unknown); %returns_size_t(wally_psbt_get_output_unknown_len); %returns_size_t(wally_psbt_get_output_unknowns_size); @@ -934,6 +936,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %returns_void__(wally_psbt_set_output_keypaths); %returns_void__(wally_psbt_set_output_redeem_script); %returns_void__(wally_psbt_set_output_script); +%returns_void__(wally_psbt_set_output_taproot_internal_key); %returns_void__(wally_psbt_set_output_unknowns); %returns_void__(wally_psbt_set_output_value_blinding_rangeproof); %returns_void__(wally_psbt_set_output_value_commitment); @@ -1352,6 +1355,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %ignore wally_psbt_output_set_asset_blinding_surjectionproof; %ignore wally_psbt_output_clear_asset_blinding_surjectionproof; %ignore wally_psbt_output_get_blinding_status; +%ignore wally_psbt_output_set_taproot_internal_key; %include "../include/wally_core.h" %include "../include/wally_address.h" diff --git a/src/swig_python/contrib/psbt.py b/src/swig_python/contrib/psbt.py index 0c9755b17..3b8d669d4 100644 --- a/src/swig_python/contrib/psbt.py +++ b/src/swig_python/contrib/psbt.py @@ -655,6 +655,9 @@ def test_psbt(self): psbt_get_output_unknown, psbt_find_output_unknown, p, dummy_unknowns, dummy_unknown_key) + self._try_get_set_b(psbt_set_output_taproot_internal_key, + psbt_get_output_taproot_internal_key, + None, psbt, dummy_tap_internal_key) # # Outputs: PSBT V2 diff --git a/src/swig_python/python_extra.py_in b/src/swig_python/python_extra.py_in index ef8184807..a8cf554b7 100644 --- a/src/swig_python/python_extra.py_in +++ b/src/swig_python/python_extra.py_in @@ -206,6 +206,7 @@ psbt_get_input_witness_utxo = psbt_get_input_witness_utxo_alloc psbt_get_output_keypath = _wrap_bin(psbt_get_output_keypath, psbt_get_output_keypath_len) psbt_get_output_redeem_script = _wrap_bin(psbt_get_output_redeem_script, psbt_get_output_redeem_script_len) psbt_get_output_script = _wrap_bin(psbt_get_output_script, psbt_get_output_script_len) +psbt_get_output_taproot_internal_key = _wrap_bin(psbt_get_output_taproot_internal_key, psbt_get_output_taproot_internal_key_len) psbt_get_output_unknown = _wrap_bin(psbt_get_output_unknown, psbt_get_output_unknown_len) psbt_get_output_witness_script = _wrap_bin(psbt_get_output_witness_script, psbt_get_output_witness_script_len) psbt_init = psbt_init_alloc diff --git a/src/test/util.py b/src/test/util.py index 5ee5f5d8e..5b37098c3 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -604,6 +604,7 @@ class wally_psbt(Structure): ('wally_psbt_output_set_keypaths', c_int, [POINTER(wally_psbt_output), POINTER(wally_map)]), ('wally_psbt_output_set_redeem_script', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]), ('wally_psbt_output_set_script', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]), + ('wally_psbt_output_set_taproot_internal_key', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]), ('wally_psbt_output_set_unknowns', c_int, [POINTER(wally_psbt_output), POINTER(wally_map)]), ('wally_psbt_output_set_value_blinding_rangeproof', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]), ('wally_psbt_output_set_value_commitment', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t]), @@ -877,6 +878,8 @@ class wally_psbt(Structure): ('wally_psbt_get_output_redeem_script_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]), ('wally_psbt_get_output_script', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t, c_size_t_p]), ('wally_psbt_get_output_script_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]), + ('wally_psbt_get_output_taproot_internal_key', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t, c_size_t_p]), + ('wally_psbt_get_output_taproot_internal_key_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]), ('wally_psbt_get_output_unknown', c_int, [POINTER(wally_psbt), c_size_t, c_size_t, c_void_p, c_size_t, c_size_t_p]), ('wally_psbt_get_output_unknown_len', c_int, [POINTER(wally_psbt), c_size_t, c_size_t, c_size_t_p]), ('wally_psbt_get_output_unknowns_size', c_int, [POINTER(wally_psbt), c_size_t, c_size_t_p]), @@ -944,6 +947,7 @@ class wally_psbt(Structure): ('wally_psbt_set_output_keypaths', c_int, [POINTER(wally_psbt), c_size_t, POINTER(wally_map)]), ('wally_psbt_set_output_redeem_script', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t]), ('wally_psbt_set_output_script', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t]), + ('wally_psbt_set_output_taproot_internal_key', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t]), ('wally_psbt_set_output_unknowns', c_int, [POINTER(wally_psbt), c_size_t, POINTER(wally_map)]), ('wally_psbt_set_output_value_blinding_rangeproof', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t]), ('wally_psbt_set_output_value_commitment', c_int, [POINTER(wally_psbt), c_size_t, c_void_p, c_size_t]), diff --git a/src/wasm_package/src/functions.js b/src/wasm_package/src/functions.js index b15ac488f..ba4b54a09 100644 --- a/src/wasm_package/src/functions.js +++ b/src/wasm_package/src/functions.js @@ -389,6 +389,7 @@ export const psbt_get_output_keypath_len = wrap('wally_psbt_get_output_keypath_l export const psbt_get_output_keypaths_size = wrap('wally_psbt_get_output_keypaths_size', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); export const psbt_get_output_redeem_script_len = wrap('wally_psbt_get_output_redeem_script_len', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); export const psbt_get_output_script_len = wrap('wally_psbt_get_output_script_len', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); +export const psbt_get_output_taproot_internal_key_len = wrap('wally_psbt_get_output_taproot_internal_key_len', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); export const psbt_get_output_unknown_len = wrap('wally_psbt_get_output_unknown_len', [T.OpaqueRef, T.Int32, T.Int32, T.DestPtr(T.Int32)]); export const psbt_get_output_unknowns_size = wrap('wally_psbt_get_output_unknowns_size', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); export const psbt_get_output_value_blinding_rangeproof_len = wrap('wally_psbt_get_output_value_blinding_rangeproof_len', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); @@ -523,6 +524,7 @@ export const psbt_output_set_ecdh_public_key = wrap('wally_psbt_output_set_ecdh_ export const psbt_output_set_keypaths = wrap('wally_psbt_output_set_keypaths', [T.OpaqueRef, T.OpaqueRef]); export const psbt_output_set_redeem_script = wrap('wally_psbt_output_set_redeem_script', [T.OpaqueRef, T.Bytes]); export const psbt_output_set_script = wrap('wally_psbt_output_set_script', [T.OpaqueRef, T.Bytes]); +export const psbt_output_set_taproot_internal_key = wrap('wally_psbt_output_set_taproot_internal_key', [T.OpaqueRef, T.Bytes]); export const psbt_output_set_unknowns = wrap('wally_psbt_output_set_unknowns', [T.OpaqueRef, T.OpaqueRef]); export const psbt_output_set_value_blinding_rangeproof = wrap('wally_psbt_output_set_value_blinding_rangeproof', [T.OpaqueRef, T.Bytes]); export const psbt_output_set_value_commitment = wrap('wally_psbt_output_set_value_commitment', [T.OpaqueRef, T.Bytes]); @@ -582,6 +584,7 @@ export const psbt_set_output_ecdh_public_key = wrap('wally_psbt_set_output_ecdh_ export const psbt_set_output_keypaths = wrap('wally_psbt_set_output_keypaths', [T.OpaqueRef, T.Int32, T.OpaqueRef]); export const psbt_set_output_redeem_script = wrap('wally_psbt_set_output_redeem_script', [T.OpaqueRef, T.Int32, T.Bytes]); export const psbt_set_output_script = wrap('wally_psbt_set_output_script', [T.OpaqueRef, T.Int32, T.Bytes]); +export const psbt_set_output_taproot_internal_key = wrap('wally_psbt_set_output_taproot_internal_key', [T.OpaqueRef, T.Int32, T.Bytes]); export const psbt_set_output_unknowns = wrap('wally_psbt_set_output_unknowns', [T.OpaqueRef, T.Int32, T.OpaqueRef]); export const psbt_set_output_value_blinding_rangeproof = wrap('wally_psbt_set_output_value_blinding_rangeproof', [T.OpaqueRef, T.Int32, T.Bytes]); export const psbt_set_output_value_commitment = wrap('wally_psbt_set_output_value_commitment', [T.OpaqueRef, T.Int32, T.Bytes]); @@ -831,6 +834,7 @@ export const psbt_get_output_ecdh_public_key = wrap('wally_psbt_get_output_ecdh_ export const psbt_get_output_keypath = wrap('wally_psbt_get_output_keypath', [T.OpaqueRef, T.Int32, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_keypath_len, false)]); export const psbt_get_output_redeem_script = wrap('wally_psbt_get_output_redeem_script', [T.OpaqueRef, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_redeem_script_len, false)]); export const psbt_get_output_script = wrap('wally_psbt_get_output_script', [T.OpaqueRef, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_script_len, false)]); +export const psbt_get_output_taproot_internal_key = wrap('wally_psbt_get_output_taproot_internal_key', [T.OpaqueRef, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_taproot_internal_key_len, false)]); export const psbt_get_output_unknown = wrap('wally_psbt_get_output_unknown', [T.OpaqueRef, T.Int32, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_unknown_len, false)]); export const psbt_get_output_value_blinding_rangeproof = wrap('wally_psbt_get_output_value_blinding_rangeproof', [T.OpaqueRef, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_value_blinding_rangeproof_len, false)]); export const psbt_get_output_value_commitment = wrap('wally_psbt_get_output_value_commitment', [T.OpaqueRef, T.Int32, T.DestPtrVarLen(T.Bytes, psbt_get_output_value_commitment_len, false)]); diff --git a/src/wasm_package/src/index.d.ts b/src/wasm_package/src/index.d.ts index 669005f1f..44e54addc 100644 --- a/src/wasm_package/src/index.d.ts +++ b/src/wasm_package/src/index.d.ts @@ -349,6 +349,7 @@ export function psbt_get_output_keypath_len(psbt: Ref_wally_psbt, index: number, export function psbt_get_output_keypaths_size(psbt: Ref_wally_psbt, index: number): number; export function psbt_get_output_redeem_script_len(psbt: Ref_wally_psbt, index: number): number; export function psbt_get_output_script_len(psbt: Ref_wally_psbt, index: number): number; +export function psbt_get_output_taproot_internal_key_len(psbt: Ref_wally_psbt, index: number): number; export function psbt_get_output_unknown_len(psbt: Ref_wally_psbt, index: number, subindex: number): number; export function psbt_get_output_unknowns_size(psbt: Ref_wally_psbt, index: number): number; export function psbt_get_output_value_blinding_rangeproof_len(psbt: Ref_wally_psbt, index: number): number; @@ -483,6 +484,7 @@ export function psbt_output_set_ecdh_public_key(output: Ref_wally_psbt_output, p export function psbt_output_set_keypaths(output: Ref_wally_psbt_output, map_in: Ref_wally_map): void; export function psbt_output_set_redeem_script(output: Ref_wally_psbt_output, script: Buffer|Uint8Array): void; export function psbt_output_set_script(output: Ref_wally_psbt_output, script: Buffer|Uint8Array): void; +export function psbt_output_set_taproot_internal_key(output: Ref_wally_psbt_output, pub_key: Buffer|Uint8Array): void; export function psbt_output_set_unknowns(output: Ref_wally_psbt_output, map_in: Ref_wally_map): void; export function psbt_output_set_value_blinding_rangeproof(output: Ref_wally_psbt_output, rangeproof: Buffer|Uint8Array): void; export function psbt_output_set_value_commitment(output: Ref_wally_psbt_output, commitment: Buffer|Uint8Array): void; @@ -542,6 +544,7 @@ export function psbt_set_output_ecdh_public_key(psbt: Ref_wally_psbt, index: num export function psbt_set_output_keypaths(psbt: Ref_wally_psbt, index: number, map_in: Ref_wally_map): void; export function psbt_set_output_redeem_script(psbt: Ref_wally_psbt, index: number, script: Buffer|Uint8Array): void; export function psbt_set_output_script(psbt: Ref_wally_psbt, index: number, script: Buffer|Uint8Array): void; +export function psbt_set_output_taproot_internal_key(psbt: Ref_wally_psbt, index: number, pub_key: Buffer|Uint8Array): void; export function psbt_set_output_unknowns(psbt: Ref_wally_psbt, index: number, map_in: Ref_wally_map): void; export function psbt_set_output_value_blinding_rangeproof(psbt: Ref_wally_psbt, index: number, rangeproof: Buffer|Uint8Array): void; export function psbt_set_output_value_commitment(psbt: Ref_wally_psbt, index: number, commitment: Buffer|Uint8Array): void; @@ -791,6 +794,7 @@ export function psbt_get_output_ecdh_public_key(psbt: Ref_wally_psbt, index: num export function psbt_get_output_keypath(psbt: Ref_wally_psbt, index: number, subindex: number): Buffer; export function psbt_get_output_redeem_script(psbt: Ref_wally_psbt, index: number): Buffer; export function psbt_get_output_script(psbt: Ref_wally_psbt, index: number): Buffer; +export function psbt_get_output_taproot_internal_key(psbt: Ref_wally_psbt, index: number): Buffer; export function psbt_get_output_unknown(psbt: Ref_wally_psbt, index: number, subindex: number): Buffer; export function psbt_get_output_value_blinding_rangeproof(psbt: Ref_wally_psbt, index: number): Buffer; export function psbt_get_output_value_commitment(psbt: Ref_wally_psbt, index: number): Buffer; diff --git a/tools/wasm_exports.sh b/tools/wasm_exports.sh index 853fc6dd0..afcab56d9 100644 --- a/tools/wasm_exports.sh +++ b/tools/wasm_exports.sh @@ -265,6 +265,8 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_psbt_get_output_redeem_script_len' \ ,'_wally_psbt_get_output_script' \ ,'_wally_psbt_get_output_script_len' \ +,'_wally_psbt_get_output_taproot_internal_key' \ +,'_wally_psbt_get_output_taproot_internal_key_len' \ ,'_wally_psbt_get_output_unknown' \ ,'_wally_psbt_get_output_unknown_len' \ ,'_wally_psbt_get_output_unknowns_size' \ @@ -317,6 +319,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_psbt_output_set_keypaths' \ ,'_wally_psbt_output_set_redeem_script' \ ,'_wally_psbt_output_set_script' \ +,'_wally_psbt_output_set_taproot_internal_key' \ ,'_wally_psbt_output_set_unknowns' \ ,'_wally_psbt_output_set_witness_script' \ ,'_wally_psbt_output_taproot_keypath_add' \ @@ -346,6 +349,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_psbt_set_output_keypaths' \ ,'_wally_psbt_set_output_redeem_script' \ ,'_wally_psbt_set_output_script' \ +,'_wally_psbt_set_output_taproot_internal_key' \ ,'_wally_psbt_set_output_unknowns' \ ,'_wally_psbt_set_output_witness_script' \ ,'_wally_psbt_set_tx_modifiable_flags' \