Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/wallet-lib/src/types/WalletStore/WalletStore.js
Original file line number Diff line number Diff line change
@@ -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;
48 changes: 48 additions & 0 deletions packages/wallet-lib/src/types/WalletStore/WalletStore.spec.js
Original file line number Diff line number Diff line change
@@ -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())
});
})
})
Original file line number Diff line number Diff line change
@@ -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;
16 changes: 16 additions & 0 deletions packages/wallet-lib/src/types/WalletStore/methods/exportState.js
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function getIdentityIdByIndex(identityIndex) {
return this.state.identities.get(identityIndex);
}
module.exports = getIdentityIdByIndex;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function getIndexedIdentityIds() {
return [...this.state.identities.values()];
}
module.exports = getIndexedIdentityIds;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function getPathState(path) {
return this.state.paths.get(path);
}
module.exports = getPathState;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;