Skip to content

Commit fc96aa7

Browse files
committed
Add a function to extract the secretkey from a keypair
1 parent 9e5939d commit fc96aa7

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

include/secp256k1_extrakeys.h

+13
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,19 @@ SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(
165165
const unsigned char *seckey
166166
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
167167

168+
/** Get the secret key from a keypair.
169+
*
170+
* Returns: 0 if the arguments are invalid. 1 otherwise.
171+
* Args: ctx: pointer to a context object (cannot be NULL)
172+
* Out: seckey: pointer to a 32-byte buffer for the secret key (cannot be NULL)
173+
* In: keypair: pointer to a keypair (cannot be NULL)
174+
*/
175+
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_sec(
176+
const secp256k1_context* ctx,
177+
unsigned char *seckey,
178+
const secp256k1_keypair *keypair
179+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3);
180+
168181
/** Get the public key from a keypair.
169182
*
170183
* Returns: 0 if the arguments are invalid. 1 otherwise.

src/modules/extrakeys/main_impl.h

+10
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,16 @@ int secp256k1_keypair_create(const secp256k1_context* ctx, secp256k1_keypair *ke
186186
return ret;
187187
}
188188

189+
int secp256k1_keypair_sec(const secp256k1_context* ctx, unsigned char *seckey, const secp256k1_keypair *keypair) {
190+
VERIFY_CHECK(ctx != NULL);
191+
ARG_CHECK(seckey != NULL);
192+
memset(seckey, 0, 32);
193+
ARG_CHECK(keypair != NULL);
194+
195+
memcpy(seckey, &keypair->data[0], 32);
196+
return 1;
197+
}
198+
189199
int secp256k1_keypair_pub(const secp256k1_context* ctx, secp256k1_pubkey *pubkey, const secp256k1_keypair *keypair) {
190200
VERIFY_CHECK(ctx != NULL);
191201
ARG_CHECK(pubkey != NULL);

0 commit comments

Comments
 (0)