Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Keystore): Allow to store secret as hex or buffer #939

Merged
merged 1 commit into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion es/utils/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,9 @@ export async function recover (password, keyObject) {
keyObject.crypto.symmetric_alg
)
if (!key) throw new Error('Invalid password')
return Buffer.from(key).toString('hex')

if (Buffer.from(key).length === 64) return Buffer.from(key).toString('hex')
return Buffer.from(key).toString('utf-8')
}

/**
Expand Down
21 changes: 13 additions & 8 deletions test/unit/keystore.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,32 +45,37 @@ describe('Keystore', function () {

const { secretKey } = generateKeyPair(true)
const publicKey = getAddressFromPriv(secretKey)
let keystore
let keystoreBuffer
let keystoreHex

it('dump account to keystore object', async () => {
keystore = await dump('test', password, secretKey)
validateKeyObj(keystore).should.be.equal(true)
keystoreBuffer = await dump('test', password, secretKey)
keystoreHex = await dump('test', password, secretKey.toString('hex'))
validateKeyObj(keystoreBuffer).should.be.equal(true)
validateKeyObj(keystoreHex).should.be.equal(true)
})

it('restore account from keystore object', async () => {
const priv = await recover(password, keystore)
const accAddress = getAddressFromPriv(priv)
const privFromBuffer = await recover(password, keystoreBuffer)
const privFromHex = await recover(password, keystoreHex)
const accAddress = getAddressFromPriv(privFromBuffer)

secretKey.toString('hex').should.be.equal(priv)
secretKey.toString('hex').should.be.equal(privFromBuffer)
secretKey.toString('hex').should.be.equal(privFromHex)
publicKey.should.be.equal(accAddress)
})

it('use invalid keystore json', async () => {
try {
await await recover(password, invalidKeystore)
await recover(password, invalidKeystore)
} catch (e) {
e.message.should.be.equal('Invalid key file format. Require properties: ciphertext,symmetric_alg')
}
})

it('use invalid keystore password', async () => {
try {
await await recover(password + 1, keystore)
await recover(password + 1, keystoreBuffer)
} catch (e) {
e.message.should.be.equal('Invalid password or nonce')
}
Expand Down