Skip to content

Commit b502072

Browse files
committed
psbt: add a known-length call for psbt_from_base64
1 parent cabd186 commit b502072

File tree

9 files changed

+46
-11
lines changed

9 files changed

+46
-11
lines changed

include/wally.hpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,9 +1214,15 @@ inline int psbt_free(struct wally_psbt* psbt) {
12141214
return detail::check_ret(__FUNCTION__, ret);
12151215
}
12161216

1217-
template <class BASE64>
1218-
inline int psbt_from_base64(const BASE64& base64, uint32_t flags, struct wally_psbt** output) {
1219-
int ret = ::wally_psbt_from_base64(detail::get_p(base64), flags, output);
1217+
template <class STR_IN>
1218+
inline int psbt_from_base64(const STR_IN& str_in, uint32_t flags, struct wally_psbt** output) {
1219+
int ret = ::wally_psbt_from_base64(detail::get_p(str_in), flags, output);
1220+
return detail::check_ret(__FUNCTION__, ret);
1221+
}
1222+
1223+
template <class STR_IN>
1224+
inline int psbt_from_base64_n(const STR_IN& str_in, size_t str_len, uint32_t flags, struct wally_psbt** output) {
1225+
int ret = ::wally_psbt_from_base64_n(detail::get_p(str_in), str_len, flags, output);
12201226
return detail::check_ret(__FUNCTION__, ret);
12211227
}
12221228

include/wally_psbt.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2363,14 +2363,25 @@ WALLY_CORE_API int wally_psbt_to_bytes(
23632363
size_t *written);
23642364

23652365
/**
2366-
* Create a PSBT from its serialized base64 string.
2366+
* Create a PSBT from a serialized base64 string.
23672367
*
2368-
* :param base64: Base64 string to create the PSBT from.
2368+
* :param str_in: Base64 string to create the PSBT from.
23692369
* :param flags: `WALLY_PSBT_PARSE_FLAG_STRICT` or 0.
23702370
* :param output: Destination for the resulting PSBT.
23712371
*/
23722372
WALLY_CORE_API int wally_psbt_from_base64(
2373-
const char *base64,
2373+
const char *str_in,
2374+
uint32_t flags,
2375+
struct wally_psbt **output);
2376+
2377+
/**
2378+
* Create a PSBT from a known-length serialized base64 string.
2379+
*
2380+
* See `wally_psbt_from_base64`.
2381+
*/
2382+
WALLY_CORE_API int wally_psbt_from_base64_n(
2383+
const char *str_in,
2384+
size_t str_len,
23742385
uint32_t flags,
23752386
struct wally_psbt **output);
23762387

src/psbt.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3357,22 +3357,23 @@ int wally_psbt_to_bytes(const struct wally_psbt *psbt, uint32_t flags,
33573357
return WALLY_OK;
33583358
}
33593359

3360-
int wally_psbt_from_base64(const char *base64, uint32_t flags, struct wally_psbt **output)
3360+
int wally_psbt_from_base64_n(const char *str_in, size_t str_len, uint32_t flags, struct wally_psbt **output)
33613361
{
33623362
unsigned char *decoded;
33633363
size_t max_len, written;
33643364
int ret;
33653365

33663366
OUTPUT_CHECK;
3367-
if ((ret = wally_base64_get_maximum_length(base64, 0, &max_len)) != WALLY_OK)
3367+
if ((ret = wally_base64_n_get_maximum_length(str_in, str_len, 0, &max_len)) != WALLY_OK)
33683368
return ret;
33693369

33703370
/* Allocate the buffer to decode into */
33713371
if ((decoded = wally_malloc(max_len)) == NULL)
33723372
return WALLY_ENOMEM;
33733373

33743374
/* Decode the base64 psbt into binary */
3375-
if ((ret = wally_base64_to_bytes(base64, 0, decoded, max_len, &written)) != WALLY_OK)
3375+
ret = wally_base64_n_to_bytes(str_in, str_len, 0, decoded, max_len, &written);
3376+
if (ret != WALLY_OK)
33763377
goto done;
33773378

33783379
if (written <= sizeof(PSBT_MAGIC)) {
@@ -3392,6 +3393,12 @@ int wally_psbt_from_base64(const char *base64, uint32_t flags, struct wally_psbt
33923393
return ret;
33933394
}
33943395

3396+
int wally_psbt_from_base64(const char *str_in, uint32_t flags, struct wally_psbt **output)
3397+
{
3398+
size_t str_len = str_in ? strlen(str_in) : 0;
3399+
return wally_psbt_from_base64_n(str_in, str_len, flags, output);
3400+
}
3401+
33953402
int wally_psbt_to_base64(const struct wally_psbt *psbt, uint32_t flags, char **output)
33963403
{
33973404
unsigned char *buff;

src/swig_java/swig.i

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ static jobjectArray create_jstringArray(JNIEnv *jenv, char **p, size_t len) {
731731
%returns_size_t(wally_psbt_find_global_scalar);
732732
%returns_void__(wally_psbt_free);
733733
%returns_struct(wally_psbt_from_base64, wally_psbt);
734+
%returns_struct(wally_psbt_from_base64_n, wally_psbt);
734735
%returns_struct(wally_psbt_from_bytes, wally_psbt);
735736
%returns_struct(wally_psbt_from_tx, wally_psbt);
736737
%returns_void__(wally_psbt_generate_input_explicit_proofs);

src/test/test_psbt.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,13 +320,19 @@ def test_invalid_args(self):
320320

321321
# psbt_from_base64
322322
src_base64 = JSON['valid'][0]['psbt']
323+
src_len = len(src_base64)
323324
for args in [(None, 0, psbt), # NULL base64
324325
('', 0, psbt), # Empty base64
325326
(src_base64, 0xff, psbt), # Invalid flags
326327
(src_base64, 0, None)]: # NULL dest
327328
self.assertEqual(WALLY_EINVAL, wally_psbt_from_base64(*args))
328329

329-
self.assertEqual(WALLY_OK, wally_psbt_from_base64(JSON['valid'][0]['psbt'], 0, psbt))
330+
for args in [(None, src_len, 0, psbt), # NULL base64 string, non-0 length
331+
(src_base64, 0, 0, psbt)]: # Non-NULL base64 string, 0 length
332+
self.assertEqual(WALLY_EINVAL, wally_psbt_from_base64_n(*args))
333+
334+
self.assertEqual(WALLY_OK, wally_psbt_from_base64(src_base64, 0, psbt))
335+
self.assertEqual(WALLY_OK, wally_psbt_from_base64_n(src_base64, src_len, 0, psbt))
330336

331337
# psbt_clone_alloc
332338
clone = pointer(wally_psbt())

src/test/util.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,7 @@ class wally_psbt(Structure):
448448
('wally_psbt_find_input_spending_utxo', c_int, [POINTER(wally_psbt), c_void_p, c_size_t, c_uint32, c_size_t_p]),
449449
('wally_psbt_free', c_int, [POINTER(wally_psbt)]),
450450
('wally_psbt_from_base64', c_int, [c_char_p, c_uint32, POINTER(POINTER(wally_psbt))]),
451+
('wally_psbt_from_base64_n', c_int, [c_char_p, c_size_t, c_uint32, POINTER(POINTER(wally_psbt))]),
451452
('wally_psbt_from_bytes', c_int, [c_void_p, c_size_t, c_uint32, POINTER(POINTER(wally_psbt))]),
452453
('wally_psbt_from_tx', c_int, [POINTER(wally_tx), c_uint32, c_uint32, POINTER(POINTER(wally_psbt))]),
453454
('wally_psbt_get_id', c_int, [POINTER(wally_psbt), c_uint32, c_void_p, c_size_t]),

src/wasm_package/src/functions.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/wasm_package/src/index.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,8 @@ export function psbt_find_input_unknown(psbt: Ref_wally_psbt, index: number, key
277277
export function psbt_find_output_keypath(psbt: Ref_wally_psbt, index: number, key: Buffer|Uint8Array): number;
278278
export function psbt_find_output_unknown(psbt: Ref_wally_psbt, index: number, key: Buffer|Uint8Array): number;
279279
export function psbt_free(psbt: Ref_wally_psbt): void;
280-
export function psbt_from_base64(base64: string, flags: number): Ref_wally_psbt;
280+
export function psbt_from_base64(str_in: string, flags: number): Ref_wally_psbt;
281+
export function psbt_from_base64_n(str_in: string, str_len: number, flags: number): Ref_wally_psbt;
281282
export function psbt_from_bytes(bytes: Buffer|Uint8Array, flags: number): Ref_wally_psbt;
282283
export function psbt_from_tx(tx: Ref_wally_tx, version: number, flags: number): Ref_wally_psbt;
283284
export function psbt_generate_input_explicit_proofs(psbt: Ref_wally_psbt, index: number, satoshi: bigint, asset: Buffer|Uint8Array, abf: Buffer|Uint8Array, vbf: Buffer|Uint8Array, entropy: Buffer|Uint8Array): void;

tools/wasm_exports.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ EXPORTED_FUNCTIONS="['_malloc','_free','_bip32_key_free' \
211211
,'_wally_psbt_find_output_unknown' \
212212
,'_wally_psbt_free' \
213213
,'_wally_psbt_from_base64' \
214+
,'_wally_psbt_from_base64_n' \
214215
,'_wally_psbt_from_bytes' \
215216
,'_wally_psbt_from_tx' \
216217
,'_wally_psbt_get_fallback_locktime' \

0 commit comments

Comments
 (0)