-
Notifications
You must be signed in to change notification settings - Fork 25
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
Showing
14 changed files
with
471 additions
and
628 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export * from './kadenaGenKeypairFromSeed'; | ||
export * from './kadenaGetPublic'; | ||
export * from './kadenaKeyPairsFromRandom'; | ||
export * from './mnemonic'; | ||
export * from './sign'; | ||
export * from './kadenaMnemonic'; | ||
export * from './kadenaSign'; |
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
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
78 changes: 78 additions & 0 deletions
78
packages/libs/hd-wallet/src/bip44/tests/kadenaGenKeypairFromSeed.test.ts
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,78 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { | ||
kadenaGenKeypairFromSeed, | ||
kadenaGenMnemonic, | ||
kadenaMnemonicToSeed, | ||
} from '../'; | ||
|
||
import { kadenaDecrypt } from '../../utils/kadenaEncryption'; | ||
|
||
describe('kadenaGenKeypairFromSeed', () => { | ||
it('should generate an encrypted keypair from the seedBuffer when a password is provided', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
const [publicKey, encryptedPrivateKey] = kadenaGenKeypairFromSeed( | ||
password, | ||
seed, | ||
0, | ||
); | ||
|
||
expect(publicKey).toHaveLength(64); | ||
expect(typeof encryptedPrivateKey).toBe('string'); // Checks if privateKey is a string, thus encrypted | ||
}); | ||
|
||
it('should generate a range of keypairs from the seed', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
const keyPairs = kadenaGenKeypairFromSeed(password, seed, [0, 3]); | ||
expect(keyPairs).toHaveLength(4); | ||
keyPairs.forEach(([publicKey, privateKey]) => { | ||
expect(publicKey).toHaveLength(64); | ||
expect( | ||
Buffer.from(kadenaDecrypt(password, privateKey)).toString('hex'), | ||
).toHaveLength(64); | ||
}); | ||
}); | ||
|
||
it('should throw an error for out-of-bounds index values', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
const outOfBoundsIndex = -1; | ||
|
||
expect(() => { | ||
kadenaGenKeypairFromSeed(password, seed, outOfBoundsIndex); | ||
}).toThrowError('Invalid child index: -1'); | ||
}); | ||
|
||
it('returns an encrypted private key that can be decrypted with the password', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
const [, encryptedPrivateKey] = kadenaGenKeypairFromSeed(password, seed, 0); | ||
const decryptedPrivateKey = kadenaDecrypt(password, encryptedPrivateKey); | ||
expect(decryptedPrivateKey).toBeTruthy(); | ||
expect(Buffer.from(decryptedPrivateKey).toString('hex')).toHaveLength(64); | ||
}); | ||
|
||
// it('should handle the highest non-hardened index without throwing errors', async () => { | ||
// const mnemonic = kadenaGenMnemonic(); | ||
// const { seedBuffer } = await kadenaMnemonicToSeed(mnemonic); | ||
|
||
// /* | ||
// * HD wallets as per BIP32 spec define two types of indices: | ||
// * - Non-hardened (ranging from 0 to 2^31 - 1) | ||
// * - Hardened (ranging from 2^31 to 2^32 - 1). | ||
// * The highest non-hardened index is therefore 2^31 - 1, | ||
// * which is the largest 32-bit integer that can be used for generating non-hardened keys. | ||
// */ | ||
|
||
// const highestNonHardenedIndex = 2 ** 31 - 1; | ||
// expect(() => { | ||
// kadenaGenKeypairFromSeed(seedBuffer, highestNonHardenedIndex); | ||
// }).not.toThrow(); | ||
// }); | ||
}); |
57 changes: 57 additions & 0 deletions
57
packages/libs/hd-wallet/src/bip44/tests/kadenaGetPublic.test.ts
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,57 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { kadenaGenMnemonic, kadenaGetPublic, kadenaMnemonicToSeed } from '..'; | ||
|
||
describe('kadenaGetPublic', () => { | ||
it('should retrieve the public key from seed and the given index', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
|
||
const publicKeyIndex0 = kadenaGetPublic(password, seed, 0); | ||
const publicKeyIndex1 = kadenaGetPublic(password, seed, 1); | ||
|
||
expect(publicKeyIndex0).toHaveLength(64); | ||
expect(publicKeyIndex1).toHaveLength(64); | ||
expect(publicKeyIndex1).not.toBe(publicKeyIndex0); | ||
}); | ||
|
||
it('should retrieve distinct public keys from seedBuffer for different indexes', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
|
||
const indexes = [0, 1, 2, 3, 4]; | ||
const publicKeys = indexes.map((index) => | ||
kadenaGetPublic(password, seed, index), | ||
); | ||
|
||
publicKeys.forEach((publicKey) => { | ||
expect(publicKey).toHaveLength(64); | ||
}); | ||
|
||
const uniquePublicKeys = new Set(publicKeys); // Check that all public keys are unique | ||
expect(uniquePublicKeys.size).toBe(indexes.length); | ||
}); | ||
|
||
it('should get the similar public keys as Enkrypt for the same path', async () => { | ||
const password = 'pass'; | ||
const seed = await kadenaMnemonicToSeed( | ||
password, | ||
// this mnemonic is generated by Enkrypt wallet | ||
'coyote utility final warfare thumb symbol mule scale final nominee behave crumble', | ||
); | ||
let publicKey = kadenaGetPublic(password, seed, 0); | ||
expect(publicKey).toBe( | ||
'43726c4a2e7b03fa5d23635307e5b130baf8b261e1081c099a2b43db1d4554cc', | ||
); | ||
publicKey = kadenaGetPublic(password, seed, 1); | ||
expect(publicKey).toBe( | ||
'3f53dfad097fdf8501c32b275e109980ed7121866a63ca34bb035c4a2e41a265', | ||
); | ||
publicKey = kadenaGetPublic(password, seed, 2); | ||
expect(publicKey).toBe( | ||
'3021bcfa703cc4fac007ab4c5050df5c0b8ca7d655ea80c84af9ea5e43ecf0ff', | ||
); | ||
}); | ||
}); |
45 changes: 45 additions & 0 deletions
45
packages/libs/hd-wallet/src/bip44/tests/kadenaMnemonic.test.ts
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,45 @@ | ||
import { describe, expect, it } from 'vitest'; | ||
|
||
import { kadenaGenMnemonic, kadenaMnemonicToSeed } from '../'; | ||
|
||
import { kadenaDecrypt } from '../../utils/kadenaEncryption'; | ||
|
||
describe('kadenaGenMnemonic', () => { | ||
it('should generate a valid mnemonic', () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
expect(mnemonic.split(' ')).toHaveLength(12); | ||
}); | ||
}); | ||
|
||
describe('kadenaMnemonicToSeed', () => { | ||
it('should convert mnemonic to encrypt seed with a password', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
expect(typeof seed).toBe('string'); // Check if the seed is a string, indicating it has been encrypted | ||
}); | ||
|
||
it('returns encrypted seed that can be decrypted with the password', async () => { | ||
const mnemonic = kadenaGenMnemonic(); | ||
const password = 'password'; | ||
const seed = await kadenaMnemonicToSeed(password, mnemonic); | ||
const decryptedSeed = kadenaDecrypt(password, seed); | ||
expect(decryptedSeed).toBeTruthy(); | ||
}); | ||
|
||
it('should throw an error for an invalid mnemonic', async () => { | ||
const invalidMnemonic = 'this is not a valid mnemonic'; | ||
|
||
await expect( | ||
kadenaMnemonicToSeed('password', invalidMnemonic), | ||
).rejects.toThrowError('Invalid mnemonic.'); | ||
}); | ||
|
||
it('should throw an error when mnemonic is empty', async () => { | ||
const emptyMnemonic = ''; | ||
|
||
await expect( | ||
kadenaMnemonicToSeed('password', emptyMnemonic), | ||
).rejects.toThrowError('Invalid mnemonic.'); | ||
}); | ||
}); |
Oops, something went wrong.