-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MemoryAccount): Add validation of keypair (#594)
* feat(MemoryAccount): Add validation of keypair. Cover by tests * fix(Account): condition in selectAccount
- Loading branch information
Showing
6 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* ISC License (ISC) | ||
* Copyright (c) 2018 aeternity developers | ||
* | ||
* Permission to use, copy, modify, and/or distribute this software for any | ||
* purpose with or without fee is hereby granted, provided that the above | ||
* copyright notice and this permission notice appear in all copies. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH | ||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY | ||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, | ||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM | ||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR | ||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | ||
* PERFORMANCE OF THIS SOFTWARE. | ||
*/ | ||
|
||
import '../' | ||
import { describe, it } from 'mocha' | ||
import MemoryAccount from '../../es/account/memory' | ||
import { generateKeyPair } from '../../es/utils/crypto' | ||
|
||
const testAcc = generateKeyPair() | ||
|
||
describe('MemoryAccount', function () { | ||
describe('Fail on invalid params', () => { | ||
it('Fail on empty keypair', async () => { | ||
try { | ||
MemoryAccount({}) | ||
} catch (e) { | ||
e.message.should.be.equal('KeyPair must be an object') | ||
} | ||
}) | ||
it('Fail on empty keypair object', async () => { | ||
try { | ||
MemoryAccount({ keypair: {} }) | ||
} catch (e) { | ||
e.message.should.be.equal('KeyPair must must have "secretKey", "publicKey" properties') | ||
} | ||
}) | ||
it('Fail on invalid secret key', async () => { | ||
try { | ||
MemoryAccount({ | ||
keypair: { | ||
publicKey: testAcc.publicKey, | ||
secretKey: ' ' | ||
} | ||
}) | ||
} catch (e) { | ||
e.message.should.be.equal('Secret key must be hex string or Buffer') | ||
} | ||
}) | ||
it('Fail on invalid publicKey', async () => { | ||
try { | ||
MemoryAccount({ | ||
keypair: { | ||
publicKey: ' ', | ||
secretKey: testAcc.secretKey | ||
} | ||
}) | ||
} catch (e) { | ||
e.message.should.be.equal('Public Key must be a base58c string with "ak_" prefix') | ||
} | ||
}) | ||
}) | ||
it('Init with secretKey as hex string', async () => { | ||
const acc = MemoryAccount({ | ||
keypair: { | ||
publicKey: testAcc.publicKey, | ||
secretKey: testAcc.secretKey | ||
} | ||
}) | ||
return acc.address().should.eventually.be.equal(testAcc.publicKey) | ||
}) | ||
it('Init with secretKey as hex Buffer', async () => { | ||
const acc = MemoryAccount({ | ||
keypair: { | ||
publicKey: testAcc.publicKey, | ||
secretKey: Buffer.from(testAcc.secretKey, 'hex') | ||
} | ||
}) | ||
return acc.address().should.eventually.be.equal(testAcc.publicKey) | ||
}) | ||
}) |