@dotrino/identity 0.27.0 → 0.28.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/package.json +1 -1
- package/src/node.js +16 -5
package/package.json
CHANGED
package/src/node.js
CHANGED
|
@@ -20,15 +20,25 @@ import path from 'node:path'
|
|
|
20
20
|
import os from 'node:os'
|
|
21
21
|
import { createIdentityCore } from '../vault/core.js'
|
|
22
22
|
|
|
23
|
-
/**
|
|
24
|
-
|
|
23
|
+
/**
|
|
24
|
+
* kv síncrono respaldado por un archivo JSON (estilo localStorage).
|
|
25
|
+
*
|
|
26
|
+
* `atRest` (opcional) cifra el archivo entero en disco: quien lo provee decide con qué
|
|
27
|
+
* (el vault lo liga a la máquina, ver `dotrino-vault/src/atrest.js`). Si el archivo estaba
|
|
28
|
+
* en claro se lee igual y queda cifrado en la primera escritura, sin pedir nada.
|
|
29
|
+
*/
|
|
30
|
+
function fileKv (filePath, atRest = null) {
|
|
25
31
|
let data = {}
|
|
26
32
|
try {
|
|
27
|
-
if (fs.existsSync(filePath))
|
|
33
|
+
if (fs.existsSync(filePath)) {
|
|
34
|
+
const raw = fs.readFileSync(filePath, 'utf8')
|
|
35
|
+
data = JSON.parse(atRest ? atRest.decrypt(raw) : raw) || {}
|
|
36
|
+
}
|
|
28
37
|
} catch (_) { data = {} }
|
|
29
38
|
const flush = () => {
|
|
30
39
|
fs.mkdirSync(path.dirname(filePath), { recursive: true })
|
|
31
|
-
|
|
40
|
+
const text = JSON.stringify(data)
|
|
41
|
+
fs.writeFileSync(filePath, atRest ? atRest.encrypt(text) : text, { mode: 0o600 })
|
|
32
42
|
}
|
|
33
43
|
return {
|
|
34
44
|
getItem: (k) => (k in data ? data[k] : null),
|
|
@@ -91,6 +101,7 @@ export class Identity {
|
|
|
91
101
|
*/
|
|
92
102
|
constructor (options = {}) {
|
|
93
103
|
this._dir = options.dir || DEFAULT_DIR
|
|
104
|
+
this._atRest = options.atRest || null
|
|
94
105
|
this._core = null
|
|
95
106
|
this._listeners = new Map()
|
|
96
107
|
}
|
|
@@ -109,7 +120,7 @@ export class Identity {
|
|
|
109
120
|
async ready () {
|
|
110
121
|
if (this._core) return this
|
|
111
122
|
this._core = await createIdentityCore({
|
|
112
|
-
kv: fileKv(path.join(this._dir, 'identity.json')),
|
|
123
|
+
kv: fileKv(path.join(this._dir, 'identity.json'), this._atRest),
|
|
113
124
|
peers: filePeers(path.join(this._dir, 'peers.json')),
|
|
114
125
|
makeSync: null
|
|
115
126
|
})
|