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 2, 2024
1 parent cebd16d commit 9c72e7a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 42 deletions.
29 changes: 22 additions & 7 deletions examples/silentpayments.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions include/secp256k1_silentpayments.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 7 additions & 33 deletions src/modules/silentpayments/main_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down

0 comments on commit 9c72e7a

Please sign in to comment.