Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
Merge pull request #405 from jimni1222/decrypt
Browse files Browse the repository at this point in the history
Not to damage the original data, deep copy the keystore object
  • Loading branch information
jimni1222 authored Dec 15, 2020
2 parents fd1b86e + 4e1d8bf commit b362e19
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/caver-klay/caver-klay-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,7 +1393,8 @@ Accounts.prototype.decrypt = function(v3Keystore, password, nonStrict) {
throw new Error('No password given.')
}

const json = _.isObject(v3Keystore) ? v3Keystore : JSON.parse(nonStrict ? v3Keystore.toLowerCase() : v3Keystore)
// To deep copy an object, using JSON.parse and JSON.stringify (object -> string -> object)
const json = _.isObject(v3Keystore) ? _.cloneDeep(v3Keystore) : JSON.parse(nonStrict ? v3Keystore.toLowerCase() : v3Keystore)

if (json.version !== 3 && json.version !== 4) {
console.warn('This is not a V3 or V4 wallet.')
Expand Down
3 changes: 2 additions & 1 deletion packages/caver-wallet/src/keyring/keyringFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ class KeyringFactory {
* @return {AbstractKeyring}
*/
static decrypt(keystore, password) {
const json = _.isObject(keystore) ? keystore : JSON.parse(keystore)
// To deep copy an object, using JSON.parse and JSON.stringify (object -> string -> object)
const json = _.isObject(keystore) ? _.cloneDeep(keystore) : JSON.parse(keystore)

if (json.version !== 3 && json.version !== 4) console.warn('This is not a V3 or V4 wallet.')

Expand Down
1 change: 1 addition & 0 deletions test/packages/caver.klay.accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3531,6 +3531,7 @@ describe('caver.klay.accounts.decrypt', () => {
const result = caver.klay.accounts.decrypt(keystoreJsonV3, password)

isAccount(result, { keys: expectedAccount.keys, address: expectedAccount.address })
expect(keystoreJsonV3.crypto).not.to.be.undefined
}).timeout(50000)
})

Expand Down
1 change: 1 addition & 0 deletions test/packages/caver.wallet.keyring.js
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,7 @@ describe('caver.wallet.keyring.decrypt', () => {
const result = caver.wallet.keyring.decrypt(keystoreJsonV3, password)

validateKeyring(result, { keys: expectedAccount.keys, address: expectedAccount.address })
expect(keystoreJsonV3.crypto).not.to.be.undefined
})
})
})
Expand Down

0 comments on commit b362e19

Please sign in to comment.