1 | var assert = require('assert'), |
||
2 | bip39 = require('bip39'); |
||
3 | |||
4 | if (!String.prototype.repeat) { |
||
5 | /*jshint -W121 */ |
||
6 | String.prototype.repeat = function(count) { |
||
0 ignored issues
–
show
Compatibility
Best Practice
introduced
by
![]() |
|||
7 | 'use strict'; |
||
8 | if (this === null) { |
||
9 | throw new TypeError('can\'t convert ' + this + ' to object'); |
||
10 | } |
||
11 | var str = '' + this; |
||
12 | count = +count; |
||
13 | if (count !== count) { |
||
14 | count = 0; |
||
15 | } |
||
16 | if (count < 0) { |
||
17 | throw new RangeError('repeat count must be non-negative'); |
||
18 | } |
||
19 | if (count === Infinity) { |
||
20 | throw new RangeError('repeat count must be less than infinity'); |
||
21 | } |
||
22 | count = Math.floor(count); |
||
23 | if (str.length === 0 || count === 0) { |
||
24 | return ''; |
||
25 | } |
||
26 | // Ensuring count is a 31-bit integer allows us to heavily optimize the |
||
27 | // main part. But anyway, most current (August 2014) browsers can't handle |
||
28 | // strings 1 << 28 chars or longer, so: |
||
29 | if (str.length * count >= 1 << 28) { |
||
30 | throw new RangeError('repeat count must not overflow maximum string size'); |
||
31 | } |
||
32 | var rpt = ''; |
||
33 | for (;;) { |
||
34 | if ((count & 1) === 1) { |
||
35 | rpt += str; |
||
36 | } |
||
37 | count >>>= 1; |
||
38 | if (count === 0) { |
||
39 | break; |
||
40 | } |
||
41 | str += str; |
||
42 | } |
||
43 | // Could we try: |
||
44 | // return Array(count + 1).join(this); |
||
45 | return rpt; |
||
46 | }; |
||
47 | } |
||
48 | |||
49 | var EncryptionMnemonic = { |
||
50 | chunkSize: 4, |
||
51 | paddingDummy: 0x81 /* because salts with length > 128 should be forbidden? */ |
||
52 | }; |
||
53 | |||
54 | var derivePadding = function(data) { |
||
55 | if (data[0] > 0x80) { |
||
56 | throw new Error('Mnemonic sanity check - first byte can never be above 0x80'); |
||
57 | } |
||
58 | |||
59 | return EncryptionMnemonic.paddingDummy.toString(16).repeat(EncryptionMnemonic.chunkSize - data.length % EncryptionMnemonic.chunkSize); |
||
60 | }; |
||
61 | |||
62 | EncryptionMnemonic.encode = function(data) { |
||
63 | assert(data instanceof Buffer, 'Data must be provided as a Buffer'); |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
64 | |||
65 | var padding = derivePadding(data); |
||
66 | var mnemonic = bip39.entropyToMnemonic(padding + data.toString('hex')); |
||
67 | |||
68 | try { |
||
69 | bip39.mnemonicToEntropy(mnemonic); |
||
70 | } catch (e) { |
||
71 | throw new Error('BIP39 library produced an invalid mnemonic'); |
||
72 | } |
||
73 | |||
74 | return mnemonic; |
||
75 | }; |
||
76 | |||
77 | EncryptionMnemonic.decode = function(mnemonic) { |
||
78 | assert(typeof mnemonic === 'string', 'Mnemonic must be provided as a string'); |
||
79 | |||
80 | var decoded = new Buffer(bip39.mnemonicToEntropy(mnemonic), 'hex'); |
||
0 ignored issues
–
show
The variable
Buffer seems to be never declared. If this is a global, consider adding a /** global: Buffer */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
81 | var padFinish = 0; |
||
82 | while (decoded[padFinish] === this.paddingDummy) { |
||
83 | padFinish++; |
||
84 | } |
||
85 | |||
86 | var data = decoded.slice(padFinish, decoded.length); |
||
87 | if (derivePadding(data) !== decoded.slice(0, padFinish).toString('hex')) { |
||
88 | throw new Error('There is only one way to pad a string'); |
||
89 | } |
||
90 | |||
91 | return data; |
||
92 | }; |
||
93 | |||
94 | module.exports = EncryptionMnemonic; |
||
95 |