oneie 5.5.2 → 5.7.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/README.md +29 -0
- package/dist/package.json +2 -2
- package/dist/src/index.js +15 -15
- package/dist/src/init.d.ts +10 -1
- package/dist/src/init.js +26 -11
- package/dist/src/join.d.ts +1 -1
- package/dist/src/join.js +2 -1
- package/dist/src/key-publish.d.ts +40 -0
- package/dist/src/key-publish.js +232 -0
- package/dist/src/key-seal.d.ts +21 -0
- package/dist/src/key-seal.js +142 -0
- package/dist/src/key.js +22 -5
- package/dist/src/lib/age-plugin.d.ts +51 -0
- package/dist/src/lib/age-plugin.js +130 -0
- package/dist/src/lib/env-sealed.d.ts +53 -0
- package/dist/src/lib/env-sealed.js +184 -0
- package/dist/src/lib/journey.d.ts +137 -0
- package/dist/src/lib/journey.js +232 -0
- package/dist/src/lib/key-receipt.d.ts +48 -0
- package/dist/src/lib/key-receipt.js +116 -0
- package/dist/src/lib/lock-lookup.d.ts +40 -0
- package/dist/src/lib/lock-lookup.js +92 -0
- package/dist/src/lib/message-seal.d.ts +63 -0
- package/dist/src/lib/message-seal.js +150 -0
- package/dist/src/lib/shop-init.js +2 -2
- package/dist/src/lib/vault-read.d.ts +24 -0
- package/dist/src/lib/vault-read.js +77 -0
- package/dist/src/media.d.ts +27 -0
- package/dist/src/media.js +249 -18
- package/dist/src/secrets-device.d.ts +32 -0
- package/dist/src/secrets-device.js +127 -0
- package/dist/src/secrets-give.d.ts +125 -0
- package/dist/src/secrets-give.js +236 -0
- package/dist/src/secrets-run.d.ts +31 -0
- package/dist/src/secrets-run.js +182 -0
- package/dist/src/secrets.d.ts +32 -0
- package/dist/src/secrets.js +197 -0
- package/dist/src/social.js +27 -8
- package/dist/src/start.d.ts +15 -0
- package/dist/src/start.js +200 -0
- package/dist/src/write.d.ts +43 -0
- package/dist/src/write.js +174 -0
- package/package.json +2 -2
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The journey bare `npx oneie` walks: key → products → test sale → handover.
|
|
3
|
+
*
|
|
4
|
+
* Pure on purpose, like `shop-init.ts`: every round trip arrives through `deps`,
|
|
5
|
+
* so the whole walk is exercised without a network, a key or a Keychain.
|
|
6
|
+
* `src/start.ts` is the wiring and holds no logic of its own.
|
|
7
|
+
*
|
|
8
|
+
* RESUMABLE BY READ-BACK. Each step writes what it proved into the folder's
|
|
9
|
+
* journey state before the next one starts, and a re-run skips every step whose
|
|
10
|
+
* proof is already there. The test sale waits on a person paying in a browser,
|
|
11
|
+
* so a run that ends at `pending` is the normal shape, not an error: pay, run it
|
|
12
|
+
* again, and it picks up at the sale.
|
|
13
|
+
*
|
|
14
|
+
* THE WORDS NEVER REACH THIS PROCESS'S STDOUT. The key step mints the agent's
|
|
15
|
+
* wallet and seals it (Keychain, or a passphrase typed at a terminal) through
|
|
16
|
+
* `deps.wallet`; what comes back is addresses and a file path. Nothing here can
|
|
17
|
+
* print what it never holds. The person who receives the shop makes their own
|
|
18
|
+
* key in their own browser when they open the handover link.
|
|
19
|
+
*
|
|
20
|
+
* A SALE IS COUNTED, NEVER CLAIMED. A printed checkout link is not a sale. The
|
|
21
|
+
* step is done only when `storefront:stats` reads back a purchase.
|
|
22
|
+
*/
|
|
23
|
+
export interface AskEnvelope {
|
|
24
|
+
outcome?: string;
|
|
25
|
+
result?: Record<string, unknown>;
|
|
26
|
+
error?: string;
|
|
27
|
+
}
|
|
28
|
+
export interface ProvisionAnswer {
|
|
29
|
+
ok: boolean;
|
|
30
|
+
status: number;
|
|
31
|
+
json: Record<string, unknown>;
|
|
32
|
+
}
|
|
33
|
+
export interface WalletAnswer {
|
|
34
|
+
ok: boolean;
|
|
35
|
+
file?: string;
|
|
36
|
+
keychain?: boolean;
|
|
37
|
+
addresses?: Record<string, string>;
|
|
38
|
+
error?: string;
|
|
39
|
+
}
|
|
40
|
+
export interface ProductSpec {
|
|
41
|
+
name: string;
|
|
42
|
+
/** Minor units (100 = 1.00). */
|
|
43
|
+
amount: number;
|
|
44
|
+
currency: string;
|
|
45
|
+
}
|
|
46
|
+
export interface JourneyState {
|
|
47
|
+
v: 1;
|
|
48
|
+
api: string;
|
|
49
|
+
workspace: string;
|
|
50
|
+
uid?: string;
|
|
51
|
+
/** The owner link minted at birth. A bearer credential: the folder is 0600 and git-ignored. */
|
|
52
|
+
inviteUrl?: string;
|
|
53
|
+
email?: string;
|
|
54
|
+
wallet?: WalletAnswer;
|
|
55
|
+
storefront?: boolean;
|
|
56
|
+
products?: Array<ProductSpec & {
|
|
57
|
+
pid: string;
|
|
58
|
+
ppid?: string;
|
|
59
|
+
url: string;
|
|
60
|
+
}>;
|
|
61
|
+
sale?: {
|
|
62
|
+
purchases: number;
|
|
63
|
+
revenueCents?: number;
|
|
64
|
+
at: number;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
export interface JourneyDeps {
|
|
68
|
+
load(): JourneyState | null;
|
|
69
|
+
save(state: JourneyState): void;
|
|
70
|
+
/** The uid-bound key the folder holds for `state.workspace` — never printed. */
|
|
71
|
+
key(): string | null;
|
|
72
|
+
/** A key file is on disk in this folder (readable or not) — the workspace its metadata names, or '' when unnamed. */
|
|
73
|
+
existingKey(): string | null;
|
|
74
|
+
provision(body: Record<string, unknown>): Promise<ProvisionAnswer>;
|
|
75
|
+
/** Write the key folder. Refuses (ok:false) rather than writing a key git can see. */
|
|
76
|
+
saveKey(p: {
|
|
77
|
+
apiKey: string;
|
|
78
|
+
regKey?: string;
|
|
79
|
+
workspace: string;
|
|
80
|
+
uid?: string;
|
|
81
|
+
expiresAt?: number;
|
|
82
|
+
}): {
|
|
83
|
+
ok: boolean;
|
|
84
|
+
error?: string;
|
|
85
|
+
};
|
|
86
|
+
wallet(p: {
|
|
87
|
+
key: string;
|
|
88
|
+
workspace: string;
|
|
89
|
+
}): Promise<WalletAnswer>;
|
|
90
|
+
ask(key: string, receiver: string, data: Record<string, unknown>): Promise<AskEnvelope>;
|
|
91
|
+
sleep(ms: number): Promise<void>;
|
|
92
|
+
now(): number;
|
|
93
|
+
}
|
|
94
|
+
export interface JourneyOptions {
|
|
95
|
+
api: string;
|
|
96
|
+
name: string;
|
|
97
|
+
email?: string;
|
|
98
|
+
products: ProductSpec[];
|
|
99
|
+
/** Seconds to keep reading back the sale before ending at `pending`. 0 = read once. */
|
|
100
|
+
waitSeconds: number;
|
|
101
|
+
/** Between read-backs while waiting. */
|
|
102
|
+
pollMs?: number;
|
|
103
|
+
}
|
|
104
|
+
export type StepName = 'key' | 'products' | 'sale' | 'handover';
|
|
105
|
+
export type StepStatus = 'done' | 'pending' | 'failed' | 'waiting';
|
|
106
|
+
export interface StepReport {
|
|
107
|
+
step: StepName;
|
|
108
|
+
status: StepStatus;
|
|
109
|
+
/** Already done on an earlier run — nothing was written this time. */
|
|
110
|
+
reused?: boolean;
|
|
111
|
+
detail: Record<string, unknown>;
|
|
112
|
+
}
|
|
113
|
+
export interface JourneyReport {
|
|
114
|
+
ok: boolean;
|
|
115
|
+
workspace?: string;
|
|
116
|
+
steps: StepReport[];
|
|
117
|
+
next?: string;
|
|
118
|
+
}
|
|
119
|
+
export declare const TEST_CARD = "4242 4242 4242 4242 \u00B7 any future expiry \u00B7 any CVC \u00B7 any postcode";
|
|
120
|
+
export declare const DEFAULT_PRODUCT: ProductSpec;
|
|
121
|
+
/** `"Name=9.99"` → a product in minor units. A spec that is not that shape is refused by name. */
|
|
122
|
+
export declare function parseProductSpec(raw: string, currency?: string): {
|
|
123
|
+
ok: true;
|
|
124
|
+
product: ProductSpec;
|
|
125
|
+
} | {
|
|
126
|
+
ok: false;
|
|
127
|
+
error: string;
|
|
128
|
+
};
|
|
129
|
+
/** One reading of an `/api/ask` answer. A dissolve and a refusal both arrive as 200. */
|
|
130
|
+
export declare function readAnswer(env: AskEnvelope, receiver: string): {
|
|
131
|
+
ok: true;
|
|
132
|
+
result: Record<string, unknown>;
|
|
133
|
+
} | {
|
|
134
|
+
ok: false;
|
|
135
|
+
error: string;
|
|
136
|
+
};
|
|
137
|
+
export declare function runJourney(deps: JourneyDeps, o: JourneyOptions): Promise<JourneyReport>;
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The journey bare `npx oneie` walks: key → products → test sale → handover.
|
|
3
|
+
*
|
|
4
|
+
* Pure on purpose, like `shop-init.ts`: every round trip arrives through `deps`,
|
|
5
|
+
* so the whole walk is exercised without a network, a key or a Keychain.
|
|
6
|
+
* `src/start.ts` is the wiring and holds no logic of its own.
|
|
7
|
+
*
|
|
8
|
+
* RESUMABLE BY READ-BACK. Each step writes what it proved into the folder's
|
|
9
|
+
* journey state before the next one starts, and a re-run skips every step whose
|
|
10
|
+
* proof is already there. The test sale waits on a person paying in a browser,
|
|
11
|
+
* so a run that ends at `pending` is the normal shape, not an error: pay, run it
|
|
12
|
+
* again, and it picks up at the sale.
|
|
13
|
+
*
|
|
14
|
+
* THE WORDS NEVER REACH THIS PROCESS'S STDOUT. The key step mints the agent's
|
|
15
|
+
* wallet and seals it (Keychain, or a passphrase typed at a terminal) through
|
|
16
|
+
* `deps.wallet`; what comes back is addresses and a file path. Nothing here can
|
|
17
|
+
* print what it never holds. The person who receives the shop makes their own
|
|
18
|
+
* key in their own browser when they open the handover link.
|
|
19
|
+
*
|
|
20
|
+
* A SALE IS COUNTED, NEVER CLAIMED. A printed checkout link is not a sale. The
|
|
21
|
+
* step is done only when `storefront:stats` reads back a purchase.
|
|
22
|
+
*/
|
|
23
|
+
export const TEST_CARD = '4242 4242 4242 4242 · any future expiry · any CVC · any postcode';
|
|
24
|
+
export const DEFAULT_PRODUCT = { name: 'Test product', amount: 100, currency: 'usd' };
|
|
25
|
+
/** `"Name=9.99"` → a product in minor units. A spec that is not that shape is refused by name. */
|
|
26
|
+
export function parseProductSpec(raw, currency = 'usd') {
|
|
27
|
+
const at = raw.lastIndexOf('=');
|
|
28
|
+
const name = at > 0 ? raw.slice(0, at).trim() : '';
|
|
29
|
+
const price = at > 0 ? raw.slice(at + 1).trim() : '';
|
|
30
|
+
if (!name || !/^\d+(\.\d{1,2})?$/.test(price)) {
|
|
31
|
+
return { ok: false, error: `--product takes "Name=price" in major units, e.g. "Mug=12.50" (got "${raw}")` };
|
|
32
|
+
}
|
|
33
|
+
const amount = Math.round(Number(price) * 100);
|
|
34
|
+
if (amount < 50)
|
|
35
|
+
return { ok: false, error: `--product "${name}": the card rail cannot charge under 0.50` };
|
|
36
|
+
return { ok: true, product: { name, amount, currency: currency.toLowerCase() } };
|
|
37
|
+
}
|
|
38
|
+
/** One reading of an `/api/ask` answer. A dissolve and a refusal both arrive as 200. */
|
|
39
|
+
export function readAnswer(env, receiver) {
|
|
40
|
+
if (env.outcome && env.outcome !== 'result')
|
|
41
|
+
return { ok: false, error: `${receiver} ${env.outcome}${env.error ? `: ${env.error}` : ''}` };
|
|
42
|
+
const result = env.result;
|
|
43
|
+
if (!result)
|
|
44
|
+
return { ok: false, error: env.error ?? `${receiver} returned no result` };
|
|
45
|
+
if (result.ok === false || typeof result.error === 'string') {
|
|
46
|
+
return { ok: false, error: `${receiver}: ${String(result.error ?? 'refused')}` };
|
|
47
|
+
}
|
|
48
|
+
return { ok: true, result };
|
|
49
|
+
}
|
|
50
|
+
const productUrl = (api, workspace, pid) => `${api}/u/${workspace}/shop/${pid}`;
|
|
51
|
+
async function keyStep(deps, o, state) {
|
|
52
|
+
let key = state ? deps.key() : null;
|
|
53
|
+
let reused = Boolean(state && key);
|
|
54
|
+
// Never mint over a key already in this folder: `writeKeyFolder` replaces it,
|
|
55
|
+
// and the workspace it opened would be lost with it.
|
|
56
|
+
const onDisk = deps.existingKey();
|
|
57
|
+
if (state && !key) {
|
|
58
|
+
return { report: { step: 'key', status: 'failed', detail: { workspace: state.workspace, error: `the journey here is for ${state.workspace}, but its key is missing or unreadable — move .one/ aside to start a new one` } }, state: null, key: null };
|
|
59
|
+
}
|
|
60
|
+
if (!state && onDisk !== null) {
|
|
61
|
+
return { report: { step: 'key', status: 'failed', detail: { error: `this folder already holds a key${onDisk ? ` for ${onDisk}` : ''} that no journey made — run in a fresh folder, or move .one/ aside` } }, state: null, key: null };
|
|
62
|
+
}
|
|
63
|
+
if (!state) {
|
|
64
|
+
// The owner invite is asked for at birth because it can only be minted there:
|
|
65
|
+
// an agent key is walled off `manage_members` afterwards. With an email the
|
|
66
|
+
// server sends it; without one it still returns the link to print.
|
|
67
|
+
const body = { name: o.name, live: true, invite: { role: 'owner', ...(o.email ? { email: o.email } : {}) } };
|
|
68
|
+
const res = await deps.provision(body);
|
|
69
|
+
const j = res.json;
|
|
70
|
+
if (!res.ok || typeof j.apiKey !== 'string' || typeof j.workspace !== 'string') {
|
|
71
|
+
return { report: { step: 'key', status: 'failed', detail: { error: String(j.error ?? `provision refused (${res.status})`) } }, state: null, key: null };
|
|
72
|
+
}
|
|
73
|
+
const saved = deps.saveKey({
|
|
74
|
+
apiKey: j.apiKey,
|
|
75
|
+
regKey: typeof j.regKey === 'string' ? j.regKey : undefined,
|
|
76
|
+
workspace: j.workspace,
|
|
77
|
+
uid: typeof j.uid === 'string' ? j.uid : undefined,
|
|
78
|
+
expiresAt: typeof j.expiresAt === 'number' ? j.expiresAt : undefined,
|
|
79
|
+
});
|
|
80
|
+
if (!saved.ok) {
|
|
81
|
+
// The workspace exists but its key could not be kept — say so; a re-run mints another.
|
|
82
|
+
return { report: { step: 'key', status: 'failed', detail: { workspace: j.workspace, error: saved.error ?? 'key folder not written' } }, state: null, key: null };
|
|
83
|
+
}
|
|
84
|
+
state = {
|
|
85
|
+
v: 1,
|
|
86
|
+
api: o.api,
|
|
87
|
+
workspace: j.workspace,
|
|
88
|
+
...(typeof j.uid === 'string' ? { uid: j.uid } : {}),
|
|
89
|
+
...(typeof j.inviteUrl === 'string' ? { inviteUrl: j.inviteUrl } : {}),
|
|
90
|
+
...(o.email ? { email: o.email } : {}),
|
|
91
|
+
};
|
|
92
|
+
deps.save(state);
|
|
93
|
+
key = (typeof j.regKey === 'string' && j.regKey) || j.apiKey;
|
|
94
|
+
reused = false;
|
|
95
|
+
}
|
|
96
|
+
if (!state.wallet?.ok) {
|
|
97
|
+
state.wallet = await deps.wallet({ key: key, workspace: state.workspace });
|
|
98
|
+
deps.save(state);
|
|
99
|
+
reused = false;
|
|
100
|
+
}
|
|
101
|
+
const w = state.wallet;
|
|
102
|
+
const detail = { workspace: state.workspace };
|
|
103
|
+
if (w.ok) {
|
|
104
|
+
detail.wallet = w.file;
|
|
105
|
+
detail.sealed = w.keychain ? 'Apple Keychain — Touch ID opens it' : 'passphrase';
|
|
106
|
+
detail.addresses = w.addresses;
|
|
107
|
+
detail.paper = 'run `npx oneie wallet paper` in your own terminal to write the 24 words down — they are never printed here';
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
detail.error = `wallet: ${w.error ?? 'not made'}`;
|
|
111
|
+
}
|
|
112
|
+
return { report: { step: 'key', status: w.ok ? 'done' : 'failed', reused, detail }, state, key };
|
|
113
|
+
}
|
|
114
|
+
async function productsStep(deps, o, state, key) {
|
|
115
|
+
let wrote = false;
|
|
116
|
+
if (!state.storefront) {
|
|
117
|
+
const r = readAnswer(await deps.ask(key, 'storefront:enable', { slug: state.workspace, enabled: true, testMode: true }), 'storefront:enable');
|
|
118
|
+
if (!r.ok)
|
|
119
|
+
return { step: 'products', status: 'failed', detail: { error: r.error } };
|
|
120
|
+
state.storefront = true;
|
|
121
|
+
deps.save(state);
|
|
122
|
+
wrote = true;
|
|
123
|
+
}
|
|
124
|
+
const have = state.products ?? [];
|
|
125
|
+
// Resumed by name: a run that made one of two products and then failed makes
|
|
126
|
+
// only the missing one next time, never a duplicate and never neither.
|
|
127
|
+
const wanted = o.products.length > 0 ? o.products : have.length > 0 ? [] : [DEFAULT_PRODUCT];
|
|
128
|
+
const specs = wanted.filter((p) => !have.some((h) => h.name === p.name));
|
|
129
|
+
for (const p of specs) {
|
|
130
|
+
const r = readAnswer(await deps.ask(key, 'products:create', {
|
|
131
|
+
slug: state.workspace,
|
|
132
|
+
name: p.name,
|
|
133
|
+
status: 'active',
|
|
134
|
+
prices: [{ unit_amount: p.amount, currency: p.currency, is_default: true }],
|
|
135
|
+
}), 'products:create');
|
|
136
|
+
if (!r.ok)
|
|
137
|
+
return { step: 'products', status: 'failed', detail: { error: r.error, created: have.length } };
|
|
138
|
+
const pid = String(r.result.pid ?? '');
|
|
139
|
+
if (!pid)
|
|
140
|
+
return { step: 'products', status: 'failed', detail: { error: 'products:create answered without a pid', created: have.length } };
|
|
141
|
+
have.push({ ...p, pid, ...(r.result.ppid ? { ppid: String(r.result.ppid) } : {}), url: productUrl(state.api, state.workspace, pid) });
|
|
142
|
+
state.products = have;
|
|
143
|
+
deps.save(state);
|
|
144
|
+
wrote = true;
|
|
145
|
+
}
|
|
146
|
+
return {
|
|
147
|
+
step: 'products',
|
|
148
|
+
status: 'done',
|
|
149
|
+
reused: !wrote,
|
|
150
|
+
detail: { mode: 'test money', products: have.map((p) => `${p.name} ${(p.amount / 100).toFixed(2)} ${p.currency.toUpperCase()} ${p.url}`) },
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
async function saleStep(deps, o, state, key) {
|
|
154
|
+
if (state.sale)
|
|
155
|
+
return { step: 'sale', status: 'done', reused: true, detail: { purchases: state.sale.purchases } };
|
|
156
|
+
const deadline = deps.now() + Math.max(0, o.waitSeconds) * 1000;
|
|
157
|
+
for (;;) {
|
|
158
|
+
const r = readAnswer(await deps.ask(key, 'storefront:stats', { slug: state.workspace }), 'storefront:stats');
|
|
159
|
+
if (!r.ok)
|
|
160
|
+
return { step: 'sale', status: 'failed', detail: { error: r.error } };
|
|
161
|
+
// An answer with no count is not zero purchases: an entitlement or credit gate
|
|
162
|
+
// answers with its own shape, and reading that as 0 would keep the person paying
|
|
163
|
+
// into a step that can never turn done.
|
|
164
|
+
if (typeof r.result.purchases !== 'number') {
|
|
165
|
+
return { step: 'sale', status: 'failed', detail: { error: `storefront:stats answered without a purchase count (keys: ${Object.keys(r.result).join(', ') || 'none'})` } };
|
|
166
|
+
}
|
|
167
|
+
const purchases = r.result.purchases;
|
|
168
|
+
if (purchases >= 1) {
|
|
169
|
+
state.sale = {
|
|
170
|
+
purchases,
|
|
171
|
+
...(typeof r.result.revenue_cents === 'number' ? { revenueCents: r.result.revenue_cents } : {}),
|
|
172
|
+
at: deps.now(),
|
|
173
|
+
};
|
|
174
|
+
deps.save(state);
|
|
175
|
+
return { step: 'sale', status: 'done', detail: { purchases, read: 'storefront:stats' } };
|
|
176
|
+
}
|
|
177
|
+
if (deps.now() >= deadline)
|
|
178
|
+
break;
|
|
179
|
+
await deps.sleep(o.pollMs ?? 5000);
|
|
180
|
+
}
|
|
181
|
+
const first = state.products?.[0];
|
|
182
|
+
return {
|
|
183
|
+
step: 'sale',
|
|
184
|
+
status: 'pending',
|
|
185
|
+
detail: { pay: first?.url ?? `${state.api}/u/${state.workspace}/shop`, card: TEST_CARD, purchases: 0 },
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function handoverStep(state, saleDone) {
|
|
189
|
+
if (!saleDone)
|
|
190
|
+
return { step: 'handover', status: 'waiting', detail: { on: 'the test sale' } };
|
|
191
|
+
if (!state.inviteUrl) {
|
|
192
|
+
return {
|
|
193
|
+
step: 'handover',
|
|
194
|
+
status: 'failed',
|
|
195
|
+
detail: {
|
|
196
|
+
error: 'no owner link: the server minted none when this workspace was born, and an agent key cannot mint one afterwards',
|
|
197
|
+
fix: 'run `npx oneie --email <owner>` in a fresh folder — the link is minted at birth',
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
step: 'handover',
|
|
203
|
+
status: 'done',
|
|
204
|
+
detail: {
|
|
205
|
+
link: state.inviteUrl,
|
|
206
|
+
...(state.email ? { emailed: state.email } : {}),
|
|
207
|
+
note: 'anyone holding this link can claim the owner seat — send it only to that person. It works once and lasts 7 days',
|
|
208
|
+
},
|
|
209
|
+
};
|
|
210
|
+
}
|
|
211
|
+
export async function runJourney(deps, o) {
|
|
212
|
+
const steps = [];
|
|
213
|
+
const k = await keyStep(deps, o, deps.load());
|
|
214
|
+
steps.push(k.report);
|
|
215
|
+
if (!k.state || !k.key)
|
|
216
|
+
return { ok: false, steps, next: 'fix the key step and run `npx oneie` again' };
|
|
217
|
+
const state = k.state;
|
|
218
|
+
const products = await productsStep(deps, o, state, k.key);
|
|
219
|
+
steps.push(products);
|
|
220
|
+
if (products.status !== 'done') {
|
|
221
|
+
return { ok: false, workspace: state.workspace, steps, next: 'fix the products step and run `npx oneie` again' };
|
|
222
|
+
}
|
|
223
|
+
const sale = await saleStep(deps, o, state, k.key);
|
|
224
|
+
steps.push(sale);
|
|
225
|
+
const handover = handoverStep(state, sale.status === 'done');
|
|
226
|
+
steps.push(handover);
|
|
227
|
+
const failed = steps.some((s) => s.status === 'failed');
|
|
228
|
+
const next = sale.status === 'pending'
|
|
229
|
+
? 'pay with the test card at the link above, then run `npx oneie` again (or `npx oneie --wait 300` to wait here)'
|
|
230
|
+
: undefined;
|
|
231
|
+
return { ok: !failed, workspace: state.workspace, steps, ...(next ? { next } : {}) };
|
|
232
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* key-receipt.ts — the agent's half of the key receipt (text/handover-course.md; the
|
|
3
|
+
* person's half is one.ie/web/src/lib/key/draw-receipt.ts).
|
|
4
|
+
*
|
|
5
|
+
* `one key new` makes no network call. This measures that instead of saying it: from the
|
|
6
|
+
* start of the key command to the moment the key exists, it counts every connection,
|
|
7
|
+
* DNS lookup and datagram the process opens. The network is NOT switched off, and
|
|
8
|
+
* nothing here needs it to be: the number is zero because nothing asks.
|
|
9
|
+
*
|
|
10
|
+
* What it can and cannot see. It counts inside THIS process. It cannot see another
|
|
11
|
+
* process, and `npx` may download the CLI before the key command starts: that is the
|
|
12
|
+
* package manager, not the key. So the receipt carries the check that does not trust
|
|
13
|
+
* it, a command that makes the network unavailable and runs the same key command.
|
|
14
|
+
*
|
|
15
|
+
* Nothing secret is on it: no words, no key, no addresses, no hostname, no user name.
|
|
16
|
+
*/
|
|
17
|
+
export interface KeyReceipt {
|
|
18
|
+
lane: 'agent';
|
|
19
|
+
madeAt: string;
|
|
20
|
+
tookMs: number;
|
|
21
|
+
/** Connections + DNS lookups + datagrams opened by this process while the key was made. */
|
|
22
|
+
requests: number;
|
|
23
|
+
counted: {
|
|
24
|
+
connections: number;
|
|
25
|
+
dnsLookups: number;
|
|
26
|
+
datagrams: number;
|
|
27
|
+
};
|
|
28
|
+
computer: {
|
|
29
|
+
chip: string | null;
|
|
30
|
+
cores: number;
|
|
31
|
+
memoryGb: number;
|
|
32
|
+
platform: string;
|
|
33
|
+
arch: string;
|
|
34
|
+
node: string;
|
|
35
|
+
};
|
|
36
|
+
network: {
|
|
37
|
+
online: boolean;
|
|
38
|
+
interfaces: number;
|
|
39
|
+
};
|
|
40
|
+
check: string[];
|
|
41
|
+
}
|
|
42
|
+
/** The check that does not trust this file: the network made unavailable, same key command. */
|
|
43
|
+
export declare const CHECKS: readonly string[];
|
|
44
|
+
export declare function startKeyReceipt(): {
|
|
45
|
+
seal: () => KeyReceipt;
|
|
46
|
+
};
|
|
47
|
+
/** The receipt as text: a box, 60 characters wide, no colour, safe in a log. */
|
|
48
|
+
export declare function renderKeyReceipt(r: KeyReceipt): string;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* key-receipt.ts — the agent's half of the key receipt (text/handover-course.md; the
|
|
3
|
+
* person's half is one.ie/web/src/lib/key/draw-receipt.ts).
|
|
4
|
+
*
|
|
5
|
+
* `one key new` makes no network call. This measures that instead of saying it: from the
|
|
6
|
+
* start of the key command to the moment the key exists, it counts every connection,
|
|
7
|
+
* DNS lookup and datagram the process opens. The network is NOT switched off, and
|
|
8
|
+
* nothing here needs it to be: the number is zero because nothing asks.
|
|
9
|
+
*
|
|
10
|
+
* What it can and cannot see. It counts inside THIS process. It cannot see another
|
|
11
|
+
* process, and `npx` may download the CLI before the key command starts: that is the
|
|
12
|
+
* package manager, not the key. So the receipt carries the check that does not trust
|
|
13
|
+
* it, a command that makes the network unavailable and runs the same key command.
|
|
14
|
+
*
|
|
15
|
+
* Nothing secret is on it: no words, no key, no addresses, no hostname, no user name.
|
|
16
|
+
*/
|
|
17
|
+
import dgram from 'node:dgram';
|
|
18
|
+
import dns from 'node:dns';
|
|
19
|
+
import net from 'node:net';
|
|
20
|
+
import os from 'node:os';
|
|
21
|
+
import { performance } from 'node:perf_hooks';
|
|
22
|
+
/** The check that does not trust this file: the network made unavailable, same key command. */
|
|
23
|
+
export const CHECKS = [
|
|
24
|
+
'macOS sandbox-exec -p \\',
|
|
25
|
+
" '(version 1)(allow default)(deny network*)' \\",
|
|
26
|
+
' one key new --out k.json \\',
|
|
27
|
+
' --yes-i-accept-plaintext-file',
|
|
28
|
+
'Linux unshare -rn one key new --out k.json \\',
|
|
29
|
+
' --yes-i-accept-plaintext-file',
|
|
30
|
+
' (the Linux line is not yet measured)',
|
|
31
|
+
];
|
|
32
|
+
export function startKeyReceipt() {
|
|
33
|
+
const t0 = performance.now();
|
|
34
|
+
const counted = { connections: 0, dnsLookups: 0, datagrams: 0 };
|
|
35
|
+
const undo = [];
|
|
36
|
+
// Each patch counts and passes through, so the measurement never changes what runs.
|
|
37
|
+
const origConnect = net.Socket.prototype.connect;
|
|
38
|
+
net.Socket.prototype.connect = function (...a) {
|
|
39
|
+
counted.connections++;
|
|
40
|
+
return origConnect.apply(this, a);
|
|
41
|
+
};
|
|
42
|
+
undo.push(() => { net.Socket.prototype.connect = origConnect; });
|
|
43
|
+
const origSend = dgram.Socket.prototype.send;
|
|
44
|
+
dgram.Socket.prototype.send = function (...a) {
|
|
45
|
+
counted.datagrams++;
|
|
46
|
+
return origSend.apply(this, a);
|
|
47
|
+
};
|
|
48
|
+
undo.push(() => { dgram.Socket.prototype.send = origSend; });
|
|
49
|
+
const origLookup = dns.lookup;
|
|
50
|
+
dns.lookup = function (...a) {
|
|
51
|
+
counted.dnsLookups++;
|
|
52
|
+
return origLookup.apply(dns, a);
|
|
53
|
+
};
|
|
54
|
+
undo.push(() => { dns.lookup = origLookup; });
|
|
55
|
+
return {
|
|
56
|
+
seal() {
|
|
57
|
+
const tookMs = Math.round(performance.now() - t0);
|
|
58
|
+
for (const u of undo.splice(0))
|
|
59
|
+
u();
|
|
60
|
+
const cpus = os.cpus();
|
|
61
|
+
const interfaces = Object.values(os.networkInterfaces())
|
|
62
|
+
.flat()
|
|
63
|
+
.filter((i) => Boolean(i) && !i.internal).length;
|
|
64
|
+
return {
|
|
65
|
+
lane: 'agent',
|
|
66
|
+
madeAt: new Date().toISOString(),
|
|
67
|
+
tookMs,
|
|
68
|
+
requests: counted.connections + counted.dnsLookups + counted.datagrams,
|
|
69
|
+
counted,
|
|
70
|
+
computer: {
|
|
71
|
+
chip: cpus[0]?.model?.trim() || null,
|
|
72
|
+
cores: cpus.length,
|
|
73
|
+
memoryGb: Math.round(os.totalmem() / 2 ** 30),
|
|
74
|
+
platform: os.platform(),
|
|
75
|
+
arch: os.arch(),
|
|
76
|
+
node: process.version,
|
|
77
|
+
},
|
|
78
|
+
network: { online: interfaces > 0, interfaces },
|
|
79
|
+
check: [...CHECKS],
|
|
80
|
+
};
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
const INNER = 56;
|
|
85
|
+
function row(s = '') {
|
|
86
|
+
const t = [...s].length > INNER ? [...s].slice(0, INNER).join('') : s;
|
|
87
|
+
return `│ ${t}${' '.repeat(INNER - [...t].length)} │`;
|
|
88
|
+
}
|
|
89
|
+
/** The receipt as text: a box, 60 characters wide, no colour, safe in a log. */
|
|
90
|
+
export function renderKeyReceipt(r) {
|
|
91
|
+
const d = new Date(r.madeAt);
|
|
92
|
+
const day = d.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' });
|
|
93
|
+
const time = d.toLocaleTimeString('en-GB', { hour12: false });
|
|
94
|
+
const head = 'KEY RECEIPT';
|
|
95
|
+
const gap = ' '.repeat(Math.max(1, INNER - head.length - day.length));
|
|
96
|
+
const c = r.computer;
|
|
97
|
+
const machine = [c.chip, `${c.cores} cores`, `${c.memoryGb} GB`, `${c.platform} ${c.arch}`].filter(Boolean).join(' · ');
|
|
98
|
+
return [
|
|
99
|
+
`╭${'─'.repeat(INNER + 2)}╮`,
|
|
100
|
+
row(`${head}${gap}${day}`),
|
|
101
|
+
row(),
|
|
102
|
+
row(` ${r.requests} requests sent, from start to key`),
|
|
103
|
+
row(),
|
|
104
|
+
row(`made ${time} · took ${r.tookMs} ms`),
|
|
105
|
+
row(`computer ${machine}`),
|
|
106
|
+
row(`network ${r.network.online ? 'on' : 'off'} · ${r.network.interfaces} interface${r.network.interfaces === 1 ? '' : 's'}, none used`),
|
|
107
|
+
row(`counted connections ${r.counted.connections} · DNS ${r.counted.dnsLookups} · datagrams ${r.counted.datagrams}`),
|
|
108
|
+
row(),
|
|
109
|
+
row('Measured inside this process. Check it without trusting'),
|
|
110
|
+
row('this: make the network unavailable and run it again.'),
|
|
111
|
+
row(),
|
|
112
|
+
...r.check.map((l) => row(l)),
|
|
113
|
+
`╰${'─'.repeat(INNER + 2)}╯`,
|
|
114
|
+
'',
|
|
115
|
+
].join('\n');
|
|
116
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lock-lookup.ts — a name's published lock, fetched AND checked, for `one key lock-of`
|
|
3
|
+
* and `one write <slug>` with no `--to`.
|
|
4
|
+
*
|
|
5
|
+
* GET <web base>/api/lock/<slug> → { ok, published, lock, suiAddress, signature, message }
|
|
6
|
+
* GET <web base>/api/lock/<slug>/<agent-id> the same, for an AGENT's own lock
|
|
7
|
+
*
|
|
8
|
+
* THE SERVER IS NOT TRUSTED. The reply is a claim; this file rebuilds the signed text from
|
|
9
|
+
* the slug and the lock, checks the Sui signature itself, and only then hands back a lock
|
|
10
|
+
* (`verifyLockRecord`, @oneie/sdk/message-lock). A swapped lock, a signature made for
|
|
11
|
+
* another name, a signer other than the address named, all stop here. What a terminal
|
|
12
|
+
* cannot check alone is that the signing address belongs to the NAME — that link is the
|
|
13
|
+
* server's, from the account's registered wallet, and every success line says so.
|
|
14
|
+
*
|
|
15
|
+
* Every way a lookup ends is its own code, because an agent branches on them:
|
|
16
|
+
* not_published the name answered and has no lock (a person must publish one)
|
|
17
|
+
* lookup_not_live 404 — the route is not deployed here (never read as "not published")
|
|
18
|
+
* unreachable / lookup_failed could not ask / the door errored
|
|
19
|
+
* unverifiable a lock WAS published and does not check out — never use it
|
|
20
|
+
* The deadline is the same 15s the write door gets (engine rule 4).
|
|
21
|
+
*/
|
|
22
|
+
import type { Fail } from './message-seal.js';
|
|
23
|
+
/** Exit code per refusal class. 1 stays the generic "refused" lane E already used. */
|
|
24
|
+
export declare const EXIT: Record<string, number>;
|
|
25
|
+
export declare const exitFor: (code: string | undefined) => number;
|
|
26
|
+
export interface CheckedLock {
|
|
27
|
+
ok: true;
|
|
28
|
+
/** The workspace half of the name. */
|
|
29
|
+
slug: string;
|
|
30
|
+
/** Set for an agent's lock (`sam/scout`): the agent id. */
|
|
31
|
+
agent?: string;
|
|
32
|
+
/** What was asked and checked: `sam` or `sam/scout`. */
|
|
33
|
+
name: string;
|
|
34
|
+
lock: string;
|
|
35
|
+
suiAddress: string;
|
|
36
|
+
publishedAt: string | null;
|
|
37
|
+
}
|
|
38
|
+
export declare const TRUST_LIMIT = "the signature is checked here; that this Sui address belongs to the name is ONE's word, from the account's registered wallet";
|
|
39
|
+
/** Fetch and verify a name's published lock — `sam`, or `sam/scout` for an agent's own. Never throws. */
|
|
40
|
+
export declare function lookupLock(base: string, name: string, fetchImpl?: typeof fetch): Promise<CheckedLock | Fail>;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lock-lookup.ts — a name's published lock, fetched AND checked, for `one key lock-of`
|
|
3
|
+
* and `one write <slug>` with no `--to`.
|
|
4
|
+
*
|
|
5
|
+
* GET <web base>/api/lock/<slug> → { ok, published, lock, suiAddress, signature, message }
|
|
6
|
+
* GET <web base>/api/lock/<slug>/<agent-id> the same, for an AGENT's own lock
|
|
7
|
+
*
|
|
8
|
+
* THE SERVER IS NOT TRUSTED. The reply is a claim; this file rebuilds the signed text from
|
|
9
|
+
* the slug and the lock, checks the Sui signature itself, and only then hands back a lock
|
|
10
|
+
* (`verifyLockRecord`, @oneie/sdk/message-lock). A swapped lock, a signature made for
|
|
11
|
+
* another name, a signer other than the address named, all stop here. What a terminal
|
|
12
|
+
* cannot check alone is that the signing address belongs to the NAME — that link is the
|
|
13
|
+
* server's, from the account's registered wallet, and every success line says so.
|
|
14
|
+
*
|
|
15
|
+
* Every way a lookup ends is its own code, because an agent branches on them:
|
|
16
|
+
* not_published the name answered and has no lock (a person must publish one)
|
|
17
|
+
* lookup_not_live 404 — the route is not deployed here (never read as "not published")
|
|
18
|
+
* unreachable / lookup_failed could not ask / the door errored
|
|
19
|
+
* unverifiable a lock WAS published and does not check out — never use it
|
|
20
|
+
* The deadline is the same 15s the write door gets (engine rule 4).
|
|
21
|
+
*/
|
|
22
|
+
/** Exit code per refusal class. 1 stays the generic "refused" lane E already used. */
|
|
23
|
+
export const EXIT = {
|
|
24
|
+
no_key: 2, bad_key: 2, no_slug: 2, bad_derivation: 2,
|
|
25
|
+
unauthorized: 3, no_api_key: 3,
|
|
26
|
+
no_sui_address: 4, wrong_signer: 4, not_the_agent: 4,
|
|
27
|
+
bad_signature: 5, bad_lock: 5, bad_slug: 5, bad_agent: 5,
|
|
28
|
+
unreachable: 6, lookup_not_live: 6, lookup_failed: 6, db_error: 6, no_db: 6, failed: 6,
|
|
29
|
+
not_published: 7,
|
|
30
|
+
unverifiable: 8,
|
|
31
|
+
};
|
|
32
|
+
export const exitFor = (code) => EXIT[code ?? ''] ?? 1;
|
|
33
|
+
const DEADLINE_MS = 15_000;
|
|
34
|
+
export const TRUST_LIMIT = "the signature is checked here; that this Sui address belongs to the name is ONE's word, from the account's registered wallet";
|
|
35
|
+
/** Fetch and verify a name's published lock — `sam`, or `sam/scout` for an agent's own. Never throws. */
|
|
36
|
+
export async function lookupLock(base, name, fetchImpl = (...a) => fetch(...a)) {
|
|
37
|
+
const { parseLockName, verifyLockRecord } = await import('@oneie/sdk/message-lock');
|
|
38
|
+
const parsed = parseLockName(name);
|
|
39
|
+
if (!parsed) {
|
|
40
|
+
return name.includes('/')
|
|
41
|
+
? { ok: false, error: `"${name}" is not an agent name — <workspace>/<agent-id>, the agent id with letters, digits, dots, dashes or underscores`, code: 'bad_agent' }
|
|
42
|
+
: { ok: false, error: `"${name}" is not a name — lowercase letters, digits and dashes`, code: 'bad_slug' };
|
|
43
|
+
}
|
|
44
|
+
const { slug, agent } = parsed;
|
|
45
|
+
const label = agent === undefined ? slug : `${slug}/${agent}`;
|
|
46
|
+
const ctl = new AbortController();
|
|
47
|
+
const timer = setTimeout(() => ctl.abort(), DEADLINE_MS);
|
|
48
|
+
let res;
|
|
49
|
+
try {
|
|
50
|
+
const path = agent === undefined ? encodeURIComponent(slug) : `${encodeURIComponent(slug)}/${encodeURIComponent(agent)}`;
|
|
51
|
+
res = await fetchImpl(`${base}/api/lock/${path}`, { headers: { accept: 'application/json' }, signal: ctl.signal });
|
|
52
|
+
}
|
|
53
|
+
catch {
|
|
54
|
+
return { ok: false, error: `could not reach ${base} to look up ${label}'s lock`, code: 'unreachable' };
|
|
55
|
+
}
|
|
56
|
+
finally {
|
|
57
|
+
clearTimeout(timer);
|
|
58
|
+
}
|
|
59
|
+
if (res.status === 404) {
|
|
60
|
+
return { ok: false, error: `the lock lookup is not live on ${base} yet (it answered 404) — nothing is known about ${label}'s lock; pass --to age1… instead`, code: 'lookup_not_live' };
|
|
61
|
+
}
|
|
62
|
+
let body = null;
|
|
63
|
+
try {
|
|
64
|
+
body = (await res.json());
|
|
65
|
+
}
|
|
66
|
+
catch {
|
|
67
|
+
/* handled below as lookup_failed */
|
|
68
|
+
}
|
|
69
|
+
if (res.status === 400 && body?.error === 'bad_slug')
|
|
70
|
+
return { ok: false, error: `"${slug}" is not a name`, code: 'bad_slug' };
|
|
71
|
+
if (res.status === 400 && body?.error === 'bad_agent')
|
|
72
|
+
return { ok: false, error: `"${agent}" is not an agent name`, code: 'bad_agent' };
|
|
73
|
+
if (!res.ok || !body || body.ok === false || typeof body.published !== 'boolean') {
|
|
74
|
+
return { ok: false, error: `the lock lookup on ${base} did not answer properly (HTTP ${res.status}${typeof body?.error === 'string' ? `, ${body.error}` : ''}) — try again`, code: 'lookup_failed' };
|
|
75
|
+
}
|
|
76
|
+
if (!body.published) {
|
|
77
|
+
return { ok: false, error: `${label} has not published a lock — ask them for their age1… lock and pass it with --to`, code: 'not_published' };
|
|
78
|
+
}
|
|
79
|
+
const verdict = verifyLockRecord(slug, body, agent);
|
|
80
|
+
if (!verdict.ok) {
|
|
81
|
+
return { ok: false, error: `${label}'s published lock does not check out (${verdict.detail}) — it was NOT used`, code: 'unverifiable' };
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
ok: true,
|
|
85
|
+
slug,
|
|
86
|
+
...(agent === undefined ? {} : { agent }),
|
|
87
|
+
name: label,
|
|
88
|
+
lock: verdict.lock,
|
|
89
|
+
suiAddress: verdict.suiAddress,
|
|
90
|
+
publishedAt: typeof body.publishedAt === 'string' ? body.publishedAt : null,
|
|
91
|
+
};
|
|
92
|
+
}
|