@economicagents/keystore 0.1.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/LICENSE ADDED
@@ -0,0 +1,204 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other
43
+ modifications represent, as a whole, an original work of authorship.
44
+ For the purposes of this License, Derivative Works shall not include
45
+ works that remain separable from, or merely link (or bind by name)
46
+ to the interfaces of, the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control
57
+ systems, and issue tracking systems that are managed by, or on behalf
58
+ of, the Licensor for the purpose of discussing and improving the Work,
59
+ but excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute
108
+ must include a readable copy of the attribution notices
109
+ contained within such NOTICE file, excluding those notices
110
+ that do not pertain to any part of the Derivative Works, in
111
+ at least one of the following places: within a NOTICE text
112
+ file distributed as part of the Derivative Works; within
113
+ the Source form or documentation, if provided along with the
114
+ Derivative Works; or, within a display generated by the
115
+ Derivative Works, if and wherever such third-party notices
116
+ normally appear. The contents of the NOTICE file are for
117
+ informational purposes only and do not modify the License.
118
+ You may add Your own attribution notices within Derivative
119
+ Works that You distribute, alongside or as an addendum to
120
+ the NOTICE text from the Work, provided that such additional
121
+ attribution notices cannot be construed as modifying the
122
+ License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies
129
+ with the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing
142
+ the origin of the Work and reproducing the content of the NOTICE
143
+ file.
144
+
145
+ 7. Disclaimer of Warranty. Unless required by applicable law or
146
+ agreed to in writing, Licensor provides the Work (and each
147
+ Contributor provides its Contributions) on an "AS IS" BASIS,
148
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
149
+ implied, including, without limitation, any warranties or
150
+ conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or
151
+ FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for
152
+ determining the appropriateness of using or redistributing the
153
+ Work and assume any risks associated with Your exercise of
154
+ permissions under this License.
155
+
156
+ 8. Limitation of Liability. In no event and under no legal theory,
157
+ whether in tort (including negligence), contract, or otherwise,
158
+ unless required by applicable law (such as deliberate and grossly
159
+ negligent acts) or agreed to in writing, shall any Contributor be
160
+ liable to You for damages, including any direct, indirect, special,
161
+ incidental, or consequential damages of any character arising as
162
+ a result of this License or out of the use or inability to use the
163
+ Work (including but not limited to damages for loss of goodwill,
164
+ work stoppage, computer failure or malfunction, or any and all
165
+ other commercial damages or losses), even if such Contributor has
166
+ been advised of the possibility of such damages.
167
+
168
+ 9. Accepting Warranty or Additional Liability. While redistributing
169
+ the Work or Derivative Works thereof, You may choose to offer,
170
+ and charge a fee for, acceptance of support, warranty, indemnity,
171
+ or other liability obligations and/or rights consistent with this
172
+ License. However, in accepting such obligations, You may act only
173
+ on Your own behalf and on Your sole responsibility, not on behalf
174
+ of any other Contributor, and only if You agree to indemnify,
175
+ defend, and hold each Contributor harmless for any liability
176
+ incurred by, or claims asserted against, such Contributor by
177
+ reason of your accepting any such warranty or additional liability.
178
+
179
+ END OF TERMS AND CONDITIONS
180
+
181
+ APPENDIX: How to apply the Apache License to your work.
182
+
183
+ To apply the Apache License to your work, attach the following
184
+ boilerplate notice, with the fields enclosed by brackets "[]"
185
+ replaced with your own identifying information. (Don't include
186
+ the brackets!) The text should be enclosed in the appropriate
187
+ comment syntax for the file format. We also recommend that a
188
+ file or class name and description of purpose be included on the
189
+ same "printed page" as the copyright notice for easier
190
+ identification within third-party archives.
191
+
192
+ Copyright 2024-2025 AEP Contributors
193
+
194
+ Licensed under the Apache License, Version 2.0 (the "License");
195
+ you may not use this file except in compliance with the License.
196
+ You may obtain a copy of the License at
197
+
198
+ http://www.apache.org/licenses/LICENSE-2.0
199
+
200
+ Unless required by applicable law or agreed to in writing, software
201
+ distributed under the License is distributed on an "AS IS" BASIS,
202
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
203
+ See the License for the specific language governing permissions and
204
+ limitations under the License.
@@ -0,0 +1,8 @@
1
+ import { type PrivateKeyAccount } from "viem/accounts";
2
+ export type SignerResult = {
3
+ account: PrivateKeyAccount;
4
+ privateKey: `0x${string}`;
5
+ };
6
+ export declare function getSignerAccount(accountNameOverride?: string): Promise<SignerResult>;
7
+ export declare function getSignerAccountSync(accountNameOverride?: string): SignerResult;
8
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EAAuB,KAAK,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAwD5E,MAAM,MAAM,YAAY,GAAG;IAAE,OAAO,EAAE,iBAAiB,CAAC;IAAC,UAAU,EAAE,KAAK,MAAM,EAAE,CAAA;CAAE,CAAC;AAErF,wBAAsB,gBAAgB,CAAC,mBAAmB,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,YAAY,CAAC,CA6B1F;AAED,wBAAgB,oBAAoB,CAAC,mBAAmB,CAAC,EAAE,MAAM,GAAG,YAAY,CAqC/E"}
package/dist/index.js ADDED
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Foundry keystore signer resolver for AEP.
3
+ * Prefer keystore; fallback to PRIVATE_KEY with warning.
4
+ */
5
+ import { readFileSync, existsSync, readdirSync } from "fs";
6
+ import { join } from "path";
7
+ import { homedir } from "os";
8
+ import { Wallet } from "ethers";
9
+ import { privateKeyToAccount } from "viem/accounts";
10
+ const DEFAULT_KEYSTORE_DIR = join(homedir(), ".foundry", "keystores");
11
+ const PRIVATE_KEY_WARNING = "Warning: PRIVATE_KEY in .env is insecure. Prefer: cast wallet import aep --interactive, then set AEP_KEYSTORE_ACCOUNT=aep";
12
+ let privateKeyWarningEmitted = false;
13
+ function resolveKeystorePath(accountName) {
14
+ const explicitPath = process.env.AEP_KEYSTORE_PATH;
15
+ if (explicitPath && existsSync(explicitPath)) {
16
+ return explicitPath;
17
+ }
18
+ const keystoreDir = process.env.FOUNDRY_KEYSTORE_DIR ?? DEFAULT_KEYSTORE_DIR;
19
+ if (!existsSync(keystoreDir)) {
20
+ return null;
21
+ }
22
+ const candidates = [
23
+ join(keystoreDir, accountName),
24
+ join(keystoreDir, `${accountName}.json`),
25
+ ];
26
+ for (const p of candidates) {
27
+ if (existsSync(p)) {
28
+ return p;
29
+ }
30
+ }
31
+ const files = readdirSync(keystoreDir, { withFileTypes: true });
32
+ for (const f of files) {
33
+ if (f.isFile() && (f.name === accountName || f.name === `${accountName}.json`)) {
34
+ return join(keystoreDir, f.name);
35
+ }
36
+ }
37
+ return null;
38
+ }
39
+ async function getPassword() {
40
+ const pw = process.env.FOUNDRY_PASSWORD ??
41
+ process.env.AEP_KEYSTORE_PASSWORD ??
42
+ process.env.ETH_KEYSTORE_PASSWORD;
43
+ if (pw)
44
+ return pw;
45
+ if (process.stdin.isTTY) {
46
+ const readline = await import("readline");
47
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
48
+ return new Promise((resolve) => {
49
+ rl.question("Keystore password: ", (answer) => {
50
+ rl.close();
51
+ resolve(answer);
52
+ });
53
+ });
54
+ }
55
+ throw new Error("Keystore password required. Set FOUNDRY_PASSWORD or AEP_KEYSTORE_PASSWORD, or run interactively.");
56
+ }
57
+ export async function getSignerAccount(accountNameOverride) {
58
+ const accountName = accountNameOverride ??
59
+ process.env.AEP_KEYSTORE_ACCOUNT ??
60
+ process.env.FOUNDRY_ACCOUNT ??
61
+ "foundry";
62
+ const keystorePath = resolveKeystorePath(accountName);
63
+ if (keystorePath) {
64
+ const json = readFileSync(keystorePath, "utf-8");
65
+ const password = await getPassword();
66
+ const wallet = Wallet.fromEncryptedJsonSync(json, password);
67
+ const pk = (wallet.privateKey.startsWith("0x") ? wallet.privateKey : `0x${wallet.privateKey}`);
68
+ return { account: privateKeyToAccount(pk), privateKey: pk };
69
+ }
70
+ const rawPk = process.env.PRIVATE_KEY;
71
+ if (rawPk) {
72
+ if (!privateKeyWarningEmitted && process.env.AEP_SILENCE_PRIVATE_KEY_WARNING !== "1") {
73
+ console.error(PRIVATE_KEY_WARNING);
74
+ privateKeyWarningEmitted = true;
75
+ }
76
+ const pk = (rawPk.startsWith("0x") ? rawPk : `0x${rawPk}`);
77
+ return { account: privateKeyToAccount(pk), privateKey: pk };
78
+ }
79
+ throw new Error("No signer. Recommended: cast wallet import aep --interactive, then set AEP_KEYSTORE_ACCOUNT=aep. Fallback: set PRIVATE_KEY in .env (insecure).");
80
+ }
81
+ export function getSignerAccountSync(accountNameOverride) {
82
+ const accountName = accountNameOverride ??
83
+ process.env.AEP_KEYSTORE_ACCOUNT ??
84
+ process.env.FOUNDRY_ACCOUNT ??
85
+ "foundry";
86
+ const keystorePath = resolveKeystorePath(accountName);
87
+ if (keystorePath) {
88
+ const json = readFileSync(keystorePath, "utf-8");
89
+ const password = process.env.FOUNDRY_PASSWORD ??
90
+ process.env.AEP_KEYSTORE_PASSWORD ??
91
+ process.env.ETH_KEYSTORE_PASSWORD;
92
+ if (!password) {
93
+ throw new Error("Keystore password required. Set FOUNDRY_PASSWORD or AEP_KEYSTORE_PASSWORD for non-interactive use.");
94
+ }
95
+ const wallet = Wallet.fromEncryptedJsonSync(json, password);
96
+ const pk = (wallet.privateKey.startsWith("0x") ? wallet.privateKey : `0x${wallet.privateKey}`);
97
+ return { account: privateKeyToAccount(pk), privateKey: pk };
98
+ }
99
+ const rawPk = process.env.PRIVATE_KEY;
100
+ if (rawPk) {
101
+ if (!privateKeyWarningEmitted && process.env.AEP_SILENCE_PRIVATE_KEY_WARNING !== "1") {
102
+ console.error(PRIVATE_KEY_WARNING);
103
+ privateKeyWarningEmitted = true;
104
+ }
105
+ const pk = (rawPk.startsWith("0x") ? rawPk : `0x${rawPk}`);
106
+ return { account: privateKeyToAccount(pk), privateKey: pk };
107
+ }
108
+ throw new Error("No signer. Recommended: cast wallet import aep --interactive, then set AEP_KEYSTORE_ACCOUNT=aep. Fallback: set PRIVATE_KEY in .env (insecure).");
109
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@economicagents/keystore",
3
+ "version": "0.1.0",
4
+ "description": "Foundry keystore signer resolver for AEP",
5
+ "license": "Apache-2.0",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/economicagents/AEP.git"
9
+ },
10
+ "bugs": "https://github.com/economicagents/AEP/issues",
11
+ "homepage": "https://github.com/economicagents/AEP#readme",
12
+ "keywords": [
13
+ "aep",
14
+ "keystore",
15
+ "foundry",
16
+ "signer"
17
+ ],
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "type": "module",
22
+ "main": "dist/index.js",
23
+ "types": "dist/index.d.ts",
24
+ "exports": {
25
+ ".": {
26
+ "import": "./dist/index.js",
27
+ "types": "./dist/index.d.ts"
28
+ }
29
+ },
30
+ "dependencies": {
31
+ "ethers": "^6.13.0",
32
+ "viem": "^2.21.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^22.0.0",
36
+ "typescript": "^5.6.0",
37
+ "vitest": "^2.1.0"
38
+ },
39
+ "files": [
40
+ "dist"
41
+ ],
42
+ "scripts": {
43
+ "build": "tsc",
44
+ "dev": "tsc --watch",
45
+ "test": "vitest run",
46
+ "test:watch": "vitest"
47
+ }
48
+ }