@circuit-llm/bundle 0.2.1
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 +25 -0
- package/dist/index.d.ts +77 -0
- package/dist/index.js +312 -0
- package/package.json +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Circuit LLM
|
|
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,25 @@
|
|
|
1
|
+
# @circuit-llm/bundle
|
|
2
|
+
|
|
3
|
+
> Build, sign, verify, and unpack **content-addressed (sha256) signed agent bundles** — the canonical codec shared by the Circuit agent cloud and the `circuit` CLI. **Zero runtime dependencies.**
|
|
4
|
+
|
|
5
|
+
Part of the **[Circuit SDK](https://github.com/Circuit-LLM/circuit-sdk)**. [Packages →](https://github.com/Circuit-LLM/circuit-sdk/blob/main/docs/packages.md)
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @circuit-llm/bundle
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { packDir, createBundle, verifyBundle, unpackTo, fromSeed } from '@circuit-llm/bundle';
|
|
17
|
+
|
|
18
|
+
const resources = await packDir('./my-agent'); // read + hash a folder
|
|
19
|
+
const bundle = createBundle(resources, { entry: 'index.js' }, fromSeed(seed)); // signed manifest
|
|
20
|
+
|
|
21
|
+
verifyBundle(bundle); // checks hashes + signature
|
|
22
|
+
await unpackTo(bundle, './out');
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Every file is content-addressed and the manifest is signed, so a bundle can't be silently altered between author and host. `isSafeEntry` blocks path traversal.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import crypto from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
declare function base58(buf: Uint8Array | Buffer): string;
|
|
4
|
+
declare function base58decode(str: string): Buffer;
|
|
5
|
+
interface Keypair {
|
|
6
|
+
seed: Buffer;
|
|
7
|
+
priv: crypto.KeyObject;
|
|
8
|
+
pubkey: Buffer;
|
|
9
|
+
address: string;
|
|
10
|
+
}
|
|
11
|
+
/** Deterministic keypair from a 32-byte seed. */
|
|
12
|
+
declare function fromSeed(seed: Uint8Array | Buffer): Keypair;
|
|
13
|
+
/** 64-byte Ed25519 signature over `msg`. `priv` is a node KeyObject (see fromSeed). */
|
|
14
|
+
declare function sign(priv: crypto.KeyObject, msg: Uint8Array | Buffer): Buffer;
|
|
15
|
+
/** Verify a signature against a 32-byte raw Ed25519 public key. */
|
|
16
|
+
declare function verify(pubkey: Uint8Array | Buffer, msg: Uint8Array | Buffer, sig: Uint8Array | Buffer): boolean;
|
|
17
|
+
declare const sha256hex: (s: crypto.BinaryLike) => string;
|
|
18
|
+
|
|
19
|
+
declare const BUNDLE_SCHEMA = 1;
|
|
20
|
+
type BundleRuntime = 'node' | 'oci';
|
|
21
|
+
interface BundleResources {
|
|
22
|
+
maxCpu?: number | null;
|
|
23
|
+
maxMemoryMb?: number | null;
|
|
24
|
+
}
|
|
25
|
+
interface BundleManifest {
|
|
26
|
+
schema: number;
|
|
27
|
+
agentId: string;
|
|
28
|
+
runtime: BundleRuntime;
|
|
29
|
+
entry: string;
|
|
30
|
+
sdk: string | null;
|
|
31
|
+
egress: string[];
|
|
32
|
+
resources: BundleResources | null;
|
|
33
|
+
sha256: string;
|
|
34
|
+
publisherPubkey: string;
|
|
35
|
+
sig?: string;
|
|
36
|
+
}
|
|
37
|
+
interface VerifyBundleResult {
|
|
38
|
+
ok: boolean;
|
|
39
|
+
code?: 'sha256-mismatch' | 'bad-manifest-sig' | 'publisher-not-owner' | 'agent-id-mismatch' | 'bad-entry';
|
|
40
|
+
}
|
|
41
|
+
declare function canonResources(r: BundleResources | null | undefined): BundleResources | null;
|
|
42
|
+
declare function manifestSigningBytes(m: BundleManifest): Buffer;
|
|
43
|
+
declare function signManifest(m: BundleManifest, priv: crypto.KeyObject): string;
|
|
44
|
+
declare function verifyManifest(m: BundleManifest): boolean;
|
|
45
|
+
interface PackResult {
|
|
46
|
+
bytes: Buffer;
|
|
47
|
+
sha256: string;
|
|
48
|
+
files: string[];
|
|
49
|
+
excludedSecrets: string[];
|
|
50
|
+
}
|
|
51
|
+
declare function packDir(dir: string): PackResult;
|
|
52
|
+
declare function unpackTo(bytes: Buffer | Uint8Array, destDir: string): string;
|
|
53
|
+
declare function isSafeEntry(entry: unknown): entry is string;
|
|
54
|
+
interface CreateBundleOptions {
|
|
55
|
+
dir: string;
|
|
56
|
+
agentId: string;
|
|
57
|
+
runtime?: BundleRuntime;
|
|
58
|
+
entry?: string;
|
|
59
|
+
sdk?: string | null;
|
|
60
|
+
egress?: string[];
|
|
61
|
+
resources?: BundleResources | null;
|
|
62
|
+
priv: crypto.KeyObject;
|
|
63
|
+
publisherPubkey: string;
|
|
64
|
+
}
|
|
65
|
+
declare function createBundle({ dir, agentId, runtime, entry, sdk, egress, resources, priv, publisherPubkey, }: CreateBundleOptions): {
|
|
66
|
+
bytes: Buffer;
|
|
67
|
+
sha256: string;
|
|
68
|
+
manifest: BundleManifest;
|
|
69
|
+
files: string[];
|
|
70
|
+
excludedSecrets: string[];
|
|
71
|
+
};
|
|
72
|
+
declare function verifyBundle(bytes: Buffer | Uint8Array, manifest: BundleManifest, { expectedOwner, expectedAgentId }?: {
|
|
73
|
+
expectedOwner?: string;
|
|
74
|
+
expectedAgentId?: string;
|
|
75
|
+
}): VerifyBundleResult;
|
|
76
|
+
|
|
77
|
+
export { BUNDLE_SCHEMA, type BundleManifest, type BundleResources, type BundleRuntime, type CreateBundleOptions, type Keypair, type PackResult, type VerifyBundleResult, base58, base58decode, canonResources, createBundle, fromSeed, isSafeEntry, manifestSigningBytes, packDir, sha256hex, sign, signManifest, unpackTo, verify, verifyBundle, verifyManifest };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,312 @@
|
|
|
1
|
+
// src/crypto.ts
|
|
2
|
+
import crypto from "crypto";
|
|
3
|
+
var PKCS8_PREFIX = Buffer.from("302e020100300506032b657004220420", "hex");
|
|
4
|
+
var SPKI_PREFIX = Buffer.from("302a300506032b6570032100", "hex");
|
|
5
|
+
var B58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
|
6
|
+
var B58_MAP = Object.fromEntries([...B58].map((ch, i) => [ch, i]));
|
|
7
|
+
function base58(buf) {
|
|
8
|
+
const bytes = Uint8Array.from(buf);
|
|
9
|
+
let zeros = 0;
|
|
10
|
+
while (zeros < bytes.length && bytes[zeros] === 0) zeros++;
|
|
11
|
+
const digits = [];
|
|
12
|
+
for (let i = zeros; i < bytes.length; i++) {
|
|
13
|
+
let carry = bytes[i];
|
|
14
|
+
for (let j = 0; j < digits.length; j++) {
|
|
15
|
+
carry += digits[j] << 8;
|
|
16
|
+
digits[j] = carry % 58;
|
|
17
|
+
carry = carry / 58 | 0;
|
|
18
|
+
}
|
|
19
|
+
while (carry) {
|
|
20
|
+
digits.push(carry % 58);
|
|
21
|
+
carry = carry / 58 | 0;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
let out = "1".repeat(zeros);
|
|
25
|
+
for (let k = digits.length - 1; k >= 0; k--) out += B58[digits[k]];
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function base58decode(str) {
|
|
29
|
+
let zeros = 0;
|
|
30
|
+
while (zeros < str.length && str[zeros] === "1") zeros++;
|
|
31
|
+
const bytes = [];
|
|
32
|
+
for (let i = zeros; i < str.length; i++) {
|
|
33
|
+
let carry = B58_MAP[str[i]];
|
|
34
|
+
if (carry === void 0) throw new Error(`invalid base58 char '${str[i]}'`);
|
|
35
|
+
for (let j = 0; j < bytes.length; j++) {
|
|
36
|
+
carry += bytes[j] * 58;
|
|
37
|
+
bytes[j] = carry & 255;
|
|
38
|
+
carry >>= 8;
|
|
39
|
+
}
|
|
40
|
+
while (carry) {
|
|
41
|
+
bytes.push(carry & 255);
|
|
42
|
+
carry >>= 8;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const out = Buffer.alloc(zeros + bytes.length);
|
|
46
|
+
for (let i = 0; i < bytes.length; i++) out[zeros + bytes.length - 1 - i] = bytes[i];
|
|
47
|
+
return out;
|
|
48
|
+
}
|
|
49
|
+
function fromSeed(seed) {
|
|
50
|
+
if (seed.length !== 32) throw new Error("seed must be 32 bytes");
|
|
51
|
+
const priv = crypto.createPrivateKey({
|
|
52
|
+
key: Buffer.concat([PKCS8_PREFIX, Buffer.from(seed)]),
|
|
53
|
+
format: "der",
|
|
54
|
+
type: "pkcs8"
|
|
55
|
+
});
|
|
56
|
+
const spki = crypto.createPublicKey(priv).export({ format: "der", type: "spki" });
|
|
57
|
+
const pubkey = Buffer.from(spki.subarray(spki.length - 32));
|
|
58
|
+
return { seed: Buffer.from(seed), priv, pubkey, address: base58(pubkey) };
|
|
59
|
+
}
|
|
60
|
+
function sign(priv, msg) {
|
|
61
|
+
return crypto.sign(null, Buffer.from(msg), priv);
|
|
62
|
+
}
|
|
63
|
+
function verify(pubkey, msg, sig) {
|
|
64
|
+
const pub = crypto.createPublicKey({
|
|
65
|
+
key: Buffer.concat([SPKI_PREFIX, Buffer.from(pubkey)]),
|
|
66
|
+
format: "der",
|
|
67
|
+
type: "spki"
|
|
68
|
+
});
|
|
69
|
+
return crypto.verify(null, Buffer.from(msg), pub, Buffer.from(sig));
|
|
70
|
+
}
|
|
71
|
+
var sha256hex = (s) => crypto.createHash("sha256").update(s).digest("hex");
|
|
72
|
+
|
|
73
|
+
// src/bundle.ts
|
|
74
|
+
import crypto2 from "crypto";
|
|
75
|
+
import fs from "fs";
|
|
76
|
+
import path from "path";
|
|
77
|
+
import os from "os";
|
|
78
|
+
import zlib from "zlib";
|
|
79
|
+
import { execFileSync } from "child_process";
|
|
80
|
+
var BUNDLE_SCHEMA = 1;
|
|
81
|
+
function canonResources(r) {
|
|
82
|
+
return r ? { maxCpu: r.maxCpu ?? null, maxMemoryMb: r.maxMemoryMb ?? null } : null;
|
|
83
|
+
}
|
|
84
|
+
function manifestSigningBytes(m) {
|
|
85
|
+
const canon = {
|
|
86
|
+
agentId: m.agentId,
|
|
87
|
+
egress: Array.isArray(m.egress) ? [...m.egress].sort() : [],
|
|
88
|
+
entry: m.entry,
|
|
89
|
+
resources: canonResources(m.resources),
|
|
90
|
+
runtime: m.runtime,
|
|
91
|
+
schema: BUNDLE_SCHEMA,
|
|
92
|
+
sdk: m.sdk ?? null,
|
|
93
|
+
sha256: m.sha256
|
|
94
|
+
};
|
|
95
|
+
return Buffer.from(JSON.stringify(canon));
|
|
96
|
+
}
|
|
97
|
+
function signManifest(m, priv) {
|
|
98
|
+
return base58(sign(priv, manifestSigningBytes(m)));
|
|
99
|
+
}
|
|
100
|
+
function verifyManifest(m) {
|
|
101
|
+
if (!m || !m.sig || !m.publisherPubkey) return false;
|
|
102
|
+
try {
|
|
103
|
+
return verify(base58decode(m.publisherPubkey), manifestSigningBytes(m), base58decode(m.sig));
|
|
104
|
+
} catch {
|
|
105
|
+
return false;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
var ALWAYS_IGNORE = [".git/", "node_modules/", ".hg/", ".svn/", ".DS_Store", "Thumbs.db", "*.log"];
|
|
109
|
+
var SECRET_IGNORE = [
|
|
110
|
+
".env",
|
|
111
|
+
".env.*",
|
|
112
|
+
"*.env",
|
|
113
|
+
"*.pem",
|
|
114
|
+
"*.key",
|
|
115
|
+
"*.p12",
|
|
116
|
+
"*.pfx",
|
|
117
|
+
"id.json",
|
|
118
|
+
"id_*.json",
|
|
119
|
+
"*keypair*.json",
|
|
120
|
+
"*keypair*",
|
|
121
|
+
"wallet.json",
|
|
122
|
+
"*.wallet",
|
|
123
|
+
".npmrc",
|
|
124
|
+
".netrc",
|
|
125
|
+
"secrets.json",
|
|
126
|
+
"secrets.*",
|
|
127
|
+
".secrets/",
|
|
128
|
+
".ssh/",
|
|
129
|
+
".aws/",
|
|
130
|
+
".gnupg/",
|
|
131
|
+
".circuit/"
|
|
132
|
+
];
|
|
133
|
+
var _reCache = /* @__PURE__ */ new Map();
|
|
134
|
+
function globRe(glob) {
|
|
135
|
+
const cached = _reCache.get(glob);
|
|
136
|
+
if (cached) return cached;
|
|
137
|
+
let body = "";
|
|
138
|
+
for (const ch of glob) {
|
|
139
|
+
if (ch === "*") body += "[^/]*";
|
|
140
|
+
else if (ch === "?") body += "[^/]";
|
|
141
|
+
else body += ch.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
142
|
+
}
|
|
143
|
+
const re = new RegExp(`^${body}$`);
|
|
144
|
+
_reCache.set(glob, re);
|
|
145
|
+
return re;
|
|
146
|
+
}
|
|
147
|
+
function matchAny(rel, name, patterns) {
|
|
148
|
+
for (const raw of patterns) {
|
|
149
|
+
let p = (raw || "").trim();
|
|
150
|
+
if (!p || p.startsWith("#") || p.startsWith("!")) continue;
|
|
151
|
+
if (p.endsWith("/")) p = p.slice(0, -1);
|
|
152
|
+
if (p.startsWith("/")) p = p.slice(1);
|
|
153
|
+
if (!p) continue;
|
|
154
|
+
if (p.includes("/")) {
|
|
155
|
+
if (globRe(p).test(rel) || rel === p || rel.startsWith(`${p}/`)) return true;
|
|
156
|
+
} else if (globRe(p).test(name)) {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
162
|
+
function readIgnore(dir, file) {
|
|
163
|
+
try {
|
|
164
|
+
return fs.readFileSync(path.join(dir, file), "utf8").split("\n");
|
|
165
|
+
} catch {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function listIncluded(dir) {
|
|
170
|
+
const userIgnore = [...readIgnore(dir, ".gitignore"), ...readIgnore(dir, ".circuitignore")];
|
|
171
|
+
const files = [];
|
|
172
|
+
const excludedSecrets = [];
|
|
173
|
+
const rec = (cur, rel) => {
|
|
174
|
+
let names;
|
|
175
|
+
try {
|
|
176
|
+
names = fs.readdirSync(cur).sort();
|
|
177
|
+
} catch {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
for (const name of names) {
|
|
181
|
+
const r = rel ? `${rel}/${name}` : name;
|
|
182
|
+
let st;
|
|
183
|
+
try {
|
|
184
|
+
st = fs.lstatSync(path.join(cur, name));
|
|
185
|
+
} catch {
|
|
186
|
+
continue;
|
|
187
|
+
}
|
|
188
|
+
if (st.isSymbolicLink()) continue;
|
|
189
|
+
if (matchAny(r, name, SECRET_IGNORE)) {
|
|
190
|
+
excludedSecrets.push(r + (st.isDirectory() ? "/" : ""));
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
if (matchAny(r, name, ALWAYS_IGNORE)) continue;
|
|
194
|
+
if (matchAny(r, name, userIgnore)) continue;
|
|
195
|
+
if (st.isDirectory()) rec(path.join(cur, name), r);
|
|
196
|
+
else if (st.isFile()) files.push(r);
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
rec(dir, "");
|
|
200
|
+
return { files: files.sort(), excludedSecrets };
|
|
201
|
+
}
|
|
202
|
+
function ustarHeader(name, size, mode = 420) {
|
|
203
|
+
const buf = Buffer.alloc(512);
|
|
204
|
+
let nm = name;
|
|
205
|
+
let prefix = "";
|
|
206
|
+
if (Buffer.byteLength(nm) > 100) {
|
|
207
|
+
let split = -1;
|
|
208
|
+
for (let p = nm.indexOf("/"); p !== -1; p = nm.indexOf("/", p + 1)) {
|
|
209
|
+
if (Buffer.byteLength(nm.slice(p + 1)) <= 100 && Buffer.byteLength(nm.slice(0, p)) <= 155) {
|
|
210
|
+
split = p;
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (split === -1) throw new Error(`path too long to bundle (USTAR limit): ${name}`);
|
|
215
|
+
prefix = nm.slice(0, split);
|
|
216
|
+
nm = nm.slice(split + 1);
|
|
217
|
+
}
|
|
218
|
+
buf.write(nm, 0, 100, "utf8");
|
|
219
|
+
buf.write((mode & 4095).toString(8).padStart(7, "0") + "\0", 100, 8, "ascii");
|
|
220
|
+
buf.write("0000000\0", 108, 8, "ascii");
|
|
221
|
+
buf.write("0000000\0", 116, 8, "ascii");
|
|
222
|
+
buf.write(size.toString(8).padStart(11, "0") + "\0", 124, 12, "ascii");
|
|
223
|
+
buf.write("00000000000\0", 136, 12, "ascii");
|
|
224
|
+
buf.write(" ", 148, 8, "ascii");
|
|
225
|
+
buf.write("0", 156, 1, "ascii");
|
|
226
|
+
buf.write("ustar\0", 257, 6, "ascii");
|
|
227
|
+
buf.write("00", 263, 2, "ascii");
|
|
228
|
+
if (prefix) buf.write(prefix, 345, 155, "utf8");
|
|
229
|
+
let sum = 0;
|
|
230
|
+
for (let i = 0; i < 512; i++) sum += buf[i];
|
|
231
|
+
buf.write((sum & 262143).toString(8).padStart(6, "0") + "\0 ", 148, 8, "ascii");
|
|
232
|
+
return buf;
|
|
233
|
+
}
|
|
234
|
+
function tarGzip(dir, files) {
|
|
235
|
+
const blocks = [];
|
|
236
|
+
for (const rel of files) {
|
|
237
|
+
const data = fs.readFileSync(path.join(dir, rel));
|
|
238
|
+
blocks.push(ustarHeader(rel, data.length), data);
|
|
239
|
+
const pad = (512 - data.length % 512) % 512;
|
|
240
|
+
if (pad) blocks.push(Buffer.alloc(pad));
|
|
241
|
+
}
|
|
242
|
+
blocks.push(Buffer.alloc(1024));
|
|
243
|
+
const gz = zlib.gzipSync(Buffer.concat(blocks), { level: 9 });
|
|
244
|
+
gz.writeUInt32LE(0, 4);
|
|
245
|
+
gz[9] = 255;
|
|
246
|
+
return gz;
|
|
247
|
+
}
|
|
248
|
+
function packDir(dir) {
|
|
249
|
+
const { files, excludedSecrets } = listIncluded(dir);
|
|
250
|
+
if (!files.length) throw new Error("nothing to bundle \u2014 every file was excluded by ignore rules");
|
|
251
|
+
const bytes = tarGzip(dir, files);
|
|
252
|
+
return { bytes, sha256: sha256hex(bytes), files, excludedSecrets };
|
|
253
|
+
}
|
|
254
|
+
function unpackTo(bytes, destDir) {
|
|
255
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
256
|
+
const tmp = path.join(os.tmpdir(), `cbundle-${crypto2.randomBytes(6).toString("hex")}.tgz`);
|
|
257
|
+
try {
|
|
258
|
+
fs.writeFileSync(tmp, bytes);
|
|
259
|
+
execFileSync("tar", ["--no-same-owner", "-xzf", tmp, "-C", destDir], { stdio: "pipe" });
|
|
260
|
+
} finally {
|
|
261
|
+
fs.rmSync(tmp, { force: true });
|
|
262
|
+
}
|
|
263
|
+
return destDir;
|
|
264
|
+
}
|
|
265
|
+
function isSafeEntry(entry) {
|
|
266
|
+
return typeof entry === "string" && /^[\w][\w.-]*$/.test(entry) && entry !== "." && entry !== ".." && !entry.includes("/");
|
|
267
|
+
}
|
|
268
|
+
function createBundle({
|
|
269
|
+
dir,
|
|
270
|
+
agentId,
|
|
271
|
+
runtime = "node",
|
|
272
|
+
entry = "agent.js",
|
|
273
|
+
sdk = null,
|
|
274
|
+
egress = [],
|
|
275
|
+
resources = null,
|
|
276
|
+
priv,
|
|
277
|
+
publisherPubkey
|
|
278
|
+
}) {
|
|
279
|
+
if (runtime !== "node" && runtime !== "oci") throw new Error(`unknown runtime '${runtime}'`);
|
|
280
|
+
if (!isSafeEntry(entry)) throw new Error(`unsafe entry '${entry}'`);
|
|
281
|
+
if (!fs.existsSync(path.join(dir, entry))) throw new Error(`entry '${entry}' not found in ${dir}`);
|
|
282
|
+
const { bytes, sha256, files, excludedSecrets } = packDir(dir);
|
|
283
|
+
const manifest = { schema: BUNDLE_SCHEMA, agentId, runtime, entry, sdk, egress, resources, sha256, publisherPubkey };
|
|
284
|
+
manifest.sig = signManifest(manifest, priv);
|
|
285
|
+
return { bytes, sha256, manifest, files, excludedSecrets };
|
|
286
|
+
}
|
|
287
|
+
function verifyBundle(bytes, manifest, { expectedOwner, expectedAgentId } = {}) {
|
|
288
|
+
if (sha256hex(bytes) !== manifest.sha256) return { ok: false, code: "sha256-mismatch" };
|
|
289
|
+
if (!verifyManifest(manifest)) return { ok: false, code: "bad-manifest-sig" };
|
|
290
|
+
if (expectedOwner && manifest.publisherPubkey !== expectedOwner) return { ok: false, code: "publisher-not-owner" };
|
|
291
|
+
if (expectedAgentId && manifest.agentId !== expectedAgentId) return { ok: false, code: "agent-id-mismatch" };
|
|
292
|
+
if (!isSafeEntry(manifest.entry)) return { ok: false, code: "bad-entry" };
|
|
293
|
+
return { ok: true };
|
|
294
|
+
}
|
|
295
|
+
export {
|
|
296
|
+
BUNDLE_SCHEMA,
|
|
297
|
+
base58,
|
|
298
|
+
base58decode,
|
|
299
|
+
canonResources,
|
|
300
|
+
createBundle,
|
|
301
|
+
fromSeed,
|
|
302
|
+
isSafeEntry,
|
|
303
|
+
manifestSigningBytes,
|
|
304
|
+
packDir,
|
|
305
|
+
sha256hex,
|
|
306
|
+
sign,
|
|
307
|
+
signManifest,
|
|
308
|
+
unpackTo,
|
|
309
|
+
verify,
|
|
310
|
+
verifyBundle,
|
|
311
|
+
verifyManifest
|
|
312
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@circuit-llm/bundle",
|
|
3
|
+
"version": "0.2.1",
|
|
4
|
+
"description": "Circuit SDK agent bundles — build, sign, verify, and unpack content-addressed (sha256) signed agent bundles. The canonical codec shared by the Circuit agent cloud and the circuit CLI.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"development": "./src/index.ts",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"scripts": {
|
|
15
|
+
"test": "node --experimental-strip-types --conditions=development --test test/*.test.ts",
|
|
16
|
+
"typecheck": "tsc -p tsconfig.json",
|
|
17
|
+
"build": "tsup src/index.ts --format esm --dts --clean --out-dir dist",
|
|
18
|
+
"prepack": "tsup src/index.ts --format esm --dts --clean --out-dir dist"
|
|
19
|
+
},
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist"
|
|
24
|
+
],
|
|
25
|
+
"publishConfig": {
|
|
26
|
+
"access": "public"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/Circuit-LLM/circuit-sdk.git",
|
|
31
|
+
"directory": "packages/bundle"
|
|
32
|
+
},
|
|
33
|
+
"homepage": "https://github.com/Circuit-LLM/circuit-sdk/tree/main/packages/bundle#readme",
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/Circuit-LLM/circuit-sdk/issues"
|
|
36
|
+
},
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=18"
|
|
39
|
+
}
|
|
40
|
+
}
|