tenjin-cli 0.1.0-alpha.1 → 0.1.0-alpha.11
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/NOTICE.md +33 -0
- package/README.md +596 -79
- package/dist/{_esm-ZH2J4QEH.js → _esm-C5CRWCYU.js} +2 -2
- package/dist/_esm-N4KAVXQR.js +881 -0
- package/dist/buy-THMVOPUH.js +44 -0
- package/dist/ccip-RPPPKTJR.js +19 -0
- package/dist/{ccip-UCLT5VOW.js → ccip-ZGGWQN4X.js} +6 -5
- package/dist/chunk-2I4QTOLB.js +11 -0
- package/dist/chunk-2I4QTOLB.js.map +1 -0
- package/dist/chunk-2SDUV6MQ.js +352 -0
- package/dist/chunk-2SDUV6MQ.js.map +1 -0
- package/dist/chunk-3GSZUADW.js +199 -0
- package/dist/chunk-3GSZUADW.js.map +1 -0
- package/dist/{chunk-BRJHUQSY.js → chunk-47PP7TAG.js} +908 -812
- package/dist/chunk-47PP7TAG.js.map +1 -0
- package/dist/chunk-4NTZETIL.js +158 -0
- package/dist/chunk-4NTZETIL.js.map +1 -0
- package/dist/chunk-4TCU3YRD.js +130 -0
- package/dist/chunk-4TCU3YRD.js.map +1 -0
- package/dist/chunk-6BGYHARE.js +14620 -0
- package/dist/chunk-6BGYHARE.js.map +1 -0
- package/dist/chunk-6UY2UQMY.js +213 -0
- package/dist/chunk-6UY2UQMY.js.map +1 -0
- package/dist/{chunk-IPOWB3G3.js → chunk-7FK2UN2T.js} +3515 -507
- package/dist/chunk-7FK2UN2T.js.map +1 -0
- package/dist/chunk-ALUNME53.js +306 -0
- package/dist/chunk-ALUNME53.js.map +1 -0
- package/dist/{chunk-EXBMF5X7.js → chunk-CNUXRXI2.js} +526 -79
- package/dist/chunk-CNUXRXI2.js.map +1 -0
- package/dist/chunk-E6YOODH7.js +913 -0
- package/dist/chunk-E6YOODH7.js.map +1 -0
- package/dist/chunk-EDVI7RRD.js +317 -0
- package/dist/chunk-EDVI7RRD.js.map +1 -0
- package/dist/chunk-FKSAUOJU.js +137 -0
- package/dist/chunk-FKSAUOJU.js.map +1 -0
- package/dist/chunk-GKV6VZSZ.js +125 -0
- package/dist/chunk-GKV6VZSZ.js.map +1 -0
- package/dist/chunk-GUUAYR3K.js +175 -0
- package/dist/chunk-GUUAYR3K.js.map +1 -0
- package/dist/chunk-IAFYUFIC.js +1408 -0
- package/dist/chunk-IAFYUFIC.js.map +1 -0
- package/dist/chunk-IB63U6BM.js +185 -0
- package/dist/chunk-IB63U6BM.js.map +1 -0
- package/dist/chunk-IXTENA2H.js +235 -0
- package/dist/chunk-IXTENA2H.js.map +1 -0
- package/dist/chunk-JTYIX6OD.js +161 -0
- package/dist/chunk-JTYIX6OD.js.map +1 -0
- package/dist/chunk-KYQVEKP5.js +209 -0
- package/dist/chunk-KYQVEKP5.js.map +1 -0
- package/dist/chunk-KZAAWQNP.js +6199 -0
- package/dist/chunk-KZAAWQNP.js.map +1 -0
- package/dist/{chunk-T4LKNY7T.js → chunk-M2O56UK7.js} +225 -70
- package/dist/chunk-M2O56UK7.js.map +1 -0
- package/dist/chunk-MJZZTKCC.js +323 -0
- package/dist/chunk-MJZZTKCC.js.map +1 -0
- package/dist/chunk-NUF2PKJO.js +207 -0
- package/dist/chunk-NUF2PKJO.js.map +1 -0
- package/dist/chunk-O25VWJCG.js +1357 -0
- package/dist/chunk-O25VWJCG.js.map +1 -0
- package/dist/chunk-OCN27NJV.js +612 -0
- package/dist/chunk-OCN27NJV.js.map +1 -0
- package/dist/chunk-OZHJ3FLR.js +105 -0
- package/dist/chunk-OZHJ3FLR.js.map +1 -0
- package/dist/chunk-P74I3WH4.js +204 -0
- package/dist/chunk-P74I3WH4.js.map +1 -0
- package/dist/chunk-POJRB2AE.js +83 -0
- package/dist/chunk-POJRB2AE.js.map +1 -0
- package/dist/{chunk-YG2DO46P.js → chunk-PZRAAAY2.js} +107 -22
- package/dist/chunk-PZRAAAY2.js.map +1 -0
- package/dist/chunk-PZSUWDF2.js +35 -0
- package/dist/chunk-PZSUWDF2.js.map +1 -0
- package/dist/chunk-QW2Q2KAH.js +27 -0
- package/dist/chunk-QW2Q2KAH.js.map +1 -0
- package/dist/{chunk-CUWUHTIL.js → chunk-RBJPAZFI.js} +16 -202
- package/dist/chunk-RBJPAZFI.js.map +1 -0
- package/dist/chunk-RSBN67PA.js +152 -0
- package/dist/chunk-RSBN67PA.js.map +1 -0
- package/dist/chunk-S5RYMCV6.js +70 -0
- package/dist/chunk-S5RYMCV6.js.map +1 -0
- package/dist/chunk-SFGXAZ3M.js +316 -0
- package/dist/chunk-SFGXAZ3M.js.map +1 -0
- package/dist/chunk-SGK2B724.js +67 -0
- package/dist/chunk-SGK2B724.js.map +1 -0
- package/dist/chunk-TDDN5GXR.js +145 -0
- package/dist/chunk-TDDN5GXR.js.map +1 -0
- package/dist/{chunk-LPRVHM7P.js → chunk-U5S527PP.js} +1655 -114
- package/dist/chunk-U5S527PP.js.map +1 -0
- package/dist/chunk-UFPJBS4C.js +242 -0
- package/dist/chunk-UFPJBS4C.js.map +1 -0
- package/dist/chunk-UHTVXQ2O.js +140 -0
- package/dist/chunk-UHTVXQ2O.js.map +1 -0
- package/dist/chunk-UY6CGULW.js +83 -0
- package/dist/chunk-UY6CGULW.js.map +1 -0
- package/dist/chunk-VWY3U732.js +674 -0
- package/dist/chunk-VWY3U732.js.map +1 -0
- package/dist/{chunk-Q53IYV2G.js → chunk-W6R3HAAG.js} +20 -17
- package/dist/{chunk-Q53IYV2G.js.map → chunk-W6R3HAAG.js.map} +1 -1
- package/dist/chunk-XJN5WUKL.js +204 -0
- package/dist/chunk-XJN5WUKL.js.map +1 -0
- package/dist/chunk-XWHXX2BI.js +149 -0
- package/dist/chunk-XWHXX2BI.js.map +1 -0
- package/dist/{chunk-DXPYFVKG.js → chunk-Y7ERTA7D.js} +35 -15
- package/dist/chunk-Y7ERTA7D.js.map +1 -0
- package/dist/chunk-YLYINX2H.js +7806 -0
- package/dist/chunk-YLYINX2H.js.map +1 -0
- package/dist/chunk-ZE7KOO7A.js +233 -0
- package/dist/chunk-ZE7KOO7A.js.map +1 -0
- package/dist/chunk-ZQIQ55KK.js +51 -0
- package/dist/chunk-ZQIQ55KK.js.map +1 -0
- package/dist/{chunk-3IMLRUZE.js → chunk-ZXTAQYNA.js} +6 -2
- package/dist/{cli-WA65NP4Y.js → cli-Y2HBOSGK.js} +437 -175
- package/dist/cli-Y2HBOSGK.js.map +1 -0
- package/dist/config-WDGMZKHU.js +26 -0
- package/dist/config-WDGMZKHU.js.map +1 -0
- package/dist/dist-U4M6QCKE.js +2842 -0
- package/dist/dist-U4M6QCKE.js.map +1 -0
- package/dist/doctor-CSIWKWM2.js +27 -0
- package/dist/doctor-CSIWKWM2.js.map +1 -0
- package/dist/edit-E7N6F3OY.js +38 -0
- package/dist/edit-E7N6F3OY.js.map +1 -0
- package/dist/ethersCompat-Df4a6QIZ-WHYKB6HA.js +18 -0
- package/dist/ethersCompat-Df4a6QIZ-WHYKB6HA.js.map +1 -0
- package/dist/fund-OKK627GL.js +38 -0
- package/dist/fund-OKK627GL.js.map +1 -0
- package/dist/index.js +1 -1
- package/dist/inspect-573WHEV4.js +35 -0
- package/dist/inspect-573WHEV4.js.map +1 -0
- package/dist/install-SQAYMNQC.js +1134 -0
- package/dist/install-SQAYMNQC.js.map +1 -0
- package/dist/{mine.wasm-GQECMSGN.js → mine.wasm-6PIT3DK6.js} +2 -2
- package/dist/outcome-W3KYGF23.js +22 -0
- package/dist/outcome-W3KYGF23.js.map +1 -0
- package/dist/publish-N5U4XYVA.js +40 -0
- package/dist/publish-N5U4XYVA.js.map +1 -0
- package/dist/read-PJ7Z6UGF.js +158 -0
- package/dist/read-PJ7Z6UGF.js.map +1 -0
- package/dist/run-FSO3MMXN.js +16883 -0
- package/dist/run-FSO3MMXN.js.map +1 -0
- package/dist/search-ZWOP4EGK.js +23 -0
- package/dist/search-ZWOP4EGK.js.map +1 -0
- package/dist/{secp256k1-BEZS642W.js → secp256k1-XRPVBC6V.js} +5 -4
- package/dist/secp256k1-XRPVBC6V.js.map +1 -0
- package/dist/send-F34ZDXDZ.js +223 -0
- package/dist/send-F34ZDXDZ.js.map +1 -0
- package/dist/session-4N4ZT3AH.js +88 -0
- package/dist/session-4N4ZT3AH.js.map +1 -0
- package/dist/skill-heal-XBEK272G.js +85 -0
- package/dist/skill-heal-XBEK272G.js.map +1 -0
- package/dist/uninstall-BTKPHPDL.js +68 -0
- package/dist/uninstall-BTKPHPDL.js.map +1 -0
- package/dist/usdc-4VCDHFWX.js +38 -0
- package/dist/usdc-4VCDHFWX.js.map +1 -0
- package/dist/viemAdapter-By13KInL-TUQJBMHM.js +14 -0
- package/dist/viemAdapter-By13KInL-TUQJBMHM.js.map +1 -0
- package/dist/wallet-UL4P2JRO.js +42 -0
- package/dist/wallet-UL4P2JRO.js.map +1 -0
- package/dist/wallet-WJ6UP5OU.js +32 -0
- package/dist/wallet-WJ6UP5OU.js.map +1 -0
- package/docs/agent-permissions.md +294 -0
- package/package.json +25 -18
- package/skills/tenjin/SKILL.md +364 -0
- package/skills/tenjin-publish/SKILL.md +232 -0
- package/skills/tenjin-search/SKILL.md +341 -0
- package/dist/chunk-BRJHUQSY.js.map +0 -1
- package/dist/chunk-CUWUHTIL.js.map +0 -1
- package/dist/chunk-DXPYFVKG.js.map +0 -1
- package/dist/chunk-EXBMF5X7.js.map +0 -1
- package/dist/chunk-EZROUI5U.js +0 -135
- package/dist/chunk-EZROUI5U.js.map +0 -1
- package/dist/chunk-IPOWB3G3.js.map +0 -1
- package/dist/chunk-LPRVHM7P.js.map +0 -1
- package/dist/chunk-T4LKNY7T.js.map +0 -1
- package/dist/chunk-YBMMV4Y4.js +0 -51
- package/dist/chunk-YBMMV4Y4.js.map +0 -1
- package/dist/chunk-YG2DO46P.js.map +0 -1
- package/dist/chunk-ZPZFPVDN.js +0 -67
- package/dist/chunk-ZPZFPVDN.js.map +0 -1
- package/dist/cli-WA65NP4Y.js.map +0 -1
- package/dist/config-CEFTL5ZL.js +0 -164
- package/dist/config-CEFTL5ZL.js.map +0 -1
- package/dist/doctor-3QBYN46N.js +0 -338
- package/dist/doctor-3QBYN46N.js.map +0 -1
- package/dist/usdc-44R6XZF2.js +0 -18
- package/dist/wallet-ELBTBMXO.js +0 -20
- package/dist/wallet-XD576HF3.js +0 -173
- package/dist/wallet-XD576HF3.js.map +0 -1
- /package/dist/{_esm-ZH2J4QEH.js.map → _esm-C5CRWCYU.js.map} +0 -0
- /package/dist/{ccip-UCLT5VOW.js.map → _esm-N4KAVXQR.js.map} +0 -0
- /package/dist/{chunk-3IMLRUZE.js.map → buy-THMVOPUH.js.map} +0 -0
- /package/dist/{secp256k1-BEZS642W.js.map → ccip-RPPPKTJR.js.map} +0 -0
- /package/dist/{usdc-44R6XZF2.js.map → ccip-ZGGWQN4X.js.map} +0 -0
- /package/dist/{wallet-ELBTBMXO.js.map → chunk-ZXTAQYNA.js.map} +0 -0
- /package/dist/{mine.wasm-GQECMSGN.js.map → mine.wasm-6PIT3DK6.js.map} +0 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { createRequire as __tenjinCreateRequire } from 'node:module'; const require = __tenjinCreateRequire(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
buildSiwxHeader
|
|
4
|
+
} from "./chunk-6BGYHARE.js";
|
|
5
|
+
import {
|
|
6
|
+
isSessionUsable,
|
|
7
|
+
loadSessionFile,
|
|
8
|
+
signWithSession
|
|
9
|
+
} from "./chunk-NUF2PKJO.js";
|
|
10
|
+
import {
|
|
11
|
+
originOf
|
|
12
|
+
} from "./chunk-PZSUWDF2.js";
|
|
13
|
+
import {
|
|
14
|
+
sessionPath
|
|
15
|
+
} from "./chunk-ZQIQ55KK.js";
|
|
16
|
+
import {
|
|
17
|
+
writeFileAtomic
|
|
18
|
+
} from "./chunk-PZRAAAY2.js";
|
|
19
|
+
|
|
20
|
+
// src/lib/session-key.ts
|
|
21
|
+
import { webcrypto } from "crypto";
|
|
22
|
+
var SESSION_CHAIN_ID = "eip155:8453";
|
|
23
|
+
var SESSION_TTL_MS = 24 * 60 * 60 * 1e3;
|
|
24
|
+
var subtle = webcrypto.subtle;
|
|
25
|
+
function generateP256KeyPair() {
|
|
26
|
+
return subtle.generateKey({ name: "ECDSA", namedCurve: "P-256" }, true, [
|
|
27
|
+
"sign",
|
|
28
|
+
"verify"
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
function toBase64Url(bytes) {
|
|
32
|
+
return Buffer.from(bytes).toString("base64url");
|
|
33
|
+
}
|
|
34
|
+
function delegationResources(publicKeyRaw, expIso, scope) {
|
|
35
|
+
return [
|
|
36
|
+
`urn:tenjin:session:pubkey:p256:${publicKeyRaw}`,
|
|
37
|
+
`urn:tenjin:session:exp:${expIso}`,
|
|
38
|
+
`urn:tenjin:session:scope:${scope}`
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
async function establishSession(config, deps = {}) {
|
|
42
|
+
const now = deps.now ?? Date.now;
|
|
43
|
+
const pair = await generateP256KeyPair();
|
|
44
|
+
const rawPub = new Uint8Array(await subtle.exportKey("raw", pair.publicKey));
|
|
45
|
+
const publicKeyRaw = toBase64Url(rawPub);
|
|
46
|
+
const jwk = await subtle.exportKey("jwk", pair.privateKey);
|
|
47
|
+
const expIso = new Date(now() + SESSION_TTL_MS).toISOString();
|
|
48
|
+
const delegation = await buildSiwxHeader(config.signer, {
|
|
49
|
+
baseUrl: config.baseUrl,
|
|
50
|
+
chainId: config.chainId,
|
|
51
|
+
ttlMs: SESSION_TTL_MS,
|
|
52
|
+
statement: "Delegate a Tenjin session key.",
|
|
53
|
+
resources: delegationResources(publicKeyRaw, expIso, config.scope)
|
|
54
|
+
});
|
|
55
|
+
const file = {
|
|
56
|
+
address: config.signer.address.toLowerCase(),
|
|
57
|
+
origin: originOf(config.baseUrl),
|
|
58
|
+
delegation,
|
|
59
|
+
exp: expIso,
|
|
60
|
+
scope: config.scope,
|
|
61
|
+
publicKeyRaw,
|
|
62
|
+
privateKeyJwk: jwk
|
|
63
|
+
};
|
|
64
|
+
await saveSessionFile(config.dataDir, file);
|
|
65
|
+
return file;
|
|
66
|
+
}
|
|
67
|
+
async function saveSessionFile(dir, file) {
|
|
68
|
+
await writeFileAtomic(sessionPath(dir), `${JSON.stringify(file, null, 2)}
|
|
69
|
+
`, {
|
|
70
|
+
mode: 384,
|
|
71
|
+
dirMode: 448
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
function recoveryFor(code) {
|
|
75
|
+
switch (code) {
|
|
76
|
+
case "proof_expired":
|
|
77
|
+
return "resign";
|
|
78
|
+
case "session_expired":
|
|
79
|
+
case "proof_revoked":
|
|
80
|
+
case "insufficient_scope":
|
|
81
|
+
return "reestablish";
|
|
82
|
+
case "session_key_unbound":
|
|
83
|
+
return "fatal";
|
|
84
|
+
default:
|
|
85
|
+
return "fatal";
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function createSessionKeyAuth(config, deps = {}) {
|
|
89
|
+
const now = deps.now ?? Date.now;
|
|
90
|
+
let cached = null;
|
|
91
|
+
let forceReestablish = false;
|
|
92
|
+
const ensure = async () => {
|
|
93
|
+
if (!forceReestablish && cached !== null && isSessionUsable(cached, config.signer.address, now(), config.scope, originOf(config.baseUrl))) {
|
|
94
|
+
return cached;
|
|
95
|
+
}
|
|
96
|
+
if (!forceReestablish) {
|
|
97
|
+
const onDisk = await loadSessionFile(config.dataDir);
|
|
98
|
+
const origin = originOf(config.baseUrl);
|
|
99
|
+
if (onDisk !== null && isSessionUsable(onDisk, config.signer.address, now(), config.scope, origin)) {
|
|
100
|
+
cached = onDisk;
|
|
101
|
+
return cached;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
cached = await establishSession(config, deps);
|
|
105
|
+
forceReestablish = false;
|
|
106
|
+
return cached;
|
|
107
|
+
};
|
|
108
|
+
return {
|
|
109
|
+
async headersFor(req) {
|
|
110
|
+
const file = await ensure();
|
|
111
|
+
return signWithSession(file, req, deps);
|
|
112
|
+
},
|
|
113
|
+
async recover(code) {
|
|
114
|
+
const recovery = recoveryFor(code);
|
|
115
|
+
if (recovery === "fatal") return false;
|
|
116
|
+
if (recovery === "reestablish") {
|
|
117
|
+
cached = null;
|
|
118
|
+
forceReestablish = true;
|
|
119
|
+
}
|
|
120
|
+
return true;
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
function createSiwxAuth(config) {
|
|
125
|
+
return {
|
|
126
|
+
async headersFor() {
|
|
127
|
+
const header = await buildSiwxHeader(config.signer, {
|
|
128
|
+
baseUrl: config.baseUrl,
|
|
129
|
+
chainId: config.chainId
|
|
130
|
+
});
|
|
131
|
+
return { "SIGN-IN-WITH-X": header };
|
|
132
|
+
},
|
|
133
|
+
async recover(code) {
|
|
134
|
+
return code === "nonce_already_used" || code === "invalid_proof" || code === "proof_expired";
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export {
|
|
140
|
+
SESSION_CHAIN_ID,
|
|
141
|
+
establishSession,
|
|
142
|
+
createSessionKeyAuth,
|
|
143
|
+
createSiwxAuth
|
|
144
|
+
};
|
|
145
|
+
//# sourceMappingURL=chunk-TDDN5GXR.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":";;;;;;;;;;;;;;;;;;;;AAAA,SAAS,iBAAiB;AAyEnB,IAAM,mBAAmB;AAEhC,IAAM,iBAAiB,KAAK,KAAK,KAAK;AA0BtC,IAAM,SAAS,UAAU;AAEzB,SAAS,sBAAwD;AAC/D,SAAO,OAAO,YAAY,EAAE,MAAM,SAAS,YAAY,QAAQ,GAAG,MAAM;AAAA,IACtE;AAAA,IACA;AAAA,EACF,CAAC;AACH;AAGA,SAAS,YAAY,OAA2B;AAC9C,SAAO,OAAO,KAAK,KAAK,EAAE,SAAS,WA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