Skip to content

Commit

Permalink
Add silentpayments_test_outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
jlest01 committed Aug 1, 2024
1 parent 00b0cb1 commit baf189e
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
4 changes: 4 additions & 0 deletions examples/silentpayments.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions include/secp256k1_silentpayments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
51 changes: 51 additions & 0 deletions src/modules/silentpayments/main_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,57 @@ 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++) {
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);
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 == sizeof(compressed_scan_pubkey));

len = sizeof(compressed_spend_pubkey);
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 == 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,
Expand Down

0 comments on commit baf189e

Please sign in to comment.