Skip to content

Commit 34068bc

Browse files
committed
Add silentpayments_test_outputs
1 parent 00b0cb1 commit 34068bc

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

examples/silentpayments.c

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ int main(void) {
218218
for (i = 0; i < N_TX_OUTPUTS; i++) {
219219
generated_output_ptrs[i] = &generated_outputs[i];
220220
}
221+
222+
ret = secp256k1_silentpayments_test_outputs(ctx, recipients, N_TX_OUTPUTS);
223+
assert(ret);
224+
221225
ret = secp256k1_silentpayments_sender_create_outputs(ctx,
222226
generated_output_ptrs,
223227
recipient_ptrs, N_TX_OUTPUTS,

include/secp256k1_silentpayments.h

+6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_sender_c
107107
size_t n_plain_seckeys
108108
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(5);
109109

110+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_test_outputs(
111+
const secp256k1_context *ctx,
112+
const secp256k1_silentpayments_recipient *recipients,
113+
size_t n_recipients
114+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2);
115+
110116
/** Create Silent Payment label tweak and label.
111117
*
112118
* Given a recipient's scan key b_scan and a label integer m, calculate the

src/modules/silentpayments/main_impl.h

+54
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,60 @@ static int secp256k1_silentpayments_create_output_pubkey(const secp256k1_context
139139
return ret;
140140
}
141141

142+
static void print_hex(unsigned char* data, size_t size) {
143+
size_t i;
144+
printf("0x");
145+
for (i = 0; i < size; i++) {
146+
printf("%02x", data[i]);
147+
}
148+
printf("\n");
149+
}
150+
151+
int secp256k1_silentpayments_test_outputs(
152+
const secp256k1_context *ctx,
153+
const secp256k1_silentpayments_recipient *recipients,
154+
size_t n_recipients
155+
) {
156+
size_t i;
157+
int ret = 1;
158+
159+
160+
for (i = 0; i < n_recipients; i++) {
161+
ARG_CHECK(recipients[i].index == i);
162+
}
163+
164+
for (i = 0; i < n_recipients; i++) {
165+
int return_val;
166+
unsigned char compressed_scan_pubkey[33];
167+
unsigned char compressed_spend_pubkey[33];
168+
size_t len;
169+
170+
printf("index: %ld\n", recipients[i].index);
171+
172+
/* Serialize pubkey1 in a compressed form (33 bytes), should always return 1 */
173+
len = sizeof(compressed_scan_pubkey);
174+
return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_scan_pubkey, &len, &recipients[i].scan_pubkey, SECP256K1_EC_COMPRESSED);
175+
VERIFY_CHECK(return_val);
176+
/* Should be the same size as the size of the output, because we passed a 33 byte array. */
177+
VERIFY_CHECK(len == sizeof(compressed_scan_pubkey));
178+
179+
len = sizeof(compressed_spend_pubkey);
180+
return_val = secp256k1_ec_pubkey_serialize(ctx, compressed_spend_pubkey, &len, &recipients[i].spend_pubkey, SECP256K1_EC_COMPRESSED);
181+
VERIFY_CHECK(return_val);
182+
/* Should be the same size as the size of the output, because we passed a 33 byte array. */
183+
VERIFY_CHECK(len == sizeof(compressed_spend_pubkey));
184+
185+
printf("scan_pubkey: ");
186+
print_hex(compressed_scan_pubkey, sizeof(compressed_scan_pubkey));
187+
188+
printf("spend_pubkey: ");
189+
print_hex(compressed_spend_pubkey, sizeof(compressed_spend_pubkey));
190+
191+
}
192+
193+
return ret;
194+
}
195+
142196
int secp256k1_silentpayments_sender_create_outputs(
143197
const secp256k1_context *ctx,
144198
secp256k1_xonly_pubkey **generated_outputs,

0 commit comments

Comments
 (0)