fix keystore Crypto field to smaller letter starting crypto

crypto_fix
Dennis Won 5 years ago
parent f5c958460e
commit 09cdf36fe2
  1. 46
      examples/import_keystore.js
  2. 14
      packages/harmony-crypto/src/keystore.ts
  3. 2
      packages/harmony-crypto/src/types.ts

@ -0,0 +1,46 @@
// import the Account class
const { Account } = require('@harmony-js/account');
// suppose we have an account
const myPrivateKey = '0x831f0b3ff835d5ec4602878742fda25591b6d3bb2731366621ac83a05216bec8';
const myAccountWithMyPrivateKey = new Account(myPrivateKey);
// suppose we have a password, and we want to encrypt the account above
const myStrongPassword = '123';
async function encryptAndDecrypt(password) {
// we get the privateKey before encrypted as comparison
const unencryptedPrivateKey = myAccountWithMyPrivateKey.privateKey;
// export the account to keyStore string, which will make the privateKey encrpyted
const keyStoreFile = await myAccountWithMyPrivateKey.toFile(password);
// exported keyStoreFile
console.log({ keyStoreFile });
// see if the account is encrypted
console.log(`Is this account encrypted? \n ${myAccountWithMyPrivateKey.encrypted}`);
// keystore file should be equal to encrypted privateKey
console.log(
`Is privateKey equal to keyStore string? \n ${keyStoreFile ===
myAccountWithMyPrivateKey.privateKey}`,
);
}
// encryptAndDecrypt(myStrongPassword)
// suppose we have keyStorefile, in this example, we just use same password and keystore string encrypted above
const someKeyStoreFile =
'{"address":"1cc87010760602a576455d6d2f03a3bf92d2c2ca","crypto":{"cipher":"aes-128-ctr","ciphertext":"8bc4575478cae488a0f6d1b33ac72e61e0a04ce650aa7d03de26cfe6f6b0dce1","cipherparams":{"iv":"3bf838a3c67a4cbd6e02158f1ccd49d7"},"kdf":"scrypt","kdfparams":{"dklen":32,"n":262144,"p":1,"r":8,"salt":"c8c5374b1ce08b25b7d5733f8d072396af6f93f4bf65c06efd29a7155c62883c"},"mac":"a9ad452817814e71a2af5571add57ee0d944d7ab63bf3a5cfaf66af53bb8b9b6"},"id":"ab8e578b-9431-47f6-9937-6f1f18e8f34f","version":3}';
//'{"version":3,"id":"62326332-3139-4839-b534-656134623066","address":"1fe3da351d9fc0c4f02de5412ad7def8aee956c5","crypto":{"ciphertext":"b86ab81682c9f5a35738ad9bd38cd9b46c1b852ef33f16dd83068f79e20d5531","cipherparams":{"iv":"44efb5a514f34968e92cafad80566487"},"cipher":"aes-128-ctr","kdf":"scrypt","kdfparams":{"salt":"d70ae1f311601562113f98c8ebe382f52a332dca1588886e5ea91e2f8a647134","n":8192,"r":8,"p":1,"dklen":32},"mac":"7b63e4e31a75a22b7091291bb58302655b738539ef3e30b30a7a7f170f6163ef"}}'
async function importKeyStoreFileAndDecrypt(keyStoreFile, password) {
// import keyStore string and provide the password, remember to make a new Account first
const importedAccount = await Account.new().fromFile(keyStoreFile, password);
// the account should decypted which `Account.encrypted` is false
console.log(`Is this account encrypted? \n ${importedAccount.encrypted}`);
// see if the privatekey is equal to unencrypted one?
console.log(
`Is the account recovered from keystore? \n ${importedAccount.privateKey === myPrivateKey}`,
);
}
importKeyStoreFileAndDecrypt(someKeyStoreFile, myStrongPassword);

@ -96,7 +96,7 @@ export const encrypt = async (
version: 3,
id: uuid.v4({ random: uuidRandom || hexToIntArray(randomBytes(16)) }),
address: address.toLowerCase().replace('0x', ''),
Crypto: {
crypto: {
ciphertext: ciphertext.toString('hex'),
cipherparams: {
iv: iv.toString('hex'),
@ -116,15 +116,15 @@ export const encrypt = async (
* @return {string} privateKey
*/
export const decrypt = async (keystore: Keystore, password: string): Promise<string> => {
const ciphertext = Buffer.from(keystore.Crypto.ciphertext, 'hex');
const iv = Buffer.from(keystore.Crypto.cipherparams.iv, 'hex');
const { kdfparams } = keystore.Crypto;
const ciphertext = Buffer.from(keystore.crypto.ciphertext, 'hex');
const iv = Buffer.from(keystore.crypto.cipherparams.iv, 'hex');
const { kdfparams } = keystore.crypto;
const derivedKey = await getDerivedKey(Buffer.from(password), keystore.Crypto.kdf, kdfparams);
const derivedKey = await getDerivedKey(Buffer.from(password), keystore.crypto.kdf, kdfparams);
const mac = keccak256(concat([derivedKey.slice(16, 32), ciphertext])).replace('0x', '');
if (mac.toUpperCase() !== keystore.Crypto.mac.toUpperCase()) {
if (mac.toUpperCase() !== keystore.crypto.mac.toUpperCase()) {
return Promise.reject(new Error('Failed to decrypt.'));
}
@ -172,7 +172,7 @@ export const encryptPhrase = async (
return JSON.stringify({
version: 3,
id: uuid.v4({ random: uuidRandom || hexToIntArray(randomBytes(16)) }),
Crypto: {
crypto: {
ciphertext: ciphertext.toString('hex'),
cipherparams: {
iv: iv.toString('hex'),

@ -24,7 +24,7 @@ export interface EncryptOptions {
export interface Keystore {
address?: string;
Crypto: {
crypto: {
cipher: string;
cipherparams: {
iv: string;

Loading…
Cancel
Save