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 c4e3d89 commit cebd16d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 17 deletions.
17 changes: 15 additions & 2 deletions examples/silentpayments.c
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ const unsigned char* label_lookup(
}

int main(void) {
enum { N_TX_INPUTS = 2, N_TX_OUTPUTS = 3 };
enum { N_TX_INPUTS = 2, N_TX_OUTPUTS = 3, N_OUT_PUBKEYS = 198 };

int n_out_pubkeys = N_OUT_PUBKEYS;
unsigned char out_pubkeys[N_OUT_PUBKEYS];

unsigned char randomize[32];
unsigned char xonly_print[32];
secp256k1_xonly_pubkey tx_inputs[N_TX_INPUTS];
Expand Down Expand Up @@ -219,8 +223,17 @@ int main(void) {
generated_output_ptrs[i] = &generated_outputs[i];
}

ret = secp256k1_silentpayments_test_outputs(ctx, recipients, N_TX_OUTPUTS);
ret = secp256k1_silentpayments_test_outputs(
ctx,
recipients,
N_TX_OUTPUTS,
out_pubkeys,
n_out_pubkeys
);
assert(ret);

printf("out_pubkeys: ");
print_hex(out_pubkeys, n_out_pubkeys);

ret = secp256k1_silentpayments_sender_create_outputs(ctx,
generated_output_ptrs,
Expand Down
4 changes: 3 additions & 1 deletion include/secp256k1_silentpayments.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_sender_c
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_test_outputs(
const secp256k1_context *ctx,
const secp256k1_silentpayments_recipient *recipients,
size_t n_recipients
size_t n_recipients,
unsigned char *out_pubkeys,
size_t n_out_pubkeys
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);

/** Create Silent Payment label tweak and label.
Expand Down
37 changes: 23 additions & 14 deletions src/modules/silentpayments/main_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,54 +139,63 @@ static int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context
return ret;
}

static void print_hex(unsigned char* data, size_t size) {
/* 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 n_recipients,
unsigned char *out_pubkeys,
size_t n_out_pubkeys
) {
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[33];
unsigned char compressed_spend_pubkey[33];
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);
// printf("index: %ld\n", recipients[i].index);

/* Serialize pubkey1 in a compressed form (33 bytes), should always return 1 */
len = sizeof(compressed_scan_pubkey);
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 == sizeof(compressed_scan_pubkey));
VERIFY_CHECK(len == 33);

len = sizeof(compressed_spend_pubkey);
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 == sizeof(compressed_spend_pubkey));
VERIFY_CHECK(len == 33);

printf("scan_pubkey: ");
print_hex(compressed_scan_pubkey, sizeof(compressed_scan_pubkey));
// printf("scan_pubkey: ");
// print_hex(compressed_scan_pubkey, 33);

printf("spend_pubkey: ");
print_hex(compressed_spend_pubkey, sizeof(compressed_spend_pubkey));
// printf("spend_pubkey: ");
// print_hex(compressed_spend_pubkey, 33);

}

VERIFY_CHECK((i * 66) == n_out_pubkeys);

return ret;
}

Expand Down

0 comments on commit cebd16d

Please sign in to comment.