|
| 1 | +/* |
| 2 | + * ISC License (ISC) |
| 3 | + * Copyright (c) 2018 aeternity developers |
| 4 | + * |
| 5 | + * Permission to use, copy, modify, and/or distribute this software for any |
| 6 | + * purpose with or without fee is hereby granted, provided that the above |
| 7 | + * copyright notice and this permission notice appear in all copies. |
| 8 | + * |
| 9 | + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH |
| 10 | + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY |
| 11 | + * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, |
| 12 | + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM |
| 13 | + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR |
| 14 | + * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 15 | + * PERFORMANCE OF THIS SOFTWARE. |
| 16 | + */ |
| 17 | + |
| 18 | +import '../' |
| 19 | +import { describe, it } from 'mocha' |
| 20 | +import MemoryAccount from '../../es/account/memory' |
| 21 | +import { generateKeyPair } from '../../es/utils/crypto' |
| 22 | + |
| 23 | +const testAcc = generateKeyPair() |
| 24 | + |
| 25 | +describe('MemoryAccount', function () { |
| 26 | + describe('Fail on invalid params', () => { |
| 27 | + it('Fail on empty keypair', async () => { |
| 28 | + try { |
| 29 | + MemoryAccount({}) |
| 30 | + } catch (e) { |
| 31 | + e.message.should.be.equal('KeyPair must be an object') |
| 32 | + } |
| 33 | + }) |
| 34 | + it('Fail on empty keypair object', async () => { |
| 35 | + try { |
| 36 | + MemoryAccount({ keypair: {} }) |
| 37 | + } catch (e) { |
| 38 | + e.message.should.be.equal('KeyPair must must have "secretKey", "publicKey" properties') |
| 39 | + } |
| 40 | + }) |
| 41 | + it('Fail on invalid secret key', async () => { |
| 42 | + try { |
| 43 | + MemoryAccount({ |
| 44 | + keypair: { |
| 45 | + publicKey: testAcc.publicKey, |
| 46 | + secretKey: ' ' |
| 47 | + } |
| 48 | + }) |
| 49 | + } catch (e) { |
| 50 | + e.message.should.be.equal('Secret key must be hex string or Buffer') |
| 51 | + } |
| 52 | + }) |
| 53 | + it('Fail on invalid publicKey', async () => { |
| 54 | + try { |
| 55 | + MemoryAccount({ |
| 56 | + keypair: { |
| 57 | + publicKey: ' ', |
| 58 | + secretKey: testAcc.secretKey |
| 59 | + } |
| 60 | + }) |
| 61 | + } catch (e) { |
| 62 | + e.message.should.be.equal('Public Key must be a base58c string with "ak_" prefix') |
| 63 | + } |
| 64 | + }) |
| 65 | + }) |
| 66 | + it('Init with secretKey as hex string', async () => { |
| 67 | + const acc = MemoryAccount({ |
| 68 | + keypair: { |
| 69 | + publicKey: testAcc.publicKey, |
| 70 | + secretKey: testAcc.secretKey |
| 71 | + } |
| 72 | + }) |
| 73 | + return acc.address().should.eventually.be.equal(testAcc.publicKey) |
| 74 | + }) |
| 75 | + it('Init with secretKey as hex Buffer', async () => { |
| 76 | + const acc = MemoryAccount({ |
| 77 | + keypair: { |
| 78 | + publicKey: testAcc.publicKey, |
| 79 | + secretKey: Buffer.from(testAcc.secretKey, 'hex') |
| 80 | + } |
| 81 | + }) |
| 82 | + return acc.address().should.eventually.be.equal(testAcc.publicKey) |
| 83 | + }) |
| 84 | +}) |
0 commit comments