From 9c72e7a2b48148b6847268fec1215ce4deab5764 Mon Sep 17 00:00:00 2001 From: jlest01 <174762002+jlest01@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:53:19 -0300 Subject: [PATCH] Add `silentpayments_test_outputs` --- examples/silentpayments.c | 29 ++++++++++++++----- include/secp256k1_silentpayments.h | 3 +- src/modules/silentpayments/main_impl.h | 40 +++++--------------------- 3 files changed, 30 insertions(+), 42 deletions(-) diff --git a/examples/silentpayments.c b/examples/silentpayments.c index 9777b47279..b2301212f4 100644 --- a/examples/silentpayments.c +++ b/examples/silentpayments.c @@ -111,10 +111,9 @@ const unsigned char* label_lookup( } int main(void) { - enum { N_TX_INPUTS = 2, N_TX_OUTPUTS = 3, N_OUT_PUBKEYS = 198 }; + enum { N_TX_INPUTS = 2, N_TX_OUTPUTS = 3 }; - int n_out_pubkeys = N_OUT_PUBKEYS; - unsigned char out_pubkeys[N_OUT_PUBKEYS]; + secp256k1_xonly_pubkey out_pubkeys[N_TX_OUTPUTS]; unsigned char randomize[32]; unsigned char xonly_print[32]; @@ -227,13 +226,29 @@ int main(void) { ctx, recipients, N_TX_OUTPUTS, - out_pubkeys, - n_out_pubkeys + out_pubkeys ); assert(ret); - printf("out_pubkeys: "); - print_hex(out_pubkeys, n_out_pubkeys); + for (i = 0; i < N_TX_OUTPUTS; i++) { + unsigned char serialized_pubkey[33]; + unsigned char serialized_xonly_pubkey[32]; + size_t len; + + len = 33; + ret = secp256k1_ec_pubkey_serialize(ctx, serialized_pubkey, &len, &recipients[i].scan_pubkey, SECP256K1_EC_COMPRESSED); + + ret = secp256k1_xonly_pubkey_serialize(ctx, serialized_xonly_pubkey, &out_pubkeys[i]); + assert(ret); + + printf("Output %li:\n", i); + print_hex(serialized_pubkey, 33); + print_hex(serialized_xonly_pubkey, 32); + } + + + /* printf("out_pubkeys: "); + print_hex(out_pubkeys, n_out_pubkeys); */ ret = secp256k1_silentpayments_sender_create_outputs(ctx, generated_output_ptrs, diff --git a/include/secp256k1_silentpayments.h b/include/secp256k1_silentpayments.h index b280309a25..46cd021fdf 100644 --- a/include/secp256k1_silentpayments.h +++ b/include/secp256k1_silentpayments.h @@ -111,8 +111,7 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_test_out const secp256k1_context *ctx, const secp256k1_silentpayments_recipient *recipients, size_t n_recipients, - unsigned char *out_pubkeys, - size_t n_out_pubkeys + secp256k1_xonly_pubkey *generated_outputs ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); /** Create Silent Payment label tweak and label. diff --git a/src/modules/silentpayments/main_impl.h b/src/modules/silentpayments/main_impl.h index f79ef55ce5..e7f486f42e 100644 --- a/src/modules/silentpayments/main_impl.h +++ b/src/modules/silentpayments/main_impl.h @@ -152,50 +152,24 @@ int secp256k1_silentpayments_test_outputs( const secp256k1_context *ctx, const secp256k1_silentpayments_recipient *recipients, size_t n_recipients, - unsigned char *out_pubkeys, - size_t n_out_pubkeys + secp256k1_xonly_pubkey *generated_outputs ) { size_t i; int ret = 1; - VERIFY_CHECK(n_out_pubkeys == 66 * n_recipients); - for (i = 0; i < n_recipients; i++) { ARG_CHECK(recipients[i].index == i); } - /* Initialize out_pubkeys to zero */ - memset(out_pubkeys, 0, n_out_pubkeys); - for (i = 0; i < n_recipients; i++) { - unsigned char *compressed_scan_pubkey = &out_pubkeys[i * 66]; - unsigned char *compressed_spend_pubkey = &out_pubkeys[i * 66 + 33]; - - size_t len; - - // printf("index: %ld\n", recipients[i].index); - - /* Serialize pubkey1 in a compressed form (33 bytes), should always return 1 */ - len = 33; - ret = secp256k1_ec_pubkey_serialize(ctx, compressed_scan_pubkey, &len, &recipients[i].scan_pubkey, SECP256K1_EC_COMPRESSED); - /* Should be the same size as the size of the output, because we passed a 33 byte array. */ - VERIFY_CHECK(len == 33); - - len = 33; - ret = secp256k1_ec_pubkey_serialize(ctx, compressed_spend_pubkey, &len, &recipients[i].spend_pubkey, SECP256K1_EC_COMPRESSED); - /* Should be the same size as the size of the output, because we passed a 33 byte array. */ - VERIFY_CHECK(len == 33); - - // printf("scan_pubkey: "); - // print_hex(compressed_scan_pubkey, 33); - - // printf("spend_pubkey: "); - // print_hex(compressed_spend_pubkey, 33); - + secp256k1_xonly_pubkey new_xonly_pubkey; + ret = secp256k1_xonly_pubkey_from_pubkey(ctx, &new_xonly_pubkey, NULL, &recipients[i].scan_pubkey); + if (!ret) { + return 0; + } + generated_outputs[i] = new_xonly_pubkey; } - VERIFY_CHECK((i * 66) == n_out_pubkeys); - return ret; }