Changeset View
Changeset View
Standalone View
Standalone View
src/pubkey.cpp
Show First 20 Lines • Show All 227 Lines • ▼ Show 20 Lines | bool CPubKey::RecoverCompact(const uint256 &hash, | ||||
if (!secp256k1_ecdsa_recoverable_signature_parse_compact( | if (!secp256k1_ecdsa_recoverable_signature_parse_compact( | ||||
secp256k1_context_verify, &sig, &vchSig[1], recid)) { | secp256k1_context_verify, &sig, &vchSig[1], recid)) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, | if (!secp256k1_ecdsa_recover(secp256k1_context_verify, &pubkey, &sig, | ||||
hash.begin())) { | hash.begin())) { | ||||
return false; | return false; | ||||
} | } | ||||
uint8_t pub[PUBLIC_KEY_SIZE]; | uint8_t pub[SIZE]; | ||||
size_t publen = PUBLIC_KEY_SIZE; | size_t publen = SIZE; | ||||
secp256k1_ec_pubkey_serialize( | secp256k1_ec_pubkey_serialize( | ||||
secp256k1_context_verify, pub, &publen, &pubkey, | secp256k1_context_verify, pub, &publen, &pubkey, | ||||
fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); | fComp ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); | ||||
Set(pub, pub + publen); | Set(pub, pub + publen); | ||||
return true; | return true; | ||||
} | } | ||||
bool CPubKey::IsFullyValid() const { | bool CPubKey::IsFullyValid() const { | ||||
Show All 9 Lines | bool CPubKey::Decompress() { | ||||
if (!IsValid()) { | if (!IsValid()) { | ||||
return false; | return false; | ||||
} | } | ||||
secp256k1_pubkey pubkey; | secp256k1_pubkey pubkey; | ||||
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, | if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, | ||||
size())) { | size())) { | ||||
return false; | return false; | ||||
} | } | ||||
uint8_t pub[PUBLIC_KEY_SIZE]; | uint8_t pub[SIZE]; | ||||
size_t publen = PUBLIC_KEY_SIZE; | size_t publen = SIZE; | ||||
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, | secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, | ||||
&pubkey, SECP256K1_EC_UNCOMPRESSED); | &pubkey, SECP256K1_EC_UNCOMPRESSED); | ||||
Set(pub, pub + publen); | Set(pub, pub + publen); | ||||
return true; | return true; | ||||
} | } | ||||
bool CPubKey::Derive(CPubKey &pubkeyChild, ChainCode &ccChild, | bool CPubKey::Derive(CPubKey &pubkeyChild, ChainCode &ccChild, | ||||
unsigned int nChild, const ChainCode &cc) const { | unsigned int nChild, const ChainCode &cc) const { | ||||
assert(IsValid()); | assert(IsValid()); | ||||
assert((nChild >> 31) == 0); | assert((nChild >> 31) == 0); | ||||
assert(size() == COMPRESSED_PUBLIC_KEY_SIZE); | assert(size() == COMPRESSED_SIZE); | ||||
uint8_t out[64]; | uint8_t out[64]; | ||||
BIP32Hash(cc, nChild, *begin(), begin() + 1, out); | BIP32Hash(cc, nChild, *begin(), begin() + 1, out); | ||||
memcpy(ccChild.begin(), out + 32, 32); | memcpy(ccChild.begin(), out + 32, 32); | ||||
secp256k1_pubkey pubkey; | secp256k1_pubkey pubkey; | ||||
if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, | if (!secp256k1_ec_pubkey_parse(secp256k1_context_verify, &pubkey, vch, | ||||
size())) { | size())) { | ||||
return false; | return false; | ||||
} | } | ||||
if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, | if (!secp256k1_ec_pubkey_tweak_add(secp256k1_context_verify, &pubkey, | ||||
out)) { | out)) { | ||||
return false; | return false; | ||||
} | } | ||||
uint8_t pub[COMPRESSED_PUBLIC_KEY_SIZE]; | uint8_t pub[COMPRESSED_SIZE]; | ||||
size_t publen = COMPRESSED_PUBLIC_KEY_SIZE; | size_t publen = COMPRESSED_SIZE; | ||||
secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, | secp256k1_ec_pubkey_serialize(secp256k1_context_verify, pub, &publen, | ||||
&pubkey, SECP256K1_EC_COMPRESSED); | &pubkey, SECP256K1_EC_COMPRESSED); | ||||
pubkeyChild.Set(pub, pub + publen); | pubkeyChild.Set(pub, pub + publen); | ||||
return true; | return true; | ||||
} | } | ||||
void CExtPubKey::Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const { | void CExtPubKey::Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const { | ||||
code[0] = nDepth; | code[0] = nDepth; | ||||
memcpy(code + 1, vchFingerprint, 4); | memcpy(code + 1, vchFingerprint, 4); | ||||
code[5] = (nChild >> 24) & 0xFF; | code[5] = (nChild >> 24) & 0xFF; | ||||
code[6] = (nChild >> 16) & 0xFF; | code[6] = (nChild >> 16) & 0xFF; | ||||
code[7] = (nChild >> 8) & 0xFF; | code[7] = (nChild >> 8) & 0xFF; | ||||
code[8] = (nChild >> 0) & 0xFF; | code[8] = (nChild >> 0) & 0xFF; | ||||
memcpy(code + 9, chaincode.begin(), 32); | memcpy(code + 9, chaincode.begin(), 32); | ||||
assert(pubkey.size() == CPubKey::COMPRESSED_PUBLIC_KEY_SIZE); | assert(pubkey.size() == CPubKey::COMPRESSED_SIZE); | ||||
memcpy(code + 41, pubkey.begin(), CPubKey::COMPRESSED_PUBLIC_KEY_SIZE); | memcpy(code + 41, pubkey.begin(), CPubKey::COMPRESSED_SIZE); | ||||
} | } | ||||
void CExtPubKey::Decode(const uint8_t code[BIP32_EXTKEY_SIZE]) { | void CExtPubKey::Decode(const uint8_t code[BIP32_EXTKEY_SIZE]) { | ||||
nDepth = code[0]; | nDepth = code[0]; | ||||
memcpy(vchFingerprint, code + 1, 4); | memcpy(vchFingerprint, code + 1, 4); | ||||
nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8]; | nChild = (code[5] << 24) | (code[6] << 16) | (code[7] << 8) | code[8]; | ||||
memcpy(chaincode.begin(), code + 9, 32); | memcpy(chaincode.begin(), code + 9, 32); | ||||
pubkey.Set(code + 41, code + BIP32_EXTKEY_SIZE); | pubkey.Set(code + 41, code + BIP32_EXTKEY_SIZE); | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |