Skip to content

Commit 8d4be74

Browse files
committed
Merge branch 'bip86-pub'
2 parents a90bc37 + c470de8 commit 8d4be74

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

src/keystore.c

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,46 @@ static void _tagged_hash(const char* tag, const uint8_t* msg, size_t msg_len, ui
795795
rust_sha256_finish(&hash_ctx, hash_out);
796796
}
797797

798+
bool keystore_secp256k1_schnorr_bip86_pubkey(
799+
const uint32_t* keypath,
800+
size_t keypath_len,
801+
uint8_t* pubkey_out)
802+
{
803+
if (keystore_is_locked()) {
804+
return false;
805+
}
806+
struct ext_key xpub __attribute__((__cleanup__(keystore_zero_xkey))) = {0};
807+
if (!keystore_get_xpub(keypath, keypath_len, &xpub)) {
808+
return false;
809+
}
810+
811+
const secp256k1_context* ctx = wally_get_secp_context();
812+
813+
secp256k1_pubkey pubkey = {0};
814+
if (!secp256k1_ec_pubkey_parse(ctx, &pubkey, xpub.pub_key, sizeof(xpub.pub_key))) {
815+
return false;
816+
}
817+
secp256k1_xonly_pubkey xonly_pubkey = {0};
818+
if (!secp256k1_xonly_pubkey_from_pubkey(ctx, &xonly_pubkey, NULL, &pubkey)) {
819+
return false;
820+
}
821+
uint8_t xonly_pubkey_serialized[32] = {0};
822+
if (!secp256k1_xonly_pubkey_serialize(ctx, xonly_pubkey_serialized, &xonly_pubkey)) {
823+
return false;
824+
}
825+
uint8_t hash[32] = {0};
826+
secp256k1_pubkey tweaked_pubkey = {0};
827+
_tagged_hash("TapTweak", xonly_pubkey_serialized, sizeof(xonly_pubkey_serialized), hash);
828+
if (!secp256k1_xonly_pubkey_tweak_add(ctx, &tweaked_pubkey, &xonly_pubkey, hash)) {
829+
return false;
830+
}
831+
secp256k1_xonly_pubkey tweaked_xonly_pubkey = {0};
832+
if (!secp256k1_xonly_pubkey_from_pubkey(ctx, &tweaked_xonly_pubkey, NULL, &tweaked_pubkey)) {
833+
return false;
834+
}
835+
return secp256k1_xonly_pubkey_serialize(ctx, pubkey_out, &tweaked_xonly_pubkey) == 1;
836+
}
837+
798838
static bool _schnorr_bip86_keypair(
799839
const uint32_t* keypath,
800840
size_t keypath_len,
@@ -834,20 +874,6 @@ static void _cleanup_keypair(secp256k1_keypair* keypair)
834874
util_zero(keypair, sizeof(secp256k1_keypair));
835875
}
836876

837-
bool keystore_secp256k1_schnorr_bip86_pubkey(
838-
const uint32_t* keypath,
839-
size_t keypath_len,
840-
uint8_t* pubkey_out)
841-
{
842-
secp256k1_keypair __attribute__((__cleanup__(_cleanup_keypair))) keypair = {0};
843-
secp256k1_xonly_pubkey pubkey = {0};
844-
if (!_schnorr_bip86_keypair(keypath, keypath_len, &keypair, &pubkey)) {
845-
return false;
846-
}
847-
const secp256k1_context* ctx = wally_get_secp_context();
848-
return secp256k1_xonly_pubkey_serialize(ctx, pubkey_out, &pubkey) == 1;
849-
}
850-
851877
bool keystore_secp256k1_schnorr_bip86_sign(
852878
const uint32_t* keypath,
853879
size_t keypath_len,

0 commit comments

Comments
 (0)