UNPKG

@dashevo/wallet-lib

Version:
125 lines (118 loc) 4.71 kB
const { expect } = require('chai'); const Dashcore = require('@dashevo/dashcore-lib'); const knifeMnemonic = require('../../../fixtures/knifeeasily'); const fluidMnemonic = require('../../../fixtures/fluidDepth'); const cR4t6ePrivateKey = require('../../../fixtures/cR4t6e_pk'); const { WALLET_TYPES } = require('../../CONSTANTS'); const { Account, EVENTS } = require('../../index'); const EventEmitter = require('events'); const inMem = require('../../adapters/InMem'); const Storage = require('../Storage/Storage'); const {mock} = require("sinon"); const KeyChainStore = require("../KeyChainStore/KeyChainStore"); const DerivableKeyChain = require("../DerivableKeyChain/DerivableKeyChain"); const blockHeader = new Dashcore.BlockHeader.fromObject({ hash: '00000ac3a0c9df709260e41290d6902e5a4a073099f11fe8c1ce80aadc4bb331', version: 2, prevHash: '00000ce430de949c85a145b02e33ebbaed3772dc8f3d668f66edc6852c24d002', merkleRoot: '663360403b5fba9cd8744c3706f9660c7d3fee4e5a9ee98ce0ad5e5ad7824c1d', time: 1398712821, bits: 504365040, nonce: 312363 }); const mocks = { adapter: inMem, offlineMode: true, }; describe('Account - class', function suite() { this.timeout(10000); before(() => { const emitter = new EventEmitter(); const mockStorage = { on: emitter.on, emit: emitter.emit, storage: new Storage(), getStore: () => {}, saveState: () => {}, createAccount: () => {}, }; mocks.wallet = (new (function Wallet() { this.walletId = '1234567891'; this.walletType = WALLET_TYPES.HDWALLET; this.accounts = []; this.network = Dashcore.Networks.testnet; this.storage = new Storage(); this.transport = { client: { blockHeadersProvider: { initializeChainWith: () => {} } } } })()); mocks.wallet.storage.application.network = mocks.wallet.network; mocks.wallet.storage.currentNetwork = mocks.wallet.network.toString() mocks.wallet.storage.createWalletStore(mocks.wallet.walletId); mocks.wallet.storage.createChainStore(mocks.wallet.network); mocks.wallet.keyChainStore = new KeyChainStore() mocks.wallet.keyChainStore.addKeyChain(new DerivableKeyChain({mnemonic: fluidMnemonic.mnemonic}), { isMasterKeyChain: true }) }); it('should be specify on missing params', () => { const expectedException1 = 'Expected wallet to be passed as param'; expect(() => new Account()).to.throw(expectedException1); }); it('should create an account', () => { const mockWallet = mocks.wallet; const account = new Account(mockWallet, { injectDefaultPlugins: false }); account.init(mockWallet).then(()=>{ expect(account).to.be.deep.equal(mockWallet.accounts[0]); expect(account.index).to.be.deep.equal(0); expect(account.injectDefaultPlugins).to.be.deep.equal(false); expect(account.allowSensitiveOperations).to.be.deep.equal(false); expect(account.state.isReady).to.be.deep.equal(true); expect(account.type).to.be.deep.equal(undefined); expect(account.transactions).to.be.deep.equal({}); expect(account.label).to.be.deep.equal(null); expect(account.transport).to.be.deep.equal(undefined); expect(account.cacheTx).to.be.deep.equal(true); expect(account.plugins).to.be.deep.equal({ workers: {}, standard: {}, watchers: {}, }); account.disconnect(); }) }); it('should correctly create the right expected index', async () => { const mockWallet = mocks.wallet; const account = new Account(mockWallet, { injectDefaultPlugins: false }); await account.init(mockWallet); const account2 = new Account(mockWallet, { index: 10, injectDefaultPlugins: false }); await account2.init(mockWallet); const account3 = new Account(mockWallet, { injectDefaultPlugins: false }); await account3.init(mockWallet); expect(account.index).to.be.deep.equal(1); expect(account2.index).to.be.deep.equal(10); expect(account3.index).to.be.deep.equal(2); account.disconnect(); account2.disconnect(); account3.disconnect(); }); // TODO: Events are not working properly, restore this functionality once // all events repaired // it('should forward events', function (done) { // const mockWallet = mocks.wallet; // const account = new Account(mockWallet, { injectDefaultPlugins: false }); // // account.on(EVENTS.CONFIGURED, ()=>{ // done(); // }); // // account.init(mockWallet) // .then(async ()=>{ // account.on(EVENTS.BLOCKHEADER, ()=>{ // done(); // }); // account.importBlockHeader(blockHeader); // }) // // }); });