diff --git a/packages/wallet-lib/src/types/WalletStore/WalletStore.js b/packages/wallet-lib/src/types/WalletStore/WalletStore.js new file mode 100644 index 00000000000..b105d6792b8 --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/WalletStore.js @@ -0,0 +1,21 @@ +class WalletStore { + constructor(walletId) { + this.walletId = walletId; + + this.state = { + mnemonic: null, + paths: new Map(), + identities: new Map(), + }; + } +} + +WalletStore.prototype.createPathState = require('./methods/createPathState'); +WalletStore.prototype.exportState = require('./methods/exportState'); +WalletStore.prototype.getIdentityIdByIndex = require('./methods/getIdentityIdByIndex'); +WalletStore.prototype.getIndexedIdentityIds = require('./methods/getIndexedIdentityIds'); +WalletStore.prototype.getPathState = require('./methods/getPathState'); +WalletStore.prototype.importState = require('./methods/importState'); +WalletStore.prototype.insertIdentityIdAtIndex = require('./methods/insertIdentityIdAtIndex'); + +module.exports = WalletStore; diff --git a/packages/wallet-lib/src/types/WalletStore/WalletStore.spec.js b/packages/wallet-lib/src/types/WalletStore/WalletStore.spec.js new file mode 100644 index 00000000000..0d5d8f4d41b --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/WalletStore.spec.js @@ -0,0 +1,48 @@ +const { expect } = require('chai'); +const WalletStore = require('./WalletStore'); + +let walletStore; +describe('WalletStore - Class', ()=> { + describe('simple usage', () => { + it('should create a walletStore', function () { + walletStore = new WalletStore('squawk7700'); + expect(walletStore.walletId).to.equal('squawk7700'); + }); + it('should create path state', function () { + walletStore.createPathState('m/0') + expect(walletStore.state.paths.get('m/0')).to.deep.equal({ + path: 'm/0', + addresses: {} + }); + // TODO: Can be done later to have a better way to update path state + walletStore.state.paths.get('m/0').addresses['m/0'] = 'yTwEca67QSkZ6axGdpNFzWPaCj8zqYybY7' + }); + + it('should get path state', function () { + const pathState = walletStore.getPathState('m/0'); + expect(pathState).to.deep.equal({ + path: 'm/0', + addresses: { + 'm/0': 'yTwEca67QSkZ6axGdpNFzWPaCj8zqYybY7' + } + }); + }); + it('should insert identity', function () { + const identityId = 'abcde1234'; + const identityIndex = 0; + walletStore.insertIdentityIdAtIndex(identityId, identityIndex); + }); + it('should get indexed identity ids', function () { + expect(walletStore.getIndexedIdentityIds()).to.deep.equal(['abcde1234']) + }); + it('should get identity id by index', function () { + expect(walletStore.getIdentityIdByIndex(0)).to.deep.equal('abcde1234') + }); + it('should export and import state', function () { + const exportedState = walletStore.exportState(); + const importedWalletStore = new WalletStore(); + importedWalletStore.importState(exportedState); + expect(exportedState).to.deep.equal(importedWalletStore.exportState()) + }); + }) +}) diff --git a/packages/wallet-lib/src/types/WalletStore/methods/createPathState.js b/packages/wallet-lib/src/types/WalletStore/methods/createPathState.js new file mode 100644 index 00000000000..2dd215942ce --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/createPathState.js @@ -0,0 +1,12 @@ +const logger = require('../../../logger'); + +function createPathState(path) { + logger.debug(`WalletStore - Creating path state ${path}`); + if (!this.state.paths.has(path)) { + this.state.paths.set(path, { + path, + addresses: {}, + }); + } +} +module.exports = createPathState; diff --git a/packages/wallet-lib/src/types/WalletStore/methods/exportState.js b/packages/wallet-lib/src/types/WalletStore/methods/exportState.js new file mode 100644 index 00000000000..03b37ea6667 --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/exportState.js @@ -0,0 +1,16 @@ +const { cloneDeep } = require('lodash'); + +function exportState() { + const { walletId } = this; + const { mnemonic, paths, identities } = this.state; + + return { + walletId, + state: { + mnemonic, + paths: cloneDeep(Object.fromEntries(paths)), + identities: cloneDeep(Object.fromEntries(identities)), + }, + }; +} +module.exports = exportState; diff --git a/packages/wallet-lib/src/types/WalletStore/methods/getIdentityIdByIndex.js b/packages/wallet-lib/src/types/WalletStore/methods/getIdentityIdByIndex.js new file mode 100644 index 00000000000..f90d1e5782a --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/getIdentityIdByIndex.js @@ -0,0 +1,4 @@ +function getIdentityIdByIndex(identityIndex) { + return this.state.identities.get(identityIndex); +} +module.exports = getIdentityIdByIndex; diff --git a/packages/wallet-lib/src/types/WalletStore/methods/getIndexedIdentityIds.js b/packages/wallet-lib/src/types/WalletStore/methods/getIndexedIdentityIds.js new file mode 100644 index 00000000000..e251ba4806b --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/getIndexedIdentityIds.js @@ -0,0 +1,4 @@ +function getIndexedIdentityIds() { + return [...this.state.identities.values()]; +} +module.exports = getIndexedIdentityIds; diff --git a/packages/wallet-lib/src/types/WalletStore/methods/getPathState.js b/packages/wallet-lib/src/types/WalletStore/methods/getPathState.js new file mode 100644 index 00000000000..d2143cd4d95 --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/getPathState.js @@ -0,0 +1,4 @@ +function getPathState(path) { + return this.state.paths.get(path); +} +module.exports = getPathState; diff --git a/packages/wallet-lib/src/types/WalletStore/methods/importState.js b/packages/wallet-lib/src/types/WalletStore/methods/importState.js new file mode 100644 index 00000000000..e4eb709107c --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/importState.js @@ -0,0 +1,9 @@ +const { cloneDeep } = require('lodash'); + +function importState(state) { + this.walletId = state.walletId; + this.state.mnemonic = state.state.mnemonic; + this.state.paths = new Map(cloneDeep(Object.entries(state.state.paths))); + this.state.identities = new Map(cloneDeep(Object.entries(state.state.identities))); +} +module.exports = importState; diff --git a/packages/wallet-lib/src/types/WalletStore/methods/insertIdentityIdAtIndex.js b/packages/wallet-lib/src/types/WalletStore/methods/insertIdentityIdAtIndex.js new file mode 100644 index 00000000000..ee8a9325190 --- /dev/null +++ b/packages/wallet-lib/src/types/WalletStore/methods/insertIdentityIdAtIndex.js @@ -0,0 +1,12 @@ +const IdentityReplaceError = require('../../../errors/IndentityIdReplaceError'); + +function insertIdentityIdAtIndex(identityId, identityIndex) { + const existingId = this.getIdentityIdByIndex(identityIndex); + + if (Boolean(existingId) && existingId !== identityId) { + throw new IdentityReplaceError(`Trying to replace identity at index ${identityIndex}`); + } + + this.state.identities.set(identityIndex, identityId); +} +module.exports = insertIdentityIdAtIndex;