eff-mnemonic 0.0.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.
package/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # EFF Large Mnemonic
2
+
3
+ Convert buffers to and from a human-readable mnemonic phrase using the eff wordlists. This library was built with the purpose of generating mnemonics from passwords or private keys so that they can be easily written down on paper.
4
+
5
+ ## CLI Usage
6
+
7
+ You can use the cli to encode and decode secret phrases or files.
8
+
9
+ #### Encode in interactive mode
10
+
11
+ ```sh
12
+ > ./cli
13
+ 🔑 # enter your secret phrase
14
+ ┌─────────┬────────────┬───────────┬────────────┬──────────┬───────────┐
15
+ │ (index) │ 0 │ 1 │ 2 │ 3 │ 4 │
16
+ ├─────────┼────────────┼───────────┼────────────┼──────────┼───────────┤
17
+ │ 0 │ 'bloomers' │ 'triceps' │ 'shoptalk' │ 'travel' │ 'prodigy' │
18
+ │ 1 │ 'outlast' │ 'shrank' │ │ │ │
19
+ └─────────┴────────────┴───────────┴────────────┴──────────┴───────────┘
20
+ ```
21
+
22
+ #### Decode in interactive mode
23
+
24
+ ```sh
25
+ ./cli -d
26
+ 🔎 bloomers triceps shoptalk travel prodigy outlast shranK
27
+ hello world%
28
+ ```
29
+
30
+ #### Using pipes and redirection
31
+
32
+ ```sh
33
+ # the cli pretty prints the mnemonic if output is not redirected
34
+ > echo -n "hello" | ./cli
35
+ ┌─────────┬───────────┬─────────┬─────────┐
36
+ │ (index) │ 0 │ 1 │ 2 │
37
+ ├─────────┼───────────┼─────────┼─────────┤
38
+ │ 0 │ 'uranium' │ 'frown' │ 'vowed' │
39
+ └─────────┴───────────┴─────────┴─────────┘
40
+
41
+ # the cli plays nice when the output is piped
42
+ > echo -n "hello" | ./cli | ./cli -d | cat
43
+ hello%
44
+
45
+ # the cli plays nice when the output is redirected
46
+ > echo -n "hello" | ./cli | ./cli -d > output.txt
47
+ ```
48
+
49
+ ## Programmatic Usage
50
+
51
+ Use `bufferToMnemonic` to encode and `mnemonicToBuffer` to decode.
52
+
53
+ ```ts
54
+ import { bufferToMnemonic, mnemonicToBuffer } from "./dist/index.mjs";
55
+
56
+ const input = "hello world";
57
+ const encoded = await bufferToMnemonic(Buffer.from(input, "utf8"));
58
+
59
+ console.log(encoded.join(" ")); // bloomers triceps shoptalk travel prodigy outlast shrank
60
+
61
+ const decoded = (await mnemonicToBuffer(encoded)).toString("utf8");
62
+
63
+ console.log(decoded); // hello world
64
+ ```
65
+
66
+ ### bufferToMnemonic(buffer, type)
67
+
68
+ - `buffer` `<Buffer>` The buffer to encode.
69
+ - `type` `<"large" | "short_1" | "short_2_0">` The eff wordlist to use, default `"large"`.
70
+ - Returns: `<string[]>` An array of mnemonic words.
71
+
72
+ Encodes abuffer into a human-readable mnemonic word array from the eff wordlist.
73
+
74
+ ### mnemonicToBuffer(buffer, type)
75
+
76
+ Arguments
77
+
78
+ - `words` `<string[]>` An array of mnemonic words.
79
+ - `type` `<"large" | "short_1" | "short_2_0">` The eff wordlist to use, default `"large"`
80
+ - Returns: `<Buffer>` The decoded buffer.
81
+
82
+ Decodes a mnemonic word array back into its original buffer.
83
+
84
+ ## Quirks
85
+
86
+ Internally, null characters `0x00` are used to pad the input if it's not divisible by 5. So the decoding process strips preceeding null characters before returning the output.
87
+
88
+ #### Default behaviour
89
+
90
+ ```ts
91
+ const input = "\x00\x00hello world";
92
+ const encoded = await bufferToMnemonic(Buffer.from(input, "utf8"));
93
+ const decoded = (await mnemonicToBuffer(encoded)).toString("utf8");
94
+
95
+ console.dir(decoded); // "hello world"
96
+ console.log(input === decoded); // false
97
+ ```
98
+
99
+ #### Preserve preceeding null characters
100
+
101
+ ```ts
102
+ const input = "\0\0hello world";
103
+
104
+ // prepend a non zero byte such as 0xff
105
+ const encoded = await bufferToMnemonic(
106
+ Buffer.concat([Buffer.from([0xff]), Buffer.from(input, "utf8")]),
107
+ );
108
+
109
+ // discard the prepended byte
110
+ const decoded = (await mnemonicToBuffer(encoded)).subarray(1).toString("utf8");
111
+
112
+ console.dir(decoded); // "\x00\x00hello world"
113
+ console.log(input === decoded); // true
114
+ ```
115
+
116
+ ## References
117
+
118
+ - [EFF's New Wordlists for Random Passphrases](https://www.eff.org/deeplinks/2016/07/new-wordlists-random-passphrases)
119
+ - [EFF's long word list (for use with five dice)](https://www.eff.org/files/2016/07/18/eff_large_wordlist.txt)
120
+ - [EFF's general short word list (for use with four dice)](https://www.eff.org/files/2016/09/08/eff_short_wordlist_1.txt)
121
+ - [EFF's short word list (with words that have unique three-character prefixes)](https://www.eff.org/files/2016/09/08/eff_short_wordlist_2_0.txt)