C(Pub)Key::Derive: Return optional tweak vector for peg-out wallets

This commit is contained in:
Gregory Sanders 2018-12-17 13:33:39 -05:00
parent f1dbd1cf3d
commit 1559cc9c3b
4 changed files with 12 additions and 4 deletions

View file

@ -265,7 +265,7 @@ bool CKey::Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipChe
return VerifyPubKey(vchPubKey);
}
bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc, std::vector<unsigned char>* tweak) const {
assert(IsValid());
assert(IsCompressed());
std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
@ -277,6 +277,10 @@ bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const
assert(size() == 32);
BIP32Hash(cc, nChild, 0, begin(), vout.data());
}
if (tweak) {
tweak->clear();
*tweak = std::vector<unsigned char>(vout.data(), vout.data()+32);
}
memcpy(ccChild.begin(), vout.data()+32, 32);
memcpy((unsigned char*)keyChild.begin(), begin(), 32);
bool ret = secp256k1_ec_privkey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), vout.data());

View file

@ -126,7 +126,7 @@ public:
bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
//! Derive BIP32 child key.
bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc, std::vector<unsigned char>* tweak = nullptr /* ELEMENTS: vector of key tweak values that are filled out if non-null */) const;
/**
* Verify thoroughly whether a private key and a public key match.

View file

@ -224,13 +224,17 @@ bool CPubKey::Decompress() {
return true;
}
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
bool CPubKey::Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc, std::vector<unsigned char>* tweak) const {
assert(IsValid());
assert((nChild >> 31) == 0);
assert(size() == COMPRESSED_PUBLIC_KEY_SIZE);
unsigned char out[64];
BIP32Hash(cc, nChild, *begin(), begin()+1, out);
memcpy(ccChild.begin(), out+32, 32);
if (tweak) {
tweak->clear();
*tweak = std::vector<unsigned char>(out, out+32);
}
secp256k1_pubkey pubkey;
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, size())) {
return false;

View file

@ -200,7 +200,7 @@ public:
bool Decompress();
//! Derive BIP32 child pubkey.
bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc, std::vector<unsigned char>* tweak = nullptr /* ELEMENTS: vector of key tweak values that are filled out if non-null */) const;
};
struct CExtPubKey {