Changeset View
Changeset View
Standalone View
Standalone View
src/key.cpp
Show First 20 Lines • Show All 157 Lines • ▼ Show 20 Lines | |||||
CPrivKey CKey::GetPrivKey() const { | CPrivKey CKey::GetPrivKey() const { | ||||
assert(fValid); | assert(fValid); | ||||
CPrivKey privkey; | CPrivKey privkey; | ||||
int ret; | int ret; | ||||
size_t privkeylen; | size_t privkeylen; | ||||
privkey.resize(279); | privkey.resize(279); | ||||
privkeylen = 279; | privkeylen = 279; | ||||
ret = ec_privkey_export_der( | ret = ec_privkey_export_der( | ||||
secp256k1_context_sign, (uint8_t *)&privkey[0], &privkeylen, begin(), | secp256k1_context_sign, (uint8_t *)privkey.data(), &privkeylen, begin(), | ||||
fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); | fCompressed ? SECP256K1_EC_COMPRESSED : SECP256K1_EC_UNCOMPRESSED); | ||||
assert(ret); | assert(ret); | ||||
privkey.resize(privkeylen); | privkey.resize(privkeylen); | ||||
return privkey; | return privkey; | ||||
} | } | ||||
CPubKey CKey::GetPubKey() const { | CPubKey CKey::GetPubKey() const { | ||||
assert(fValid); | assert(fValid); | ||||
Show All 21 Lines | bool CKey::SignECDSA(const uint256 &hash, std::vector<uint8_t> &vchSig, | ||||
uint8_t extra_entropy[32] = {0}; | uint8_t extra_entropy[32] = {0}; | ||||
WriteLE32(extra_entropy, test_case); | WriteLE32(extra_entropy, test_case); | ||||
secp256k1_ecdsa_signature sig; | secp256k1_ecdsa_signature sig; | ||||
int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), | int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), | ||||
begin(), secp256k1_nonce_function_rfc6979, | begin(), secp256k1_nonce_function_rfc6979, | ||||
test_case ? extra_entropy : nullptr); | test_case ? extra_entropy : nullptr); | ||||
assert(ret); | assert(ret); | ||||
secp256k1_ecdsa_signature_serialize_der( | secp256k1_ecdsa_signature_serialize_der( | ||||
secp256k1_context_sign, (uint8_t *)&vchSig[0], &nSigLen, &sig); | secp256k1_context_sign, (uint8_t *)vchSig.data(), &nSigLen, &sig); | ||||
vchSig.resize(nSigLen); | vchSig.resize(nSigLen); | ||||
return true; | return true; | ||||
} | } | ||||
bool CKey::SignSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig, | bool CKey::SignSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig, | ||||
uint32_t test_case) const { | uint32_t test_case) const { | ||||
if (!fValid) { | if (!fValid) { | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | bool CKey::SignCompact(const uint256 &hash, | ||||
assert(rec != -1); | assert(rec != -1); | ||||
vchSig[0] = 27 + rec + (fCompressed ? 4 : 0); | vchSig[0] = 27 + rec + (fCompressed ? 4 : 0); | ||||
return true; | return true; | ||||
} | } | ||||
bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, | bool CKey::Load(CPrivKey &privkey, CPubKey &vchPubKey, | ||||
bool fSkipCheck = false) { | bool fSkipCheck = false) { | ||||
if (!ec_privkey_import_der(secp256k1_context_sign, (uint8_t *)begin(), | if (!ec_privkey_import_der(secp256k1_context_sign, (uint8_t *)begin(), | ||||
&privkey[0], privkey.size())) | privkey.data(), privkey.size())) | ||||
return false; | return false; | ||||
fCompressed = vchPubKey.IsCompressed(); | fCompressed = vchPubKey.IsCompressed(); | ||||
fValid = true; | fValid = true; | ||||
if (fSkipCheck) { | if (fSkipCheck) { | ||||
return true; | return true; | ||||
} | } | ||||
Show All 32 Lines | |||||
void CExtKey::SetMaster(const uint8_t *seed, unsigned int nSeedLen) { | void CExtKey::SetMaster(const uint8_t *seed, unsigned int nSeedLen) { | ||||
static const uint8_t hashkey[] = {'B', 'i', 't', 'c', 'o', 'i', | static const uint8_t hashkey[] = {'B', 'i', 't', 'c', 'o', 'i', | ||||
'n', ' ', 's', 'e', 'e', 'd'}; | 'n', ' ', 's', 'e', 'e', 'd'}; | ||||
std::vector<uint8_t, secure_allocator<uint8_t>> vout(64); | std::vector<uint8_t, secure_allocator<uint8_t>> vout(64); | ||||
CHMAC_SHA512(hashkey, sizeof(hashkey)) | CHMAC_SHA512(hashkey, sizeof(hashkey)) | ||||
.Write(seed, nSeedLen) | .Write(seed, nSeedLen) | ||||
.Finalize(vout.data()); | .Finalize(vout.data()); | ||||
key.Set(&vout[0], &vout[32], true); | key.Set(vout.data(), vout.data() + 32, true); | ||||
memcpy(chaincode.begin(), &vout[32], 32); | memcpy(chaincode.begin(), vout.data() + 32, 32); | ||||
nDepth = 0; | nDepth = 0; | ||||
nChild = 0; | nChild = 0; | ||||
memset(vchFingerprint, 0, sizeof(vchFingerprint)); | memset(vchFingerprint, 0, sizeof(vchFingerprint)); | ||||
} | } | ||||
CExtPubKey CExtKey::Neuter() const { | CExtPubKey CExtKey::Neuter() const { | ||||
CExtPubKey ret; | CExtPubKey ret; | ||||
ret.nDepth = nDepth; | ret.nDepth = nDepth; | ||||
▲ Show 20 Lines • Show All 60 Lines • Show Last 20 Lines |