@sidhujag/sysweb3-keyring 1.0.502 → 1.0.503
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/cjs/storage.js +38 -13
- package/cjs/storage.js.map +1 -1
- package/package.json +1 -1
package/cjs/storage.js
CHANGED
|
@@ -7,24 +7,49 @@ exports.getDecryptedVault = exports.setEncryptedVault = void 0;
|
|
|
7
7
|
const sysweb3_core_1 = require("@sidhujag/sysweb3-core");
|
|
8
8
|
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
9
9
|
const storage = sysweb3_core_1.sysweb3Di.getStateStorageDb();
|
|
10
|
+
// Simple async mutex implementation to prevent concurrent vault operations
|
|
11
|
+
class AsyncMutex {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.mutex = Promise.resolve();
|
|
14
|
+
}
|
|
15
|
+
async runExclusive(callback) {
|
|
16
|
+
const oldMutex = this.mutex;
|
|
17
|
+
let release;
|
|
18
|
+
this.mutex = new Promise((resolve) => {
|
|
19
|
+
release = resolve;
|
|
20
|
+
});
|
|
21
|
+
await oldMutex;
|
|
22
|
+
try {
|
|
23
|
+
return await callback();
|
|
24
|
+
}
|
|
25
|
+
finally {
|
|
26
|
+
release();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const vaultMutex = new AsyncMutex();
|
|
10
31
|
// Single vault for all networks - stores the mnemonic and can derive accounts for any slip44
|
|
11
32
|
const setEncryptedVault = async (decryptedVault, pwd) => {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
33
|
+
return vaultMutex.runExclusive(async () => {
|
|
34
|
+
const encryptedVault = crypto_js_1.default.AES.encrypt(JSON.stringify(decryptedVault), pwd);
|
|
35
|
+
// Always use single 'vault' key for all networks
|
|
36
|
+
await storage.set('vault', encryptedVault.toString());
|
|
37
|
+
});
|
|
15
38
|
};
|
|
16
39
|
exports.setEncryptedVault = setEncryptedVault;
|
|
17
40
|
const getDecryptedVault = async (pwd) => {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
return vaultMutex.runExclusive(async () => {
|
|
42
|
+
// Always use single 'vault' key
|
|
43
|
+
const vault = await storage.get('vault');
|
|
44
|
+
if (!vault) {
|
|
45
|
+
throw new Error('Vault not found');
|
|
46
|
+
}
|
|
47
|
+
const decryptedVault = crypto_js_1.default.AES.decrypt(vault, pwd).toString(crypto_js_1.default.enc.Utf8);
|
|
48
|
+
if (!decryptedVault) {
|
|
49
|
+
throw new Error('Failed to decrypt vault - invalid password or corrupted data');
|
|
50
|
+
}
|
|
51
|
+
return JSON.parse(decryptedVault);
|
|
52
|
+
});
|
|
28
53
|
};
|
|
29
54
|
exports.getDecryptedVault = getDecryptedVault;
|
|
30
55
|
//# sourceMappingURL=storage.js.map
|
package/cjs/storage.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":";;;;;;AAAA,yDAAmD;AACnD,0DAAiC;AAEjC,MAAM,OAAO,GAAG,wBAAS,CAAC,iBAAiB,EAAE,CAAC;AAE9C,6FAA6F;AACtF,MAAM,iBAAiB,GAAG,KAAK,EAAE,cAAmB,EAAE,GAAW,EAAE,EAAE;IAC1E,MAAM,cAAc,GAAG,mBAAQ,CAAC,GAAG,CAAC,OAAO,CACzC,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,EAC9B,GAAG,CACJ,CAAC;
|
|
1
|
+
{"version":3,"file":"storage.js","sourceRoot":"","sources":["../../src/storage.ts"],"names":[],"mappings":";;;;;;AAAA,yDAAmD;AACnD,0DAAiC;AAEjC,MAAM,OAAO,GAAG,wBAAS,CAAC,iBAAiB,EAAE,CAAC;AAE9C,2EAA2E;AAC3E,MAAM,UAAU;IAAhB;QACU,UAAK,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