Skip to content

Commit ac97828

Browse files
committed
silentpayments: add sortable recipient struct
1 parent 31187da commit ac97828

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

include/secp256k1_silentpayments.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ extern "C" {
2424
* implementation without requiring any further elliptic-curve operations from the wallet.
2525
*/
2626

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

src/modules/silentpayments/main_impl.h

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,32 @@
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 *ctx) {
15+
return secp256k1_ec_pubkey_cmp((secp256k1_context *)ctx,
16+
&(*(const secp256k1_silentpayments_recipient **)pk1)->scan_pubkey,
17+
&(*(const secp256k1_silentpayments_recipient **)pk2)->scan_pubkey
18+
);
19+
}
1320

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

1640
#endif

0 commit comments

Comments
 (0)