Skip to content

Commit 521d6e7

Browse files
committed
silentpayments: add sortable recipient struct
1 parent 93b4218 commit 521d6e7

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

include/secp256k1_silentpayments.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SECP256K1_SILENTPAYMENTS_H
33

44
#include "secp256k1.h"
5+
#include "secp256k1_extrakeys.h"
56

67
#ifdef __cplusplus
78
extern "C" {
@@ -24,6 +25,21 @@ extern "C" {
2425
* implementation without requiring any further elliptic-curve operations from the wallet.
2526
*/
2627

28+
/* This struct serves as an In param for passing the silent payment address data.
29+
* The index field is for when more than one address is being sent to in a transaction. Index is
30+
* set based on the original ordering of the addresses and used to return the generated outputs
31+
* matching the original ordering. When more than one recipient is used the recipient array
32+
* will be sorted in place as part of generating the outputs, but the generated outputs will be
33+
* outputs will be returned original ordering specified by the index to ensure the caller is able
34+
* to match up the generated outputs to the correct silent payment address (e.g. to be able to
35+
* assign the correct amounts to the correct generatetd outputs in the final transaction).
36+
*/
37+
typedef struct {
38+
secp256k1_pubkey scan_pubkey;
39+
secp256k1_pubkey spend_pubkey;
40+
size_t index;
41+
} secp256k1_silentpayments_recipient;
42+
2743
#ifdef __cplusplus
2844
}
2945
#endif

src/modules/silentpayments/main_impl.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,36 @@
99
#include "../../../include/secp256k1.h"
1010
#include "../../../include/secp256k1_silentpayments.h"
1111

12-
/* TODO: implement functions for sender side. */
12+
/** Sort an array of silent payment recipients. This is used to group recipients by scan pubkey to
13+
* ensure the correct values of k are used when creating multiple outputs for a recipient. */
14+
static int secp256k1_silentpayments_recipient_sort_cmp(const void* pk1, const void* pk2, void *cmp_data) {
15+
return secp256k1_ec_pubkey_cmp(
16+
((secp256k1_ec_pubkey_sort_cmp_data*)cmp_data)->ctx,
17+
&(*(const secp256k1_silentpayments_recipient **)pk1)->scan_pubkey,
18+
&(*(const secp256k1_silentpayments_recipient **)pk2)->scan_pubkey
19+
);
20+
}
1321

14-
/* TODO: implement functions for receiver side. */
22+
int secp256k1_silentpayments_recipient_sort(const secp256k1_context* ctx, const secp256k1_silentpayments_recipient **recipients, size_t n_recipients) {
23+
secp256k1_ec_pubkey_sort_cmp_data cmp_data;
24+
VERIFY_CHECK(ctx != NULL);
25+
ARG_CHECK(recipients != NULL);
26+
27+
cmp_data.ctx = ctx;
28+
29+
/* Suppress wrong warning (fixed in MSVC 19.33) */
30+
#if defined(_MSC_VER) && (_MSC_VER < 1933)
31+
#pragma warning(push)
32+
#pragma warning(disable: 4090)
33+
#endif
34+
35+
secp256k1_hsort(recipients, n_recipients, sizeof(*recipients), secp256k1_silentpayments_recipient_sort_cmp, &cmp_data);
36+
37+
#if defined(_MSC_VER) && (_MSC_VER < 1933)
38+
#pragma warning(pop)
39+
#endif
40+
41+
return 1;
42+
}
1543

1644
#endif

0 commit comments

Comments
 (0)