From ef245548e1d02087c99a2b6343065b6c70331176 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 12:56:39 +0900 Subject: [PATCH 01/11] addr: Add BIP-352 silent payment addresses A silent payment address is bech32m with an sp or tsp prefix, a version, and the receiver's scan and spend public keys. It may be up to 1023 characters, so it cannot use the 90 character segwit address code. Add wally_addr_silent_payment_from_bytes(), which writes a version 0 address, and wally_addr_silent_payment_to_bytes(), which returns the two keys. As BIP-352 requires, decoding accepts versions 1 to 30 and keeps only their first 66 bytes, rejects version 31, and rejects a version 0 address carrying extra data. Both keys must be valid points. The tests use every recipient in the BIP-352 vectors. --- include/wally.hpp | 12 ++++ include/wally_address.h | 39 +++++++++++ src/bech32.c | 92 +++++++++++++++++++++++++ src/swig_java/swig.i | 2 + src/swig_python/python_extra.py_in | 1 + src/test/test_bech32.py | 104 +++++++++++++++++++++++++++++ src/test/util.py | 2 + src/wasm_package/src/const.js | 1 + src/wasm_package/src/functions.js | 2 + src/wasm_package/src/index.d.ts | 2 + tools/wasm_exports.sh | 2 + 11 files changed, 259 insertions(+) diff --git a/include/wally.hpp b/include/wally.hpp index 49ddc1de4..993475d23 100644 --- a/include/wally.hpp +++ b/include/wally.hpp @@ -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 +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 +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 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); diff --git a/include/wally_address.h b/include/wally_address.h index 2df708160..d687aff88 100644 --- a/include/wally_address.h +++ b/include/wally_address.h @@ -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. * @@ -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. * diff --git a/src/bech32.c b/src/bech32.c index 4b1a5dce1..f087dbdb5 100644 --- a/src/bech32.c +++ b/src/bech32.c @@ -288,3 +288,95 @@ int wally_addr_segwit_get_version(const char *addr, const char *addr_family, addr_family, addr_family ? strlen(addr_family) : 0, flags, written); } + +/* BIP-352 silent payment addresses */ +#define SP_ADDR_MAX_LEN 1023 /* Maximum encoded length */ +#define SP_ADDR_VERSION_MAX 30 /* Highest version a sender may pay */ +#define SP_ADDR_DATA_LEN 107 /* Version + 66 bytes as 5 bit groups */ + +static bool sp_keys_verify(const unsigned char *keys) +{ + return wally_ec_public_key_verify(keys, EC_PUBLIC_KEY_LEN) == WALLY_OK && + wally_ec_public_key_verify(keys + EC_PUBLIC_KEY_LEN, EC_PUBLIC_KEY_LEN) == WALLY_OK; +} + +int wally_addr_silent_payment_from_bytes(const unsigned char *bytes, size_t bytes_len, + const char *addr_family, uint32_t flags, + char **output) +{ + uint8_t data[SP_ADDR_DATA_LEN]; + size_t data_len = 0; + size_t addr_family_len = addr_family ? strlen(addr_family) : 0; + char *result; + int ret = WALLY_OK; + + if (output) + *output = NULL; + + if (!bytes || bytes_len != WALLY_SILENT_PAYMENT_KEYS_LEN || !addr_family_len || + flags || !output || !sp_keys_verify(bytes)) + return WALLY_EINVAL; + + /* hrp, separator, data, checksum and NUL */ + result = wally_malloc(addr_family_len + 1 + SP_ADDR_DATA_LEN + 6 + 1); + if (!result) + return WALLY_ENOMEM; + + data[0] = 0; /* Version 0 */ + convert_bits(data + 1, &data_len, 5, bytes, bytes_len, 8, 1); + if (!bech32_encode(result, addr_family, addr_family_len, + data, data_len + 1, SP_ADDR_MAX_LEN, true)) { + wally_free(result); + ret = WALLY_EINVAL; + } else + *output = result; + + wally_clear(data, sizeof(data)); + return ret; +} + +int wally_addr_silent_payment_to_bytes(const char *addr, const char *addr_family, + uint32_t flags, + unsigned char *bytes_out, size_t len) +{ + size_t addr_len = addr ? strlen(addr) : 0; + size_t addr_family_len = addr_family ? strlen(addr_family) : 0; + size_t data_len, payload_len = 0; + uint8_t *data = NULL, *payload = NULL; + char *hrp = NULL; + bool is_bech32m = false; + int ret = WALLY_EINVAL; + + if (!addr_len || addr_len > SP_ADDR_MAX_LEN || !addr_family_len || + flags || !bytes_out || len != WALLY_SILENT_PAYMENT_KEYS_LEN) + return WALLY_EINVAL; + + data = wally_malloc(addr_len); + hrp = wally_malloc(addr_len); + payload = wally_malloc(addr_len); + if (!data || !hrp || !payload) { + ret = WALLY_ENOMEM; + goto done; + } + + if (!bech32_decode(hrp, data, &data_len, addr, addr_len, SP_ADDR_MAX_LEN, &is_bech32m) || + !is_bech32m || !data_len || data[0] > SP_ADDR_VERSION_MAX || + strlen(hrp) != addr_family_len || memcmp(hrp, addr_family, addr_family_len) || + !convert_bits(payload, &payload_len, 8, data + 1, data_len - 1, 5, 0)) + goto done; + + /* Version 0 is exactly the two keys; later versions may append data */ + if (payload_len < WALLY_SILENT_PAYMENT_KEYS_LEN || + (data[0] == 0 && payload_len != WALLY_SILENT_PAYMENT_KEYS_LEN) || + !sp_keys_verify(payload)) + goto done; + + memcpy(bytes_out, payload, WALLY_SILENT_PAYMENT_KEYS_LEN); + ret = WALLY_OK; + +done: + clear_and_free(data, addr_len); + clear_and_free(hrp, addr_len); + clear_and_free(payload, addr_len); + return ret; +} diff --git a/src/swig_java/swig.i b/src/swig_java/swig.i index d0a901f3c..2728c3978 100644 --- a/src/swig_java/swig.i +++ b/src/swig_java/swig.i @@ -531,6 +531,8 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %returns_size_t(wally_addr_segwit_n_get_version); %returns_size_t(wally_addr_segwit_n_to_bytes); %returns_size_t(wally_addr_segwit_to_bytes); +%returns_string(wally_addr_silent_payment_from_bytes); +%returns_array_(wally_addr_silent_payment_to_bytes, 4, 5, WALLY_SILENT_PAYMENT_KEYS_LEN); %returns_size_t(wally_address_to_scriptpubkey); %returns_array_(wally_aes, 6, 7, AES_BLOCK_LEN); %returns_size_t(wally_aes_len); diff --git a/src/swig_python/python_extra.py_in b/src/swig_python/python_extra.py_in index b4fe37311..8c33672bd 100644 --- a/src/swig_python/python_extra.py_in +++ b/src/swig_python/python_extra.py_in @@ -113,6 +113,7 @@ WALLY_TX_SEQUENCE_FINAL = 0xffffffff # BEGIN AUTOGENERATED addr_segwit_n_to_bytes = _wrap_bin(addr_segwit_n_to_bytes, WALLY_SEGWIT_ADDRESS_PUBKEY_MAX_LEN, resize=True) addr_segwit_to_bytes = _wrap_bin(addr_segwit_to_bytes, WALLY_SEGWIT_ADDRESS_PUBKEY_MAX_LEN, resize=True) +addr_silent_payment_to_bytes = _wrap_bin(addr_silent_payment_to_bytes, WALLY_SILENT_PAYMENT_KEYS_LEN) address_to_scriptpubkey = _wrap_bin(address_to_scriptpubkey, WALLY_ADDRESS_PUBKEY_MAX_LEN, resize=True) ae_host_commit_from_bytes = _wrap_bin(ae_host_commit_from_bytes, WALLY_HOST_COMMITMENT_LEN) ae_sig_from_bytes = _wrap_bin(ae_sig_from_bytes, EC_SIGNATURE_LEN) diff --git a/src/test/test_bech32.py b/src/test/test_bech32.py index db8d556c6..1480e5106 100755 --- a/src/test/test_bech32.py +++ b/src/test/test_bech32.py @@ -1,7 +1,9 @@ +import json import unittest from util import * WALLY_WITNESSSCRIPT_MAX_LEN = 42 +WALLY_SILENT_PAYMENT_KEYS_LEN = 66 valid_cases = { # https://github.com/bitcoin/bips/blob/master/bip-0350.mediawiki @@ -156,5 +158,107 @@ def test_segwit_address(self): ret, ver = wally_addr_segwit_n_get_version(addr, len(addr), family, 0, 0) self.assertEqual(ret, WALLY_EINVAL) # Bad family length + def sp_encode(self, hrp, version, payload, bech32m=True): + """Encode a silent payment address without validating it""" + charset = 'qpzry9x8gf2tvdw0s3jn54khce6mua7l' + def polymod(values): + gen = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3] + chk = 1 + for v in values: + top = chk >> 25 + chk = (chk & 0x1ffffff) << 5 ^ v + for i in range(5): + chk ^= gen[i] if ((top >> i) & 1) else 0 + return chk + acc, bits, data = 0, 0, [version] + for b in payload: + acc, bits = (acc << 8) | b, bits + 8 + while bits >= 5: + bits -= 5 + data.append((acc >> bits) & 31) + if bits: + data.append((acc << (5 - bits)) & 31) + expanded = [ord(c) >> 5 for c in hrp] + [0] + [ord(c) & 31 for c in hrp] + const = 0x2bc830a3 if bech32m else 1 + chk = polymod(expanded + data + [0] * 6) ^ const + data += [(chk >> 5 * (5 - i)) & 31 for i in range(6)] + return hrp + '1' + ''.join(charset[d] for d in data) + + def sp_decode(self, addr, family): + out, out_len = make_cbuffer('00' * WALLY_SILENT_PAYMENT_KEYS_LEN) + ret = wally_addr_silent_payment_to_bytes(utf8(addr), utf8(family), 0, out, out_len) + return ret, h(out) if ret == WALLY_OK else None + + def test_silent_payment_address(self): + """Tests for encoding and decoding BIP-352 silent payment addresses""" + vectors = root_dir + 'src/secp256k1/src/modules/silentpayments/' \ + 'bip352_send_and_receive_test_vectors.json' + with open(vectors, 'r') as f: + cases = json.load(f) + recipients = [r for case in cases for s in case['sending'] + for r in s['given']['recipients']] + self.assertTrue(len(recipients) > 0) + + for r in recipients: + addr, keys = r['address'], r['scan_pub_key'] + r['spend_pub_key'] + self.assertEqual(len(addr), 116) # Mainnet, version 0 + for a in [addr, addr.upper()]: + self.assertEqual(self.sp_decode(a, 'sp'), (WALLY_OK, utf8(keys))) + keys_buf, keys_len = make_cbuffer(keys) + ret, encoded = wally_addr_silent_payment_from_bytes(keys_buf, keys_len, utf8('sp'), 0) + self.assertEqual((ret, encoded), (WALLY_OK, addr)) + + # Testnet addresses are one character longer + keys = recipients[0]['scan_pub_key'] + recipients[0]['spend_pub_key'] + keys_buf, keys_len = make_cbuffer(keys) + ret, encoded = wally_addr_silent_payment_from_bytes(keys_buf, keys_len, utf8('tsp'), 0) + self.assertEqual((ret, len(encoded)), (WALLY_OK, 117)) + self.assertEqual(self.sp_decode(encoded, 'tsp'), (WALLY_OK, utf8(keys))) + self.assertEqual(self.sp_decode(encoded, 'sp'), (WALLY_EINVAL, None)) + + # Later versions may append data, which is ignored + payload = bytes.fromhex(keys) + for version in [1, 30]: + addr = self.sp_encode('sp', version, payload + b'\x00' * 10) + self.assertEqual(self.sp_decode(addr, 'sp'), (WALLY_OK, utf8(keys))) + + bad_key = '04' + keys[2:] + for addr in [ + self.sp_encode('sp', 0, payload + b'\x00'), # Version 0 with extra data + self.sp_encode('sp', 0, payload[:-1]), # Version 0 too short + self.sp_encode('sp', 1, payload[:-1]), # Later version too short + self.sp_encode('sp', 31, payload), # Version 31 is invalid + self.sp_encode('sp', 0, payload, False), # Bech32 checksum + self.sp_encode('sp', 0, bytes.fromhex(bad_key)), # Invalid scan key + self.sp_encode('sp', 1, payload + b'\x00' * 640), # Too long + recipients[0]['address'][:-1] + 'q', # Bad checksum + recipients[0]['address'][:4] + recipients[0]['address'][4:].upper(), # Mixed case + ]: + self.assertEqual(self.sp_decode(addr, 'sp'), (WALLY_EINVAL, None)) + + # Invalid arguments + addr = recipients[0]['address'] + out, out_len = make_cbuffer('00' * WALLY_SILENT_PAYMENT_KEYS_LEN) + for args in [ + (None, utf8('sp'), 0, out, out_len), # Missing address + (utf8(addr), None, 0, out, out_len), # Missing family + (utf8(addr), utf8(''), 0, out, out_len), # Empty family + (utf8(addr), utf8('sp'), 1, out, out_len), # Unknown flags + (utf8(addr), utf8('sp'), 0, None, out_len), # Missing output + (utf8(addr), utf8('sp'), 0, out, out_len - 1), # Short output + ]: + self.assertEqual(wally_addr_silent_payment_to_bytes(*args), WALLY_EINVAL) + + bad_buf, bad_len = make_cbuffer(bad_key) + for args in [ + (None, keys_len, utf8('sp'), 0), # Missing keys + (keys_buf, keys_len - 1, utf8('sp'), 0), # Short keys + (bad_buf, bad_len, utf8('sp'), 0), # Invalid scan key + (keys_buf, keys_len, None, 0), # Missing family + (keys_buf, keys_len, utf8('SP'), 0), # Uppercase family + (keys_buf, keys_len, utf8('sp'), 1), # Unknown flags + ]: + self.assertEqual(wally_addr_silent_payment_from_bytes(*args), (WALLY_EINVAL, None)) + if __name__ == '__main__': unittest.main() diff --git a/src/test/util.py b/src/test/util.py index 0a35e3995..c7754f8bf 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -265,6 +265,8 @@ class wally_psbt(Structure): ('wally_addr_segwit_n_get_version', c_int, [c_char_p, c_size_t, c_char_p, c_size_t, c_uint32, c_size_t_p]), ('wally_addr_segwit_n_to_bytes', c_int, [c_char_p, c_size_t, c_char_p, c_size_t, c_uint32, c_void_p, c_size_t, c_size_t_p]), ('wally_addr_segwit_to_bytes', c_int, [c_char_p, c_char_p, c_uint32, c_void_p, c_size_t, c_size_t_p]), + ('wally_addr_silent_payment_from_bytes', c_int, [c_void_p, c_size_t, c_char_p, c_uint32, c_char_p_p]), + ('wally_addr_silent_payment_to_bytes', c_int, [c_char_p, c_char_p, c_uint32, c_void_p, c_size_t]), ('wally_address_to_scriptpubkey', c_int, [c_char_p, c_uint32, c_void_p, c_size_t, c_size_t_p]), ('wally_ae_host_commit_from_bytes', c_int, [c_void_p, c_size_t, c_uint32, c_void_p, c_size_t]), ('wally_ae_sig_from_bytes', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_void_p, c_size_t]), diff --git a/src/wasm_package/src/const.js b/src/wasm_package/src/const.js index 444bf220d..6c4337f80 100755 --- a/src/wasm_package/src/const.js +++ b/src/wasm_package/src/const.js @@ -234,6 +234,7 @@ export const WALLY_SIGTYPE_MASK = 0xf; /* Mask for signature type in flags */ export const WALLY_SIGTYPE_PRE_SW = 0x1; /* Pre-segwit signature hash */ export const WALLY_SIGTYPE_SW_V0 = 0x2; /* Segwit v0 signature hash */ export const WALLY_SIGTYPE_SW_V1 = 0x3; /* Segwit v1 (taproot) signature hash */ +export const WALLY_SILENT_PAYMENT_KEYS_LEN = 66; /** BIP-352 scan public key followed by spend public key */ export const WALLY_TXHASH_LEN = 32; /** Size of a transaction hash in bytes */ export const WALLY_TX_ASSET_CT_ASSET_LEN = 33; /* version byte + 32 bytes */ export const WALLY_TX_ASSET_CT_ASSET_PREFIX_A = 0x0a; diff --git a/src/wasm_package/src/functions.js b/src/wasm_package/src/functions.js index 7b6436699..f2f6299b5 100644 --- a/src/wasm_package/src/functions.js +++ b/src/wasm_package/src/functions.js @@ -65,6 +65,8 @@ export const addr_segwit_get_version = wrap('wally_addr_segwit_get_version', [T. export const addr_segwit_n_get_version = wrap('wally_addr_segwit_n_get_version', [T.String, T.Int32, T.String, T.Int32, T.Int32, T.DestPtr(T.Int32)]); export const addr_segwit_n_to_bytes = wrap('wally_addr_segwit_n_to_bytes', [T.String, T.Int32, T.String, T.Int32, T.Int32, T.DestPtrVarLen(T.Bytes, C.WALLY_SEGWIT_ADDRESS_PUBKEY_MAX_LEN, true)]); export const addr_segwit_to_bytes = wrap('wally_addr_segwit_to_bytes', [T.String, T.String, T.Int32, T.DestPtrVarLen(T.Bytes, C.WALLY_SEGWIT_ADDRESS_PUBKEY_MAX_LEN, true)]); +export const addr_silent_payment_from_bytes = wrap('wally_addr_silent_payment_from_bytes', [T.Bytes, T.String, T.Int32, T.DestPtrPtr(T.String)]); +export const addr_silent_payment_to_bytes = wrap('wally_addr_silent_payment_to_bytes', [T.String, T.String, T.Int32, T.DestPtrSized(T.Bytes, C.WALLY_SILENT_PAYMENT_KEYS_LEN)]); export const address_to_scriptpubkey = wrap('wally_address_to_scriptpubkey', [T.String, T.Int32, T.DestPtrVarLen(T.Bytes, C.WALLY_ADDRESS_PUBKEY_MAX_LEN, true)]); export const ae_host_commit_from_bytes = wrap('wally_ae_host_commit_from_bytes', [T.Bytes, T.Int32, T.DestPtrSized(T.Bytes, C.WALLY_HOST_COMMITMENT_LEN)]); export const ae_sig_from_bytes = wrap('wally_ae_sig_from_bytes', [T.Bytes, T.Bytes, T.Bytes, T.Int32, T.DestPtrSized(T.Bytes, C.EC_SIGNATURE_LEN)]); diff --git a/src/wasm_package/src/index.d.ts b/src/wasm_package/src/index.d.ts index 9194455e5..c92c1a12f 100644 --- a/src/wasm_package/src/index.d.ts +++ b/src/wasm_package/src/index.d.ts @@ -25,6 +25,8 @@ export function addr_segwit_get_version(addr: string, addr_family: string, flags export function addr_segwit_n_get_version(addr: string, addr_len: number, addr_family: string, addr_family_len: number, flags: number): number; export function addr_segwit_n_to_bytes(addr: string, addr_len: number, addr_family: string, addr_family_len: number, flags: number): Buffer; export function addr_segwit_to_bytes(addr: string, addr_family: string, flags: number): Buffer; +export function addr_silent_payment_from_bytes(bytes: Buffer|Uint8Array|null, addr_family: string, flags: number): string; +export function addr_silent_payment_to_bytes(addr: string, addr_family: string, flags: number): Buffer; export function address_to_scriptpubkey(addr: string, network: number): Buffer; export function ae_host_commit_from_bytes(entropy: Buffer|Uint8Array|null, flags: number): Buffer; export function ae_sig_from_bytes(priv_key: Buffer|Uint8Array|null, bytes: Buffer|Uint8Array|null, entropy: Buffer|Uint8Array|null, flags: number): Buffer; diff --git a/tools/wasm_exports.sh b/tools/wasm_exports.sh index f5e8af147..759b53106 100644 --- a/tools/wasm_exports.sh +++ b/tools/wasm_exports.sh @@ -59,6 +59,8 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_addr_segwit_n_get_version' \ ,'_wally_addr_segwit_n_to_bytes' \ ,'_wally_addr_segwit_to_bytes' \ +,'_wally_addr_silent_payment_from_bytes' \ +,'_wally_addr_silent_payment_to_bytes' \ ,'_wally_address_to_scriptpubkey' \ ,'_wally_ae_host_commit_from_bytes' \ ,'_wally_ae_sig_from_bytes' \ From 990e5d82afe65fd262413b95d0f40ae2a2e9225f Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:09:50 +0900 Subject: [PATCH 02/11] psbt: Parse BIP-375 silent payment outputs A BIP-375 output to a silent payment address carries the receiver's scan and spend public keys in PSBT_OUT_SP_V0_INFO, with an optional PSBT_OUT_SP_V0_LABEL used to verify change. Its PSBT_OUT_SCRIPT is left out until a signer computes it from the inputs' private keys. Parse, validate and serialize both fields for PSBTv2, and reject them in v0, including when converting a PSBT with a code to v0. Allow a v2 output without a script only when it has a silent payment code, and reject a label without one. A transaction cannot be built while any silent payment script is missing, so signing and extraction fail until the signer has filled them in. Add accessors for the silent payment code. The BIP-373 MuSig2 output field 0x08 sits inside the new range and is still kept as an unknown field. Tests use the BIP-375 vectors from bitcoin/bips commit a34f044b. --- include/wally.hpp | 23 + include/wally_psbt.h | 53 + src/data/bip375_vectors.json | 1804 ++++++++++++++++++++++++++++ src/psbt.c | 65 +- src/psbt_io.h | 13 +- src/swig_java/swig.i | 5 + src/swig_python/python_extra.py_in | 1 + src/swig_python/swig.i | 1 + src/test/test_psbt.py | 135 +++ 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 + 13 files changed, 2107 insertions(+), 9 deletions(-) create mode 100644 src/data/bip375_vectors.json diff --git a/include/wally.hpp b/include/wally.hpp index 993475d23..ea64fe600 100644 --- a/include/wally.hpp +++ b/include/wally.hpp @@ -1635,6 +1635,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 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); @@ -1647,6 +1652,18 @@ inline int psbt_output_find_unknown(const OUTPUT& output, const KEY& key, size_t return detail::check_ret(__FUNCTION__, ret); } +template +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 +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 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()); @@ -1677,6 +1694,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_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 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()); diff --git a/include/wally_psbt.h b/include/wally_psbt.h index d4e83281d..08f41c7d2 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -1451,6 +1451,56 @@ WALLY_CORE_API int wally_psbt_output_set_taproot_internal_key( const unsigned char *pub_key, size_t pub_key_len); +/** + * Get the BIP-375 silent payment code from an output. + * + * :param output: The output to get from. + * :param bytes_out: Destination for the scan public key followed by the spend public key. + * :param len: Size of ``bytes_out`` in bytes. + * :param written: Destination for the number of bytes written + *| to ``bytes_out``. Will be zero if the value is not present. + * + * .. note:: This operates on the PSBT field ``PSBT_OUT_SP_V0_INFO``. + */ +WALLY_CORE_API int wally_psbt_output_get_sp_v0_info( + const struct wally_psbt_output *output, + unsigned char *bytes_out, + size_t len, + size_t *written); + +/** + * Get the length of the BIP-375 silent payment code from an output. + * + * :param output: The output to get from. + * :param written: Destination for the length, or zero if not present. + */ +WALLY_CORE_API int wally_psbt_output_get_sp_v0_info_len( + const struct wally_psbt_output *output, + size_t *written); + +/** + * Set the BIP-375 silent payment code in an output. + * + * An output with a silent payment code does not need a script until the + * signer computes it. + * + * :param output: The output to update. + * :param keys: The scan public key followed by the spend public key. + * :param keys_len: Length of ``keys`` in bytes. Must be `WALLY_SILENT_PAYMENT_KEYS_LEN`. + */ +WALLY_CORE_API int wally_psbt_output_set_sp_v0_info( + struct wally_psbt_output *output, + const unsigned char *keys, + size_t keys_len); + +/** + * Clear the BIP-375 silent payment code from an output. + * + * :param output: The output to update. + */ +WALLY_CORE_API int wally_psbt_output_clear_sp_v0_info( + struct wally_psbt_output *output); + #ifndef WALLY_ABI_NO_ELEMENTS /** @@ -1950,6 +2000,9 @@ WALLY_CORE_API int wally_psbt_free( *| or ``WALLY_PSBT_VERSION_2``. * * .. note:: This call converts the PSBT in place to the specified version. + * + * .. note:: Converting to version 0 fails if any output has a BIP-375 silent + *| payment code, as those fields are only valid in version 2. */ WALLY_CORE_API int wally_psbt_set_version( struct wally_psbt *psbt, diff --git a/src/data/bip375_vectors.json b/src/data/bip375_vectors.json new file mode 100644 index 000000000..8aa19513d --- /dev/null +++ b/src/data/bip375_vectors.json @@ -0,0 +1,1804 @@ +{ + "description": "BIP-375 Test Vectors", + "version": "1.1.1", + "notes": [ + "Generated by https://github.com/macgyver13/bip375-test-generator/", + "Each vector includes: base64-encoded psbt and description", + "Supplementary material (inputs, outputs, sp_proofs) is included for diagnostics and should not be used for validation", + "'checks' overrides the validation sequence, e.g. skip structure checks to test input eligibility" + ], + "invalid": [ + { + "description": "psbt structure: missing PSBT_OUT_SP_V0_INFO field when PSBT_OUT_SP_V0_LABEL set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEgImjPUplZ8wrI96VHHKTGdTalHw5bForwPu1HFe3McuABCgQBAAAAAA==", + "supplementary": {} + }, + { + "description": "psbt structure: incorrect byte length for PSBT_OUT_SP_V0_LABEL field", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQECAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR8goQcAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9IMEUCIQCspcmETsmSLIHmcIF+My2i3RXSxdOpRiq21l5TtoKJXgIgTfMs6jgucsQ8BYQE/a5wWVHmuj3jPYM8g6lbsBg4CscBAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQMIoIYBAAAAAAABBCJRICJoz1KZWfMKyPelRxykxnU2pR8OWxaK8D7tRxXtzHLgAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA+qPIovitxbaze/LKBAHKHTwCkdvXAe9BRkXEmzGSz3vAQoBAQABAwhw8wUAAAAAAAEEFgAUOMGPNd2AY+OApw/+tPmP9O80gXciAgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvwwAAAAAAAAAAAAAAAEA", + "supplementary": {} + }, + { + "description": "psbt structure: incorrect byte length for PSBT_OUT_SP_V0_INFO field", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUECekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84AA=", + "supplementary": {} + }, + { + "description": "psbt structure: incorrect byte length for PSBT_IN_SP_ECDH_SHARE field", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GggA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QoiHgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaECKE7OYVUX3K9bolxSuuQmz41SoQqm7i1bNDt7SHfihmVmLMSKKSeC9fpXOEFP3xbKKy1Q6aHB2AOPOiYIu4yAhAAEDCBhzAQAAAAAAAQQiUSAybfUP4KB7estyBwvrO2Muua0VumlcweWqaqAHthRv2AEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": {} + }, + { + "description": "psbt structure: incorrect byte length for PSBT_IN_SP_DLEQ field", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4Gg/ihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgAAEDCBhzAQAAAAAAAQQiUSAybfUP4KB7estyBwvrO2Muua0VumlcweWqaqAHthRv2AEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": {} + }, + { + "description": "psbt structure: PSBT_GLOBAL_TX_MODIFIABLE field is non-zero when PSBT_OUT_SCRIPT set for sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAQABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": {} + }, + { + "description": "psbt structure: missing PSBT_OUT_SCRIPT field when sending to non-sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAA=", + "supplementary": {} + }, + { + "description": "ecdh coverage: only one ineligible P2SH multisig input when PSBT_OUT_SCRIPT set for sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiDNOBpFUI/j2cq2O34PcEoPVp2/1VqYEC2eozruYht19AEPBAAAAAABAFMCAAAAAXyoMfPUiQn+aMsOqqoR+Hc97n6McwhTN1kOJLsGOQ20AAAAAAD/////AaCGAQAAAAAAF6kU2OZohxTu7Qqi7dSJqhyjwXFwHk2HAAAAAAEER1IhAr5Ar99ismPLfe4rmF0cOP5BsZlNKwUHU0PRcsIY6rHEIQJ6R5JT0AogWhCpQ483IBm582TgqDaAC/g529KOfOa5VVKuARAE/v///wABAwiQXwEAAAAAAAEEIlEgzeOdiwW0lvjxjyCyfQr5oyMzCeBHIhnggT/ztdN4fikBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "", + "public_key": "", + "prevout_txid": "cd381a45508fe3d9cab63b7e0f704a0f569dbfd55a98102d9ea33aee621b75f4", + "prevout_index": 0, + "prevout_scriptpubkey": "a914d8e6688714eeed0aa2edd489aa1ca3c171701e4d87", + "amount": 100000, + "non_witness_utxo": "02000000017ca831f3d48909fe68cb0eaaaa11f8773dee7e8c73085337590e24bb06390db40000000000ffffffff01a08601000000000017a914d8e6688714eeed0aa2edd489aa1ca3c171701e4d8700000000", + "sequence": 4294967294, + "signed": false + } + ] + } + }, + { + "description": "ecdh coverage: missing PSBT_IN_SP_ECDH_SHARE field for input 0 when PSBT_OUT_SCRIPT set for sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////AAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBHxAnAAAAAAAAFgAURjPVnK0TQ0eZcuDJlpIdCl2ttl0BAwQBAAAAIgYCQ6DUDp3giCeUFhdSuuQrqQlSeEIJ8GNDDwryVjVKhbsIAAAAgAEAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghAuV6etywGNj7MVToDBS5elCZS0I1aEHqn8ExcGFEAOY3Ih4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhA9/o4HeILO0WcbkJsYhPUF1MI8kgqROMyhCMmJNTa/Vcymb868WqzAsViY2OjfUSHB/fnoevqBke8zUhiA7srpgABAwgYcwEAAAAAAAEEIlEgzeOdiwW0lvjxjyCyfQr5oyMzCeBHIhnggT/ztdN4fikBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "00144633d59cad1343479972e0c996921d0a5dadb65d", + "amount": 10000, + "witness_utxo": "10270000000000001600144633d59cad1343479972e0c996921d0a5dadb65d", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "02e57a7adcb018d8fb3154e80c14b97a50994b42356841ea9fc13170614400e637", + "dleq_proof": "f7fa381de20b3b459c6e426c6213d4175308f2482a44e33284232624d4dafd573299bf3af16ab302c5626363a37d448707f7e7a1ebea0647bccd486203bb2ba6", + "input_index": 1 + } + ] + } + }, + { + "description": "ecdh coverage: missing PSBT_IN_SP_DLEQ field for input when PSBT_IN_SP_ECDH_SHARE set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbAAEDCBhzAQAAAAAAAQQiUSAybfUP4KB7estyBwvrO2Muua0VumlcweWqaqAHthRv2AEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": null, + "input_index": 0 + } + ] + } + }, + { + "description": "ecdh coverage: missing PSBT_GLOBAL_SP_DLEQ field when PSBT_GLOBAL_SP_ECDH_SHARE set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBACIHAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWwABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEiBgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvwgAAACAAAAAAAEQBP7///8AAQMIGHMBAAAAAAABBCJRIDJt9Q/goHt6y3IHC+s7Yy65rRW6aVzB5apqoAe2FG/YAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b" + } + ] + } + }, + { + "description": "ecdh coverage: invalid proof in PSBT_IN_SP_DLEQ field", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAbvHreDwFl9ZoVl4787j95b7SgVKV9D4XAVEk1EXcy9rdKkne51lhePEhbl1NuRdP/dpjNON8kDEQjAjOE3TeDwABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "6ef1eb783c0597d668565e3bf3b8fde5bed2815295f43e17015124d445dccbdadd2a49dee7596178f1216e5d4db9174ffdda6334e37c9031108c08ce1374de0f", + "input_index": 0 + } + ] + } + }, + { + "description": "ecdh coverage: invalid proof in PSBT_GLOBAL_SP_DLEQ field", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBACIHAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIIAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQOfI/UQrlNiKKmoskrQ0ZI+gB8n+CARyozeA0LUjKETQm9iLP+6G0S1DhsRlf7t4Xi0vFL/PkYV0P+EqyCUPldkAAQ4gGKcXZjsLqxSxKhp3EyP/HkB53VMuXdE+KOoQgccAmEoBDwQAAAAAAQEfoIYBAAAAAAAWABQimnLTSmRb00lru/ULu4HJBj9PlCICAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/RzBEAiAJB3pqpkrBSuehqk/mjAKs4gbJ6scjSQYa52A5ALkibgIgbxQypXj44zCvC5ACVnRryfTlKqLozHkCncjnajwpSwYBIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////AAEDCBhzAQAAAAAAAQQiUSAybfUP4KB7estyBwvrO2Muua0VumlcweWqaqAHthRv2AEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "2430479d2da1310a024d2aab8e4c7b49d76cd51f45ee8127c159b0393a675b6f75c581ebe3d0817ddaf3bb598cfd7a022f1a87ed479664f6b947a47db5941cb2" + } + ] + } + }, + { + "description": "ecdh coverage: missing PSBT_IN_BIP32_DERIVATION field for input when PSBT_IN_SP_DLEQ set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIAkHemqmSsFK56GqT+aMAqziBsnqxyNJBhrnYDkAuSJuAiBvFDKlePjjMK8LkAJWdGvJ9OUqoujMeQKdyOdqPClLBgEBAwQBAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQMIGHMBAAAAAAABBCJRIDJt9Q/goHt6y3IHC+s7Yy65rRW6aVzB5apqoAe2FG/YAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ] + } + }, + { + "description": "ecdh coverage: output 1 missing ECDH share for scan key with one input / three sp outputs (different scan keys)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEDAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR9ADQMAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdArlFYVfpRu8H90tU+gm+Sn6Tjcr7+ye0s3LFc8BZ8SqwIQLChkR0sISqyB9uMKxe30ZnOTzmBNgI0/SZoJhOi1JJdCIdA2UdLAc/ywKk2C3aU/HVAdd6A1BmZ5hUZHOyEmts0afeIQN2iSMnxOeAru7UzhvJK7ASK3t5ENlvCtUUi0lvnD6S+yIeArlFYVfpRu8H90tU+gm+Sn6Tjcr7+ye0s3LFc8BZ8SqwQPCxHVHD/ROzjgpsjvwu4Mpxdzr2dLMVHmiSFJJcS1fIFojJ4Hi3UIhabKsoa1d+k6JJfrSWNrGvG2vILnyRElEiHgNlHSwHP8sCpNgt2lPx1QHXegNQZmeYVGRzshJrbNGn3kCY8UrEWvZFmrDITUfaU2TPsUT/kL2scy+cq8bbAA3PGpXjsx97cKBCmKUbb9RRbIHXFsmVT4rxDD1ZgJruK704AAEDCJBfAQAAAAAAAQQiUSDTlxRqgoZbVOsSuepx1oJP6rD5KkgTf3p5OmhZazNnpAEJQgNlHSwHP8sCpNgt2lPx1QHXegNQZmeYVGRzshJrbNGn3gPycOClYyGDtqsBB06HfmgA2wD1x8XS+vu2j5wo1IVFJAABAwgQJwAAAAAAAAEEIlEgzeOdiwW0lvjxjyCyfQr5oyMzCeBHIhnggT/ztdN4fikBCUIDUteMQTkAMq2RgWppf8dA2OuQnPBNcIhSZLBR8jheJewC4MTBh7chX5l8hIWnW0WRtsKEd7laESO5lpItMVSGC/gAAQMIIE4AAAAAAAABBCJRINLSQezkGFFKoY45kuyVPE1K1exwSmk+IQa6IsOwWnqDAQlCArlFYVfpRu8H90tU+gm+Sn6Tjcr7+ye0s3LFc8BZ8SqwAhJoHj8J6uOKjy8/7NfdVvxYbPoCjoqzP3KpD4QKbSJcAA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 200000, + "witness_utxo": "400d030000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "03651d2c073fcb02a4d82dda53f1d501d77a0350666798546473b2126b6cd1a7de", + "ecdh_share": "0376892327c4e780aeeed4ce1bc92bb0122b7b7910d96f0ad5148b496f9c3e92fb", + "dleq_proof": "98f14ac45af6459ab0c84d47da5364cfb144ff90bdac732f9cabc6db000dcf1a95e3b31f7b70a04298a51b6fd4516c81d716c9954f8af10c3d59809aee2bbd38", + "input_index": 0 + }, + { + "scan_key": "02b9456157e946ef07f74b54fa09be4a7e938dcafbfb27b4b372c573c059f12ab0", + "ecdh_share": "02c2864474b084aac81f6e30ac5edf4667393ce604d808d3f499a0984e8b524974", + "dleq_proof": "f0b11d51c3fd13b38e0a6c8efc2ee0ca71773af674b3151e689214925c4b57c81688c9e078b750885a6cab286b577e93a2497eb49636b1af1b6bc82e7c911251", + "input_index": 0 + } + ] + } + }, + { + "description": "ecdh coverage: input 1 missing ECDH share for output 1 with two inputs / two sp outputs (different scan keys)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQECAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdA1LXjEE5ADKtkYFqaX/HQNjrkJzwTXCIUmSwUfI4XiXsIQMJH9SAORd/RNe3npYWiEnGLrP4eADEZ2fFCo84+Y79FSIdA2UdLAc/ywKk2C3aU/HVAdd6A1BmZ5hUZHOyEmts0afeIQN2iSMnxOeAru7UzhvJK7ASK3t5ENlvCtUUi0lvnD6S+yIeA1LXjEE5ADKtkYFqaX/HQNjrkJzwTXCIUmSwUfI4XiXsQL4acjrvVeiYG5lZmpvBKzj4froBWMxwZokPImnK3kY7dzQal9KScZZNAcAkHMMi/VOQwpuZFg+CbzKTZYmMvwIiHgNlHSwHP8sCpNgt2lPx1QHXegNQZmeYVGRzshJrbNGn3kCY8UrEWvZFmrDITUfaU2TPsUT/kL2scy+cq8bbAA3PGpXjsx97cKBCmKUbb9RRbIHXFsmVT4rxDD1ZgJruK704AAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBH6CGAQAAAAAAFgAURjPVnK0TQ0eZcuDJlpIdCl2ttl0BAwQBAAAAIgYCQ6DUDp3giCeUFhdSuuQrqQlSeEIJ8GNDDwryVjVKhbsIAAAAgAEAAAABEAT+////Ih0DZR0sBz/LAqTYLdpT8dUB13oDUGZnmFRkc7ISa2zRp94hAnUEQAf+LQMNV3wymRzyo+deU1BCRbPL5bMFOufF8iBaIh4DZR0sBz/LAqTYLdpT8dUB13oDUGZnmFRkc7ISa2zRp95AOLnjgGtJvr7vxs7gZcKwZv2wCuzs8ei1sqr0fPBBeyma4KJW7Xy2yCcC63E31zv1eSccQoTQBuO1nXCpuWfYiQABAwiQXwEAAAAAAAEEIlEg9Jwu8ZtnBhG4bd7+iQzvlCaKgq4zHkb+GVHWxuNyPLEBCUIDZR0sBz/LAqTYLdpT8dUB13oDUGZnmFRkc7ISa2zRp94D8nDgpWMhg7arAQdOh35oANsA9cfF0vr7to+cKNSFRSQAAQMIkF8BAAAAAAABBCJRID2h4XbGT7RZU+xG4FA2NLx+jjQ6do2+77sKPojMSkyaAQlCA1LXjEE5ADKtkYFqaX/HQNjrkJzwTXCIUmSwUfI4XiXsAuDEwYe3IV+ZfISFp1tFkbbChHe5WhEjuZaSLTFUhgv4AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "00144633d59cad1343479972e0c996921d0a5dadb65d", + "amount": 100000, + "witness_utxo": "a0860100000000001600144633d59cad1343479972e0c996921d0a5dadb65d", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "03651d2c073fcb02a4d82dda53f1d501d77a0350666798546473b2126b6cd1a7de", + "ecdh_share": "0376892327c4e780aeeed4ce1bc92bb0122b7b7910d96f0ad5148b496f9c3e92fb", + "dleq_proof": "98f14ac45af6459ab0c84d47da5364cfb144ff90bdac732f9cabc6db000dcf1a95e3b31f7b70a04298a51b6fd4516c81d716c9954f8af10c3d59809aee2bbd38", + "input_index": 0 + }, + { + "scan_key": "0352d78c41390032ad91816a697fc740d8eb909cf04d70885264b051f2385e25ec", + "ecdh_share": "03091fd48039177f44d7b79e96168849c62eb3f87800c46767c50a8f38f98efd15", + "dleq_proof": "be1a723aef55e8981b99599a9bc12b38f87eba0158cc7066890f2269cade463b77341a97d29271964d01c0241cc322fd5390c29b99160f826f329365898cbf02", + "input_index": 0 + }, + { + "scan_key": "03651d2c073fcb02a4d82dda53f1d501d77a0350666798546473b2126b6cd1a7de", + "ecdh_share": "0275044007fe2d030d577c32991cf2a3e75e53504245b3cbe5b3053ae7c5f2205a", + "dleq_proof": "38b9e3806b49bebeefc6cee065c2b066fdb00aececf1e8b5b2aaf47cf0417b299ae0a256ed7cb6c82702eb7137d73bf579271c4284d006e3b59d70a9b967d889", + "input_index": 1 + } + ] + } + }, + { + "description": "ecdh coverage: input 1 missing ECDH share for scan key with two inputs / one sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR9QwwAAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQ4gvp1lyDkV+wkkirvfyoyj7ubONQxVT2Xo1Sbl31J6tSIBDwQAAAAAAQEfUMMAAAAAAAAWABRGM9WcrRNDR5ly4MmWkh0KXa22XSIGAkOg1A6d4IgnlBYXUrrkK6kJUnhCCfBjQw8K8lY1SoW7CAAAAIABAAAAARAE/v///wABAwgYcwEAAAAAAAEEIlEgzeOdiwW0lvjxjyCyfQr5oyMzCeBHIhnggT/ztdN4fikBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 50000, + "witness_utxo": "50c3000000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "00144633d59cad1343479972e0c996921d0a5dadb65d", + "amount": 50000, + "witness_utxo": "50c30000000000001600144633d59cad1343479972e0c996921d0a5dadb65d", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ] + } + }, + { + "description": "input eligibility: segwit version greater than 1 in transaction inputs with sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABZSFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIHLzeIUvuENyYHLyUHbw53Vg7UwuBHFm7mpibHW/2znWAiAhKq5fCVdONB9YhvX/8y9XFuq5AwpKU3nmAWtqTULcJAEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "5214229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000165214229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ] + }, + "checks": [ + "input_eligibility" + ] + }, + { + "description": "input eligibility: segwit version greater than 1 supplied via PSBT_IN_NON_WITNESS_UTXO with sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiDJcwwbkl87iv+6mh+VHww4gI7IlcsXC2fRZkcbayYwlgEPBAAAAAAiAgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIv0cwRAIgcvN4hS+4Q3JgcvJQdvDndWDtTC4EcWbuamJsdb/bOdYCICEqrl8JV040H1iG9f/zL1cW6rkDCkpTeeYBa2pNQtwkAQEDBAEAAAAiBgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvwgAAACAAAAAAAEQBP7///8iHQJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaCED7KT/Ebco4uD2DOYiKUOm/1W52V9ie/mpnQhLyHLVClsiHgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaECKE7OYVUX3K9bolxSuuQmz41SoQqm7i1bNDt7SHfihmVmLMSKKSeC9fpXOEFP3xbKKy1Q6aHB2AOPOiYIu4yAhAQBSAgAAAAH0whLVrtHn9v9jPyzgnGR8V77dkwMU2Y3ndQPVjpQLMwAAAAAA/////wGghgEAAAAAABZSFCKactNKZFvTSWu79Qu7gckGP0+UAAAAAAABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "prevout_txid": "c9730c1b925f3b8affba9a1f951f0c38808ec895cb170b67d166471b6b263096", + "prevout_index": 0, + "prevout_scriptpubkey": "5214229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "non_witness_utxo": "0200000001f4c212d5aed1e7f6ff633f2ce09c647c57bedd930314d98de77503d58e940b330000000000ffffffff01a086010000000000165214229a72d34a645bd3496bbbf50bbb81c9063f4f9400000000" + } + ] + } + }, + { + "description": "input eligibility: non-SIGHASH_ALL signature on input with sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIHLzeIUvuENyYHLyUHbw53Vg7UwuBHFm7mpibHW/2znWAiAhKq5fCVdONB9YhvX/8y9XFuq5AwpKU3nmAWtqTULcJAEBAwQCAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ] + } + }, + { + "description": "output scripts: P2TR input with NUMS internal key cannot derive sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAT8Qa2S1e1sTdvn2xHGQ9AzAazvty4qrqXhQqz1/Z9AgEPBAAAAAABASsQJwAAAAAAACJRIFCSm3TBoElUt4tLYDXpel4HiloPKOyW1Ue/7prOgDrAAQMEAQAAAAEQBP7///8BFyBQkpt0waBJVLeLS2A16XpeB4paDyjsltVHv+6azoA6wCIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQMIHCUAAAAAAAABBCJRICdoyIruoiMiW6OthOeGNS+YKdLT6TeKOToL/1mvh83QAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "13f106b64b57b5b1376f9f6c47190f40cc06b3bedcb8aaba97850ab3d7f67d02", + "prevout_index": 0, + "prevout_scriptpubkey": "512050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0", + "amount": 10000, + "witness_utxo": "102700000000000022512050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 9500, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "51202768c88aeea223225ba3ad84e786352f9829d2d3e9378a393a0bff59af87cdd0" + } + ] + } + }, + { + "description": "output scripts: PSBT_OUT_SCRIPT does not match derived sp output", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQMIGHMBAAAAAAABBCJRIM3jnYsFtJb48Y8gsn0K+aMjMwngRyIZ4IE/87XTeH4pAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 95000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120cde39d8b05b496f8f18f20b27d0af9a3233309e0472219e0813ff3b5d3787e29" + } + ] + } + }, + { + "description": "output scripts: two sp outputs (same scan / different spend keys) not sorted lexicographically by spend key", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQECAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCID9iBcRocGpGJF8XE6u4uLmjp7/YOsdF98ByDUQxtM38AiBxWKv7gg8r9a1nRfVvwHCcmVzMrP4XCY2KobYfjJ/y/wEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwiQXwEAAAAAAAEEIlEgdUe4Fj1bvFSYQL5MwUaq0JUgc2e565RwbeZwjUCPk/kBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgCi4D5lDNIleEbx2x1q254/iwluzMvIXshHEAXvx9nfYYAAQMIECcAAAAAAAABBCJRIGziw1OV8XDyS20tgNCkDEb0a6S0hD9qsxGpNLMcXYTcAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA6r4Yq7amM+SU5r33DN10dpfsGsSHvCh8DGp9zgv1T27AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068028b80f994334895e11bc76c75ab6e78fe2c25bb332f217b211c4017bf1f677d86", + "script": "51207547b8163d5bbc549840be4cc146aad095207367b9eb94706de6708d408f93f9" + }, + { + "output_index": 1, + "amount": 10000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe06803aaf862aeda98cf92539af7dc3375d1da5fb06b121ef0a1f031a9f7382fd53dbb", + "script": "51206ce2c35395f170f24b6d2d80d0a40c46f46ba4b4843f6ab311a934b31c5d84dc" + } + ] + } + }, + { + "description": "output scripts: k values assigned to wrong output indices with three sp outputs (same scan / spend keys)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEDAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIFg36hg2U7tRrGNQq+bDhHvokGogZdwwSNbSCOp8DkyCAiAQ+0/6sv7Vqmy2KAqTxHnPsE5Gyp7fF6owXNFnSiQsSQEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwiQXwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkAAQMIECcAAAAAAAABBCJRIJcUQsifBHhuCCcorhFu6rgC8/qelf2w03U6aEAgIAbeAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AAEDCBAnAAAAAAAAAQQiUSA/y66kkIf44D2d79Ory9bejAMrWD+Fpl0FeIHExEPNwAEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120326df50fe0a07b7acb72070beb3b632eb9ad15ba695cc1e5aa6aa007b6146fd8" + }, + { + "output_index": 1, + "amount": 10000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120971442c89f04786e082728ae116eeab802f3fa9e95fdb0d3753a6840202006de" + }, + { + "output_index": 2, + "amount": 10000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "51203fcbaea49087f8e03d9defd3abcbd6de8c032b583f85a65d057881c4c443cdc0" + } + ] + } + } + ], + "valid": [ + { + "description": "input eligibility: bare OP_2 script is not a segwit v2 witness program", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAQqghgEAAAAAAAFSIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIHLzeIUvuENyYHLyUHbw53Vg7UwuBHFm7mpibHW/2znWAiAhKq5fCVdONB9YhvX/8y9XFuq5AwpKU3nmAWtqTULcJAEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "prevout_scriptpubkey": "52", + "amount": 100000, + "witness_utxo": "a0860100000000000152" + } + ] + }, + "checks": [ + "input_eligibility" + ] + }, + { + "description": "can finalize: one P2PKH input single-signer", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiBSJ0jrF3ZNKMpJSBXsjUnn0w1SvHNCLHyG63TjlwVylAEPBAAAAAABAFUCAAAAAfTCEtWu0ef2/2M/LOCcZHxXvt2TAxTZjed1A9WOlAszAAAAAAD/////AaCGAQAAAAAAGXapFB4q14ctMpQTpW3wlovjOCIngxY7iKwAAAAAIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIDnBDcvHz0XG2UNW/1DBK42GqVUM8DcXPZzr94cU5nx1AiBxlVpC7SBTJDIHI8TwFCXc6J9CX4NwKEy0J2z9tt6jrAEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwgYcwEAAAAAAAEEIlEg4UDSh7RbRs1OqvpDdwYVcLq+g9G4vJUKdKn+oJIz16YBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "522748eb17764d28ca494815ec8d49e7d30d52bc73422c7c86eb74e397057294", + "prevout_index": 0, + "prevout_scriptpubkey": "76a9141e2ad7872d329413a56df0968be338222783163b88ac", + "amount": 100000, + "non_witness_utxo": "0200000001f4c212d5aed1e7f6ff633f2ce09c647c57bedd930314d98de77503d58e940b330000000000ffffffff01a0860100000000001976a9141e2ad7872d329413a56df0968be338222783163b88ac00000000", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 95000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120e140d287b45b46cd4eaafa4377061570babe83d1b8bc950a74a9fea09233d7a6" + } + ] + } + }, + { + "description": "can finalize: two inputs single-signer using global ECDH share", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBACIHAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQLTJicjNSYH6E/K92UfjS1jfgIK6obfrYDTBs1PSM/MYiIIAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQMZTtx+UaBipW9w58JGERbyQOgh2tzgGDp5dsdzsMKvfVOC18XhXP1EZ3nGqY60ZtEnjvLZUY5Nsi67qTZFzkrkAAQ4gGKcXZjsLqxSxKhp3EyP/HkB53VMuXdE+KOoQgccAmEoBDwQAAAAAAQEfoIYBAAAAAAAWABQimnLTSmRb00lru/ULu4HJBj9PlCICAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/RzBEAiAXBfzaBiZu2zKxaYZ3s39nu25F1JLr/OZWqIDufxOOtgIgPc3jI53ZBjavZPskEspB45fJWKAwd3z/nIlAucd310wBIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////AAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBH6CGAQAAAAAAFgAUE5NQEXxXeUGphYArue3vwROTK8ciAgL1tZ+l5JIiHr9VunitRCYFvq6VFmuh66MlDQu6rH4u3EcwRAIgA4mx4K7v1/q2Ce+rAYv7AWrP8hu4EPPEjDi6ckQYDpUCIBUP7GDiVvQMR1dN/RKaCOr3rB2Xq+4x9AaZ+Hk1RhnzASIGAvW1n6XkkiIev1W6eK1EJgW+rpUWa6HroyUNC7qsfi7cCAAAAIABAAAAARAE/v///wABAwgYcwEAAAAAAAEEIlEgEz1qT1CQlbV5T3SR+8PxLDxFvC2bYK8luLunBMgHKq8BCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "295c2eedddd8331d20b5d4cf9e69bb523ed85cb0bf35ab12e04fea66fe6d4a4a", + "public_key": "02f5b59fa5e492221ebf55ba78ad442605beae95166ba1eba3250d0bbaac7e2edc", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "0014139350117c577941a985802bb9edefc113932bc7", + "amount": 100000, + "witness_utxo": "a086010000000000160014139350117c577941a985802bb9edefc113932bc7", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "02d3262723352607e84fcaf7651f8d2d637e020aea86dfad80d306cd4f48cfcc62", + "dleq_proof": "c653b71f946818a95bdc39f0918445bc903a0876b738060e9e5db1dcec30abdf54e0b5f178573f5119de71aa63ad19b449e3bcb65463936c8baeea4d917392b9" + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 95000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120133d6a4f509095b5794f7491fbc3f12c3c45bc2d9b60af25b8bba704c8072aaf" + } + ] + } + }, + { + "description": "can finalize: two inputs single-signer using per-input ECDH shares", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIBcF/NoGJm7bMrFphnezf2e7bkXUkuv85laogO5/E462AiA9zeMjndkGNq9k+yQSykHjl8lYoDB3fP+ciUC5x3fXTAEiBgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvwgAAACAAAAAAAEQBP7///8iHQJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaCED7KT/Ebco4uD2DOYiKUOm/1W52V9ie/mpnQhLyHLVClsiHgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaECKE7OYVUX3K9bolxSuuQmz41SoQqm7i1bNDt7SHfihmVmLMSKKSeC9fpXOEFP3xbKKy1Q6aHB2AOPOiYIu4yAhAAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBH6CGAQAAAAAAFgAUE5NQEXxXeUGphYArue3vwROTK8ciAgL1tZ+l5JIiHr9VunitRCYFvq6VFmuh66MlDQu6rH4u3EcwRAIgA4mx4K7v1/q2Ce+rAYv7AWrP8hu4EPPEjDi6ckQYDpUCIBUP7GDiVvQMR1dN/RKaCOr3rB2Xq+4x9AaZ+Hk1RhnzAQEDBAEAAAAiBgL1tZ+l5JIiHr9VunitRCYFvq6VFmuh66MlDQu6rH4u3AgAAACAAQAAAAEQBP7///8iHQJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaCEDNNLO907e1EZCJ2B/xZrB3oDBKiQzpWJxUo7cz1D6bCsiHgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaEA3wAwLQqBz9rZfDEtuVuBndLOAwd894otkCxIe4LO0z3hQ30VuC12OGXXd/yUmLIOB4g+FLNhwveP2qDKmMlvMAAEDCBhzAQAAAAAAAQQiUSATPWpPUJCVtXlPdJH7w/EsPEW8LZtgryW4u6cEyAcqrwEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "295c2eedddd8331d20b5d4cf9e69bb523ed85cb0bf35ab12e04fea66fe6d4a4a", + "public_key": "02f5b59fa5e492221ebf55ba78ad442605beae95166ba1eba3250d0bbaac7e2edc", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "0014139350117c577941a985802bb9edefc113932bc7", + "amount": 100000, + "witness_utxo": "a086010000000000160014139350117c577941a985802bb9edefc113932bc7", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + }, + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "0334d2cef74eded4464227607fc59ac1de80c12a2433a56271528edccf50fa6c2b", + "dleq_proof": "37c00c0b42a073f6b65f0c4b6e56e06774b380c1df3de28b640b121ee0b3b4cf7850df456e0b5d8e1975ddff25262c8381e20f852cd870bde3f6a832a6325bcc", + "input_index": 1 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 95000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120133d6a4f509095b5794f7491fbc3f12c3c45bc2d9b60af25b8bba704c8072aaf" + } + ] + } + }, + { + "description": "can finalize: two inputs / two sp outputs with mixed global and per-input ECDH shares", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQECAQYBACIHAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQNmz8f9zgbgjtSbGzlSuNFRqPfgdf5fbdWN1o+dre7KlCIIAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oQF6riwhtSFD+nRyflhqR+Ze2HKjQhQr8BqeL3zx0nvVAUOaStT+1sIbl54eT8B5203R/ieMXen/v9vjBYX8bBuMAAQ4gGKcXZjsLqxSxKhp3EyP/HkB53VMuXdE+KOoQgccAmEoBDwQAAAAAAQEfQA0DAAAAAAAWABQimnLTSmRb00lru/ULu4HJBj9PlCICAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/SDBFAiEA1k6e8CREAEbpyjkwLrh7LfYN/FHJwKkX1oLzz4EFkIwCIB6+EqXx/SGW8u+11Tr+V850W5Af6ImXitWa7+FKT1A3AQEDBAEAAAAiBgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvwgAAACAAAAAAAEQBP7///8iHQKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVECED+9Fz2cSNWMUCJCbQUni8oXuajiKCZmkBobALngS3mUkiHgKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVEEDAXaQQSHz67DeP6+avUAXjrmyGI2gYsOVbIUAX61Bz7e9AF5V1tdzaqQY0kayLg/0GJNQXek0t1JlMRkDWuMzeAAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBH0ANAwAAAAAAFgAUE5NQEXxXeUGphYArue3vwROTK8ciAgL1tZ+l5JIiHr9VunitRCYFvq6VFmuh66MlDQu6rH4u3EcwRAIgWoJ5pshwRPECyFkwyJ7C/wGUKGJBraOMeFTy6DZR1fICICvMZkqflwTVcxLbwryDaGcIDyLEdYuR27ktF6HkEeqpAQEDBAEAAAAiBgL1tZ+l5JIiHr9VunitRCYFvq6VFmuh66MlDQu6rH4u3AgAAACAAQAAAAEQBP7///8iHQKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVECEDpqibG5hqmVUz4XvtZFALVBuw1zkHHv4q4rfvsKDKa88iHgKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVEEDDSmopQLvppzVsHMb/41jdREHbWIl8upnCEut1KF59oeyhHHksl6OASTNFqyJN6dlMKzbuhSauI1KBcaMjHeczAAEDCJBfAQAAAAAAAQQiUSCA65gxi89t1hGsF6lksJFzbATK6t8ubodOXO+Gx8YFFwEJQgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKAMSZlgePoQcu5TeYupzVYOiMsTIiPpu3vLsL4cHifwd1AABAwiQXwEAAAAAAAEEIlEgELYvxakKvKAWeWHb/0zq6aoyTvssfyRmJ+i1oz3jfIQBCUICqfH6wsxFdoTzPekAc/4g0iyUfJIWO4YrFNCrod9HFRACSkjXbiDUC89fLhpcP/6z3CqLO4jXmkhtVMnZ6qj6GI4A", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 200000, + "witness_utxo": "400d030000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "295c2eedddd8331d20b5d4cf9e69bb523ed85cb0bf35ab12e04fea66fe6d4a4a", + "public_key": "02f5b59fa5e492221ebf55ba78ad442605beae95166ba1eba3250d0bbaac7e2edc", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "0014139350117c577941a985802bb9edefc113932bc7", + "amount": 200000, + "witness_utxo": "400d030000000000160014139350117c577941a985802bb9edefc113932bc7", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "0366cfc7fdce06e08ed49b1b3952b8d151a8f7e075fe5f6dd58dd68f9dadeeca94", + "dleq_proof": "5eab8b086d4850fe9d1c9f961a91f997b61ca8d0850afc06a78bdf3c749ef54050e692b53fb5b086e5e78793f01e76d3747f89e3177a7feff6f8c1617f1b06e3" + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "03fbd173d9c48d58c5022426d05278bca17b9a8e2282666901a1b00b9e04b79949", + "dleq_proof": "c05da410487cfaec378febe6af5005e3ae6c86236818b0e55b214017eb5073edef40179575b5dcdaa9063491ac8b83fd0624d4177a4d2dd4994c4640d6b8ccde", + "input_index": 0 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "03a6a89b1b986a995533e17bed64500b541bb0d739071efe2ae2b7efb0a0ca6bcf", + "dleq_proof": "c34a6a2940bbe9a7356c1cc6ffe358dd4441db58897cba99c212eb75285e7da1eca11c792c97a380493345ab224de9d94c2b36ee8526ae23528171a3231de733", + "input_index": 1 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28031266581e3e841cbb94de62ea735583a232c4c888fa6edef2ec2f870789fc1dd4", + "script": "512080eb98318bcf6dd611ac17a964b091736c04caeadf2e6e874e5cef86c7c60517" + }, + { + "output_index": 1, + "amount": 90000, + "sp_v0_info": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510024a48d76e20d40bcf5f2e1a5c3ffeb3dc2a8b3b88d79a486d54c9d9eaa8fa188e", + "script": "512010b62fc5a90abca0167961dbff4ceae9aa324efb2c7f246627e8b5a33de37c84" + } + ] + } + }, + { + "description": "can finalize: one input / one sp output with both global and per-input ECDH shares", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBACIHAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQMKGLm7QZm4kzDrF/juWseG4IU9zS88zfF0S5fOTt4FPiIIAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oQCf7kMXWHHjjEVIA/9GD0o0KtIOAYslUGTTGQW1CZ84xvUKH6TWUUsCBevDvTVDP9XhDaw0o+e2PIbeaRHZAgI4AAQ4gGKcXZjsLqxSxKhp3EyP/HkB53VMuXdE+KOoQgccAmEoBDwQAAAAAAQEfQA0DAAAAAAAWABQimnLTSmRb00lru/ULu4HJBj9PlCICAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/RzBEAiA3eo+fWjPY+iVcM7OSJtO88CKd+tlgRw74AWpxQvIzWAIgCBvWlIh63us2Ia9aLeTw05yxNpLQQb99vM00oysFQekBAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQMKGLm7QZm4kzDrF/juWseG4IU9zS88zfF0S5fOTt4FPiIeAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oQAATg9RmG+hyXvKQSPSMkR+J9fNDF9fAwKzGDvZfG1K//3qW2DObYl34Orq1eqJz7c2aNceU2suxyGEXG7JnB18AAQMIkF8BAAAAAAABBCJRINxNRNnjYeXnQ2V5x44lklVB4in9wVOtWYmtiuawYkx4AQlCAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oAxJmWB4+hBy7lN5i6nNVg6IyxMiI+m7e8uwvhweJ/B3UAA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 200000, + "witness_utxo": "400d030000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "030a18b9bb4199b89330eb17f8ee5ac786e0853dcd2f3ccdf1744b97ce4ede053e", + "dleq_proof": "27fb90c5d61c78e3115200ffd183d28d0ab4838062c9541934c6416d4267ce31bd4287e9359452c0817af0ef4d50cff578436b0d28f9ed8f21b79a447640808e" + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28031266581e3e841cbb94de62ea735583a232c4c888fa6edef2ec2f870789fc1dd4", + "script": "5120dc4d44d9e361e5e7436579c78e25925541e229fdc153ad5989ad8ae6b0624c78" + } + ] + } + }, + { + "description": "can finalize: three sp outputs (different scan keys) with multiple global ECDH shares", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEDAQYBACIHAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQIQLC2d9g7mmlqGYE3ZpigsIu+OLJJ22m5ZWlRj83Gq3uJCIHAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQLcfS7KxWiwEVF1ujzVSW/0xLbZCUItvMrrjyUvpU+L0CIHA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsIQJwvvXAapqFX8Y7/yvQvi1wAzyCC2FTQTyNbtpR8lobKSIIAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQQOyCQr97WIHO4GBWbH8TzEfkcQyJomRYwgMBswMeSBc7wL6gFoDTN1616AYRqw1Qix5Jq7VxsYeAMqrknwsC99IiCAMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKEDqQ0cOXhvjKRtIoLy1P1WVLGSGDnY177pQA8yt7XfdEo4hZpew9SO+IX7Gh1B3l8zOvFzZQ2KvG0bYA2Qr1flAIggDjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGxAG9/lTtSioG//mwAJrk49gPxHYWR1r98hJEz+PG0QTfJHQah1UU14ZU+5yhEY5tPlUvOPjKujVS6DM8UBvwy9PwABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR8goQcAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9IMEUCIQCLg3ekbsHGTj/QlvLhIwW7hrrfzKDP2b9X3JYim55whAIgfn3H3eyoVVwXfr7/yUnqTDNSD+W+7Hun4nJ+NPvUjf0BIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////AAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBH6CGAQAAAAAAFgAURjPVnK0TQ0eZcuDJlpIdCl2ttl0iAgJDoNQOneCIJ5QWF1K65CupCVJ4QgnwY0MPCvJWNUqFu0gwRQIhAPmSLe+tUsYSCQS6pacAUymDbsHD2uFsoGjAFrJrBNLYAiBw+V8k+cklbkGWlGnDUqz6yOiUMjUC6s0WPJSi42nuHwEiBgJDoNQOneCIJ5QWF1K65CupCVJ4QgnwY0MPCvJWNUqFuwgAAACAAQAAAAEQBP7///8AAQMIkF8BAAAAAAABBCJRIKFPr9x+36QEQnF0sSasoOmZMTP+F+JCCurtIO9U/DjcAQlCAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oAxJmWB4+hBy7lN5i6nNVg6IyxMiI+m7e8uwvhweJ/B3UAAEDCJBfAQAAAAAAAQQiUSB99fJJWUQ0FLvv6J0tqJSGSlYotoAzuSPoaGvcw96dlAEJQgKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVEAJKSNduINQLz18uGlw//rPcKos7iNeaSG1UydnqqPoYjgABAwiQXwEAAAAAAAEEIlEgsvEqf1cMxQV4QPpjk/cfBpABQgiBgYEMNLuIdh2SlQ0BCUIDjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGwD+BRzhEyddMkh/xelr0ELYzc0jXr7/3OJG8XLDLJMsMAA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 500000, + "witness_utxo": "20a1070000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "00144633d59cad1343479972e0c996921d0a5dadb65d", + "amount": 100000, + "witness_utxo": "a0860100000000001600144633d59cad1343479972e0c996921d0a5dadb65d", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "02dc7d2ecac568b0115175ba3cd5496ff4c4b6d909422dbccaeb8f252fa54f8bd0", + "dleq_proof": "ea43470e5e1be3291b48a0bcb53f55952c64860e7635efba5003ccaded77dd128e216697b0f523be217ec687507797cccebc5cd94362af1b46d803642bd5f940" + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "02c2d9df60ee69a5a86604dd9a6282c22ef8e2c9276da6e595a5463f371aadee24", + "dleq_proof": "ec8242bf7b5881cee060566c7f13cc47e4710c89a26458c20301b3031e48173bc0bea01680d3375eb5e80611ab0d508b1e49abb571b1878032aae49f0b02f7d2" + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "0270bef5c06a9a855fc63bff2bd0be2d70033c820b6153413c8d6eda51f25a1b29", + "dleq_proof": "1bdfe54ed4a2a06fff9b0009ae4e3d80fc47616475afdf21244cfe3c6d104df24741a875514d78654fb9ca1118e6d3e552f38f8caba3552e8333c501bf0cbd3f" + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28031266581e3e841cbb94de62ea735583a232c4c888fa6edef2ec2f870789fc1dd4", + "script": "5120a14fafdc7edfa404427174b126aca0e9993133fe17e2420aeaed20ef54fc38dc" + }, + { + "output_index": 1, + "amount": 90000, + "sp_v0_info": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510024a48d76e20d40bcf5f2e1a5c3ffeb3dc2a8b3b88d79a486d54c9d9eaa8fa188e", + "script": "51207df5f24959443414bbefe89d2da894864a5628b68033b923e8686bdcc3de9d94" + }, + { + "output_index": 2, + "amount": 90000, + "sp_v0_info": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c03f81473844c9d74c921ff17a5af410b6337348d7afbff73891bc5cb0cb24cb0c0", + "script": "5120b2f12a7f570cc5057840fa6393f71f06900142088181810c34bb88761d92950d" + } + ] + } + }, + { + "description": "can finalize: one P2WPKH input / two mixed outputs - labeled sp output and BIP 32 change", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQECAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR8goQcAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9IMEUCIQCspcmETsmSLIHmcIF+My2i3RXSxdOpRiq21l5TtoKJXgIgTfMs6jgucsQ8BYQE/a5wWVHmuj3jPYM8g6lbsBg4CscBAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQMIoIYBAAAAAAABBCJRICJoz1KZWfMKyPelRxykxnU2pR8OWxaK8D7tRxXtzHLgAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA+qPIovitxbaze/LKBAHKHTwCkdvXAe9BRkXEmzGSz3vAQoEAQAAAAABAwhw8wUAAAAAAAEEFgAUOMGPNd2AY+OApw/+tPmP9O80gXciAgLIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvwwAAAAAAAAAAAAAAAEA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 500000, + "witness_utxo": "20a1070000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe06803ea8f228be2b716dacdefcb2810072874f00a476f5c07bd051917126cc64b3def", + "sp_v0_label": 1, + "script": "51202268cf529959f30ac8f7a5471ca4c67536a51f0e5b168af03eed4715edcc72e0" + }, + { + "output_index": 1, + "amount": 390000, + "script": "001438c18f35dd8063e380a70ffeb4f98ff4ef348177" + } + ] + } + }, + { + "description": "can finalize: one input / two sp outputs - output 0 has no label / output 1 uses label=0 convention for sp change", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQECAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR/gkwQAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCICJa45kPVF365uoUKpN0arSqgSfjyBNY63wha08anrtiAiBl42BSGRArJQdeOVXz80zjvPKjXR4QFd1dw7/tPs81qQEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0CXw7Bs42ZhKfrYvFb5AMCBBDxQu301p42P1YVr9Kmts0hA84eNnXZDZkmSfqd2xH5AlSbs7AvVvbonMCgTIuMN92UIh0Ci3kOCYf96fDRI5jra+MMc3Z5PGR4ZaKgLy78lg2WZWghAlWBv6pzMVb2uY5xom9w/XazZ4ZJ9isfxed7jpq45hBQIh4CXw7Bs42ZhKfrYvFb5AMCBBDxQu301p42P1YVr9Kmts1ADolhPR9rJTbbj6kkIA2zpvjvyhciVWTVaxEuUIZbTTAuBY+m9hwp4jZqSnJp1FVaXGHWVVnb+/rJ4urf5TY31SIeAot5DgmH/enw0SOY62vjDHN2eTxkeGWioC8u/JYNlmVoQHlAgZ626fJYwVg4fApkzBb1F8fvRAkSLM6qlPTi/1N2xCR7/BcNmyY7Dj0ga2aMh+lLNN7q+gCheSsEeteIdbsAAQMIoIYBAAAAAAABBCJRIKBIDZZnnBk25au9SLqw4B/6yx7n0cnhaC8VQBaZZYXZAQlCAot5DgmH/enw0SOY62vjDHN2eTxkeGWioC8u/JYNlmVoA7RDRzNcY/UF5V4mirKA8xi0pqoUSS/zecNmih9U74ybAAEDCDDmAgAAAAAAAQQiUSDkKhgUiNj/9ucWH3FfXvD0fcbISAhIqtaERxvz6ljzUgEJQgJfDsGzjZmEp+ti8VvkAwIEEPFC7fTWnjY/VhWv0qa2zQLOCzIZpapd+PIc45PpYs3afVFUbaTTXDjtAYilludg8wEKBAAAAAAA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 300000, + "witness_utxo": "e093040000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "025f0ec1b38d9984a7eb62f15be403020410f142edf4d69e363f5615afd2a6b6cd", + "ecdh_share": "03ce1e3675d90d992649fa9ddb11f902549bb3b02f56f6e89cc0a04c8b8c37dd94", + "dleq_proof": "0e89613d1f6b2536db8fa924200db3a6f8efca17225564d56b112e50865b4d302e058fa6f61c29e2366a4a7269d4555a5c61d65559dbfbfac9e2eadfe53637d5", + "input_index": 0 + }, + { + "scan_key": "028b790e0987fde9f0d12398eb6be30c7376793c647865a2a02f2efc960d966568", + "ecdh_share": "025581bfaa733156f6b98e71a26f70fd76b3678649f62b1fc5e77b8e9ab8e61050", + "dleq_proof": "7940819eb6e9f258c158387c0a64cc16f517c7ef4409122cceaa94f4e2ff5376c4247bfc170d9b263b0e3d206b668c87e94b34deeafa00a1792b047ad78875bb", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 100000, + "sp_v0_info": "028b790e0987fde9f0d12398eb6be30c7376793c647865a2a02f2efc960d96656803b44347335c63f505e55e268ab280f318b4a6aa14492ff379c3668a1f54ef8c9b", + "script": "5120a0480d96679c1936e5abbd48bab0e01ffacb1ee7d1c9e1682f154016996585d9" + }, + { + "output_index": 1, + "amount": 190000, + "sp_v0_info": "025f0ec1b38d9984a7eb62f15be403020410f142edf4d69e363f5615afd2a6b6cd02ce0b3219a5aa5df8f21ce393e962cdda7d51546da4d35c38ed0188a596e760f3", + "sp_v0_label": 0, + "script": "5120e42a181488d8fff6e7161f715f5ef0f47dc6c8480848aad684471bf3ea58f352" + } + ] + } + }, + { + "description": "can finalize: three sp outputs (same scan key) - output 0 uses label=1, outputs 1 and 2 uses label=2 (same spend key)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEDAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+AGgYAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIFaplAuaU3aICoTcvxrXSZnHH6i4SXpbYSuzopSyB0k4AiA98iUFQysKS0I3s2sHC3R8ZpycvK7JZiE0PcjMfiNnYgEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Ci3kOCYf96fDRI5jra+MMc3Z5PGR4ZaKgLy78lg2WZWghAlWBv6pzMVb2uY5xom9w/XazZ4ZJ9isfxed7jpq45hBQIh4Ci3kOCYf96fDRI5jra+MMc3Z5PGR4ZaKgLy78lg2WZWhAeUCBnrbp8ljBWDh8CmTMFvUXx+9ECRIszqqU9OL/U3bEJHv8Fw2bJjsOPSBrZoyH6Us03ur6AKF5KwR614h1uwABAwihhgEAAAAAAAEEIlEgG/+hD9qiUCwvDJKF/grxmlD4UV47bfv1behQ2ZkBRAABCUICi3kOCYf96fDRI5jra+MMc3Z5PGR4ZaKgLy78lg2WZWgD1DFYje5hqzo5clspnDdC3Gaghh3s9ZG0I7DlJwcPgjQBCgQBAAAAAAEDCDLmAgAAAAAAAQQiUSCITaZwjIK5tWyYvTvm5fU4lLsb/ZC/rPiUAlVw9r1TBQEJQgKLeQ4Jh/3p8NEjmOtr4wxzdnk8ZHhloqAvLvyWDZZlaAMhk60Mh1+sUkNzgCKQCnckxhdd2smUQWWqtFeq35xPPAEKBAIAAAAAAQMIooYBAAAAAAABBCJRINKR0zkoDgyG4scIpZH46OL4ktEqfP6FELZneZtxExyGAQlCAot5DgmH/enw0SOY62vjDHN2eTxkeGWioC8u/JYNlmVoAyGTrQyHX6xSQ3OAIpAKdyTGF13ayZRBZaq0V6rfnE88AQoEAgAAAAA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 400000, + "witness_utxo": "801a060000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "028b790e0987fde9f0d12398eb6be30c7376793c647865a2a02f2efc960d966568", + "ecdh_share": "025581bfaa733156f6b98e71a26f70fd76b3678649f62b1fc5e77b8e9ab8e61050", + "dleq_proof": "7940819eb6e9f258c158387c0a64cc16f517c7ef4409122cceaa94f4e2ff5376c4247bfc170d9b263b0e3d206b668c87e94b34deeafa00a1792b047ad78875bb", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 100001, + "sp_v0_info": "028b790e0987fde9f0d12398eb6be30c7376793c647865a2a02f2efc960d96656803d431588dee61ab3a39725b299c3742dc66a0861decf591b423b0e527070f8234", + "sp_v0_label": 1, + "script": "51201bffa10fdaa2502c2f0c9285fe0af19a50f8515e3b6dfbf56de850d999014400" + }, + { + "output_index": 1, + "amount": 190002, + "sp_v0_info": "028b790e0987fde9f0d12398eb6be30c7376793c647865a2a02f2efc960d966568032193ad0c875fac5243738022900a7724c6175ddac9944165aab457aadf9c4f3c", + "sp_v0_label": 2, + "script": "5120884da6708c82b9b56c98bd3be6e5f53894bb1bfd90bfacf894025570f6bd5305" + }, + { + "output_index": 2, + "amount": 100002, + "sp_v0_info": "028b790e0987fde9f0d12398eb6be30c7376793c647865a2a02f2efc960d966568032193ad0c875fac5243738022900a7724c6175ddac9944165aab457aadf9c4f3c", + "sp_v0_label": 2, + "script": "5120d291d339280e0c86e2c708a591f8e8e2f892d12a7cfe8510b667799b71131c86" + } + ] + } + }, + { + "description": "can finalize: two inputs using per-input ECDH shares - only eligible inputs contribute shares (P2SH excluded)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9IMEUCIQCLTLcGPIp7P5Ia4ABZbNd4jlRA4dY+8e4bzsjCrJQMogIgNw2OmoWgzI3SWwuwgfMaotzFugoiSOqGCoFlHKNKDGgBAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQ4g0iImLyaHKbt8hMVYEppdaYYYXO9TXsFJwN9g3UqSwgQBDwQAAAAAAQBTAgAAAAFA5vqmEmjxehAQHWJVNYKIKuSFi+4TNj24D98oH4Jf/AAAAAAA/////wHwSQIAAAAAABepFPRfjMomjsJuS76JkXDxOCUNfPVehwAAAAABBEdSIQKHfKAvFEBZvYLQDhs5muN094pSzvehyjfyjXiLNA0veSEDTIekSHL14fAG002IoAlZKCvUU150d8PWjDFlmR6hs5ZSrgEQBP7///8iHQJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaCECYototkRuaocCWXPZOvsZTkIKEPOhzLOK3YLUZKiUpCgiHgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEDCJBfAQAAAAAAAQQiUSAybfUP4KB7estyBwvrO2Muua0VumlcweWqaqAHthRv2AEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "", + "public_key": "", + "prevout_txid": "d222262f268729bb7c84c558129a5d6986185cef535ec149c0df60dd4a92c204", + "prevout_index": 0, + "prevout_scriptpubkey": "a914f45f8cca268ec26e4bbe899170f138250d7cf55e87", + "amount": 150000, + "non_witness_utxo": "020000000140e6faa61268f17a10101d62553582882ae4858bee13363db80fdf281f825ffc0000000000ffffffff01f04902000000000017a914f45f8cca268ec26e4bbe899170f138250d7cf55e8700000000", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120326df50fe0a07b7acb72070beb3b632eb9ad15ba695cc1e5aa6aa007b6146fd8" + } + ] + } + }, + { + "description": "can finalize: two inputs using global ECDH share - only eligible inputs contribute shares (P2SH excluded)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBACIHAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIIAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQCQwR50toTEKAk0qq45Me0nXbNUfRe6BJ8FZsDk6Z1tvdcWB6+PQgX3a87tZjP16Ai8ah+1HlmT2uUekfbWUHLIAAQ4gGKcXZjsLqxSxKhp3EyP/HkB53VMuXdE+KOoQgccAmEoBDwQAAAAAAQEfoIYBAAAAAAAWABQimnLTSmRb00lru/ULu4HJBj9PlCICAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/SDBFAiEAi0y3BjyKez+SGuAAWWzXeI5UQOHWPvHuG87IwqyUDKICIDcNjpqFoMyN0lsLsIHzGqLcxboKIkjqhgqBZRyjSgxoASIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///wABDiDSIiYvJocpu3yExVgSml1phhhc71NewUnA32DdSpLCBAEPBAAAAAABAFMCAAAAAUDm+qYSaPF6EBAdYlU1gogq5IWL7hM2PbgP3ygfgl/8AAAAAAD/////AfBJAgAAAAAAF6kU9F+MyiaOwm5LvomRcPE4JQ189V6HAAAAAAEER1IhAod8oC8UQFm9gtAOGzma43T3ilLO96HKN/KNeIs0DS95IQNMh6RIcvXh8AbTTYigCVkoK9RTXnR3w9aMMWWZHqGzllKuARAE/v///wABAwiQXwEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "", + "public_key": "", + "prevout_txid": "d222262f268729bb7c84c558129a5d6986185cef535ec149c0df60dd4a92c204", + "prevout_index": 0, + "prevout_scriptpubkey": "a914f45f8cca268ec26e4bbe899170f138250d7cf55e87", + "amount": 150000, + "non_witness_utxo": "020000000140e6faa61268f17a10101d62553582882ae4858bee13363db80fdf281f825ffc0000000000ffffffff01f04902000000000017a914f45f8cca268ec26e4bbe899170f138250d7cf55e8700000000", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "2430479d2da1310a024d2aab8e4c7b49d76cd51f45ee8127c159b0393a675b6f75c581ebe3d0817ddaf3bb598cfd7a022f1a87ed479664f6b947a47db5941cb2" + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120326df50fe0a07b7acb72070beb3b632eb9ad15ba695cc1e5aa6aa007b6146fd8" + } + ] + } + }, + { + "description": "can finalize: two mixed input types - only eligible inputs contribute ECDH shares (NUMS internal key excluded)", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAT8Qa2S1e1sTdvn2xHGQ9AzAazvty4qrqXhQqz1/Z9AgEPBAAAAAABASsQJwAAAAAAACJRIFCSm3TBoElUt4tLYDXpel4HiloPKOyW1Ue/7prOgDrAARAE/v///wEXIFCSm3TBoElUt4tLYDXpel4HiloPKOyW1Ue/7prOgDrAAAEOIL6dZcg5FfsJJIq738qMo+7mzjUMVU9l6NUm5d9SerUiAQ8EAAAAAAEBH6CGAQAAAAAAFgAURjPVnK0TQ0eZcuDJlpIdCl2ttl0iAgJDoNQOneCIJ5QWF1K65CupCVJ4QgnwY0MPCvJWNUqFu0gwRQIhAPRhsN/2Q/wKVsFSIUU2OBYiEkh+UGpZ4GnXXtHwq4T2AiBSxx6U6TEuqKkAH80mNyvnCZfdwqpinFqBFtxR9bfpogEBAwQBAAAAIgYCQ6DUDp3giCeUFhdSuuQrqQlSeEIJ8GNDDwryVjVKhbsIAAAAgAEAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghAuV6etywGNj7MVToDBS5elCZS0I1aEHqn8ExcGFEAOY3Ih4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhA9/o4HeILO0WcbkJsYhPUF1MI8kgqROMyhCMmJNTa/Vcymb868WqzAsViY2OjfUSHB/fnoevqBke8zUhiA7srpgABAwgcJQAAAAAAAAEEIlEgBXZyJSSDmoybydLZc46Yu0DdLsUCO9x9p/cGI818hfsBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "13f106b64b57b5b1376f9f6c47190f40cc06b3bedcb8aaba97850ab3d7f67d02", + "prevout_index": 0, + "prevout_scriptpubkey": "512050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0", + "amount": 10000, + "witness_utxo": "102700000000000022512050929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "00144633d59cad1343479972e0c996921d0a5dadb65d", + "amount": 100000, + "witness_utxo": "a0860100000000001600144633d59cad1343479972e0c996921d0a5dadb65d", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "02e57a7adcb018d8fb3154e80c14b97a50994b42356841ea9fc13170614400e637", + "dleq_proof": "f7fa381de20b3b459c6e426c6213d4175308f2482a44e33284232624d4dafd573299bf3af16ab302c5626363a37d448707f7e7a1ebea0647bccd486203bb2ba6", + "input_index": 1 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 9500, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "51200576722524839a8c9bc9d2d9738e98bb40dd2ec5023bdc7da7f70623cd7c85fb" + } + ] + } + }, + { + "description": "can finalize: three sp outputs (same scan key) - each output has distinct k value", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEEAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+AGgYAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9HMEQCIA3+9XB4r8pV2yK0ze42yxF/h2HkeAgptCHRBkisr62PAiBk5vWJD0/QW3emSnGDHJiGTZyYTJ7x/r6afFC08VkHIQEBAwQBAAAAIgYCyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL8IAAAAgAAAAAABEAT+////Ih0Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GghA+yk/xG3KOLg9gzmIilDpv9VudlfYnv5qZ0IS8hy1QpbIh4Cekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GhAihOzmFVF9yvW6JcUrrkJs+NUqEKpu4tWzQ7e0h34oZlZizEiikngvX6VzhBT98WyistUOmhwdgDjzomCLuMgIQABAwighgEAAAAAAAEEIlEgMm31D+Cge3rLcgcL6ztjLrmtFbppXMHlqmqgB7YUb9gBCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkAAQMIoIYBAAAAAAABBCJRID/LrqSQh/jgPZ3v06vL1t6MAytYP4WmXQV4gcTEQ83AAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AAEDCKCGAQAAAAAAAQQiUSCXFELInwR4bggnKK4Rbuq4AvP6npX9sNN1OmhAICAG3gEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQABAwiQXwEAAAAAAAEEIlEgee9WiKP6f+/DkidFWx8cLwjHAIdCTWoA+EwxQhrKjaQA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 400000, + "witness_utxo": "801a060000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120326df50fe0a07b7acb72070beb3b632eb9ad15ba695cc1e5aa6aa007b6146fd8" + }, + { + "output_index": 1, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "51203fcbaea49087f8e03d9defd3abcbd6de8c032b583f85a65d057881c4c443cdc0" + }, + { + "output_index": 2, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120971442c89f04786e082728ae116eeab802f3fa9e95fdb0d3753a6840202006de" + }, + { + "output_index": 3, + "amount": 90000, + "script": "512079ef5688a3fa7fefc39227455b1f1c2f08c70087424d6a00f84c31421aca8da4" + } + ] + } + }, + { + "description": "can finalize: three sp outputs (same scan key) / two regular outputs - k values assigned independently of output index", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEGAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR8goQcAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9IMEUCIQD333KAYwDd2+SyTNcz9TsypkayNsKUYFfeR65QOyqxaAIgTGlAW3e+2vOyB+3ll0m7o9VMRhWoi7025Ugkr3fJ5zEBAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQMIoIYBAAAAAAABBCJRIDJt9Q/goHt6y3IHC+s7Yy65rRW6aVzB5apqoAe2FG/YAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AAEDCFDDAAAAAAAAAQQiUSCcaj0jgY6CDtHQz965Y2V8jsMne32bhlx411V/Xe3WogABAwighgEAAAAAAAEEIlEgP8uupJCH+OA9ne/Tq8vW3owDK1g/haZdBXiBxMRDzcABCUICekh/wZ+3aYd7h0LW6hgRjzxOcrHqjG3mAqetSkHb4GgDYeGx6d5eQssgB/fKVLng1X7ROTj61W0/GeV1E6j84DkAAQMIUMMAAAAAAAABBCJRIHnvVoij+n/vw5InRVsfHC8IxwCHQk1qAPhMMUIayo2kAAEDCKCGAQAAAAAAAQQiUSCXFELInwR4bggnKK4Rbuq4AvP6npX9sNN1OmhAICAG3gEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQABAwjILAEAAAAAAAEEIlEgb8UG+AUqn1qKqbmQ1emU9JXd7W1HusRMqs2t2NiZHNAA", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 500000, + "witness_utxo": "20a1070000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120326df50fe0a07b7acb72070beb3b632eb9ad15ba695cc1e5aa6aa007b6146fd8" + }, + { + "output_index": 1, + "amount": 50000, + "script": "51209c6a3d23818e820ed1d0cfdeb963657c8ec3277b7d9b865c78d7557f5dedd6a2" + }, + { + "output_index": 2, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "51203fcbaea49087f8e03d9defd3abcbd6de8c032b583f85a65d057881c4c443cdc0" + }, + { + "output_index": 3, + "amount": 50000, + "script": "512079ef5688a3fa7fefc39227455b1f1c2f08c70087424d6a00f84c31421aca8da4" + }, + { + "output_index": 4, + "amount": 100000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120971442c89f04786e082728ae116eeab802f3fa9e95fdb0d3753a6840202006de" + }, + { + "output_index": 5, + "amount": 77000, + "script": "51206fc506f8052a9f5a8aa9b990d5e994f495dded6d47bac44caacdadd8d8991cd0" + } + ] + } + }, + { + "description": "in progress: two P2TR inputs, neither is signed", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAT8Qa2S1e1sTdvn2xHGQ9AzAazvty4qrqXhQqz1/Z9AgEPBAAAAAABASughgEAAAAAACJRIMgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/AQMEAQAAAAEQBP7///8BFyDIF7t1Ia/DXqlvO/snDm61Dd/6VWBie5Yf7ADymWUIvyIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQ4gK5A3/0HdF05VIvD0BFw/QENiMq8mANTaXJ6hPMPKtLUBDwQAAAAAAQErECcAAAAAAAAiUSBDoNQOneCIJ5QWF1K65CupCVJ4QgnwY0MPCvJWNUqFuwEDBAEAAAABEAT+////ARcgQ6DUDp3giCeUFhdSuuQrqQlSeEIJ8GNDDwryVjVKhbsiHQJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaCEC5Xp63LAY2PsxVOgMFLl6UJlLQjVoQeqfwTFwYUQA5jciHgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaED3+jgd4gs7RZxuQmxiE9QXUwjySCpE4zKEIyYk1Nr9VzKZvzrxarMCxWJjY6N9RIcH9+eh6+oGR7zNSGIDuyumAAEDCLj5AgAAAAAAAQQiUSDLH766UNBuv/tpC9qdX/Oc4vrbT87clmMu/xukHMDAewEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "13f106b64b57b5b1376f9f6c47190f40cc06b3bedcb8aaba97850ab3d7f67d02", + "prevout_index": 0, + "prevout_scriptpubkey": "5120c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "amount": 100000, + "witness_utxo": "a086010000000000225120c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "2b9037ff41dd174e5522f0f4045c3f40436232af2600d4da5c9ea13cc3cab4b5", + "prevout_index": 0, + "prevout_scriptpubkey": "512043a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "amount": 10000, + "witness_utxo": "102700000000000022512043a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + }, + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "02e57a7adcb018d8fb3154e80c14b97a50994b42356841ea9fc13170614400e637", + "dleq_proof": "f7fa381de20b3b459c6e426c6213d4175308f2482a44e33284232624d4dafd573299bf3af16ab302c5626363a37d448707f7e7a1ebea0647bccd486203bb2ba6", + "input_index": 1 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 195000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039", + "script": "5120cb1fbeba50d06ebffb690bda9d5ff39ce2fadb4fcedc96632eff1ba41cc0c07b" + } + ] + } + }, + { + "description": "in progress: one P2TR input / one sp output with no ECDH shares when PSBT_OUT_SCRIPT field is not set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQEBAQYBAAABDiAT8Qa2S1e1sTdvn2xHGQ9AzAazvty4qrqXhQqz1/Z9AgEPBAAAAAABASughgEAAAAAACJRIMgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/ARAE/v///wEXIMgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/AAEDCBhzAQAAAAAAAQlCAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoA2HhseneXkLLIAf3ylS54NV+0Tk4+tVtPxnldROo/OA5AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "13f106b64b57b5b1376f9f6c47190f40cc06b3bedcb8aaba97850ab3d7f67d02", + "prevout_index": 0, + "prevout_scriptpubkey": "5120c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "amount": 100000, + "witness_utxo": "a086010000000000225120c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [], + "outputs": [ + { + "output_index": 0, + "amount": 95000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039" + } + ] + } + }, + { + "description": "in progress: two inputs / one sp output, input 1 missing ECDH share when PSBT_OUT_SCRIPT field is not set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQIBBQEBAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoIQPspP8Rtyji4PYM5iIpQ6b/VbnZX2J7+amdCEvIctUKWyIeAnpIf8Gft2mHe4dC1uoYEY88TnKx6oxt5gKnrUpB2+BoQIoTs5hVRfcr1uiXFK65CbPjVKhCqbuLVs0O3tId+KGZWYsxIopJ4L1+lc4QU/fFsorLVDpocHYA486Jgi7jICEAAQ4gvp1lyDkV+wkkirvfyoyj7ubONQxVT2Xo1Sbl31J6tSIBDwQAAAAAAQEf8EkCAAAAAAAWABRGM9WcrRNDR5ly4MmWkh0KXa22XSIGAkOg1A6d4IgnlBYXUrrkK6kJUnhCCfBjQw8K8lY1SoW7CAAAAIABAAAAARAE/v///wABAwhADQMAAAAAAAEJQgJ6SH/Bn7dph3uHQtbqGBGPPE5yseqMbeYCp61KQdvgaANh4bHp3l5CyyAH98pUueDVftE5OPrVbT8Z5XUTqPzgOQA=", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 1, + "private_key": "586b0810bbc3c7266971aeb7f8778b41707e092a1aeaf0d953b211f956e35541", + "public_key": "0243a0d40e9de0882794161752bae42ba90952784209f063430f0af256354a85bb", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "00144633d59cad1343479972e0c996921d0a5dadb65d", + "amount": 150000, + "witness_utxo": "f0490200000000001600144633d59cad1343479972e0c996921d0a5dadb65d", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068", + "ecdh_share": "03eca4ff11b728e2e0f60ce6222943a6ff55b9d95f627bf9a99d084bc872d50a5b", + "dleq_proof": "8a13b3985545f72bd6e89714aeb909b3e354a842a9bb8b56cd0eded21df8a199598b31228a49e0bd7e95ce1053f7c5b28acb543a68707600e3ce89822ee32021", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 200000, + "sp_v0_info": "027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe0680361e1b1e9de5e42cb2007f7ca54b9e0d57ed13938fad56d3f19e57513a8fce039" + } + ] + } + }, + { + "description": "in progress: one input / two sp outputs, input 0 missing ECDH share for output 0 when PSBT_OUT_SCRIPT field is not set", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQEBBQECAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR9ADQMAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdA1LXjEE5ADKtkYFqaX/HQNjrkJzwTXCIUmSwUfI4XiXsIQMJH9SAORd/RNe3npYWiEnGLrP4eADEZ2fFCo84+Y79FSIeA1LXjEE5ADKtkYFqaX/HQNjrkJzwTXCIUmSwUfI4XiXsQL4acjrvVeiYG5lZmpvBKzj4froBWMxwZokPImnK3kY7dzQal9KScZZNAcAkHMMi/VOQwpuZFg+CbzKTZYmMvwIAAQMIkF8BAAAAAAABCUIDZR0sBz/LAqTYLdpT8dUB13oDUGZnmFRkc7ISa2zRp94D8nDgpWMhg7arAQdOh35oANsA9cfF0vr7to+cKNSFRSQAAQMIkF8BAAAAAAABBCJRIENYHfbIefmqrcU2YN6xVrnxIr0a7KczIDvOh/zx84buAQlCA1LXjEE5ADKtkYFqaX/HQNjrkJzwTXCIUmSwUfI4XiXsAuDEwYe3IV+ZfISFp1tFkbbChHe5WhEjuZaSLTFUhgv4AA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 200000, + "witness_utxo": "400d030000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": false + } + ], + "sp_proofs": [ + { + "scan_key": "0352d78c41390032ad91816a697fc740d8eb909cf04d70885264b051f2385e25ec", + "ecdh_share": "03091fd48039177f44d7b79e96168849c62eb3f87800c46767c50a8f38f98efd15", + "dleq_proof": "be1a723aef55e8981b99599a9bc12b38f87eba0158cc7066890f2269cade463b77341a97d29271964d01c0241cc322fd5390c29b99160f826f329365898cbf02", + "input_index": 0 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 90000, + "sp_v0_info": "03651d2c073fcb02a4d82dda53f1d501d77a0350666798546473b2126b6cd1a7de03f270e0a5632183b6ab01074e877e6800db00f5c7c5d2fafbb68f9c28d4854524" + }, + { + "output_index": 1, + "amount": 90000, + "sp_v0_info": "0352d78c41390032ad91816a697fc740d8eb909cf04d70885264b051f2385e25ec02e0c4c187b7215f997c8485a75b4591b6c28477b95a1123b996922d3154860bf8", + "script": "512043581df6c879f9aaadc53660deb156b9f122bd1aeca733203bce87fcf1f386ee" + } + ] + } + }, + { + "description": "in progress: large PSBT with nine mixed inputs / six outputs - some inputs signed", + "psbt": "cHNidP8B+wQCAAAAAQIEAgAAAAEEAQkBBQEGAQYBAAABDiAYpxdmOwurFLEqGncTI/8eQHndUy5d0T4o6hCBxwCYSgEPBAAAAAABAR+ghgEAAAAAABYAFCKactNKZFvTSWu79Qu7gckGP0+UIgICyBe7dSGvw16pbzv7Jw5utQ3f+lVgYnuWH+wA8pllCL9IMEUCIQCLNThMVlDzsjgXC0oPt/Sorr8CiDHHC50NMY3FUceJxgIgMZmwKWN19OdCfS+4KMjLfkHblItuYaaIKCeY8x+RIogBAQMEAQAAACIGAsgXu3Uhr8NeqW87+ycObrUN3/pVYGJ7lh/sAPKZZQi/CAAAAIAAAAAAARAE/v///yIdAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQIQP70XPZxI1YxQIkJtBSeLyhe5qOIoJmaQGhsAueBLeZSSIdAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQMKGLm7QZm4kzDrF/juWseG4IU9zS88zfF0S5fOTt4FPiIdA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsIQNzctF59PYo2oLMvU5CF72wgJ8UF8mr6qNfqAXUitZVTCIeAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQQMBdpBBIfPrsN4/r5q9QBeOubIYjaBiw5VshQBfrUHPt70AXlXW13NqpBjSRrIuD/QYk1Bd6TS3UmUxGQNa4zN4iHgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKEAAE4PUZhvocl7ykEj0jJEfifXzQxfXwMCsxg72XxtSv/96ltgzm2Jd+Dq6tXqic+3NmjXHlNrLschhFxuyZwdfIh4DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGxApoctTrEZ/ir3CVP3UqIiNrVKO1f3DlEfEL2LQv9Wtaz2NlCTxIDqnxN3Z4SUW1kxisKSfZFariRM+2g/QYijpwABDiC+nWXIORX7CSSKu9/KjKPu5s41DFVPZejVJuXfUnq1IgEPBAAAAAABAR+ghgEAAAAAABYAFBOTUBF8V3lBqYWAK7nt78ETkyvHIgIC9bWfpeSSIh6/Vbp4rUQmBb6ulRZroeujJQ0Luqx+LtxHMEQCIGBe6cE9y5EoaPHeTsHHUaxLRis49eXxBj+EHthVABbpAiBHqaafS5jOmNbyz3jQjoWlVWhRC3tc+65oAn9FjH5+BQEBAwQBAAAAIgYC9bWfpeSSIh6/Vbp4rUQmBb6ulRZroeujJQ0Luqx+LtwIAAAAgAEAAAABEAT+////Ih0CqfH6wsxFdoTzPekAc/4g0iyUfJIWO4YrFNCrod9HFRAhA6aomxuYaplVM+F77WRQC1QbsNc5Bx7+KuK377CgymvPIh0DM2HT/3LzTmtOenvvluW4FR5mqXmtznU8OcWDy+V2bSghAvaue4qsmg+cnwTqtIQGIXLfLjjZ8vbsMbQEu1ooaqvwIh0DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGwhArHT7S+kvDFqCOsUhfp17X3VTKeprhjQxKF9qT8ww4XVIh4CqfH6wsxFdoTzPekAc/4g0iyUfJIWO4YrFNCrod9HFRBAw0pqKUC76ac1bBzG/+NY3URB21iJfLqZwhLrdShefaHsoRx5LJejgEkzRasiTenZTCs27oUmriNSgXGjIx3nMyIeAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oQDNKztgQbrqkKH2spWzKJnlfLSpzl4TPa3NNozcHV/SZ4SDA5AvkBDbNdMTvJpBXBvUvp/pujnZFHoWDeDBRqEMiHgOO0yIOczRPkeLTM5Hj4Wkds52vBuQjfhl+0NAHvycobEDYQaNzrSiWuXK4z+oWNP8Uow+A2Ez9bvGIkzl7jzRsgFGcmxYaciRNDptk5UNsmM6d1OF6Byj8mXtce885zstoAAEOINBBx0LKNkRy0aGMeo6uB+bpBKnqG4XxmFJfuD7yduNDAQ8EAAAAAAEBH6CGAQAAAAAAFgAUIwf8d0yhdLQq2oFLwubkRP89gwwiAgJlXvXa8F0o0cpzuo4hEwDbK3BBZzcYPg+ow1XuTBy6xkcwRAIgKTOk1x6k+7U8mzjYjLK+bqIQKQBrzEx5rJgFGKrIYxkCICj9GcotjGhqD9CjsK6gmpqVgMw4oisFIgS+RXXwNa0/AQEDBAEAAAAiBgJlXvXa8F0o0cpzuo4hEwDbK3BBZzcYPg+ow1XuTBy6xggAAACAAgAAAAEQBP7///8iHQKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVECED33Bv8m1nL+UMb1aiyh5xdOdQVckgu9M34afWvygm9twiHQMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKCEDv7hiU765b1i4xGnkQs/Pn3ilQTqVQjvo5oZ2Ty+nvT8iHQOO0yIOczRPkeLTM5Hj4Wkds52vBuQjfhl+0NAHvycobCEDhgSqjP8wnd30mx82b4nCaWFlh/xB22CFQPlDFTb2ZG8iHgKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVEED9LowiubZy07ueJtS22i3D8SVFWpaf2OKywbp5EHatcbOMAfkhjD1ndv2JXS8wZ0hJltheJoOQwrQz5sSyGJHxIh4DM2HT/3LzTmtOenvvluW4FR5mqXmtznU8OcWDy+V2bShAUt/78AlfKbHeJMnWjVSdHFS85pAEVP43/RD1yQPf7oX3utinKtqOUs2QkvUdnpC1p+TzbKBN+Y53+60TRzBbxyIeA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsQJPh5dSIi8HP4C7s+p3qFE6thPI6EPwaMBEDweVjk1J7Y0F8bEpa9FNZ/4tuF5r3OXjk1i6GLu3Soob1J45Ms0AAAQ4gUiYs1dQCvzJhbQf8Ayhqj3/nivesQR3/9BZV3cEBHm8BDwQAAAAAAQEfoIYBAAAAAAAWABRwGLzpZSiNi/DM8+DsSLbx27/lSCICAwOg9FKNJk6OlSVmNLlEFOJ0j3KWJwT+V+iwosxE0jg9SDBFAiEA2yW73RvPhQMJZj64j7uw62ETCVeF1/bVM42du8cFAycCIHmV0Cb1bqmeBz39CnlsbnT8Ye0I7NJGcM7zQNrVCueaAQEDBAEAAAAiBgMDoPRSjSZOjpUlZjS5RBTidI9ylicE/lfosKLMRNI4PQgAAACAAwAAAAEQBP7///8iHQKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVECEDgCmZjHPCsTnbdVV+wwEexl7NISRHljAGVxhSo+kiicwiHQMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKCECmAuscmThS/ggzENbaaeN9ybBd+sCbxuBtS3oEScvWOsiHQOO0yIOczRPkeLTM5Hj4Wkds52vBuQjfhl+0NAHvycobCEDon2fH6U+Q/cINaaZCVS3ijX3RwZve4K0qeqC6LteBqYiHgKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVEEA4HOPBrJ/H9qUVNkViUphHaOj70BQHAjn0yxOxZ5PHTMXtZ3yKfqzBc+mI01Oj8onH4hQdt6S0FFxUmeyDfzauIh4DM2HT/3LzTmtOenvvluW4FR5mqXmtznU8OcWDy+V2bShAOOA0Y7ZFDb0FEGWoEqbyYxCH95UeRqyE5m2oEXVXF8CTGA5NSYJ7jAl8pwk/6S05P5Wl162EmkGALcTkVYq96SIeA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsQHMqoJV6nbLHWeOayl0BZ+8T0qNKQ8gS6cydcS1Fj3sihdp1EesW9h+WANIL5OlN/dK8OuN/2UP8NJR9/D/cZ5cAAQ4g4pjq8k/hymi0Hktrg8w9kc7ZnG541bmSz7FXcbVFcGQBDwQAAAAAAQEfoIYBAAAAAAAWABSTMrsCDLA9SetNBHEsHMCOKwbY2CICA6iRG+taSP4RnLAVCIzww6EYzwJnckSJctG3DHZKxZQSRzBEAiBr1hL2RzkhB3F8V6+V7oRzl08sMjurcHnoGRbsuQIcjgIgWlToLfcW0eQKehOGkN9w6tSjzILOEImugKMj+1/ui1UBAQMEAQAAACIGA6iRG+taSP4RnLAVCIzww6EYzwJnckSJctG3DHZKxZQSCAAAAIAEAAAAARAE/v///yIdAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQIQK+9HOMbzJ/FySxQHmIlZzfK9GSEIlL/QQ1NbYROCcIGCIdAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQJ+t8HEr81+Yqtq09K6JzOjdFIgZB+RkD6/57zF29xrXSIdA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsIQPKiLzbbR9SkekMddPaYBqL0pmR6rv3+UsvOUg+OFjJoSIeAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQQApYSScQKCC3tsrWvPsDGiOjcxKth9I/HLWoNZvi1f85tqKi/jfNDVPuNH8T5rNSV2my54nwaZy5hViOrC2vEN0iHgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKEB3sVcjNtm9Ti9zQR649qliNaEw9ETL6wekifRHkh43SJ1x/T2yigu6o1uIw3t1eezb9s4WOHANuB38hr95zRvnIh4DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGxAXQ3oon8NUogCzzNUoa9Qe88P7UGdpkHJwIB811Tgevh5C1EccF1fv2MwWrIq/gSX+s8HC1r+r87jppAFR3Z3VgABDiAXMIM+y1vgE2LXFJUiYESEIW4cexjPMT0c8VQ++F3ztgEPBAAAAAABASughgEAAAAAACJRINFVcTxSDOLYJ2FkIV1lmrYjxfcwb4ZSwXVuJoM+iG4BAQMEAQAAAAEQBP7///8BFyDRVXE8Ugzi2CdhZCFdZZq2I8X3MG+GUsF1biaDPohuASIdAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQIQJPKhWksVprePCmMvx/3xEpKfZL1N0U9npjz0+48yWiBCIdAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQOAK2mzYNpFhVErHPlnsYQBAibXXOW3Yud6ASi8sGjMuyIdA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsIQJAdhVQXsveqIExJQTu9vRoGiYxbvc9M1N6f9eT9GWOqSIeAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQQI8iPeJ5ht2HkqA9nVtmIyPoaXgw66u2c2OIH+cshkgq5hoJJo0IO4NUz8K601wUjuspGvYi3UYGNGBjMq8bgpciHgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKEAQOuBcEOQymDdsP+4dxfDQQ4n08gr0k0bUjq0yTWFdbyj8oKMYRFEw6laoYgGbLeDCY32zxBjAsHS1hUiuiyDJIh4DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGxA8FWZBzhUTQmbFZKQ5/VELLkSJiYBdKqdjByDELfLAc4rlMxdK4/fgWZ+Avt5bbDlwnh33g1BoqDnCbAURbrylgABDiDpNTLg7aOcTTnaLT+R0UUpvENfwu+5GW4l+6rEtOibgQEPBAAAAAABASughgEAAAAAACJRINAOFftian7+6FxbzmdtY2tgSJFOmjSs4LTLxEvJSAd6AQMEAQAAAAEQBP7///8BFyDQDhX7Ymp+/uhcW85nbWNrYEiRTpo0rOC0y8RLyUgHeiIdAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQIQIVP83+lq5wNj/RVCZG0wnCEyuDJVQOPdf4jjf5eWB5piIdAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQIPqjoMMa52ledpVG4wqngqvTH7aBuXChOqNcwh39E4ACIdA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsIQPzE/j0desB487LwOqsL3JtKQMsb/fDrWzDv4uxAYZSfCIeAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQQEMg6fYP08Km9zeqzlRsI7DH126EFZwK0onB30wPilAOUSCfKW+dfSI6HSkr1B0mb6rLG+hDvs9LcG6xXbpwKAYiHgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKEDkB0puyJPquAbV35p07SV+eF/zeGQ9uqeTPHgTp7X2yQyoK2JTnzmzjCxy/lz4HCKl5kH2c2RPbEhrPbV3RkRjIh4DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGxAOynhLPaT8nITn3RFLyvMwrzQdDLc6PsQc1TBRYoSpHyFRNLWyh4zvOBXi2UIz4pZNGc/n8Q1pfhKIqVLWty8/AABDiC2Nou8Mwzd/p4lUO2338Fa50ysm8x9ln/oHm8n+Fl7HQEPBAAAAAABAFMCAAAAAf6hTz0WmysEANsUmWIhsxGCu56GyhaekbnHvZP5xz6IAAAAAAD/////AaCGAQAAAAAAF6kUDfEBNX9z5mWFAM9W86Ey17uK+92HAAAAAAEBIKCGAQAAAAAAF6kUDfEBNX9z5mWFAM9W86Ey17uK+92HIgICBQDxZ89mnn2sOeauzaHyjGa3jscJfu/vJdVPe+eF5/ZHMEQCIA134+usWENCsMr5fopp9YcHxKsAzK2+hsRKoYe1Zfv/AiBubgVutYtmJeL++r0axbRapqEto5ypCKEaIcY396JcMAEBAwQBAAAAAQQWABTGvIEF7G22DE2lOsNc2iBAaekOiiIGAgUA8WfPZp59rDnmrs2h8oxmt47HCX7v7yXVT3vnhef2CAAAAIAHAAAAARAE/v///yIdAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQIQL+eEHN37Rv2yypveVslVjC0bNhIWZWWQgCJOJAMUOECCIdAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oIQJvAnsjWnWTyolDxte3432S6rM5YX9/DDRBBsWN0MvGqiIdA47TIg5zNE+R4tMzkePhaR2zna8G5CN+GX7Q0Ae/JyhsIQIGEUUMbBlE7N7LePYYFsdn+6Q2jyH36bFpd5x28jz0zCIeAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQQIgHJVFmLD7r7lRfGIuNr8aClzovIY58oH/I5C/1ehE0YwRJ/XAxd9ehLQyc7QJIRNwbng7K1nWH3VxHlDGzahoiHgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKEAWtYFI97r/eD5DEPGFUD/pJBfbpDuWbIOiGEMiyx8gQky50ECuahrqTnREvceGJYqXVkTOGgGVVkwrn8PGBqKSIh4DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGxASrNr3WnrHNDUsK6uQD3OTbUijFujopHccl3iM4LRc20mXXvDP4NpClylYJcZ8Z6BlS1MjV4oj9DLjGQREEuAowABDiADvOulN51z2ur+Z3lysD3PfvMLfxDqqDrQS8C/ANiy9wEPBAAAAAABAFUCAAAAASo0rBsNt3qBmMHikALMR7v81u2DCWAwTSyaOrVISGtdAAAAAAD/////AaCGAQAAAAAAGXapFFab8xHmijg3Z7Q/gwQt+r5gCT7eiKwAAAAAIgIDGZ0vFV8y67FnrrDMym5a+3NAiY3xt6uZNlAPbUORkCpHMEQCIGIn/SRuyIcF32NUW0i+PRLD1KVITUtOxDRAmsuEETxaAiA8y8m0eI77f7hg2oVYqUx+TEJa11XoaOLY8Fz7RhJflwEBAwQBAAAAIgYDGZ0vFV8y67FnrrDMym5a+3NAiY3xt6uZNlAPbUORkCoIAAAAgAgAAAABEAT+////Ih0CqfH6wsxFdoTzPekAc/4g0iyUfJIWO4YrFNCrod9HFRAhAvPBdbCLIHY3YWqnTme6rBoYB8lLznCAD3b1sXtSwAgtIh0DM2HT/3LzTmtOenvvluW4FR5mqXmtznU8OcWDy+V2bSghAvoc7ZMlmKJ/ooQQuVXqZ0ebWkAztp8smeTiZ2e7oKVWIh0DjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGwhAwDg/RcQ3FatfBc6qRQdcxV3AJvD3/y6hXakjnerNNlwIh4CqfH6wsxFdoTzPekAc/4g0iyUfJIWO4YrFNCrod9HFRBAmSJWWP7eeowkN+s4Ib3J03AKvIlui23O+E4pNHt4vCsD5tozg25PQHZzkMNmRM4Bazax1UaWNUD6UdCwQH221yIeAzNh0/9y805rTnp775bluBUeZql5rc51PDnFg8vldm0oQKCUjFF9cJdsDjnKZ6vxnnIXBA2+Fyu5ZTQTPm/6OUHBZZQW9UBmK3Vh77cAZLTXGpsXtjvFY/Yw20Qi8nPg3xIiHgOO0yIOczRPkeLTM5Hj4Wkds52vBuQjfhl+0NAHvycobEDuCqplynPMxUezHCt+0q4xpPTM2upz8q3SRjDWCO3prqXYCGiBhuvKQrh376sT815g8hDU9iBgL4UPwHMcIB6RAAEDCDB1AAAAAAAAAQQiUSAa+l2IGIzvm5Gz8QArEeD56UrTmBl4xNEqBgVWTAgSMwEJQgMzYdP/cvNOa056e++W5bgVHmapea3OdTw5xYPL5XZtKAMSZlgePoQcu5TeYupzVYOiMsTIiPpu3vLsL4cHifwd1AABAwgwdQAAAAAAAAEEIlEgyr9+Sg3kJOybut1/ckLM52c+FwH0nWoZqXBV2jMrEyQBCUIDM2HT/3LzTmtOenvvluW4FR5mqXmtznU8OcWDy+V2bSgDEmZYHj6EHLuU3mLqc1WDojLEyIj6bt7y7C+HB4n8HdQAAQMIIE4AAAAAAAABBCJRIEqtyeClkNIeDBnLICTycUD5sZwqEoLVDqfF9F6wP9SiAQlCAqnx+sLMRXaE8z3pAHP+INIslHySFjuGKxTQq6HfRxUQAkpI124g1AvPXy4aXD/+s9wqizuI15pIbVTJ2eqo+hiOAAEDCCBOAAAAAAAAAQQiUSAvpDcA6h0o/XrU3LMys+XloNjLVCWPZmHE3gN31ygE5QEJQgKp8frCzEV2hPM96QBz/iDSLJR8khY7hisU0Kuh30cVEAJKSNduINQLz18uGlw//rPcKos7iNeaSG1UydnqqPoYjgABAwgQJwAAAAAAAAEEIlEgUZhvruMZQ2tRs4QJwbMA9DluKzLjzJoSripL4TXab24BCUIDjtMiDnM0T5Hi0zOR4+FpHbOdrwbkI34ZftDQB78nKGwD+BRzhEyddMkh/xelr0ELYzc0jXr7/3OJG8XLDLJMsMAAAQMIUMMAAAAAAAABBCJRIG/FBvgFKp9aiqm5kNXplPSV3e1tR7rETKrNrdjYmRzQAA==", + "supplementary": { + "inputs": [ + { + "input_index": 0, + "private_key": "7e31eeeb1aa2597b6d63b357541461d75ddae76b7603d24619f5ebed9e88ec31", + "public_key": "02c817bb7521afc35ea96f3bfb270e6eb50ddffa5560627b961fec00f2996508bf", + "prevout_txid": "18a717663b0bab14b12a1a771323ff1e4079dd532e5dd13e28ea1081c700984a", + "prevout_index": 0, + "prevout_scriptpubkey": "0014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "amount": 100000, + "witness_utxo": "a086010000000000160014229a72d34a645bd3496bbbf50bbb81c9063f4f94", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 1, + "private_key": "295c2eedddd8331d20b5d4cf9e69bb523ed85cb0bf35ab12e04fea66fe6d4a4a", + "public_key": "02f5b59fa5e492221ebf55ba78ad442605beae95166ba1eba3250d0bbaac7e2edc", + "prevout_txid": "be9d65c83915fb09248abbdfca8ca3eee6ce350c554f65e8d526e5df527ab522", + "prevout_index": 0, + "prevout_scriptpubkey": "0014139350117c577941a985802bb9edefc113932bc7", + "amount": 100000, + "witness_utxo": "a086010000000000160014139350117c577941a985802bb9edefc113932bc7", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 2, + "private_key": "e6db4a04e2fc29745ea1a9dcf1fed7449c8228ffe0503d8171e64126d5399800", + "public_key": "02655ef5daf05d28d1ca73ba8e211300db2b70416737183e0fa8c355ee4c1cbac6", + "prevout_txid": "d041c742ca364472d1a18c7a8eae07e6e904a9ea1b85f198525fb83ef276e343", + "prevout_index": 0, + "prevout_scriptpubkey": "00142307fc774ca174b42ada814bc2e6e444ff3d830c", + "amount": 100000, + "witness_utxo": "a0860100000000001600142307fc774ca174b42ada814bc2e6e444ff3d830c", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 3, + "private_key": "f5208ed67b0019ba4e306aff029d247931e0dfacd1fe6e69091bc6bc70a42619", + "public_key": "0303a0f4528d264e8e95256634b94414e2748f72962704fe57e8b0a2cc44d2383d", + "prevout_txid": "52262cd5d402bf32616d07fc03286a8f7fe78af7ac411dfff41655ddc1011e6f", + "prevout_index": 0, + "prevout_scriptpubkey": "00147018bce965288d8bf0ccf3e0ec48b6f1dbbfe548", + "amount": 100000, + "witness_utxo": "a0860100000000001600147018bce965288d8bf0ccf3e0ec48b6f1dbbfe548", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 4, + "private_key": "0c19c34dc1005feb5ca60104d20472f7f2f97bd4d521264618196bf71155b20a", + "public_key": "03a8911beb5a48fe119cb015088cf0c3a118cf026772448972d1b70c764ac59412", + "prevout_txid": "e298eaf24fe1ca68b41e4b6b83cc3d91ced99c6e78d5b992cfb15771b5457064", + "prevout_index": 0, + "prevout_scriptpubkey": "00149332bb020cb03d49eb4d04712c1cc08e2b06d8d8", + "amount": 100000, + "witness_utxo": "a0860100000000001600149332bb020cb03d49eb4d04712c1cc08e2b06d8d8", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 5, + "private_key": "5aaa151cbdeb566911784d166b01ce57f35993b98f96846efec1cfefcc0d8f56", + "public_key": "02d155713c520ce2d8276164215d659ab623c5f7306f8652c1756e26833e886e01", + "prevout_txid": "1730833ecb5be01362d7149522604484216e1c7b18cf313d1cf1543ef85df3b6", + "prevout_index": 0, + "prevout_scriptpubkey": "5120d155713c520ce2d8276164215d659ab623c5f7306f8652c1756e26833e886e01", + "amount": 100000, + "witness_utxo": "a086010000000000225120d155713c520ce2d8276164215d659ab623c5f7306f8652c1756e26833e886e01", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 6, + "private_key": "d92c1506167b3a00a21f4d90d7b5ccb4713c00194aa66f9c7deb162c467f9643", + "public_key": "02d00e15fb626a7efee85c5bce676d636b6048914e9a34ace0b4cbc44bc948077a", + "prevout_txid": "e93532e0eda39c4d39da2d3f91d14529bc435fc2efb9196e25fbaac4b4e89b81", + "prevout_index": 0, + "prevout_scriptpubkey": "5120d00e15fb626a7efee85c5bce676d636b6048914e9a34ace0b4cbc44bc948077a", + "amount": 100000, + "witness_utxo": "a086010000000000225120d00e15fb626a7efee85c5bce676d636b6048914e9a34ace0b4cbc44bc948077a", + "sequence": 4294967294, + "signed": false + }, + { + "input_index": 7, + "private_key": "d8bc8d0e1233a97133f3573d6dceb6736e8b6d2e4839925145022ea3efdad8f8", + "public_key": "020500f167cf669e7dac39e6aecda1f28c66b78ec7097eefef25d54f7be785e7f6", + "prevout_txid": "b6368bbc330cddfe9e2550edb7dfc15ae74cac9bcc7d967fe81e6f27f8597b1d", + "prevout_index": 0, + "prevout_scriptpubkey": "a9140df101357f73e6658500cf56f3a132d7bb8afbdd87", + "amount": 100000, + "witness_utxo": "a08601000000000017a9140df101357f73e6658500cf56f3a132d7bb8afbdd87", + "sequence": 4294967294, + "signed": true + }, + { + "input_index": 8, + "private_key": "81e30f5b5d049bdf5c5bea9c3329bf57cc8b473240182a9f2a6eee07435bd54a", + "public_key": "03199d2f155f32ebb167aeb0ccca6e5afb7340898df1b7ab9936500f6d4391902a", + "prevout_txid": "03bceba5379d73daeafe677972b03dcf7ef30b7f10eaa83ad04bc0bf00d8b2f7", + "prevout_index": 0, + "prevout_scriptpubkey": "76a914569bf311e68a383767b43f83042dfabe60093ede88ac", + "amount": 100000, + "non_witness_utxo": "02000000012a34ac1b0db77a8198c1e29002cc47bbfcd6ed830960304d2c9a3ab548486b5d0000000000ffffffff01a0860100000000001976a914569bf311e68a383767b43f83042dfabe60093ede88ac00000000", + "sequence": 4294967294, + "signed": true + } + ], + "sp_proofs": [ + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "030a18b9bb4199b89330eb17f8ee5ac786e0853dcd2f3ccdf1744b97ce4ede053e", + "dleq_proof": "001383d4661be8725ef29048f48c911f89f5f34317d7c0c0acc60ef65f1b52bfff7a96d8339b625df83abab57aa273edcd9a35c794dacbb1c861171bb267075f", + "input_index": 0 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "03fbd173d9c48d58c5022426d05278bca17b9a8e2282666901a1b00b9e04b79949", + "dleq_proof": "c05da410487cfaec378febe6af5005e3ae6c86236818b0e55b214017eb5073edef40179575b5dcdaa9063491ac8b83fd0624d4177a4d2dd4994c4640d6b8ccde", + "input_index": 0 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "037372d179f4f628da82ccbd4e4217bdb0809f1417c9abeaa35fa805d48ad6554c", + "dleq_proof": "a6872d4eb119fe2af70953f752a22236b54a3b57f70e511f10bd8b42ff56b5acf6365093c480ea9f13776784945b59318ac2927d915aae244cfb683f4188a3a7", + "input_index": 0 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "02f6ae7b8aac9a0f9c9f04eab484062172df2e38d9f2f6ec31b404bb5a286aabf0", + "dleq_proof": "334aced8106ebaa4287daca56cca26795f2d2a739784cf6b734da3370757f499e120c0e40be40436cd74c4ef26905706f52fa7fa6e8e76451e8583783051a843", + "input_index": 1 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "03a6a89b1b986a995533e17bed64500b541bb0d739071efe2ae2b7efb0a0ca6bcf", + "dleq_proof": "c34a6a2940bbe9a7356c1cc6ffe358dd4441db58897cba99c212eb75285e7da1eca11c792c97a380493345ab224de9d94c2b36ee8526ae23528171a3231de733", + "input_index": 1 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "02b1d3ed2fa4bc316a08eb1485fa75ed7dd54ca7a9ae18d0c4a17da93f30c385d5", + "dleq_proof": "d841a373ad2896b972b8cfea1634ff14a30f80d84cfd6ef18893397b8f346c80519c9b161a72244d0e9b64e5436c98ce9dd4e17a0728fc997b5c7bcf39cecb68", + "input_index": 1 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "03bfb86253beb96f58b8c469e442cfcf9f78a5413a95423be8e686764f2fa7bd3f", + "dleq_proof": "52dffbf0095f29b1de24c9d68d549d1c54bce6900454fe37fd10f5c903dfee85f7bad8a72ada8e52cd9092f51d9e90b5a7e4f36ca04df98e77fbad1347305bc7", + "input_index": 2 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "03df706ff26d672fe50c6f56a2ca1e7174e75055c920bbd337e1a7d6bf2826f6dc", + "dleq_proof": "fd2e8c22b9b672d3bb9e26d4b6da2dc3f125455a969fd8e2b2c1ba791076ad71b38c01f9218c3d6776fd895d2f3067484996d85e268390c2b433e6c4b21891f1", + "input_index": 2 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "038604aa8cff309dddf49b1f366f89c269616587fc41db608540f9431536f6646f", + "dleq_proof": "93e1e5d4888bc1cfe02eecfa9dea144ead84f23a10fc1a301103c1e56393527b63417c6c4a5af45359ff8b6e179af73978e4d62e862eedd2a286f5278e4cb340", + "input_index": 2 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "02980bac7264e14bf820cc435b69a78df726c177eb026f1b81b52de811272f58eb", + "dleq_proof": "38e03463b6450dbd051065a812a6f2631087f7951e46ac84e66da811755717c093180e4d49827b8c097ca7093fe92d393f95a5d7ad849a41802dc4e4558abde9", + "input_index": 3 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "038029998c73c2b139db75557ec3011ec65ecd212447963006571852a3e92289cc", + "dleq_proof": "381ce3c1ac9fc7f6a51536456252984768e8fbd014070239f4cb13b16793c74cc5ed677c8a7eacc173e988d353a3f289c7e2141db7a4b4145c5499ec837f36ae", + "input_index": 3 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "03a27d9f1fa53e43f70835a6990954b78a35f747066f7b82b4a9ea82e8bb5e06a6", + "dleq_proof": "732aa0957a9db2c759e39aca5d0167ef13d2a34a43c812e9cc9d712d458f7b2285da7511eb16f61f9600d20be4e94dfdd2bc3ae37fd943fc34947dfc3fdc6797", + "input_index": 3 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "027eb7c1c4afcd7e62ab6ad3d2ba2733a3745220641f91903ebfe7bcc5dbdc6b5d", + "dleq_proof": "77b1572336d9bd4e2f73411eb8f6a96235a130f444cbeb07a489f447921e37489d71fd3db28a0bbaa35b88c37b7579ecdbf6ce1638700db81dfc86bf79cd1be7", + "input_index": 4 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "02bef4738c6f327f1724b1407988959cdf2bd19210894bfd043535b61138270818", + "dleq_proof": "0a584927102820b7b6cad6bcfb031a23a37312ad87d23f1cb5a8359be2d5ff39b6a2a2fe37cd0d53ee347f13e6b3525769b2e789f0699cb985588eac2daf10dd", + "input_index": 4 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "03ca88bcdb6d1f5291e90c75d3da601a8bd29991eabbf7f94b2f39483e3858c9a1", + "dleq_proof": "5d0de8a27f0d528802cf3354a1af507bcf0fed419da641c9c0807cd754e07af8790b511c705d5fbf63305ab22afe0497facf070b5afeafcee3a6900547767756", + "input_index": 4 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "03802b69b360da4585512b1cf967b184010226d75ce5b762e77a0128bcb068ccbb", + "dleq_proof": "103ae05c10e43298376c3fee1dc5f0d04389f4f20af49346d48ead324d615d6f28fca0a318445130ea56a862019b2de0c2637db3c418c0b074b58548ae8b20c9", + "input_index": 5 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "024f2a15a4b15a6b78f0a632fc7fdf112929f64bd4dd14f67a63cf4fb8f325a204", + "dleq_proof": "8f223de27986dd8792a03d9d5b662323e8697830ebabb67363881fe72c86482ae61a09268d083b8354cfc2bad35c148eeb291af622dd460634606332af1b8297", + "input_index": 5 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "02407615505ecbdea881312504eef6f4681a26316ef73d33537a7fd793f4658ea9", + "dleq_proof": "f055990738544d099b159290e7f5442cb91226260174aa9d8c1c8310b7cb01ce2b94cc5d2b8fdf81667e02fb796db0e5c27877de0d41a2a0e709b01445baf296", + "input_index": 5 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "020faa3a0c31ae7695e769546e30aa782abd31fb681b970a13aa35cc21dfd13800", + "dleq_proof": "e4074a6ec893eab806d5df9a74ed257e785ff378643dbaa7933c7813a7b5f6c90ca82b62539f39b38c2c72fe5cf81c22a5e641f673644f6c486b3db577464463", + "input_index": 6 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "02153fcdfe96ae70363fd1542646d309c2132b8325540e3dd7f88e37f9796079a6", + "dleq_proof": "4320e9f60fd3c2a6f737aace546c23b0c7d76e84159c0ad289c1df4c0f8a500e51209f296f9d7d223a1d292bd41d266faacb1be843becf4b706eb15dba702806", + "input_index": 6 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "03f313f8f475eb01e3cecbc0eaac2f726d29032c6ff7c3ad6cc3bf8bb10186527c", + "dleq_proof": "3b29e12cf693f272139f74452f2bccc2bcd07432dce8fb107354c1458a12a47c8544d2d6ca1e33bce0578b6508cf8a5934673f9fc435a5f84a22a54b5adcbcfc", + "input_index": 6 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "026f027b235a7593ca8943c6d7b7e37d92eab339617f7f0c344106c58dd0cbc6aa", + "dleq_proof": "16b58148f7baff783e4310f185503fe92417dba43b966c83a2184322cb1f20424cb9d040ae6a1aea4e7444bdc786258a975644ce1a0195564c2b9fc3c606a292", + "input_index": 7 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "02fe7841cddfb46fdb2ca9bde56c9558c2d1b36121665659080224e24031438408", + "dleq_proof": "88072551662c3eebee545f188b8dafc682973a2f218e7ca07fc8e42ff57a1134630449fd703177d7a12d0c9ced024844dc1b9e0ecad67587dd5c479431b36a1a", + "input_index": 7 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "020611450c6c1944ecdecb78f61816c767fba4368f21f7e9b169779c76f23cf4cc", + "dleq_proof": "4ab36bdd69eb1cd0d4b0aeae403dce4db5228c5ba3a291dc725de23382d1736d265d7bc33f83690a5ca5609719f19e81952d4c8d5e288fd0cb8c6411104b80a3", + "input_index": 7 + }, + { + "scan_key": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28", + "ecdh_share": "02fa1ced932598a27fa28410b955ea67479b5a4033b69f2c99e4e26767bba0a556", + "dleq_proof": "a0948c517d70976c0e39ca67abf19e7217040dbe172bb96534133e6ffa3941c1659416f540662b7561efb70064b4d71a9b17b63bc563f630db4422f273e0df12", + "input_index": 8 + }, + { + "scan_key": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510", + "ecdh_share": "02f3c175b08b207637616aa74e67baac1a1807c94bce70800f76f5b17b52c0082d", + "dleq_proof": "99225658fede7a8c2437eb3821bdc9d3700abc896e8b6dcef84e29347b78bc2b03e6da33836e4f40767390c36644ce016b36b1d546963540fa51d0b0407db6d7", + "input_index": 8 + }, + { + "scan_key": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c", + "ecdh_share": "0300e0fd1710dc56ad7c173aa9141d731577009bc3dffcba8576a48e77ab34d970", + "dleq_proof": "ee0aaa65ca73ccc547b31c2b7ed2ae31a4f4ccdaea73f2add24630d608ede9aea5d808688186ebca42b877efab13f35e60f210d4f620602f850fc0731c201e91", + "input_index": 8 + } + ], + "outputs": [ + { + "output_index": 0, + "amount": 30000, + "sp_v0_info": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28031266581e3e841cbb94de62ea735583a232c4c888fa6edef2ec2f870789fc1dd4", + "script": "51201afa5d88188cef9b91b3f1002b11e0f9e94ad3981978c4d12a0605564c081233" + }, + { + "output_index": 1, + "amount": 30000, + "sp_v0_info": "033361d3ff72f34e6b4e7a7bef96e5b8151e66a979adce753c39c583cbe5766d28031266581e3e841cbb94de62ea735583a232c4c888fa6edef2ec2f870789fc1dd4", + "script": "5120cabf7e4a0de424ec9bbadd7f7242cce7673e1701f49d6a19a97055da332b1324" + }, + { + "output_index": 2, + "amount": 20000, + "sp_v0_info": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510024a48d76e20d40bcf5f2e1a5c3ffeb3dc2a8b3b88d79a486d54c9d9eaa8fa188e", + "script": "51204aadc9e0a590d21e0c19cb2024f27140f9b19c2a1282d50ea7c5f45eb03fd4a2" + }, + { + "output_index": 3, + "amount": 20000, + "sp_v0_info": "02a9f1fac2cc457684f33de90073fe20d22c947c92163b862b14d0aba1df471510024a48d76e20d40bcf5f2e1a5c3ffeb3dc2a8b3b88d79a486d54c9d9eaa8fa188e", + "script": "51202fa43700ea1d28fd7ad4dcb332b3e5e5a0d8cb54258f6661c4de0377d72804e5" + }, + { + "output_index": 4, + "amount": 10000, + "sp_v0_info": "038ed3220e73344f91e2d33391e3e1691db39daf06e4237e197ed0d007bf27286c03f81473844c9d74c921ff17a5af410b6337348d7afbff73891bc5cb0cb24cb0c0", + "script": "512051986faee319436b51b38409c1b300f4396e2b32e3cc9a12ae2a4be135da6f6e" + }, + { + "output_index": 5, + "amount": 50000, + "script": "51206fc506f8052a9f5a8aa9b990d5e994f495dded6d47bac44caacdadd8d8991cd0" + } + ] + } + } + ] +} diff --git a/src/psbt.c b/src/psbt.c index a82e1cdf2..0e6753abc 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -1,5 +1,6 @@ #include "internal.h" +#include #include #include #include @@ -642,6 +643,16 @@ static int psbt_output_field_verify(uint32_t field_type, return val && val_len == SHA256_LEN ? WALLY_OK : WALLY_EINVAL; case PSBT_OUT_TAP_TREE: return taproot_tree_value_verify(val, val_len); + case PSBT_OUT_SP_V0_INFO: + /* BIP-375: 33 byte scan key followed by 33 byte spend key */ + if (!val || val_len != WALLY_SILENT_PAYMENT_KEYS_LEN || + wally_ec_public_key_verify(val, EC_PUBLIC_KEY_LEN) != WALLY_OK || + wally_ec_public_key_verify(val + EC_PUBLIC_KEY_LEN, EC_PUBLIC_KEY_LEN) != WALLY_OK) + return WALLY_EINVAL; + return WALLY_OK; + case PSBT_OUT_SP_V0_LABEL: + /* BIP-375: 32 bit little endian label */ + return val && val_len == sizeof(uint32_t) ? WALLY_OK : WALLY_EINVAL; default: break; } @@ -948,6 +959,7 @@ static void psbt_inputs_free(struct wally_psbt_input *inputs, size_t num_inputs) 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) +MAP_INNER_FIELD(output, sp_v0_info, PSBT_OUT_SP_V0_INFO, psbt_fields) SET_MAP(wally_psbt_output, keypath,) ADD_KEYPATH(wally_psbt_output) ADD_TAP_KEYPATH(wally_psbt_output) @@ -2619,7 +2631,9 @@ static int pull_psbt_output(const struct wally_psbt *psbt, field_bit = field_type; } } else { - is_known = field_type <= PSBT_OUT_MAX; + is_known = field_type <= PSBT_OUT_MAX && + !(PSBT_FT(field_type) & PSBT_OUT_UNPARSED) && + !(is_pset && (PSBT_FT(field_type) & PSBT_OUT_SP_FIELDS)); if (is_known) field_bit = PSBT_FT(field_type); } @@ -2650,6 +2664,8 @@ static int pull_psbt_output(const struct wally_psbt *psbt, case PSBT_OUT_REDEEM_SCRIPT: case PSBT_OUT_WITNESS_SCRIPT: case PSBT_OUT_TAP_INTERNAL_KEY: + case PSBT_OUT_SP_V0_INFO: + case PSBT_OUT_SP_V0_LABEL: pull_varlength_buff(cursor, max, &val_p, &val_len); ret = wally_map_add_integer(&result->psbt_fields, raw_field_type, val_p, val_len); @@ -2701,12 +2717,18 @@ static int pull_psbt_output(const struct wally_psbt *psbt, mandatory &= ~PSBT_FT(PSBT_OUT_AMOUNT); } #endif /* BUILD_ELEMENTS */ + if (keyset & PSBT_FT(PSBT_OUT_SP_V0_INFO)) { + /* BIP-375: A silent payment output's script may not be computed yet */ + mandatory &= ~PSBT_FT(PSBT_OUT_SCRIPT); + } if (ret == WALLY_OK && !(flags & WALLY_PSBT_PARSE_FLAG_LOOSE)) { if (mandatory && (keyset & mandatory) != mandatory) ret = WALLY_EINVAL; /* Mandatory field is missing*/ else if (disallowed && (keyset & disallowed)) ret = WALLY_EINVAL; /* Disallowed field present */ + else if ((keyset & PSBT_OUT_SP_FIELDS) == PSBT_FT(PSBT_OUT_SP_V0_LABEL)) + ret = WALLY_EINVAL; /* Label without a silent payment code */ } #ifdef BUILD_ELEMENTS if (ret == WALLY_OK && is_pset) { @@ -3379,16 +3401,22 @@ static int push_psbt_output(const struct wally_psbt *psbt, push_psbt_map(cursor, max, PSBT_OUT_BIP32_DERIVATION, false, &output->keypaths); if (psbt->version == PSBT_2) { - if (!is_pset && (!output->has_amount || !output->script || !output->script_len)) + const bool is_sp = !is_pset && + wally_map_get_integer(&output->psbt_fields, PSBT_OUT_SP_V0_INFO); + + if (!is_pset && (!output->has_amount || + (!is_sp && (!output->script || !output->script_len)))) return WALLY_EINVAL; /* Must be provided */ if (output->has_amount) push_psbt_le64(cursor, max, PSBT_OUT_AMOUNT, false, output->amount); - /* Core/Elements always write the script; if missing its written as empty */ - push_psbt_varbuff(cursor, max, PSBT_OUT_SCRIPT, false, - output->script ? output->script : &dummy, - output->script_len); + /* Core/Elements always write the script; if missing its written as empty. + * A silent payment output's script is omitted until it is computed. */ + if (!is_sp || output->script) + push_psbt_varbuff(cursor, max, PSBT_OUT_SCRIPT, false, + output->script ? output->script : &dummy, + output->script_len); } if ((ret = push_varbuff_from_map(cursor, max, PSBT_OUT_TAP_INTERNAL_KEY, @@ -3411,6 +3439,20 @@ static int push_psbt_output(const struct wally_psbt *psbt, return ret; } + if (!is_pset && psbt->version == PSBT_2) { + if (wally_map_get_integer(&output->psbt_fields, PSBT_OUT_SP_V0_LABEL) && + !wally_map_get_integer(&output->psbt_fields, PSBT_OUT_SP_V0_INFO)) + return WALLY_EINVAL; /* Label without a silent payment code */ + if ((ret = push_varbuff_from_map(cursor, max, PSBT_OUT_SP_V0_INFO, + PSBT_OUT_SP_V0_INFO, + false, &output->psbt_fields)) != WALLY_OK) + return ret; + if ((ret = push_varbuff_from_map(cursor, max, PSBT_OUT_SP_V0_LABEL, + PSBT_OUT_SP_V0_LABEL, + false, &output->psbt_fields)) != WALLY_OK) + return ret; + } + #ifdef BUILD_ELEMENTS if (is_pset && psbt->version == PSBT_2) { uint32_t ft; @@ -4200,6 +4242,8 @@ static int psbt_build_output(const struct wally_psbt_output *src, } if (!src->has_amount) return WALLY_EINVAL; + if (!src->script && wally_map_get_integer(&src->psbt_fields, PSBT_OUT_SP_V0_INFO)) + return WALLY_EINVAL; /* Silent payment output script not computed yet */ return wally_tx_add_raw_output(tx, src->amount, src->script, src->script_len, 0); } @@ -4288,9 +4332,14 @@ static int psbt_v2_to_v0(struct wally_psbt *psbt) { size_t i; bool is_pset; - int ret = psbt_build_tx(psbt, &psbt->tx, &is_pset, false); + int ret; - if (ret != WALLY_OK) + /* BIP-375 fields are v2 only, and v0 could not keep a code with its output */ + for (i = 0; i < psbt->num_outputs; ++i) + if (wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO)) + return WALLY_EINVAL; + + if ((ret = psbt_build_tx(psbt, &psbt->tx, &is_pset, false)) != WALLY_OK) return ret; for (i = 0; i < psbt->num_inputs; ++i) { diff --git a/src/psbt_io.h b/src/psbt_io.h index 321ed5692..94b3f454f 100644 --- a/src/psbt_io.h +++ b/src/psbt_io.h @@ -224,7 +224,17 @@ #define PSBT_OUT_TAP_INTERNAL_KEY 0x05 #define PSBT_OUT_TAP_TREE 0x06 #define PSBT_OUT_TAP_BIP32_DERIVATION 0x07 -#define PSBT_OUT_MAX PSBT_OUT_TAP_BIP32_DERIVATION +#define PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS 0x08 +#define PSBT_OUT_SP_V0_INFO 0x09 +#define PSBT_OUT_SP_V0_LABEL 0x0a +#define PSBT_OUT_MAX PSBT_OUT_SP_V0_LABEL + +/* Output PSBT fields in range that we do not parse; kept as unknowns */ +#define PSBT_OUT_UNPARSED PSBT_FT(PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS) + +/* BIP-375 silent payment output fields (not used in PSETs) */ +#define PSBT_OUT_SP_FIELDS (PSBT_FT(PSBT_OUT_SP_V0_INFO) | \ + PSBT_FT(PSBT_OUT_SP_V0_LABEL)) /* Outputs: PSET */ #define PSET_OUT_UNUSED_ZERO 0x00 /* Unused, WTF */ @@ -257,6 +267,7 @@ /* Output PSBT/PSET fields that must *not* be present in v0 */ #define PSBT_OUT_DISALLOWED_V0 (PSBT_FT(PSBT_OUT_AMOUNT) | \ PSBT_FT(PSBT_OUT_SCRIPT) | \ + PSBT_OUT_SP_FIELDS | \ PSET_FT(PSET_OUT_VALUE_COMMITMENT) | \ PSET_FT(PSET_OUT_ASSET) | \ PSET_FT(PSET_OUT_ASSET_COMMITMENT) | \ diff --git a/src/swig_java/swig.i b/src/swig_java/swig.i index 2728c3978..50d52dd15 100644 --- a/src/swig_java/swig.i +++ b/src/swig_java/swig.i @@ -281,6 +281,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %apply(char *STRING, size_t LENGTH) { (const unsigned char* issuance_amount_rangeproof, size_t issuance_amount_rangeproof_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* iv, size_t iv_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* key, size_t key_len) }; +%apply(char *STRING, size_t LENGTH) { (const unsigned char* keys, size_t keys_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* label, size_t label_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* mainchain_script, size_t mainchain_script_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* merkle_hashes, size_t merkle_hashes_len) }; @@ -1402,6 +1403,10 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %ignore wally_psbt_output_get_blinding_status; %ignore wally_psbt_output_set_taproot_internal_key; %ignore wally_tx_elements_input_is_pegin; +%ignore wally_psbt_output_get_sp_v0_info; +%ignore wally_psbt_output_get_sp_v0_info_len; +%ignore wally_psbt_output_set_sp_v0_info; +%ignore wally_psbt_output_clear_sp_v0_info; %include "../include/wally_core.h" %include "../include/wally_address.h" diff --git a/src/swig_python/python_extra.py_in b/src/swig_python/python_extra.py_in index 8c33672bd..e1bdad1e1 100644 --- a/src/swig_python/python_extra.py_in +++ b/src/swig_python/python_extra.py_in @@ -223,6 +223,7 @@ psbt_get_output_taproot_internal_key = _wrap_bin(psbt_get_output_taproot_interna 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 +psbt_output_get_sp_v0_info = _wrap_bin(psbt_output_get_sp_v0_info, psbt_output_get_sp_v0_info_len) psbt_to_bytes = _wrap_bin(psbt_to_bytes, psbt_get_length) ripemd160 = _wrap_bin(ripemd160, RIPEMD160_LEN) script_push_from_bytes = _wrap_bin(script_push_from_bytes, script_push_from_bytes_len, resize=True) diff --git a/src/swig_python/swig.i b/src/swig_python/swig.i index 77ced5827..fca531f72 100644 --- a/src/swig_python/swig.i +++ b/src/swig_python/swig.i @@ -361,6 +361,7 @@ static void destroy_words(PyObject *obj) { (void)obj; } %pybuffer_nullable_binary(const unsigned char* issuance_amount_rangeproof, size_t issuance_amount_rangeproof_len); %pybuffer_nullable_binary(const unsigned char* iv, size_t iv_len); %pybuffer_nullable_binary(const unsigned char* key, size_t key_len); +%pybuffer_nullable_binary(const unsigned char* keys, size_t keys_len); %pybuffer_nullable_binary(const unsigned char* label, size_t label_len); %pybuffer_nullable_binary(const unsigned char* mainchain_script, size_t mainchain_script_len); %pybuffer_nullable_binary(const unsigned char* merkle_hashes, size_t merkle_hashes_len); diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 6b684b465..ebcbec5f3 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -5,6 +5,8 @@ FLAG_GRIND_R = 0x4 MOD_NONE = 0 INIT_PSET = 1 +WALLY_PSBT_EXTRACT_NON_FINAL = 0x1 +WALLY_SILENT_PAYMENT_KEYS_LEN = 66 with open(root_dir + 'src/data/psbt.json', 'r') as f: JSON = json.load(f) @@ -411,6 +413,139 @@ def test_taproot_tree(self): self.assertEqual(ret, WALLY_EINVAL) wally_psbt_free(psbt) + def test_silent_payment_outputs(self): + """Test BIP-375 silent payment output fields""" + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + + num_unresolved = 0 + for case in cases['valid']: + psbt = self.parse_base64(case['psbt']) + b64 = self.to_base64(psbt) + psbt2 = self.parse_base64(b64) + self.assertEqual(self.to_base64(psbt2), b64) + wally_psbt_free(psbt2) + unresolved = False + for i in range(psbt.contents.num_outputs): + out = byref(psbt.contents.outputs[i]) + ret, info_len = wally_psbt_output_get_sp_v0_info_len(out) + self.assertEqual(ret, WALLY_OK) + self.assertIn(info_len, [0, WALLY_SILENT_PAYMENT_KEYS_LEN]) + if info_len and not psbt.contents.outputs[i].script: + unresolved = True + if unresolved: + # No transaction can be built until every script is computed + num_unresolved += 1 + tx = pointer(wally_tx()) + ret = wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx) + self.assertEqual(ret, WALLY_EINVAL) + wally_psbt_free(psbt) + self.assertEqual(num_unresolved, 3) + + invalid = [ + 'psbt structure: missing PSBT_OUT_SP_V0_INFO field when PSBT_OUT_SP_V0_LABEL set', + 'psbt structure: incorrect byte length for PSBT_OUT_SP_V0_LABEL field', + 'psbt structure: incorrect byte length for PSBT_OUT_SP_V0_INFO field', + 'psbt structure: missing PSBT_OUT_SCRIPT field when sending to non-sp output', + ] + for case in cases['invalid']: + if case['description'] in invalid: + self.parse_base64(case['psbt'], WALLY_EINVAL) + invalid.remove(case['description']) + self.assertEqual(invalid, []) + + # An output may omit its script only if it has a silent payment code + psbt = pointer(wally_psbt()) + self.assertEqual(WALLY_OK, wally_psbt_init_alloc(2, 0, 1, 0, 0, psbt)) + tx_out = pointer(wally_tx_output()) + ret = wally_tx_output_init_alloc(1000, b'\x51', 1, tx_out) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(WALLY_OK, wally_psbt_add_tx_output_at(psbt, 0, 0, tx_out)) + out = byref(psbt.contents.outputs[0]) + keys = bytes.fromhex('0220bcfac5b99e04ad1a06ddfb016ee13582609d60b6291e98d01a9bc9a16c96d4' + '025cc9856d6f8375350e123978daac200c260cb5b5ae83106cab90484dcd8fcf36') + for bad in [keys[:-1], keys + b'\x00', b'\x04' + keys[1:], keys[:33] + b'\x04' + keys[34:]]: + self.assertEqual(wally_psbt_output_set_sp_v0_info(out, bad, len(bad)), WALLY_EINVAL) + self.assertEqual(wally_psbt_output_set_sp_v0_info(out, keys, len(keys)), WALLY_OK) + buf, buf_len = make_cbuffer('00' * len(keys)) + ret, written = wally_psbt_output_get_sp_v0_info(out, buf, buf_len) + self.assertEqual((ret, written, bytes(buf)), (WALLY_OK, len(keys), keys)) + + # BIP-375 fields are v2 only: a code must not survive a conversion to v0 + self.assertEqual(wally_psbt_output_set_script(out, b'\x51', 1), WALLY_OK) + self.assertEqual(wally_psbt_set_version(psbt, 0, 0), WALLY_EINVAL) + self.assertEqual(psbt.contents.version, 2) + + self.assertEqual(wally_psbt_output_set_script(out, None, 0), WALLY_OK) + b64 = self.to_base64(psbt) + psbt2 = self.parse_base64(b64) + self.assertEqual(self.to_base64(psbt2), b64) + self.assertEqual(psbt2.contents.outputs[0].script_len, 0) + wally_psbt_free(psbt2) + + self.assertEqual(wally_psbt_output_clear_sp_v0_info(out), WALLY_OK) + ret, written = wally_psbt_output_get_sp_v0_info_len(out) + self.assertEqual((ret, written), (WALLY_OK, 0)) + self.assertEqual(wally_psbt_to_base64(psbt, 0), (WALLY_EINVAL, None)) + wally_psbt_free(psbt) + + # Signing is refused while a silent payment script is missing + case = [c for c in cases['valid'] if c['description'].startswith('in progress: one P2TR')][0] + psbt = self.parse_base64(case['psbt']) + self.assertEqual(wally_psbt_sign(psbt, b'\x01' * 32, 32, 0), WALLY_EINVAL) + wally_psbt_free(psbt) + + # Clearing the code of a labelled output cannot write a lone label + case = [c for c in cases['valid'] if 'label=0 convention' in c['description']][0] + psbt = self.parse_base64(case['psbt']) + self.assertEqual(wally_psbt_output_clear_sp_v0_info(byref(psbt.contents.outputs[1])), WALLY_OK) + self.assertEqual(wally_psbt_to_base64(psbt, 0), (WALLY_EINVAL, None)) + wally_psbt_free(psbt) + + # Silent payment fields are v2 only; BIP-373 field 0x08 stays unknown + def with_output_fields(version, fields): + psbt = pointer(wally_psbt()) + self.assertEqual(wally_psbt_init_alloc(version, 0, 1, 0, 0, psbt), WALLY_OK) + if version == 0: + tx = pointer(wally_tx()) + self.assertEqual(wally_tx_init_alloc(2, 0, 0, 1, tx), WALLY_OK) + self.assertEqual(wally_tx_add_raw_output(tx, 1000, b'\x51', 1, 0), WALLY_OK) + self.assertEqual(wally_psbt_set_global_tx(psbt, tx), WALLY_OK) + wally_tx_free(tx) + else: + tx_out = pointer(wally_tx_output()) + self.assertEqual(wally_tx_output_init_alloc(1000, b'\x51', 1, tx_out), WALLY_OK) + self.assertEqual(wally_psbt_add_tx_output_at(psbt, 0, 0, tx_out), WALLY_OK) + ret, length = wally_psbt_get_length(psbt, 0) + self.assertEqual(ret, WALLY_OK) + buf, buf_len = make_cbuffer('00' * length) + self.assertEqual(wally_psbt_to_bytes(psbt, 0, buf, buf_len), (WALLY_OK, length)) + wally_psbt_free(psbt) + return bytes(buf)[:-1] + b''.join(fields) + b'\x00' # Before the last separator + + info = b'\x01\x09' + bytes([len(keys)]) + keys + label = b'\x01\x0a\x04' + bytes(4) + musig = [b'\x22\x08' + keys[:33] + bytes([len(keys)]) + keys, + b'\x22\x08' + keys[33:] + bytes([len(keys)]) + keys] + for version, fields, expected, num_unknowns in [ + (0, [info], WALLY_EINVAL, 0), + (0, [info, label], WALLY_EINVAL, 0), + (0, musig, WALLY_OK, 2), + (2, [info, label], WALLY_OK, 0), + (2, musig, WALLY_OK, 2), + ]: + raw = with_output_fields(version, fields) + psbt = pointer(wally_psbt()) + self.assertEqual(wally_psbt_from_bytes(raw, len(raw), 0, psbt), expected) + if expected == WALLY_OK: + self.assertEqual(psbt.contents.outputs[0].unknowns.num_items, num_unknowns) + b64 = self.to_base64(psbt) + psbt2 = self.parse_base64(b64) + self.assertEqual(self.to_base64(psbt2), b64) + wally_psbt_free(psbt2) + wally_psbt_free(psbt) + def test_psbt(self): """Test creating and modifying various PSBT fields""" tx = pointer(wally_tx()) diff --git a/src/test/util.py b/src/test/util.py index c7754f8bf..cb0d3fa3d 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -599,6 +599,7 @@ class wally_psbt(Structure): ('wally_psbt_output_clear_blinder_index', c_int, [POINTER(wally_psbt_output)]), ('wally_psbt_output_clear_blinding_public_key', c_int, [POINTER(wally_psbt_output)]), ('wally_psbt_output_clear_ecdh_public_key', c_int, [POINTER(wally_psbt_output)]), + ('wally_psbt_output_clear_sp_v0_info', c_int, [POINTER(wally_psbt_output)]), ('wally_psbt_output_clear_value_blinding_rangeproof', c_int, [POINTER(wally_psbt_output)]), ('wally_psbt_output_clear_value_commitment', c_int, [POINTER(wally_psbt_output)]), ('wally_psbt_output_clear_value_rangeproof', c_int, [POINTER(wally_psbt_output)]), @@ -617,6 +618,8 @@ class wally_psbt(Structure): ('wally_psbt_output_get_blinding_status', c_int, [POINTER(wally_psbt_output), c_uint32, c_size_t_p]), ('wally_psbt_output_get_ecdh_public_key', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_size_t_p]), ('wally_psbt_output_get_ecdh_public_key_len', c_int, [POINTER(wally_psbt_output), c_size_t_p]), + ('wally_psbt_output_get_sp_v0_info', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_size_t_p]), + ('wally_psbt_output_get_sp_v0_info_len', c_int, [POINTER(wally_psbt_output), c_size_t_p]), ('wally_psbt_output_get_value_blinding_rangeproof', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_size_t_p]), ('wally_psbt_output_get_value_blinding_rangeproof_len', c_int, [POINTER(wally_psbt_output), c_size_t_p]), ('wally_psbt_output_get_value_commitment', c_int, [POINTER(wally_psbt_output), c_void_p, c_size_t, c_size_t_p]), @@ -635,6 +638,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_sp_v0_info', 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]), diff --git a/src/wasm_package/src/functions.js b/src/wasm_package/src/functions.js index f2f6299b5..9b1072555 100644 --- a/src/wasm_package/src/functions.js +++ b/src/wasm_package/src/functions.js @@ -525,6 +525,7 @@ export const psbt_output_clear_asset_surjectionproof = wrap('wally_psbt_output_c export const psbt_output_clear_blinder_index = wrap('wally_psbt_output_clear_blinder_index', [T.OpaqueRef]); export const psbt_output_clear_blinding_public_key = wrap('wally_psbt_output_clear_blinding_public_key', [T.OpaqueRef]); export const psbt_output_clear_ecdh_public_key = wrap('wally_psbt_output_clear_ecdh_public_key', [T.OpaqueRef]); +export const psbt_output_clear_sp_v0_info = wrap('wally_psbt_output_clear_sp_v0_info', [T.OpaqueRef]); export const psbt_output_clear_value_blinding_rangeproof = wrap('wally_psbt_output_clear_value_blinding_rangeproof', [T.OpaqueRef]); export const psbt_output_clear_value_commitment = wrap('wally_psbt_output_clear_value_commitment', [T.OpaqueRef]); export const psbt_output_clear_value_rangeproof = wrap('wally_psbt_output_clear_value_rangeproof', [T.OpaqueRef]); @@ -537,6 +538,7 @@ export const psbt_output_get_asset_surjectionproof_len = wrap('wally_psbt_output export const psbt_output_get_blinding_public_key_len = wrap('wally_psbt_output_get_blinding_public_key_len', [T.OpaqueRef, T.DestPtr(T.Int32)]); export const psbt_output_get_blinding_status = wrap('wally_psbt_output_get_blinding_status', [T.OpaqueRef, T.Int32, T.DestPtr(T.Int32)]); export const psbt_output_get_ecdh_public_key_len = wrap('wally_psbt_output_get_ecdh_public_key_len', [T.OpaqueRef, T.DestPtr(T.Int32)]); +export const psbt_output_get_sp_v0_info_len = wrap('wally_psbt_output_get_sp_v0_info_len', [T.OpaqueRef, T.DestPtr(T.Int32)]); export const psbt_output_get_value_blinding_rangeproof_len = wrap('wally_psbt_output_get_value_blinding_rangeproof_len', [T.OpaqueRef, T.DestPtr(T.Int32)]); export const psbt_output_get_value_commitment_len = wrap('wally_psbt_output_get_value_commitment_len', [T.OpaqueRef, T.DestPtr(T.Int32)]); export const psbt_output_get_value_rangeproof_len = wrap('wally_psbt_output_get_value_rangeproof_len', [T.OpaqueRef, T.DestPtr(T.Int32)]); @@ -552,6 +554,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_sp_v0_info = wrap('wally_psbt_output_set_sp_v0_info', [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]); @@ -901,6 +904,7 @@ export const psbt_output_get_asset_commitment = wrap('wally_psbt_output_get_asse export const psbt_output_get_asset_surjectionproof = wrap('wally_psbt_output_get_asset_surjectionproof', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_asset_surjectionproof_len, false)]); export const psbt_output_get_blinding_public_key = wrap('wally_psbt_output_get_blinding_public_key', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_blinding_public_key_len, false)]); export const psbt_output_get_ecdh_public_key = wrap('wally_psbt_output_get_ecdh_public_key', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_ecdh_public_key_len, false)]); +export const psbt_output_get_sp_v0_info = wrap('wally_psbt_output_get_sp_v0_info', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_sp_v0_info_len, false)]); export const psbt_output_get_value_blinding_rangeproof = wrap('wally_psbt_output_get_value_blinding_rangeproof', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_value_blinding_rangeproof_len, false)]); export const psbt_output_get_value_commitment = wrap('wally_psbt_output_get_value_commitment', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_value_commitment_len, false)]); export const psbt_output_get_value_rangeproof = wrap('wally_psbt_output_get_value_rangeproof', [T.OpaqueRef, T.DestPtrVarLen(T.Bytes, psbt_output_get_value_rangeproof_len, false)]); diff --git a/src/wasm_package/src/index.d.ts b/src/wasm_package/src/index.d.ts index c92c1a12f..84a263970 100644 --- a/src/wasm_package/src/index.d.ts +++ b/src/wasm_package/src/index.d.ts @@ -485,6 +485,7 @@ export function psbt_output_clear_asset_surjectionproof(output: Ref_wally_psbt_o export function psbt_output_clear_blinder_index(output: Ref_wally_psbt_output): void; export function psbt_output_clear_blinding_public_key(output: Ref_wally_psbt_output): void; export function psbt_output_clear_ecdh_public_key(output: Ref_wally_psbt_output): void; +export function psbt_output_clear_sp_v0_info(output: Ref_wally_psbt_output): void; export function psbt_output_clear_value_blinding_rangeproof(output: Ref_wally_psbt_output): void; export function psbt_output_clear_value_commitment(output: Ref_wally_psbt_output): void; export function psbt_output_clear_value_rangeproof(output: Ref_wally_psbt_output): void; @@ -497,6 +498,7 @@ export function psbt_output_get_asset_surjectionproof_len(output: Ref_wally_psbt export function psbt_output_get_blinding_public_key_len(output: Ref_wally_psbt_output): number; export function psbt_output_get_blinding_status(output: Ref_wally_psbt_output, flags: number): number; export function psbt_output_get_ecdh_public_key_len(output: Ref_wally_psbt_output): number; +export function psbt_output_get_sp_v0_info_len(output: Ref_wally_psbt_output): number; export function psbt_output_get_value_blinding_rangeproof_len(output: Ref_wally_psbt_output): number; export function psbt_output_get_value_commitment_len(output: Ref_wally_psbt_output): number; export function psbt_output_get_value_rangeproof_len(output: Ref_wally_psbt_output): number; @@ -512,6 +514,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|null): void; export function psbt_output_set_script(output: Ref_wally_psbt_output, script: Buffer|Uint8Array|null): void; +export function psbt_output_set_sp_v0_info(output: Ref_wally_psbt_output, keys: Buffer|Uint8Array|null): void; export function psbt_output_set_taproot_internal_key(output: Ref_wally_psbt_output, pub_key: Buffer|Uint8Array|null): 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|null): void; @@ -861,6 +864,7 @@ export function psbt_output_get_asset_commitment(output: Ref_wally_psbt_output): export function psbt_output_get_asset_surjectionproof(output: Ref_wally_psbt_output): Buffer; export function psbt_output_get_blinding_public_key(output: Ref_wally_psbt_output): Buffer; export function psbt_output_get_ecdh_public_key(output: Ref_wally_psbt_output): Buffer; +export function psbt_output_get_sp_v0_info(output: Ref_wally_psbt_output): Buffer; export function psbt_output_get_value_blinding_rangeproof(output: Ref_wally_psbt_output): Buffer; export function psbt_output_get_value_commitment(output: Ref_wally_psbt_output): Buffer; export function psbt_output_get_value_rangeproof(output: Ref_wally_psbt_output): Buffer; diff --git a/tools/wasm_exports.sh b/tools/wasm_exports.sh index 759b53106..ce3931759 100644 --- a/tools/wasm_exports.sh +++ b/tools/wasm_exports.sh @@ -337,13 +337,17 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_psbt_is_finalized' \ ,'_wally_psbt_is_input_finalized' \ ,'_wally_psbt_output_clear_amount' \ +,'_wally_psbt_output_clear_sp_v0_info' \ ,'_wally_psbt_output_find_keypath' \ ,'_wally_psbt_output_find_unknown' \ +,'_wally_psbt_output_get_sp_v0_info' \ +,'_wally_psbt_output_get_sp_v0_info_len' \ ,'_wally_psbt_output_keypath_add' \ ,'_wally_psbt_output_set_amount' \ ,'_wally_psbt_output_set_keypaths' \ ,'_wally_psbt_output_set_redeem_script' \ ,'_wally_psbt_output_set_script' \ +,'_wally_psbt_output_set_sp_v0_info' \ ,'_wally_psbt_output_set_taproot_internal_key' \ ,'_wally_psbt_output_set_unknowns' \ ,'_wally_psbt_output_set_witness_script' \ From 7304df7f1b4aade9a4cbe4c84febbed675e73a29 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:11:55 +0900 Subject: [PATCH 03/11] psbt: Identify silent payment PSBTs by their codes BIP-375 computes a PSBT's unique id with each silent payment output's script replaced by a zero byte and its silent payment code, so the id is the same before and after a signer computes the scripts. Without this, a PSBT with an unresolved output had no id at all, and combine, which compares ids, refused it. Build the id that way, and let combine take a computed script from either side when the other side does not have it yet. Two different computed scripts for the same output still fail to combine. --- src/psbt.c | 41 ++++++++++++++++++++++++-------- src/test/test_psbt.py | 55 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 10 deletions(-) diff --git a/src/psbt.c b/src/psbt.c index 0e6753abc..5b3599ba7 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -3876,11 +3876,19 @@ static int combine_output(struct wally_psbt_output *dst, } #endif } else { + /* A silent payment output's script may be computed on one side only; + * its code is part of the PSBT id so it is known to match */ + const bool sp_unresolved = !is_pset && (!dst->script_len || !src->script_len) && + wally_map_get_integer(&dst->psbt_fields, PSBT_OUT_SP_V0_INFO) && + wally_map_get_integer(&src->psbt_fields, PSBT_OUT_SP_V0_INFO); + /* Ensure amount, script (and asset, for elements) match */ if (dst->has_amount != src->has_amount || dst->amount != src->amount || - dst->script_len != src->script_len || - (dst->script_len && memcmp(dst->script, src->script, src->script_len))) + (!sp_unresolved && (dst->script_len != src->script_len || + (dst->script_len && memcmp(dst->script, src->script, src->script_len))))) ret = WALLY_EINVAL; /* Mismatched amount or script */ + else if (sp_unresolved && !dst->script_len && src->script_len) + ret = wally_psbt_output_set_script(dst, src->script, src->script_len); else if (is_pset) { #ifdef BUILD_ELEMENTS const struct wally_map_item *src_p = src->pset_fields.items + src_asset - 1; @@ -4186,8 +4194,11 @@ static int psbt_build_input(const struct wally_psbt_input *src, } static int psbt_build_output(const struct wally_psbt_output *src, - bool is_pset, bool unblinded, struct wally_tx *tx) + bool is_pset, bool unblinded, bool for_id, + struct wally_tx *tx) { + const struct wally_map_item *sp_info; + if (is_pset) { #ifndef BUILD_ELEMENTS (void)unblinded; @@ -4242,13 +4253,23 @@ static int psbt_build_output(const struct wally_psbt_output *src, } if (!src->has_amount) return WALLY_EINVAL; - if (!src->script && wally_map_get_integer(&src->psbt_fields, PSBT_OUT_SP_V0_INFO)) + sp_info = wally_map_get_integer(&src->psbt_fields, PSBT_OUT_SP_V0_INFO); + if (sp_info && for_id) { + /* BIP-375: Identify by the silent payment code, not the script */ + unsigned char sp_script[1 + WALLY_SILENT_PAYMENT_KEYS_LEN]; + if (sp_info->value_len != WALLY_SILENT_PAYMENT_KEYS_LEN) + return WALLY_EINVAL; + sp_script[0] = 0; /* Version 0 */ + memcpy(sp_script + 1, sp_info->value, sp_info->value_len); + return wally_tx_add_raw_output(tx, src->amount, sp_script, sizeof(sp_script), 0); + } + if (sp_info && !src->script) return WALLY_EINVAL; /* Silent payment output script not computed yet */ return wally_tx_add_raw_output(tx, src->amount, src->script, src->script_len, 0); } static int psbt_build_tx(const struct wally_psbt *psbt, struct wally_tx **tx, - bool *is_pset, bool unblinded) + bool *is_pset, bool unblinded, bool for_id) { size_t is_elements, locktime, i; int ret; @@ -4274,7 +4295,7 @@ static int psbt_build_tx(const struct wally_psbt *psbt, struct wally_tx **tx, ret = psbt_build_input(psbt->inputs + i, *is_pset, unblinded, *tx); for (i = 0; ret == WALLY_OK && i < psbt->num_outputs; ++i) - ret = psbt_build_output(psbt->outputs + i, *is_pset, unblinded, *tx); + ret = psbt_build_output(psbt->outputs + i, *is_pset, unblinded, for_id, *tx); if (ret != WALLY_OK) { wally_tx_free(*tx); @@ -4339,7 +4360,7 @@ static int psbt_v2_to_v0(struct wally_psbt *psbt) if (wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO)) return WALLY_EINVAL; - if ((ret = psbt_build_tx(psbt, &psbt->tx, &is_pset, false)) != WALLY_OK) + if ((ret = psbt_build_tx(psbt, &psbt->tx, &is_pset, false, false)) != WALLY_OK) return ret; for (i = 0; i < psbt->num_inputs; ++i) { @@ -4396,7 +4417,7 @@ int wally_psbt_get_id(const struct wally_psbt *psbt, uint32_t flags, unsigned ch if ((flags & ~PSBT_ID_ALL_FLAGS) || !bytes_out || len != WALLY_TXHASH_LEN) return WALLY_EINVAL; - if ((ret = psbt_build_tx(psbt, &tx, &is_pset, true)) == WALLY_OK) { + if ((ret = psbt_build_tx(psbt, &tx, &is_pset, true, true)) == WALLY_OK) { if (!(flags & WALLY_PSBT_ID_USE_LOCKTIME)) { /* Set locktime to 0. This is what core/Elements do, * although the specs aren't fixed to describe this yet */ @@ -4990,7 +5011,7 @@ int wally_psbt_sign_bip32(struct wally_psbt *psbt, (flags & ~EC_FLAG_GRIND_R)) return WALLY_EINVAL; - if ((ret = psbt_build_tx(psbt, &tx, &is_pset, false)) != WALLY_OK) + if ((ret = psbt_build_tx(psbt, &tx, &is_pset, false, false)) != WALLY_OK) return ret; #ifdef BUILD_ELEMENTS @@ -5454,7 +5475,7 @@ int wally_psbt_extract(const struct wally_psbt *psbt, uint32_t flags, struct wal (flags & ALL_EXTRACT_FLAGS) == ALL_EXTRACT_FLAGS) return WALLY_EINVAL; - if ((ret = psbt_build_tx(psbt, &result, &is_pset, false)) != WALLY_OK) + if ((ret = psbt_build_tx(psbt, &result, &is_pset, false, false)) != WALLY_OK) return ret; for (i = 0; for_final && i < psbt->num_inputs; ++i) { diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index ebcbec5f3..3363ded9f 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -413,6 +413,60 @@ def test_taproot_tree(self): self.assertEqual(ret, WALLY_EINVAL) wally_psbt_free(psbt) + def get_id(self, psbt): + buf, buf_len = make_cbuffer('00' * 32) + self.assertEqual(wally_psbt_get_id(psbt, 0, buf, buf_len), WALLY_OK) + return h(buf) + + def check_silent_payment_id(self, psbt): + """Check the BIP-375 id of a PSBT with unresolved silent payment outputs""" + unresolved_id = self.get_id(psbt) + + # The id does not change once the output scripts are computed + p2tr = b'\x51\x20' + b'\x01' * 32 + resolved = pointer(wally_psbt()) + self.assertEqual(wally_psbt_clone_alloc(psbt, 0, resolved), WALLY_OK) + # BIP-375 defines the id with each silent payment output's script + # replaced by a zero byte followed by its silent payment code + as_scripts = pointer(wally_psbt()) + self.assertEqual(wally_psbt_clone_alloc(psbt, 0, as_scripts), WALLY_OK) + sp_outputs = [] + for i in range(psbt.contents.num_outputs): + out = byref(psbt.contents.outputs[i]) + keys, keys_len = make_cbuffer('00' * WALLY_SILENT_PAYMENT_KEYS_LEN) + ret, written = wally_psbt_output_get_sp_v0_info(out, keys, keys_len) + self.assertEqual(ret, WALLY_OK) + if written and not psbt.contents.outputs[i].script: + sp_outputs.append(i) + out = byref(resolved.contents.outputs[i]) + self.assertEqual(wally_psbt_output_set_script(out, p2tr, len(p2tr)), WALLY_OK) + out = byref(as_scripts.contents.outputs[i]) + script = b'\x00' + bytes(keys) + self.assertEqual(wally_psbt_output_set_script(out, script, len(script)), WALLY_OK) + self.assertEqual(wally_psbt_output_clear_sp_v0_info(out), WALLY_OK) + self.assertNotEqual(sp_outputs, []) + self.assertEqual(self.get_id(resolved), unresolved_id) + self.assertEqual(self.get_id(as_scripts), unresolved_id) + + # Combining fills in the computed scripts, from either side + for dst, src in [(psbt, resolved), (resolved, psbt)]: + combined = pointer(wally_psbt()) + self.assertEqual(wally_psbt_clone_alloc(dst, 0, combined), WALLY_OK) + self.assertEqual(wally_psbt_combine(combined, src), WALLY_OK) + self.assertEqual(self.to_base64(combined), self.to_base64(resolved)) + wally_psbt_free(combined) + + # Two different computed scripts cannot be combined + other = pointer(wally_psbt()) + self.assertEqual(wally_psbt_clone_alloc(resolved, 0, other), WALLY_OK) + p2tr = b'\x51\x20' + b'\x02' * 32 + out = byref(other.contents.outputs[sp_outputs[0]]) + self.assertEqual(wally_psbt_output_set_script(out, p2tr, len(p2tr)), WALLY_OK) + self.assertEqual(self.get_id(other), unresolved_id) + self.assertEqual(wally_psbt_combine(other, resolved), WALLY_EINVAL) + for p in [resolved, as_scripts, other]: + wally_psbt_free(p) + def test_silent_payment_outputs(self): """Test BIP-375 silent payment output fields""" # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b @@ -440,6 +494,7 @@ def test_silent_payment_outputs(self): tx = pointer(wally_tx()) ret = wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx) self.assertEqual(ret, WALLY_EINVAL) + self.check_silent_payment_id(psbt) wally_psbt_free(psbt) self.assertEqual(num_unresolved, 3) From 55a5967f8dcda2340ed16d89e520eba106350a67 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 13:17:27 +0900 Subject: [PATCH 04/11] crypto: Add BIP-374 DLEQ proof generation A BIP-375 signer that computes a silent payment ECDH share also publishes a BIP-374 proof that the share came from the inputs' own private keys, so anyone holding the input public keys can check a share before relying on the output script derived from it. Add wally_dleq_proof_generate() and wally_dleq_proof_verify(). The generator defaults to the secp256k1 one, as silent payments use. Generation multiplies the private key and nonce through the constant time ECDH routine and reduces the nonce modulo the group order in constant time; verification only handles public values. Tests run every case in the BIP-374 vectors from bitcoin/bips commit afaa8566. --- include/wally.hpp | 12 ++ include/wally_crypto.h | 71 ++++++++ src/data/bip374_generate_vectors.csv | 15 ++ src/data/bip374_verify_vectors.csv | 18 ++ src/sign.c | 250 +++++++++++++++++++++++++++ src/swig_java/swig.i | 6 + src/swig_python/python_extra.py_in | 1 + src/swig_python/swig.i | 4 + src/test/test_sign.py | 69 ++++++++ src/test/util.py | 2 + src/wasm_package/src/const.js | 1 + src/wasm_package/src/functions.js | 2 + src/wasm_package/src/index.d.ts | 2 + tools/wasm_exports.sh | 2 + 14 files changed, 455 insertions(+) create mode 100644 src/data/bip374_generate_vectors.csv create mode 100644 src/data/bip374_verify_vectors.csv diff --git a/include/wally.hpp b/include/wally.hpp index ea64fe600..64527a64e 100644 --- a/include/wally.hpp +++ b/include/wally.hpp @@ -737,6 +737,18 @@ inline int descriptor_to_script_get_maximum_length(const DESCRIPTOR& descriptor, return detail::check_ret(__FUNCTION__, ret); } +template +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 +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 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()); diff --git a/include/wally_crypto.h b/include/wally_crypto.h index b5f31ff14..300632ac8 100644 --- a/include/wally_crypto.h +++ b/include/wally_crypto.h @@ -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 */ diff --git a/src/data/bip374_generate_vectors.csv b/src/data/bip374_generate_vectors.csv new file mode 100644 index 000000000..4601ee0cf --- /dev/null +++ b/src/data/bip374_generate_vectors.csv @@ -0,0 +1,15 @@ +index,point_G,scalar_a,point_B,auxrand_r,message,result_proof,comment +0,02cef38f55e78b321a1f785cb1c6e33dfcef9784c18bdc4e279801c449ccdfb88e,07ff93d43f1012a5d4a44aba55240212ed39c87b3344e46757d99f24177fc576,02dad4b35c2379ba8334c9a5dda8f6e6d5cd575a7cc9d3ca4faaac51839daaa30f,cb979b0fc8ccc7f237751e719d992fcc324b6500af33999cd54a3e5c05fb1ea4,efb07d4b382d3da1079fbf24df623ba6c2e4c764993bbfa6dd7a4fe4aaf33859,7e7e934169e0bf4706e6b29e5a621c7fe199a524744a25af80071e111c0e2e94118e730d8add118dd2ee4f7d1cc183e1b87168362d1a6f85c16d8671a3fc7a8a,Success case 1 +1,02464e351831efedb755223cabbf664f10564b4742c725c023034bc928ed339e0e,f4e9172285393c6ada994c811b3e50fc47e96421ea7e54f4a4e459528d4cf562,03fe589b0fa23f060f6d4d1e76b9b19d5bb3db0e56d39a4303913de0e706463008,75f12482b9209dae12230ea1f8bf69723a1b447d361db8f510dd9ab33556fd4c,76184ce9eea5b339ebf5304b57452c1ada1466610f0a58574d6c496798cee04b,6b4521a8363a7ebc5d95ac6ec6b64db81fcf21795187d7c4600c42b73fb4fb9870ab8d106c0fd2d292c1710e10437b20575ddb3cb32eb77a5618d94ddba600f2,Success case 2 +2,0222db2054fef98344352a13bc0304a71da7b5e9a2f7fd1f3c9f3519a3d9377fb7,589476913e763b60d5c2a5bfb39230ec669caac1b44312e9bcd2d3f4473abfef,03bc7a19970c812118f74ba659b491e00dade6096ff62d1afe032a92b8671498ed,4da1c4c4b0f9db4eb6b2e5cb648d7e8a0aa35aa5c4ec4d07f096e0e03deca366,66503623468a78cfcef47888c85e0010ecd897f441d263448bfc7a89b882ab20,12aa2aa469b3c037871a09d18ab18d3840219b1ed169f6ef9deae6d927949884a459705ae89a57522224ce3482dee00a41ba511188ae60efdeb736223eb66e7b,Success case 3 +3,03dfa65bd3711eba75fa1996a0c1d95a4419bd835304152d9aa6efa590670f2af6,24d0ed3fc189eb1b64e5dc9dd4af0f3c8c143b0c79cb5fcca0dfa08a11cc60a1,03b51081323d38fb0b75f0c1ec6755fdb79c239c327ca11269fe68ba8a878b704e,31a68d6db27f6404bbceff646ff1b26a34704a0105a36c5a845d0257cea19c9b,f2996b3766d123a949e65541baf1d89d446360d05af51bd93f0445d8c472c952,7907653d29c5722ae44510e7f2839f253450aefc833b7e0a3b38384032f847f2cf41136b2fe6a558ad125287d20c0117f2a30c4ac0c4cebbcfa1dd3a69d84200,Success case 4 +4,02b15de5a3aefcfe2473916c76e619b5800ac7250ef93a9e6e0dd1505104fc58e7,73fffa796edb72d111b5e0bbda1608f098ac98120796f971b438691e1bfb7b96,03a4692be176ff89a972de9cc407083096847b950d1cae72b947665a3d5f4c2f01,1cdfb4d7cce5e50783299896a471a44e6aa2c5e2100d6c37987c6b40503c6162,0ceb45f560f2cf6b76a139ffe2c47c5ca6d26d6a3a210e59f197413bbec040b4,02277ca5a7acfe8ae13c2db4a8f74489d0ba100ed8b082381ddb6522c4510718ab88b8dbbd785c388ade79586cf6416f3c47a79670af84abccc788a5d9f2e327,Success case 5 +5,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,c08ca8e0bb59769fc6a4e078456284e00ea34f65add988c246e1bba85824ccdc,034bccb1c570ac1f3bc42d61fe35de605b99626501ccb20297e1acbbf2d7152aa1,c8d7056abd4726eb5a0f198740af14d6c1f0c16e5d7a37eaec621b661e669ac4,,503562d36910cd2d61a4d07c8ff680265c713e63dde0dcb88e6ea3c58597bdc05b86db9af95eccc475ce2177f941c118fefed20227d4ce8ce9557cb008758de6,Success case 6 +6,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,8e641ba6bf7f64eec76005a29585a5035376375f33e331215aedfe03b8e80e7a,0231c64e3efa506fdad6aad0f6084d5f6739de7f448d7e66f9d22f842638f41d60,02a7b2e2f5a5e9b1078dbb160502a32491fe80a091e91dd92cf77b0b7d90970f,35841ca532846e1cdd23a3d107824343584f88eff580929469865eae8355ee3c,5c7b27a33210750e9de8679d9f43497cf9f12ac642cde0a1fc26443aa2fc89bf71aabf7bac89f5d8a96cbe86daba155fa74d6f3e111136179e53b04eb6d7807f,Success case 7 +7,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,cfb9a7ecc49bea4f2e2ee34c38a6f48b5cd5bd06f4e4d4ffb45905b3d26db842,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,Success case 8 +8,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0000000000000000000000000000000000000000000000000000000000000001,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,fb00c556ac6a084b9eb9b7e0f612f8bcc97c6c2602e2b1a18dfe1459c790c06a04ec11baec2cab0b791f275d9b494ecd51a3599c7a9785053aee2c255e336bd9,Success case (a=1) +9,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364140,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,c1267f0fdcd6c01c9633ea287faa4352092a68726e127bdd64668a4d4201d937b798288bf15cea9f7b25f0e7ef37d3e63fc4fede9cced31146784ed2035f2476,Success case (a=N-1 [group order - 1]) +10,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,0000000000000000000000000000000000000000000000000000000000000000,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,INVALID,Failure case (a=0) +11,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,INVALID,Failure case (a=N [group order]) +12,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364142,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,INVALID,Failure case (a=N+1 [group order + 1]) +13,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,cfb9a7ecc49bea4f2e2ee34c38a6f48b5cd5bd06f4e4d4ffb45905b3d26db842,INFINITY,d38466b77484154a3fcb3151094c1c8a845c73a3c036b3a8ebffd8ef62c9047f,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,INVALID,Failure case (B is point at infinity) diff --git a/src/data/bip374_verify_vectors.csv b/src/data/bip374_verify_vectors.csv new file mode 100644 index 000000000..1368013b0 --- /dev/null +++ b/src/data/bip374_verify_vectors.csv @@ -0,0 +1,18 @@ +index,point_G,point_A,point_B,point_C,proof,message,result_success,comment +0,02cef38f55e78b321a1f785cb1c6e33dfcef9784c18bdc4e279801c449ccdfb88e,02b540b22c2c5ef0dc886abdaad27498453d893265560bc08a187319af6f845f58,02dad4b35c2379ba8334c9a5dda8f6e6d5cd575a7cc9d3ca4faaac51839daaa30f,03fefe00951dcd0ef10b12523393c2b8113119de4fdeeab320694e96bdccd2775b,7e7e934169e0bf4706e6b29e5a621c7fe199a524744a25af80071e111c0e2e94118e730d8add118dd2ee4f7d1cc183e1b87168362d1a6f85c16d8671a3fc7a8a,efb07d4b382d3da1079fbf24df623ba6c2e4c764993bbfa6dd7a4fe4aaf33859,TRUE,Success case 1 +1,02464e351831efedb755223cabbf664f10564b4742c725c023034bc928ed339e0e,032baaf1b10845a51b551196984a91efe2adf9d41b92bec3927218e6e4ca344002,03fe589b0fa23f060f6d4d1e76b9b19d5bb3db0e56d39a4303913de0e706463008,031f59aa1df22190e00380d8c5941adf899f596593765a1251005fd24f2bf7c884,6b4521a8363a7ebc5d95ac6ec6b64db81fcf21795187d7c4600c42b73fb4fb9870ab8d106c0fd2d292c1710e10437b20575ddb3cb32eb77a5618d94ddba600f2,76184ce9eea5b339ebf5304b57452c1ada1466610f0a58574d6c496798cee04b,TRUE,Success case 2 +2,0222db2054fef98344352a13bc0304a71da7b5e9a2f7fd1f3c9f3519a3d9377fb7,026aa26fcd626f8f55295859e9f8dd1f103149dd64d77c2bbba1bcf33bb37ebaa2,03bc7a19970c812118f74ba659b491e00dade6096ff62d1afe032a92b8671498ed,035628d1a69910daef614c7cae68d71ece55c5908af2360629e25c1b7de21eeb4b,12aa2aa469b3c037871a09d18ab18d3840219b1ed169f6ef9deae6d927949884a459705ae89a57522224ce3482dee00a41ba511188ae60efdeb736223eb66e7b,66503623468a78cfcef47888c85e0010ecd897f441d263448bfc7a89b882ab20,TRUE,Success case 3 +3,03dfa65bd3711eba75fa1996a0c1d95a4419bd835304152d9aa6efa590670f2af6,031bf61ba89009ee1266c9003a72e8e07d77877678ccda7f15325aadcd64ed186b,03b51081323d38fb0b75f0c1ec6755fdb79c239c327ca11269fe68ba8a878b704e,02d1b1f37a80217ba73785babfa63251052775f9d3ca65060054033288b7a3f66b,7907653d29c5722ae44510e7f2839f253450aefc833b7e0a3b38384032f847f2cf41136b2fe6a558ad125287d20c0117f2a30c4ac0c4cebbcfa1dd3a69d84200,f2996b3766d123a949e65541baf1d89d446360d05af51bd93f0445d8c472c952,TRUE,Success case 4 +4,02b15de5a3aefcfe2473916c76e619b5800ac7250ef93a9e6e0dd1505104fc58e7,0296c8e00dda60bb5565b77371ff913091978646b58ccf218bc591f68a75232e6e,03a4692be176ff89a972de9cc407083096847b950d1cae72b947665a3d5f4c2f01,03a7a7f9527fd387c2b2ce0c76669d646c78a3b470a4b34d3a2dabafc8505ef472,02277ca5a7acfe8ae13c2db4a8f74489d0ba100ed8b082381ddb6522c4510718ab88b8dbbd785c388ade79586cf6416f3c47a79670af84abccc788a5d9f2e327,0ceb45f560f2cf6b76a139ffe2c47c5ca6d26d6a3a210e59f197413bbec040b4,TRUE,Success case 5 +5,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,02637b2c3ea8ca80b9caecc50f4134c86ae9cf7a269133e7afc71f30e3a3cda60c,034bccb1c570ac1f3bc42d61fe35de605b99626501ccb20297e1acbbf2d7152aa1,0285b826c8dd175805901906b6c9b4140a30cbcc94c6e7dcf36476038bf90d4718,503562d36910cd2d61a4d07c8ff680265c713e63dde0dcb88e6ea3c58597bdc05b86db9af95eccc475ce2177f941c118fefed20227d4ce8ce9557cb008758de6,,TRUE,Success case 6 +6,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,02983a72b4cb44d4322641a7b2001900cd6ae0908a610546c73ed126accdba0514,0231c64e3efa506fdad6aad0f6084d5f6739de7f448d7e66f9d22f842638f41d60,03af1bc14b384eda28398df6a7900e567c5b6f6613cafce5027b98be015286f71b,5c7b27a33210750e9de8679d9f43497cf9f12ac642cde0a1fc26443aa2fc89bf71aabf7bac89f5d8a96cbe86daba155fa74d6f3e111136179e53b04eb6d7807f,35841ca532846e1cdd23a3d107824343584f88eff580929469865eae8355ee3c,TRUE,Success case 7 +7,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,TRUE,Success case 8 +8,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Swapped points case 1 +9,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Swapped points case 2 +10,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Swapped points case 3 +11,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Swapped points case 4 +12,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Swapped points case 5 +13,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,78a5544afa75bf152653fe55fb76926f2f65131ff090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Tampered proof (random bit-flip) +14,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bde0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb6d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Tampered message (random bit-flip) +15,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141de0e7bfacc10ac12d36f55316ba134b6ba0b844a65ae05cad53c0b296c6639bb,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Proof with e equal to curve order +16,0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,03611410561c35dae13135e4ad8094baac9bbcf2f4e18498181a8ff8a6d43be9d9,021cb81121a00f89769903305a367ad3cc02d5b402b12c026e06ac94bde28cd608,03d9a98624c0c74fc7eebd39ed84175f80d03c774908e75ca737a0745d1c64e20a,78a5544afa75bf152653fe55fb76926f2f65131bf090972a0b0b37d310c28a6bfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141,22616bb5fb2d7c68270f305122f2a09e833239c4b1c9a04e285119fb606ac794,FALSE,Proof with s equal to curve order diff --git a/src/sign.c b/src/sign.c index 017e73b87..8a1e3ddff 100644 --- a/src/sign.c +++ b/src/sign.c @@ -1,6 +1,7 @@ #include "internal.h" #include #include "script_int.h" +#include #include #include "ccan/ccan/build_assert/build_assert.h" @@ -745,6 +746,255 @@ int wally_format_bitcoin_message(const unsigned char *bytes, size_t bytes_len, return WALLY_OK; } +/* BIP-374 DLEQ proofs */ +#define DLEQ_TAG_AUX "BIP0374/aux" +#define DLEQ_TAG_NONCE "BIP0374/nonce" +#define DLEQ_TAG_CHALLENGE "BIP0374/challenge" +#define DLEQ_HASH_MAX_LEN (6 * EC_PUBLIC_KEY_LEN + SHA256_LEN) + +/* The secp256k1 group order n */ +static const unsigned char DLEQ_N[EC_SCALAR_LEN] = { + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, + 0xba, 0xae, 0xdc, 0xe6, 0xaf, 0x48, 0xa0, 0x3b, + 0xbf, 0xd2, 0x5e, 0x8c, 0xd0, 0x36, 0x41, 0x41 +}; + +/* The secp256k1 generator G, compressed */ +static const unsigned char DLEQ_G[EC_PUBLIC_KEY_LEN] = { + 0x02, 0x79, 0xbe, 0x66, 0x7e, 0xf9, 0xdc, 0xbb, + 0xac, 0x55, 0xa0, 0x62, 0x95, 0xce, 0x87, 0x0b, + 0x07, 0x02, 0x9b, 0xfc, 0xdb, 0x2d, 0xce, 0x28, + 0xd9, 0x59, 0xf2, 0x81, 0x5b, 0x16, 0xf8, 0x17, + 0x98 +}; + +/* Reduce a 32 byte big endian integer modulo n, in constant time. + * Any 32 byte value is less than 2n, so one subtraction is enough. */ +static void dleq_scalar_reduce(unsigned char *s) +{ + unsigned char t[EC_SCALAR_LEN]; + unsigned int v, borrow = 0, mask; + int i; + + for (i = EC_SCALAR_LEN - 1; i >= 0; --i) { + v = (unsigned int)s[i] - DLEQ_N[i] - borrow; + t[i] = v & 0xff; + borrow = (v >> 8) & 1; + } + mask = borrow - 1; /* All ones if s >= n, so take s - n */ + for (i = 0; i < EC_SCALAR_LEN; ++i) + s[i] = (unsigned char)((s[i] & ~mask) | (t[i] & mask)); + wally_clear(t, sizeof(t)); +} + +/* Multiply a point by a public scalar (variable time) */ +static bool dleq_point_mul(const secp256k1_pubkey *point, const unsigned char *scalar, + secp256k1_pubkey *result) +{ + memcpy(result, point, sizeof(*result)); + return secp256k1_ec_pubkey_tweak_mul(secp_ctx(), result, scalar) != 0; +} + +/* ECDH "hash" that returns the compressed shared point itself */ +static int dleq_ecdh_point(unsigned char *output, const unsigned char *x32, + const unsigned char *y32, void *data) +{ + (void)data; + output[0] = 0x02 | (y32[31] & 1); + memcpy(output + 1, x32, 32); + return 1; +} + +/* Multiply a point by a secret scalar in constant time */ +static bool dleq_point_mul_secret(const secp256k1_pubkey *point, const unsigned char *scalar, + secp256k1_pubkey *result) +{ + unsigned char buf[EC_PUBLIC_KEY_LEN]; + bool ok = secp256k1_ecdh(secp_ctx(), buf, point, scalar, dleq_ecdh_point, NULL) && + pubkey_parse(result, buf, sizeof(buf)); + wally_clear(buf, sizeof(buf)); + return ok; +} + +/* p = s*X - e*Y */ +static bool dleq_point_sub_mul(const unsigned char *s, const secp256k1_pubkey *x, + const unsigned char *e, const secp256k1_pubkey *y, + secp256k1_pubkey *p) +{ + secp256k1_pubkey sx, ey; + const secp256k1_pubkey *points[2] = { &sx, &ey }; + + return dleq_point_mul(x, s, &sx) && dleq_point_mul(y, e, &ey) && + pubkey_negate(&ey) && pubkey_combine(p, points, 2); +} + +/* Append the compressed encoding of a point */ +static bool dleq_push_point(unsigned char **cursor, const secp256k1_pubkey *p) +{ + size_t len = EC_PUBLIC_KEY_LEN; + bool ok = pubkey_serialize(*cursor, &len, p, PUBKEY_COMPRESSED) && + len == EC_PUBLIC_KEY_LEN; + *cursor += EC_PUBLIC_KEY_LEN; + return ok; +} + +/* e = hash_challenge(A || B || C || G || R1 || R2 || m) mod n */ +static bool dleq_challenge(const secp256k1_pubkey *points[6], + const unsigned char *msg, size_t msg_len, + unsigned char *e) +{ + unsigned char buf[DLEQ_HASH_MAX_LEN], *p = buf; + bool ok = true; + size_t i; + + for (i = 0; i < 6; ++i) + ok &= dleq_push_point(&p, points[i]); + if (msg_len) { + memcpy(p, msg, msg_len); + p += msg_len; + } + ok = ok && wally_bip340_tagged_hash(buf, p - buf, DLEQ_TAG_CHALLENGE, + e, SHA256_LEN) == WALLY_OK; + if (ok) + dleq_scalar_reduce(e); + wally_clear(buf, sizeof(buf)); + return ok; +} + +static bool dleq_generator_parse(secp256k1_pubkey *g, + const unsigned char *generator, size_t generator_len) +{ + if (!generator) + return !generator_len && pubkey_parse(g, DLEQ_G, sizeof(DLEQ_G)); + return generator_len == EC_PUBLIC_KEY_LEN && + pubkey_parse(g, generator, generator_len); +} + +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) +{ + secp256k1_pubkey a, b, c, g, r1, r2; + const secp256k1_pubkey *points[6] = { &a, &b, &c, &g, &r1, &r2 }; + unsigned char e[EC_SCALAR_LEN]; + bool ok; + + if (!pub_key_a || pub_key_a_len != EC_PUBLIC_KEY_LEN || + !pub_key_b || pub_key_b_len != EC_PUBLIC_KEY_LEN || + !pub_key_c || pub_key_c_len != EC_PUBLIC_KEY_LEN || + !proof || proof_len != WALLY_DLEQ_PROOF_LEN || + (msg ? msg_len != SHA256_LEN : msg_len != 0) || flags) + return WALLY_EINVAL; + + if (!secp_ctx()) + return WALLY_ENOMEM; + + /* Zero e or s can only come from a hash collision with zero, so rejecting + * them alongside values of n or more does not lose valid proofs */ + ok = pubkey_parse(&a, pub_key_a, pub_key_a_len) && + pubkey_parse(&b, pub_key_b, pub_key_b_len) && + pubkey_parse(&c, pub_key_c, pub_key_c_len) && + dleq_generator_parse(&g, generator, generator_len) && + seckey_verify(proof) && seckey_verify(proof + EC_SCALAR_LEN) && + dleq_point_sub_mul(proof + EC_SCALAR_LEN, &g, proof, &a, &r1) && + dleq_point_sub_mul(proof + EC_SCALAR_LEN, &b, proof, &c, &r2) && + dleq_challenge(points, msg, msg_len, e) && + !memcmp(e, proof, EC_SCALAR_LEN); + + wally_clear(e, sizeof(e)); + return ok ? WALLY_OK : WALLY_EINVAL; +} + +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) +{ + secp256k1_pubkey a, b, c, g, r1, r2; + const secp256k1_pubkey *points[6] = { &a, &b, &c, &g, &r1, &r2 }; + unsigned char a_bytes[EC_PUBLIC_KEY_LEN], c_bytes[EC_PUBLIC_KEY_LEN]; + unsigned char buf[SHA256_LEN + 2 * EC_PUBLIC_KEY_LEN + SHA256_LEN], *p = buf; + unsigned char k[EC_SCALAR_LEN]; + size_t i; + bool ok; + + if (!priv_key || priv_key_len != EC_PRIVATE_KEY_LEN || + !pub_key || pub_key_len != EC_PUBLIC_KEY_LEN || + !aux_rand || aux_rand_len != SHA256_LEN || + (msg ? msg_len != SHA256_LEN : msg_len != 0) || flags || + !bytes_out || len != WALLY_DLEQ_PROOF_LEN) + return WALLY_EINVAL; + + if (!secp_ctx()) + return WALLY_ENOMEM; + + if (!seckey_verify(priv_key) || !pubkey_parse(&b, pub_key, pub_key_len) || + !dleq_generator_parse(&g, generator, generator_len)) + return WALLY_EINVAL; + + /* A = a*G, C = a*B */ + ok = dleq_point_mul_secret(&g, priv_key, &a) && + dleq_point_mul_secret(&b, priv_key, &c); + if (ok) { + p = a_bytes; + ok = dleq_push_point(&p, &a); + p = c_bytes; + ok = ok && dleq_push_point(&p, &c); + } + + /* t = a xor hash_aux(r), k = hash_nonce(t || A || C || m) mod n */ + p = buf; + ok = ok && wally_bip340_tagged_hash(aux_rand, aux_rand_len, DLEQ_TAG_AUX, + p, SHA256_LEN) == WALLY_OK; + for (i = 0; ok && i < SHA256_LEN; ++i) + p[i] ^= priv_key[i]; + p += SHA256_LEN; + memcpy(p, a_bytes, EC_PUBLIC_KEY_LEN); + p += EC_PUBLIC_KEY_LEN; + memcpy(p, c_bytes, EC_PUBLIC_KEY_LEN); + p += EC_PUBLIC_KEY_LEN; + if (msg_len) { + memcpy(p, msg, msg_len); + p += msg_len; + } + ok = ok && wally_bip340_tagged_hash(buf, p - buf, DLEQ_TAG_NONCE, + k, sizeof(k)) == WALLY_OK; + if (ok) + dleq_scalar_reduce(k); + + /* R1 = k*G, R2 = k*B; fails if k is zero */ + ok = ok && seckey_verify(k) && + dleq_point_mul_secret(&g, k, &r1) && dleq_point_mul_secret(&b, k, &r2); + + /* proof = e || s, where s = k + e*a mod n */ + ok = ok && dleq_challenge(points, msg, msg_len, bytes_out); + if (ok) { + memcpy(bytes_out + EC_SCALAR_LEN, priv_key, EC_PRIVATE_KEY_LEN); + ok = seckey_tweak_mul(bytes_out + EC_SCALAR_LEN, bytes_out) && + seckey_tweak_add(bytes_out + EC_SCALAR_LEN, k); + } + + ok = ok && wally_dleq_proof_verify(a_bytes, sizeof(a_bytes), + pub_key, pub_key_len, + c_bytes, sizeof(c_bytes), + bytes_out, len, generator, generator_len, + msg, msg_len, 0) == WALLY_OK; + + if (!ok) + wally_clear(bytes_out, len); + wally_clear_4(buf, sizeof(buf), k, sizeof(k), + &a, sizeof(a), &c, sizeof(c)); + return ok ? WALLY_OK : WALLY_ERROR; +} + #ifndef BUILD_STANDARD_SECP int wally_s2c_sig_from_bytes(const unsigned char *priv_key, size_t priv_key_len, const unsigned char *bytes, size_t bytes_len, diff --git a/src/swig_java/swig.i b/src/swig_java/swig.i index 50d52dd15..dfd8b5a7a 100644 --- a/src/swig_java/swig.i +++ b/src/swig_java/swig.i @@ -286,6 +286,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %apply(char *STRING, size_t LENGTH) { (const unsigned char* mainchain_script, size_t mainchain_script_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* merkle_hashes, size_t merkle_hashes_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* merkle_root, size_t merkle_root_len) }; +%apply(char *STRING, size_t LENGTH) { (const unsigned char* msg, size_t msg_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* nonce, size_t nonce_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* nonce_hash, size_t nonce_hash_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* offline_keys, size_t offline_keys_len) }; @@ -300,6 +301,9 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %apply(char *STRING, size_t LENGTH) { (const unsigned char* priv_key, size_t priv_key_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* proof, size_t proof_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* pub_key, size_t pub_key_len) }; +%apply(char *STRING, size_t LENGTH) { (const unsigned char* pub_key_a, size_t pub_key_a_len) }; +%apply(char *STRING, size_t LENGTH) { (const unsigned char* pub_key_b, size_t pub_key_b_len) }; +%apply(char *STRING, size_t LENGTH) { (const unsigned char* pub_key_c, size_t pub_key_c_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* rangeproof, size_t rangeproof_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* redeem_script, size_t redeem_script_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* s2c_data, size_t s2c_data_len) }; @@ -613,6 +617,8 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %returns_sarray(wally_descriptor_to_addresses); %returns_size_t(wally_descriptor_to_script); %returns_size_t(wally_descriptor_to_script_get_maximum_length); +%returns_array_(wally_dleq_proof_generate, 12, 13, WALLY_DLEQ_PROOF_LEN); +%returns_void__(wally_dleq_proof_verify); %returns_array_(wally_ec_private_key_bip341_tweak, 6, 7, EC_PRIVATE_KEY_LEN); %returns_void__(wally_ec_private_key_verify); %returns_array_(wally_ec_public_key_bip341_tweak, 6, 7, EC_PUBLIC_KEY_LEN); diff --git a/src/swig_python/python_extra.py_in b/src/swig_python/python_extra.py_in index e1bdad1e1..58fdd769f 100644 --- a/src/swig_python/python_extra.py_in +++ b/src/swig_python/python_extra.py_in @@ -159,6 +159,7 @@ descriptor_get_taproot_leaf_hash = _wrap_bin(descriptor_get_taproot_leaf_hash, S descriptor_get_taproot_leaf_script = _wrap_bin(descriptor_get_taproot_leaf_script, descriptor_get_taproot_leaf_script_len) descriptor_get_taproot_merkle_root = _wrap_bin(descriptor_get_taproot_merkle_root, SHA256_LEN) descriptor_to_script = _wrap_bin(descriptor_to_script, descriptor_to_script_get_maximum_length, resize=True) +dleq_proof_generate = _wrap_bin(dleq_proof_generate, WALLY_DLEQ_PROOF_LEN) ec_private_key_bip341_tweak = _wrap_bin(ec_private_key_bip341_tweak, EC_PRIVATE_KEY_LEN) ec_public_key_bip341_tweak = _wrap_bin(ec_public_key_bip341_tweak, EC_PUBLIC_KEY_LEN) ec_public_key_compress = _wrap_bin(ec_public_key_compress, EC_PUBLIC_KEY_LEN) diff --git a/src/swig_python/swig.i b/src/swig_python/swig.i index fca531f72..ef2561f20 100644 --- a/src/swig_python/swig.i +++ b/src/swig_python/swig.i @@ -366,6 +366,7 @@ static void destroy_words(PyObject *obj) { (void)obj; } %pybuffer_nullable_binary(const unsigned char* mainchain_script, size_t mainchain_script_len); %pybuffer_nullable_binary(const unsigned char* merkle_hashes, size_t merkle_hashes_len); %pybuffer_nullable_binary(const unsigned char* merkle_root, size_t merkle_root_len); +%pybuffer_nullable_binary(const unsigned char* msg, size_t msg_len); %pybuffer_nullable_binary(const unsigned char* nonce, size_t nonce_len); %pybuffer_nullable_binary(const unsigned char* nonce_hash, size_t nonce_hash_len); %pybuffer_nullable_binary(const unsigned char* offline_keys, size_t offline_keys_len); @@ -380,6 +381,9 @@ static void destroy_words(PyObject *obj) { (void)obj; } %pybuffer_nullable_binary(const unsigned char* priv_key, size_t priv_key_len); %pybuffer_nullable_binary(const unsigned char* proof, size_t proof_len); %pybuffer_nullable_binary(const unsigned char* pub_key, size_t pub_key_len); +%pybuffer_nullable_binary(const unsigned char* pub_key_a, size_t pub_key_a_len); +%pybuffer_nullable_binary(const unsigned char* pub_key_b, size_t pub_key_b_len); +%pybuffer_nullable_binary(const unsigned char* pub_key_c, size_t pub_key_c_len); %pybuffer_nullable_binary(const unsigned char* rangeproof, size_t rangeproof_len); %pybuffer_nullable_binary(const unsigned char* redeem_script, size_t redeem_script_len); %pybuffer_nullable_binary(const unsigned char* s2c_data, size_t s2c_data_len); diff --git a/src/test/test_sign.py b/src/test/test_sign.py index c1adfecf3..ebc3083aa 100755 --- a/src/test/test_sign.py +++ b/src/test/test_sign.py @@ -335,6 +335,75 @@ def test_recoverable_sig(self): ]: self.assertEqual(WALLY_EINVAL, wally_ec_sig_to_public_key(*args)) + def test_dleq_proof(self): + """Test BIP-374 DLEQ proofs against the BIP vectors""" + # bip-0374 test vectors from bitcoin/bips commit afaa8566 + def read_csv(name): + with open(root_dir + 'src/data/' + name, 'r') as f: + rows = [line.rstrip('\n').split(',') for line in f] + return [dict(zip(rows[0], row)) for row in rows[1:]] + + def buf(hex_str): + if not hex_str: + return None, 0 + if hex_str == 'INFINITY': + return bytes([0] * 33), 33 # Not a valid encoding + return bytes.fromhex(hex_str), len(hex_str) // 2 + + out, out_len = make_cbuffer('00' * 64) + for row in read_csv('bip374_generate_vectors.csv'): + args = [buf(row[k]) for k in ['scalar_a', 'point_B', 'auxrand_r', 'point_G', 'message']] + ret = wally_dleq_proof_generate(*[x for a in args for x in a], 0, out, out_len) + if row['result_proof'] == 'INVALID': + self.assertEqual(ret, WALLY_EINVAL, row['comment']) + else: + self.assertEqual((ret, h(out)), (WALLY_OK, utf8(row['result_proof'])), row['comment']) + + for row in read_csv('bip374_verify_vectors.csv'): + args = [buf(row[k]) for k in ['point_A', 'point_B', 'point_C', 'proof', 'point_G', 'message']] + ret = wally_dleq_proof_verify(*[x for a in args for x in a], 0) + expected = WALLY_OK if row['result_success'] == 'TRUE' else WALLY_EINVAL + self.assertEqual(ret, expected, row['comment']) + + # A missing generator means the secp256k1 generator + g = '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798' + num_default = 0 + for row in read_csv('bip374_generate_vectors.csv'): + if row['point_G'] == g and row['result_proof'] != 'INVALID': + args = [buf(row[k]) for k in ['scalar_a', 'point_B', 'auxrand_r']] + ret = wally_dleq_proof_generate(*[x for a in args for x in a], None, 0, + *buf(row['message']), 0, out, out_len) + self.assertEqual((ret, h(out)), (WALLY_OK, utf8(row['result_proof']))) + num_default += 1 + for row in read_csv('bip374_verify_vectors.csv'): + if row['point_G'] == g: + args = [buf(row[k]) for k in ['point_A', 'point_B', 'point_C', 'proof']] + ret = wally_dleq_proof_verify(*[x for a in args for x in a], None, 0, + *buf(row['message']), 0) + expected = WALLY_OK if row['result_success'] == 'TRUE' else WALLY_EINVAL + self.assertEqual(ret, expected, row['comment']) + num_default += 1 + self.assertGreater(num_default, 2) + + # Invalid arguments + a, b, aux = bytes([0x11] * 32), bytes.fromhex(g), bytes(32) + ret = wally_dleq_proof_generate(a, 32, b, 33, aux, 32, None, 0, None, 0, 0, out, out_len) + self.assertEqual(ret, WALLY_OK) + for args in [ + (None, 32, b, 33, aux, 32, None, 0, None, 0, 0, out, out_len), # Missing a + (a, 31, b, 33, aux, 32, None, 0, None, 0, 0, out, out_len), # Short a + (a, 32, None, 33, aux, 32, None, 0, None, 0, 0, out, out_len), # Missing B + (a, 32, b, 33, None, 32, None, 0, None, 0, 0, out, out_len), # Missing aux + (a, 32, b, 33, aux, 31, None, 0, None, 0, 0, out, out_len), # Short aux + (a, 32, b, 33, aux, 32, None, 33, None, 0, 0, out, out_len), # Bad G length + (a, 32, b, 33, aux, 32, None, 0, None, 32, 0, out, out_len), # Bad msg length + (a, 32, b, 33, aux, 32, None, 0, aux, 31, 0, out, out_len), # Short msg + (a, 32, b, 33, aux, 32, None, 0, None, 0, 1, out, out_len), # Unknown flags + (a, 32, b, 33, aux, 32, None, 0, None, 0, 0, None, out_len), # Missing output + (a, 32, b, 33, aux, 32, None, 0, None, 0, 0, out, out_len - 1), # Short output + ]: + self.assertEqual(wally_dleq_proof_generate(*args), WALLY_EINVAL) + @unittest.skipUnless(wally_s2c_sig_from_bytes, "s2c module not enabled") def test_s2c(self): priv_key, pub_key, msg, s2c_data, sig_out, s2c_opening_out = self.cbufferize( diff --git a/src/test/util.py b/src/test/util.py index cb0d3fa3d..d013ac360 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -355,6 +355,8 @@ class wally_psbt(Structure): ('wally_descriptor_to_addresses', c_int, [c_void_p, c_uint32, c_uint32, c_uint32, c_uint32, POINTER(c_char_p), c_size_t]), ('wally_descriptor_to_script', c_int, [c_void_p, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, c_void_p, c_size_t, c_size_t_p]), ('wally_descriptor_to_script_get_maximum_length', c_int, [c_void_p, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, c_uint32, c_size_t_p]), + ('wally_dleq_proof_generate', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_void_p, c_size_t]), + ('wally_dleq_proof_verify', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, c_uint32]), ('wally_ec_private_key_bip341_tweak', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_void_p, c_size_t]), ('wally_ec_private_key_verify', c_int, [c_void_p, c_size_t]), ('wally_ec_public_key_bip341_tweak', c_int, [c_void_p, c_size_t, c_void_p, c_size_t, c_uint32, c_void_p, c_size_t]), diff --git a/src/wasm_package/src/const.js b/src/wasm_package/src/const.js index 6c4337f80..d2b5891b5 100755 --- a/src/wasm_package/src/const.js +++ b/src/wasm_package/src/const.js @@ -115,6 +115,7 @@ export const WALLY_CA_PREFIX_LIQUID_REGTEST = 0x04; /** Liquid v1 confidential a export const WALLY_CA_PREFIX_LIQUID_TESTNET = 0x17; /** Liquid v1 confidential address prefix for testnet */ export const WALLY_CS_MAX_ASSETS = 256; export const WALLY_DESCRIPTOR_TAPTREE_MAX_DEPTH = 128; /* BIP-341: maximum taptree depth */ +export const WALLY_DLEQ_PROOF_LEN = 64; /** The length of a BIP-374 DLEQ proof */ export const WALLY_EINVAL = -2; /** Invalid argument */ export const WALLY_ENOMEM = -3; /** malloc() failed */ export const WALLY_ERROR = -1; /** General error */ diff --git a/src/wasm_package/src/functions.js b/src/wasm_package/src/functions.js index 9b1072555..11108ce47 100644 --- a/src/wasm_package/src/functions.js +++ b/src/wasm_package/src/functions.js @@ -193,6 +193,8 @@ export const descriptor_set_network = wrap('wally_descriptor_set_network', [T.Op export const descriptor_to_address = wrap('wally_descriptor_to_address', [T.OpaqueRef, T.Int32, T.Int32, T.Int32, T.Int32, T.DestPtrPtr(T.String)]); export const descriptor_to_addresses = wrap('wally_descriptor_to_addresses', [T.OpaqueRef, T.Int32, T.Int32, T.Int32, T.Int32, T.DestPtrSized(T.String, T.USER_PROVIDED_LEN)]); export const descriptor_to_script_get_maximum_length = wrap('wally_descriptor_to_script_get_maximum_length', [T.OpaqueRef, T.Int32, T.Int32, T.Int32, T.Int32, T.Int32, T.Int32, T.DestPtr(T.Int32)]); +export const dleq_proof_generate = wrap('wally_dleq_proof_generate', [T.Bytes, T.Bytes, T.Bytes, T.Bytes, T.Bytes, T.Int32, T.DestPtrSized(T.Bytes, C.WALLY_DLEQ_PROOF_LEN)]); +export const dleq_proof_verify = wrap('wally_dleq_proof_verify', [T.Bytes, T.Bytes, T.Bytes, T.Bytes, T.Bytes, T.Bytes, T.Int32]); export const ec_private_key_bip341_tweak = wrap('wally_ec_private_key_bip341_tweak', [T.Bytes, T.Bytes, T.Int32, T.DestPtrSized(T.Bytes, C.EC_PRIVATE_KEY_LEN)]); export const ec_private_key_verify = wrap('wally_ec_private_key_verify', [T.Bytes]); export const ec_public_key_bip341_tweak = wrap('wally_ec_public_key_bip341_tweak', [T.Bytes, T.Bytes, T.Int32, T.DestPtrSized(T.Bytes, C.EC_PUBLIC_KEY_LEN)]); diff --git a/src/wasm_package/src/index.d.ts b/src/wasm_package/src/index.d.ts index 84a263970..4f01e273e 100644 --- a/src/wasm_package/src/index.d.ts +++ b/src/wasm_package/src/index.d.ts @@ -153,6 +153,8 @@ export function descriptor_set_network(descriptor: Ref_wally_descriptor, network export function descriptor_to_address(descriptor: Ref_wally_descriptor, variant: number, multi_index: number, child_num: number, flags: number): string; export function descriptor_to_addresses(descriptor: Ref_wally_descriptor, variant: number, multi_index: number, child_num: number, flags: number, out_len: number): string; export function descriptor_to_script_get_maximum_length(descriptor: Ref_wally_descriptor, depth: number, index: number, variant: number, multi_index: number, child_num: number, flags: number): number; +export function dleq_proof_generate(priv_key: Buffer|Uint8Array|null, pub_key: Buffer|Uint8Array|null, aux_rand: Buffer|Uint8Array|null, generator: Buffer|Uint8Array|null, msg: Buffer|Uint8Array|null, flags: number): Buffer; +export function dleq_proof_verify(pub_key_a: Buffer|Uint8Array|null, pub_key_b: Buffer|Uint8Array|null, pub_key_c: Buffer|Uint8Array|null, proof: Buffer|Uint8Array|null, generator: Buffer|Uint8Array|null, msg: Buffer|Uint8Array|null, flags: number): void; export function ec_private_key_bip341_tweak(priv_key: Buffer|Uint8Array|null, merkle_root: Buffer|Uint8Array|null, flags: number): Buffer; export function ec_private_key_verify(priv_key: Buffer|Uint8Array|null): void; export function ec_public_key_bip341_tweak(pub_key: Buffer|Uint8Array|null, merkle_root: Buffer|Uint8Array|null, flags: number): Buffer; diff --git a/tools/wasm_exports.sh b/tools/wasm_exports.sh index ce3931759..7a3998f45 100644 --- a/tools/wasm_exports.sh +++ b/tools/wasm_exports.sh @@ -121,6 +121,8 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_descriptor_to_addresses' \ ,'_wally_descriptor_to_script' \ ,'_wally_descriptor_to_script_get_maximum_length' \ +,'_wally_dleq_proof_generate' \ +,'_wally_dleq_proof_verify' \ ,'_wally_ec_private_key_bip341_tweak' \ ,'_wally_ec_private_key_verify' \ ,'_wally_ec_public_key_bip341_tweak' \ From c6da2101646bbc4aeb99093fc43d4b812e61e60f Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:27:01 +0900 Subject: [PATCH 05/11] psbt: Parse BIP-375 ECDH shares and DLEQ proofs BIP-375 signers publish an ECDH share and a BIP-374 proof for each silent payment scan key, either once for all inputs (global fields 0x07 and 0x08) or per input (fields 0x1d and 0x1e). Until now these were kept as unknown fields with no validation. Parse, validate, serialize and combine them for PSBTv2, reject them in v0, and leave them unknown in PSETs. They are keyed by scan key, so they get new sp_ecdh_shares and sp_dleq_proofs maps on the PSBT and its inputs, which changes the layout of both structs. The BIP-373 MuSig2 input fields inside the new range stay unknown fields. --- include/wally_psbt.h | 6 ++ src/psbt.c | 73 +++++++++++++++++++++++- src/psbt_io.h | 31 +++++++++-- src/test/test_psbt.py | 125 ++++++++++++++++++++++++++++++++++++++++++ src/test/util.py | 4 ++ 5 files changed, 232 insertions(+), 7 deletions(-) diff --git a/include/wally_psbt.h b/include/wally_psbt.h index 08f41c7d2..9e86f5dc4 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -89,6 +89,9 @@ struct wally_psbt_input { /* Hashes and paths for taproot bip32 derivation path */ struct wally_map taproot_leaf_hashes; struct wally_map taproot_leaf_paths; + /* BIP-375 ECDH shares and DLEQ proofs, keyed by silent payment scan key */ + struct wally_map sp_ecdh_shares; + struct wally_map sp_dleq_proofs; #ifndef WALLY_ABI_NO_ELEMENTS uint64_t issuance_amount; /* Issuance amount, or 0 if not given */ uint64_t inflation_keys; /* Number of reissuance tokens, or 0 if none given */ @@ -140,6 +143,9 @@ struct wally_psbt { uint32_t fallback_locktime; uint32_t has_fallback_locktime; uint32_t tx_modifiable_flags; + /* BIP-375 ECDH shares and DLEQ proofs, keyed by silent payment scan key */ + struct wally_map sp_ecdh_shares; + struct wally_map sp_dleq_proofs; #ifndef WALLY_ABI_NO_ELEMENTS struct wally_map global_scalars; uint32_t pset_modifiable_flags; diff --git a/src/psbt.c b/src/psbt.c index 5b3599ba7..e109efe10 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -1,6 +1,7 @@ #include "internal.h" #include +#include #include #include #include @@ -599,6 +600,31 @@ static int taproot_tree_verify(const unsigned char *key, size_t key_len, return taproot_tree_value_verify(val, val_len); } +/* BIP-375: An ECDH share or DLEQ proof keyed by a 33 byte scan key */ +static int sp_scan_key_verify(const unsigned char *key, size_t key_len) +{ + return key && key_len == EC_PUBLIC_KEY_LEN ? + wally_ec_public_key_verify(key, key_len) : WALLY_EINVAL; +} + +static int sp_ecdh_share_verify(const unsigned char *key, size_t key_len, + const unsigned char *val, size_t val_len) +{ + if (sp_scan_key_verify(key, key_len) != WALLY_OK || !val || + val_len != EC_PUBLIC_KEY_LEN) + return WALLY_EINVAL; + return wally_ec_public_key_verify(val, val_len); +} + +static int sp_dleq_proof_verify(const unsigned char *key, size_t key_len, + const unsigned char *val, size_t val_len) +{ + if (sp_scan_key_verify(key, key_len) != WALLY_OK || !val || + val_len != WALLY_DLEQ_PROOF_LEN) + return WALLY_EINVAL; + return WALLY_OK; +} + static int psbt_input_field_verify(uint32_t field_type, const unsigned char *val, size_t val_len) { @@ -915,6 +941,8 @@ static void psbt_input_init(struct wally_psbt_input *input) wally_map_init(0, NULL /* FIXME */, &input->taproot_leaf_scripts); wally_map_init(0, map_leaf_hashes_verify, &input->taproot_leaf_hashes); wally_map_init(0, wally_keypath_xonly_public_key_verify, &input->taproot_leaf_paths); + wally_map_init(0, sp_ecdh_share_verify, &input->sp_ecdh_shares); + wally_map_init(0, sp_dleq_proof_verify, &input->sp_dleq_proofs); #ifdef BUILD_ELEMENTS wally_map_init(0, pset_map_input_field_verify, &input->pset_fields); #endif /* BUILD_ELEMENTS */ @@ -935,6 +963,8 @@ static int psbt_input_free(struct wally_psbt_input *input, bool free_parent) wally_map_clear(&input->taproot_leaf_scripts); wally_map_clear(&input->taproot_leaf_hashes); wally_map_clear(&input->taproot_leaf_paths); + wally_map_clear(&input->sp_ecdh_shares); + wally_map_clear(&input->sp_dleq_proofs); #ifdef BUILD_ELEMENTS wally_tx_free(input->pegin_tx); wally_tx_witness_stack_free(input->pegin_witness); @@ -1285,6 +1315,10 @@ static int psbt_init(uint32_t version, size_t num_inputs, size_t num_outputs, ret = wally_map_init(num_unknowns, NULL, &psbt_out->unknowns); if (ret == WALLY_OK) ret = wally_map_init(0, wally_keypath_bip32_verify, &psbt_out->global_xpubs); + if (ret == WALLY_OK) + ret = wally_map_init(0, sp_ecdh_share_verify, &psbt_out->sp_ecdh_shares); + if (ret == WALLY_OK) + ret = wally_map_init(0, sp_dleq_proof_verify, &psbt_out->sp_dleq_proofs); #ifdef BUILD_ELEMENTS if (ret == WALLY_OK) ret = wally_map_init(0, scalar_verify, &psbt_out->global_scalars); @@ -1398,6 +1432,8 @@ int wally_psbt_free(struct wally_psbt *psbt) wally_map_clear(&psbt->unknowns); wally_map_clear(&psbt->global_xpubs); + wally_map_clear(&psbt->sp_ecdh_shares); + wally_map_clear(&psbt->sp_dleq_proofs); #ifdef BUILD_ELEMENTS wally_map_clear(&psbt->global_scalars); #endif /* BUILD_ELEMENTS */ @@ -2421,7 +2457,9 @@ static int pull_psbt_input(const struct wally_psbt *psbt, field_bit = field_type; } } else { - is_known = field_type <= PSBT_IN_MAX; + is_known = field_type <= PSBT_IN_MAX && + !(PSBT_FT(field_type) & PSBT_IN_UNPARSED) && + !(is_pset && (PSBT_FT(field_type) & PSBT_IN_SP_FIELDS)); if (is_known) field_bit = PSBT_FT(field_type); } @@ -2507,6 +2545,12 @@ static int pull_psbt_input(const struct wally_psbt *psbt, &result->taproot_leaf_hashes, &result->taproot_leaf_paths); break; + case PSBT_IN_SP_ECDH_SHARE: + ret = pull_map_item(cursor, max, key, key_len, &result->sp_ecdh_shares); + break; + case PSBT_IN_SP_DLEQ: + ret = pull_map_item(cursor, max, key, key_len, &result->sp_dleq_proofs); + break; #ifdef BUILD_ELEMENTS case PSET_FT(PSET_IN_EXPLICIT_VALUE): ret = wally_psbt_input_set_amount(result, pull_le64_subfield(cursor, max)); @@ -2793,7 +2837,9 @@ int wally_psbt_from_bytes(const unsigned char *bytes, size_t len, field_bit = field_type; } } else { - is_known = field_type <= PSBT_GLOBAL_MAX || field_type == PSBT_GLOBAL_VERSION; + is_known = (field_type <= PSBT_GLOBAL_MAX && + !(is_pset && (PSBT_FT(field_type) & PSBT_GLOBAL_SP_FIELDS))) || + field_type == PSBT_GLOBAL_VERSION; if (is_known) { if (field_type == PSBT_GLOBAL_VERSION) field_bit = PSBT_GLOBAL_VERSION_BIT; @@ -2849,6 +2895,12 @@ int wally_psbt_from_bytes(const unsigned char *bytes, size_t len, if ((*output)->tx_modifiable_flags & ~PSBT_TXMOD_ALL_FLAGS) ret = WALLY_EINVAL; /* Invalid flags */ break; + case PSBT_GLOBAL_SP_ECDH_SHARE: + ret = pull_map_item(cursor, max, key, key_len, &(*output)->sp_ecdh_shares); + break; + case PSBT_GLOBAL_SP_DLEQ: + ret = pull_map_item(cursor, max, key, key_len, &(*output)->sp_dleq_proofs); + break; #ifdef BUILD_ELEMENTS case PSET_FT(PSET_GLOBAL_SCALAR): { const unsigned char *workaround; @@ -3327,6 +3379,11 @@ static int push_psbt_input(const struct wally_psbt *psbt, false, &input->psbt_fields)) != WALLY_OK) return ret; + if (!is_pset && psbt->version == PSBT_2) { + push_psbt_map(cursor, max, PSBT_IN_SP_ECDH_SHARE, false, &input->sp_ecdh_shares); + push_psbt_map(cursor, max, PSBT_IN_SP_DLEQ, false, &input->sp_dleq_proofs); + } + #ifdef BUILD_ELEMENTS if (is_pset && psbt->version == PSBT_2) { uint32_t ft; @@ -3575,6 +3632,10 @@ int wally_psbt_to_bytes(const struct wally_psbt *psbt, uint32_t flags, push_varint(&cursor, &max, sizeof(uint8_t)); push_u8(&cursor, &max, psbt->tx_modifiable_flags & 0xff); } + if (!is_pset) { + push_psbt_map(&cursor, &max, PSBT_GLOBAL_SP_ECDH_SHARE, false, &psbt->sp_ecdh_shares); + push_psbt_map(&cursor, &max, PSBT_GLOBAL_SP_DLEQ, false, &psbt->sp_dleq_proofs); + } #ifdef BUILD_ELEMENTS push_psbt_map(&cursor, &max, PSET_GLOBAL_SCALAR, true, &psbt->global_scalars); @@ -3800,6 +3861,10 @@ static int combine_input(struct wally_psbt_input *dst, return ret; if ((ret = wally_map_combine(&dst->psbt_fields, &src->psbt_fields)) != WALLY_OK) return ret; + if ((ret = wally_map_combine(&dst->sp_ecdh_shares, &src->sp_ecdh_shares)) != WALLY_OK) + return ret; + if ((ret = wally_map_combine(&dst->sp_dleq_proofs, &src->sp_dleq_proofs)) != WALLY_OK) + return ret; if ((ret = combine_map_if_empty(&dst->taproot_leaf_signatures, &src->taproot_leaf_signatures)) != WALLY_OK) return ret; if ((ret = combine_map_if_empty(&dst->taproot_leaf_scripts, &src->taproot_leaf_scripts)) != WALLY_OK) @@ -4008,6 +4073,10 @@ static int psbt_combine(struct wally_psbt *psbt, const struct wally_psbt *src, if (ret == WALLY_OK) ret = wally_map_combine(&psbt->global_xpubs, &src->global_xpubs); + if (ret == WALLY_OK) + ret = wally_map_combine(&psbt->sp_ecdh_shares, &src->sp_ecdh_shares); + if (ret == WALLY_OK) + ret = wally_map_combine(&psbt->sp_dleq_proofs, &src->sp_dleq_proofs); #ifdef BUILD_ELEMENTS if (ret == WALLY_OK && is_pset) { diff --git a/src/psbt_io.h b/src/psbt_io.h index 94b3f454f..f99523e53 100644 --- a/src/psbt_io.h +++ b/src/psbt_io.h @@ -11,8 +11,8 @@ * shift them by 32 bits to allow using a single uint64_t to hold * all key bits to check using a single uint64_t bitset (lower 32 bits * for PSBT, upper 32 bits for PSET). This allows for up to 31 key types - * each for PSBT and PSET. PSBT currently maxes out at 25 keys for - * inputs; if this increases to 31 the shift can be made greater as long + * each for PSBT and PSET. PSBT currently maxes out at 31 keys for + * inputs; if this increases further the shift can be made greater as long * as PSET keys can be represented in the remaining bits (PSET currently * maxes out at 16 bits for inputs). */ @@ -35,10 +35,16 @@ #define PSBT_GLOBAL_INPUT_COUNT 0x04 #define PSBT_GLOBAL_OUTPUT_COUNT 0x05 #define PSBT_GLOBAL_TX_MODIFIABLE 0x06 +#define PSBT_GLOBAL_SP_ECDH_SHARE 0x07 +#define PSBT_GLOBAL_SP_DLEQ 0x08 /* VERSION and PROPRIETARY are treated specially, hence our max is the max * of the contiguous defined fields. */ -#define PSBT_GLOBAL_MAX PSBT_GLOBAL_TX_MODIFIABLE +#define PSBT_GLOBAL_MAX PSBT_GLOBAL_SP_DLEQ + +/* BIP-375 silent payment global fields (not used in PSETs) */ +#define PSBT_GLOBAL_SP_FIELDS (PSBT_FT(PSBT_GLOBAL_SP_ECDH_SHARE) | \ + PSBT_FT(PSBT_GLOBAL_SP_DLEQ)) /* PSBT_GLOBAL_VERSION is not contiguous with the other keys, and is * out of range of the keys we can track in the lower 32 bits of a @@ -56,10 +62,12 @@ /* Global PSBT/PSET fields that can be repeated */ #define PSBT_GLOBAL_REPEATABLE (PSBT_FT(PSBT_GLOBAL_XPUB) | \ + PSBT_GLOBAL_SP_FIELDS | \ PSET_FT(PSET_GLOBAL_SCALAR)) /* Global PSBT/PSET fields that contain data in their keys */ #define PSBT_GLOBAL_HAVE_KEYDATA (PSBT_FT(PSBT_GLOBAL_XPUB) | \ + PSBT_GLOBAL_SP_FIELDS | \ PSET_FT(PSET_GLOBAL_SCALAR)) /* Global PSBT/PSET fields that must be present in v0 */ @@ -76,6 +84,7 @@ PSBT_FT(PSBT_GLOBAL_INPUT_COUNT) | \ PSBT_FT(PSBT_GLOBAL_OUTPUT_COUNT) | \ PSBT_FT(PSBT_GLOBAL_TX_MODIFIABLE) | \ + PSBT_GLOBAL_SP_FIELDS | \ PSET_FT(PSET_GLOBAL_SCALAR) | \ PSET_FT(PSET_GLOBAL_TX_MODIFIABLE) | \ PSET_FT(PSET_GLOBAL_GENESIS_HASH)) @@ -116,7 +125,17 @@ #define PSBT_IN_TAP_BIP32_DERIVATION 0x16 #define PSBT_IN_TAP_INTERNAL_KEY 0x17 #define PSBT_IN_TAP_MERKLE_ROOT 0x18 -#define PSBT_IN_MAX PSBT_IN_TAP_MERKLE_ROOT +#define PSBT_IN_SP_ECDH_SHARE 0x1d +#define PSBT_IN_SP_DLEQ 0x1e +#define PSBT_IN_MAX PSBT_IN_SP_DLEQ + +/* Input PSBT fields in range that we do not parse; kept as unknowns. + * 0x1a-0x1c are the BIP-373 MuSig2 fields. */ +#define PSBT_IN_UNPARSED (PSBT_FT(0x19) | PSBT_FT(0x1a) | PSBT_FT(0x1b) | PSBT_FT(0x1c)) + +/* BIP-375 silent payment input fields (not used in PSETs) */ +#define PSBT_IN_SP_FIELDS (PSBT_FT(PSBT_IN_SP_ECDH_SHARE) | \ + PSBT_FT(PSBT_IN_SP_DLEQ)) /* Inputs: PSET */ #define PSET_IN_ISSUANCE_VALUE 0x00 @@ -151,7 +170,8 @@ PSBT_FT(PSBT_IN_HASH256) | \ PSBT_FT(PSBT_IN_TAP_SCRIPT_SIG) | \ PSBT_FT(PSBT_IN_TAP_LEAF_SCRIPT) | \ - PSBT_FT(PSBT_IN_TAP_BIP32_DERIVATION)) + PSBT_FT(PSBT_IN_TAP_BIP32_DERIVATION) | \ + PSBT_IN_SP_FIELDS) /* Input PSBT/PSET fields that can be repeated */ #define PSBT_IN_REPEATABLE PSBT_IN_HAVE_KEYDATA @@ -167,6 +187,7 @@ /* Input PSBT/PSET fields that must *not* be present in v0 */ #define PSBT_IN_DISALLOWED_V0 (PSBT_FT(PSBT_IN_PREVIOUS_TXID) | \ PSBT_FT(PSBT_IN_OUTPUT_INDEX) | \ + PSBT_IN_SP_FIELDS | \ PSBT_FT(PSBT_IN_SEQUENCE) | \ PSBT_FT(PSBT_IN_REQUIRED_TIME_LOCKTIME) | \ PSBT_FT(PSBT_IN_REQUIRED_HEIGHT_LOCKTIME) | \ diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 3363ded9f..86f1b7e37 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -467,6 +467,131 @@ def check_silent_payment_id(self, psbt): for p in [resolved, as_scripts, other]: wally_psbt_free(p) + def get_map_value(self, map_ref, key): + ret, index = wally_map_find(map_ref, key, len(key)) + self.assertEqual(ret, WALLY_OK) + if not index: + return None + buf, buf_len = make_cbuffer('00' * 64) + ret, written = wally_map_get_item(map_ref, index - 1, buf, buf_len) + self.assertEqual(ret, WALLY_OK) + return bytes(buf)[:written] + + def test_silent_payment_shares(self): + """Test BIP-375 ECDH share and DLEQ proof fields""" + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + + num_global, num_input = 0, 0 + for case in cases['valid']: + psbt = self.parse_base64(case['psbt']) + b64 = self.to_base64(psbt) + psbt2 = self.parse_base64(b64) + self.assertEqual(self.to_base64(psbt2), b64) + wally_psbt_free(psbt2) + for proof in case['supplementary'].get('sp_proofs', []): + scan = bytes.fromhex(proof['scan_key']) + if 'input_index' in proof: + p = psbt.contents.inputs[proof['input_index']] + num_input += 1 + else: + p = psbt.contents + num_global += 1 + share = self.get_map_value(byref(p.sp_ecdh_shares), scan) + dleq = self.get_map_value(byref(p.sp_dleq_proofs), scan) + self.assertEqual(share, bytes.fromhex(proof['ecdh_share'])) + self.assertEqual(dleq, bytes.fromhex(proof['dleq_proof'])) + wally_psbt_free(psbt) + self.assertGreater(num_global, 0) + self.assertGreater(num_input, 0) + + invalid = [ + 'psbt structure: incorrect byte length for PSBT_IN_SP_ECDH_SHARE field', + 'psbt structure: incorrect byte length for PSBT_IN_SP_DLEQ field', + ] + for case in cases['invalid']: + if case['description'] in invalid: + self.parse_base64(case['psbt'], WALLY_EINVAL) + invalid.remove(case['description']) + self.assertEqual(invalid, []) + + # The maps only accept valid scan keys, shares and proofs + psbt = pointer(wally_psbt()) + self.assertEqual(wally_psbt_init_alloc(2, 1, 0, 0, 0, psbt), WALLY_OK) + scan = bytes.fromhex('027a487fc19fb769877b8742d6ea18118f3c4e72b1ea8c6de602a7ad4a41dbe068') + share = bytes.fromhex('02d3262723352607e84fcaf7651f8d2d637e020aea86dfad80d306cd4f48cfcc62') + shares, proofs = byref(psbt.contents.sp_ecdh_shares), byref(psbt.contents.sp_dleq_proofs) + for key, val in [(scan[:-1], share), (b'\x04' + scan[1:], share), + (scan, share[:-1]), (scan, b'\x04' + share[1:])]: + self.assertEqual(wally_map_add(shares, key, len(key), val, len(val)), WALLY_EINVAL) + for key, val in [(scan[:-1], bytes(64)), (scan, bytes(63)), (scan, bytes(65))]: + self.assertEqual(wally_map_add(proofs, key, len(key), val, len(val)), WALLY_EINVAL) + self.assertEqual(wally_map_add(shares, scan, len(scan), share, len(share)), WALLY_OK) + self.assertEqual(wally_map_add(proofs, scan, len(scan), bytes(64), 64), WALLY_OK) + wally_psbt_free(psbt) + + # Share and proof fields are v2 only; BIP-373 input fields stay unknown + def build(version, num_inputs): + psbt = pointer(wally_psbt()) + self.assertEqual(wally_psbt_init_alloc(version, num_inputs, 0, 0, 0, psbt), WALLY_OK) + txhash = b'\x01' * 32 # A null prevout is refused + if version == 0: + tx = pointer(wally_tx()) + self.assertEqual(wally_tx_init_alloc(2, 0, num_inputs, 0, tx), WALLY_OK) + for i in range(num_inputs): + ret = wally_tx_add_raw_input(tx, txhash, 32, i, 0xffffffff, None, 0, None, 0) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_set_global_tx(psbt, tx), WALLY_OK) + wally_tx_free(tx) + else: + for i in range(num_inputs): + tx_in = pointer(wally_tx_input()) + ret = wally_tx_input_init_alloc(txhash, 32, i, 0xffffffff, None, 0, None, tx_in) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_add_tx_input_at(psbt, i, 0, tx_in), WALLY_OK) + ret, length = wally_psbt_get_length(psbt, 0) + self.assertEqual(ret, WALLY_OK) + buf, buf_len = make_cbuffer('00' * length) + self.assertEqual(wally_psbt_to_bytes(psbt, 0, buf, buf_len), (WALLY_OK, length)) + wally_psbt_free(psbt) + return bytes(buf) + + g_share = b'\x22\x07' + scan + b'\x21' + share + g_proof = b'\x22\x08' + scan + b'\x40' + bytes(64) + i_share = b'\x22\x1d' + scan + b'\x21' + share + i_proof = b'\x22\x1e' + scan + b'\x40' + bytes(64) + musig = b'\x22\x1b' + scan + b'\x21' + share # BIP-373 MuSig2 public nonce + + # With one empty input and no outputs, a v0 PSBT ends with the global + # separator then the input separator. A v2 PSBT with no inputs ends with + # the global separator; with one input and no outputs, the input separator. + v0, v2_globals, v2_input = build(0, 1), build(2, 0), build(2, 1) + for raw, expected in [ + (v0[:-2] + g_share + v0[-2:], WALLY_EINVAL), + (v0[:-2] + g_proof + v0[-2:], WALLY_EINVAL), + (v0[:-1] + i_share + v0[-1:], WALLY_EINVAL), + (v0[:-1] + i_proof + v0[-1:], WALLY_EINVAL), + (v0[:-1] + musig + v0[-1:], WALLY_OK), + (v2_globals[:-1] + g_share + g_proof + v2_globals[-1:], WALLY_OK), + (v2_input[:-1] + i_share + i_proof + musig + v2_input[-1:], WALLY_OK), + ]: + psbt = pointer(wally_psbt()) + self.assertEqual(wally_psbt_from_bytes(raw, len(raw), 0, psbt), expected) + if expected != WALLY_OK: + continue + p = psbt.contents.inputs[0] if psbt.contents.num_inputs else psbt.contents + num_sp = 0 if raw.startswith(v0[:-2]) else 1 + self.assertEqual((p.sp_ecdh_shares.num_items, p.sp_dleq_proofs.num_items), + (num_sp, num_sp)) + if psbt.contents.num_inputs: + self.assertEqual(p.unknowns.num_items, 1) # MuSig2 field + b64 = self.to_base64(psbt) + psbt2 = self.parse_base64(b64) + self.assertEqual(self.to_base64(psbt2), b64) + wally_psbt_free(psbt2) + wally_psbt_free(psbt) + def test_silent_payment_outputs(self): """Test BIP-375 silent payment output fields""" # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b diff --git a/src/test/util.py b/src/test/util.py index d013ac360..8d06380ad 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -152,6 +152,8 @@ class wally_psbt_input(Structure): ('taproot_leaf_scripts', wally_map), ('taproot_leaf_hashes', wally_map), ('taproot_leaf_paths', wally_map), + ('sp_ecdh_shares', wally_map), + ('sp_dleq_proofs', wally_map), ('issuance_amount', c_uint64), ('inflation_keys', c_uint64), ('pegin_amount', c_uint64), @@ -192,6 +194,8 @@ class wally_psbt(Structure): ('fallback_locktime', c_uint32), ('has_fallback_locktime', c_uint32), ('tx_modifiable_flags', c_uint32), + ('sp_ecdh_shares', wally_map), + ('sp_dleq_proofs', wally_map), ('global_scalars', wally_map), ('pset_modifiable_flags', c_uint32), ('genesis_blockhash', c_ubyte * 32), From aba84ef6e3a0a1b7505643f9422e3650310f4d98 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:27:27 +0900 Subject: [PATCH 06/11] psbt: Compute BIP-375 silent payment scripts A silent payment output's script depends on the ECDH shares of the transaction's eligible inputs, so a signer or coordinator has to derive it rather than read it from the PSBT. Add wally_psbt_compute_sp_scripts(). It finds the eligible inputs and their keys under the BIP-352 rules, verifies every DLEQ proof present for an eligible input or the whole transaction, derives each script in the BIP-375 order, sets missing scripts, checks present ones and clears the modifiable flags. It changes nothing if any step fails. An eligible input's key must hash to its prevout script, and a P2SH redeem script must hash to its prevout. A key that is not the one actually spent makes the payment unfindable by its receiver. The BIP-375 reference validator does not check this and its vectors' P2WPKH prevouts do not commit to their keys, so the tests correct those prevouts first; output scripts depend only on keys and outpoints. Parsing now also rejects a PSBT whose silent payment script is set while its inputs or outputs are still modifiable, as BIP-375 requires. --- include/wally.hpp | 6 + include/wally_psbt.h | 28 ++ src/psbt.c | 616 ++++++++++++++++++++++++++++++ src/swig_java/swig.i | 1 + src/test/test_psbt.py | 133 +++++++ src/test/util.py | 1 + src/wasm_package/src/functions.js | 1 + src/wasm_package/src/index.d.ts | 1 + tools/wasm_exports.sh | 1 + 9 files changed, 788 insertions(+) diff --git a/include/wally.hpp b/include/wally.hpp index 64527a64e..867a2af9f 100644 --- a/include/wally.hpp +++ b/include/wally.hpp @@ -1341,6 +1341,12 @@ inline int psbt_combine_ex(const PSBT& psbt, uint32_t flags, const struct wally_ return detail::check_ret(__FUNCTION__, ret); } +template +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 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); diff --git a/include/wally_psbt.h b/include/wally_psbt.h index 9e86f5dc4..ac10a370a 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -2031,6 +2031,34 @@ WALLY_CORE_API int wally_psbt_get_id( unsigned char *bytes_out, size_t len); +/** + * Compute and check the scripts of BIP-375 silent payment outputs. + * + * Each output with a silent payment code has its script derived from the + * BIP-375 ECDH shares in the PSBT, after every DLEQ proof present for an + * eligible input, or for the whole transaction, is verified against the + * input public keys. Missing scripts are set, and scripts already present + * must match. If any script is set, the PSBT's inputs and outputs modifiable + * flags are cleared. The PSBT is unchanged if any step fails. + * + * :param psbt: The PSBTv2 to update. + * :param flags: For future use. Must be 0. + * + * .. note:: Returns `WALLY_EINVAL` if an input spends a segwit v2 or later + *| output, an eligible input's public key or a share or proof is missing, + *| a proof does not verify, a present script does not match, or an input + *| claims to be skipped (a finalized input or the NUMS internal key) without + *| the data to show that BIP-352 skips it. + * + * .. note:: An input's prevout script comes from its witness or non-witness + *| utxo. A non-witness utxo is checked against the input, but a witness utxo + *| alone cannot be: the caller must trust it or check it, for example + *| against the keys it holds, as a false script can change the key sum. + */ +WALLY_CORE_API int wally_psbt_compute_sp_scripts( + struct wally_psbt *psbt, + uint32_t flags); + /** * Return the calculated transaction lock time of a PSBT. * diff --git a/src/psbt.c b/src/psbt.c index e109efe10..349e4a787 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -625,6 +625,22 @@ static int sp_dleq_proof_verify(const unsigned char *key, size_t key_len, return WALLY_OK; } +/* BIP-375: Once a silent payment output's script is set, the inputs and + * outputs must no longer be modifiable */ +static bool sp_script_is_modifiable(const struct wally_psbt *psbt) +{ + size_t i; + + if (!(psbt->tx_modifiable_flags & (WALLY_PSBT_TXMOD_INPUTS | WALLY_PSBT_TXMOD_OUTPUTS))) + return false; + for (i = 0; i < psbt->num_outputs; ++i) { + const struct wally_psbt_output *out = psbt->outputs + i; + if (out->script && wally_map_get_integer(&out->psbt_fields, PSBT_OUT_SP_V0_INFO)) + return true; + } + return false; +} + static int psbt_input_field_verify(uint32_t field_type, const unsigned char *val, size_t val_len) { @@ -2980,6 +2996,10 @@ int wally_psbt_from_bytes(const unsigned char *bytes, size_t len, ret = pull_psbt_output(*output, cursor, max, tx_flags, flags, (*output)->outputs + i); + if (ret == WALLY_OK && !(flags & WALLY_PSBT_PARSE_FLAG_LOOSE) && + sp_script_is_modifiable(*output)) + ret = WALLY_EINVAL; /* Modifiable after a silent payment script is set */ + if (ret == WALLY_OK && !*cursor) ret = WALLY_EINVAL; /* Ran out of data */ else if (ret == WALLY_OK && *max && (flags & WALLY_PSBT_PARSE_FLAG_COMPLETE)) @@ -4476,6 +4496,602 @@ int wally_psbt_set_version(struct wally_psbt *psbt, return psbt->version == PSBT_0 ? psbt_v0_to_v2(psbt) : psbt_v2_to_v0(psbt); } +/* BIP-352/BIP-375 silent payment output script computation */ +#define SP_TAG_INPUTS "BIP0352/Inputs" +#define SP_TAG_SHARED_SECRET "BIP0352/SharedSecret" +#define SP_OUTPOINT_LEN (WALLY_TXHASH_LEN + sizeof(uint32_t)) +#define SP_K_MAX 2323 /* BIP-352: The most outputs per scan key */ + +/* BIP-341 NUMS point H: inputs using it as their internal key are skipped */ +static const unsigned char SP_NUMS_H[EC_XONLY_PUBLIC_KEY_LEN] = { + 0x50, 0x92, 0x9b, 0x74, 0xc1, 0xa0, 0x49, 0x54, + 0xb7, 0x8b, 0x4b, 0x60, 0x35, 0xe9, 0x7a, 0x5e, + 0x07, 0x8a, 0x5a, 0x0f, 0x28, 0xec, 0x96, 0xd5, + 0x47, 0xbf, 0xee, 0x9a, 0xce, 0x80, 0x3a, 0xc0 +}; + +/* A silent payment output: its index and code */ +struct sp_output { + size_t index; + const unsigned char *code; /* Scan key followed by spend key */ +}; + +static int sp_output_cmp(const struct sp_output *a, const struct sp_output *b) +{ + /* BIP-375: Order by code, then by output index */ + int cmp = memcmp(a->code, b->code, WALLY_SILENT_PAYMENT_KEYS_LEN); + if (cmp) + return cmp; + return a->index < b->index ? -1 : a->index > b->index; +} + +/* Get the script an input spends. The non-witness utxo is the one source that + * can be verified, so it must match the input, and any witness utxo must agree */ +static int sp_input_prevout_script(const struct wally_psbt_input *input, + const unsigned char **script, size_t *script_len) +{ + if (input->utxo) { + const struct wally_tx_output *out; + int ret = is_matching_txid(input->utxo, input->txhash, WALLY_TXHASH_LEN); + if (ret != WALLY_OK) + return ret; + if (input->index >= input->utxo->num_outputs) + return WALLY_EINVAL; + out = input->utxo->outputs + input->index; + if (input->witness_utxo && + (input->witness_utxo->script_len != out->script_len || + memcmp(input->witness_utxo->script, out->script, out->script_len))) + return WALLY_EINVAL; /* Inconsistent utxos */ + *script = out->script; + *script_len = out->script_len; + return WALLY_OK; + } + if (!input->witness_utxo) + return WALLY_EINVAL; /* Need the prevout to know if it is eligible */ + *script = input->witness_utxo->script; + *script_len = input->witness_utxo->script_len; + return WALLY_OK; +} + +/* Results of looking for the public key behind a hash160 */ +#define SP_KEY_NONE 0 /* Not found */ +#define SP_KEY_COMPRESSED 1 /* Found: the input is eligible */ +#define SP_KEY_UNCOMPRESSED 2 /* Found, uncompressed: BIP-352 skips the input */ + +/* Check one candidate key against hash160 */ +static bool sp_match_key(const unsigned char *key, size_t key_len, + const unsigned char *hash160, unsigned char *pub_key, int *found) +{ + unsigned char h[HASH160_LEN]; + + if ((key_len != EC_PUBLIC_KEY_LEN && key_len != EC_PUBLIC_KEY_UNCOMPRESSED_LEN) || + wally_hash160(key, key_len, h, sizeof(h)) != WALLY_OK || + memcmp(h, hash160, HASH160_LEN)) + return false; + if (key_len == EC_PUBLIC_KEY_LEN) { + memcpy(pub_key, key, EC_PUBLIC_KEY_LEN); + *found = SP_KEY_COMPRESSED; + } else + *found = SP_KEY_UNCOMPRESSED; + return true; +} + +/* Find the public key in an input that hashes to hash160 */ +static int sp_find_hashed_pubkey(const struct wally_psbt_input *input, + const unsigned char *hash160, unsigned char *pub_key) +{ + const struct wally_map *maps[2] = { &input->keypaths, &input->signatures }; + const size_t lens[2] = { EC_PUBLIC_KEY_LEN, EC_PUBLIC_KEY_UNCOMPRESSED_LEN }; + const struct wally_map_item *item; + int found = SP_KEY_NONE; + size_t i, j; + + for (i = 0; i < 2; ++i) + for (j = 0; j < maps[i]->num_items; ++j) + if (sp_match_key(maps[i]->items[j].key, maps[i]->items[j].key_len, + hash160, pub_key, &found)) + return found; + if (input->final_witness && input->final_witness->num_items) { + const struct wally_tx_witness_stack *stack = input->final_witness; + const struct wally_tx_witness_item *wit = stack->items + stack->num_items - 1; + if (sp_match_key(wit->witness, wit->witness_len, hash160, pub_key, &found)) + return found; + } + item = wally_map_get_integer(&input->psbt_fields, PSBT_IN_FINAL_SCRIPTSIG); + if (item) { + /* P2PKH: As in BIP-352, search from the end of the scriptSig, as a + * malleated scriptSig may not end with the key. Compressed keys first, + * since BIP-352 only looks for those */ + for (i = 0; i < 2; ++i) + for (j = item->value_len; j >= lens[i]; --j) + if (sp_match_key(item->value + j - lens[i], lens[i], + hash160, pub_key, &found)) + return found; + } + return SP_KEY_NONE; +} + +/* A key must hash to the prevout, also for a finalized input: if nothing does, + * the input data does not spend the prevout. BIP-352 only skips uncompressed keys */ +static int sp_key_result(int found, bool *is_eligible) +{ + *is_eligible = found == SP_KEY_COMPRESSED; + return found == SP_KEY_NONE ? WALLY_EINVAL : WALLY_OK; +} + +/* Get the last push of a script made only of pushes, as a P2SH scriptSig must be */ +static bool sp_script_last_push(const unsigned char *script, size_t script_len, + const unsigned char **push, size_t *push_len) +{ + size_t i = 0, n; + + *push = NULL; + *push_len = 0; + while (i < script_len) { + const unsigned char op = script[i++]; + size_t num_len_bytes = op == 0x4c ? 1 : op == 0x4d ? 2 : op == 0x4e ? 4 : 0; + if (op == OP_1NEGATE || (op >= OP_1 && op <= OP_16)) { + *push = NULL; /* A number, which cannot be a redeem script */ + *push_len = 0; + continue; + } + if (op > 0x4e) + return false; /* Not a push */ + n = op; + if (num_len_bytes) { + size_t b; + if (script_len - i < num_len_bytes) + return false; + for (b = 0, n = 0; b < num_len_bytes; ++b) + n |= (size_t)script[i + b] << (8 * b); + i += num_len_bytes; + } + if (n > script_len - i) + return false; + *push = script + i; + *push_len = n; + i += n; + } + return *push != NULL; +} + +/* Whether a taproot output key commits to the NUMS key H, so that it cannot be + * spent by key path. An output key of H itself has no known private key either */ +static int sp_commits_to_nums(const unsigned char *output_key, + const unsigned char *merkle_root, size_t merkle_root_len, + bool *commits) +{ + unsigned char tweaked[EC_PUBLIC_KEY_LEN]; + int ret; + + *commits = !merkle_root && !memcmp(output_key, SP_NUMS_H, sizeof(SP_NUMS_H)); + if (*commits) + return WALLY_OK; + ret = wally_ec_public_key_bip341_tweak(SP_NUMS_H, sizeof(SP_NUMS_H), + merkle_root, merkle_root_len, 0, + tweaked, sizeof(tweaked)); + if (ret == WALLY_OK) + *commits = !memcmp(tweaked + 1, output_key, EC_XONLY_PUBLIC_KEY_LEN); + return ret; +} + +/* Compute the merkle root a script path spend commits to */ +static int sp_control_block_merkle_root(const struct wally_tx_witness_item *leaf, + const struct wally_tx_witness_item *control, + unsigned char *root) +{ + unsigned char *buf, branch[2 * SHA256_LEN]; + size_t i, buf_len = 1 + varint_get_length(leaf->witness_len) + leaf->witness_len; + int ret; + + if (!(buf = wally_malloc(buf_len))) + return WALLY_ENOMEM; + buf[0] = control->witness[0] & 0xfe; /* Leaf version */ + buf_len = 1 + varint_to_bytes(leaf->witness_len, buf + 1); + if (leaf->witness_len) + memcpy(buf + buf_len, leaf->witness, leaf->witness_len); + buf_len += leaf->witness_len; + ret = wally_bip340_tagged_hash(buf, buf_len, "TapLeaf", root, SHA256_LEN); + clear_and_free(buf, buf_len); + + for (i = 1 + SHA256_LEN; ret == WALLY_OK && i < control->witness_len; i += SHA256_LEN) { + /* The two child hashes are hashed in sorted order */ + const unsigned char *node = control->witness + i; + const bool root_first = memcmp(root, node, SHA256_LEN) < 0; + memcpy(branch, root_first ? root : node, SHA256_LEN); + memcpy(branch + SHA256_LEN, root_first ? node : root, SHA256_LEN); + ret = wally_bip340_tagged_hash(branch, sizeof(branch), "TapBranch", root, SHA256_LEN); + } + return ret; +} + +/* Whether a taproot input is skipped for spending from the NUMS key. The claim + * must match the output key, or a key path input could be left out of the sum, + * giving a script the receiver cannot find. Fails if the claim does not match */ +static int sp_taproot_is_nums(const struct wally_psbt_input *input, + const unsigned char *output_key, bool *is_nums) +{ + const struct wally_tx_witness_stack *stack = input->final_witness; + const struct wally_map_item *item; + bool commits = false; + int ret; + + *is_nums = false; + if (stack) { + /* Finalized: the witness says how the output is spent */ + const struct wally_tx_witness_item *control; + unsigned char root[SHA256_LEN]; + size_t num_items = stack->num_items; + + if (!num_items) + return WALLY_EINVAL; /* Does not spend the prevout */ + if (num_items > 1 && stack->items[num_items - 1].witness_len && + stack->items[num_items - 1].witness[0] == 0x50) + --num_items; /* The last item is an annex */ + if (num_items < 2) + return WALLY_OK; /* Key path spend */ + control = stack->items + num_items - 1; + if (control->witness_len < 1 + SHA256_LEN || + (control->witness_len - 1) % SHA256_LEN) + return WALLY_EINVAL; /* Not a control block */ + if (memcmp(control->witness + 1, SP_NUMS_H, sizeof(SP_NUMS_H))) + return WALLY_OK; /* Script path spend from another internal key */ + ret = sp_control_block_merkle_root(stack->items + num_items - 2, control, root); + if (ret == WALLY_OK) + ret = sp_commits_to_nums(output_key, root, sizeof(root), &commits); + } else { + const struct wally_map_item *root_item; + item = wally_map_get_integer(&input->psbt_fields, PSBT_IN_TAP_INTERNAL_KEY); + if (!item || item->value_len != sizeof(SP_NUMS_H) || + memcmp(item->value, SP_NUMS_H, sizeof(SP_NUMS_H))) + return WALLY_OK; + root_item = wally_map_get_integer(&input->psbt_fields, PSBT_IN_TAP_MERKLE_ROOT); + ret = sp_commits_to_nums(output_key, root_item ? root_item->value : NULL, + root_item ? root_item->value_len : 0, &commits); + } + if (ret != WALLY_OK) + return ret; + if (!commits) + return WALLY_EINVAL; /* Claims the NUMS key, but the output key does not commit to it */ + *is_nums = true; + return WALLY_OK; +} + +/* Get the BIP-352 public key of an input, if it is eligible. + * Returns WALLY_EINVAL if an input is not usable with silent payments. */ +static int sp_input_pubkey(const struct wally_psbt_input *input, + bool *is_eligible, unsigned char *pub_key) +{ + const struct wally_map_item *item; + const unsigned char *script; + size_t script_len, type, witver; + bool is_nums; + int ret; + + *is_eligible = false; + if ((ret = sp_input_prevout_script(input, &script, &script_len)) != WALLY_OK) + return ret; + + if (scriptpubkey_is_witness_program(script, script_len, &witver) && witver > 1) + return WALLY_EINVAL; /* BIP-375: No segwit v2+ inputs */ + + if (wally_scriptpubkey_get_type(script, script_len, &type) != WALLY_OK) + return WALLY_OK; /* Unknown scripts are not eligible */ + + switch (type) { + case WALLY_SCRIPT_TYPE_P2TR: + if ((ret = sp_taproot_is_nums(input, script + 2, &is_nums)) != WALLY_OK || is_nums) + return ret; + pub_key[0] = 0x02; /* The x-only output key, lifted to even y */ + memcpy(pub_key + 1, script + 2, EC_XONLY_PUBLIC_KEY_LEN); + *is_eligible = true; + break; + case WALLY_SCRIPT_TYPE_P2WPKH: + ret = sp_key_result(sp_find_hashed_pubkey(input, script + 2, pub_key), is_eligible); + break; + case WALLY_SCRIPT_TYPE_P2PKH: + ret = sp_key_result(sp_find_hashed_pubkey(input, script + 3, pub_key), is_eligible); + break; + case WALLY_SCRIPT_TYPE_P2SH: { + /* Only P2SH-P2WPKH is eligible, so the redeem script is needed to + * decide. Once finalized, it is the scriptSig's last push */ + const unsigned char *redeem = NULL; + unsigned char h[HASH160_LEN]; + size_t redeem_len = 0; + bool only_push = false; + item = wally_map_get_integer(&input->psbt_fields, PSBT_IN_REDEEM_SCRIPT); + if (item) { + redeem = item->value; + redeem_len = item->value_len; + only_push = true; + } else if ((item = wally_map_get_integer(&input->psbt_fields, + PSBT_IN_FINAL_SCRIPTSIG))) { + if (!sp_script_last_push(item->value, item->value_len, &redeem, &redeem_len)) + return WALLY_EINVAL; + only_push = redeem_len + 1 == item->value_len; + } + if (!redeem || wally_hash160(redeem, redeem_len, h, sizeof(h)) != WALLY_OK || + memcmp(h, script + 2, HASH160_LEN)) + return WALLY_EINVAL; /* Missing, or not the prevout's redeem script */ + if (wally_scriptpubkey_get_type(redeem, redeem_len, &type) != WALLY_OK || + type != WALLY_SCRIPT_TYPE_P2WPKH) + return WALLY_OK; /* Some other P2SH spend: not eligible */ + if (!only_push) + return WALLY_EINVAL; /* Not a valid P2SH-P2WPKH spend */ + ret = sp_key_result(sp_find_hashed_pubkey(input, redeem + 2, pub_key), is_eligible); + break; + } + default: + return WALLY_OK; + } + /* BIP-352 skips an input whose key is not a valid point */ + if (ret != WALLY_OK || + (*is_eligible && wally_ec_public_key_verify(pub_key, EC_PUBLIC_KEY_LEN) != WALLY_OK)) + *is_eligible = false; + return ret; +} + +/* Sum points, serializing the result compressed */ +static int sp_pubkey_sum(const unsigned char *keys, size_t num_keys, unsigned char *sum) +{ + secp256k1_pubkey *points, result; + const secp256k1_pubkey **ptrs; + size_t i, len = EC_PUBLIC_KEY_LEN; + bool ok; + + if (!num_keys) + return WALLY_EINVAL; + points = wally_malloc(num_keys * sizeof(*points)); + ptrs = wally_malloc(num_keys * sizeof(*ptrs)); + ok = points && ptrs; + for (i = 0; ok && i < num_keys; ++i) { + ok = pubkey_parse(&points[i], keys + i * EC_PUBLIC_KEY_LEN, EC_PUBLIC_KEY_LEN); + ptrs[i] = &points[i]; + } + /* Fails if the keys sum to infinity */ + ok = ok && pubkey_combine(&result, ptrs, num_keys) && + pubkey_serialize(sum, &len, &result, PUBKEY_COMPRESSED) && + len == EC_PUBLIC_KEY_LEN; + wally_free(points); + wally_free(ptrs); + if (!points || !ptrs) + return WALLY_ENOMEM; + return ok ? WALLY_OK : WALLY_EINVAL; +} + +/* Get the ECDH share for scan_key, verifying its DLEQ proof(s). + * keys holds the eligible inputs' public keys in input order, then their sum. */ +static int sp_get_ecdh_share(const struct wally_psbt *psbt, const unsigned char *scan_key, + const unsigned char *keys, const bool *eligible, + unsigned char *share) +{ + const struct wally_map_item *g_share, *g_proof, *in_share, *in_proof; + const unsigned char *pub_key = keys; + unsigned char *shares = NULL; + size_t i, num_shares = 0, num_eligible = 0; + int ret = WALLY_OK; + + for (i = 0; i < psbt->num_inputs; ++i) + num_eligible += eligible[i] ? 1 : 0; + + g_share = wally_map_get(&psbt->sp_ecdh_shares, scan_key, EC_PUBLIC_KEY_LEN); + g_proof = wally_map_get(&psbt->sp_dleq_proofs, scan_key, EC_PUBLIC_KEY_LEN); + if (g_share || g_proof) { + if (!g_share || !g_proof || + wally_dleq_proof_verify(keys + num_eligible * EC_PUBLIC_KEY_LEN, + EC_PUBLIC_KEY_LEN, scan_key, EC_PUBLIC_KEY_LEN, + g_share->value, g_share->value_len, + g_proof->value, g_proof->value_len, + NULL, 0, NULL, 0, 0) != WALLY_OK) + return WALLY_EINVAL; /* Global share without a valid proof */ + } + + /* Verify every per-input share given for an eligible input */ + if (!(shares = wally_malloc(num_eligible * EC_PUBLIC_KEY_LEN))) + return WALLY_ENOMEM; + for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) { + const struct wally_psbt_input *input = psbt->inputs + i; + if (!eligible[i]) + continue; /* Shares of ineligible inputs do not contribute */ + in_share = wally_map_get(&input->sp_ecdh_shares, scan_key, EC_PUBLIC_KEY_LEN); + in_proof = wally_map_get(&input->sp_dleq_proofs, scan_key, EC_PUBLIC_KEY_LEN); + if (in_share || in_proof) { + if (!in_share || !in_proof || + wally_dleq_proof_verify(pub_key, EC_PUBLIC_KEY_LEN, + scan_key, EC_PUBLIC_KEY_LEN, + in_share->value, in_share->value_len, + in_proof->value, in_proof->value_len, + NULL, 0, NULL, 0, 0) != WALLY_OK) + ret = WALLY_EINVAL; /* Input share without a valid proof */ + else + memcpy(shares + num_shares++ * EC_PUBLIC_KEY_LEN, + in_share->value, EC_PUBLIC_KEY_LEN); + } + pub_key += EC_PUBLIC_KEY_LEN; + } + + if (ret == WALLY_OK) { + if (g_share) + memcpy(share, g_share->value, EC_PUBLIC_KEY_LEN); + else if (num_shares != num_eligible) + ret = WALLY_EINVAL; /* Not every eligible input has a share */ + else + ret = sp_pubkey_sum(shares, num_shares, share); + } + wally_free(shares); + return ret; +} + +/* Compute the output script for silent payment code k of a shared secret */ +static int sp_output_script(const unsigned char *shared_secret, uint32_t k, + const unsigned char *spend_key, unsigned char *script) +{ + unsigned char buf[EC_PUBLIC_KEY_LEN + sizeof(uint32_t)], t_k[SHA256_LEN]; + unsigned char out_key[EC_PUBLIC_KEY_LEN]; + int ret; + + memcpy(buf, shared_secret, EC_PUBLIC_KEY_LEN); + uint32_to_be_bytes(k, buf + EC_PUBLIC_KEY_LEN); + ret = wally_bip340_tagged_hash(buf, sizeof(buf), SP_TAG_SHARED_SECRET, + t_k, sizeof(t_k)); + if (ret == WALLY_OK) /* P_k = B_spend + t_k*G */ + ret = wally_ec_public_key_tweak(spend_key, EC_PUBLIC_KEY_LEN, + t_k, sizeof(t_k), out_key, sizeof(out_key)); + if (ret == WALLY_OK) { + script[0] = OP_1; + script[1] = EC_XONLY_PUBLIC_KEY_LEN; + memcpy(script + 2, out_key + 1, EC_XONLY_PUBLIC_KEY_LEN); + } + wally_clear_3(buf, sizeof(buf), t_k, sizeof(t_k), out_key, sizeof(out_key)); + return ret; +} + +int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) +{ + struct sp_output *outs = NULL, tmp; + unsigned char *keys = NULL, *scripts = NULL, **new_scripts = NULL; + unsigned char outpoint[SP_OUTPOINT_LEN], min_outpoint[SP_OUTPOINT_LEN]; + unsigned char hash_in[SP_OUTPOINT_LEN + EC_PUBLIC_KEY_LEN]; + unsigned char input_hash[SHA256_LEN], share[EC_PUBLIC_KEY_LEN]; + unsigned char shared_secret[EC_PUBLIC_KEY_LEN]; + bool *eligible = NULL, set_any = false; + size_t i, j, num_outs = 0, num_eligible = 0, len; + size_t is_pset; + uint32_t k = 0; + int ret = WALLY_OK; + + if (!psbt_is_valid(psbt) || psbt->version != PSBT_2 || flags || + wally_psbt_is_elements(psbt, &is_pset) != WALLY_OK || is_pset) + return WALLY_EINVAL; + + /* Gather the silent payment outputs */ + for (i = 0; i < psbt->num_outputs; ++i) + if (wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO)) + ++num_outs; + if (!num_outs) + return WALLY_OK; /* Nothing to do */ + if (!psbt->num_inputs || sp_script_is_modifiable(psbt)) + return WALLY_EINVAL; + + outs = wally_calloc(num_outs * sizeof(*outs)); + scripts = wally_calloc(num_outs * WALLY_SCRIPTPUBKEY_P2TR_LEN); + keys = wally_calloc((psbt->num_inputs + 1) * EC_PUBLIC_KEY_LEN); + eligible = wally_calloc(psbt->num_inputs * sizeof(*eligible)); + new_scripts = wally_calloc(num_outs * sizeof(*new_scripts)); + if (!outs || !scripts || !keys || !eligible || !new_scripts) { + ret = WALLY_ENOMEM; + goto done; + } + for (i = 0, j = 0; i < psbt->num_outputs; ++i) { + const struct wally_map_item *info; + info = wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO); + if (info) { + outs[j].index = i; + outs[j++].code = info->value; + } + } + /* Insertion sort: BIP-375 orders by code, then output index */ + for (i = 1; i < num_outs; ++i) { + tmp = outs[i]; + for (j = i; j && sp_output_cmp(&outs[j - 1], &tmp) > 0; --j) + outs[j] = outs[j - 1]; + outs[j] = tmp; + } + + /* Eligible input keys, their sum A, and the smallest outpoint */ + for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) { + const struct wally_psbt_input *input = psbt->inputs + i; + ret = sp_input_pubkey(input, &eligible[i], keys + num_eligible * EC_PUBLIC_KEY_LEN); + if (ret == WALLY_OK && eligible[i]) + ++num_eligible; + memcpy(outpoint, input->txhash, WALLY_TXHASH_LEN); + uint32_to_le_bytes(input->index, outpoint + WALLY_TXHASH_LEN); + if (!i || memcmp(outpoint, min_outpoint, sizeof(outpoint)) < 0) + memcpy(min_outpoint, outpoint, sizeof(outpoint)); + } + if (ret == WALLY_OK) /* Fails with no eligible inputs, or if their keys cancel out */ + ret = sp_pubkey_sum(keys, num_eligible, keys + num_eligible * EC_PUBLIC_KEY_LEN); + + /* input_hash = hash_BIP0352/Inputs(outpoint_L || A) */ + if (ret == WALLY_OK) { + memcpy(hash_in, min_outpoint, SP_OUTPOINT_LEN); + memcpy(hash_in + SP_OUTPOINT_LEN, keys + num_eligible * EC_PUBLIC_KEY_LEN, + EC_PUBLIC_KEY_LEN); + ret = wally_bip340_tagged_hash(hash_in, sizeof(hash_in), SP_TAG_INPUTS, + input_hash, sizeof(input_hash)); + if (ret == WALLY_OK && wally_ec_scalar_verify(input_hash, sizeof(input_hash)) != WALLY_OK) + ret = WALLY_EINVAL; + } + + /* Compute each output's script, restarting k for every new scan key */ + for (i = 0; ret == WALLY_OK && i < num_outs; ++i) { + if (!i || memcmp(outs[i].code, outs[i - 1].code, EC_PUBLIC_KEY_LEN)) { + secp256k1_pubkey s; + /* ecdh_shared_secret = input_hash * share */ + ret = sp_get_ecdh_share(psbt, outs[i].code, keys, eligible, share); + len = EC_PUBLIC_KEY_LEN; + if (ret == WALLY_OK && + (!pubkey_parse(&s, share, sizeof(share)) || + !secp256k1_ec_pubkey_tweak_mul(secp_ctx(), &s, input_hash) || + !pubkey_serialize(shared_secret, &len, &s, PUBKEY_COMPRESSED))) + ret = WALLY_EINVAL; + wally_clear(&s, sizeof(s)); + k = 0; + } + if (ret == WALLY_OK && k >= SP_K_MAX) + ret = WALLY_EINVAL; /* Too many outputs for this scan key */ + if (ret == WALLY_OK) + ret = sp_output_script(shared_secret, k++, + outs[i].code + EC_PUBLIC_KEY_LEN, + scripts + i * WALLY_SCRIPTPUBKEY_P2TR_LEN); + if (ret == WALLY_OK) { + /* A script already present must be the one we computed */ + const struct wally_psbt_output *out = psbt->outputs + outs[i].index; + if (out->script && (out->script_len != WALLY_SCRIPTPUBKEY_P2TR_LEN || + memcmp(out->script, scripts + i * WALLY_SCRIPTPUBKEY_P2TR_LEN, + WALLY_SCRIPTPUBKEY_P2TR_LEN))) + ret = WALLY_EINVAL; + } + } + + /* Only change the PSBT once every script has been computed and checked, + * and every missing one allocated, so that a failure changes nothing */ + for (i = 0; ret == WALLY_OK && i < num_outs; ++i) { + if (psbt->outputs[outs[i].index].script) + continue; + if (!(new_scripts[i] = wally_malloc(WALLY_SCRIPTPUBKEY_P2TR_LEN))) + ret = WALLY_ENOMEM; + else + memcpy(new_scripts[i], scripts + i * WALLY_SCRIPTPUBKEY_P2TR_LEN, + WALLY_SCRIPTPUBKEY_P2TR_LEN); + } + for (i = 0; ret == WALLY_OK && i < num_outs; ++i) { + struct wally_psbt_output *out = psbt->outputs + outs[i].index; + if (new_scripts[i]) { + out->script = new_scripts[i]; + out->script_len = WALLY_SCRIPTPUBKEY_P2TR_LEN; + new_scripts[i] = NULL; + set_any = true; + } + } + if (ret == WALLY_OK && set_any) /* BIP-375: No further changes once set */ + psbt->tx_modifiable_flags &= ~(WALLY_PSBT_TXMOD_INPUTS | WALLY_PSBT_TXMOD_OUTPUTS); + +done: + if (new_scripts) { + for (i = 0; i < num_outs; ++i) + wally_free(new_scripts[i]); + wally_free(new_scripts); + } + wally_free(outs); + clear_and_free(scripts, num_outs * WALLY_SCRIPTPUBKEY_P2TR_LEN); + clear_and_free(keys, (psbt->num_inputs + 1) * EC_PUBLIC_KEY_LEN); + wally_free(eligible); + wally_clear_4(input_hash, sizeof(input_hash), share, sizeof(share), + shared_secret, sizeof(shared_secret), hash_in, sizeof(hash_in)); + return ret; +} + int wally_psbt_get_id(const struct wally_psbt *psbt, uint32_t flags, unsigned char *bytes_out, size_t len) { struct wally_tx *tx; diff --git a/src/swig_java/swig.i b/src/swig_java/swig.i index dfd8b5a7a..ad526b4d0 100644 --- a/src/swig_java/swig.i +++ b/src/swig_java/swig.i @@ -725,6 +725,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %returns_void__(wally_psbt_add_global_scalar); %returns_struct(wally_psbt_blind_alloc, wally_map); %rename("psbt_blind") wally_psbt_blind_alloc; +%returns_void__(wally_psbt_compute_sp_scripts); %returns_void__(wally_psbt_clear_fallback_locktime); %returns_void__(wally_psbt_clear_input_amount); %returns_void__(wally_psbt_clear_input_amount_rangeproof); diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 86f1b7e37..bc016c913 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -1,5 +1,6 @@ import json import unittest +from ctypes import string_at from util import * FLAG_GRIND_R = 0x4 @@ -592,6 +593,136 @@ def build(version, num_inputs): wally_psbt_free(psbt2) wally_psbt_free(psbt) + def fix_sp_prevouts(self, psbt): + """Make P2WPKH prevout scripts commit to their BIP32 derivation keys. + + The BIP-375 vectors' P2WPKH prevout scripts do not hash to the input + public keys. wally requires that they do, as a receiver finds the + payment from the key that is actually spent. Output scripts depend + on the keys and outpoints only, so this does not change them. + """ + for i in range(psbt.contents.num_inputs): + inp = psbt.contents.inputs[i] + utxo = inp.witness_utxo + if not utxo or not inp.keypaths.num_items: + continue + script = string_at(utxo.contents.script, utxo.contents.script_len) + item = inp.keypaths.items[0] + pub_key = string_at(item.key, item.key_len) + if script[:2] != b'\x00\x14' or len(pub_key) != 33: + continue + h, h_len = make_cbuffer('00' * 20) + self.assertEqual(wally_hash160(pub_key, 33, h, h_len), WALLY_OK) + tx_out = pointer(wally_tx_output()) + script = b'\x00\x14' + bytes(h) + ret = wally_tx_output_init_alloc(utxo.contents.satoshi, script, len(script), tx_out) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_input_set_witness_utxo(byref(inp), tx_out), WALLY_OK) + wally_tx_output_free(tx_out) + + def get_sp_scripts(self, psbt): + """Return {output index: script} for the silent payment outputs""" + scripts = {} + for i in range(psbt.contents.num_outputs): + out = psbt.contents.outputs[i] + if wally_psbt_output_get_sp_v0_info_len(byref(out))[1]: + scripts[i] = string_at(out.script, out.script_len) if out.script else None + return scripts + + def test_silent_payment_scripts(self): + """Test computing BIP-375 silent payment output scripts""" + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + txmod_all = 0x3 # WALLY_PSBT_TXMOD_INPUTS | WALLY_PSBT_TXMOD_OUTPUTS + + num_computed = 0 + for case in cases['valid']: + if case.get('checks') == ['input_eligibility']: + # Only checks that a bare OP_2 is not a segwit v2 input; with + # no eligible input, no script can be computed + continue + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + scripts = self.get_sp_scripts(psbt) + b64 = self.to_base64(psbt) + ret = wally_psbt_compute_sp_scripts(psbt, 0) + if None not in scripts.values(): + # Every script is present: they check out, and nothing changes + self.assertEqual(ret, WALLY_OK, case['description']) + self.assertEqual(self.to_base64(psbt), b64) + # With the scripts removed, the same scripts are computed + for i in scripts: + out = byref(psbt.contents.outputs[i]) + self.assertEqual(wally_psbt_output_set_script(out, None, 0), WALLY_OK) + self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, txmod_all), WALLY_OK) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + self.assertEqual(self.get_sp_scripts(psbt), scripts, case['description']) + self.assertEqual(psbt.contents.tx_modifiable_flags & txmod_all, 0) + if scripts: + num_computed += 1 + # Modifiable flags cannot be set once the scripts are + self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, txmod_all), WALLY_OK) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + else: + # In progress with shares missing: fails and changes nothing + self.assertEqual(ret, WALLY_EINVAL, case['description']) + self.assertEqual(self.to_base64(psbt), b64) + wally_psbt_free(psbt) + self.assertGreater(num_computed, 10) + + # Every invalid vector is rejected by parsing or by computing + for case in cases['invalid']: + psbt = pointer(wally_psbt()) + ret = wally_psbt_from_base64(case['psbt'], 0, psbt) + if ret == WALLY_OK: + self.fix_sp_prevouts(psbt) + ret = wally_psbt_compute_sp_scripts(psbt, 0) + wally_psbt_free(psbt) + self.assertEqual(ret, WALLY_EINVAL, case['description']) + + # An input key that does not hash to its prevout script is refused + case = [c for c in cases['valid'] if 'single-signer using global' in c['description']][0] + psbt = self.parse_base64(case['psbt']) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + self.fix_sp_prevouts(psbt) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + + # Invalid arguments + self.assertEqual(wally_psbt_compute_sp_scripts(None, 0), WALLY_EINVAL) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 1), WALLY_EINVAL) + wally_psbt_free(psbt) + + # Finalized inputs no longer have keypaths or partial signatures: + # the key comes from the final witness or scriptSig instead + sig = bytes.fromhex('30' + '00' * 70 + '01') + for desc in ['one P2PKH input single-signer', + 'one P2WPKH input / two mixed outputs']: + case = [c for c in cases['valid'] if desc in c['description']][0] + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + scripts = self.get_sp_scripts(psbt) + inp = psbt.contents.inputs[0] + item = inp.keypaths.items[0] + pub_key = string_at(item.key, item.key_len) + wally_map_clear(byref(inp.keypaths)) + wally_map_clear(byref(inp.signatures)) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + if 'P2PKH' in desc: + script_sig = bytes([len(sig)]) + sig + bytes([len(pub_key)]) + pub_key + ret = wally_psbt_input_set_final_scriptsig(byref(inp), script_sig, len(script_sig)) + else: + witness = pointer(wally_tx_witness_stack()) + self.assertEqual(wally_tx_witness_stack_init_alloc(2, witness), WALLY_OK) + for w in [sig, pub_key]: + self.assertEqual(wally_tx_witness_stack_add(witness, w, len(w)), WALLY_OK) + ret = wally_psbt_input_set_final_witness(byref(inp), witness) + wally_tx_witness_stack_free(witness) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK, desc) + self.assertEqual(self.get_sp_scripts(psbt), scripts) + wally_psbt_free(psbt) + def test_silent_payment_outputs(self): """Test BIP-375 silent payment output fields""" # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b @@ -697,6 +828,8 @@ def with_output_fields(version, fields): tx_out = pointer(wally_tx_output()) self.assertEqual(wally_tx_output_init_alloc(1000, b'\x51', 1, tx_out), WALLY_OK) self.assertEqual(wally_psbt_add_tx_output_at(psbt, 0, 0, tx_out), WALLY_OK) + # BIP-375: Not modifiable once a silent payment script is set + self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, 0), WALLY_OK) ret, length = wally_psbt_get_length(psbt, 0) self.assertEqual(ret, WALLY_OK) buf, buf_len = make_cbuffer('00' * length) diff --git a/src/test/util.py b/src/test/util.py index 8d06380ad..f403ca190 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -476,6 +476,7 @@ class wally_psbt(Structure): ('wally_psbt_clone_alloc', c_int, [POINTER(wally_psbt), c_uint32, POINTER(POINTER(wally_psbt))]), ('wally_psbt_combine', c_int, [POINTER(wally_psbt), POINTER(wally_psbt)]), ('wally_psbt_combine_ex', c_int, [POINTER(wally_psbt), c_uint32, POINTER(wally_psbt)]), + ('wally_psbt_compute_sp_scripts', c_int, [POINTER(wally_psbt), c_uint32]), ('wally_psbt_extract', c_int, [POINTER(wally_psbt), c_uint32, POINTER(POINTER(wally_tx))]), ('wally_psbt_finalize', c_int, [POINTER(wally_psbt), c_uint32]), ('wally_psbt_finalize_input', c_int, [POINTER(wally_psbt), c_size_t, c_uint32]), diff --git a/src/wasm_package/src/functions.js b/src/wasm_package/src/functions.js index 11108ce47..59f1176d3 100644 --- a/src/wasm_package/src/functions.js +++ b/src/wasm_package/src/functions.js @@ -332,6 +332,7 @@ export const psbt_clear_output_value_rangeproof = wrap('wally_psbt_clear_output_ export const psbt_clone = wrap('wally_psbt_clone_alloc', [T.OpaqueRef, T.Int32, T.DestPtrPtr(T.OpaqueRef)]); export const psbt_combine = wrap('wally_psbt_combine', [T.OpaqueRef, T.OpaqueRef]); export const psbt_combine_ex = wrap('wally_psbt_combine_ex', [T.OpaqueRef, T.Int32, T.OpaqueRef]); +export const psbt_compute_sp_scripts = wrap('wally_psbt_compute_sp_scripts', [T.OpaqueRef, T.Int32]); export const psbt_extract = wrap('wally_psbt_extract', [T.OpaqueRef, T.Int32, T.DestPtrPtr(T.OpaqueRef)]); export const psbt_finalize = wrap('wally_psbt_finalize', [T.OpaqueRef, T.Int32]); export const psbt_finalize_input = wrap('wally_psbt_finalize_input', [T.OpaqueRef, T.Int32, T.Int32]); diff --git a/src/wasm_package/src/index.d.ts b/src/wasm_package/src/index.d.ts index 4f01e273e..7b26c7e2f 100644 --- a/src/wasm_package/src/index.d.ts +++ b/src/wasm_package/src/index.d.ts @@ -292,6 +292,7 @@ export function psbt_clear_output_value_rangeproof(psbt: Ref_wally_psbt, index: export function psbt_clone(psbt: Ref_wally_psbt, flags: number): Ref_wally_psbt; export function psbt_combine(psbt: Ref_wally_psbt, source: Ref_wally_psbt): void; export function psbt_combine_ex(psbt: Ref_wally_psbt, flags: number, source: Ref_wally_psbt): void; +export function psbt_compute_sp_scripts(psbt: Ref_wally_psbt, flags: number): void; export function psbt_extract(psbt: Ref_wally_psbt, flags: number): Ref_wally_tx; export function psbt_finalize(psbt: Ref_wally_psbt, flags: number): void; export function psbt_finalize_input(psbt: Ref_wally_psbt, index: number, flags: number): void; diff --git a/tools/wasm_exports.sh b/tools/wasm_exports.sh index 7a3998f45..7ba774e36 100644 --- a/tools/wasm_exports.sh +++ b/tools/wasm_exports.sh @@ -226,6 +226,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_psbt_clone_alloc' \ ,'_wally_psbt_combine' \ ,'_wally_psbt_combine_ex' \ +,'_wally_psbt_compute_sp_scripts' \ ,'_wally_psbt_extract' \ ,'_wally_psbt_finalize' \ ,'_wally_psbt_finalize_input' \ From 66eb6bba525c66f81cafc030f3a0f09b7557e93d Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 16:27:43 +0900 Subject: [PATCH 07/11] psbt: Add BIP-375 ECDH shares from input keys A signer holding the private keys of a transaction's inputs writes the ECDH shares and DLEQ proofs that wally_psbt_compute_sp_scripts() reads. Add wally_psbt_add_sp_ecdh_shares(). It takes one private key per input and adds one share per scan key when the keys cover every eligible input, or a share per held input otherwise. A taproot key is accepted with either parity, and a key that does not match its input is refused. Secret multiplications go through secp256k1's constant time ECDH. Tests rebuild every finalizable BIP-375 vector's scripts from its input keys both ways, and cover eligibility, parity, clone and combine. --- include/wally.hpp | 6 + include/wally_psbt.h | 31 ++ src/internal.h | 4 + src/psbt.c | 152 +++++++++ src/sign.c | 10 + src/swig_java/swig.i | 2 + src/swig_python/swig.i | 1 + src/test/test_psbt.py | 529 ++++++++++++++++++++++++++++++ src/test/util.py | 1 + src/wasm_package/src/functions.js | 1 + src/wasm_package/src/index.d.ts | 1 + tools/wasm_exports.sh | 1 + 12 files changed, 739 insertions(+) diff --git a/include/wally.hpp b/include/wally.hpp index 867a2af9f..0809f4972 100644 --- a/include/wally.hpp +++ b/include/wally.hpp @@ -1306,6 +1306,12 @@ inline int psbt_add_output_taproot_keypath(const PSBT& psbt, uint32_t index, uin return detail::check_ret(__FUNCTION__, ret); } +template +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 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); diff --git a/include/wally_psbt.h b/include/wally_psbt.h index ac10a370a..8c11114b9 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -2059,6 +2059,37 @@ WALLY_CORE_API int wally_psbt_compute_sp_scripts( struct wally_psbt *psbt, uint32_t flags); +/** + * Add BIP-375 ECDH shares and DLEQ proofs for silent payment outputs. + * + * Given the private keys of the inputs the caller holds, adds an ECDH share + * and DLEQ proof for every distinct scan key of the PSBT's silent payment + * outputs. If the keys cover every eligible input, one global share per scan + * key is added; otherwise a share per held input. Call + * `wally_psbt_compute_sp_scripts` afterwards to set the output scripts. + * + * :param psbt: The PSBTv2 to update. + * :param priv_keys: One private key per input, in input order. For each + *| eligible input held, the key for its BIP-352 public key: the output + *| (tweaked) key for taproot, of either parity. All zero for inputs not + *| held; keys for ineligible inputs are ignored. + * :param priv_keys_len: Length of ``priv_keys`` in bytes. Must be the number + *| of inputs times `EC_PRIVATE_KEY_LEN`. + * :param aux_rand: Fresh random data for the DLEQ proofs. + * :param aux_rand_len: Length of ``aux_rand`` in bytes. Must be 32. + * :param flags: For future use. Must be 0. + * + * .. note:: Returns `WALLY_EINVAL` if a key does not match its input. The PSBT + *| is unchanged if any step fails. + */ +WALLY_CORE_API int wally_psbt_add_sp_ecdh_shares( + struct wally_psbt *psbt, + const unsigned char *priv_keys, + size_t priv_keys_len, + const unsigned char *aux_rand, + size_t aux_rand_len, + uint32_t flags); + /** * Return the calculated transaction lock time of a PSBT. * diff --git a/src/internal.h b/src/internal.h index b38fa6be9..0f31c8e7a 100644 --- a/src/internal.h +++ b/src/internal.h @@ -66,6 +66,10 @@ WALLY_INTERNAL_API int seckey_verify(const unsigned char *seckey); WALLY_INTERNAL_API int seckey_negate(unsigned char *seckey); WALLY_INTERNAL_API int seckey_tweak_add(unsigned char *seckey, const unsigned char *tweak); WALLY_INTERNAL_API int seckey_tweak_mul(unsigned char *seckey, const unsigned char *tweak); +/* Multiply a compressed public key by a secret scalar in constant time */ +WALLY_INTERNAL_API bool ec_public_key_mul_secret(const unsigned char *pub_key, + const unsigned char *priv_key, + unsigned char *bytes_out); WALLY_INTERNAL_API int keypair_create(secp256k1_keypair *keypair, const unsigned char *priv_key); WALLY_INTERNAL_API int keypair_xonly_pub(secp256k1_xonly_pubkey *xpubkey, const secp256k1_keypair *keypair); WALLY_INTERNAL_API int keypair_sec(unsigned char *output, const secp256k1_keypair *keypair); diff --git a/src/psbt.c b/src/psbt.c index 349e4a787..c9516cae3 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -5092,6 +5092,158 @@ int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) return ret; } +#define sp_swap_maps(a, b) do { struct wally_map _t = a; a = b; b = _t; } while (0) + +/* Add an ECDH share and its DLEQ proof for scan_key to the given maps */ +static int sp_add_ecdh_share(struct wally_map *shares, struct wally_map *proofs, + const unsigned char *scan_key, const unsigned char *priv_key, + const unsigned char *aux_rand, size_t aux_rand_len) +{ + unsigned char share[EC_PUBLIC_KEY_LEN], proof[WALLY_DLEQ_PROOF_LEN]; + int ret = WALLY_OK; + + if (!ec_public_key_mul_secret(scan_key, priv_key, share)) + ret = WALLY_EINVAL; + if (ret == WALLY_OK) + ret = wally_dleq_proof_generate(priv_key, EC_PRIVATE_KEY_LEN, + scan_key, EC_PUBLIC_KEY_LEN, + aux_rand, aux_rand_len, NULL, 0, NULL, 0, 0, + proof, sizeof(proof)); + if (ret == WALLY_OK) + ret = wally_map_replace(shares, scan_key, EC_PUBLIC_KEY_LEN, share, sizeof(share)); + if (ret == WALLY_OK) + ret = wally_map_replace(proofs, scan_key, EC_PUBLIC_KEY_LEN, proof, sizeof(proof)); + wally_clear_2(share, sizeof(share), proof, sizeof(proof)); + return ret; +} + +int wally_psbt_add_sp_ecdh_shares(struct wally_psbt *psbt, + const unsigned char *priv_keys, size_t priv_keys_len, + const unsigned char *aux_rand, size_t aux_rand_len, + uint32_t flags) +{ + unsigned char *keys = NULL, pub_key[EC_PUBLIC_KEY_LEN], key_sum[EC_PRIVATE_KEY_LEN]; + const unsigned char **scan_keys = NULL; + struct wally_psbt *clone = NULL; + bool *held = NULL, is_eligible; + size_t i, j, num_scan_keys = 0, num_eligible = 0, num_held = 0, is_pset; + int ret = WALLY_OK; + + if (!psbt_is_valid(psbt) || psbt->version != PSBT_2 || !psbt->num_inputs || + !priv_keys || priv_keys_len != psbt->num_inputs * EC_PRIVATE_KEY_LEN || + !aux_rand || aux_rand_len != SHA256_LEN || flags || + wally_psbt_is_elements(psbt, &is_pset) != WALLY_OK || is_pset) + return WALLY_EINVAL; + + if (!secp_ctx()) + return WALLY_ENOMEM; + + /* The distinct scan keys of the silent payment outputs */ + scan_keys = wally_calloc(psbt->num_outputs * sizeof(*scan_keys) + 1); + keys = wally_calloc(psbt->num_inputs * EC_PRIVATE_KEY_LEN); + held = wally_calloc(psbt->num_inputs * sizeof(*held)); + if (!scan_keys || !keys || !held) { + ret = WALLY_ENOMEM; + goto done; + } + for (i = 0; i < psbt->num_outputs; ++i) { + const struct wally_map_item *info; + info = wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO); + for (j = 0; info && j < num_scan_keys; ++j) + if (!memcmp(scan_keys[j], info->value, EC_PUBLIC_KEY_LEN)) + info = NULL; /* Already have this scan key */ + if (info) + scan_keys[num_scan_keys++] = info->value; + } + if (!num_scan_keys) + goto done; /* No silent payment outputs */ + + /* Match each given key to its eligible input, normalizing taproot keys */ + for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) { + const unsigned char *priv_key = priv_keys + i * EC_PRIVATE_KEY_LEN; + unsigned char *key = keys + i * EC_PRIVATE_KEY_LEN; + unsigned char given_pub[EC_PUBLIC_KEY_LEN]; + const unsigned char *script; + size_t script_len; + + ret = sp_input_pubkey(psbt->inputs + i, &is_eligible, pub_key); + if (ret != WALLY_OK || !is_eligible) + continue; /* Keys for ineligible inputs are ignored */ + ++num_eligible; + if (mem_is_zero(priv_key, EC_PRIVATE_KEY_LEN)) + continue; /* Not held */ + memcpy(key, priv_key, EC_PRIVATE_KEY_LEN); + ret = wally_ec_public_key_from_private_key(key, EC_PRIVATE_KEY_LEN, + given_pub, sizeof(given_pub)); + if (ret == WALLY_OK) + ret = sp_input_prevout_script(psbt->inputs + i, &script, &script_len); + if (ret == WALLY_OK && script_len == WALLY_SCRIPTPUBKEY_P2TR_LEN && + script[0] == OP_1 && given_pub[0] == 0x03 && + !memcmp(given_pub + 1, pub_key + 1, EC_XONLY_PUBLIC_KEY_LEN)) { + /* Taproot: use the key for the even-y output key */ + if (!seckey_negate(key)) + ret = WALLY_EINVAL; + else + given_pub[0] = 0x02; + } + if (ret == WALLY_OK && memcmp(given_pub, pub_key, EC_PUBLIC_KEY_LEN)) + ret = WALLY_EINVAL; /* Key does not belong to this input */ + if (ret == WALLY_OK) { + held[i] = true; + ++num_held; + } + wally_clear(given_pub, sizeof(given_pub)); + } + + if (ret != WALLY_OK || !num_held) + goto done; + + /* Add to a copy, as a failed allocation must not leave some shares behind */ + if ((ret = wally_psbt_clone_alloc(psbt, 0, &clone)) != WALLY_OK) + goto done; + + if (num_held == num_eligible) { + /* We hold every eligible input: add a global share per scan key */ + wally_clear(key_sum, sizeof(key_sum)); + for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) + if (held[i]) + ret = wally_ec_scalar_add_to(key_sum, sizeof(key_sum), + keys + i * EC_PRIVATE_KEY_LEN, EC_PRIVATE_KEY_LEN); + if (ret == WALLY_OK && !seckey_verify(key_sum)) + ret = WALLY_EINVAL; /* Keys sum to zero; only the final sum counts */ + for (j = 0; ret == WALLY_OK && j < num_scan_keys; ++j) + ret = sp_add_ecdh_share(&clone->sp_ecdh_shares, &clone->sp_dleq_proofs, + scan_keys[j], key_sum, aux_rand, aux_rand_len); + } else { + /* Add a share per held input and scan key */ + for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) { + struct wally_psbt_input *input = clone->inputs + i; + for (j = 0; held[i] && ret == WALLY_OK && j < num_scan_keys; ++j) + ret = sp_add_ecdh_share(&input->sp_ecdh_shares, &input->sp_dleq_proofs, + scan_keys[j], keys + i * EC_PRIVATE_KEY_LEN, + aux_rand, aux_rand_len); + } + } + + if (ret == WALLY_OK) { + /* Everything succeeded: take the new maps, which cannot fail */ + sp_swap_maps(psbt->sp_ecdh_shares, clone->sp_ecdh_shares); + sp_swap_maps(psbt->sp_dleq_proofs, clone->sp_dleq_proofs); + for (i = 0; i < psbt->num_inputs; ++i) { + sp_swap_maps(psbt->inputs[i].sp_ecdh_shares, clone->inputs[i].sp_ecdh_shares); + sp_swap_maps(psbt->inputs[i].sp_dleq_proofs, clone->inputs[i].sp_dleq_proofs); + } + } + +done: + wally_psbt_free(clone); + wally_free(scan_keys); + clear_and_free(keys, psbt->num_inputs * EC_PRIVATE_KEY_LEN); + wally_free(held); + wally_clear_2(pub_key, sizeof(pub_key), key_sum, sizeof(key_sum)); + return ret; +} + int wally_psbt_get_id(const struct wally_psbt *psbt, uint32_t flags, unsigned char *bytes_out, size_t len) { struct wally_tx *tx; diff --git a/src/sign.c b/src/sign.c index 8a1e3ddff..a040f2730 100644 --- a/src/sign.c +++ b/src/sign.c @@ -817,6 +817,16 @@ static bool dleq_point_mul_secret(const secp256k1_pubkey *point, const unsigned return ok; } +bool ec_public_key_mul_secret(const unsigned char *pub_key, const unsigned char *priv_key, + unsigned char *bytes_out) +{ + secp256k1_pubkey point; + bool ok = secp_ctx() && pubkey_parse(&point, pub_key, EC_PUBLIC_KEY_LEN) && + secp256k1_ecdh(secp_ctx(), bytes_out, &point, priv_key, dleq_ecdh_point, NULL); + wally_clear(&point, sizeof(point)); + return ok; +} + /* p = s*X - e*Y */ static bool dleq_point_sub_mul(const unsigned char *s, const secp256k1_pubkey *x, const unsigned char *e, const secp256k1_pubkey *y, diff --git a/src/swig_java/swig.i b/src/swig_java/swig.i index ad526b4d0..692cc771a 100644 --- a/src/swig_java/swig.i +++ b/src/swig_java/swig.i @@ -299,6 +299,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %apply(char *STRING, size_t LENGTH) { (const unsigned char* parent160, size_t parent160_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* pass, size_t pass_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* priv_key, size_t priv_key_len) }; +%apply(char *STRING, size_t LENGTH) { (const unsigned char* priv_keys, size_t priv_keys_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* proof, size_t proof_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* pub_key, size_t pub_key_len) }; %apply(char *STRING, size_t LENGTH) { (const unsigned char* pub_key_a, size_t pub_key_a_len) }; @@ -723,6 +724,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) { %returns_void__(wally_psbt_add_output_taproot_keypath); %returns_void__(wally_psbt_add_tx_output_at); %returns_void__(wally_psbt_add_global_scalar); +%returns_void__(wally_psbt_add_sp_ecdh_shares); %returns_struct(wally_psbt_blind_alloc, wally_map); %rename("psbt_blind") wally_psbt_blind_alloc; %returns_void__(wally_psbt_compute_sp_scripts); diff --git a/src/swig_python/swig.i b/src/swig_python/swig.i index ef2561f20..dbb168531 100644 --- a/src/swig_python/swig.i +++ b/src/swig_python/swig.i @@ -379,6 +379,7 @@ static void destroy_words(PyObject *obj) { (void)obj; } %pybuffer_nullable_binary(const unsigned char* parent160, size_t parent160_len); %pybuffer_nullable_binary(const unsigned char* pass, size_t pass_len); %pybuffer_nullable_binary(const unsigned char* priv_key, size_t priv_key_len); +%pybuffer_nullable_binary(const unsigned char* priv_keys, size_t priv_keys_len); %pybuffer_nullable_binary(const unsigned char* proof, size_t proof_len); %pybuffer_nullable_binary(const unsigned char* pub_key, size_t pub_key_len); %pybuffer_nullable_binary(const unsigned char* pub_key_a, size_t pub_key_a_len); diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index bc016c913..82579d0a2 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -723,6 +723,535 @@ def test_silent_payment_scripts(self): self.assertEqual(self.get_sp_scripts(psbt), scripts) wally_psbt_free(psbt) + def test_silent_payment_add_shares(self): + """Test adding BIP-375 ECDH shares from input private keys""" + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + aux_rand = b'\x07' * 32 + + def strip_sp(psbt, scripts): + """Remove every share, proof and silent payment script""" + for p in [psbt.contents] + [psbt.contents.inputs[i] + for i in range(psbt.contents.num_inputs)]: + wally_map_clear(byref(p.sp_ecdh_shares)) + wally_map_clear(byref(p.sp_dleq_proofs)) + for i in scripts: + out = byref(psbt.contents.outputs[i]) + self.assertEqual(wally_psbt_output_set_script(out, None, 0), WALLY_OK) + + def get_keys(case, held): + return b''.join(bytes.fromhex(inp.get('private_key') or '00' * 32) if h else bytes(32) + for inp, h in zip(case['supplementary']['inputs'], held)) + + def num_shares(psbt): + return [psbt.contents.sp_ecdh_shares.num_items] + \ + [psbt.contents.inputs[i].sp_ecdh_shares.num_items + for i in range(psbt.contents.num_inputs)] + + num_global, num_per_input = 0, 0 + for case in cases['valid']: + if case.get('checks') == ['input_eligibility']: + continue # No eligible input, see test_silent_payment_scripts + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + scripts = self.get_sp_scripts(psbt) + num_inputs = psbt.contents.num_inputs + scan_keys = set() + for i in scripts: + buf, buf_len = make_cbuffer('00' * WALLY_SILENT_PAYMENT_KEYS_LEN) + out = byref(psbt.contents.outputs[i]) + self.assertEqual(wally_psbt_output_get_sp_v0_info(out, buf, buf_len)[0], WALLY_OK) + scan_keys.add(bytes(buf)[:33]) + num_scan_keys = len(scan_keys) + wally_psbt_free(psbt) + if not scripts or None in scripts.values(): + continue # Nothing to compare the computed scripts against + + # Holding every input's key gives one global share per scan key + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + strip_sp(psbt, scripts) + keys = get_keys(case, [True] * num_inputs) + ret = wally_psbt_add_sp_ecdh_shares(psbt, keys, len(keys), aux_rand, 32, 0) + self.assertEqual(ret, WALLY_OK, case['description']) + self.assertEqual(num_shares(psbt), [num_scan_keys] + [0] * num_inputs) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + self.assertEqual(self.get_sp_scripts(psbt), scripts, case['description']) + num_global += 1 + wally_psbt_free(psbt) + + # Holding one key at a time gives per-input shares + held = [bool(inp.get('private_key')) for inp in case['supplementary']['inputs']] + if sum(held) < 2: + continue + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + strip_sp(psbt, scripts) + for i in range(num_inputs): + if not held[i]: + continue + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + keys = get_keys(case, [j == i for j in range(num_inputs)]) + ret = wally_psbt_add_sp_ecdh_shares(psbt, keys, len(keys), aux_rand, 32, 0) + self.assertEqual(ret, WALLY_OK, case['description']) + if num_shares(psbt)[0]: + break # Only one input is eligible, so its share is global + if not num_shares(psbt)[0]: + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + self.assertEqual(self.get_sp_scripts(psbt), scripts, case['description']) + num_per_input += 1 + wally_psbt_free(psbt) + self.assertGreater(num_global, 10) + self.assertGreater(num_per_input, 2) + + case = [c for c in cases['valid'] if 'single-signer using global' in c['description']][0] + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + keys = get_keys(case, [True, True]) + + # A key that does not belong to its input is refused + bad = keys[32:] + keys[:32] + self.assertEqual(wally_psbt_add_sp_ecdh_shares(psbt, bad, 64, aux_rand, 32, 0), WALLY_EINVAL) + + # Holding no keys adds nothing + self.assertEqual(wally_psbt_add_sp_ecdh_shares(psbt, bytes(64), 64, aux_rand, 32, 0), WALLY_OK) + + # Invalid arguments + for args in [ + (None, keys, 64, aux_rand, 32, 0), # Missing PSBT + (psbt, None, 64, aux_rand, 32, 0), # Missing keys + (psbt, keys, 32, aux_rand, 32, 0), # One key for two inputs + (psbt, keys, 64, None, 32, 0), # Missing aux_rand + (psbt, keys, 64, aux_rand, 31, 0), # Short aux_rand + (psbt, keys, 64, aux_rand, 32, 1), # Unknown flags + ]: + self.assertEqual(wally_psbt_add_sp_ecdh_shares(*args), WALLY_EINVAL) + wally_psbt_free(psbt) + + def test_silent_payment_inputs(self): + """Test BIP-352 input eligibility when computing silent payment scripts""" + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + case = [c for c in cases['valid'] if 'single-signer using global' in c['description']][0] + keys = [bytes.fromhex(i['private_key']) for i in case['supplementary']['inputs']] + aux_rand = b'\x07' * 32 + + def hash160(data): + h, h_len = make_cbuffer('00' * 20) + self.assertEqual(wally_hash160(data, len(data), h, h_len), WALLY_OK) + return bytes(h) + + def set_prevout(inp, script): + tx_out = pointer(wally_tx_output()) + ret = wally_tx_output_init_alloc(inp.witness_utxo.contents.satoshi, + script, len(script), tx_out) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_input_set_witness_utxo(byref(inp), tx_out), WALLY_OK) + wally_tx_output_free(tx_out) + + def recompute(psbt, input_keys): + """Replace the shares and scripts with ones computed from input_keys""" + for p in [psbt.contents] + [psbt.contents.inputs[i] for i in range(2)]: + wally_map_clear(byref(p.sp_ecdh_shares)) + wally_map_clear(byref(p.sp_dleq_proofs)) + for i in self.get_sp_scripts(psbt): + out = byref(psbt.contents.outputs[i]) + self.assertEqual(wally_psbt_output_set_script(out, None, 0), WALLY_OK) + k = b''.join(input_keys) + ret = wally_psbt_add_sp_ecdh_shares(psbt, k, len(k), aux_rand, 32, 0) + if ret == WALLY_OK: + ret = wally_psbt_compute_sp_scripts(psbt, 0) + return ret, self.get_sp_scripts(psbt) + + def get_psbt(): + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + return psbt + + psbt = get_psbt() + expected = self.get_sp_scripts(psbt) + pub1 = string_at(psbt.contents.inputs[1].keypaths.items[0].key, 33) + wally_psbt_free(psbt) + + # P2SH-P2WPKH: eligible, with the redeem script from the PSBT or + # once finalized from the scriptSig; it must match the prevout + redeem = b'\x00\x14' + hash160(pub1) + p2sh = b'\xa9\x14' + hash160(redeem) + b'\x87' + multisig = b'\x51\x21' + pub1 + b'\x51\xae' # 1-of-1, not eligible + p2sh_multisig = b'\xa9\x14' + hash160(multisig) + b'\x87' + for prevout, redeem_field, finalize, expected_ret in [ + (p2sh, redeem, False, WALLY_OK), + (p2sh, None, False, WALLY_EINVAL), # Missing redeem script + (p2sh_multisig, redeem, False, WALLY_EINVAL), # Not the prevout's redeem script + (p2sh, None, True, WALLY_OK), # Finalized + ]: + psbt = get_psbt() + inp = psbt.contents.inputs[1] + set_prevout(inp, prevout) + if redeem_field: + ret = wally_psbt_input_set_redeem_script(byref(inp), redeem_field, len(redeem_field)) + self.assertEqual(ret, WALLY_OK) + if finalize: + wally_map_clear(byref(inp.keypaths)) + wally_map_clear(byref(inp.signatures)) + script_sig = bytes([len(redeem)]) + redeem + ret = wally_psbt_input_set_final_scriptsig(byref(inp), script_sig, len(script_sig)) + self.assertEqual(ret, WALLY_OK) + witness = pointer(wally_tx_witness_stack()) + self.assertEqual(wally_tx_witness_stack_init_alloc(2, witness), WALLY_OK) + for w in [bytes.fromhex('30' + '00' * 70 + '01'), pub1]: + self.assertEqual(wally_tx_witness_stack_add(witness, w, len(w)), WALLY_OK) + self.assertEqual(wally_psbt_input_set_final_witness(byref(inp), witness), WALLY_OK) + wally_tx_witness_stack_free(witness) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), expected_ret) + # A signer holding both keys gets the vector's scripts, or nothing: + # never a script that leaves out or wrongly includes input 1 + ret, scripts = recompute(psbt, keys) + self.assertEqual(ret, expected_ret) + if ret == WALLY_OK: + self.assertEqual(scripts, expected) + wally_psbt_free(psbt) + + # P2PKH with an uncompressed key: BIP-352 skips the input, so its + # key, if given, is ignored + psbt = get_psbt() + inp = psbt.contents.inputs[1] + full, full_len = make_cbuffer('00' * 65) + self.assertEqual(wally_ec_public_key_decompress(pub1, 33, full, full_len), WALLY_OK) + set_prevout(inp, b'\x76\xa9\x14' + hash160(bytes(full)) + b'\x88\xac') + wally_map_clear(byref(inp.keypaths)) + wally_map_clear(byref(inp.signatures)) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) # Key unknown + script_sig = bytes([72]) + bytes.fromhex('30' + '00' * 70 + '01') + bytes([65]) + bytes(full) + ret = wally_psbt_input_set_final_scriptsig(byref(inp), script_sig, len(script_sig)) + self.assertEqual(ret, WALLY_OK) + ret, with_key = recompute(psbt, keys) + self.assertEqual(ret, WALLY_OK) + self.assertNotEqual(with_key, expected) # Only input 0 contributes + self.assertEqual(recompute(psbt, [keys[0], bytes(32)]), (WALLY_OK, with_key)) + wally_psbt_free(psbt) + + # Taproot: the key for the output key is accepted with either parity + case = [c for c in cases['valid'] if 'two P2TR inputs, neither' in c['description']][0] + psbt = self.parse_base64(case['psbt']) + expected = self.get_sp_scripts(psbt) + n = 0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141 + tr_keys = [bytes.fromhex(i['private_key']) for i in case['supplementary']['inputs']] + negated = [(n - int.from_bytes(k, 'big')).to_bytes(32, 'big') for k in tr_keys] + for input_keys in [tr_keys, negated]: + self.assertEqual(recompute(psbt, input_keys), (WALLY_OK, expected)) + + # Shares and proofs survive cloning and combining + clone = pointer(wally_psbt()) + self.assertEqual(wally_psbt_clone_alloc(psbt, 0, clone), WALLY_OK) + self.assertEqual(self.to_base64(clone), self.to_base64(psbt)) + stripped = pointer(wally_psbt()) + self.assertEqual(wally_psbt_clone_alloc(psbt, 0, stripped), WALLY_OK) + wally_map_clear(byref(stripped.contents.sp_ecdh_shares)) + wally_map_clear(byref(stripped.contents.sp_dleq_proofs)) + self.assertEqual(wally_psbt_combine(stripped, psbt), WALLY_OK) + self.assertEqual(self.to_base64(stripped), self.to_base64(psbt)) + for p in [psbt, clone, stripped]: + wally_psbt_free(p) + + def test_silent_payment_crafted_inputs(self): + """Test inputs crafted to be left out of the silent payment key sum. + + Leaving out an input gives a script the receiver cannot find, so an + input is only skipped when the PSBT proves that BIP-352 skips it. + """ + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + case = [c for c in cases['valid'] if 'single-signer using global' in c['description']][0] + keys = b''.join(bytes.fromhex(i['private_key']) for i in case['supplementary']['inputs']) + nums = bytes.fromhex('50929b74c1a04954b78b4b6035e97a5e078a5a0f28ec96d547bfee9ace803ac0') + sig = bytes.fromhex('30' + '00' * 70 + '01') + + def hash160(data): + h, h_len = make_cbuffer('00' * 20) + self.assertEqual(wally_hash160(data, len(data), h, h_len), WALLY_OK) + return bytes(h) + + def get_psbt(): + """The vector with no shares or scripts, ready for new ones""" + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + for p in [psbt.contents] + [psbt.contents.inputs[i] for i in range(2)]: + wally_map_clear(byref(p.sp_ecdh_shares)) + wally_map_clear(byref(p.sp_dleq_proofs)) + for i in self.get_sp_scripts(psbt): + out = byref(psbt.contents.outputs[i]) + self.assertEqual(wally_psbt_output_set_script(out, None, 0), WALLY_OK) + return psbt + + def set_prevout(inp, script): + tx_out = pointer(wally_tx_output()) + ret = wally_tx_output_init_alloc(inp.witness_utxo.contents.satoshi, + script, len(script), tx_out) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_input_set_witness_utxo(byref(inp), tx_out), WALLY_OK) + wally_tx_output_free(tx_out) + + def set_witness(inp, items): + witness = pointer(wally_tx_witness_stack()) + self.assertEqual(wally_tx_witness_stack_init_alloc(max(len(items), 1), witness), WALLY_OK) + for item in items: + self.assertEqual(wally_tx_witness_stack_add(witness, item, len(item)), WALLY_OK) + self.assertEqual(wally_psbt_input_set_final_witness(byref(inp), witness), WALLY_OK) + wally_tx_witness_stack_free(witness) + + def set_script_sig(inp, script_sig): + ret = wally_psbt_input_set_final_scriptsig(byref(inp), script_sig, len(script_sig)) + self.assertEqual(ret, WALLY_OK) + + def compute(psbt, input_keys=keys): + """Add shares and compute scripts, returning the scripts if that works""" + ret = wally_psbt_add_sp_ecdh_shares(psbt, input_keys, 64, b'\x07' * 32, 32, 0) + if ret == WALLY_OK: + ret = wally_psbt_compute_sp_scripts(psbt, 0) + return ret, self.get_sp_scripts(psbt) if ret == WALLY_OK else None + + psbt = get_psbt() + inp = psbt.contents.inputs[1] + item = inp.keypaths.items[0] + pub1 = string_at(item.key, item.key_len) + ret, expected = compute(psbt) + self.assertEqual(ret, WALLY_OK) + wally_psbt_free(psbt) + + def tweak(root): + out, out_len = make_cbuffer('00' * 33) + ret = wally_ec_public_key_bip341_tweak(nums, 32, root, len(root) if root else 0, + 0, out, out_len) + self.assertEqual(ret, WALLY_OK) + return b'\x51\x20' + bytes(out)[1:] + + def without_keys(prevout): + """A PSBT whose input 1 pays prevout, with no key data on the input""" + psbt = get_psbt() + inp = psbt.contents.inputs[1] + wally_map_clear(byref(inp.keypaths)) + wally_map_clear(byref(inp.signatures)) + set_prevout(inp, prevout) + return psbt, inp + + p2wpkh = b'\x00\x14' + hash160(pub1) + p2pkh = b'\x76\xa9\x14' + hash160(pub1) + b'\x88\xac' + p2sh = b'\xa9\x14' + hash160(p2wpkh) + b'\x87' + p2sh_sig = bytes([len(p2wpkh)]) + p2wpkh + p2tr = b'\x51\x20' + pub1[1:] + + # Final fields that do not spend the prevout are refused, not skipped + for prevout, script_sig, witness in [ + (p2wpkh, None, []), # Empty witness + (p2wpkh, None, [b'\x01']), # Junk witness + (p2wpkh, b'\x00', None), # Junk scriptSig + (p2pkh, b'\x00', None), # Junk scriptSig + (p2pkh, None, [b'\x01']), # Junk witness + (p2sh, b'\x00', None), # Not the redeem script + (p2sh, b'\x51', None), # Not a push + (p2sh, p2sh_sig, [b'\x01']), # P2SH-P2WPKH with a junk witness + (p2sh, p2sh_sig, None), # P2SH-P2WPKH with no witness + (p2sh, b'\x00' + p2sh_sig, [sig, pub1]), # Extra data before the redeem script + (p2tr, None, []), # Empty witness + (p2tr, None, [b'\x51', b'\xc0' + bytes(31)]), # Short control block + ]: + psbt, inp = without_keys(prevout) + if script_sig is not None: + set_script_sig(inp, script_sig) + if witness is not None: + set_witness(inp, witness) + self.assertEqual(compute(psbt), (WALLY_EINVAL, None), (prevout, script_sig, witness)) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + wally_psbt_free(psbt) + + # A claim of the NUMS internal key must match the output key + leaf = b'\x51' + buf, buf_len = make_cbuffer('00' * 32) + tapleaf = b'\xc0' + bytes([len(leaf)]) + leaf + self.assertEqual(wally_bip340_tagged_hash(tapleaf, len(tapleaf), utf8('TapLeaf'), buf, buf_len), WALLY_OK) + root = bytes(buf) + control = b'\xc0' + nums + + psbt, inp = without_keys(p2tr) # The field, on an output with a key path + ret = wally_psbt_input_set_taproot_internal_key(byref(inp), nums, 32) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(compute(psbt), (WALLY_EINVAL, None)) + wally_psbt_free(psbt) + + psbt, inp = without_keys(p2tr) # The control block, on an output with a key path + set_witness(inp, [leaf, control]) + self.assertEqual(compute(psbt), (WALLY_EINVAL, None)) + wally_psbt_free(psbt) + + # A taproot input is eligible by default, and a key path spend stays so + # even with a stale claim of the NUMS key + psbt, inp = without_keys(p2tr) + ret, with_tr = compute(psbt) + self.assertEqual(ret, WALLY_OK) + wally_psbt_free(psbt) + + psbt, inp = without_keys(p2tr) + self.assertEqual(wally_psbt_input_set_taproot_internal_key(byref(inp), nums, 32), WALLY_OK) + set_witness(inp, [bytes(64)]) + self.assertEqual(compute(psbt), (WALLY_OK, with_tr)) + wally_psbt_free(psbt) + + # Outputs that do commit to the NUMS key are skipped: only input 0 counts + input0_only = keys[:32] + bytes(32) + for prevout, setup in [ + (tweak(None), 'field'), + (tweak(root), 'field_root'), + (tweak(root), 'witness'), + ]: + psbt, inp = without_keys(prevout) + if setup.startswith('field'): + ret = wally_psbt_input_set_taproot_internal_key(byref(inp), nums, 32) + self.assertEqual(ret, WALLY_OK) + if setup == 'field_root': + ret = wally_map_add_integer(byref(inp.psbt_fields), 0x18, root, 32) + self.assertEqual(ret, WALLY_OK) + if setup == 'witness': + set_witness(inp, [leaf, control]) + ret, nums_scripts = compute(psbt, input0_only) + self.assertEqual(ret, WALLY_OK, setup) + self.assertNotEqual(nums_scripts, with_tr) + wally_psbt_free(psbt) + + # A finalized P2SH spend of some other script is skipped, even when + # its scriptSig pushes numbers such as OP_1 before the redeem script + if_script = b'\x63\x51\x67\x51\x68' # OP_IF OP_1 OP_ELSE OP_1 OP_ENDIF + p2sh_if = b'\xa9\x14' + hash160(if_script) + b'\x87' + push_if = bytes([len(if_script)]) + if_script + for number in [b'\x51', b'\x60', b'\x4f']: # OP_1, OP_16, OP_1NEGATE + psbt, inp = without_keys(p2sh_if) + set_script_sig(inp, number + push_if) + self.assertEqual(compute(psbt, input0_only), (WALLY_OK, nums_scripts), number) + wally_psbt_free(psbt) + psbt, inp = without_keys(p2sh) # A number cannot stand for the redeem script + set_script_sig(inp, p2sh_sig + b'\x51') + self.assertEqual(compute(psbt), (WALLY_EINVAL, None)) + wally_psbt_free(psbt) + + # A non-witness utxo must match the input, and any witness utxo must agree + tx = pointer(wally_tx()) + self.assertEqual(wally_tx_init_alloc(2, 0, 0, 1, tx), WALLY_OK) + self.assertEqual(wally_tx_add_raw_output(tx, 100000, p2wpkh, len(p2wpkh), 0), WALLY_OK) + txid, txid_len = make_cbuffer('00' * 32) + self.assertEqual(wally_tx_get_txid(tx, txid, txid_len), WALLY_OK) + + def with_utxo(match, witness_script): + psbt = get_psbt() + inp = psbt.contents.inputs[1] + self.assertEqual(wally_psbt_input_set_utxo(byref(inp), tx), WALLY_OK) + if match: + ret = wally_psbt_input_set_previous_txid(byref(inp), txid, txid_len) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_input_set_output_index(byref(inp), 0), WALLY_OK) + set_prevout(inp, witness_script) + return psbt + + for match, witness_script, expected_ret in [ + (False, p2wpkh, WALLY_EINVAL), # Wrong txid + (True, b'\x51', WALLY_EINVAL), # Witness utxo disagrees + (True, p2wpkh, WALLY_OK), # All consistent + ]: + psbt = with_utxo(match, witness_script) + self.assertEqual(compute(psbt)[0], expected_ret, (match, witness_script)) + wally_psbt_free(psbt) + wally_tx_free(tx) + + def test_silent_payment_bip352_vectors(self): + """Test the BIP-352 sending vectors through the BIP-375 functions""" + vectors = root_dir + 'src/secp256k1/src/modules/silentpayments/' \ + 'bip352_send_and_receive_test_vectors.json' + with open(vectors, 'r') as f: + cases = json.load(f) + + def parse_witness(hex_str): + b, i, items = bytes.fromhex(hex_str), 1, [] + for _ in range(b[0]): + n = b[i] + i += 1 + if n == 0xfd: + n = int.from_bytes(b[i:i + 2], 'little') + i += 2 + items.append(b[i:i + n]) + i += n + return items + + def build_psbt(given): + """A finalized PSBT spending the vector's inputs to its recipients""" + vin = given['vin'] + recipients = [r for r in given['recipients'] for _ in range(r.get('count', 1))] + psbt = pointer(wally_psbt()) + ret = wally_psbt_init_alloc(2, len(vin), len(recipients), 0, 0, psbt) + self.assertEqual(ret, WALLY_OK) + for i, v in enumerate(vin): + tx_in = pointer(wally_tx_input()) + txid = bytes.fromhex(v['txid'])[::-1] + ret = wally_tx_input_init_alloc(txid, 32, v['vout'], 0xffffffff, None, 0, None, tx_in) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_add_tx_input_at(psbt, i, 0, tx_in), WALLY_OK) + wally_tx_input_free(tx_in) + inp = byref(psbt.contents.inputs[i]) + script = bytes.fromhex(v['prevout']['scriptPubKey']['hex']) + tx_out = pointer(wally_tx_output()) + ret = wally_tx_output_init_alloc(100000, script, len(script), tx_out) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_input_set_witness_utxo(inp, tx_out), WALLY_OK) + wally_tx_output_free(tx_out) + if v['scriptSig']: + script_sig = bytes.fromhex(v['scriptSig']) + ret = wally_psbt_input_set_final_scriptsig(inp, script_sig, len(script_sig)) + self.assertEqual(ret, WALLY_OK) + if v['txinwitness']: + items = parse_witness(v['txinwitness']) + witness = pointer(wally_tx_witness_stack()) + ret = wally_tx_witness_stack_init_alloc(len(items), witness) + self.assertEqual(ret, WALLY_OK) + for item in items: + ret = wally_tx_witness_stack_add(witness, item, len(item)) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_input_set_final_witness(inp, witness), WALLY_OK) + wally_tx_witness_stack_free(witness) + for i, r in enumerate(recipients): + tx_out = pointer(wally_tx_output()) + self.assertEqual(wally_tx_output_init_alloc(1000, b'\x51', 1, tx_out), WALLY_OK) + self.assertEqual(wally_psbt_add_tx_output_at(psbt, i, 0, tx_out), WALLY_OK) + wally_tx_output_free(tx_out) + out = byref(psbt.contents.outputs[i]) + keys = bytes.fromhex(r['scan_pub_key'] + r['spend_pub_key']) + self.assertEqual(wally_psbt_output_set_sp_v0_info(out, keys, len(keys)), WALLY_OK) + self.assertEqual(wally_psbt_output_set_script(out, None, 0), WALLY_OK) + self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, 0), WALLY_OK) + return psbt + + num_sent, num_refused = 0, 0 + for case in cases: + for sending in case['sending']: + psbt = build_psbt(sending['given']) + vin = sending['given']['vin'] + keys = b''.join(bytes.fromhex(v['private_key']) for v in vin) + ret = wally_psbt_add_sp_ecdh_shares(psbt, keys, len(keys), b'\x07' * 32, 32, 0) + if ret == WALLY_OK: + ret = wally_psbt_compute_sp_scripts(psbt, 0) + expected = sending['expected'].get('outputs') or [[]] + if expected[0]: + self.assertEqual(ret, WALLY_OK, case['comment']) + got = [string_at(psbt.contents.outputs[i].script, 34)[2:].hex() + for i in range(psbt.contents.num_outputs)] + self.assertTrue(any(sorted(got) == sorted(e) for e in expected), case['comment']) + num_sent += 1 + else: + self.assertEqual(ret, WALLY_EINVAL, case['comment']) + num_refused += 1 + wally_psbt_free(psbt) + self.assertEqual((num_sent, num_refused), (25, 3)) + def test_silent_payment_outputs(self): """Test BIP-375 silent payment output fields""" # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b diff --git a/src/test/util.py b/src/test/util.py index f403ca190..990b6233b 100755 --- a/src/test/util.py +++ b/src/test/util.py @@ -468,6 +468,7 @@ class wally_psbt(Structure): ('wally_psbt_add_input_taproot_keypath', c_int, [POINTER(wally_psbt), c_uint32, c_uint32, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t]), ('wally_psbt_add_output_keypath', c_int, [POINTER(wally_psbt), c_uint32, c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t]), ('wally_psbt_add_output_taproot_keypath', c_int, [POINTER(wally_psbt), c_uint32, c_uint32, c_void_p, c_size_t, c_void_p, c_size_t, c_void_p, c_size_t, POINTER(c_uint32), c_size_t]), + ('wally_psbt_add_sp_ecdh_shares', c_int, [POINTER(wally_psbt), c_void_p, c_size_t, c_void_p, c_size_t, c_uint32]), ('wally_psbt_add_tx_input_at', c_int, [POINTER(wally_psbt), c_uint32, c_uint32, POINTER(wally_tx_input)]), ('wally_psbt_add_tx_output_at', c_int, [POINTER(wally_psbt), c_uint32, c_uint32, POINTER(wally_tx_output)]), ('wally_psbt_blind', c_int, [POINTER(wally_psbt), POINTER(wally_map), POINTER(wally_map), POINTER(wally_map), POINTER(wally_map), c_void_p, c_size_t, c_uint32, c_uint32, POINTER(wally_map)]), diff --git a/src/wasm_package/src/functions.js b/src/wasm_package/src/functions.js index 59f1176d3..7f0bca005 100644 --- a/src/wasm_package/src/functions.js +++ b/src/wasm_package/src/functions.js @@ -294,6 +294,7 @@ export const psbt_add_input_signature = wrap('wally_psbt_add_input_signature', [ export const psbt_add_input_taproot_keypath = wrap('wally_psbt_add_input_taproot_keypath', [T.OpaqueRef, T.Int32, T.Int32, T.Bytes, T.Bytes, T.Bytes, T.Uint32Array]); export const psbt_add_output_keypath = wrap('wally_psbt_add_output_keypath', [T.OpaqueRef, T.Int32, T.Bytes, T.Bytes, T.Uint32Array]); export const psbt_add_output_taproot_keypath = wrap('wally_psbt_add_output_taproot_keypath', [T.OpaqueRef, T.Int32, T.Int32, T.Bytes, T.Bytes, T.Bytes, T.Uint32Array]); +export const psbt_add_sp_ecdh_shares = wrap('wally_psbt_add_sp_ecdh_shares', [T.OpaqueRef, T.Bytes, T.Bytes, T.Int32]); export const psbt_add_tx_input_at = wrap('wally_psbt_add_tx_input_at', [T.OpaqueRef, T.Int32, T.Int32, T.OpaqueRef]); export const psbt_add_tx_output_at = wrap('wally_psbt_add_tx_output_at', [T.OpaqueRef, T.Int32, T.Int32, T.OpaqueRef]); export const psbt_blind = wrap('wally_psbt_blind_alloc', [T.OpaqueRef, T.OpaqueRef, T.OpaqueRef, T.OpaqueRef, T.OpaqueRef, T.Bytes, T.Int32, T.Int32, T.DestPtrPtr(T.OpaqueRef)]); diff --git a/src/wasm_package/src/index.d.ts b/src/wasm_package/src/index.d.ts index 7b26c7e2f..03894cce1 100644 --- a/src/wasm_package/src/index.d.ts +++ b/src/wasm_package/src/index.d.ts @@ -254,6 +254,7 @@ export function psbt_add_input_signature(psbt: Ref_wally_psbt, index: number, pu export function psbt_add_input_taproot_keypath(psbt: Ref_wally_psbt, index: number, flags: number, pub_key: Buffer|Uint8Array|null, tapleaf_hashes: Buffer|Uint8Array|null, fingerprint: Buffer|Uint8Array|null, child_path: Uint32Array|number[]): void; export function psbt_add_output_keypath(psbt: Ref_wally_psbt, index: number, pub_key: Buffer|Uint8Array|null, fingerprint: Buffer|Uint8Array|null, child_path: Uint32Array|number[]): void; export function psbt_add_output_taproot_keypath(psbt: Ref_wally_psbt, index: number, flags: number, pub_key: Buffer|Uint8Array|null, tapleaf_hashes: Buffer|Uint8Array|null, fingerprint: Buffer|Uint8Array|null, child_path: Uint32Array|number[]): void; +export function psbt_add_sp_ecdh_shares(psbt: Ref_wally_psbt, priv_keys: Buffer|Uint8Array|null, aux_rand: Buffer|Uint8Array|null, flags: number): void; export function psbt_add_tx_input_at(psbt: Ref_wally_psbt, index: number, flags: number, input: Ref_wally_tx_input): void; export function psbt_add_tx_output_at(psbt: Ref_wally_psbt, index: number, flags: number, output: Ref_wally_tx_output): void; export function psbt_blind(psbt: Ref_wally_psbt, values: Ref_wally_map, vbfs: Ref_wally_map, assets: Ref_wally_map, abfs: Ref_wally_map, entropy: Buffer|Uint8Array|null, output_index: number, flags: number): Ref_wally_map; diff --git a/tools/wasm_exports.sh b/tools/wasm_exports.sh index 7ba774e36..1d63f7736 100644 --- a/tools/wasm_exports.sh +++ b/tools/wasm_exports.sh @@ -216,6 +216,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \ ,'_wally_psbt_add_input_taproot_keypath' \ ,'_wally_psbt_add_output_keypath' \ ,'_wally_psbt_add_output_taproot_keypath' \ +,'_wally_psbt_add_sp_ecdh_shares' \ ,'_wally_psbt_add_tx_input_at' \ ,'_wally_psbt_add_tx_output_at' \ ,'_wally_psbt_clear_fallback_locktime' \ From 5fd6208313edb50651c0035e6943fada949fdbe5 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:14:32 +0900 Subject: [PATCH 08/11] psbt: Sign with SIGHASH_ALL for silent payments Silent payment scripts are derived from the position of each code, so a signature that does not commit to every output could let them be moved or dropped. BIP-375 therefore allows only SIGHASH_ALL. With silent payment outputs present, signing and the signature hash now use SIGHASH_ALL, also for taproot when no sighash is given, and any other sighash is refused, as it is by wally_psbt_compute_sp_scripts(). --- include/wally_psbt.h | 4 ++++ src/psbt.c | 43 +++++++++++++++++++++++++++++++++++-------- src/test/test_psbt.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 8 deletions(-) diff --git a/include/wally_psbt.h b/include/wally_psbt.h index 8c11114b9..5ea1ace59 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -2793,6 +2793,8 @@ WALLY_CORE_API int wally_psbt_sign( * .. note:: Inputs are signed using the sighash stored for the input, *| or SIGHASH_ALL/SIGHASH_DEFAULT if not present. The caller should ensure *| that each input sighash is acceptable before signing. + * + * .. note:: Only SIGHASH_ALL is used with BIP-375 silent payment outputs. */ WALLY_CORE_API int wally_psbt_sign_bip32( struct wally_psbt *psbt, @@ -2814,6 +2816,8 @@ WALLY_CORE_API int wally_psbt_sign_bip32( * .. note:: Inputs are signed using the sighash stored for the input, *| or SIGHASH_ALL/SIGHASH_DEFAULT if not present. The caller should ensure *| that the sighash is acceptable before signing. + * + * .. note:: Only SIGHASH_ALL is used with BIP-375 silent payment outputs. */ WALLY_CORE_API int wally_psbt_sign_input_bip32( struct wally_psbt *psbt, diff --git a/src/psbt.c b/src/psbt.c index c9516cae3..232fb0bd2 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -641,6 +641,35 @@ static bool sp_script_is_modifiable(const struct wally_psbt *psbt) return false; } +static bool psbt_has_sp_outputs(const struct wally_psbt *psbt) +{ + size_t i; + if (psbt->version != PSBT_2) + return false; + for (i = 0; i < psbt->num_outputs; ++i) + if (wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO)) + return true; + return false; +} + +/* Get the sighash to sign an input with. BIP-375: with silent payment + * outputs present, only SIGHASH_ALL may be used, including for taproot */ +static int psbt_input_get_sighash(const struct wally_psbt *psbt, + const struct wally_psbt_input *inp, + bool is_taproot, uint32_t *sighash) +{ + *sighash = inp->sighash; + if (psbt_has_sp_outputs(psbt)) { + if (*sighash && *sighash != WALLY_SIGHASH_ALL) + return WALLY_EINVAL; + *sighash = WALLY_SIGHASH_ALL; + } else if (!*sighash) + *sighash = is_taproot ? WALLY_SIGHASH_DEFAULT : WALLY_SIGHASH_ALL; + else if (*sighash & 0xffffff00) + return WALLY_EINVAL; + return WALLY_OK; +} + static int psbt_input_field_verify(uint32_t field_type, const unsigned char *val, size_t val_len) { @@ -5001,6 +5030,10 @@ int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) /* Eligible input keys, their sum A, and the smallest outpoint */ for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) { const struct wally_psbt_input *input = psbt->inputs + i; + if (input->sighash && input->sighash != WALLY_SIGHASH_ALL) { + ret = WALLY_EINVAL; /* BIP-375: SIGHASH_ALL only */ + break; + } ret = sp_input_pubkey(input, &eligible[i], keys + num_eligible * EC_PUBLIC_KEY_LEN); if (ret == WALLY_OK && eligible[i]) ++num_eligible; @@ -5709,10 +5742,7 @@ int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index, #endif assets_p = is_pset ? &assets : NULL; - sighash = inp->sighash; - if (!sighash) - sighash = is_taproot ? WALLY_SIGHASH_DEFAULT : WALLY_SIGHASH_ALL; - else if (sighash & 0xffffff00) + if (psbt_input_get_sighash(psbt, inp, is_taproot, &sighash) != WALLY_OK) return WALLY_EINVAL; if (sighash_type == WALLY_SIGTYPE_PRE_SW && @@ -5798,10 +5828,7 @@ int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, flags = EC_FLAG_SCHNORR | (flags & EC_FLAG_ELEMENTS); } - sighash = inp->sighash; - if (!sighash) - sighash = is_taproot ? WALLY_SIGHASH_DEFAULT : WALLY_SIGHASH_ALL; - else if (sighash & 0xffffff00) { + if (psbt_input_get_sighash(psbt, inp, is_taproot, &sighash) != WALLY_OK) { ret = WALLY_EINVAL; goto done; } diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 82579d0a2..1bfd0092f 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -723,6 +723,36 @@ def test_silent_payment_scripts(self): self.assertEqual(self.get_sp_scripts(psbt), scripts) wally_psbt_free(psbt) + # Signing uses SIGHASH_ALL, also for taproot when no sighash is given, + # and refuses any other sighash + case = [c for c in cases['valid'] if 'two P2TR inputs, neither' in c['description']][0] + psbt = self.parse_base64(case['psbt']) + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_OK) + + def sighash_hash(): + buf, buf_len = make_cbuffer('00' * 32) + ret = wally_psbt_get_input_signature_hash(psbt, 0, tx, None, 0, 0, buf, buf_len) + return ret, bytes(buf) + + unset = sighash_hash() + self.assertEqual(unset[0], WALLY_OK) + self.assertEqual(wally_psbt_set_input_sighash(psbt, 0, 1), WALLY_OK) # ALL + self.assertEqual(sighash_hash(), unset) + self.assertEqual(wally_psbt_set_input_sighash(psbt, 0, 2), WALLY_OK) # NONE + self.assertEqual(sighash_hash()[0], WALLY_EINVAL) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + # Without silent payment outputs, taproot defaults to SIGHASH_DEFAULT + self.assertEqual(wally_psbt_set_input_sighash(psbt, 0, 0), WALLY_OK) + for i in range(psbt.contents.num_outputs): + out = byref(psbt.contents.outputs[i]) + self.assertEqual(wally_psbt_output_clear_sp_v0_info(out), WALLY_OK) + ret, default = sighash_hash() + self.assertEqual(ret, WALLY_OK) + self.assertNotEqual(default, unset[1]) + wally_tx_free(tx) + wally_psbt_free(psbt) + def test_silent_payment_add_shares(self): """Test adding BIP-375 ECDH shares from input private keys""" # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b From 1642adafb286065ebd7dc675e52d5f2d5b296a98 Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 22:14:32 +0900 Subject: [PATCH 09/11] psbt: Verify silent payment scripts before signing A silent payment output's script is only trustworthy once derived from its code. A coordinator can pre-fill it with a script paying its own key, and wally_psbt_sign() signed that, as only a missing script was refused. wally_psbt_compute_sp_scripts() caught this, but nothing made a signer call it. Verify every silent payment script, unchanged, in each call that makes a signature and in wally_psbt_extract(), which BIP-375 requires of an extractor and which callers use to build the transaction they hash. A PSBT that is not v2 but has a code, which loose parsing can produce, fails these checks rather than skipping them. wally_psbt_get_input_signature_hash() takes a caller's transaction and does not verify, so its documentation says a caller signing the hash itself must call wally_psbt_compute_sp_scripts() first. --- include/wally_psbt.h | 15 +++- src/psbt.c | 58 +++++++++++++--- src/test/test_psbt.py | 154 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 215 insertions(+), 12 deletions(-) diff --git a/include/wally_psbt.h b/include/wally_psbt.h index 5ea1ace59..5b828b159 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -2466,6 +2466,10 @@ WALLY_CORE_API int wally_psbt_get_input_scriptcode( * :param flags: Flags controlling signature hash generation. Must be 0. * :param bytes_out: Destination for the signature hash. * FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN) + * + * .. note:: This does not check BIP-375 silent payment output scripts. A caller + *| signing the hash with its own key must first check them by calling + *| `wally_psbt_compute_sp_scripts`, or the signature could pay anyone. */ WALLY_CORE_API int wally_psbt_get_input_signature_hash( struct wally_psbt *psbt, @@ -2794,7 +2798,9 @@ WALLY_CORE_API int wally_psbt_sign( *| or SIGHASH_ALL/SIGHASH_DEFAULT if not present. The caller should ensure *| that each input sighash is acceptable before signing. * - * .. note:: Only SIGHASH_ALL is used with BIP-375 silent payment outputs. + * .. note:: Fails unless every BIP-375 silent payment output script is present + *| and verifies as in `wally_psbt_compute_sp_scripts`, and only SIGHASH_ALL + *| is used with such outputs. */ WALLY_CORE_API int wally_psbt_sign_bip32( struct wally_psbt *psbt, @@ -2817,7 +2823,9 @@ WALLY_CORE_API int wally_psbt_sign_bip32( *| or SIGHASH_ALL/SIGHASH_DEFAULT if not present. The caller should ensure *| that the sighash is acceptable before signing. * - * .. note:: Only SIGHASH_ALL is used with BIP-375 silent payment outputs. + * .. note:: Fails unless every BIP-375 silent payment output script is present + *| and verifies as in `wally_psbt_compute_sp_scripts`, and only SIGHASH_ALL + *| is used with such outputs. */ WALLY_CORE_API int wally_psbt_sign_input_bip32( struct wally_psbt *psbt, @@ -2864,6 +2872,9 @@ WALLY_CORE_API int wally_psbt_finalize_input( * :param psbt: PSBT to extract from. * :param flags: :ref:`psbt-extract` controlling extraction. * :param output: Destination for the resulting transaction. + * + * .. note:: Fails unless every BIP-375 silent payment output script is present + *| and verifies as in `wally_psbt_compute_sp_scripts`. */ WALLY_CORE_API int wally_psbt_extract( const struct wally_psbt *psbt, diff --git a/src/psbt.c b/src/psbt.c index 232fb0bd2..31db4f316 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -644,8 +644,6 @@ static bool sp_script_is_modifiable(const struct wally_psbt *psbt) static bool psbt_has_sp_outputs(const struct wally_psbt *psbt) { size_t i; - if (psbt->version != PSBT_2) - return false; for (i = 0; i < psbt->num_outputs; ++i) if (wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO)) return true; @@ -660,7 +658,7 @@ static int psbt_input_get_sighash(const struct wally_psbt *psbt, { *sighash = inp->sighash; if (psbt_has_sp_outputs(psbt)) { - if (*sighash && *sighash != WALLY_SIGHASH_ALL) + if (psbt->version != PSBT_2 || (*sighash && *sighash != WALLY_SIGHASH_ALL)) return WALLY_EINVAL; *sighash = WALLY_SIGHASH_ALL; } else if (!*sighash) @@ -4975,7 +4973,9 @@ static int sp_output_script(const unsigned char *shared_secret, uint32_t k, return ret; } -int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) +/* Compute silent payment output scripts, or with verify_only require that they + * are all already present and correct, which leaves the PSBT unchanged */ +static int psbt_sp_scripts(struct wally_psbt *psbt, bool verify_only) { struct sp_output *outs = NULL, tmp; unsigned char *keys = NULL, *scripts = NULL, **new_scripts = NULL; @@ -4989,14 +4989,18 @@ int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) uint32_t k = 0; int ret = WALLY_OK; - if (!psbt_is_valid(psbt) || psbt->version != PSBT_2 || flags || + if (!psbt_is_valid(psbt) || psbt->version != PSBT_2 || wally_psbt_is_elements(psbt, &is_pset) != WALLY_OK || is_pset) return WALLY_EINVAL; - /* Gather the silent payment outputs */ - for (i = 0; i < psbt->num_outputs; ++i) + /* Gather the silent payment outputs. BIP-375: no signature until every + * output has a script */ + for (i = 0; i < psbt->num_outputs; ++i) { + if (verify_only && !psbt->outputs[i].script) + return WALLY_EINVAL; /* Not computed yet, or missing */ if (wally_map_get_integer(&psbt->outputs[i].psbt_fields, PSBT_OUT_SP_V0_INFO)) ++num_outs; + } if (!num_outs) return WALLY_OK; /* Nothing to do */ if (!psbt->num_inputs || sp_script_is_modifiable(psbt)) @@ -5125,6 +5129,11 @@ int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) return ret; } +int wally_psbt_compute_sp_scripts(struct wally_psbt *psbt, uint32_t flags) +{ + return flags ? WALLY_EINVAL : psbt_sp_scripts(psbt, false); +} + #define sp_swap_maps(a, b) do { struct wally_map _t = a; a = b; b = _t; } while (0) /* Add an ECDH share and its DLEQ proof for scan_key to the given maps */ @@ -5777,7 +5786,7 @@ int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index, return ret; } -int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, +static int psbt_sign_input_impl(struct wally_psbt *psbt, size_t index, size_t subindex, const unsigned char *txhash, size_t txhash_len, const struct ext_key *hdkey, @@ -5862,6 +5871,25 @@ int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, return ret; } +/* Before any signature: a silent payment output's script must be the one its + * code derives, or a signature could pay anyone. Every signing call checks this */ +static int psbt_check_sp_scripts(struct wally_psbt *psbt) +{ + return psbt_has_sp_outputs(psbt) ? psbt_sp_scripts(psbt, true) : WALLY_OK; +} + +int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, + size_t index, size_t subindex, + const unsigned char *txhash, size_t txhash_len, + const struct ext_key *hdkey, + uint32_t flags) +{ + int ret = psbt_is_valid(psbt) ? psbt_check_sp_scripts(psbt) : WALLY_OK; + if (ret == WALLY_OK) + ret = psbt_sign_input_impl(psbt, index, subindex, txhash, txhash_len, hdkey, flags); + return ret; +} + int wally_psbt_sign_bip32(struct wally_psbt *psbt, const struct ext_key *hdkey, uint32_t flags) { @@ -5878,6 +5906,11 @@ int wally_psbt_sign_bip32(struct wally_psbt *psbt, if ((ret = psbt_build_tx(psbt, &tx, &is_pset, false, false)) != WALLY_OK) return ret; + if ((ret = psbt_check_sp_scripts(psbt)) != WALLY_OK) { + wally_tx_free(tx); + return ret; + } + #ifdef BUILD_ELEMENTS if (is_pset) { flags |= EC_FLAG_ELEMENTS; @@ -5918,7 +5951,7 @@ int wally_psbt_sign_bip32(struct wally_psbt *psbt, 0, txhash, sizeof(txhash)); /* Sign the input */ if (ret == WALLY_OK) - ret = wally_psbt_sign_input_bip32(psbt, i, subindex, + ret = psbt_sign_input_impl(psbt, i, subindex, txhash, sizeof(txhash), &derived, flags); wally_clear(&derived, sizeof(derived)); @@ -6342,6 +6375,13 @@ int wally_psbt_extract(const struct wally_psbt *psbt, uint32_t flags, struct wal if ((ret = psbt_build_tx(psbt, &result, &is_pset, false, false)) != WALLY_OK) return ret; + /* BIP-375: Extraction also verifies silent payment scripts. Verifying does + * not change the PSBT */ + if ((ret = psbt_check_sp_scripts((struct wally_psbt *)psbt)) != WALLY_OK) { + wally_tx_free(result); + return ret; + } + for (i = 0; for_final && i < psbt->num_inputs; ++i) { const struct wally_psbt_input *input = &psbt->inputs[i]; struct wally_tx_input *txin = &result->inputs[i]; diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 1bfd0092f..37948d14c 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -1,6 +1,6 @@ import json import unittest -from ctypes import string_at +from ctypes import c_ubyte, string_at from util import * FLAG_GRIND_R = 0x4 @@ -1194,6 +1194,158 @@ def with_utxo(match, witness_script): wally_psbt_free(psbt) wally_tx_free(tx) + def test_silent_payment_signing(self): + """Test that signing verifies silent payment output scripts""" + # bip-0375/bip375_test_vectors.json from bitcoin/bips commit a34f044b + with open(root_dir + 'src/data/bip375_vectors.json', 'r') as f: + cases = json.load(f) + case = [c for c in cases['valid'] if 'single-signer using global' in c['description']][0] + keys = [bytes.fromhex(i['private_key']) for i in case['supplementary']['inputs']] + # An output paying a key the attacker holds (here, private key 1) + attacker = bytes.fromhex('5120' + '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798') + + def clear_sp_data(psbt): + for p in [psbt.contents] + [psbt.contents.inputs[i] for i in range(2)]: + wally_map_clear(byref(p.sp_ecdh_shares)) + wally_map_clear(byref(p.sp_dleq_proofs)) + for i in self.get_sp_scripts(psbt): + self.assertEqual(wally_psbt_output_set_script(byref(psbt.contents.outputs[i]), None, 0), WALLY_OK) + + def get_psbt(script=None): + psbt = self.parse_base64(case['psbt']) + self.fix_sp_prevouts(psbt) + for i in range(2): + wally_map_clear(byref(psbt.contents.inputs[i].signatures)) + if script: + out = byref(psbt.contents.outputs[0]) + self.assertEqual(wally_psbt_output_set_script(out, script, len(script)), WALLY_OK) + return psbt + + def num_sigs(psbt): + return sum(psbt.contents.inputs[i].signatures.num_items for i in range(2)) + + def assert_no_signature(psbt): + """Every public call that can lead to a signature must refuse""" + for key in keys: + self.assertEqual(wally_psbt_sign(psbt, key, 32, 0), WALLY_EINVAL) + pub_key, pub_key_len = make_cbuffer('00' * 33) + self.assertEqual(wally_ec_public_key_from_private_key(keys[0], 32, pub_key, pub_key_len), WALLY_OK) + hdkey = pointer(ext_key()) + ret = bip32_key_init_alloc(0x0488ADE4, 0, 0, bytes(32), 32, pub_key, pub_key_len, keys[0], 32, + None, 0, None, 0, hdkey) # Mainnet private + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_sign_bip32(psbt, hdkey, 0), WALLY_EINVAL) + self.assertEqual(wally_psbt_sign_input_bip32(psbt, 0, 0, bytes(32), 32, hdkey, 0), WALLY_EINVAL) + bip32_key_free(hdkey) + self.assertEqual(num_sigs(psbt), 0) + + def get_input_hash(psbt, tx): + scriptcode = b'\x76\xa9\x14' + bytes(20) + b'\x88\xac' + hash_out, hash_len = make_cbuffer('00' * 32) + return wally_psbt_get_input_signature_hash(psbt, 0, tx, scriptcode, len(scriptcode), 0, hash_out, hash_len) + + # The correct script can be signed + psbt = get_psbt() + for key in keys: + self.assertEqual(wally_psbt_sign(psbt, key, 32, 0), WALLY_OK) + self.assertEqual(num_sigs(psbt), 2) + wally_psbt_free(psbt) + + # Converting to v0 cannot switch the checks off: the conversion fails... + attacker_psbt = get_psbt(attacker) + self.assertEqual(wally_psbt_set_version(attacker_psbt, 0, 0), WALLY_EINVAL) + self.assertEqual(attacker_psbt.contents.version, 2) + assert_no_signature(attacker_psbt) + wally_psbt_free(attacker_psbt) + + # ...and a v0 PSBT with a code signs nothing and extracts nothing, whether + # the code was set by a caller or came from bytes accepted by loose parsing + psbt = get_psbt() + info = bytearray(66) + out = byref(psbt.contents.outputs[0]) + ret, written = wally_psbt_output_get_sp_v0_info(out, (c_ubyte * 66).from_buffer(info), 66) + self.assertEqual((ret, written), (WALLY_OK, 66)) + self.assertEqual(wally_psbt_output_clear_sp_v0_info(out), WALLY_OK) + self.assertEqual(wally_psbt_output_set_script(out, attacker, len(attacker)), WALLY_OK) + self.assertEqual(wally_psbt_set_version(psbt, 0, 0), WALLY_OK) + self.assertEqual(wally_psbt_output_set_sp_v0_info(byref(psbt.contents.outputs[0]), bytes(info), 66), WALLY_OK) + assert_no_signature(psbt) + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_EINVAL) + self.assertEqual(get_input_hash(psbt, psbt.contents.tx), WALLY_EINVAL) + + # The same PSBT as raw bytes, with the code in its output map + ret, v0_len = wally_psbt_get_length(psbt, 0) + self.assertEqual(ret, WALLY_OK) + v0_buf, v0_buf_len = make_cbuffer('00' * v0_len) + ret, written = wally_psbt_to_bytes(psbt, 0, v0_buf, v0_buf_len) + self.assertEqual((ret, written), (WALLY_OK, v0_len)) + wally_psbt_free(psbt) + v0_bytes = bytes(v0_buf) + if b'\x01\x09\x42' + bytes(info) not in v0_bytes: + # Serializing v0 drops the code, so add it to the last output's map + v0_bytes = v0_bytes[:-1] + b'\x01\x09\x42' + bytes(info) + b'\x00' + loose = pointer(wally_psbt()) + self.assertEqual(wally_psbt_from_bytes(v0_bytes, len(v0_bytes), 0, loose), WALLY_EINVAL) + ret = wally_psbt_from_bytes(v0_bytes, len(v0_bytes), 0x2, loose) # WALLY_PSBT_PARSE_FLAG_LOOSE + self.assertEqual(ret, WALLY_OK) + assert_no_signature(loose) + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(loose, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_EINVAL) + wally_psbt_free(loose) + + # A wrong script is refused by every call that signs, and adds nothing + psbt = get_psbt(attacker) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + assert_no_signature(psbt) + tx = pointer(wally_tx()) + ret = wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx) + self.assertEqual(ret, WALLY_EINVAL) # So is building the tx to hash + wally_psbt_free(psbt) + + # BIP-375: nothing is signed while any output has no script, as one + # with no script pays whoever spends it first + psbt = get_psbt() + tx_out = pointer(wally_tx_output()) + self.assertEqual(wally_tx_output_init_alloc(1000, None, 0, tx_out), WALLY_OK) + self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, 0x2), WALLY_OK) # WALLY_PSBT_TXMOD_OUTPUTS + self.assertEqual(wally_psbt_add_tx_output_at(psbt, 1, 0, tx_out), WALLY_OK) + self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, 0), WALLY_OK) + wally_tx_output_free(tx_out) + self.assertFalse(psbt.contents.outputs[1].script) + assert_no_signature(psbt) + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_EINVAL) + wally_psbt_free(psbt) + + # So is one with its share or proof removed, which it cannot be checked without + psbt = get_psbt() + wally_map_clear(byref(psbt.contents.sp_dleq_proofs)) + self.assertEqual(wally_psbt_sign(psbt, keys[0], 32, 0), WALLY_EINVAL) + wally_psbt_free(psbt) + + # A failed allocation while adding shares leaves the PSBT unchanged, and + # one while verifying signs nothing + for input_keys in [keys[0] + keys[1], keys[0] + bytes(32)]: + psbt = get_psbt() + clear_sp_data(psbt) + original = (wally_psbt_get_length(psbt, 0), self.to_base64(psbt)) + + def add_shares(): + ret = wally_psbt_add_sp_ecdh_shares(psbt, input_keys, 64, b'\x07' * 32, 32, 0) + if ret != WALLY_OK: + with no_malloc_failures(): + self.assertEqual((wally_psbt_get_length(psbt, 0), self.to_base64(psbt)), original) + return ret + + self.assertEqual(malloc_fail_loop(add_shares), WALLY_OK) + wally_psbt_free(psbt) + + psbt = get_psbt() + ret = malloc_fail_loop(lambda: wally_psbt_sign(psbt, keys[0], 32, 0)) + self.assertEqual(ret, WALLY_OK) + wally_psbt_free(psbt) + def test_silent_payment_bip352_vectors(self): """Test the BIP-352 sending vectors through the BIP-375 functions""" vectors = root_dir + 'src/secp256k1/src/modules/silentpayments/' \ From bf0cdccd4356bb752e92d95ebde0a07688508d7c Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:01:58 +0900 Subject: [PATCH 10/11] psbt: Require proven prevouts to sign ECDSA An input's prevout script decides whether it counts towards a silent payment's key sum. A witness utxo cannot be checked, and an ECDSA signature does not commit to the prevout scripts of the other inputs. A co-signer could claim its real P2WPKH input is a P2WSH: the signer would derive an output without that key, and the signature stays valid for the real transaction, whose receiver cannot find the payment. With silent payment outputs, refuse to sign or hash a non-taproot input unless a non-witness utxo proves the prevout of every input. A taproot signature commits to all of the prevout scripts, so a false one only makes it invalid, and witness utxos stay enough for it. --- include/wally_psbt.h | 19 ++++++-- src/psbt.c | 24 ++++++++++ src/test/test_psbt.py | 104 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 142 insertions(+), 5 deletions(-) diff --git a/include/wally_psbt.h b/include/wally_psbt.h index 5b828b159..bd716c78a 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -2052,8 +2052,9 @@ WALLY_CORE_API int wally_psbt_get_id( * * .. note:: An input's prevout script comes from its witness or non-witness *| utxo. A non-witness utxo is checked against the input, but a witness utxo - *| alone cannot be: the caller must trust it or check it, for example - *| against the keys it holds, as a false script can change the key sum. + *| alone cannot be, and a false script can change the key sum. Signing a + *| non-taproot input therefore requires a non-witness utxo on every input, + *| and a taproot signature commits to all of the prevout scripts. */ WALLY_CORE_API int wally_psbt_compute_sp_scripts( struct wally_psbt *psbt, @@ -2470,6 +2471,10 @@ WALLY_CORE_API int wally_psbt_get_input_scriptcode( * .. note:: This does not check BIP-375 silent payment output scripts. A caller *| signing the hash with its own key must first check them by calling *| `wally_psbt_compute_sp_scripts`, or the signature could pay anyone. + * + * .. note:: With silent payment outputs, fails unless only SIGHASH_ALL is used + *| and, for a non-taproot input, every input has a non-witness utxo. See + *| `wally_psbt_sign_bip32`. */ WALLY_CORE_API int wally_psbt_get_input_signature_hash( struct wally_psbt *psbt, @@ -2800,7 +2805,10 @@ WALLY_CORE_API int wally_psbt_sign( * * .. note:: Fails unless every BIP-375 silent payment output script is present *| and verifies as in `wally_psbt_compute_sp_scripts`, and only SIGHASH_ALL - *| is used with such outputs. + *| is used with such outputs. To sign a non-taproot input with such outputs, + *| every input must also have a non-witness utxo: an ECDSA signature does + *| not commit to the prevout scripts of the other inputs, so a false witness + *| utxo could otherwise leave a payment its receiver cannot find. */ WALLY_CORE_API int wally_psbt_sign_bip32( struct wally_psbt *psbt, @@ -2825,7 +2833,10 @@ WALLY_CORE_API int wally_psbt_sign_bip32( * * .. note:: Fails unless every BIP-375 silent payment output script is present *| and verifies as in `wally_psbt_compute_sp_scripts`, and only SIGHASH_ALL - *| is used with such outputs. + *| is used with such outputs. To sign a non-taproot input with such outputs, + *| every input must also have a non-witness utxo: an ECDSA signature does + *| not commit to the prevout scripts of the other inputs, so a false witness + *| utxo could otherwise leave a payment its receiver cannot find. */ WALLY_CORE_API int wally_psbt_sign_input_bip32( struct wally_psbt *psbt, diff --git a/src/psbt.c b/src/psbt.c index 31db4f316..70506670b 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -650,6 +650,28 @@ static bool psbt_has_sp_outputs(const struct wally_psbt *psbt) return false; } +static int sp_input_prevout_script(const struct wally_psbt_input *input, + const unsigned char **script, size_t *script_len); + +/* An ECDSA signature does not commit to the prevout scripts of the other + * inputs, so a false witness utxo on any of them could change the key sum and + * leave a payment its receiver cannot find. Every prevout must be proven by a + * non-witness utxo. A taproot signature commits to all of them */ +static int sp_check_prevouts_proven(const struct wally_psbt *psbt) +{ + const unsigned char *script; + size_t i, script_len; + + for (i = 0; i < psbt->num_inputs; ++i) { + int ret = sp_input_prevout_script(&psbt->inputs[i], &script, &script_len); + if (ret != WALLY_OK) + return ret; + if (!psbt->inputs[i].utxo) + return WALLY_EINVAL; + } + return WALLY_OK; +} + /* Get the sighash to sign an input with. BIP-375: with silent payment * outputs present, only SIGHASH_ALL may be used, including for taproot */ static int psbt_input_get_sighash(const struct wally_psbt *psbt, @@ -660,6 +682,8 @@ static int psbt_input_get_sighash(const struct wally_psbt *psbt, if (psbt_has_sp_outputs(psbt)) { if (psbt->version != PSBT_2 || (*sighash && *sighash != WALLY_SIGHASH_ALL)) return WALLY_EINVAL; + if (!is_taproot && sp_check_prevouts_proven(psbt) != WALLY_OK) + return WALLY_EINVAL; *sighash = WALLY_SIGHASH_ALL; } else if (!*sighash) *sighash = is_taproot ? WALLY_SIGHASH_DEFAULT : WALLY_SIGHASH_ALL; diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 37948d14c..8466bc629 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -1211,11 +1211,39 @@ def clear_sp_data(psbt): for i in self.get_sp_scripts(psbt): self.assertEqual(wally_psbt_output_set_script(byref(psbt.contents.outputs[i]), None, 0), WALLY_OK) - def get_psbt(script=None): + def prove_prevouts(psbt): + """Give each input a non-witness utxo. These change its outpoint, + so the shares and scripts have to be made again. + """ + for i in range(2): + inp = psbt.contents.inputs[i] + utxo = inp.witness_utxo.contents + script = string_at(utxo.script, utxo.script_len) + tx = pointer(wally_tx()) + self.assertEqual(wally_tx_init_alloc(2, 0, 1, inp.index + 1, tx), WALLY_OK) + self.assertEqual(wally_tx_add_raw_input(tx, bytes([i + 1]) * 32, 32, 0, 0, None, 0, None, 0), WALLY_OK) + for _ in range(inp.index + 1): + self.assertEqual(wally_tx_add_raw_output(tx, utxo.satoshi, script, len(script), 0), WALLY_OK) + txid, txid_len = make_cbuffer('00' * 32) + self.assertEqual(wally_tx_get_txid(tx, txid, txid_len), WALLY_OK) + self.assertEqual(wally_psbt_input_set_previous_txid(byref(inp), txid, txid_len), WALLY_OK) + self.assertEqual(wally_psbt_input_set_utxo(byref(inp), tx), WALLY_OK) + wally_tx_free(tx) + clear_sp_data(psbt) + ret = wally_psbt_add_sp_ecdh_shares(psbt, keys[0] + keys[1], 64, b'\x07' * 32, 32, 0) + self.assertEqual(ret, WALLY_OK) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + + def get_unproven_psbt(): psbt = self.parse_base64(case['psbt']) self.fix_sp_prevouts(psbt) for i in range(2): wally_map_clear(byref(psbt.contents.inputs[i].signatures)) + return psbt + + def get_psbt(script=None): + psbt = get_unproven_psbt() + prove_prevouts(psbt) if script: out = byref(psbt.contents.outputs[0]) self.assertEqual(wally_psbt_output_set_script(out, script, len(script)), WALLY_OK) @@ -1251,6 +1279,80 @@ def get_input_hash(psbt, tx): self.assertEqual(num_sigs(psbt), 2) wally_psbt_free(psbt) + # A taproot signature commits to every prevout script, so a witness utxo is enough + tr_case = [c for c in cases['valid'] if 'one P2TR input / one sp output with no' in c['description']][0] + psbt = self.parse_base64(tr_case['psbt']) + inp = byref(psbt.contents.inputs[0]) + internal_pub, output_pub = [make_cbuffer('00' * 33)[0] for _ in range(2)] + output_key = make_cbuffer('00' * 32)[0] + self.assertEqual(wally_ec_public_key_from_private_key(keys[0], 32, internal_pub, 33), WALLY_OK) + self.assertEqual(wally_ec_public_key_bip341_tweak(internal_pub, 33, None, 0, 0, output_pub, 33), WALLY_OK) + self.assertEqual(wally_ec_private_key_bip341_tweak(keys[0], 32, None, 0, 0, output_key, 32), WALLY_OK) + script = b'\x51\x20' + bytes(output_pub)[1:] + tx_out = pointer(wally_tx_output()) + self.assertEqual(wally_tx_output_init_alloc(100000, script, len(script), tx_out), WALLY_OK) + self.assertEqual(wally_psbt_input_set_witness_utxo(inp, tx_out), WALLY_OK) + wally_tx_output_free(tx_out) + internal_x = bytes(internal_pub)[1:] + self.assertEqual(wally_psbt_input_set_taproot_internal_key(inp, internal_x, 32), WALLY_OK) + self.assertEqual(wally_psbt_input_taproot_keypath_add(inp, internal_x, 32, None, 0, bytes(4), 4, None, 0), WALLY_OK) + self.assertEqual(wally_psbt_add_sp_ecdh_shares(psbt, output_key, 32, b'\x07' * 32, 32, 0), WALLY_OK) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + self.assertFalse(psbt.contents.inputs[0].utxo) + self.assertEqual(wally_psbt_sign(psbt, keys[0], 32, 0), WALLY_OK) + fields = psbt.contents.inputs[0].psbt_fields + item = [fields.items[i] for i in range(fields.num_items) if fields.items[i].key_len == 0x13][0] + sig = string_at(item.value, item.value_len) + self.assertEqual((len(sig), sig[64]), (65, 1)) # SIGHASH_ALL + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_OK) + tr_hash, tr_hash_len = make_cbuffer('00' * 32) + ret = wally_psbt_get_input_signature_hash(psbt, 0, tx, None, 0, 0, tr_hash, tr_hash_len) + self.assertEqual(ret, WALLY_OK) + # The signature is for the key the prevout actually pays + ret = wally_ec_sig_verify(bytes(output_pub)[1:], 32, tr_hash, 32, 0x2, sig[:64], 64) # EC_FLAG_SCHNORR + self.assertEqual(ret, WALLY_OK) + wally_tx_free(tx) + wally_psbt_free(psbt) + + # An ECDSA signature does not commit to the other inputs' prevout scripts, + # so a false witness utxo could leave the payment unfindable. Without + # non-witness utxos proving every prevout, nothing can be signed + psbt = get_unproven_psbt() + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + assert_no_signature(psbt) + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_OK) + self.assertEqual(get_input_hash(psbt, tx), WALLY_EINVAL) + wally_tx_free(tx) + wally_psbt_free(psbt) + + # The attack: the signer proves its own input, while a co-signer claims + # its real P2WPKH input is a P2WSH, which BIP-352 leaves out of the sum + psbt = get_psbt() + forged = b'\x00\x20' + bytes(32) + tx_out = pointer(wally_tx_output()) + self.assertEqual(wally_tx_output_init_alloc(1000, forged, len(forged), tx_out), WALLY_OK) + self.assertEqual(wally_psbt_input_set_witness_utxo(byref(psbt.contents.inputs[1]), tx_out), WALLY_OK) + # A real non-witness utxo beside it exposes the lie + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_EINVAL) + assert_no_signature(psbt) + # So the attacker leaves it out, and supplies a share as the only eligible input's owner + self.assertEqual(wally_psbt_input_set_utxo(byref(psbt.contents.inputs[1]), None), WALLY_OK) + clear_sp_data(psbt) + self.assertEqual(wally_psbt_add_sp_ecdh_shares(psbt, keys[0] + bytes(32), 64, b'\x07' * 32, 32, 0), WALLY_OK) + self.assertEqual(wally_psbt_compute_sp_scripts(psbt, 0), WALLY_OK) + assert_no_signature(psbt) + wally_tx_output_free(tx_out) + wally_psbt_free(psbt) + + # A non-witness utxo for another transaction proves nothing + psbt = get_psbt() + txid = bytes(reversed(bytes(psbt.contents.inputs[1].txhash))) + self.assertEqual(wally_psbt_input_set_previous_txid(byref(psbt.contents.inputs[1]), txid, 32), WALLY_OK) + assert_no_signature(psbt) + wally_psbt_free(psbt) + # Converting to v0 cannot switch the checks off: the conversion fails... attacker_psbt = get_psbt(attacker) self.assertEqual(wally_psbt_set_version(attacker_psbt, 0, 0), WALLY_EINVAL) From cdd5d20f630e011ad1148e888cd115dd3d2342ee Mon Sep 17 00:00:00 2001 From: kkdao <44775666+kkdao@users.noreply.github.com> Date: Sun, 8 Nov 2026 23:23:12 +0900 Subject: [PATCH 11/11] psbt: Bind silent payment signing to the PSBT Checking a PSBT does not protect a signature over something else. wally_psbt_get_input_signature_hash() takes a caller's transaction and wally_psbt_sign_input_bip32() takes a caller's hash, so a transaction paying an attacker could be hashed and signed while the unchanged PSBT still verified. The signing cache could also supply parts of such a transaction to wally_psbt_sign(). With silent payment outputs, the hash call now verifies the scripts and refuses any transaction other than the one the PSBT describes, the input signing call refuses any hash but that transaction's, and the cache is not used. A caller no longer has to remember to verify first. --- include/wally_psbt.h | 13 +++-- src/psbt.c | 131 +++++++++++++++++++++++++++++++++--------- src/test/test_psbt.py | 89 ++++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 32 deletions(-) diff --git a/include/wally_psbt.h b/include/wally_psbt.h index bd716c78a..1b46b0290 100644 --- a/include/wally_psbt.h +++ b/include/wally_psbt.h @@ -2468,13 +2468,11 @@ WALLY_CORE_API int wally_psbt_get_input_scriptcode( * :param bytes_out: Destination for the signature hash. * FIXED_SIZED_OUTPUT(len, bytes_out, SHA256_LEN) * - * .. note:: This does not check BIP-375 silent payment output scripts. A caller - *| signing the hash with its own key must first check them by calling - *| `wally_psbt_compute_sp_scripts`, or the signature could pay anyone. - * - * .. note:: With silent payment outputs, fails unless only SIGHASH_ALL is used + * .. note:: With BIP-375 silent payment outputs, fails unless ``tx`` is the + *| unsigned transaction the PSBT describes, every silent payment script + *| verifies as in `wally_psbt_compute_sp_scripts`, only SIGHASH_ALL is used *| and, for a non-taproot input, every input has a non-witness utxo. See - *| `wally_psbt_sign_bip32`. + *| `wally_psbt_sign_bip32`. The signing cache is not used with such outputs. */ WALLY_CORE_API int wally_psbt_get_input_signature_hash( struct wally_psbt *psbt, @@ -2837,6 +2835,9 @@ WALLY_CORE_API int wally_psbt_sign_bip32( *| every input must also have a non-witness utxo: an ECDSA signature does *| not commit to the prevout scripts of the other inputs, so a false witness *| utxo could otherwise leave a payment its receiver cannot find. + * + * .. note:: With BIP-375 silent payment outputs, also fails unless ``txhash`` is + *| the hash of the unsigned transaction the PSBT describes. */ WALLY_CORE_API int wally_psbt_sign_input_bip32( struct wally_psbt *psbt, diff --git a/src/psbt.c b/src/psbt.c index 70506670b..178f55600 100644 --- a/src/psbt.c +++ b/src/psbt.c @@ -652,6 +652,14 @@ static bool psbt_has_sp_outputs(const struct wally_psbt *psbt) static int sp_input_prevout_script(const struct wally_psbt_input *input, const unsigned char **script, size_t *script_len); +static int psbt_sp_scripts(struct wally_psbt *psbt, bool verify_only); + +/* Before any signature: a silent payment output's script must be the one its + * code derives, or a signature could pay anyone. Every signing call checks this */ +static int psbt_check_sp_scripts(struct wally_psbt *psbt) +{ + return psbt_has_sp_outputs(psbt) ? psbt_sp_scripts(psbt, true) : WALLY_OK; +} /* An ECDSA signature does not commit to the prevout scripts of the other * inputs, so a false witness utxo on any of them could change the key sum and @@ -5751,11 +5759,11 @@ static int get_signing_data(const struct wally_psbt *psbt, return ret; } -int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index, - const struct wally_tx *tx, - const unsigned char *script, size_t script_len, - uint32_t flags, - unsigned char *bytes_out, size_t len) +static int psbt_input_signature_hash(struct wally_psbt *psbt, size_t index, + const struct wally_tx *tx, + const unsigned char *script, size_t script_len, + uint32_t flags, + unsigned char *bytes_out, size_t len) { struct wally_map scripts, assets, values, *assets_p; size_t is_pset; @@ -5801,7 +5809,10 @@ int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index, NULL, 0, #endif sighash, sighash_type, - psbt->signing_cache, bytes_out, len); + /* The cache is not tied to a transaction, so it could hold + * parts of one that the silent payment checks never saw */ + psbt_has_sp_outputs(psbt) ? NULL : psbt->signing_cache, + bytes_out, len); wally_free(scripts.items); /* No need to clear the value pointers */ wally_free(values.items); @@ -5810,6 +5821,50 @@ int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index, return ret; } +/* Whether tx is the unsigned transaction that psbt describes */ +static int sp_tx_is_psbt_tx(const struct wally_psbt *psbt, const struct wally_tx *tx) +{ + struct wally_tx *own; + bool is_pset, same; + size_t i; + int ret = psbt_build_tx(psbt, &own, &is_pset, false, false); + + if (ret != WALLY_OK) + return ret; + same = tx->version == own->version && tx->locktime == own->locktime && + tx->num_inputs == own->num_inputs && tx->num_outputs == own->num_outputs; + for (i = 0; same && i < own->num_inputs; ++i) { + const struct wally_tx_input *a = tx->inputs + i, *b = own->inputs + i; + same = !memcmp(a->txhash, b->txhash, sizeof(a->txhash)) && + a->index == b->index && a->sequence == b->sequence; + } + for (i = 0; same && i < own->num_outputs; ++i) { + const struct wally_tx_output *a = tx->outputs + i, *b = own->outputs + i; + same = a->satoshi == b->satoshi && a->script_len == b->script_len && + (!a->script_len || !memcmp(a->script, b->script, a->script_len)); + } + wally_tx_free(own); + return same ? WALLY_OK : WALLY_EINVAL; +} + +int wally_psbt_get_input_signature_hash(struct wally_psbt *psbt, size_t index, + const struct wally_tx *tx, + const unsigned char *script, size_t script_len, + uint32_t flags, + unsigned char *bytes_out, size_t len) +{ + if (psbt_is_valid(psbt) && tx && psbt_has_sp_outputs(psbt)) { + /* A hash is only useful for signing: tie it to verified outputs */ + int ret = psbt_check_sp_scripts(psbt); + if (ret == WALLY_OK) + ret = sp_tx_is_psbt_tx(psbt, tx); + if (ret != WALLY_OK) + return ret; + } + return psbt_input_signature_hash(psbt, index, tx, script, script_len, flags, + bytes_out, len); +} + static int psbt_sign_input_impl(struct wally_psbt *psbt, size_t index, size_t subindex, const unsigned char *txhash, size_t txhash_len, @@ -5895,11 +5950,30 @@ static int psbt_sign_input_impl(struct wally_psbt *psbt, return ret; } -/* Before any signature: a silent payment output's script must be the one its - * code derives, or a signature could pay anyone. Every signing call checks this */ -static int psbt_check_sp_scripts(struct wally_psbt *psbt) +/* The hash an input's signature must commit to, over the transaction tx */ +static int psbt_input_own_hash(struct wally_psbt *psbt, size_t index, + const struct wally_tx *tx, + unsigned char *txhash, size_t txhash_len) { - return psbt_has_sp_outputs(psbt) ? psbt_sp_scripts(psbt, true) : WALLY_OK; + unsigned char p2pkh[WALLY_SCRIPTPUBKEY_P2PKH_LEN]; + const unsigned char *script, *scriptcode; + size_t script_len, scriptcode_len; + + /* Get the scriptpubkey or redeemscript */ + int ret = get_signing_script(psbt, index, &script, &script_len); + + /* Get the actual script to sign with */ + if (ret == WALLY_OK) + ret = get_scriptcode(psbt, index, p2pkh, sizeof(p2pkh), + script, script_len, + &scriptcode, &scriptcode_len); + + /* Get the hash to sign */ + if (ret == WALLY_OK) + ret = psbt_input_signature_hash(psbt, index, tx, + scriptcode, scriptcode_len, + 0, txhash, txhash_len); + return ret; } int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, @@ -5909,6 +5983,24 @@ int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, uint32_t flags) { int ret = psbt_is_valid(psbt) ? psbt_check_sp_scripts(psbt) : WALLY_OK; + + if (ret == WALLY_OK && psbt_is_valid(psbt) && psbt_has_sp_outputs(psbt)) { + /* The hash comes from the caller: it must be for this PSBT's transaction */ + unsigned char own_hash[SHA256_LEN]; + struct wally_tx *tx; + bool is_pset; + + if (!txhash || txhash_len != sizeof(own_hash) || index >= psbt->num_inputs) + return WALLY_EINVAL; + if ((ret = psbt_build_tx(psbt, &tx, &is_pset, false, false)) != WALLY_OK) + return ret; + ret = psbt_input_own_hash(psbt, index, tx, own_hash, sizeof(own_hash)); + wally_tx_free(tx); + if (ret == WALLY_OK && memcmp(own_hash, txhash, sizeof(own_hash))) + ret = WALLY_EINVAL; + if (ret != WALLY_OK) + return ret; + } if (ret == WALLY_OK) ret = psbt_sign_input_impl(psbt, index, subindex, txhash, txhash_len, hdkey, flags); return ret; @@ -5917,7 +6009,6 @@ int wally_psbt_sign_input_bip32(struct wally_psbt *psbt, int wally_psbt_sign_bip32(struct wally_psbt *psbt, const struct ext_key *hdkey, uint32_t flags) { - unsigned char p2pkh[WALLY_SCRIPTPUBKEY_P2PKH_LEN]; size_t i; bool is_pset; int ret; @@ -5944,8 +6035,7 @@ int wally_psbt_sign_bip32(struct wally_psbt *psbt, /* Go through each of the inputs */ for (i = 0; ret == WALLY_OK && i < psbt->num_inputs; ++i) { unsigned char txhash[WALLY_TXHASH_LEN]; - const unsigned char *script, *scriptcode; - size_t script_len, scriptcode_len, subindex = 0; + size_t subindex = 0; struct ext_key derived; bool found; @@ -5958,21 +6048,10 @@ int wally_psbt_sign_bip32(struct wally_psbt *psbt, if (ret == WALLY_OK && !found) continue; /* No key to sign with */ - /* Get the scriptpubkey or redeemscript */ - if (ret == WALLY_OK) - ret = get_signing_script(psbt, i, &script, &script_len); - - /* Get the actual script to sign with */ - if (ret == WALLY_OK) - ret = get_scriptcode(psbt, i, p2pkh, sizeof(p2pkh), - script, script_len, - &scriptcode, &scriptcode_len); - /* Get the hash to sign */ if (ret == WALLY_OK) - ret = wally_psbt_get_input_signature_hash(psbt, i, tx, - scriptcode, scriptcode_len, - 0, txhash, sizeof(txhash)); + ret = psbt_input_own_hash(psbt, i, tx, txhash, sizeof(txhash)); + /* Sign the input */ if (ret == WALLY_OK) ret = psbt_sign_input_impl(psbt, i, subindex, diff --git a/src/test/test_psbt.py b/src/test/test_psbt.py index 8466bc629..df11af9e9 100644 --- a/src/test/test_psbt.py +++ b/src/test/test_psbt.py @@ -1315,6 +1315,90 @@ def get_input_hash(psbt, tx): wally_tx_free(tx) wally_psbt_free(psbt) + # A hash is only given for, and a signature only made over, the + # transaction the PSBT describes + def get_own_hash(psbt, tx): + pub, pub_len = make_cbuffer('00' * 33) + self.assertEqual(wally_ec_public_key_from_private_key(keys[0], 32, pub, pub_len), WALLY_OK) + h160, h160_len = make_cbuffer('00' * 20) + self.assertEqual(wally_hash160(pub, pub_len, h160, h160_len), WALLY_OK) + scriptcode = b'\x76\xa9\x14' + bytes(h160) + b'\x88\xac' + out, out_len = make_cbuffer('00' * 32) + ret = wally_psbt_get_input_signature_hash(psbt, 0, tx, scriptcode, len(scriptcode), 0, out, out_len) + return ret, bytes(out) + + def get_hdkey(): + pub, pub_len = make_cbuffer('00' * 33) + self.assertEqual(wally_ec_public_key_from_private_key(keys[0], 32, pub, pub_len), WALLY_OK) + hdkey = pointer(ext_key()) + ret = bip32_key_init_alloc(0x0488ADE4, 0, 0, bytes(32), 32, pub, pub_len, keys[0], 32, + None, 0, None, 0, hdkey) # Mainnet private + self.assertEqual(ret, WALLY_OK) + return hdkey + + def check_sig(psbt, want): + """Whether the signature made for input 0 verifies over the hash want""" + pub, pub_len = make_cbuffer('00' * 33) + self.assertEqual(wally_ec_public_key_from_private_key(keys[0], 32, pub, pub_len), WALLY_OK) + item = psbt.contents.inputs[0].signatures.items[0] + der = string_at(item.value, item.value_len) + sig, sig_len = make_cbuffer('00' * 64) + self.assertEqual(wally_ec_sig_from_der(der, len(der) - 1, sig, sig_len), WALLY_OK) + return wally_ec_sig_verify(pub, pub_len, want, 32, 0x1, sig, sig_len) # EC_FLAG_ECDSA + + psbt = get_psbt() + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_OK) + ret, good_hash = get_own_hash(psbt, tx) + self.assertEqual(ret, WALLY_OK) + # Any change to the caller's transaction is refused + output = tx.contents.outputs[0] + good_script = string_at(output.script, output.script_len) + good_amount = output.satoshi + self.assertEqual(wally_tx_set_output_script(tx, 0, attacker, len(attacker)), WALLY_OK) + self.assertEqual(get_own_hash(psbt, tx)[0], WALLY_EINVAL) + self.assertEqual(wally_tx_set_output_script(tx, 0, good_script, len(good_script)), WALLY_OK) + self.assertEqual(wally_tx_set_output_satoshi(tx, 0, good_amount - 1), WALLY_OK) + self.assertEqual(get_own_hash(psbt, tx)[0], WALLY_EINVAL) + self.assertEqual(wally_tx_set_output_satoshi(tx, 0, good_amount), WALLY_OK) + tx.contents.locktime += 1 + self.assertEqual(get_own_hash(psbt, tx)[0], WALLY_EINVAL) + tx.contents.locktime -= 1 + tx.contents.inputs[0].sequence ^= 1 + self.assertEqual(get_own_hash(psbt, tx)[0], WALLY_EINVAL) + tx.contents.inputs[0].sequence ^= 1 + self.assertEqual(get_own_hash(psbt, tx), (WALLY_OK, good_hash)) + # A hash of anything else is not signed, while the PSBT's own is + hdkey = get_hdkey() + for bad in [bytes(32), bytes(reversed(good_hash))]: + ret = wally_psbt_sign_input_bip32(psbt, 0, 0, bad, 32, hdkey, 0) + self.assertEqual(ret, WALLY_EINVAL) + self.assertEqual(num_sigs(psbt), 0) + self.assertEqual(wally_psbt_sign_input_bip32(psbt, 0, 0, good_hash, 32, hdkey, 0), WALLY_OK) + self.assertEqual(num_sigs(psbt), 1) + self.assertEqual(check_sig(psbt, good_hash), WALLY_OK) + bip32_key_free(hdkey) + wally_psbt_free(psbt) + + # The signing cache cannot carry in parts of a transaction the checks never saw. + # Filling it while the output has no code, then restoring the code, changes nothing + psbt = get_psbt() + info = bytearray(66) + out = byref(psbt.contents.outputs[0]) + ret, written = wally_psbt_output_get_sp_v0_info(out, (c_ubyte * 66).from_buffer(info), 66) + self.assertEqual((ret, written), (WALLY_OK, 66)) + self.assertEqual(wally_psbt_signing_cache_enable(psbt, 0), WALLY_OK) + self.assertEqual(wally_psbt_output_clear_sp_v0_info(out), WALLY_OK) + self.assertEqual(wally_tx_set_output_script(tx, 0, attacker, len(attacker)), WALLY_OK) + self.assertEqual(get_own_hash(psbt, tx)[0], WALLY_OK) # Plain output: not checked + self.assertEqual(wally_tx_set_output_script(tx, 0, good_script, len(good_script)), WALLY_OK) + self.assertEqual(wally_psbt_output_set_sp_v0_info(out, bytes(info), 66), WALLY_OK) + self.assertEqual(wally_psbt_sign(psbt, keys[0], 32, 0), WALLY_OK) + self.assertEqual(num_sigs(psbt), 1) + self.assertEqual(check_sig(psbt, good_hash), WALLY_OK) + wally_tx_free(tx) + wally_psbt_free(psbt) + # An ECDSA signature does not commit to the other inputs' prevout scripts, # so a false witness utxo could leave the payment unfindable. Without # non-witness utxos proving every prevout, nothing can be signed @@ -1408,6 +1492,9 @@ def get_input_hash(psbt, tx): # BIP-375: nothing is signed while any output has no script, as one # with no script pays whoever spends it first psbt = get_psbt() + tx = pointer(wally_tx()) + self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_OK) + self.assertEqual(wally_tx_add_raw_output_at(tx, 1, 1000, None, 0, 0), WALLY_OK) tx_out = pointer(wally_tx_output()) self.assertEqual(wally_tx_output_init_alloc(1000, None, 0, tx_out), WALLY_OK) self.assertEqual(wally_psbt_set_tx_modifiable_flags(psbt, 0x2), WALLY_OK) # WALLY_PSBT_TXMOD_OUTPUTS @@ -1416,6 +1503,8 @@ def get_input_hash(psbt, tx): wally_tx_output_free(tx_out) self.assertFalse(psbt.contents.outputs[1].script) assert_no_signature(psbt) + self.assertEqual(get_own_hash(psbt, tx)[0], WALLY_EINVAL) # tx matches the PSBT + wally_tx_free(tx) tx = pointer(wally_tx()) self.assertEqual(wally_psbt_extract(psbt, WALLY_PSBT_EXTRACT_NON_FINAL, tx), WALLY_EINVAL) wally_psbt_free(psbt)