Skip to content

Commit a39d5e0

Browse files
committed
Add Pay-To-contract support in PubKey
1 parent 5cbe23b commit a39d5e0

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/pubkey.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,20 @@ bool CPubKey::CheckPayToContract(const CPubKey& base, const uint256& hash) const
295295
return memcmp(out, vch, COMPRESSED_PUBLIC_KEY_SIZE) == 0;
296296
}
297297

298+
bool CPubKey::CreatePayToContract(CPubKey& res, const uint256& hash) const
299+
{
300+
if (!IsCompressed()) return false;
301+
secp256k1_pubkey base_point;
302+
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &base_point, vch, size())) return false;
303+
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &base_point, hash.begin())) return false;
304+
305+
size_t tweaked_len = COMPRESSED_PUBLIC_KEY_SIZE;
306+
unsigned char tweaked[tweaked_len];
307+
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, tweaked, &tweaked_len, &base_point, SECP256K1_EC_COMPRESSED);
308+
res.Set(tweaked, tweaked + tweaked_len);
309+
return true;
310+
}
311+
298312
/* static */ bool CPubKey::CheckLowS(const std::vector<unsigned char>& vchSig) {
299313
secp256k1_ecdsa_signature sig;
300314
if (!ecdsa_signature_parse_der_lax(secp256k1_context_verify, &sig, vchSig.data(), vchSig.size())) {

src/pubkey.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ class CPubKey
209209

210210
//! Verify a P2C derivation. Hash must be a cryptographic hash that commits to base.
211211
bool CheckPayToContract(const CPubKey& base, const uint256& hash) const;
212+
213+
bool CreatePayToContract(CPubKey& res, const uint256& hash) const;
212214
};
213215

214216
struct CExtPubKey {

0 commit comments

Comments
 (0)