@theqrl/wallet.js 0.0.1 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,32 +1,93 @@
1
- import { WordList } from "../qrl/wordlist.js";
1
+ const { WordList } = require('../qrl/wordlist.js');
2
2
 
3
- export function SeedBinToMnemonic(input) {
4
- return binToMnemonic(input);
3
+ function binToMnemonic(input) {
4
+ if (input.length % 3 !== 0) {
5
+ throw new Error('byte count needs to be a multiple of 3');
6
+ }
7
+ let mnemonic = '';
8
+ let separator = '';
9
+ for (let nibble = 0; nibble < input.length * 2; nibble += 3) {
10
+ const p = nibble >> 1;
11
+ const b1 = input[p];
12
+ let b2 = 0;
13
+ if (p + 1 < input.length) {
14
+ b2 = input[p + 1];
15
+ }
16
+ let idx = 0;
17
+ if (nibble % 2 === 0) {
18
+ idx = (b1 << 4) + (b2 >> 4);
19
+ } else {
20
+ idx = ((b1 & 0x0f) << 8) + b2;
21
+ }
22
+ mnemonic += separator + WordList[idx];
23
+ separator = ' ';
24
+ }
25
+ return mnemonic;
5
26
  }
6
27
 
7
- function binToMnemonic(input) {
8
- if (String(input).length % 3 != 0) {
9
- console.error("byte count needs to be a multiple of 3");
28
+ function SeedBinToMnemonic(input) {
29
+ return binToMnemonic(input);
30
+ }
31
+
32
+ function mnemonicToBin(mnemonic) {
33
+ const mnemonicWords = mnemonic.split(' ');
34
+ const wordCount = mnemonicWords.length;
35
+ if (wordCount % 2 !== 0) {
36
+ throw new Error('word count must be even');
37
+ }
38
+
39
+ const wordLookup = {};
40
+ WordList.map((word, i) => {
41
+ wordLookup[word] = i;
42
+ return word;
43
+ });
44
+
45
+ const result = new Uint8Array((wordCount * 15) / 10);
46
+
47
+ let current = 0;
48
+ let buffering = 0;
49
+ let resultIndex = 0;
50
+
51
+ mnemonicWords.map((w) => {
52
+ const value = wordLookup[w];
53
+ if (value === undefined || value === null) {
54
+ throw new Error('invalid word in mnemonic');
10
55
  }
11
- let mnemonic = ''
12
- var separator = "";
13
- let buf = Buffer.alloc(input.length * 4);
14
- for (let nibble = 0; nibble < input.length * 2; nibble += 3) {
15
- let p = nibble >> 1;
16
- let b1 = input[p];
17
- let b2 = 0;
18
- if ((p + 1) < input.length) {
19
- b2 = input[p + 1]
20
- }
21
- let idx = 0;
22
- if (nibble % 2 == 0) {
23
- idx = (b1 << 4) + (b2 >> 4);
24
- } else {
25
- idx = ((b1 & 0x0F) << 8) + b2;
26
- }
27
- mnemonic += separator + WordList[idx];
28
- separator = " ";
56
+
57
+ buffering += 3;
58
+ current = (current << 12) + value;
59
+ let shift;
60
+ let mask;
61
+ let tmp;
62
+ for (; buffering > 2; ) {
63
+ shift = 4 * (buffering - 2);
64
+ mask = (1 << shift) - 1;
65
+ tmp = current >> shift;
66
+ buffering -= 2;
67
+ current &= mask;
68
+ result[resultIndex] = tmp;
69
+ resultIndex++;
29
70
  }
30
- // return buf.toString()
31
- return mnemonic;
32
- }
71
+ return w;
72
+ });
73
+
74
+ if (buffering > 0) {
75
+ result[resultIndex] = current & 0xff;
76
+ resultIndex++;
77
+ }
78
+ return result;
79
+ }
80
+
81
+ function MnemonicToSeedBin(mnemonic) {
82
+ const output = mnemonicToBin(mnemonic);
83
+
84
+ if (output.length !== 48) {
85
+ throw new Error('unexpected MnemonicToSeedBin output size');
86
+ }
87
+
88
+ const sizedOutput = new Uint8Array(48);
89
+ sizedOutput.set(output);
90
+ return output;
91
+ }
92
+
93
+ module.exports = { SeedBinToMnemonic, MnemonicToSeedBin };
package/src/index.js DELETED
@@ -1 +0,0 @@
1
- export default './dilithium.js'
package/src/qrl/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './wordlist.js'