17 static bool Base58ToHex(
const std::string& base58_str, std::string& hex_str)
21 size_t key_size = 39 + 4;
22 std::vector<unsigned char> vchKey;
23 if (!
DecodeBase58(base58_str.c_str(), vchKey, key_size) || vchKey.size() != key_size) {
29 for (
unsigned int i = 0; i < vchKey.size(); i++) {
30 const unsigned char* c = vchKey.data() + i;
31 ss << std::setw(2) << std::setfill(
'0') << (int)*c;
56 scrypt_hash(strPassphrase.c_str(), strPassphrase.size(),
BEGIN(s), strSalt.size() / 2,
BEGIN(prefactor), 16384, 8, 8, 32);
70 assert(ctx !=
nullptr);
73 std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
89 if (passpoint.
size() != clen)
114 uint256 addrCheck =
Hash(address.begin(), address.end());
116 return HexStr(addrCheck).substr(0, 8);
119 std::string
BIP38_Encrypt(std::string strAddress, std::string strPassphrase,
uint256 privKey,
bool fCompressed)
125 scrypt_hash(strPassphrase.c_str(), strPassphrase.size(),
BEGIN(salt), strAddressHash.size() / 2,
BEGIN(hashed), 16384, 8, 8, 64);
147 std::string strPrefix =
"0142";
148 strPrefix += (fCompressed ?
"E0" :
"C0");
153 encryptedKey = encryptedKey | (encrypted1 << 56);
156 encryptedKey = encryptedKey | (encrypted2 << (56 + 128));
163 encryptedKey = encryptedKey | (b58Checksum << 312);
169 bool BIP38_Decrypt(std::string strPassphrase, std::string strEncryptedKey,
uint256& privKey,
bool& fCompressed)
172 if (!Base58ToHex(strEncryptedKey, strKey)) {
183 std::string strAddressHash = strKey.substr(6, 8);
184 std::string ownersalt = strKey.substr(14, 16);
195 scrypt_hash(strPassphrase.c_str(), strPassphrase.size(),
BEGIN(salt), strAddressHash.size() / 2,
BEGIN(hashed), 16384, 8, 8, 64);
201 DecryptAES(encryptedPart1, derivedHalf2, decryptedPart1);
204 DecryptAES(encryptedPart2, derivedHalf2, decryptedPart2);
219 bool fLotSequence = (flag & 0x04) != 0;
221 std::string prefactorSalt = ownersalt;
223 prefactorSalt = ownersalt.substr(0, 8);
232 passfactor = prefactor;
247 DecryptAES(encryptedPart2, derivedHalf2, decryptedPart2);
271 assert(ctx !=
nullptr);
274 std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
288 k.
Set(privKey.
begin(), privKey.
end(), fCompressed);
arith_uint256 UintToArith256(const uint256 &a)
uint256 ArithToUint256(const arith_uint256 &a)
const arith_uint256 ARITH_UINT256_ZERO
std::string EncodeBase58(const unsigned char *pbegin, const unsigned char *pend)
Why base-58 instead of standard base-64 encoding?
bool DecodeBase58(const char *psz, std::vector< unsigned char > &vch, int max_ret_len)
Decode a base58-encoded string (psz) into a byte vector (vchRet).
void ComputePreFactor(std::string strPassphrase, std::string strSalt, uint256 &prefactor)
void ComputeSeedBPass(CPubKey passpoint, std::string strAddressHash, std::string strOwnerSalt, uint512 &seedBPass)
std::string AddressToBip38Hash(const std::string &address)
bool BIP38_Decrypt(std::string strPassphrase, std::string strEncryptedKey, uint256 &privKey, bool &fCompressed)
std::string BIP38_Encrypt(std::string strAddress, std::string strPassphrase, uint256 privKey, bool fCompressed)
void ComputeFactorB(uint256 seedB, uint256 &factorB)
bool ComputePasspoint(uint256 passfactor, CPubKey &passpoint)
void ComputePassfactor(std::string ownersalt, uint256 prefactor, uint256 &passfactor)
void DecryptAES(uint256 encryptedIn, uint256 decryptionKey, uint256 &output)
39 bytes - 78 characters 1) Prefix - 2 bytes - 4 chars - strKey[0..3] 2) Flagbyte - 1 byte - 2 chars ...
A decryption class for AES-256.
void Decrypt(unsigned char plaintext[16], const unsigned char ciphertext[16]) const
An encryption class for AES-256.
void Encrypt(unsigned char ciphertext[16], const unsigned char plaintext[16]) const
An encapsulated private key.
CPubKey GetPubKey() const
Compute the public key from a private key.
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
An encapsulated public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
const unsigned char * begin() const
256-bit unsigned big integer.
512-bit unsigned big integer.
std::string ToString() const
uint64_t GetCheapHash() const
A cheap hash function that just returns 64 bits from the result, it can be used when the contents are...
void scrypt_hash(const char *pass, unsigned int pLen, const char *salt, unsigned int sLen, char *output, unsigned int N, unsigned int r, unsigned int p, unsigned int dkLen)
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
std::string EncodeDestination(const CWDestination &address, const CChainParams::Base58Type addrType)
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
#define SECP256K1_CONTEXT_SIGN
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object.
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize and secp256k1_ec_privkey_export.
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_privkey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a private key by multiplying it by a tweak.
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object.
Opaque data structure that holds a parsed and valid public key.
uint256 uint256S(const char *str)
uint512 uint512S(const char *str)
const uint256 UINT256_ONE
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
#define BEGIN(a)
Utilities for converting data from/to strings.
std::string ReverseEndianString(std::string in)
Reverse the endianness of a string.