bool CECKey::SignCompact(const uint256 &hash, unsigned char *p64, int &rec) { bool fOk = false; ECDSA_SIG *sig = ECDSA_do_sign((unsigned char*)&hash, sizeof(hash), pkey); if (sig==NULL) return false; memset(p64, 0, 64); int nBitsR = BN_num_bits(sig->r); int nBitsS = BN_num_bits(sig->s); if (nBitsR <= 256 && nBitsS <= 256) { std::vector<unsigned char> pubkey; GetPubKey(pubkey, true); for (int i=0; i<4; i++) { CECKey keyRec; if (ECDSA_SIG_recover_key_GFp(keyRec.pkey, sig, (unsigned char*)&hash, sizeof(hash), i, 1) == 1) { std::vector<unsigned char> pubkeyRec; keyRec.GetPubKey(pubkeyRec, true); if (pubkeyRec == pubkey) { rec = i; fOk = true; break; } } } assert(fOk); BN_bn2bin(sig->r,&p64[32-(nBitsR+7)/8]); BN_bn2bin(sig->s,&p64[64-(nBitsS+7)/8]); } ECDSA_SIG_free(sig); return fOk; }
bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const { if (!fValid) return false; CECKey key; key.SetSecretBytes(vch); return key.Sign(hash, vchSig); }
bool CPubKey::IsFullyValid() const { if (!IsValid()) return false; CECKey key; if (!key.SetPubKey(*this)) return false; return true; }
CPubKey CKey::GetPubKey() const { assert(fValid); CECKey key; key.SetSecretBytes(vch); CPubKey pubkey; key.GetPubKey(pubkey, fCompressed); return pubkey; }
CPrivKey CKey::GetPrivKey() const { assert(fValid); CECKey key; key.SetSecretBytes(vch); CPrivKey privkey; key.GetPrivKey(privkey, fCompressed); return privkey; }
bool CKey::Decrypt(ecies_secure_t const &cryptex, std::string &vchText ) { if (!fValid) return false; CECKey key; key.SetSecretBytes(vch); return key.Decrypt(cryptex, vchText); }
bool CPubKey::Decompress() { if (!IsValid()) return false; CECKey key; if (!key.SetPubKey(*this)) return false; key.GetPubKey(*this, false); return true; }
bool CPubKey::RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) { if (vchSig.size() != 65) return false; CECKey key; if (!key.Recover(hash, &vchSig[1], (vchSig[0] - 27) & ~4)) return false; key.GetPubKey(*this, (vchSig[0] - 27) & 4); return true; }
bool CKey::SetPrivKey(const CPrivKey &privkey, bool fCompressedIn) { CECKey key; if (!key.SetPrivKey(privkey)) return false; key.GetSecretBytes(vch); fCompressed = fCompressedIn; fValid = true; return true; }
bool CPubKey::Encrypt(std::string const &vchText, ecies_secure_t &cryptex) { if (!IsValid()) return false; CECKey key; if (!key.SetPubKey(*this)) return false; return key.Encrypt(vchText, cryptex); }
bool CPubKey::Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const { if (!IsValid()) return false; CECKey key; if (!key.SetPubKey(*this)) return false; if (!key.Verify(hash, vchSig)) return false; return true; }
bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const { if (!fValid) return false; CECKey key; key.SetSecretBytes(vch); vchSig.resize(65); int rec = -1; if (!key.SignCompact(hash, &vchSig[1], rec)) return false; assert(rec != -1); vchSig[0] = 27 + rec + (fCompressed ? 4 : 0); return true; }
bool CPubKey::VerifyCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) const { if (!IsValid()) return false; if (vchSig.size() != 65) return false; CECKey key; if (!key.Recover(hash, &vchSig[1], (vchSig[0] - 27) & ~4)) return false; CPubKey pubkeyRec; key.GetPubKey(pubkeyRec, IsCompressed()); if (*this != pubkeyRec) return false; return true; }