blocknet 0.0.0-stage → 0.7.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 +202 -0
- package/NOTICE +13 -0
- package/README.md +233 -2
- package/bin/blocknet.js +388 -0
- package/package.json +54 -4
- package/src/admin.js +612 -0
- package/src/agents.js +75 -0
- package/src/approvals.js +73 -0
- package/src/client.js +115 -0
- package/src/config.js +89 -0
- package/src/console/console.css +493 -0
- package/src/console/console.js +1571 -0
- package/src/console/index.html +18 -0
- package/src/enterprise.js +37 -0
- package/src/gateway.js +480 -0
- package/src/hosts.js +77 -0
- package/src/ledger.js +55 -0
- package/src/license.js +111 -0
- package/src/mcp.js +163 -0
- package/src/notify.js +34 -0
- package/src/policy.js +69 -0
- package/src/sessions.js +147 -0
- package/src/settings.js +292 -0
- package/src/signing.js +55 -0
- package/src/simulate.js +140 -0
- package/src/trust.js +37 -0
- package/src/users.js +182 -0
- package/src/x402.js +81 -0
package/src/users.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Console users, roles and passwords.
|
|
3
|
+
*
|
|
4
|
+
* Users live in a JSON file (users.json, readable only by you). Passwords are hashed with scrypt
|
|
5
|
+
* and a per-user salt; plain passwords are never stored or logged. The file is re-read when it
|
|
6
|
+
* changes on disk, so `blocknet user add` works while the gateway runs.
|
|
7
|
+
*/
|
|
8
|
+
import { randomBytes, randomUUID, scrypt as scryptCb, timingSafeEqual } from 'node:crypto';
|
|
9
|
+
import { existsSync, mkdirSync, readFileSync, renameSync, statSync, writeFileSync } from 'node:fs';
|
|
10
|
+
import { dirname } from 'node:path';
|
|
11
|
+
import { promisify } from 'node:util';
|
|
12
|
+
|
|
13
|
+
const scrypt = promisify(scryptCb);
|
|
14
|
+
const SCRYPT = { N: 2 ** 15, r: 8, p: 1, maxmem: 64 * 1024 * 1024 };
|
|
15
|
+
export const MIN_PASSWORD = 12;
|
|
16
|
+
|
|
17
|
+
export const ROLES = ['viewer', 'approver', 'admin'];
|
|
18
|
+
const VIEW = ['overview', 'approvals.view', 'audit.view', 'agents.view', 'policy.view', 'system.view'];
|
|
19
|
+
export const PERMISSIONS = {
|
|
20
|
+
viewer: VIEW,
|
|
21
|
+
approver: [...VIEW, 'approvals.decide'],
|
|
22
|
+
admin: [...VIEW, 'approvals.decide', 'agents.manage', 'policy.manage', 'users.manage'],
|
|
23
|
+
};
|
|
24
|
+
export const can = (role, permission) => (PERMISSIONS[role] ?? []).includes(permission);
|
|
25
|
+
|
|
26
|
+
export async function hashPassword(password) {
|
|
27
|
+
const salt = randomBytes(16);
|
|
28
|
+
const key = await scrypt(password, salt, 32, SCRYPT);
|
|
29
|
+
return `scrypt$${SCRYPT.N}$${SCRYPT.r}$${SCRYPT.p}$${salt.toString('base64')}$${key.toString('base64')}`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function verifyPassword(password, stored) {
|
|
33
|
+
if (typeof stored !== 'string') return false;
|
|
34
|
+
const [scheme, N, r, p, salt, key] = stored.split('$');
|
|
35
|
+
if (scheme !== 'scrypt') return false;
|
|
36
|
+
const want = Buffer.from(key, 'base64');
|
|
37
|
+
const got = await scrypt(String(password), Buffer.from(salt, 'base64'), want.length, { N: +N, r: +r, p: +p, maxmem: SCRYPT.maxmem });
|
|
38
|
+
return timingSafeEqual(got, want);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** A readable temporary password: 4 groups of 5 characters, ~100 bits. */
|
|
42
|
+
export function temporaryPassword() {
|
|
43
|
+
const alphabet = 'abcdefghjkmnpqrstuvwxyz23456789';
|
|
44
|
+
const bytes = randomBytes(20);
|
|
45
|
+
const chars = [...bytes].map((b) => alphabet[b % alphabet.length]).join('');
|
|
46
|
+
return chars.match(/.{5}/g).join('-');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export const validEmail = (e) => typeof e === 'string' && /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(e) && e.length <= 254;
|
|
50
|
+
export const passwordProblem = (p) =>
|
|
51
|
+
typeof p !== 'string' || p.length < MIN_PASSWORD ? `password must be at least ${MIN_PASSWORD} characters` : p.length > 512 ? 'password is too long' : null;
|
|
52
|
+
|
|
53
|
+
/** What the API shows about a user: never the hash. */
|
|
54
|
+
export const publicUser = (u) =>
|
|
55
|
+
u && {
|
|
56
|
+
id: u.id,
|
|
57
|
+
email: u.email,
|
|
58
|
+
name: u.name,
|
|
59
|
+
role: u.role,
|
|
60
|
+
sso: Boolean(u.sso),
|
|
61
|
+
disabled: Boolean(u.disabled),
|
|
62
|
+
mustChangePassword: Boolean(u.mustChangePassword),
|
|
63
|
+
createdAt: u.createdAt,
|
|
64
|
+
lastLoginAt: u.lastLoginAt ?? null,
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
export class UserStore {
|
|
68
|
+
/** @param {string|null} path null keeps users in memory (tests) */
|
|
69
|
+
constructor(path) {
|
|
70
|
+
this.path = path;
|
|
71
|
+
this.users = [];
|
|
72
|
+
this.mtime = 0;
|
|
73
|
+
this.refresh();
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
refresh() {
|
|
77
|
+
if (!this.path || !existsSync(this.path)) return;
|
|
78
|
+
const mtime = statSync(this.path).mtimeMs;
|
|
79
|
+
if (mtime === this.mtime) return;
|
|
80
|
+
this.users = JSON.parse(readFileSync(this.path, 'utf8'));
|
|
81
|
+
this.mtime = mtime;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
save() {
|
|
85
|
+
if (!this.path) return;
|
|
86
|
+
mkdirSync(dirname(this.path), { recursive: true, mode: 0o700 });
|
|
87
|
+
const tmp = `${this.path}.tmp`;
|
|
88
|
+
writeFileSync(tmp, JSON.stringify(this.users, null, 2), { mode: 0o600 });
|
|
89
|
+
renameSync(tmp, this.path);
|
|
90
|
+
this.mtime = statSync(this.path).mtimeMs;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
list() {
|
|
94
|
+
this.refresh();
|
|
95
|
+
return this.users;
|
|
96
|
+
}
|
|
97
|
+
count() {
|
|
98
|
+
return this.list().length;
|
|
99
|
+
}
|
|
100
|
+
get(id) {
|
|
101
|
+
return this.list().find((u) => u.id === id);
|
|
102
|
+
}
|
|
103
|
+
byEmail(email) {
|
|
104
|
+
const e = String(email ?? '').trim().toLowerCase();
|
|
105
|
+
return this.list().find((u) => u.email === e);
|
|
106
|
+
}
|
|
107
|
+
bySubject(issuer, sub) {
|
|
108
|
+
return this.list().find((u) => u.sso?.issuer === issuer && u.sso?.sub === sub);
|
|
109
|
+
}
|
|
110
|
+
activeAdmins() {
|
|
111
|
+
return this.list().filter((u) => u.role === 'admin' && !u.disabled);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/** @returns {Promise<{user: object, temporaryPassword?: string}>} */
|
|
115
|
+
async create({ email, name, role, password, sso, mustChangePassword }) {
|
|
116
|
+
email = String(email ?? '').trim().toLowerCase();
|
|
117
|
+
if (!validEmail(email)) throw problem(400, 'enter a valid email address', 'email');
|
|
118
|
+
if (!ROLES.includes(role)) throw problem(400, `role must be one of ${ROLES.join(', ')}`, 'role');
|
|
119
|
+
if (this.byEmail(email)) throw problem(409, 'a user with that email already exists', 'email');
|
|
120
|
+
let temporary;
|
|
121
|
+
if (!sso && password == null) {
|
|
122
|
+
temporary = temporaryPassword();
|
|
123
|
+
password = temporary;
|
|
124
|
+
mustChangePassword = true;
|
|
125
|
+
}
|
|
126
|
+
if (!sso) {
|
|
127
|
+
const bad = passwordProblem(password);
|
|
128
|
+
if (bad) throw problem(400, bad, 'password');
|
|
129
|
+
}
|
|
130
|
+
const user = {
|
|
131
|
+
id: `u_${randomUUID().replace(/-/g, '').slice(0, 16)}`,
|
|
132
|
+
email,
|
|
133
|
+
name: String(name ?? '').trim().slice(0, 120) || email.split('@')[0],
|
|
134
|
+
role,
|
|
135
|
+
...(sso ? { sso } : { passwordHash: await hashPassword(password) }),
|
|
136
|
+
...(mustChangePassword && { mustChangePassword: true }),
|
|
137
|
+
sessionVersion: 1,
|
|
138
|
+
createdAt: new Date().toISOString(),
|
|
139
|
+
};
|
|
140
|
+
this.users.push(user);
|
|
141
|
+
this.save();
|
|
142
|
+
return { user, temporaryPassword: temporary };
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
update(id, fields) {
|
|
146
|
+
const u = this.get(id);
|
|
147
|
+
if (!u) throw problem(404, 'unknown user');
|
|
148
|
+
Object.assign(u, fields);
|
|
149
|
+
this.save();
|
|
150
|
+
return u;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Ends every session of this user (sessions carry the version they were made with). */
|
|
154
|
+
revokeSessions(id) {
|
|
155
|
+
const u = this.get(id);
|
|
156
|
+
if (u) this.update(id, { sessionVersion: (u.sessionVersion ?? 1) + 1 });
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async setPassword(id, password, { mustChange = false } = {}) {
|
|
160
|
+
const bad = passwordProblem(password);
|
|
161
|
+
if (bad) throw problem(400, bad, 'password');
|
|
162
|
+
const u = this.get(id);
|
|
163
|
+
if (!u) throw problem(404, 'unknown user');
|
|
164
|
+
u.passwordHash = await hashPassword(password);
|
|
165
|
+
if (mustChange) u.mustChangePassword = true;
|
|
166
|
+
else delete u.mustChangePassword;
|
|
167
|
+
u.sessionVersion = (u.sessionVersion ?? 1) + 1;
|
|
168
|
+
this.save();
|
|
169
|
+
return u;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
remove(id) {
|
|
173
|
+
const before = this.users.length;
|
|
174
|
+
this.users = this.list().filter((u) => u.id !== id);
|
|
175
|
+
if (this.users.length === before) throw problem(404, 'unknown user');
|
|
176
|
+
this.save();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
export function problem(status, message, field) {
|
|
181
|
+
return Object.assign(new Error(message), { status, expose: true, ...(field && { field }) });
|
|
182
|
+
}
|
package/src/x402.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parsing for x402 "402 Payment Required" responses.
|
|
3
|
+
*
|
|
4
|
+
* Supports:
|
|
5
|
+
* - v2: base64 JSON in the PAYMENT-REQUIRED response header
|
|
6
|
+
* - v1: JSON response body with an `accepts` array and `maxAmountRequired`
|
|
7
|
+
*
|
|
8
|
+
* Blocknet never signs or holds funds. It reads the requirement, decides
|
|
9
|
+
* whether the agent is allowed to pay it, and lets the agent's own wallet sign.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
const DEFAULT_DECIMALS = { USDC: 6 };
|
|
13
|
+
|
|
14
|
+
export function decodeHeader(value) {
|
|
15
|
+
if (!value) return null;
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(Buffer.from(value, 'base64').toString('utf8'));
|
|
18
|
+
} catch {
|
|
19
|
+
try {
|
|
20
|
+
return JSON.parse(value);
|
|
21
|
+
} catch {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @param {Headers} headers upstream response headers
|
|
29
|
+
* @param {string} bodyText upstream response body
|
|
30
|
+
* @returns {null | {version:number, options:Array<PaymentOption>, raw:object}}
|
|
31
|
+
*/
|
|
32
|
+
export function parsePaymentRequired(headers, bodyText) {
|
|
33
|
+
let raw = decodeHeader(headers.get('payment-required'));
|
|
34
|
+
if (!raw && bodyText) {
|
|
35
|
+
try {
|
|
36
|
+
raw = JSON.parse(bodyText);
|
|
37
|
+
} catch {
|
|
38
|
+
raw = null;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
if (!raw || !Array.isArray(raw.accepts) || raw.accepts.length === 0) return null;
|
|
42
|
+
return {
|
|
43
|
+
version: raw.x402Version ?? 1,
|
|
44
|
+
options: raw.accepts.map(normalizeOption),
|
|
45
|
+
raw,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @typedef {object} PaymentOption
|
|
51
|
+
* @property {string} scheme
|
|
52
|
+
* @property {string} network
|
|
53
|
+
* @property {string} asset symbol when known (e.g. USDC), else the raw asset id
|
|
54
|
+
* @property {string} payTo
|
|
55
|
+
* @property {number} amountUsd amount in whole units (USD for stablecoins)
|
|
56
|
+
* @property {string} amountAtomic
|
|
57
|
+
*/
|
|
58
|
+
export function normalizeOption(o) {
|
|
59
|
+
const atomic = String(o.amount ?? o.maxAmountRequired ?? '0');
|
|
60
|
+
const symbol = o.extra?.name === 'USD Coin' ? 'USDC' : (o.extra?.symbol ?? o.asset ?? 'USDC');
|
|
61
|
+
const decimals = o.extra?.decimals ?? DEFAULT_DECIMALS[symbol] ?? 6;
|
|
62
|
+
return {
|
|
63
|
+
scheme: o.scheme ?? 'exact',
|
|
64
|
+
network: o.network ?? 'unknown',
|
|
65
|
+
asset: symbol,
|
|
66
|
+
payTo: o.payTo ?? '',
|
|
67
|
+
amountAtomic: atomic,
|
|
68
|
+
amountUsd: Number(atomic) / 10 ** decimals,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Picks the cheapest option the policy allows, or the cheapest overall. */
|
|
73
|
+
export function chooseOption(options, { allowedNetworks, allowedAssets } = {}) {
|
|
74
|
+
const ok = options.filter(
|
|
75
|
+
(o) =>
|
|
76
|
+
(!allowedNetworks?.length || allowedNetworks.includes(o.network)) &&
|
|
77
|
+
(!allowedAssets?.length || allowedAssets.includes(o.asset)),
|
|
78
|
+
);
|
|
79
|
+
const pool = ok.length ? ok : options;
|
|
80
|
+
return { option: [...pool].sort((a, b) => a.amountUsd - b.amountUsd)[0], acceptable: ok.length > 0 };
|
|
81
|
+
}
|