diff --git a/src/CONSTANTS.js b/src/CONSTANTS.js index ac41f49d..838efb8e 100644 --- a/src/CONSTANTS.js +++ b/src/CONSTANTS.js @@ -81,7 +81,6 @@ const CONSTANTS = { ], UNSAFE_PROPERTIES: [ 'storage', - 'keyChain', 'identities', ], SAFE_PROPERTIES: [ diff --git a/src/plugins/Workers/IdentitySyncWorker.js b/src/plugins/Workers/IdentitySyncWorker.js index 1e32515b..99b5f3db 100644 --- a/src/plugins/Workers/IdentitySyncWorker.js +++ b/src/plugins/Workers/IdentitySyncWorker.js @@ -99,11 +99,12 @@ class IdentitySyncWorker extends Worker { logger.silly(`IdentitySyncWorker - got ${Identifier.from(fetchedId)} at ${index}`); // eslint-disable-next-line no-await-in-loop - await this.storage.insertIdentityIdAtIndex( - this.walletId, - Identifier.from(fetchedId).toString(), - index, - ); + await this.storage + .getWalletStore(this.walletId) + .insertIdentityIdAtIndex( + Identifier.from(fetchedId).toString(), + index, + ); } logger.silly('IdentitySyncWorker - sync finished'); diff --git a/src/transport/DAPIClientTransport/methods/getTransaction.spec.js b/src/transport/DAPIClientTransport/methods/getTransaction.spec.js index bb2dea97..63fad293 100644 --- a/src/transport/DAPIClientTransport/methods/getTransaction.spec.js +++ b/src/transport/DAPIClientTransport/methods/getTransaction.spec.js @@ -39,8 +39,8 @@ describe('transports - DAPIClientTransport .getTransaction', function suite() { expect(res.transaction.hash).to.equal('2c0ee853b91b23d881f96f0128bbb5ebb90c9ef7e7bdb4eda360b0e5abf97239'); expect(res.blockHash).to.equal('4f46066bd50cc2684484407696b7949e82bd906ea92c040f59a97cba47ed8176'); expect(res.height).to.equal(42); - expect(res.instantLocked).to.equal(true); - expect(res.chainLocked).to.equal(false); + expect(res.isInstantLocked).to.equal(true); + expect(res.isChainLocked).to.equal(false); }); it('should return null if transaction if not found', async () => { diff --git a/src/types/Account/Account.js b/src/types/Account/Account.js index 921db321..a3c2c117 100644 --- a/src/types/Account/Account.js +++ b/src/types/Account/Account.js @@ -272,10 +272,10 @@ class Account extends EventEmitter { */ waitForInstantLock(transactionHash, timeout = this.waitForInstantLockTimeout) { let rejectTimeout; - + const chainStore = this.storage.getChainStore(this.network); return Promise.race([ new Promise((resolve) => { - const instantLock = this.storage.getInstantLock(transactionHash); + const instantLock = chainStore.getInstantLock(transactionHash); if (instantLock != null) { clearTimeout(rejectTimeout); resolve(instantLock); diff --git a/src/types/Account/Account.spec.js b/src/types/Account/Account.spec.js index a7b01b6f..64dda6b0 100644 --- a/src/types/Account/Account.spec.js +++ b/src/types/Account/Account.spec.js @@ -7,6 +7,10 @@ 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 KeyChain = require("../KeyChain/KeyChain"); const blockHeader = new Dashcore.BlockHeader.fromObject({ hash: '00000ac3a0c9df709260e41290d6902e5a4a073099f11fe8c1ce80aadc4bb331', version: 2, @@ -29,7 +33,7 @@ describe('Account - class', function suite() { const mockStorage = { on: emitter.on, emit: emitter.emit, - store: {}, + storage: new Storage(), getStore: () => {}, saveState: () => {}, stopWorker: () => {}, @@ -43,8 +47,13 @@ describe('Account - class', function suite() { this.walletType = WALLET_TYPES.HDWALLET; this.accounts = []; this.network = Dashcore.Networks.testnet; - this.storage = mockStorage; + this.storage = new Storage(); })()); + mocks.wallet.storage.application.network = mocks.wallet.network; + mocks.wallet.storage.createWalletStore(mocks.wallet.walletId); + mocks.wallet.storage.createChainStore(mocks.wallet.network); + mocks.wallet.keyChainStore = new KeyChainStore() + mocks.wallet.keyChainStore.addKeyChain(new KeyChain({mnemonic: fluidMnemonic.mnemonic}), { isMasterKeyChain: true }) }); it('should be specify on missing params', () => { const expectedException1 = 'Expected wallet to be passed as param'; @@ -96,7 +105,7 @@ describe('Account - class', function suite() { await account.on(EVENTS.BLOCKHEADER, ()=>{ done(); }); - account.storage.importBlockHeader(blockHeader); + account.storage.getChainStore(account.network).importBlockHeader(blockHeader); }) }); diff --git a/src/types/Account/methods/broadcastTransaction.js b/src/types/Account/methods/broadcastTransaction.js index 75e39b0c..0d11eba6 100644 --- a/src/types/Account/methods/broadcastTransaction.js +++ b/src/types/Account/methods/broadcastTransaction.js @@ -75,7 +75,7 @@ function impactAffectedInputs({ transaction }) { */ async function broadcastTransaction(transaction, options = {}) { const { network, storage } = this; - // if (!this.transport) throw new ValidTransportLayerRequired('broadcast'); + if (!this.transport) throw new ValidTransportLayerRequired('broadcast'); // We still support having in rawtransaction, if this is the case // we first need to reform our object diff --git a/src/types/Account/methods/broadcastTransaction.spec.js b/src/types/Account/methods/broadcastTransaction.spec.js index 0613a8e3..df89a2ca 100644 --- a/src/types/Account/methods/broadcastTransaction.spec.js +++ b/src/types/Account/methods/broadcastTransaction.spec.js @@ -4,7 +4,7 @@ const broadcastTransaction = require('./broadcastTransaction'); const validRawTxs = require('../../../../fixtures/rawtx').valid; const invalidRawTxs = require('../../../../fixtures/rawtx').invalid; const expectThrowsAsync = require('../../../utils/expectThrowsAsync'); - +const ChainStore = require('../../ChainStore/ChainStore'); const { PrivateKey } = Dashcore; describe('Account - broadcastTransaction', function suite() { @@ -14,12 +14,14 @@ describe('Account - broadcastTransaction', function suite() { let keysToSign; let oneToOneTx; let fee; + // const storage = new Storage(); + // storage.createChainStore('testnet') + // storage.getChainStore('testnet').state.fees.minRelay = 888; + const chainStore = new ChainStore('testnet'); + chainStore.state.fees.minRelay = 888; + chainStore.importAddress('yTBXsrcGw74yMUsK34fBKAWJx3RNCq97Aq'); const storage = { - getStore: ()=>({ - chains:{ - "testnet": { fees: { minRelay: 888 }} - } - }) + getChainStore:()=> chainStore } beforeEach(() => { utxos = [ @@ -87,11 +89,7 @@ describe('Account - broadcastTransaction', function suite() { sendTransaction: () => sendCalled = +1, }, network: 'testnet', - storage: { - getStore: storage.getStore, - searchAddress: () => { searchCalled = +1; return { found: false }; }, - searchAddressesWithTx: () => { searchCalled = +1; return { results: [] }; }, - }, + storage }; const tx = oneToOneTx; @@ -109,11 +107,7 @@ describe('Account - broadcastTransaction', function suite() { sendTransaction: () => sendCalled = +1, }, network: 'testnet', - storage: { - getStore: storage.getStore, - searchAddress: () => { searchCalled = +1; return { found: false }; }, - searchAddressesWithTx: (affectedTxId) => { searchCalled = +1; return { results: [] }; }, - }, + storage }; return broadcastTransaction @@ -132,11 +126,7 @@ describe('Account - broadcastTransaction', function suite() { sendTransaction: () => sendCalled = +1, }, network: 'testnet', - storage: { - getStore: storage.getStore, - searchAddress: () => { searchCalled = +1; return { found: false }; }, - searchAddressesWithTx: () => { searchCalled = +1; return { results: [] }; }, - }, + storage }; const tx = oneToOneTx; @@ -151,11 +141,7 @@ describe('Account - broadcastTransaction', function suite() { sendTransaction: () => sendCalled = +1, }, network: 'testnet', - storage: { - getStore: storage.getStore, - searchAddress: () => { searchCalled = +1; return { found: false }; }, - searchAddressesWithTx: () => { searchCalled = +1; return { results: [] }; }, - }, + storage }; const tx = oneToOneTx; diff --git a/src/types/Account/methods/getUnusedIdentityIndex.js b/src/types/Account/methods/getUnusedIdentityIndex.js index 7cf353d0..0e32b274 100644 --- a/src/types/Account/methods/getUnusedIdentityIndex.js +++ b/src/types/Account/methods/getUnusedIdentityIndex.js @@ -6,7 +6,7 @@ async function getUnusedIdentityIndex() { // Force identities sync before return unused index await this.getWorker('IdentitySyncWorker').execWorker(); - const identityIds = this.storage.getIndexedIdentityIds(this.walletId); + const identityIds = this.storage.getWalletStore(this.walletId).getIndexedIdentityIds(); const firstMissingIndex = identityIds.findIndex((identityId) => !identityId); diff --git a/src/types/ChainStore/methods/importInstantLock.spec.js b/src/types/ChainStore/methods/importInstantLock.spec.js new file mode 100644 index 00000000..86cfd344 --- /dev/null +++ b/src/types/ChainStore/methods/importInstantLock.spec.js @@ -0,0 +1 @@ +const is = '01424b1c86a93794b4cc8bf9391494faf6994fe6794747ba2bc415e7bcd353204301000000c518facee06d0dc8d5b71028ce949dc8ba99d95c023af2752421a3647f6ce668827fbd5503f9a48a73ab9e30e564cd8b95f6c603caa93deaf4c3836c89d19cb71edc6edab0eccafe42d415e5598fa92e15ff71d5e8a81a34cac04c2fea031cc2f9e653a2d2a7fa2ef1994dee8df60e031c29ab7481ccb1bd3a89e7576cb79880'; \ No newline at end of file diff --git a/src/types/Storage/Storage.js b/src/types/Storage/Storage.js index b7a0e439..019a5670 100644 --- a/src/types/Storage/Storage.js +++ b/src/types/Storage/Storage.js @@ -20,5 +20,7 @@ Storage.prototype.createChainStore = require('./methods/createChainStore'); Storage.prototype.createWalletStore = require('./methods/createWalletStore'); Storage.prototype.getChainStore = require('./methods/getChainStore'); Storage.prototype.getWalletStore = require('./methods/getWalletStore'); +Storage.prototype.rehydrateState = require('./methods/rehydrateState'); +Storage.prototype.saveState = require('./methods/saveState'); module.exports = Storage; diff --git a/src/types/WalletStore/WalletStore.js b/src/types/WalletStore/WalletStore.js index cf05dbc7..238eb6b3 100644 --- a/src/types/WalletStore/WalletStore.js +++ b/src/types/WalletStore/WalletStore.js @@ -34,7 +34,7 @@ class WalletStore { return this.state.identities.get(identityIndex); } - insertIdentityAtIndex(identityId, identityIndex) { + insertIdentityIdAtIndex(identityId, identityIndex) { const existingId = this.getIdentityIdByIndex(identityIndex); if (Boolean(existingId) && existingId !== identityId) {