diff --git a/examples/silentpayments.c b/examples/silentpayments.c index 7f133ebfa6..2f77fe6d10 100644 --- a/examples/silentpayments.c +++ b/examples/silentpayments.c @@ -218,6 +218,10 @@ int main(void) { for (i = 0; i < N_TX_OUTPUTS; i++) { generated_output_ptrs[i] = &generated_outputs[i]; } + + ret = secp256k1_silentpayments_test_outputs(ctx, recipients, N_TX_OUTPUTS); + assert(ret); + ret = secp256k1_silentpayments_sender_create_outputs(ctx, generated_output_ptrs, recipient_ptrs, N_TX_OUTPUTS, diff --git a/include/secp256k1_silentpayments.h b/include/secp256k1_silentpayments.h index bdd8c1c23d..6338fc9323 100644 --- a/include/secp256k1_silentpayments.h +++ b/include/secp256k1_silentpayments.h @@ -107,6 +107,12 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_sender_c size_t n_plain_seckeys ) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5); +SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_test_outputs( + const secp256k1_context *ctx, + const secp256k1_silentpayments_recipient *recipients, + size_t n_recipients +) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2); + /** Create Silent Payment label tweak and label. * * Given a recipient's scan key b_scan and a label integer m, calculate the diff --git a/src/modules/silentpayments/main_impl.h b/src/modules/silentpayments/main_impl.h index 21190d8ffb..1da107a2ea 100644 --- a/src/modules/silentpayments/main_impl.h +++ b/src/modules/silentpayments/main_impl.h @@ -139,6 +139,60 @@ static int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context return ret; } +static void print_hex(unsigned char* data, size_t size) { + size_t i; + printf("0x"); + for (i = 0; i < size; i++) { + printf("%02x", data[i]); + } + printf("\n"); +} + +int secp256k1_silentpayments_test_outputs( + const secp256k1_context *ctx, + const secp256k1_silentpayments_recipient *recipients, + size_t n_recipients +) { + size_t i; + int ret = 1; + + + for (i = 0; i < n_recipients; i++) { + ARG_CHECK(recipients[i].index == i); + } + + for (i = 0; i < n_recipients; i++) { + int return_val; + unsigned char compressed_scan_pubkey[33]; + unsigned char compressed_spend_pubkey[33]; + size_t len; + + printf("index: %ld\n", recipients[i].index); + + /* Serialize pubkey1 in a compressed form (33 bytes), should always return 1 */ + len = sizeof(compressed_scan_pubkey); + return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_scan_pubkey, &len, &recipients[i].scan_pubkey, SECP256K1_EC_COMPRESSED); + VERIFY_CHECK(return_val); + /* Should be the same size as the size of the output, because we passed a 33 byte array. */ + VERIFY_CHECK(len == sizeof(compressed_scan_pubkey)); + + len = sizeof(compressed_spend_pubkey); + return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_spend_pubkey, &len, &recipients[i].spend_pubkey, SECP256K1_EC_COMPRESSED); + VERIFY_CHECK(return_val); + /* Should be the same size as the size of the output, because we passed a 33 byte array. */ + VERIFY_CHECK(len == sizeof(compressed_spend_pubkey)); + + printf("scan_pubkey: "); + print_hex(compressed_scan_pubkey, sizeof(compressed_scan_pubkey)); + + printf("spend_pubkey: "); + print_hex(compressed_spend_pubkey, sizeof(compressed_spend_pubkey)); + + } + + return ret; +} + int secp256k1_silentpayments_sender_create_outputs( const secp256k1_context *ctx, secp256k1_xonly_pubkey **generated_outputs,