@leofcoin/peernet 0.18.8 → 1.0.0

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/exports/store.js CHANGED
@@ -1,229 +1,227 @@
1
1
  import { join } from 'path';
2
- import { homedir, platform } from 'os';
2
+ import { homedir } from 'os';
3
3
  import { readdirSync } from 'fs';
4
- import { execSync } from 'child_process';
4
+ import { mkdirp } from 'mkdirp';
5
5
  import { ClassicLevel } from 'classic-level';
6
6
 
7
- // import base32 from '@vandeurenglenn/base32'
8
- // import base58 from '@vandeurenglenn/base58'
9
-
10
- // export const encodings = {
11
- // base58,
12
- // base32
13
- // }
14
-
15
- const encode$1 = (string, encoding = 'utf-8') => {
16
- if (typeof string === 'string') {
17
- let encoded;
18
-
19
- // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
20
- encoded = new TextEncoder().encode(string);
21
- return encoded
22
- }
23
- throw Error(`expected typeof String instead got ${string}`)
24
- };
25
-
26
- const decode$1 = (uint8Array, encoding) => {
27
- if (uint8Array instanceof Uint8Array) {
28
- let decoded;
29
- // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
30
- decoded = new TextDecoder().decode(uint8Array);
31
-
32
- return decoded
33
- }
34
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
7
+ // import base32 from '@vandeurenglenn/base32'
8
+ // import base58 from '@vandeurenglenn/base58'
9
+
10
+ // export const encodings = {
11
+ // base58,
12
+ // base32
13
+ // }
14
+
15
+ const encode$1 = (string, encoding = 'utf-8') => {
16
+ if (typeof string === 'string') {
17
+ let encoded;
18
+
19
+ // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
20
+ encoded = new TextEncoder().encode(string);
21
+ return encoded
22
+ }
23
+ throw Error(`expected typeof String instead got ${string}`)
24
+ };
25
+
26
+ const decode$1 = (uint8Array, encoding) => {
27
+ if (uint8Array instanceof Uint8Array) {
28
+ let decoded;
29
+ // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
30
+ decoded = new TextDecoder().decode(uint8Array);
31
+
32
+ return decoded
33
+ }
34
+ throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
35
35
  };
36
36
 
37
- class KeyValue {
38
-
39
- /**
40
- * @param {string | Uint8Array} input
41
- */
42
- constructor(input) {
43
- if (typeof input === 'string') {
44
- this.uint8Array = encode$1(input);
45
- } else if (input instanceof Uint8Array) {
46
- this.uint8Array = input;
47
- } else if (input instanceof KeyValue) {
48
- this.uint8Array = input.uint8Array;
49
- } else {
50
- throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue')
51
- }
52
- }
53
-
54
- isKeyValue() {
55
- return true
56
- }
57
-
58
- /**
59
- * Convert to the string representation
60
- *
61
- * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
62
- * @returns {string}
63
- */
64
- toString(encoding = 'utf8') {
65
- return decode$1(this.uint8Array)
66
- }
67
-
37
+ class KeyValue {
38
+
39
+ /**
40
+ * @param {string | Uint8Array} input
41
+ */
42
+ constructor(input) {
43
+ if (typeof input === 'string') {
44
+ this.uint8Array = encode$1(input);
45
+ } else if (input instanceof Uint8Array) {
46
+ this.uint8Array = input;
47
+ } else if (input instanceof KeyValue) {
48
+ this.uint8Array = input.uint8Array;
49
+ } else {
50
+ throw new Error('Invalid KeyValue, should be a String, Uint8Array or KeyValue')
51
+ }
52
+ }
53
+
54
+ isKeyValue() {
55
+ return true
56
+ }
57
+
58
+ /**
59
+ * Convert to the string representation
60
+ *
61
+ * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
62
+ * @returns {string}
63
+ */
64
+ toString(encoding = 'utf8') {
65
+ return decode$1(this.uint8Array)
66
+ }
67
+
68
68
  }
69
69
 
70
- const mkdirp = path => execSync(`mkdir "${platform() === 'win32' ? path.replace(/\//g, '\\') : path}"`);
71
-
72
- const init = (root, home = true) => {
73
- let _root;
74
- if (home) _root = join(homedir(), root);
75
- if (readdirSync) try {
76
- readdirSync(_root);
77
- } catch (e) {
78
- mkdirp(_root);
79
- }
80
-
81
- return _root
70
+ const init = (root, home = true) => {
71
+ let _root = root;
72
+ if (home) _root = join(homedir(), root);
73
+ if (readdirSync) try {
74
+ readdirSync(_root);
75
+ } catch (e) {
76
+ mkdirp(_root);
77
+ }
78
+
79
+ return _root
82
80
  };
83
81
 
84
- // import base32 from '@vandeurenglenn/base32'
85
- // import base58 from '@vandeurenglenn/base58'
86
-
87
- // export const encodings = {
88
- // base58,
89
- // base32
90
- // }
91
-
92
- const encode = (string, encoding = 'utf-8') => {
93
- if (typeof string === 'string') {
94
- let encoded;
95
-
96
- // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
97
- encoded = new TextEncoder().encode(string);
98
- return encoded
99
- }
100
- throw Error(`expected typeof String instead got ${string}`)
101
- };
102
-
103
- const decode = (uint8Array, encoding) => {
104
- if (uint8Array instanceof Uint8Array) {
105
- let decoded;
106
- // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
107
- decoded = new TextDecoder().decode(uint8Array);
108
-
109
- return decoded
110
- }
111
- throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
82
+ // import base32 from '@vandeurenglenn/base32'
83
+ // import base58 from '@vandeurenglenn/base58'
84
+
85
+ // export const encodings = {
86
+ // base58,
87
+ // base32
88
+ // }
89
+
90
+ const encode = (string, encoding = 'utf-8') => {
91
+ if (typeof string === 'string') {
92
+ let encoded;
93
+
94
+ // if (encodings[encoding]) encoded = encodings[encoding].encode(encoded)
95
+ encoded = new TextEncoder().encode(string);
96
+ return encoded
97
+ }
98
+ throw Error(`expected typeof String instead got ${string}`)
112
99
  };
113
100
 
114
- const pathSepS = '/';
115
- class KeyPath {
116
-
117
- /**
118
- * @param {string | Uint8Array} input
119
- */
120
- constructor(input) {
121
- if (typeof input === 'string') {
122
- this.uint8Array = encode(input);
123
- } else if (input instanceof Uint8Array) {
124
- this.uint8Array = input;
125
- } else if (input instanceof KeyPath) {
126
- this.uint8Array = input.uint8Array;
127
- } else {
128
- throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath')
129
- }
130
- }
131
-
132
- isKeyPath() {
133
- return true
134
- }
135
-
136
- /**
137
- * Convert to the string representation
138
- *
139
- * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
140
- * @returns {string}
141
- */
142
- toString(encoding = 'hex') {
143
- return decode(this.uint8Array)
144
- }
145
-
146
- /**
147
- * Returns the `list` representation of this path.
148
- *
149
- * @returns string[]
150
- *
151
- * @example
152
- * ```js
153
- * new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
154
- * // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
155
- * ```
156
- */
157
- list() {
158
- return this.toString().split(pathSepS).slice(1)
159
- }
160
-
101
+ const decode = (uint8Array, encoding) => {
102
+ if (uint8Array instanceof Uint8Array) {
103
+ let decoded;
104
+ // if (encodings[encoding]) decoded = encodings[encoding].decode(decoded)
105
+ decoded = new TextDecoder().decode(uint8Array);
106
+
107
+ return decoded
108
+ }
109
+ throw Error(`expected typeof uint8Array instead got ${uint8Array}`)
110
+ };
111
+
112
+ const pathSepS = '/';
113
+ class KeyPath {
114
+
115
+ /**
116
+ * @param {string | Uint8Array} input
117
+ */
118
+ constructor(input) {
119
+ if (typeof input === 'string') {
120
+ this.uint8Array = encode(input);
121
+ } else if (input instanceof Uint8Array) {
122
+ this.uint8Array = input;
123
+ } else if (input instanceof KeyPath) {
124
+ this.uint8Array = input.uint8Array;
125
+ } else {
126
+ throw new Error('Invalid keyPath, should be a String, Uint8Array or KeyPath')
127
+ }
128
+ }
129
+
130
+ isKeyPath() {
131
+ return true
132
+ }
133
+
134
+ /**
135
+ * Convert to the string representation
136
+ *
137
+ * @param {import('uint8arrays/to-string').SupportedEncodings} [encoding='utf8'] - The encoding to use.
138
+ * @returns {string}
139
+ */
140
+ toString(encoding = 'hex') {
141
+ return decode(this.uint8Array)
142
+ }
143
+
144
+ /**
145
+ * Returns the `list` representation of this path.
146
+ *
147
+ * @returns string[]
148
+ *
149
+ * @example
150
+ * ```js
151
+ * new Key('/Comedy/MontyPython/Actor:JohnCleese').list()
152
+ * // => ['Comedy', 'MontyPythong', 'Actor:JohnCleese']
153
+ * ```
154
+ */
155
+ list() {
156
+ return this.toString().split(pathSepS).slice(1)
157
+ }
158
+
161
159
  }
162
160
 
163
- class Store {
164
- constructor(name = 'storage', root, version = 'v1.0.0') {
165
- this.name = name;
166
- this.root = init(root);
167
- this.version = version;
168
-
169
- this.db = new ClassicLevel(join(this.root, this.name), { valueEncoding: 'view'});
170
- }
171
-
172
- toKeyPath(key) {
173
- if (!key.isKeyPath()) key = new KeyPath(key);
174
- return key.toString('base32')
175
- }
176
-
177
- toKeyValue(value) {
178
- if (!value.isKeyValue()) value = new KeyValue(value);
179
- return value.uint8Array
180
- }
181
-
182
- toUint8Array(buffer) {
183
- return Buffer.isBuffer(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT) : buffer
184
- }
185
-
186
- async get(key) {
187
- return this.toUint8Array(await this.db.get(this.toKeyPath(key)))
188
- }
189
-
190
- async put(key, value) {
191
- return this.db.put(this.toKeyPath(key), this.toKeyValue(value))
192
- }
193
-
194
- async delete(key) {
195
- return this.db.del(this.toKeyPath(key))
196
- }
197
-
198
- async clear() {
199
- return this.db.clear()
200
- }
201
-
202
- async values(limit = -1) {
203
- const values = [];
204
- for await (const value of this.db.values({limit})) {
205
- values.push(this.toUint8Array(value));
206
- }
207
- return values
208
- }
209
-
210
- async keys(limit = -1) {
211
- const keys = [];
212
- for await (const key of this.db.keys({limit})) {
213
- keys.push(key);
214
- }
215
- return keys
216
- }
217
-
218
- /**
219
- *
220
- * @param {object} options { limit, gt, lt, reverse }
221
- * @returns
222
- */
223
- iterate(options) {
224
- return this.db.iterator(options)
225
- }
226
-
161
+ class Store {
162
+ constructor(name = 'storage', root, version = 'v1.0.0') {
163
+ this.name = name;
164
+ this.root = init(root);
165
+ this.version = version;
166
+
167
+ this.db = new ClassicLevel(join(this.root, this.name), { valueEncoding: 'view'});
168
+ }
169
+
170
+ toKeyPath(key) {
171
+ if (!key.isKeyPath()) key = new KeyPath(key);
172
+ return key.toString()
173
+ }
174
+
175
+ toKeyValue(value) {
176
+ if (!value.isKeyValue()) value = new KeyValue(value);
177
+ return value.uint8Array
178
+ }
179
+
180
+ toUint8Array(buffer) {
181
+ return Buffer.isBuffer(buffer) ? new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.byteLength / Uint8Array.BYTES_PER_ELEMENT) : buffer
182
+ }
183
+
184
+ async get(key) {
185
+ return this.toUint8Array(await this.db.get(this.toKeyPath(key)))
186
+ }
187
+
188
+ async put(key, value) {
189
+ return this.db.put(this.toKeyPath(key), this.toKeyValue(value))
190
+ }
191
+
192
+ async delete(key) {
193
+ return this.db.del(this.toKeyPath(key))
194
+ }
195
+
196
+ async clear() {
197
+ return this.db.clear()
198
+ }
199
+
200
+ async values(limit = -1) {
201
+ const values = [];
202
+ for await (const value of this.db.values({limit})) {
203
+ values.push(this.toUint8Array(value));
204
+ }
205
+ return values
206
+ }
207
+
208
+ async keys(limit = -1) {
209
+ const keys = [];
210
+ for await (const key of this.db.keys({limit})) {
211
+ keys.push(key);
212
+ }
213
+ return keys
214
+ }
215
+
216
+ /**
217
+ *
218
+ * @param {object} options { limit, gt, lt, reverse }
219
+ * @returns
220
+ */
221
+ iterate(options) {
222
+ return this.db.iterator(options)
223
+ }
224
+
227
225
  }
228
226
 
229
227
  export { Store as default };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/peernet",
3
- "version": "0.18.8",
3
+ "version": "1.0.0",
4
4
  "description": "",
5
5
  "main": "src/peernet.js",
6
6
  "exports": {
@@ -26,7 +26,7 @@
26
26
  "dependencies": {
27
27
  "@leofcoin/codec-format-interface": "^1.6.0",
28
28
  "@leofcoin/codecs": "^1.0.0",
29
- "@leofcoin/generate-account": "^1.2.0",
29
+ "@leofcoin/generate-account": "^2.0.0",
30
30
  "@leofcoin/multi-wallet": "^3.0.1",
31
31
  "@leofcoin/peernet-swarm": "^0.5.0",
32
32
  "@leofcoin/storage": "^3.0.0",
@@ -36,6 +36,7 @@
36
36
  "@vandeurenglenn/debug": "^1.0.0",
37
37
  "@vandeurenglenn/is-hex": "^1.0.0",
38
38
  "@vandeurenglenn/little-pubsub": "^1.3.1",
39
+ "inquirer": "^9.1.4",
39
40
  "multi-signature": "^1.3.1",
40
41
  "socket-request-client": "^1.5.0",
41
42
  "socket-request-server": "^1.5.0"
package/rollup.config.js CHANGED
@@ -10,7 +10,7 @@ rimraf.sync('./exports/**')
10
10
 
11
11
 
12
12
  export default [{
13
- input: ['./src/peernet.ts', './node_modules/@leofcoin/storage/exports/browser-store.js'],
13
+ input: ['./src/peernet.ts', './src/prompts/password/browser.js', './node_modules/@leofcoin/storage/exports/browser-store.js'],
14
14
  output: {
15
15
  format: 'es',
16
16
  dir: './exports/browser'
@@ -23,14 +23,20 @@ export default [{
23
23
  mainFields: ["browser", "module", "main"]
24
24
  }),
25
25
  typescript({...tsconfig, outDir: './exports/browser'})
26
+ ],
27
+ external: [
28
+ './prompts/password/node.js'
26
29
  ]
27
30
  }, {
28
- input: ['./src/peernet.ts', './node_modules/@leofcoin/storage/exports/store.js'],
31
+ input: ['./src/peernet.ts', './src/prompts/password/node.js', './node_modules/@leofcoin/storage/exports/store.js'],
29
32
  output: {
30
33
  format: 'es',
31
34
  dir: './exports'
32
35
  },
33
36
  plugins: [
34
37
  typescript({...tsconfig, outDir: './exports'})
38
+ ],
39
+ external: [
40
+ './prompts/password/browser.js'
35
41
  ]
36
42
  }]
@@ -13,15 +13,8 @@ export default class MessageHandler {
13
13
  * @return message
14
14
  */
15
15
  async hashAndSignMessage(message) {
16
- let identity = await walletStore.get('identity')
17
- identity = JSON.parse(identity)
18
- if (!globalThis.MultiWallet) {
19
- const importee = await import(/* webpackChunkName: "multi-wallet" */ '@leofcoin/multi-wallet')
20
- globalThis.MultiWallet = importee.default
21
- }
22
- const wallet = new MultiWallet(this.network)
23
- wallet.recover(identity.mnemonic)
24
- message.decoded.signature = wallet.sign(Buffer.from(await message.hash).slice(0, 32))
16
+ const hash = await message.peernetHash
17
+ message.decoded.signature = globalThis.identity.sign(hash.buffer)
25
18
  return message
26
19
  }
27
20
 
@@ -0,0 +1,93 @@
1
+ import MultiWallet from '@leofcoin/multi-wallet'
2
+
3
+ const walletStore = globalThis.walletStore
4
+ const accountStore = globalThis.accountStore
5
+
6
+ export default class Identity {
7
+ #wallet: MultiWallet
8
+ network: MultiWallet.network
9
+ id: string
10
+
11
+ constructor(network: string) {
12
+ this.network = network
13
+ }
14
+
15
+ get accounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
16
+ return this.getAccounts()
17
+ }
18
+
19
+ async getAccounts(): Promise<[[name: string, externalAddress: string, internalAddress: string]]> {
20
+ let accounts = await walletStore.get('accounts')
21
+ accounts = new TextDecoder().decode(accounts)
22
+ return JSON.parse(accounts)
23
+ }
24
+
25
+ async load(password?: string): Promise<void> {
26
+ let importee: { default: () => Promise<string> }
27
+ if (!password) {
28
+ if (globalThis.navigator) importee = await import('./prompts/password/browser.js')
29
+ else {
30
+ const path = await import('node:path');
31
+ const { fileURLToPath } = await import('node:url');
32
+ const __filename = fileURLToPath(import.meta.url);
33
+ const __dirname = path.dirname(__filename);
34
+ const url = path.join(__dirname, './prompts/password/node.js')
35
+ importee = await import(url)
36
+ }
37
+ password = await importee.default()
38
+ }
39
+
40
+ const accountExists = await accountStore.has('public')
41
+ if (accountExists) {
42
+ const pub = await accountStore.get('public')
43
+ this.id = JSON.parse(new TextDecoder().decode(pub)).walletId;
44
+ const selected = await walletStore.get('selected-account')
45
+ globalThis.peernet.selectedAccount = new TextDecoder().decode(selected)
46
+ } else {
47
+ const importee = await import(/* webpackChunkName: "generate-account" */ '@leofcoin/generate-account')
48
+ const {identity, accounts} = await importee.default(password, this.network)
49
+ await accountStore.put('public', JSON.stringify({walletId: identity.walletId}));
50
+
51
+ await walletStore.put('version', String(1))
52
+ await walletStore.put('accounts', JSON.stringify(accounts))
53
+ await walletStore.put('selected-account', accounts[0][1])
54
+ await walletStore.put('identity', JSON.stringify(identity))
55
+
56
+ globalThis.peernet.selectedAccount = accounts[0][1]
57
+ this.id = identity.walletId
58
+ }
59
+ const identity = JSON.parse(new TextDecoder().decode(await walletStore.get('identity')))
60
+ this.#wallet = new MultiWallet(this.network)
61
+ await this.#wallet.recover(identity.mnemonic, password, this.network)
62
+ }
63
+
64
+ sign(hash: Uint8Array) {
65
+ return this.#wallet.sign(hash.subarray(0, 32))
66
+ }
67
+
68
+ async export(password: string) {
69
+ if (!password) throw new Error('IdentityExportError: password required')
70
+ const identity = JSON.parse(new TextDecoder().decode(await walletStore.get('identity')))
71
+ this.#wallet = new MultiWallet(this.network)
72
+ await this.#wallet.recover(identity.mnemonic, password, this.network)
73
+ return this.#wallet.toMultiWif()
74
+ }
75
+
76
+ async import(multiWIF: string) {
77
+ this.#wallet = new MultiWallet(this.network)
78
+ await this.#wallet.fromMultiWif(multiWIF)
79
+ }
80
+
81
+ async unlock({ key, iv, cipher }) {
82
+
83
+ }
84
+
85
+ /**
86
+ * Lock current wallet or import wallet using a MultiWIF and lock
87
+ *
88
+ * @param password
89
+ * @param multiWIF
90
+ */
91
+ async lock(password: string, multiWIF?: string) {
92
+ }
93
+ }