-
Notifications
You must be signed in to change notification settings - Fork 3
[KLC-1871] Implement Node Wallet package #4
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
Merged
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1950a9a
feat: implement HD wallet and keystore functionality
phcarneirobc e4ee6b3
add @scure/bip32 and @noble/curves dependencies
phcarneirobc 2168a4c
fix: lint
phcarneirobc 23cc5f9
fix: update dependencies for security
phcarneirobc 1dd58f5
test: add unit tests for HD wallet and keystore functionalities
phcarneirobc 580cf5c
feat: update to use Klever coin type and derivation path & add comments
phcarneirobc 891a77b
fix: reduce scrypt N parameter in keystore integration test
phcarneirobc 4de010f
feat: implement constant-time comparison for security
phcarneirobc 334e01f
fix: update test for invalid mnemonic checksum validation
phcarneirobc 7cbc0e8
feat: add protected method to retrieve private key in NodeWallet
phcarneirobc 07731ec
fix: update AES encryption/decryption functions to use 'any' type
phcarneirobc d8e40dd
fix: update derivation paths to include trailing apostrophes for cons…
phcarneirobc 6d10fc3
refactor: rename Wallet to DefaultWallet for clarity and consistency
phcarneirobc f8ecfe4
refactor: update getPrivateKey return type to Uint8Array
phcarneirobc db480b9
feat: update keystore to use AES-256-GCM for encryption and decryption
phcarneirobc 4a6be7a
feat: implement SLIP-0010 Ed25519 key derivation and update mnemonicT…
phcarneirobc a5dffd7
refactor: reorder imports and update keystore version in tests
phcarneirobc cde6385
fix: tests derivation path and improve and error handling
phcarneirobc 72a2a2c
update wallet type in KleverState from IWallet to Wallet
phcarneirobc eca9bd0
fix: update wallet type in KleverAction from IWallet to Wallet
phcarneirobc 48c7ad7
refactor: remove DefaultWallet class and update wallet exports
phcarneirobc 69d6bee
refactor: update aes256GcmEncrypt and aes256GcmDecrypt to use BufferS…
phcarneirobc fd9dd55
fix: add DOM library to tsconfig for type support
phcarneirobc 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 hidden or 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 |
|---|---|---|
|
|
@@ -62,7 +62,9 @@ | |
| }, | ||
| "pnpm": { | ||
| "overrides": { | ||
| "vite": "^7.1.12" | ||
| "vite": "^7.1.12", | ||
| "glob": ">=10.5.0", | ||
| "js-yaml": ">=4.1.1" | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
256 changes: 256 additions & 0 deletions
256
packages/connect-crypto/src/__tests__/hd-wallet.test.ts
This file contains hidden or 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,256 @@ | ||
| import { describe, expect, it } from 'vitest' | ||
| import { | ||
| buildDerivationPath, | ||
| DEFAULT_DERIVATION_PATH, | ||
| deriveMultipleKeys, | ||
| generateMnemonicPhrase, | ||
| isValidMnemonic, | ||
| KLEVER_COIN_TYPE, | ||
| mnemonicToPrivateKey, | ||
| } from '../hd-wallet' | ||
|
|
||
| describe('hd-wallet', () => { | ||
| describe('generateMnemonicPhrase', () => { | ||
| it('should generate a 12-word mnemonic by default (128 bits)', () => { | ||
| const mnemonic = generateMnemonicPhrase() | ||
| const words = mnemonic.split(' ') | ||
|
|
||
| expect(words.length).toBe(12) | ||
| expect(isValidMnemonic(mnemonic)).toBe(true) | ||
| }) | ||
|
|
||
| it('should generate a 15-word mnemonic (160 bits)', () => { | ||
| const mnemonic = generateMnemonicPhrase({ strength: 160 }) | ||
| const words = mnemonic.split(' ') | ||
|
|
||
| expect(words.length).toBe(15) | ||
| expect(isValidMnemonic(mnemonic)).toBe(true) | ||
| }) | ||
|
|
||
| it('should generate a 18-word mnemonic (192 bits)', () => { | ||
| const mnemonic = generateMnemonicPhrase({ strength: 192 }) | ||
| const words = mnemonic.split(' ') | ||
|
|
||
| expect(words.length).toBe(18) | ||
| expect(isValidMnemonic(mnemonic)).toBe(true) | ||
| }) | ||
|
|
||
| it('should generate a 21-word mnemonic (224 bits)', () => { | ||
| const mnemonic = generateMnemonicPhrase({ strength: 224 }) | ||
| const words = mnemonic.split(' ') | ||
|
|
||
| expect(words.length).toBe(21) | ||
| expect(isValidMnemonic(mnemonic)).toBe(true) | ||
| }) | ||
|
|
||
| it('should generate a 24-word mnemonic (256 bits)', () => { | ||
| const mnemonic = generateMnemonicPhrase({ strength: 256 }) | ||
| const words = mnemonic.split(' ') | ||
|
|
||
| expect(words.length).toBe(24) | ||
| expect(isValidMnemonic(mnemonic)).toBe(true) | ||
| }) | ||
|
|
||
| it('should generate different mnemonics each time', () => { | ||
| const mnemonic1 = generateMnemonicPhrase() | ||
| const mnemonic2 = generateMnemonicPhrase() | ||
|
|
||
| expect(mnemonic1).not.toBe(mnemonic2) | ||
| }) | ||
|
|
||
| it('should throw error for invalid strength', () => { | ||
| expect(() => generateMnemonicPhrase({ strength: 100 as any })).toThrow( | ||
| 'Invalid strength. Must be 128, 160, 192, 224, or 256', | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe('isValidMnemonic', () => { | ||
| it('should return true for valid mnemonic', () => { | ||
| const mnemonic = generateMnemonicPhrase() | ||
| expect(isValidMnemonic(mnemonic)).toBe(true) | ||
| }) | ||
|
|
||
| it('should return false for invalid mnemonic', () => { | ||
| expect(isValidMnemonic('invalid mnemonic phrase test')).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false for empty string', () => { | ||
| expect(isValidMnemonic('')).toBe(false) | ||
| }) | ||
|
|
||
| it('should return false for mnemonic with wrong checksum', () => { | ||
| const validMnemonic = | ||
| 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about' | ||
| const words = validMnemonic.split(' ') | ||
| words[words.length - 1] = 'zoo' | ||
| const invalidMnemonic = words.join(' ') | ||
|
|
||
| expect(isValidMnemonic(invalidMnemonic)).toBe(false) | ||
| }) | ||
| }) | ||
|
|
||
| describe('mnemonicToPrivateKey', () => { | ||
| const testMnemonic = | ||
| 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about' | ||
|
|
||
| it('should derive private key from mnemonic with default path', () => { | ||
| const privateKey = mnemonicToPrivateKey(testMnemonic) | ||
|
|
||
| expect(privateKey).toBeDefined() | ||
| expect(privateKey.bytes.length).toBe(32) | ||
| }) | ||
|
|
||
| it('should derive private key with custom path', () => { | ||
| const privateKey1 = mnemonicToPrivateKey(testMnemonic, { | ||
| path: "m/44'/690'/0'/0'/0'", | ||
| }) | ||
| const privateKey2 = mnemonicToPrivateKey(testMnemonic, { | ||
| path: "m/44'/690'/0'/0'/1'", | ||
| }) | ||
|
|
||
| expect(privateKey1.toHex()).not.toBe(privateKey2.toHex()) | ||
| }) | ||
|
|
||
| it('should derive private key with passphrase', () => { | ||
| const privateKey1 = mnemonicToPrivateKey(testMnemonic) | ||
| const privateKey2 = mnemonicToPrivateKey(testMnemonic, { | ||
| passphrase: 'my-passphrase', | ||
| }) | ||
|
|
||
| expect(privateKey1.toHex()).not.toBe(privateKey2.toHex()) | ||
| }) | ||
|
|
||
| it('should be deterministic for same mnemonic and path', () => { | ||
| const privateKey1 = mnemonicToPrivateKey(testMnemonic) | ||
| const privateKey2 = mnemonicToPrivateKey(testMnemonic) | ||
|
|
||
| expect(privateKey1.toHex()).toBe(privateKey2.toHex()) | ||
| }) | ||
|
|
||
| it('should throw error for invalid mnemonic', () => { | ||
| expect(() => mnemonicToPrivateKey('invalid mnemonic')).toThrow('Invalid mnemonic phrase') | ||
| }) | ||
| }) | ||
|
|
||
| describe('deriveMultipleKeys', () => { | ||
| const testMnemonic = | ||
| 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about' | ||
|
|
||
| it('should derive multiple keys', () => { | ||
| const keys = deriveMultipleKeys(testMnemonic, 5) | ||
|
|
||
| expect(keys.length).toBe(5) | ||
| expect(new Set(keys.map((k) => k.toHex())).size).toBe(5) // All unique | ||
| }) | ||
|
|
||
| it('should derive keys sequentially from index', () => { | ||
| const keys = deriveMultipleKeys(testMnemonic, 3, { | ||
| path: "m/44'/690'/0'/0'/0'", | ||
| }) | ||
|
|
||
| const key0 = mnemonicToPrivateKey(testMnemonic, { path: "m/44'/690'/0'/0'/0'" }) | ||
| const key1 = mnemonicToPrivateKey(testMnemonic, { path: "m/44'/690'/0'/0'/1'" }) | ||
| const key2 = mnemonicToPrivateKey(testMnemonic, { path: "m/44'/690'/0'/0'/2'" }) | ||
|
|
||
| expect(keys[0].toHex()).toBe(key0.toHex()) | ||
| expect(keys[1].toHex()).toBe(key1.toHex()) | ||
| expect(keys[2].toHex()).toBe(key2.toHex()) | ||
| }) | ||
|
|
||
| it('should derive keys starting from custom index', () => { | ||
| const keys = deriveMultipleKeys(testMnemonic, 2, { | ||
| path: "m/44'/690'/0'/0'/5'", | ||
| }) | ||
|
|
||
| const key5 = mnemonicToPrivateKey(testMnemonic, { path: "m/44'/690'/0'/0'/5'" }) | ||
| const key6 = mnemonicToPrivateKey(testMnemonic, { path: "m/44'/690'/0'/0'/6'" }) | ||
|
|
||
| expect(keys[0].toHex()).toBe(key5.toHex()) | ||
| expect(keys[1].toHex()).toBe(key6.toHex()) | ||
| }) | ||
|
|
||
| it('should derive keys with passphrase', () => { | ||
| const keys1 = deriveMultipleKeys(testMnemonic, 2) | ||
| const keys2 = deriveMultipleKeys(testMnemonic, 2, { passphrase: 'test' }) | ||
|
|
||
| expect(keys1[0].toHex()).not.toBe(keys2[0].toHex()) | ||
| expect(keys1[1].toHex()).not.toBe(keys2[1].toHex()) | ||
| }) | ||
|
|
||
| it('should throw error for count less than 1', () => { | ||
| expect(() => deriveMultipleKeys(testMnemonic, 0)).toThrow('Count must be at least 1') | ||
| expect(() => deriveMultipleKeys(testMnemonic, -1)).toThrow('Count must be at least 1') | ||
| }) | ||
| }) | ||
|
|
||
| describe('buildDerivationPath', () => { | ||
| it('should build default path (0/0/0)', () => { | ||
| const path = buildDerivationPath() | ||
|
|
||
| expect(path).toBe(`m/44'/${KLEVER_COIN_TYPE}'/0'/0'/0'`) | ||
| }) | ||
|
|
||
| it('should build path with custom account', () => { | ||
| const path = buildDerivationPath(5) | ||
|
|
||
| expect(path).toBe(`m/44'/${KLEVER_COIN_TYPE}'/5'/0'/0'`) | ||
| }) | ||
|
|
||
| it('should build path with custom account and change', () => { | ||
| const path = buildDerivationPath(2, 1) | ||
|
|
||
| expect(path).toBe(`m/44'/${KLEVER_COIN_TYPE}'/2'/1'/0'`) | ||
| }) | ||
|
|
||
| it('should build path with custom account, change, and index', () => { | ||
| const path = buildDerivationPath(3, 0, 10) | ||
|
|
||
| expect(path).toBe(`m/44'/${KLEVER_COIN_TYPE}'/3'/0'/10'`) | ||
| }) | ||
|
|
||
| it('should use KLEVER_COIN_TYPE constant', () => { | ||
| const path = buildDerivationPath(0, 0, 0) | ||
|
|
||
| expect(path).toContain(`44'/${KLEVER_COIN_TYPE}'`) | ||
| }) | ||
|
|
||
| it('should throw error for negative account', () => { | ||
| expect(() => buildDerivationPath(-1)).toThrow('Account must be a non-negative integer') | ||
| }) | ||
|
|
||
| it('should throw error for non-integer account', () => { | ||
| expect(() => buildDerivationPath(1.5)).toThrow('Account must be a non-negative integer') | ||
| }) | ||
|
|
||
| it('should throw error for invalid change value', () => { | ||
| expect(() => buildDerivationPath(0, -1)).toThrow( | ||
| 'Change must be 0 (external) or 1 (internal)', | ||
| ) | ||
| expect(() => buildDerivationPath(0, 2)).toThrow('Change must be 0 (external) or 1 (internal)') | ||
| expect(() => buildDerivationPath(0, 0.5)).toThrow( | ||
| 'Change must be 0 (external) or 1 (internal)', | ||
| ) | ||
| }) | ||
|
|
||
| it('should throw error for negative index', () => { | ||
| expect(() => buildDerivationPath(0, 0, -1)).toThrow('Index must be a non-negative integer') | ||
| }) | ||
|
|
||
| it('should throw error for non-integer index', () => { | ||
| expect(() => buildDerivationPath(0, 0, 1.5)).toThrow('Index must be a non-negative integer') | ||
| }) | ||
| }) | ||
|
|
||
| describe('DEFAULT_DERIVATION_PATH', () => { | ||
| it('should be a valid BIP44 path', () => { | ||
| expect(DEFAULT_DERIVATION_PATH).toBe("m/44'/690'/0'/0'/0'") | ||
| }) | ||
| }) | ||
|
|
||
| describe('KLEVER_COIN_TYPE', () => { | ||
| it('should be 690', () => { | ||
| expect(KLEVER_COIN_TYPE).toBe(690) | ||
| }) | ||
| }) | ||
| }) |
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.
Uh oh!
There was an error while loading. Please reload this page.