-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: multi-seed memoized keychain #80
Draft
mvayngrib
wants to merge
7
commits into
master
Choose a base branch
from
mv/memoized-multi-seed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d0c6123
refactor: bump cache key
mvayngrib d910628
refactor: memoized-keychain to multi-seed api
mvayngrib 0e91ffc
chore: add typeforce dep
mvayngrib 92e97ef
feat: export memoized api from feature
mvayngrib a22d5b6
test: memoized-keychain integration
mvayngrib 2ddc70c
test: memoized keychain
mvayngrib 9724ba1
feat: remove publicKeys from memory on removeAllSeeds
mvayngrib File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
106 changes: 106 additions & 0 deletions
106
features/keychain/module/__tests__/memoized-keychain.test.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,106 @@ | ||
import stableStringify from 'json-stable-stringify' | ||
import { mnemonicToSeed } from 'bip39' | ||
import BJSON from 'buffer-json' | ||
|
||
import KeyIdentifier from '@exodus/key-identifier' | ||
import memoizedKeychainDefinition from '../memoized-keychain' | ||
import { getSeedId } from '../crypto/seed-id' | ||
|
||
const mockStorage = { | ||
get: jest.fn(), | ||
set: jest.fn(), | ||
} | ||
|
||
const seed = mnemonicToSeed( | ||
'menu memory fury language physical wonder dog valid smart edge decrease worth' | ||
) | ||
|
||
const seedId = getSeedId(seed) | ||
|
||
const keyIdentifierEthereum0 = new KeyIdentifier({ | ||
derivationAlgorithm: 'BIP32', | ||
derivationPath: "m/44'/60'/0'/0/0", | ||
assetName: 'ethereum', | ||
}) | ||
|
||
const storage = mockStorage | ||
|
||
describe('memoizedKeychain', () => { | ||
let memoizedKeychain | ||
|
||
beforeEach(() => { | ||
mockStorage.get.mockResolvedValue(null) | ||
memoizedKeychain = memoizedKeychainDefinition.factory({ storage }) | ||
memoizedKeychain.addSeed(seed) | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should construct correctly', () => { | ||
expect(() => memoizedKeychainDefinition.factory({ storage })).not.toThrow() | ||
}) | ||
|
||
it('should clone correctly', () => { | ||
expect(() => memoizedKeychainDefinition.factory({ storage }).clone()).not.toThrow() | ||
}) | ||
|
||
describe.each([ | ||
{ | ||
name: 'memoized', | ||
factory: () => { | ||
const keychain = memoizedKeychainDefinition.factory({ storage }) | ||
keychain.addSeed(seed) | ||
return keychain | ||
}, | ||
}, | ||
{ | ||
name: 'memoizedClone', | ||
factory: () => { | ||
const clone = memoizedKeychain.clone() | ||
clone.addSeed(seed) | ||
return clone | ||
}, | ||
}, | ||
])('memoization: $name', ({ factory }) => { | ||
const storageKey = stableStringify({ | ||
seedId, | ||
keyId: { | ||
assetName: 'ethereum', | ||
derivationAlgorithm: 'BIP32', | ||
derivationPath: "m/44'/60'/0'/0/0", | ||
keyType: 'secp256k1', | ||
}, | ||
}) | ||
|
||
const storageData = { | ||
[storageKey]: { | ||
xpub: 'xpub6H8P7xAy1nvvefMui3rD6yK3cdkBSAhukKRcxeqydPqdm8L8FAvxu33Hgoajcr8PW1oBPDm7sRdPSoUz55kcCF9LNd5RatNgExPrn8Pvd5P', | ||
publicKey: Buffer.from('AgyNoIyo7xR+pkMj5TnXkQE/8NtK6dMck+KUghia9w3l', 'base64'), | ||
}, | ||
} | ||
|
||
it('should retrieve publicKeys from storage at construction', async () => { | ||
mockStorage.get.mockResolvedValueOnce(BJSON.stringify(storageData)) | ||
factory() | ||
expect(mockStorage.get).toHaveBeenCalledTimes(1) | ||
}) | ||
|
||
it('should write publicKeys to storage if not found', async () => { | ||
mockStorage.get.mockResolvedValueOnce(null) | ||
const keychain = factory() | ||
const exported = await keychain.exportKey({ seedId, keyId: keyIdentifierEthereum0 }) | ||
expect(mockStorage.set).toHaveBeenCalledTimes(1) | ||
expect(mockStorage.set).toHaveBeenCalledWith('data1', BJSON.stringify(storageData)) | ||
const exportedFromCache = await keychain.exportKey({ seedId, keyId: keyIdentifierEthereum0 }) | ||
expect(mockStorage.set).toHaveBeenCalledTimes(1) | ||
expect(exported).toStrictEqual({ privateKey: null, xpriv: null, ...exportedFromCache }) | ||
}) | ||
|
||
it('should still allow private keys', async () => { | ||
mockStorage.get.mockResolvedValueOnce(null) | ||
const keychain = factory() | ||
await expect( | ||
keychain.exportKey({ seedId, keyId: keyIdentifierEthereum0, exportPrivate: true }) | ||
).resolves.not.toThrow() | ||
}) | ||
}) | ||
}) |
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
adapted from the one removed here 82e5cf2