-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
66 lines (51 loc) · 2.14 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import nacl from 'tweetnacl'
import {
randomMnemonicPassphrase,
mnemonicPassphrase,
entropyFromPassphrase
} from './index.mjs'
const INVALID_ENTROPY = 'Invalid entropy'
const INVALID_CHECKSUM = 'Invalid mnemonic checksum'
describe('mnemonic-passphrase', () => {
const mnemonicLength = 24
it('generates random mnemonic', () => {
const mnemonic = randomMnemonicPassphrase()
expect(mnemonic).toBeDefined()
expect(mnemonic.split(' ').length).toBe(mnemonicLength)
})
it('generates a mnemonic from entropy', () => {
const entropy = nacl.randomBytes(10)
const mnemonic = mnemonicPassphrase(entropy)
expect(mnemonic).toBeDefined()
})
it('mnemonic is reversable', () => {
const entropy = nacl.randomBytes(nacl.secretbox.keyLength)
const entropyBuffer = Buffer.from(entropy)
const entropyHex = entropyBuffer.toString('hex')
const mnemonic = mnemonicPassphrase(entropy)
const reversed = entropyFromPassphrase(mnemonic)
expect(reversed).toBe(entropyHex)
})
it('minimal entropy is 16 and must be divisible by 4', () => {
const entropy = nacl.randomBytes(16)
const mnemonic = mnemonicPassphrase(entropy)
const reversed = entropyFromPassphrase(mnemonic)
const entropy15 = nacl.randomBytes(15)
const mnemonic15 = mnemonicPassphrase(entropy15)
expect(() => entropyFromPassphrase(mnemonic15)).toThrow(new Error(INVALID_CHECKSUM))
const entropy12 = nacl.randomBytes(12)
const mnemonic12 = mnemonicPassphrase(entropy12)
expect(() => entropyFromPassphrase(mnemonic12)).toThrow(new Error(INVALID_ENTROPY))
})
it('max entropy is 32 and must be divisible by 4', () => {
const entropy = nacl.randomBytes(32)
const mnemonic = mnemonicPassphrase(entropy)
const reversed = entropyFromPassphrase(mnemonic)
const entropy33 = nacl.randomBytes(33)
const mnemonic33 = mnemonicPassphrase(entropy33)
expect(() => entropyFromPassphrase(mnemonic33)).toThrow(new Error(INVALID_CHECKSUM))
const entropy36 = nacl.randomBytes(36)
const mnemonic36 = mnemonicPassphrase(entropy36)
expect(() => entropyFromPassphrase(mnemonic36)).toThrow(new Error(INVALID_ENTROPY))
})
})