tenjin-cli 0.1.0-alpha.5 → 0.1.0-alpha.8
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/README.md +169 -199
- package/dist/{buy-YMXXVNFH.js → buy-K2G6XQY2.js} +20 -18
- package/dist/{candidate-H7FR4AU5.js → candidate-DJB2NNTG.js} +7 -8
- package/dist/{chunk-ZW3B5LWX.js → chunk-2YX6LTCD.js} +5 -5
- package/dist/{chunk-4WVKXKDK.js → chunk-6OE2IJMH.js} +29 -238
- package/dist/chunk-6OE2IJMH.js.map +1 -0
- package/dist/{chunk-ADJYSM45.js → chunk-6ZBZCBR7.js} +27 -13
- package/dist/chunk-6ZBZCBR7.js.map +1 -0
- package/dist/{chunk-HTDALK7M.js → chunk-B7YB4MMU.js} +15 -12
- package/dist/{chunk-HTDALK7M.js.map → chunk-B7YB4MMU.js.map} +1 -1
- package/dist/{chunk-NFSOVL3C.js → chunk-BBUYCDSG.js} +12 -3
- package/dist/chunk-BBUYCDSG.js.map +1 -0
- package/dist/chunk-C3S6725N.js +337 -0
- package/dist/chunk-C3S6725N.js.map +1 -0
- package/dist/chunk-CYRYVZPN.js +213 -0
- package/dist/chunk-CYRYVZPN.js.map +1 -0
- package/dist/{chunk-MDDGEGSW.js → chunk-DAJ56EUU.js} +3 -3
- package/dist/{chunk-IOPWPMT2.js → chunk-DH6D6QIO.js} +5 -5
- package/dist/{chunk-NJF2WENW.js → chunk-DQDGBWWH.js} +4 -2
- package/dist/chunk-DQDGBWWH.js.map +1 -0
- package/dist/chunk-EDENZN2Q.js +614 -0
- package/dist/chunk-EDENZN2Q.js.map +1 -0
- package/dist/chunk-GABYDXWL.js +190 -0
- package/dist/chunk-GABYDXWL.js.map +1 -0
- package/dist/{chunk-LAM66WFJ.js → chunk-GM4RZUVU.js} +6 -8
- package/dist/{chunk-LAM66WFJ.js.map → chunk-GM4RZUVU.js.map} +1 -1
- package/dist/{chunk-PAUXMRSF.js → chunk-J6CVKC4M.js} +73 -2
- package/dist/chunk-J6CVKC4M.js.map +1 -0
- package/dist/chunk-L6AV2Q73.js +105 -0
- package/dist/chunk-L6AV2Q73.js.map +1 -0
- package/dist/{chunk-PYNSGYBE.js → chunk-NVHQCTIC.js} +249 -552
- package/dist/chunk-NVHQCTIC.js.map +1 -0
- package/dist/{chunk-CVX6JNGD.js → chunk-OTDI5FXG.js} +3 -5
- package/dist/{chunk-CVX6JNGD.js.map → chunk-OTDI5FXG.js.map} +1 -1
- package/dist/{chunk-3IRDP7TN.js → chunk-P3FBESBB.js} +6 -4
- package/dist/{chunk-3IRDP7TN.js.map → chunk-P3FBESBB.js.map} +1 -1
- package/dist/{chunk-FU6MCSLG.js → chunk-PCLPVJ3J.js} +16 -78
- package/dist/chunk-PCLPVJ3J.js.map +1 -0
- package/dist/chunk-QB2NNV6C.js +143 -0
- package/dist/chunk-QB2NNV6C.js.map +1 -0
- package/dist/{chunk-C444K6MI.js → chunk-RGFBBNGP.js} +42 -17
- package/dist/{chunk-C444K6MI.js.map → chunk-RGFBBNGP.js.map} +1 -1
- package/dist/{chunk-TXPPMK4L.js → chunk-RGTTACP3.js} +5 -5
- package/dist/chunk-SGK2B724.js +67 -0
- package/dist/chunk-SGK2B724.js.map +1 -0
- package/dist/{chunk-ZDY2KY5X.js → chunk-URSGGRO4.js} +28 -11
- package/dist/chunk-URSGGRO4.js.map +1 -0
- package/dist/{chunk-5SCU253N.js → chunk-UYP7G3GZ.js} +2 -2
- package/dist/{chunk-ZIYVN4CD.js → chunk-VZ3AJCO5.js} +3 -5
- package/dist/{chunk-ZIYVN4CD.js.map → chunk-VZ3AJCO5.js.map} +1 -1
- package/dist/{chunk-QONTBYQW.js → chunk-WRWCDLVK.js} +4 -6
- package/dist/{chunk-QONTBYQW.js.map → chunk-WRWCDLVK.js.map} +1 -1
- package/dist/chunk-XBJ3VLWX.js +35 -0
- package/dist/chunk-XBJ3VLWX.js.map +1 -0
- package/dist/{chunk-RTJLLPFA.js → chunk-XFHZWORF.js} +123 -24
- package/dist/chunk-XFHZWORF.js.map +1 -0
- package/dist/{chunk-BIKAUVC2.js → chunk-ZA3RFDAV.js} +40 -6
- package/dist/chunk-ZA3RFDAV.js.map +1 -0
- package/dist/{cli-IW76QTN3.js → cli-KNCYWMAG.js} +237 -25
- package/dist/cli-KNCYWMAG.js.map +1 -0
- package/dist/{config-VCOS7P5K.js → config-SAQEQRFB.js} +5 -6
- package/dist/dist-U4M6QCKE.js +2842 -0
- package/dist/dist-U4M6QCKE.js.map +1 -0
- package/dist/{doctor-WS65JG3V.js → doctor-FBRQNOUQ.js} +10 -9
- package/dist/{publish-DSFWQRUN.js → edit-IZZ2HIMR.js} +20 -18
- package/dist/index.js +1 -1
- package/dist/{inspect-SFJWUQHO.js → inspect-BFWZ5XOP.js} +17 -16
- package/dist/{install-RVPQPCKH.js → install-QXZCHIBT.js} +400 -166
- package/dist/install-QXZCHIBT.js.map +1 -0
- package/dist/outcome-VXCBEGNC.js +20 -0
- package/dist/publish-5NTCRF2V.js +40 -0
- package/dist/read-T6OTMU72.js +158 -0
- package/dist/read-T6OTMU72.js.map +1 -0
- package/dist/{run-OMPKA43G.js → run-5OGX64IY.js} +106 -32
- package/dist/{run-OMPKA43G.js.map → run-5OGX64IY.js.map} +1 -1
- package/dist/search-LIIWUMWV.js +24 -0
- package/dist/{send-3G4C64HJ.js → send-ENE47KQE.js} +7 -7
- package/dist/session-HY4QIWHV.js +87 -0
- package/dist/session-HY4QIWHV.js.map +1 -0
- package/dist/{wallet-PP22LAX7.js → wallet-OCCGEH6H.js} +7 -7
- package/dist/{wallet-B7KJS2R5.js → wallet-RBDVNZVR.js} +5 -5
- package/dist/wallet-RBDVNZVR.js.map +1 -0
- package/docs/agent-permissions.md +255 -0
- package/package.json +3 -1
- package/skills/tenjin/SKILL.md +57 -13
- package/skills/tenjin-publish/SKILL.md +12 -4
- package/skills/tenjin-search/SKILL.md +68 -12
- package/dist/chunk-2JQ5JSG6.js +0 -68
- package/dist/chunk-2JQ5JSG6.js.map +0 -1
- package/dist/chunk-4WVKXKDK.js.map +0 -1
- package/dist/chunk-ADJYSM45.js.map +0 -1
- package/dist/chunk-BIKAUVC2.js.map +0 -1
- package/dist/chunk-FU6MCSLG.js.map +0 -1
- package/dist/chunk-L4IHCN6B.js +0 -63
- package/dist/chunk-L4IHCN6B.js.map +0 -1
- package/dist/chunk-NFSOVL3C.js.map +0 -1
- package/dist/chunk-NJF2WENW.js.map +0 -1
- package/dist/chunk-PAUXMRSF.js.map +0 -1
- package/dist/chunk-PYNSGYBE.js.map +0 -1
- package/dist/chunk-RTJLLPFA.js.map +0 -1
- package/dist/chunk-ZDY2KY5X.js.map +0 -1
- package/dist/cli-IW76QTN3.js.map +0 -1
- package/dist/install-RVPQPCKH.js.map +0 -1
- package/dist/outcome-GNYCSZ7J.js +0 -20
- package/dist/search-KOI6Y7P6.js +0 -24
- /package/dist/{buy-YMXXVNFH.js.map → buy-K2G6XQY2.js.map} +0 -0
- /package/dist/{candidate-H7FR4AU5.js.map → candidate-DJB2NNTG.js.map} +0 -0
- /package/dist/{chunk-ZW3B5LWX.js.map → chunk-2YX6LTCD.js.map} +0 -0
- /package/dist/{chunk-MDDGEGSW.js.map → chunk-DAJ56EUU.js.map} +0 -0
- /package/dist/{chunk-IOPWPMT2.js.map → chunk-DH6D6QIO.js.map} +0 -0
- /package/dist/{chunk-TXPPMK4L.js.map → chunk-RGTTACP3.js.map} +0 -0
- /package/dist/{chunk-5SCU253N.js.map → chunk-UYP7G3GZ.js.map} +0 -0
- /package/dist/{config-VCOS7P5K.js.map → config-SAQEQRFB.js.map} +0 -0
- /package/dist/{doctor-WS65JG3V.js.map → doctor-FBRQNOUQ.js.map} +0 -0
- /package/dist/{inspect-SFJWUQHO.js.map → edit-IZZ2HIMR.js.map} +0 -0
- /package/dist/{outcome-GNYCSZ7J.js.map → inspect-BFWZ5XOP.js.map} +0 -0
- /package/dist/{publish-DSFWQRUN.js.map → outcome-VXCBEGNC.js.map} +0 -0
- /package/dist/{search-KOI6Y7P6.js.map → publish-5NTCRF2V.js.map} +0 -0
- /package/dist/{wallet-B7KJS2R5.js.map → search-LIIWUMWV.js.map} +0 -0
- /package/dist/{send-3G4C64HJ.js.map → send-ENE47KQE.js.map} +0 -0
- /package/dist/{wallet-PP22LAX7.js.map → wallet-OCCGEH6H.js.map} +0 -0
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
buildSiwxHeader
|
|
4
|
+
} from "./chunk-PCLPVJ3J.js";
|
|
5
|
+
import {
|
|
6
|
+
isSessionUsable,
|
|
7
|
+
loadSessionFile,
|
|
8
|
+
signWithSession
|
|
9
|
+
} from "./chunk-GABYDXWL.js";
|
|
10
|
+
import {
|
|
11
|
+
originOf
|
|
12
|
+
} from "./chunk-XBJ3VLWX.js";
|
|
13
|
+
import {
|
|
14
|
+
sessionPath,
|
|
15
|
+
writeFileAtomic
|
|
16
|
+
} from "./chunk-J6CVKC4M.js";
|
|
17
|
+
|
|
18
|
+
// src/lib/session-key.ts
|
|
19
|
+
import { webcrypto } from "crypto";
|
|
20
|
+
var SESSION_CHAIN_ID = "eip155:8453";
|
|
21
|
+
var SESSION_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
22
|
+
var subtle = webcrypto.subtle;
|
|
23
|
+
function generateP256KeyPair() {
|
|
24
|
+
return subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, [
|
|
25
|
+
"sign",
|
|
26
|
+
"verify"
|
|
27
|
+
]);
|
|
28
|
+
}
|
|
29
|
+
function toBase64Url(bytes) {
|
|
30
|
+
return Buffer.from(bytes).toString("base64url");
|
|
31
|
+
}
|
|
32
|
+
function delegationResources(publicKeyRaw, expIso, scope) {
|
|
33
|
+
return [
|
|
34
|
+
`urn:tenjin:session:pubkey:p256:${publicKeyRaw}`,
|
|
35
|
+
`urn:tenjin:session:exp:${expIso}`,
|
|
36
|
+
`urn:tenjin:session:scope:${scope}`
|
|
37
|
+
];
|
|
38
|
+
}
|
|
39
|
+
async function establishSession(config, deps = {}) {
|
|
40
|
+
const now = deps.now ?? Date.now;
|
|
41
|
+
const pair = await generateP256KeyPair();
|
|
42
|
+
const rawPub = new Uint8Array(await subtle.exportKey("raw", pair.publicKey));
|
|
43
|
+
const publicKeyRaw = toBase64Url(rawPub);
|
|
44
|
+
const jwk = await subtle.exportKey("jwk", pair.privateKey);
|
|
45
|
+
const expIso = new Date(now() + SESSION_TTL_MS).toISOString();
|
|
46
|
+
const delegation = await buildSiwxHeader(config.signer, {
|
|
47
|
+
baseUrl: config.baseUrl,
|
|
48
|
+
chainId: config.chainId,
|
|
49
|
+
ttlMs: SESSION_TTL_MS,
|
|
50
|
+
statement: "Delegate a Tenjin session key.",
|
|
51
|
+
resources: delegationResources(publicKeyRaw, expIso, config.scope)
|
|
52
|
+
});
|
|
53
|
+
const file = {
|
|
54
|
+
address: config.signer.address.toLowerCase(),
|
|
55
|
+
origin: originOf(config.baseUrl),
|
|
56
|
+
delegation,
|
|
57
|
+
exp: expIso,
|
|
58
|
+
scope: config.scope,
|
|
59
|
+
publicKeyRaw,
|
|
60
|
+
privateKeyJwk: jwk
|
|
61
|
+
};
|
|
62
|
+
await saveSessionFile(config.dataDir, file);
|
|
63
|
+
return file;
|
|
64
|
+
}
|
|
65
|
+
async function saveSessionFile(dir, file) {
|
|
66
|
+
await writeFileAtomic(sessionPath(dir), `${JSON.stringify(file, null, 2)}
|
|
67
|
+
`, {
|
|
68
|
+
mode: 384,
|
|
69
|
+
dirMode: 448
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
function recoveryFor(code) {
|
|
73
|
+
switch (code) {
|
|
74
|
+
case "proof_expired":
|
|
75
|
+
return "resign";
|
|
76
|
+
case "session_expired":
|
|
77
|
+
case "proof_revoked":
|
|
78
|
+
case "insufficient_scope":
|
|
79
|
+
return "reestablish";
|
|
80
|
+
case "session_key_unbound":
|
|
81
|
+
return "fatal";
|
|
82
|
+
default:
|
|
83
|
+
return "fatal";
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function createSessionKeyAuth(config, deps = {}) {
|
|
87
|
+
const now = deps.now ?? Date.now;
|
|
88
|
+
let cached = null;
|
|
89
|
+
let forceReestablish = false;
|
|
90
|
+
const ensure = async () => {
|
|
91
|
+
if (!forceReestablish && cached !== null && isSessionUsable(cached, config.signer.address, now(), config.scope, originOf(config.baseUrl))) {
|
|
92
|
+
return cached;
|
|
93
|
+
}
|
|
94
|
+
if (!forceReestablish) {
|
|
95
|
+
const onDisk = await loadSessionFile(config.dataDir);
|
|
96
|
+
const origin = originOf(config.baseUrl);
|
|
97
|
+
if (onDisk !== null && isSessionUsable(onDisk, config.signer.address, now(), config.scope, origin)) {
|
|
98
|
+
cached = onDisk;
|
|
99
|
+
return cached;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
cached = await establishSession(config, deps);
|
|
103
|
+
forceReestablish = false;
|
|
104
|
+
return cached;
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
async headersFor(req) {
|
|
108
|
+
const file = await ensure();
|
|
109
|
+
return signWithSession(file, req, deps);
|
|
110
|
+
},
|
|
111
|
+
async recover(code) {
|
|
112
|
+
const recovery = recoveryFor(code);
|
|
113
|
+
if (recovery === "fatal") return false;
|
|
114
|
+
if (recovery === "reestablish") {
|
|
115
|
+
cached = null;
|
|
116
|
+
forceReestablish = true;
|
|
117
|
+
}
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
function createSiwxAuth(config) {
|
|
123
|
+
return {
|
|
124
|
+
async headersFor() {
|
|
125
|
+
const header = await buildSiwxHeader(config.signer, {
|
|
126
|
+
baseUrl: config.baseUrl,
|
|
127
|
+
chainId: config.chainId
|
|
128
|
+
});
|
|
129
|
+
return { "SIGN-IN-WITH-X": header };
|
|
130
|
+
},
|
|
131
|
+
async recover(code) {
|
|
132
|
+
return code === "nonce_already_used" || code === "invalid_proof" || code === "proof_expired";
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
export {
|
|
138
|
+
SESSION_CHAIN_ID,
|
|
139
|
+
establishSession,
|
|
140
|
+
createSessionKeyAuth,
|
|
141
|
+
createSiwxAuth
|
|
142
|
+
};
|
|
143
|
+
//# sourceMappingURL=chunk-QB2NNV6C.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/lib/session-key.ts"],"sourcesContent":["import { webcrypto } from 'node:crypto';\nimport { sessionPath } from './paths';\nimport { writeFileAtomic } from './atomic-json';\nimport { buildSiwxHeader } from './siwx';\nimport { originOf } from './url';\nimport {\n isSessionUsable,\n loadSessionFile,\n signWithSession,\n type SessionFile,\n type SessionKeyDeps,\n type SessionScope,\n type SignableRequest,\n} from './session-present';\nimport type { TenjinSigner } from './wallet/provider';\n\n/**\n * Session-key delegation (RFC 9421 signed HTTP, RFC 9530-shaped Content-Digest),\n * verified byte-for-byte against the live \"Auth — session keys\" contract in\n * https://tenjin.blog/llms-full.txt (D35).\n *\n * The point: a plain SIWX write burns a single-use nonce, so every write needs a\n * fresh WALLET signature. A session key trades that for ONE wallet signature per\n * session: generate a P-256 keypair, wallet-sign a SIWX message binding its\n * pubkey/exp/scope, then sign each subsequent write with the P-256 key (no wallet\n * popup) until the delegation expires. The delegated key is short-lived (≤24h,\n * server-clamped) and cached 0600, address-bound so a wallet change invalidates it.\n *\n * This module is the MINT half — everything that needs a wallet signer, i.e.\n * `establishSession`, the cache writer, and the two `WriteAuth` implementations.\n * The present-only half (load a file, sign one request with it) lives in\n * `session-present.ts` and is re-exported below, so `posts-api`/`publish`/`edit`\n * keep importing one module while `read` can import the present half ALONE and\n * stay structurally unable to open a keystore. Do not move a signer-touching\n * function down into `session-present.ts`: read's import-graph pin is what makes\n * \"read cannot mint and cannot pay\" a property of the code rather than a promise.\n *\n * A minted delegation is a wallet-derived credential, so it is bound to the\n * ORIGIN it was minted against and every presenter re-checks that binding.\n *\n * Never hand-rolls crypto: P-256 keygen is node:crypto webcrypto (subtle) and the\n * wallet delegation reuses the siwx.ts seam.\n */\n\nexport {\n contentDigest,\n isSessionPresentable,\n readSessionFile,\n isSessionUsable,\n keyidFor,\n loadSessionFile,\n scopeSatisfies,\n signatureBase,\n signatureParams,\n signWithSession,\n targetUri,\n} from './session-present';\nexport type {\n SessionFile,\n SessionFileState,\n SessionKeyDeps,\n SessionScope,\n SignableRequest,\n SignatureParamsInput,\n} from './session-present';\n\n/**\n * The chain a session delegation is signed over. Writes require Base mainnet per\n * the server's SIWX chain constraint, and a session covers reads and writes\n * alike, so there is exactly ONE session chain id rather than a per-caller\n * choice: `session start` and the write path must mint against the same chain or\n * the file they share would flip between two delegations.\n */\nexport const SESSION_CHAIN_ID = 'eip155:8453';\n\nconst SESSION_TTL_MS = 24 * 60 * 60 * 1000; // 24h; the server clamps to ≤24h.\n\n/**\n * The write-auth seam a posts client signs through. `headersFor` attaches the\n * signature headers (establishing the session lazily on first use); `recover`\n * reacts to a 401 by returning whether a retry is worthwhile.\n */\nexport interface WriteAuth {\n headersFor(req: SignableRequest): Promise<Record<string, string>>;\n /** React to a write's 401 `code`; true ⇒ the next headersFor retry may succeed. */\n recover(code: string | undefined): Promise<boolean>;\n}\n\nexport interface SessionKeyConfig {\n signer: TenjinSigner;\n baseUrl: string;\n chainId: string;\n dataDir: string;\n /**\n * The scope this session is minted at, and the scope a cached session must\n * cover to be reused. Required, not defaulted: over-granting silently is the\n * failure mode worth making impossible, so every caller states what it needs.\n */\n scope: SessionScope;\n}\n\nconst subtle = webcrypto.subtle;\n\nfunction generateP256KeyPair(): Promise<webcrypto.CryptoKeyPair> {\n return subtle.generateKey({ name: 'ECDSA', namedCurve: 'P-256' }, true, [\n 'sign',\n 'verify',\n ]) as Promise<webcrypto.CryptoKeyPair>;\n}\n\n/** base64url (no padding) of raw bytes — the pubkey/keyid encoding. */\nfunction toBase64Url(bytes: Uint8Array): string {\n return Buffer.from(bytes).toString('base64url');\n}\n\n/** The three URNs bound into the delegation's SIWX `resources` array (D35). */\nexport function delegationResources(\n publicKeyRaw: string,\n expIso: string,\n scope: SessionScope,\n): string[] {\n return [\n `urn:tenjin:session:pubkey:p256:${publicKeyRaw}`,\n `urn:tenjin:session:exp:${expIso}`,\n `urn:tenjin:session:scope:${scope}`,\n ];\n}\n\n/**\n * Establish a session with ONE wallet signature: generate a P-256 keypair, bind\n * its pubkey/exp/scope into a SIWX message, wallet-sign it, and cache the result\n * 0600 address-bound. The returned file's `delegation` is the constant\n * `Tenjin-Session-Delegation` header for the session's life.\n */\nexport async function establishSession(\n config: SessionKeyConfig,\n deps: SessionKeyDeps = {},\n): Promise<SessionFile> {\n const now = deps.now ?? Date.now;\n const pair = await generateP256KeyPair();\n const rawPub = new Uint8Array(await subtle.exportKey('raw', pair.publicKey));\n const publicKeyRaw = toBase64Url(rawPub);\n const jwk = (await subtle.exportKey('jwk', pair.privateKey)) as SessionFile['privateKeyJwk'];\n\n const expIso = new Date(now() + SESSION_TTL_MS).toISOString();\n const delegation = await buildSiwxHeader(config.signer, {\n baseUrl: config.baseUrl,\n chainId: config.chainId,\n ttlMs: SESSION_TTL_MS,\n statement: 'Delegate a Tenjin session key.',\n resources: delegationResources(publicKeyRaw, expIso, config.scope),\n });\n\n const file: SessionFile = {\n address: config.signer.address.toLowerCase(),\n origin: originOf(config.baseUrl),\n delegation,\n exp: expIso,\n scope: config.scope,\n publicKeyRaw,\n privateKeyJwk: jwk,\n };\n await saveSessionFile(config.dataDir, file);\n return file;\n}\n\n// ---------------------------------------------------------------------------\n// Cache writes (0600, address-bound). The reader lives in session-present.\n// ---------------------------------------------------------------------------\n\nexport async function saveSessionFile(dir: string, file: SessionFile): Promise<void> {\n await writeFileAtomic(sessionPath(dir), `${JSON.stringify(file, null, 2)}\\n`, {\n mode: 0o600,\n dirMode: 0o700,\n });\n}\n\n// ---------------------------------------------------------------------------\n// The WriteAuth implementation: lazy establish, cache reuse, 401 recovery.\n// ---------------------------------------------------------------------------\n\n/** How each 401 `code` maps to recovery (per the llms-full.txt policy section). */\ntype Recovery = 'resign' | 'reestablish' | 'fatal';\n\nexport function recoveryFor(code: string | undefined): Recovery {\n switch (code) {\n case 'proof_expired':\n // The per-request signature is too old; just re-sign (no wallet).\n return 'resign';\n case 'session_expired':\n case 'proof_revoked':\n case 'insufficient_scope':\n // The delegation itself is gone/insufficient; re-establish (one wallet sig).\n return 'reestablish';\n case 'session_key_unbound':\n // keyid ≠ the delegation-bound key: retrying cannot fix it.\n return 'fatal';\n default:\n return 'fatal';\n }\n}\n\n/**\n * A session-key WriteAuth: mints (or loads) the delegation on first use and signs\n * every write with the P-256 key — so a returning agent never wallet-signs again\n * until the session expires. On a 401 it re-signs or re-establishes per the code.\n */\nexport function createSessionKeyAuth(\n config: SessionKeyConfig,\n deps: SessionKeyDeps = {},\n): WriteAuth {\n const now = deps.now ?? Date.now;\n let cached: SessionFile | null = null;\n let forceReestablish = false;\n\n const ensure = async (): Promise<SessionFile> => {\n if (\n !forceReestablish &&\n cached !== null &&\n isSessionUsable(cached, config.signer.address, now(), config.scope, originOf(config.baseUrl))\n ) {\n return cached;\n }\n if (!forceReestablish) {\n const onDisk = await loadSessionFile(config.dataDir);\n const origin = originOf(config.baseUrl);\n if (\n onDisk !== null &&\n isSessionUsable(onDisk, config.signer.address, now(), config.scope, origin)\n ) {\n cached = onDisk;\n return cached;\n }\n }\n cached = await establishSession(config, deps);\n forceReestablish = false;\n return cached;\n };\n\n return {\n async headersFor(req) {\n const file = await ensure();\n return signWithSession(file, req, deps);\n },\n async recover(code) {\n const recovery = recoveryFor(code);\n if (recovery === 'fatal') return false;\n if (recovery === 'reestablish') {\n cached = null;\n forceReestablish = true;\n }\n // 'resign' needs no state change: the next headersFor mints a fresh\n // created/nonce over the same (still-valid) delegation.\n return true;\n },\n };\n}\n\n/**\n * The plain-SIWX fallback (no session): every write carries its own fresh\n * wallet-signed `SIGN-IN-WITH-X` header. Used when session establishment is\n * disabled; a burned/stale nonce (401) is recovered by re-signing.\n */\nexport function createSiwxAuth(config: SessionKeyConfig): WriteAuth {\n return {\n async headersFor() {\n const header = await buildSiwxHeader(config.signer, {\n baseUrl: config.baseUrl,\n chainId: config.chainId,\n });\n return { 'SIGN-IN-WITH-X': header };\n },\n async recover(code) {\n // A single-use nonce burns on every write; a stale/burned nonce (or an\n // expired proof) is recovered by re-signing with a fresh nonce + issuedAt.\n return code === 'nonce_already_used' || code === 'invalid_proof' || code === 'proof_expired';\n },\n };\n}\n"],"mappings":"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","names":[]}
|
|
@@ -4,28 +4,30 @@ import {
|
|
|
4
4
|
readWalletRecord,
|
|
5
5
|
walletFileMode,
|
|
6
6
|
writeWalletRecord
|
|
7
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-WRWCDLVK.js";
|
|
8
8
|
import {
|
|
9
9
|
hasCode
|
|
10
10
|
} from "./chunk-2I4QTOLB.js";
|
|
11
|
+
import {
|
|
12
|
+
emitNotice
|
|
13
|
+
} from "./chunk-BBUYCDSG.js";
|
|
11
14
|
import {
|
|
12
15
|
withFileLock
|
|
13
16
|
} from "./chunk-YBMMV4Y4.js";
|
|
14
17
|
import {
|
|
15
18
|
evaluateSpendPolicy
|
|
16
19
|
} from "./chunk-UO22YNY4.js";
|
|
17
|
-
import {
|
|
18
|
-
fsyncDir,
|
|
19
|
-
writeFileAtomic
|
|
20
|
-
} from "./chunk-2JQ5JSG6.js";
|
|
21
20
|
import {
|
|
22
21
|
CliError,
|
|
23
22
|
archivedWalletPath,
|
|
24
23
|
external_exports,
|
|
24
|
+
fsyncDir,
|
|
25
25
|
passphraseBlobPath,
|
|
26
26
|
passphraseBlobPathFor,
|
|
27
|
-
|
|
28
|
-
|
|
27
|
+
spendLedgerPath,
|
|
28
|
+
walletPath,
|
|
29
|
+
writeFileAtomic
|
|
30
|
+
} from "./chunk-J6CVKC4M.js";
|
|
29
31
|
import {
|
|
30
32
|
hashAuthorization,
|
|
31
33
|
hashMessage,
|
|
@@ -1378,7 +1380,6 @@ function walletRecord(address, keystore) {
|
|
|
1378
1380
|
// src/lib/wallet/spend.ts
|
|
1379
1381
|
import { mkdir, readFile as readFile2 } from "fs/promises";
|
|
1380
1382
|
import { randomUUID } from "crypto";
|
|
1381
|
-
import { join } from "path";
|
|
1382
1383
|
var DEFAULT_WINDOW_MS = 864e5;
|
|
1383
1384
|
var RESERVATION_TTL_MS = 6e5;
|
|
1384
1385
|
var ReservationSchema = external_exports.object({
|
|
@@ -1398,8 +1399,9 @@ function emptyLedger(nowMs) {
|
|
|
1398
1399
|
function createLocalSpendAuthorizer(deps) {
|
|
1399
1400
|
const windowMs = deps.windowMs ?? DEFAULT_WINDOW_MS;
|
|
1400
1401
|
const now = deps.now ?? Date.now;
|
|
1401
|
-
const path =
|
|
1402
|
+
const path = spendLedgerPath(deps.dir);
|
|
1402
1403
|
const lockPath = `${path}.lock`;
|
|
1404
|
+
let warnedCorrupt = false;
|
|
1403
1405
|
const freshen = (ledger, nowMs) => {
|
|
1404
1406
|
if (ledger === null || nowMs - ledger.windowStartMs >= windowMs) return emptyLedger(nowMs);
|
|
1405
1407
|
return {
|
|
@@ -1422,7 +1424,12 @@ function createLocalSpendAuthorizer(deps) {
|
|
|
1422
1424
|
await mkdir(deps.dir, { recursive: true, mode: 448 });
|
|
1423
1425
|
return withFileLock(lockPath, async () => {
|
|
1424
1426
|
const nowMs = now();
|
|
1425
|
-
const
|
|
1427
|
+
const read = await readLedger(path);
|
|
1428
|
+
if (read.corrupt !== void 0 && !warnedCorrupt) {
|
|
1429
|
+
warnedCorrupt = true;
|
|
1430
|
+
deps.onCorrupt?.(read.corrupt);
|
|
1431
|
+
}
|
|
1432
|
+
const ledger = freshen(read.ledger, nowMs);
|
|
1426
1433
|
return fn(ledger, nowMs);
|
|
1427
1434
|
});
|
|
1428
1435
|
}
|
|
@@ -1483,15 +1490,25 @@ async function readLedger(path) {
|
|
|
1483
1490
|
let raw;
|
|
1484
1491
|
try {
|
|
1485
1492
|
raw = await readFile2(path, "utf8");
|
|
1486
|
-
} catch {
|
|
1487
|
-
return null;
|
|
1493
|
+
} catch (err) {
|
|
1494
|
+
if (hasCode(err, "ENOENT")) return { ledger: null };
|
|
1495
|
+
return { ledger: null, corrupt: err instanceof Error ? err.message : String(err) };
|
|
1488
1496
|
}
|
|
1497
|
+
let json;
|
|
1489
1498
|
try {
|
|
1490
|
-
|
|
1491
|
-
return parsed.success ? parsed.data : null;
|
|
1499
|
+
json = JSON.parse(raw);
|
|
1492
1500
|
} catch {
|
|
1493
|
-
return null;
|
|
1501
|
+
return { ledger: null, corrupt: "not valid JSON" };
|
|
1494
1502
|
}
|
|
1503
|
+
const parsed = LedgerSchema.safeParse(json);
|
|
1504
|
+
if (parsed.success) return { ledger: parsed.data };
|
|
1505
|
+
const issue = parsed.error.issues[0];
|
|
1506
|
+
const field = issue?.path.join(".");
|
|
1507
|
+
const message = issue?.message ?? "schema mismatch";
|
|
1508
|
+
return {
|
|
1509
|
+
ledger: null,
|
|
1510
|
+
corrupt: field !== void 0 && field.length > 0 ? `${field}: ${message}` : message
|
|
1511
|
+
};
|
|
1495
1512
|
}
|
|
1496
1513
|
|
|
1497
1514
|
// src/lib/wallet/index.ts
|
|
@@ -1505,7 +1522,15 @@ function resolveWalletProvider(ctx, opts = {}) {
|
|
|
1505
1522
|
}
|
|
1506
1523
|
function resolveSpendAuthorizer(ctx, policy, opts = {}) {
|
|
1507
1524
|
if (opts.authorizer !== void 0) return opts.authorizer;
|
|
1508
|
-
return createLocalSpendAuthorizer({
|
|
1525
|
+
return createLocalSpendAuthorizer({
|
|
1526
|
+
dir: ctx.dataDir,
|
|
1527
|
+
policy,
|
|
1528
|
+
onCorrupt: (reason) => emitNotice(
|
|
1529
|
+
ctx.io,
|
|
1530
|
+
`spend ledger at ${spendLedgerPath(ctx.dataDir)} was unreadable (${reason}); spending window restarted`,
|
|
1531
|
+
{ json: ctx.flags.json }
|
|
1532
|
+
)
|
|
1533
|
+
});
|
|
1509
1534
|
}
|
|
1510
1535
|
async function describeWallet(provider) {
|
|
1511
1536
|
try {
|
|
@@ -1538,4 +1563,4 @@ export {
|
|
|
1538
1563
|
@noble/ciphers/esm/utils.js:
|
|
1539
1564
|
(*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) *)
|
|
1540
1565
|
*/
|
|
1541
|
-
//# sourceMappingURL=chunk-
|
|
1566
|
+
//# sourceMappingURL=chunk-RGFBBNGP.js.map
|