@augustdigital/config 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 +21 -0
- package/README.md +53 -0
- package/lib/config.d.ts +44 -0
- package/lib/config.js +80 -0
- package/lib/errors.d.ts +6 -0
- package/lib/errors.js +13 -0
- package/lib/index.d.ts +9 -0
- package/lib/index.js +49 -0
- package/lib/keystore.d.ts +37 -0
- package/lib/keystore.js +176 -0
- package/lib/paths.d.ts +6 -0
- package/lib/paths.js +33 -0
- package/lib/precedence.d.ts +11 -0
- package/lib/precedence.js +69 -0
- package/lib/profile.d.ts +26 -0
- package/lib/profile.js +65 -0
- package/lib/schemas/envelope.d.ts +155 -0
- package/lib/schemas/envelope.js +34 -0
- package/lib/schemas/index.d.ts +4 -0
- package/lib/schemas/index.js +21 -0
- package/lib/schemas/meta.d.ts +115 -0
- package/lib/schemas/meta.js +34 -0
- package/lib/schemas/user.d.ts +133 -0
- package/lib/schemas/user.js +48 -0
- package/lib/schemas/vault.d.ts +265 -0
- package/lib/schemas/vault.js +109 -0
- package/lib/sdk.d.ts +14 -0
- package/lib/sdk.js +120 -0
- package/lib/session.d.ts +8 -0
- package/lib/session.js +76 -0
- package/package.json +51 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 August Digital
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
# `@augustdigital/config`
|
|
2
|
+
|
|
3
|
+
Private workspace package. Shared config, keystore, session, profiles,
|
|
4
|
+
and SDK factory used by [`@augustdigital/cli`](../cli) and
|
|
5
|
+
[`@augustdigital/mcp`](../mcp). Also owns the Zod schemas that define
|
|
6
|
+
the stable JSON envelope they emit.
|
|
7
|
+
|
|
8
|
+
Not published to npm.
|
|
9
|
+
|
|
10
|
+
## Surface
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import {
|
|
14
|
+
// config + paths
|
|
15
|
+
loadConfig,
|
|
16
|
+
saveConfig,
|
|
17
|
+
configFile,
|
|
18
|
+
activeConfigDir,
|
|
19
|
+
// profiles
|
|
20
|
+
saveProfile,
|
|
21
|
+
loadProfile,
|
|
22
|
+
listProfiles,
|
|
23
|
+
deleteProfile,
|
|
24
|
+
// keystore + session
|
|
25
|
+
Keystore,
|
|
26
|
+
readSession,
|
|
27
|
+
writeSession,
|
|
28
|
+
clearSession,
|
|
29
|
+
sessionActive,
|
|
30
|
+
// resolvers (precedence: flag → env → session → profile → config)
|
|
31
|
+
getApiKey,
|
|
32
|
+
getRpcUrl,
|
|
33
|
+
getDefaultChainId,
|
|
34
|
+
// SDK factory
|
|
35
|
+
createSDK,
|
|
36
|
+
// typed errors
|
|
37
|
+
AugustConfigError,
|
|
38
|
+
// output schemas
|
|
39
|
+
schemas,
|
|
40
|
+
} from '@augustdigital/config';
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## On-disk layout
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
~/.augustdigital/
|
|
47
|
+
├── config.json # default chain, RPCs, profile, telemetry
|
|
48
|
+
├── keystore.json # AES-256-GCM; holds named secrets (e.g. `august`)
|
|
49
|
+
├── .session # mode 0600; decrypted API key + expiry
|
|
50
|
+
└── profiles/ # named overlays for chain + RPC sets
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
Override the root with `AUGUST_CONFIG_DIR`.
|
package/lib/config.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const ConfigSchemaV1: z.ZodObject<{
|
|
3
|
+
schemaVersion: z.ZodDefault<z.ZodLiteral<1>>;
|
|
4
|
+
appName: z.ZodDefault<z.ZodString>;
|
|
5
|
+
defaultProfile: z.ZodDefault<z.ZodString>;
|
|
6
|
+
defaultChainId: z.ZodDefault<z.ZodNumber>;
|
|
7
|
+
rpcs: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
8
|
+
telemetry: z.ZodDefault<z.ZodBoolean>;
|
|
9
|
+
auth: z.ZodDefault<z.ZodObject<{
|
|
10
|
+
sessionTtlSec: z.ZodDefault<z.ZodNumber>;
|
|
11
|
+
}, "strip", z.ZodTypeAny, {
|
|
12
|
+
sessionTtlSec?: number;
|
|
13
|
+
}, {
|
|
14
|
+
sessionTtlSec?: number;
|
|
15
|
+
}>>;
|
|
16
|
+
}, "strip", z.ZodTypeAny, {
|
|
17
|
+
schemaVersion?: 1;
|
|
18
|
+
appName?: string;
|
|
19
|
+
defaultProfile?: string;
|
|
20
|
+
defaultChainId?: number;
|
|
21
|
+
rpcs?: Record<string, string>;
|
|
22
|
+
telemetry?: boolean;
|
|
23
|
+
auth?: {
|
|
24
|
+
sessionTtlSec?: number;
|
|
25
|
+
};
|
|
26
|
+
}, {
|
|
27
|
+
schemaVersion?: 1;
|
|
28
|
+
appName?: string;
|
|
29
|
+
defaultProfile?: string;
|
|
30
|
+
defaultChainId?: number;
|
|
31
|
+
rpcs?: Record<string, string>;
|
|
32
|
+
telemetry?: boolean;
|
|
33
|
+
auth?: {
|
|
34
|
+
sessionTtlSec?: number;
|
|
35
|
+
};
|
|
36
|
+
}>;
|
|
37
|
+
export type Config = z.infer<typeof ConfigSchemaV1>;
|
|
38
|
+
export type PartialConfig = z.input<typeof ConfigSchemaV1>;
|
|
39
|
+
export declare function loadConfig(path?: string): Config;
|
|
40
|
+
export declare function saveConfig(patch: PartialConfig, path?: string): Config;
|
|
41
|
+
export declare function configExists(path?: string): boolean;
|
|
42
|
+
export declare function defaultConfig(): Config;
|
|
43
|
+
export declare function activeConfigDir(): string;
|
|
44
|
+
export {};
|
package/lib/config.js
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.loadConfig = loadConfig;
|
|
4
|
+
exports.saveConfig = saveConfig;
|
|
5
|
+
exports.configExists = configExists;
|
|
6
|
+
exports.defaultConfig = defaultConfig;
|
|
7
|
+
exports.activeConfigDir = activeConfigDir;
|
|
8
|
+
const node_fs_1 = require("node:fs");
|
|
9
|
+
const node_path_1 = require("node:path");
|
|
10
|
+
const zod_1 = require("zod");
|
|
11
|
+
const errors_1 = require("./errors");
|
|
12
|
+
const paths_1 = require("./paths");
|
|
13
|
+
const RpcMapSchema = zod_1.z.record(zod_1.z.string().min(1), zod_1.z.string().url());
|
|
14
|
+
const ConfigSchemaV1 = zod_1.z.object({
|
|
15
|
+
schemaVersion: zod_1.z.literal(1).default(1),
|
|
16
|
+
appName: zod_1.z.string().min(3).max(64).default('august-cli'),
|
|
17
|
+
defaultProfile: zod_1.z.string().min(1).default('default'),
|
|
18
|
+
defaultChainId: zod_1.z.number().int().default(1),
|
|
19
|
+
rpcs: RpcMapSchema.default({}),
|
|
20
|
+
telemetry: zod_1.z.boolean().default(false),
|
|
21
|
+
auth: zod_1.z
|
|
22
|
+
.object({
|
|
23
|
+
sessionTtlSec: zod_1.z.number().int().positive().default(3600),
|
|
24
|
+
})
|
|
25
|
+
.default({ sessionTtlSec: 3600 }),
|
|
26
|
+
});
|
|
27
|
+
const DEFAULT_CONFIG = ConfigSchemaV1.parse({});
|
|
28
|
+
function ensureFileMode(path, mode) {
|
|
29
|
+
try {
|
|
30
|
+
(0, node_fs_1.chmodSync)(path, mode);
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
function loadConfig(path = (0, paths_1.configFile)()) {
|
|
36
|
+
if (!(0, node_fs_1.existsSync)(path)) {
|
|
37
|
+
return { ...DEFAULT_CONFIG };
|
|
38
|
+
}
|
|
39
|
+
let raw;
|
|
40
|
+
try {
|
|
41
|
+
raw = JSON.parse((0, node_fs_1.readFileSync)(path, 'utf8'));
|
|
42
|
+
}
|
|
43
|
+
catch (err) {
|
|
44
|
+
throw new errors_1.AugustConfigError('CONFIG_INVALID', `Config file at ${path} is not valid JSON: ${err.message}`, 'Restore from backup or re-run `august init`.');
|
|
45
|
+
}
|
|
46
|
+
const result = ConfigSchemaV1.safeParse(raw);
|
|
47
|
+
if (!result.success) {
|
|
48
|
+
throw new errors_1.AugustConfigError('CONFIG_INVALID', `Config file at ${path} failed validation: ${result.error.message}`, 'Edit the file to match the schema, or re-run `august init`.');
|
|
49
|
+
}
|
|
50
|
+
return result.data;
|
|
51
|
+
}
|
|
52
|
+
function saveConfig(patch, path = (0, paths_1.configFile)()) {
|
|
53
|
+
const existing = (0, node_fs_1.existsSync)(path) ? loadConfig(path) : { ...DEFAULT_CONFIG };
|
|
54
|
+
const merged = ConfigSchemaV1.parse({
|
|
55
|
+
...existing,
|
|
56
|
+
...patch,
|
|
57
|
+
rpcs: { ...existing.rpcs, ...(patch.rpcs ?? {}) },
|
|
58
|
+
auth: { ...existing.auth, ...(patch.auth ?? {}) },
|
|
59
|
+
});
|
|
60
|
+
const dir = (0, node_path_1.dirname)(path);
|
|
61
|
+
if (!(0, node_fs_1.existsSync)(dir)) {
|
|
62
|
+
(0, node_fs_1.mkdirSync)(dir, { recursive: true, mode: 0o700 });
|
|
63
|
+
}
|
|
64
|
+
(0, node_fs_1.writeFileSync)(path, JSON.stringify(merged, null, 2), {
|
|
65
|
+
encoding: 'utf8',
|
|
66
|
+
mode: 0o644,
|
|
67
|
+
});
|
|
68
|
+
ensureFileMode(dir, 0o700);
|
|
69
|
+
return merged;
|
|
70
|
+
}
|
|
71
|
+
function configExists(path = (0, paths_1.configFile)()) {
|
|
72
|
+
return (0, node_fs_1.existsSync)(path);
|
|
73
|
+
}
|
|
74
|
+
function defaultConfig() {
|
|
75
|
+
return { ...DEFAULT_CONFIG };
|
|
76
|
+
}
|
|
77
|
+
function activeConfigDir() {
|
|
78
|
+
return (0, paths_1.configDir)();
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=config.js.map
|
package/lib/errors.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export type AugustConfigErrorCode = 'CONFIG_NOT_FOUND' | 'CONFIG_INVALID' | 'KEYSTORE_NOT_FOUND' | 'KEYSTORE_INVALID' | 'KEYSTORE_WRONG_PASSPHRASE' | 'KEY_NOT_FOUND' | 'SESSION_NOT_FOUND' | 'SESSION_EXPIRED' | 'SESSION_INVALID' | 'API_KEY_MISSING' | 'RPC_MISSING' | 'PROFILE_NOT_FOUND' | 'PROFILE_INVALID' | 'VAULT_NAME_NOT_FOUND' | 'VAULT_NAME_AMBIGUOUS' | 'VAULT_CHAIN_UNKNOWN';
|
|
2
|
+
export declare class AugustConfigError extends Error {
|
|
3
|
+
readonly code: AugustConfigErrorCode;
|
|
4
|
+
readonly remediation?: string;
|
|
5
|
+
constructor(code: AugustConfigErrorCode, message: string, remediation?: string);
|
|
6
|
+
}
|
package/lib/errors.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AugustConfigError = void 0;
|
|
4
|
+
class AugustConfigError extends Error {
|
|
5
|
+
constructor(code, message, remediation) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = 'AugustConfigError';
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.remediation = remediation;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.AugustConfigError = AugustConfigError;
|
|
13
|
+
//# sourceMappingURL=errors.js.map
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export * from './paths';
|
|
2
|
+
export * from './errors';
|
|
3
|
+
export * from './config';
|
|
4
|
+
export * from './profile';
|
|
5
|
+
export * from './keystore';
|
|
6
|
+
export * from './session';
|
|
7
|
+
export * from './precedence';
|
|
8
|
+
export * from './sdk';
|
|
9
|
+
export * as schemas from './schemas';
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
19
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
20
|
+
};
|
|
21
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
22
|
+
var ownKeys = function(o) {
|
|
23
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
24
|
+
var ar = [];
|
|
25
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
26
|
+
return ar;
|
|
27
|
+
};
|
|
28
|
+
return ownKeys(o);
|
|
29
|
+
};
|
|
30
|
+
return function (mod) {
|
|
31
|
+
if (mod && mod.__esModule) return mod;
|
|
32
|
+
var result = {};
|
|
33
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
34
|
+
__setModuleDefault(result, mod);
|
|
35
|
+
return result;
|
|
36
|
+
};
|
|
37
|
+
})();
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.schemas = void 0;
|
|
40
|
+
__exportStar(require("./paths"), exports);
|
|
41
|
+
__exportStar(require("./errors"), exports);
|
|
42
|
+
__exportStar(require("./config"), exports);
|
|
43
|
+
__exportStar(require("./profile"), exports);
|
|
44
|
+
__exportStar(require("./keystore"), exports);
|
|
45
|
+
__exportStar(require("./session"), exports);
|
|
46
|
+
__exportStar(require("./precedence"), exports);
|
|
47
|
+
__exportStar(require("./sdk"), exports);
|
|
48
|
+
exports.schemas = __importStar(require("./schemas"));
|
|
49
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export interface KeystoreFile {
|
|
2
|
+
version: 1;
|
|
3
|
+
id: string;
|
|
4
|
+
crypto: {
|
|
5
|
+
cipher: 'aes-256-gcm';
|
|
6
|
+
ciphertext: string;
|
|
7
|
+
iv: string;
|
|
8
|
+
tag: string;
|
|
9
|
+
kdf: 'scrypt';
|
|
10
|
+
kdfparams: {
|
|
11
|
+
N: number;
|
|
12
|
+
r: number;
|
|
13
|
+
p: number;
|
|
14
|
+
dklen: number;
|
|
15
|
+
salt: string;
|
|
16
|
+
};
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
export type KeystoreContents = Record<string, string>;
|
|
20
|
+
export declare class Keystore {
|
|
21
|
+
private contents;
|
|
22
|
+
private passphrase;
|
|
23
|
+
private readonly path;
|
|
24
|
+
private constructor();
|
|
25
|
+
static create(passphrase: string, path?: string): Promise<Keystore>;
|
|
26
|
+
static unlock(passphrase: string, path?: string): Promise<Keystore>;
|
|
27
|
+
get(name: string): string | undefined;
|
|
28
|
+
has(name: string): boolean;
|
|
29
|
+
set(name: string, value: string): void;
|
|
30
|
+
delete(name: string): void;
|
|
31
|
+
names(): string[];
|
|
32
|
+
rotate(newPassphrase: string): Promise<void>;
|
|
33
|
+
save(): Promise<void>;
|
|
34
|
+
static safeEqual(a: string, b: string): boolean;
|
|
35
|
+
}
|
|
36
|
+
export declare function keystoreExists(path?: string): boolean;
|
|
37
|
+
export declare function ensureConfigDir(): void;
|
package/lib/keystore.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Keystore = void 0;
|
|
4
|
+
exports.keystoreExists = keystoreExists;
|
|
5
|
+
exports.ensureConfigDir = ensureConfigDir;
|
|
6
|
+
const node_crypto_1 = require("node:crypto");
|
|
7
|
+
const node_fs_1 = require("node:fs");
|
|
8
|
+
const node_path_1 = require("node:path");
|
|
9
|
+
const node_util_1 = require("node:util");
|
|
10
|
+
const errors_1 = require("./errors");
|
|
11
|
+
const paths_1 = require("./paths");
|
|
12
|
+
const scryptAsync = (0, node_util_1.promisify)(node_crypto_1.scrypt);
|
|
13
|
+
const SCRYPT_PARAMS = {
|
|
14
|
+
N: 1 << 17,
|
|
15
|
+
r: 8,
|
|
16
|
+
p: 1,
|
|
17
|
+
maxmem: 256 * 1024 * 1024,
|
|
18
|
+
};
|
|
19
|
+
const KEY_BYTES = 32;
|
|
20
|
+
const IV_BYTES = 12;
|
|
21
|
+
const SALT_BYTES = 16;
|
|
22
|
+
async function deriveKey(passphrase, salt) {
|
|
23
|
+
return scryptAsync(passphrase, salt, KEY_BYTES, SCRYPT_PARAMS);
|
|
24
|
+
}
|
|
25
|
+
function encodePayload(contents) {
|
|
26
|
+
return Buffer.from(JSON.stringify(contents), 'utf8');
|
|
27
|
+
}
|
|
28
|
+
function decodePayload(plaintext) {
|
|
29
|
+
let parsed;
|
|
30
|
+
try {
|
|
31
|
+
parsed = JSON.parse(plaintext.toString('utf8'));
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
throw new errors_1.AugustConfigError('KEYSTORE_INVALID', 'Keystore payload is not valid JSON after decryption.', 'The keystore file may be corrupted. Restore from backup or re-run `august init`.');
|
|
35
|
+
}
|
|
36
|
+
if (!parsed ||
|
|
37
|
+
typeof parsed !== 'object' ||
|
|
38
|
+
Array.isArray(parsed) ||
|
|
39
|
+
Object.values(parsed).some((v) => typeof v !== 'string')) {
|
|
40
|
+
throw new errors_1.AugustConfigError('KEYSTORE_INVALID', 'Keystore payload has unexpected shape.', 'The keystore file may be corrupted. Restore from backup or re-run `august init`.');
|
|
41
|
+
}
|
|
42
|
+
return parsed;
|
|
43
|
+
}
|
|
44
|
+
function ensureFileMode(path, mode) {
|
|
45
|
+
try {
|
|
46
|
+
(0, node_fs_1.chmodSync)(path, mode);
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
class Keystore {
|
|
52
|
+
constructor(contents, passphrase, path) {
|
|
53
|
+
this.contents = contents;
|
|
54
|
+
this.passphrase = passphrase;
|
|
55
|
+
this.path = path;
|
|
56
|
+
}
|
|
57
|
+
static async create(passphrase, path = (0, paths_1.keystoreFile)()) {
|
|
58
|
+
return new Keystore({}, passphrase, path);
|
|
59
|
+
}
|
|
60
|
+
static async unlock(passphrase, path = (0, paths_1.keystoreFile)()) {
|
|
61
|
+
if (!(0, node_fs_1.existsSync)(path)) {
|
|
62
|
+
throw new errors_1.AugustConfigError('KEYSTORE_NOT_FOUND', `No keystore found at ${path}.`, 'Run `august init` to create one.');
|
|
63
|
+
}
|
|
64
|
+
let parsed;
|
|
65
|
+
try {
|
|
66
|
+
parsed = JSON.parse((0, node_fs_1.readFileSync)(path, 'utf8'));
|
|
67
|
+
}
|
|
68
|
+
catch {
|
|
69
|
+
throw new errors_1.AugustConfigError('KEYSTORE_INVALID', `Keystore at ${path} is not valid JSON.`, 'Restore from backup or re-run `august init`.');
|
|
70
|
+
}
|
|
71
|
+
if (parsed?.version !== 1 ||
|
|
72
|
+
parsed.crypto?.cipher !== 'aes-256-gcm' ||
|
|
73
|
+
parsed.crypto.kdf !== 'scrypt') {
|
|
74
|
+
throw new errors_1.AugustConfigError('KEYSTORE_INVALID', `Keystore at ${path} has unsupported version or crypto fields.`, 'Re-run `august init` to recreate it with the current format.');
|
|
75
|
+
}
|
|
76
|
+
const { ciphertext, iv, tag, kdfparams } = parsed.crypto;
|
|
77
|
+
const salt = Buffer.from(kdfparams.salt, 'hex');
|
|
78
|
+
const key = await scryptAsync(passphrase, salt, kdfparams.dklen, {
|
|
79
|
+
N: kdfparams.N,
|
|
80
|
+
r: kdfparams.r,
|
|
81
|
+
p: kdfparams.p,
|
|
82
|
+
maxmem: SCRYPT_PARAMS.maxmem,
|
|
83
|
+
});
|
|
84
|
+
const decipher = (0, node_crypto_1.createDecipheriv)('aes-256-gcm', key, Buffer.from(iv, 'hex'));
|
|
85
|
+
decipher.setAuthTag(Buffer.from(tag, 'hex'));
|
|
86
|
+
let plaintext;
|
|
87
|
+
try {
|
|
88
|
+
plaintext = Buffer.concat([
|
|
89
|
+
decipher.update(Buffer.from(ciphertext, 'hex')),
|
|
90
|
+
decipher.final(),
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
throw new errors_1.AugustConfigError('KEYSTORE_WRONG_PASSPHRASE', 'Failed to decrypt keystore — passphrase is incorrect or file is corrupted.', 'Re-enter your passphrase, or run `august init --force` to recreate the keystore.');
|
|
95
|
+
}
|
|
96
|
+
const contents = decodePayload(plaintext);
|
|
97
|
+
return new Keystore(contents, passphrase, path);
|
|
98
|
+
}
|
|
99
|
+
get(name) {
|
|
100
|
+
return this.contents[name];
|
|
101
|
+
}
|
|
102
|
+
has(name) {
|
|
103
|
+
return Object.prototype.hasOwnProperty.call(this.contents, name);
|
|
104
|
+
}
|
|
105
|
+
set(name, value) {
|
|
106
|
+
this.contents[name] = value;
|
|
107
|
+
}
|
|
108
|
+
delete(name) {
|
|
109
|
+
delete this.contents[name];
|
|
110
|
+
}
|
|
111
|
+
names() {
|
|
112
|
+
return Object.keys(this.contents);
|
|
113
|
+
}
|
|
114
|
+
async rotate(newPassphrase) {
|
|
115
|
+
this.passphrase = newPassphrase;
|
|
116
|
+
await this.save();
|
|
117
|
+
}
|
|
118
|
+
async save() {
|
|
119
|
+
const salt = (0, node_crypto_1.randomBytes)(SALT_BYTES);
|
|
120
|
+
const iv = (0, node_crypto_1.randomBytes)(IV_BYTES);
|
|
121
|
+
const key = await deriveKey(this.passphrase, salt);
|
|
122
|
+
const cipher = (0, node_crypto_1.createCipheriv)('aes-256-gcm', key, iv);
|
|
123
|
+
const ciphertext = Buffer.concat([
|
|
124
|
+
cipher.update(encodePayload(this.contents)),
|
|
125
|
+
cipher.final(),
|
|
126
|
+
]);
|
|
127
|
+
const tag = cipher.getAuthTag();
|
|
128
|
+
const file = {
|
|
129
|
+
version: 1,
|
|
130
|
+
id: (0, node_crypto_1.randomBytes)(16).toString('hex'),
|
|
131
|
+
crypto: {
|
|
132
|
+
cipher: 'aes-256-gcm',
|
|
133
|
+
ciphertext: ciphertext.toString('hex'),
|
|
134
|
+
iv: iv.toString('hex'),
|
|
135
|
+
tag: tag.toString('hex'),
|
|
136
|
+
kdf: 'scrypt',
|
|
137
|
+
kdfparams: {
|
|
138
|
+
N: SCRYPT_PARAMS.N,
|
|
139
|
+
r: SCRYPT_PARAMS.r,
|
|
140
|
+
p: SCRYPT_PARAMS.p,
|
|
141
|
+
dklen: KEY_BYTES,
|
|
142
|
+
salt: salt.toString('hex'),
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
const dir = (0, node_path_1.dirname)(this.path);
|
|
147
|
+
if (!(0, node_fs_1.existsSync)(dir)) {
|
|
148
|
+
(0, node_fs_1.mkdirSync)(dir, { recursive: true, mode: 0o700 });
|
|
149
|
+
}
|
|
150
|
+
(0, node_fs_1.writeFileSync)(this.path, JSON.stringify(file, null, 2), {
|
|
151
|
+
encoding: 'utf8',
|
|
152
|
+
mode: 0o600,
|
|
153
|
+
});
|
|
154
|
+
ensureFileMode(this.path, 0o600);
|
|
155
|
+
ensureFileMode(dir, 0o700);
|
|
156
|
+
}
|
|
157
|
+
static safeEqual(a, b) {
|
|
158
|
+
const ab = Buffer.from(a, 'utf8');
|
|
159
|
+
const bb = Buffer.from(b, 'utf8');
|
|
160
|
+
if (ab.length !== bb.length)
|
|
161
|
+
return false;
|
|
162
|
+
return (0, node_crypto_1.timingSafeEqual)(ab, bb);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
exports.Keystore = Keystore;
|
|
166
|
+
function keystoreExists(path = (0, paths_1.keystoreFile)()) {
|
|
167
|
+
return (0, node_fs_1.existsSync)(path);
|
|
168
|
+
}
|
|
169
|
+
function ensureConfigDir() {
|
|
170
|
+
const dir = (0, paths_1.configDir)();
|
|
171
|
+
if (!(0, node_fs_1.existsSync)(dir)) {
|
|
172
|
+
(0, node_fs_1.mkdirSync)(dir, { recursive: true, mode: 0o700 });
|
|
173
|
+
}
|
|
174
|
+
ensureFileMode(dir, 0o700);
|
|
175
|
+
}
|
|
176
|
+
//# sourceMappingURL=keystore.js.map
|
package/lib/paths.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare function configDir(): string;
|
|
2
|
+
export declare function configFile(): string;
|
|
3
|
+
export declare function keystoreFile(): string;
|
|
4
|
+
export declare function sessionFile(): string;
|
|
5
|
+
export declare function profilesDir(): string;
|
|
6
|
+
export declare function profileFile(name: string): string;
|
package/lib/paths.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.configDir = configDir;
|
|
4
|
+
exports.configFile = configFile;
|
|
5
|
+
exports.keystoreFile = keystoreFile;
|
|
6
|
+
exports.sessionFile = sessionFile;
|
|
7
|
+
exports.profilesDir = profilesDir;
|
|
8
|
+
exports.profileFile = profileFile;
|
|
9
|
+
const node_os_1 = require("node:os");
|
|
10
|
+
const node_path_1 = require("node:path");
|
|
11
|
+
const DEFAULT_DIR_NAME = '.augustdigital';
|
|
12
|
+
function configDir() {
|
|
13
|
+
const override = process.env.AUGUST_CONFIG_DIR?.trim();
|
|
14
|
+
if (override)
|
|
15
|
+
return (0, node_path_1.resolve)(override);
|
|
16
|
+
return (0, node_path_1.join)((0, node_os_1.homedir)(), DEFAULT_DIR_NAME);
|
|
17
|
+
}
|
|
18
|
+
function configFile() {
|
|
19
|
+
return (0, node_path_1.join)(configDir(), 'config.json');
|
|
20
|
+
}
|
|
21
|
+
function keystoreFile() {
|
|
22
|
+
return (0, node_path_1.join)(configDir(), 'keystore.json');
|
|
23
|
+
}
|
|
24
|
+
function sessionFile() {
|
|
25
|
+
return (0, node_path_1.join)(configDir(), '.session');
|
|
26
|
+
}
|
|
27
|
+
function profilesDir() {
|
|
28
|
+
return (0, node_path_1.join)(configDir(), 'profiles');
|
|
29
|
+
}
|
|
30
|
+
function profileFile(name) {
|
|
31
|
+
return (0, node_path_1.join)(profilesDir(), `${name}.json`);
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=paths.js.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Config } from './config';
|
|
2
|
+
export interface ResolveOptions {
|
|
3
|
+
config?: Config;
|
|
4
|
+
profileName?: string;
|
|
5
|
+
cliApiKey?: string;
|
|
6
|
+
cliRpcUrl?: string;
|
|
7
|
+
cliRpcChainId?: number;
|
|
8
|
+
}
|
|
9
|
+
export declare function getApiKey(opts?: ResolveOptions): string;
|
|
10
|
+
export declare function getRpcUrl(chainId: number, opts?: ResolveOptions): string;
|
|
11
|
+
export declare function getDefaultChainId(opts?: ResolveOptions): number;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getApiKey = getApiKey;
|
|
4
|
+
exports.getRpcUrl = getRpcUrl;
|
|
5
|
+
exports.getDefaultChainId = getDefaultChainId;
|
|
6
|
+
const sdk_1 = require("@augustdigital/sdk");
|
|
7
|
+
const errors_1 = require("./errors");
|
|
8
|
+
const config_1 = require("./config");
|
|
9
|
+
const profile_1 = require("./profile");
|
|
10
|
+
const session_1 = require("./session");
|
|
11
|
+
function resolveProfile(config, profileName) {
|
|
12
|
+
const target = profileName ?? process.env.AUGUST_PROFILE ?? config.defaultProfile;
|
|
13
|
+
if (!target)
|
|
14
|
+
return null;
|
|
15
|
+
try {
|
|
16
|
+
return (0, profile_1.loadProfile)(target);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function getApiKey(opts = {}) {
|
|
23
|
+
if (opts.cliApiKey && opts.cliApiKey.length > 0)
|
|
24
|
+
return opts.cliApiKey;
|
|
25
|
+
const envKey = process.env.AUGUST_API_KEY?.trim();
|
|
26
|
+
if (envKey)
|
|
27
|
+
return envKey;
|
|
28
|
+
try {
|
|
29
|
+
const session = (0, session_1.readSession)();
|
|
30
|
+
if (session?.apiKey)
|
|
31
|
+
return session.apiKey;
|
|
32
|
+
}
|
|
33
|
+
catch {
|
|
34
|
+
}
|
|
35
|
+
throw new errors_1.AugustConfigError('API_KEY_MISSING', 'No August API key is available.', 'Run `august login` to unlock your keystore, or set AUGUST_API_KEY.');
|
|
36
|
+
}
|
|
37
|
+
function getRpcUrl(chainId, opts = {}) {
|
|
38
|
+
if (opts.cliRpcUrl &&
|
|
39
|
+
(opts.cliRpcChainId === undefined || opts.cliRpcChainId === chainId)) {
|
|
40
|
+
return opts.cliRpcUrl;
|
|
41
|
+
}
|
|
42
|
+
const envUrl = process.env[`AUGUST_RPC_${chainId}`]?.trim();
|
|
43
|
+
if (envUrl)
|
|
44
|
+
return envUrl;
|
|
45
|
+
const config = opts.config ?? (0, config_1.loadConfig)();
|
|
46
|
+
const profile = resolveProfile(config, opts.profileName);
|
|
47
|
+
const profileRpc = profile?.rpcs?.[String(chainId)];
|
|
48
|
+
if (profileRpc)
|
|
49
|
+
return profileRpc;
|
|
50
|
+
const configRpc = config.rpcs?.[String(chainId)];
|
|
51
|
+
if (configRpc)
|
|
52
|
+
return configRpc;
|
|
53
|
+
const [publicFallback] = (0, sdk_1.getFallbackRpcUrls)(chainId);
|
|
54
|
+
if (publicFallback)
|
|
55
|
+
return publicFallback;
|
|
56
|
+
throw new errors_1.AugustConfigError('RPC_MISSING', `No RPC URL configured for chain ${chainId} and no public fallback is available.`, `Set \`AUGUST_RPC_${chainId}=<url>\`, run \`august config set rpcs.${chainId} <url>\`, or pass \`--rpc <url> --chain ${chainId}\`.`);
|
|
57
|
+
}
|
|
58
|
+
function getDefaultChainId(opts = {}) {
|
|
59
|
+
if (opts.cliRpcChainId !== undefined)
|
|
60
|
+
return opts.cliRpcChainId;
|
|
61
|
+
const envChain = process.env.AUGUST_CHAIN_ID?.trim();
|
|
62
|
+
if (envChain && Number.isFinite(Number(envChain))) {
|
|
63
|
+
return Number(envChain);
|
|
64
|
+
}
|
|
65
|
+
const config = opts.config ?? (0, config_1.loadConfig)();
|
|
66
|
+
const profile = resolveProfile(config, opts.profileName);
|
|
67
|
+
return profile?.defaultChainId ?? config.defaultChainId;
|
|
68
|
+
}
|
|
69
|
+
//# sourceMappingURL=precedence.js.map
|
package/lib/profile.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
declare const ProfileSchema: z.ZodObject<{
|
|
3
|
+
schemaVersion: z.ZodDefault<z.ZodLiteral<1>>;
|
|
4
|
+
name: z.ZodString;
|
|
5
|
+
defaultChainId: z.ZodOptional<z.ZodNumber>;
|
|
6
|
+
rpcs: z.ZodDefault<z.ZodRecord<z.ZodString, z.ZodString>>;
|
|
7
|
+
notes: z.ZodOptional<z.ZodString>;
|
|
8
|
+
}, "strip", z.ZodTypeAny, {
|
|
9
|
+
schemaVersion?: 1;
|
|
10
|
+
defaultChainId?: number;
|
|
11
|
+
rpcs?: Record<string, string>;
|
|
12
|
+
name?: string;
|
|
13
|
+
notes?: string;
|
|
14
|
+
}, {
|
|
15
|
+
schemaVersion?: 1;
|
|
16
|
+
defaultChainId?: number;
|
|
17
|
+
rpcs?: Record<string, string>;
|
|
18
|
+
name?: string;
|
|
19
|
+
notes?: string;
|
|
20
|
+
}>;
|
|
21
|
+
export type Profile = z.infer<typeof ProfileSchema>;
|
|
22
|
+
export declare function saveProfile(profile: Profile): Profile;
|
|
23
|
+
export declare function loadProfile(name: string): Profile;
|
|
24
|
+
export declare function listProfiles(): string[];
|
|
25
|
+
export declare function deleteProfile(name: string): void;
|
|
26
|
+
export {};
|