-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
770473c
commit 44083bc
Showing
9 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
packages/wallet-lib/src/types/WalletStore/WalletStore.spec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
}); | ||
}) | ||
}) |
12 changes: 12 additions & 0 deletions
12
packages/wallet-lib/src/types/WalletStore/methods/createPathState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
16
packages/wallet-lib/src/types/WalletStore/methods/exportState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
4 changes: 4 additions & 0 deletions
4
packages/wallet-lib/src/types/WalletStore/methods/getIdentityIdByIndex.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
4 changes: 4 additions & 0 deletions
4
packages/wallet-lib/src/types/WalletStore/methods/getIndexedIdentityIds.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
function getIndexedIdentityIds() { | ||
return [...this.state.identities.values()]; | ||
} | ||
module.exports = getIndexedIdentityIds; |
4 changes: 4 additions & 0 deletions
4
packages/wallet-lib/src/types/WalletStore/methods/getPathState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
9 changes: 9 additions & 0 deletions
9
packages/wallet-lib/src/types/WalletStore/methods/importState.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
12 changes: 12 additions & 0 deletions
12
packages/wallet-lib/src/types/WalletStore/methods/insertIdentityIdAtIndex.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |