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 #106 from jimni1222/internalFunction
Browse files Browse the repository at this point in the history
Rename determineAddress to _determineAddress
  • Loading branch information
jimni1222 authored Sep 19, 2019
2 parents e5d4902 + e75251e commit 98065ad
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 83 deletions.
65 changes: 33 additions & 32 deletions packages/caver-klay/caver-klay-accounts/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,37 @@ Accounts.prototype._addAccountFunctions = function (account) {
return account;
};


/**
* _determineAddress determines the priority of the parameters entered and returns the address that should be used for the account.
*
* @method _determineAddress
* @param {Object} legacyAccount Account with a legacy account key extracted from private key to be used for address determination.
* @param {String} addressFromKey Address extracted from key.
* @param {String} userInputAddress Address passed as parameter by user.
* @return {String}
*/
Accounts.prototype._determineAddress = function _determineAddress(legacyAccount, addressFromKey, userInputAddress) {
if (userInputAddress) {
if(addressFromKey && addressFromKey !== userInputAddress) {
throw new Error('The address extracted from the private key does not match the address received as the input value.')
}

if(!utils.isAddress(userInputAddress)) {
throw new Error('The address received as the input value is invalid.')
}
return userInputAddress

} else if (addressFromKey){
if(!utils.isAddress(addressFromKey)) {
throw new Error('The address extracted from the private key is invalid.')
}
// If userInputAddress is undefined and address which is came from private is existed, set address in account.
return addressFromKey
}
return legacyAccount.address
}

Accounts.prototype.create = function create(entropy) {
return this._addAccountFunctions(Account.create(entropy || utils.randomHex(32)));
};
Expand All @@ -120,7 +151,7 @@ Accounts.prototype.create = function create(entropy) {
Accounts.prototype.privateKeyToAccount = function privateKeyToAccount(key, userInputAddress) {
let {legacyAccount: account, klaytnWalletKeyAddress} = this.getLegacyAccount(key)

account.address = this.determineAddress(account, klaytnWalletKeyAddress, userInputAddress)
account.address = this._determineAddress(account, klaytnWalletKeyAddress, userInputAddress)
account.address = account.address.toLowerCase()
account.address = utils.addHexPrefix(account.address)

Expand All @@ -137,7 +168,7 @@ Accounts.prototype.privateKeyToAccount = function privateKeyToAccount(key, userI
*/
Accounts.prototype.isDecoupled = function isDecoupled(key, userInputAddress) {
let { legacyAccount, klaytnWalletKeyAddress } = this.getLegacyAccount(key)
let actualAddress = this.determineAddress(legacyAccount, klaytnWalletKeyAddress, userInputAddress)
let actualAddress = this._determineAddress(legacyAccount, klaytnWalletKeyAddress, userInputAddress)

return legacyAccount.address.toLowerCase() !== actualAddress.toLowerCase()
}
Expand All @@ -160,36 +191,6 @@ Accounts.prototype.getLegacyAccount = function getLegacyAccount(key) {
return { legacyAccount: Account.fromPrivate(privateKey), klaytnWalletKeyAddress }
}

/**
* determineAddress determines the priority of the parameters entered and returns the address that should be used for the account.
*
* @method determineAddress
* @param {Object} legacyAccount Account with a legacy account key extracted from private key to be used for address determination.
* @param {String} addressFromKey Address extracted from key.
* @param {String} userInputAddress Address passed as parameter by user.
* @return {String}
*/
Accounts.prototype.determineAddress = function determineAddress(legacyAccount, addressFromKey, userInputAddress) {
if (userInputAddress) {
if(addressFromKey && addressFromKey !== userInputAddress) {
throw new Error('The address extracted from the private key does not match the address received as the input value.')
}

if(!utils.isAddress(userInputAddress)) {
throw new Error('The address received as the input value is invalid.')
}
return userInputAddress

} else if (addressFromKey){
if(!utils.isAddress(addressFromKey)) {
throw new Error('The address extracted from the private key is invalid.')
}
// If userInputAddress is undefined and address which is came from private is existed, set address in account.
return addressFromKey
}
return legacyAccount.address
}

Accounts.prototype.signTransaction = function signTransaction(tx, privateKey, callback) {
var _this = this,
error = false,
Expand Down
51 changes: 0 additions & 51 deletions test/packages/caver.klay.accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -778,57 +778,6 @@ describe('caver.klay.accounts.isDecoupled', () => {
})
})

describe('caver.klay.accounts.determineAddress', () => {
context('CAVERJS-UNIT-WALLET-117 : input: valid nonDecoupledAccount', () => {
it('should return address of nonDecoupledAccount', () => {
const testAccount = caver.klay.accounts.create()

expect(caver.klay.accounts.determineAddress(testAccount)).to.equals(testAccount.address)
})
})

context('CAVERJS-UNIT-WALLET-118 : input: valid nonDecoupledAccount and addressFromKey', () => {
it('should return address of account', () => {
const testAccount = caver.klay.accounts.create()
const addressFromKey = caver.klay.accounts.create().address

expect(caver.klay.accounts.determineAddress(testAccount, addressFromKey)).to.equals(addressFromKey)
})
})

context('CAVERJS-UNIT-WALLET-119 : input: valid nonDecoupledAccount, addressFromKey and userInputAddress', () => {
it('should return address of account', () => {
const testAccount = caver.klay.accounts.create()
const addressFromKey = ''
const userInputAddress = caver.klay.accounts.create().address

expect(caver.klay.accounts.determineAddress(testAccount, addressFromKey, userInputAddress)).to.equals(userInputAddress)
})
})

context('CAVERJS-UNIT-WALLET-120 : input: valid nonDecoupledAccount, addressFromKey and userInputAddress', () => {
it('should return address of account', () => {
const testAccount = caver.klay.accounts.create()
const addressFromKey = caver.klay.accounts.create().address
const userInputAddress = addressFromKey

expect(caver.klay.accounts.determineAddress(testAccount, addressFromKey, userInputAddress)).to.equals(userInputAddress)
})
})

context('CAVERJS-UNIT-WALLET-121 : input: valid nonDecoupledAccount, addressFromKey and userInputAddress', () => {
it('should throw error if addressFromKey and userInputAddress is not matched', () => {
const testAccount = caver.klay.accounts.create()
const addressFromKey = caver.klay.accounts.create().address
const userInputAddress = caver.klay.accounts.create().address

const expectedError = 'The address extracted from the private key does not match the address received as the input value.'

expect(() => caver.klay.accounts.determineAddress(testAccount, addressFromKey, userInputAddress)).to.throws(expectedError)
})
})
})

describe('caver.klay.accounts.wallet', () => {

it('CAVERJS-UNIT-WALLET-043 : should return valid wallet instance', () => {
Expand Down

0 comments on commit 98065ad

Please sign in to comment.