@planetarium/account-web3-secret-storage
Advanced tools
Comparing version 0.54.0-dev.202333194751 to 0.54.0-dev.202341073234
@@ -5,1 +5,2 @@ export { KeyId } from "./KeyId.js"; | ||
export { getDefaultWeb3KeyStorePath, Web3KeyStore } from "./Web3KeyStore.js"; | ||
export { Web3Account, type Web3KeyObject } from "./Web3Account.js"; |
@@ -143,12 +143,59 @@ var __defProp = Object.defineProperty; | ||
// src/Web3Account.ts | ||
import { | ||
RawPrivateKey | ||
} from "@planetarium/account"; | ||
import { decryptKeystoreJson } from "ethers"; | ||
var _keyObject, _passphraseEntry; | ||
var Web3Account = class { | ||
constructor(keyObject, passphraseEntry) { | ||
__privateAdd(this, _keyObject, void 0); | ||
__privateAdd(this, _passphraseEntry, void 0); | ||
__privateSet(this, _keyObject, keyObject); | ||
__privateSet(this, _passphraseEntry, passphraseEntry); | ||
} | ||
async exportPrivateKey() { | ||
let firstAttempt = true; | ||
let account; | ||
while (true) { | ||
const passphrase = await __privateGet(this, _passphraseEntry).authenticate( | ||
__privateGet(this, _keyObject).id, | ||
firstAttempt | ||
); | ||
let currentProgress = 0; | ||
const json = JSON.stringify(__privateGet(this, _keyObject)); | ||
try { | ||
account = await decryptKeystoreJson(json, passphrase, (progress) => { | ||
currentProgress = progress; | ||
}); | ||
} catch (e) { | ||
if (currentProgress <= 0) { | ||
throw e; | ||
} | ||
firstAttempt = false; | ||
continue; | ||
} | ||
break; | ||
} | ||
return RawPrivateKey.fromHex(account.privateKey.replace(/^0x/i, "")); | ||
} | ||
async getPublicKey() { | ||
const key = await this.exportPrivateKey(); | ||
return await key.getPublicKey(); | ||
} | ||
async sign(message) { | ||
const key = await this.exportPrivateKey(); | ||
return await key.sign(message); | ||
} | ||
}; | ||
__name(Web3Account, "Web3Account"); | ||
_keyObject = new WeakMap(); | ||
_passphraseEntry = new WeakMap(); | ||
// src/Web3KeyStore.ts | ||
import { | ||
Address, | ||
RawPrivateKey | ||
RawPrivateKey as RawPrivateKey2 | ||
} from "@planetarium/account"; | ||
import { | ||
decryptKeystoreJson, | ||
encryptKeystoreJson, | ||
isKeystoreJson | ||
} from "ethers"; | ||
import { encryptKeystoreJson, isKeystoreJson } from "ethers"; | ||
import * as fs from "node:fs/promises"; | ||
@@ -177,3 +224,3 @@ import { homedir } from "node:os"; | ||
__name(parseKeyFilename, "parseKeyFilename"); | ||
var _passphraseEntry, _listKeyFiles, listKeyFiles_fn, _getKeyPath, getKeyPath_fn; | ||
var _passphraseEntry2, _listKeyFiles, listKeyFiles_fn, _getKeyPath, getKeyPath_fn, _import, import_fn; | ||
var Web3KeyStore = class { | ||
@@ -183,5 +230,6 @@ constructor(options) { | ||
__privateAdd(this, _getKeyPath); | ||
__privateAdd(this, _passphraseEntry, void 0); | ||
__privateAdd(this, _import); | ||
__privateAdd(this, _passphraseEntry2, void 0); | ||
this.path = options.path ?? getDefaultWeb3KeyStorePath(); | ||
__privateSet(this, _passphraseEntry, options.passphraseEntry); | ||
__privateSet(this, _passphraseEntry2, options.passphraseEntry); | ||
} | ||
@@ -212,27 +260,5 @@ async *list() { | ||
} | ||
let firstAttempt = true; | ||
let account; | ||
while (true) { | ||
const passphrase = await __privateGet(this, _passphraseEntry).authenticate( | ||
keyId, | ||
firstAttempt | ||
); | ||
let currentProgress = 0; | ||
try { | ||
account = await decryptKeystoreJson(json, passphrase, (progress) => { | ||
currentProgress = progress; | ||
}); | ||
} catch (e) { | ||
if (currentProgress <= 0) { | ||
return { result: "error", keyId, message: `${e}` }; | ||
} | ||
firstAttempt = false; | ||
continue; | ||
} | ||
break; | ||
} | ||
return { | ||
result: "success", | ||
// TODO: Declare Web3Account | ||
account: RawPrivateKey.fromHex(account.privateKey.replace(/^0x/i, "")), | ||
account: new Web3Account(JSON.parse(json), __privateGet(this, _passphraseEntry2)), | ||
keyId, | ||
@@ -244,6 +270,11 @@ metadata: void 0, | ||
async generate(metadata) { | ||
const account = await RawPrivateKey.generate(); | ||
const result = await this.import(account, metadata); | ||
if (result.result === "success") | ||
return { ...result, account }; | ||
const privateKey = await RawPrivateKey2.generate(); | ||
const result = await __privateMethod(this, _import, import_fn).call(this, privateKey, metadata); | ||
if (result.result === "success") { | ||
return { | ||
result: "success", | ||
keyId: result.keyId, | ||
account: new Web3Account(result.keyObject, __privateGet(this, _passphraseEntry2)) | ||
}; | ||
} | ||
return result; | ||
@@ -263,31 +294,11 @@ } | ||
async import(privateKey, metadata) { | ||
const passphrase = await __privateGet(this, _passphraseEntry).configurePassphrase(); | ||
const json = await encryptKeystoreJson( | ||
{ | ||
address: Address.deriveFrom(privateKey).toString(), | ||
privateKey: "0x" + Buffer.from(privateKey.toBytes()).toString("hex") | ||
}, | ||
passphrase | ||
); | ||
const { id: keyId } = JSON.parse(json); | ||
try { | ||
await fs.mkdir(this.path, { recursive: true }); | ||
} catch (e) { | ||
return { result: "error", message: `${e}` }; | ||
const result = await __privateMethod(this, _import, import_fn).call(this, privateKey, metadata); | ||
if (result.result === "success") { | ||
return { result: "success", keyId: result.keyId }; | ||
} | ||
const createdAt = /* @__PURE__ */ new Date(); | ||
const keyPath = path.join( | ||
this.path, | ||
`UTC--${createdAt.toISOString().replace(/\.[0-9]+Z$/, "Z").replace(/:/g, "-")}--${keyId}` | ||
); | ||
try { | ||
await fs.writeFile(keyPath, json, "utf8"); | ||
} catch (e) { | ||
return { result: "error", message: `${e}` }; | ||
} | ||
return { result: "success", keyId }; | ||
return result; | ||
} | ||
}; | ||
__name(Web3KeyStore, "Web3KeyStore"); | ||
_passphraseEntry = new WeakMap(); | ||
_passphraseEntry2 = new WeakMap(); | ||
_listKeyFiles = new WeakSet(); | ||
@@ -320,4 +331,34 @@ listKeyFiles_fn = /* @__PURE__ */ __name(async function* () { | ||
}, "#getKeyPath"); | ||
_import = new WeakSet(); | ||
import_fn = /* @__PURE__ */ __name(async function(privateKey, metadata) { | ||
const passphrase = await __privateGet(this, _passphraseEntry2).configurePassphrase(); | ||
const json = await encryptKeystoreJson( | ||
{ | ||
address: (await Address.deriveFrom(privateKey)).toString(), | ||
privateKey: `0x${Buffer.from(privateKey.toBytes()).toString("hex")}` | ||
}, | ||
passphrase | ||
); | ||
const keyObject = JSON.parse(json); | ||
const { id: keyId } = keyObject; | ||
try { | ||
await fs.mkdir(this.path, { recursive: true }); | ||
} catch (e) { | ||
return { result: "error", message: `${e}` }; | ||
} | ||
const createdAt = /* @__PURE__ */ new Date(); | ||
const keyPath = path.join( | ||
this.path, | ||
`UTC--${createdAt.toISOString().replace(/\.[0-9]+Z$/, "Z").replace(/:/g, "-")}--${keyId}` | ||
); | ||
try { | ||
await fs.writeFile(keyPath, json, "utf8"); | ||
} catch (e) { | ||
return { result: "error", message: `${e}` }; | ||
} | ||
return { result: "success", keyId, keyObject }; | ||
}, "#import"); | ||
export { | ||
TtyPassphraseEntry, | ||
Web3Account, | ||
Web3KeyStore, | ||
@@ -324,0 +365,0 @@ getDefaultWeb3KeyStorePath |
import { KeyId } from "./KeyId.js"; | ||
import { PassphraseEntry } from "./PassphraseEntry.js"; | ||
import { Web3Account } from "./Web3Account.js"; | ||
import { type AccountDeletion, type AccountGeneration, type AccountImportation, type AccountMetadata, type AccountRetrieval, type ImportableKeyStore, RawPrivateKey } from "@planetarium/account"; | ||
@@ -19,3 +20,3 @@ export interface Web3KeyStoreOptions { | ||
}; | ||
export declare class Web3KeyStore implements ImportableKeyStore<KeyId, RawPrivateKey> { | ||
export declare class Web3KeyStore implements ImportableKeyStore<KeyId, Web3Account> { | ||
#private; | ||
@@ -25,4 +26,4 @@ readonly path: string; | ||
list(): AsyncIterable<AccountMetadata<KeyId>>; | ||
get(keyId: Readonly<KeyId>): Promise<AccountRetrieval<KeyId, RawPrivateKey>>; | ||
generate(metadata?: Partial<undefined>): Promise<AccountGeneration<KeyId, RawPrivateKey>>; | ||
get(keyId: Readonly<KeyId>): Promise<AccountRetrieval<KeyId, Web3Account>>; | ||
generate(metadata?: Partial<undefined>): Promise<AccountGeneration<KeyId, Web3Account>>; | ||
delete(keyId: Readonly<KeyId>): Promise<AccountDeletion<KeyId>>; | ||
@@ -29,0 +30,0 @@ import(privateKey: RawPrivateKey, metadata?: Partial<undefined>): Promise<AccountImportation<KeyId>>; |
@@ -29,2 +29,3 @@ { | ||
"devDependencies": { | ||
"@planetarium/account": "^0.54.0-dev.202341073234", | ||
"@types/node": "^18.13.0", | ||
@@ -34,2 +35,3 @@ "@types/stream-buffers": "^3", | ||
"@vitest/ui": "^0.29.2", | ||
"fast-check": "^3.8.0", | ||
"nanobundle": "^1.5.0", | ||
@@ -41,6 +43,8 @@ "stream-buffers": "^3.0.2", | ||
"dependencies": { | ||
"@planetarium/account": "^0.54.0-dev.202333194751", | ||
"ethers": "^6.1.0" | ||
}, | ||
"version": "0.54.0-dev.202333194751" | ||
"peerDependencies": { | ||
"@planetarium/account": "^0.54.0-dev.202341073234" | ||
}, | ||
"version": "0.54.0-dev.202341073234" | ||
} |
Sorry, the diff of this file is not supported yet
38107
10
462
10