Skip to content

Commit 68a244f

Browse files
committed
silentpayments: add routine for creating labelled spend pubkeys (for addresses)
1 parent 8ba10b7 commit 68a244f

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

include/secp256k1_silentpayments.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,29 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_l
167167
unsigned int m
168168
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
169169

170+
/** Create Silent Payment labelled spend public key.
171+
*
172+
* Given a recipient's spend public key B_spend and a label_tweak, calculate
173+
* the corresponding serialized labelled spend public key:
174+
*
175+
* B_m = B_spend + label_tweak * G
176+
*
177+
* The result is used by the receiver to create a Silent Payment address, consisting
178+
* of the serialized and concatenated scan public key and (labelled) spend public key each.
179+
*
180+
* Returns: 1 if labelled spend public key creation was successful. 0 if an error occured.
181+
* Args: ctx: pointer to a context object
182+
* Out: l_addr_spend_pubkey33: pointer to the resulting labelled spend public key
183+
* In: receiver_spend_pubkey: pointer to the receiver's scan pubkey
184+
* label_tweak: pointer to the the receiver's spend
185+
*/
186+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_silentpayments_create_address_spend_pubkey(
187+
const secp256k1_context *ctx,
188+
unsigned char *l_addr_spend_pubkey33,
189+
const secp256k1_pubkey *receiver_spend_pubkey,
190+
const unsigned char *label_tweak32
191+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
192+
170193
#ifdef __cplusplus
171194
}
172195
#endif

src/modules/silentpayments/main_impl.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,29 @@ int secp256k1_silentpayments_create_label_tweak(const secp256k1_context *ctx, un
238238
return 1;
239239
}
240240

241+
int secp256k1_silentpayments_create_address_spend_pubkey(const secp256k1_context *ctx, unsigned char *l_addr_spend_pubkey33, const secp256k1_pubkey *receiver_spend_pubkey, const unsigned char *label_tweak32) {
242+
secp256k1_ge B_m;
243+
size_t ser_size;
244+
int ser_ret;
245+
246+
/* Sanity check inputs. */
247+
VERIFY_CHECK(ctx != NULL);
248+
VERIFY_CHECK(l_addr_spend_pubkey33 != NULL);
249+
VERIFY_CHECK(receiver_spend_pubkey != NULL);
250+
VERIFY_CHECK(label_tweak32 != NULL);
251+
252+
/* Calculate B_m = B_spend + label_tweak * G */
253+
secp256k1_pubkey_load(ctx, &B_m, receiver_spend_pubkey);
254+
if (!secp256k1_ec_pubkey_tweak_add_helper(&B_m, label_tweak32)) {
255+
return 0;
256+
}
257+
258+
/* Serialize B_m */
259+
ser_ret = secp256k1_eckey_pubkey_serialize(&B_m, l_addr_spend_pubkey33, &ser_size, 1);
260+
VERIFY_CHECK(ser_ret && ser_size == 33);
261+
(void)ser_ret;
262+
263+
return 1;
264+
}
265+
241266
#endif

0 commit comments

Comments
 (0)