@avow-finance/sdk 0.0.0-stage → 0.2.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/LICENSE +201 -0
- package/README.md +122 -2
- package/dist/agent-entry.d.ts +15 -0
- package/dist/agent-entry.d.ts.map +1 -0
- package/dist/agent-entry.js +11 -0
- package/dist/agent-entry.js.map +1 -0
- package/dist/agent-key.d.ts +34 -0
- package/dist/agent-key.d.ts.map +1 -0
- package/dist/agent-key.js +33 -0
- package/dist/agent-key.js.map +1 -0
- package/dist/agent.d.ts +261 -0
- package/dist/agent.d.ts.map +1 -0
- package/dist/agent.js +493 -0
- package/dist/agent.js.map +1 -0
- package/dist/avow-api.d.ts +78 -0
- package/dist/avow-api.d.ts.map +1 -0
- package/dist/avow-api.js +2 -0
- package/dist/avow-api.js.map +1 -0
- package/dist/contest.d.ts +135 -0
- package/dist/contest.d.ts.map +1 -0
- package/dist/contest.js +186 -0
- package/dist/contest.js.map +1 -0
- package/dist/http.d.ts +161 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +311 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +38 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +38 -0
- package/dist/index.js.map +1 -0
- package/dist/job/accept.d.ts +84 -0
- package/dist/job/accept.d.ts.map +1 -0
- package/dist/job/accept.js +136 -0
- package/dist/job/accept.js.map +1 -0
- package/dist/job/customer.d.ts +43 -0
- package/dist/job/customer.d.ts.map +1 -0
- package/dist/job/customer.js +83 -0
- package/dist/job/customer.js.map +1 -0
- package/dist/job/quote.d.ts +34 -0
- package/dist/job/quote.d.ts.map +1 -0
- package/dist/job/quote.js +37 -0
- package/dist/job/quote.js.map +1 -0
- package/dist/lock.d.ts +269 -0
- package/dist/lock.d.ts.map +1 -0
- package/dist/lock.js +283 -0
- package/dist/lock.js.map +1 -0
- package/dist/moderato.d.ts +27 -0
- package/dist/moderato.d.ts.map +1 -0
- package/dist/moderato.js +34 -0
- package/dist/moderato.js.map +1 -0
- package/dist/node/cli.d.ts +3 -0
- package/dist/node/cli.d.ts.map +1 -0
- package/dist/node/cli.js +346 -0
- package/dist/node/cli.js.map +1 -0
- package/dist/node/connect.d.ts +15 -0
- package/dist/node/connect.d.ts.map +1 -0
- package/dist/node/connect.js +91 -0
- package/dist/node/connect.js.map +1 -0
- package/dist/node/eval.d.ts +3 -0
- package/dist/node/eval.d.ts.map +1 -0
- package/dist/node/eval.js +67 -0
- package/dist/node/eval.js.map +1 -0
- package/dist/node/first-run.d.ts +50 -0
- package/dist/node/first-run.d.ts.map +1 -0
- package/dist/node/first-run.js +149 -0
- package/dist/node/first-run.js.map +1 -0
- package/dist/node/job.d.ts +50 -0
- package/dist/node/job.d.ts.map +1 -0
- package/dist/node/job.js +176 -0
- package/dist/node/job.js.map +1 -0
- package/dist/node/mailbox.d.ts +19 -0
- package/dist/node/mailbox.d.ts.map +1 -0
- package/dist/node/mailbox.js +81 -0
- package/dist/node/mailbox.js.map +1 -0
- package/dist/node/mcp.d.ts +74 -0
- package/dist/node/mcp.d.ts.map +1 -0
- package/dist/node/mcp.js +367 -0
- package/dist/node/mcp.js.map +1 -0
- package/dist/node/platform.d.ts +35 -0
- package/dist/node/platform.d.ts.map +1 -0
- package/dist/node/platform.js +102 -0
- package/dist/node/platform.js.map +1 -0
- package/dist/node/runner-session.d.ts +41 -0
- package/dist/node/runner-session.d.ts.map +1 -0
- package/dist/node/runner-session.js +100 -0
- package/dist/node/runner-session.js.map +1 -0
- package/dist/node/runner.d.ts +63 -0
- package/dist/node/runner.d.ts.map +1 -0
- package/dist/node/runner.js +341 -0
- package/dist/node/runner.js.map +1 -0
- package/dist/node/service-child.d.ts +2 -0
- package/dist/node/service-child.d.ts.map +1 -0
- package/dist/node/service-child.js +40 -0
- package/dist/node/service-child.js.map +1 -0
- package/dist/node/service-excerpt.d.ts +21 -0
- package/dist/node/service-excerpt.d.ts.map +1 -0
- package/dist/node/service-excerpt.js +32 -0
- package/dist/node/service-excerpt.js.map +1 -0
- package/dist/node/service-process.d.ts +12 -0
- package/dist/node/service-process.d.ts.map +1 -0
- package/dist/node/service-process.js +132 -0
- package/dist/node/service-process.js.map +1 -0
- package/dist/node/session-input.d.ts +12 -0
- package/dist/node/session-input.d.ts.map +1 -0
- package/dist/node/session-input.js +42 -0
- package/dist/node/session-input.js.map +1 -0
- package/dist/node/session-view.d.ts +20 -0
- package/dist/node/session-view.d.ts.map +1 -0
- package/dist/node/session-view.js +198 -0
- package/dist/node/session-view.js.map +1 -0
- package/dist/node-agent.d.ts +2 -0
- package/dist/node-agent.d.ts.map +1 -0
- package/dist/node-agent.js +6 -0
- package/dist/node-agent.js.map +1 -0
- package/dist/node.d.ts +2 -0
- package/dist/node.d.ts.map +1 -0
- package/dist/node.js +9 -0
- package/dist/node.js.map +1 -0
- package/dist/sandbox/agent.d.ts +187 -0
- package/dist/sandbox/agent.d.ts.map +1 -0
- package/dist/sandbox/agent.js +468 -0
- package/dist/sandbox/agent.js.map +1 -0
- package/dist/sandbox/business-store.d.ts +19 -0
- package/dist/sandbox/business-store.d.ts.map +1 -0
- package/dist/sandbox/business-store.js +19 -0
- package/dist/sandbox/business-store.js.map +1 -0
- package/dist/sandbox/business.d.ts +70 -0
- package/dist/sandbox/business.d.ts.map +1 -0
- package/dist/sandbox/business.js +115 -0
- package/dist/sandbox/business.js.map +1 -0
- package/dist/sandbox/connect.d.ts +64 -0
- package/dist/sandbox/connect.d.ts.map +1 -0
- package/dist/sandbox/connect.js +182 -0
- package/dist/sandbox/connect.js.map +1 -0
- package/dist/sandbox/deployment.d.ts +14 -0
- package/dist/sandbox/deployment.d.ts.map +1 -0
- package/dist/sandbox/deployment.js +38 -0
- package/dist/sandbox/deployment.js.map +1 -0
- package/dist/sandbox/keys.d.ts +45 -0
- package/dist/sandbox/keys.d.ts.map +1 -0
- package/dist/sandbox/keys.js +132 -0
- package/dist/sandbox/keys.js.map +1 -0
- package/dist/sandbox/mailbox.d.ts +90 -0
- package/dist/sandbox/mailbox.d.ts.map +1 -0
- package/dist/sandbox/mailbox.js +400 -0
- package/dist/sandbox/mailbox.js.map +1 -0
- package/dist/sandbox/mandate.d.ts +84 -0
- package/dist/sandbox/mandate.d.ts.map +1 -0
- package/dist/sandbox/mandate.js +107 -0
- package/dist/sandbox/mandate.js.map +1 -0
- package/dist/sandbox/namespace.d.ts +16 -0
- package/dist/sandbox/namespace.d.ts.map +1 -0
- package/dist/sandbox/namespace.js +16 -0
- package/dist/sandbox/namespace.js.map +1 -0
- package/dist/sandbox/session.d.ts +60 -0
- package/dist/sandbox/session.d.ts.map +1 -0
- package/dist/sandbox/session.js +94 -0
- package/dist/sandbox/session.js.map +1 -0
- package/dist/sandbox/setup.d.ts +136 -0
- package/dist/sandbox/setup.d.ts.map +1 -0
- package/dist/sandbox/setup.js +490 -0
- package/dist/sandbox/setup.js.map +1 -0
- package/dist/sandbox/store.d.ts +69 -0
- package/dist/sandbox/store.d.ts.map +1 -0
- package/dist/sandbox/store.js +115 -0
- package/dist/sandbox/store.js.map +1 -0
- package/dist/sandbox/supplier-key.d.ts +73 -0
- package/dist/sandbox/supplier-key.d.ts.map +1 -0
- package/dist/sandbox/supplier-key.js +180 -0
- package/dist/sandbox/supplier-key.js.map +1 -0
- package/dist/sandbox/suppliers.d.ts +7 -0
- package/dist/sandbox/suppliers.d.ts.map +1 -0
- package/dist/sandbox/suppliers.js +29 -0
- package/dist/sandbox/suppliers.js.map +1 -0
- package/dist/sandbox/terms.d.ts +32 -0
- package/dist/sandbox/terms.d.ts.map +1 -0
- package/dist/sandbox/terms.js +58 -0
- package/dist/sandbox/terms.js.map +1 -0
- package/dist/sandbox/vault.d.ts +24 -0
- package/dist/sandbox/vault.d.ts.map +1 -0
- package/dist/sandbox/vault.js +54 -0
- package/dist/sandbox/vault.js.map +1 -0
- package/dist/send.d.ts +122 -0
- package/dist/send.d.ts.map +1 -0
- package/dist/send.js +328 -0
- package/dist/send.js.map +1 -0
- package/dist/service-entry.d.ts +6 -0
- package/dist/service-entry.d.ts.map +1 -0
- package/dist/service-entry.js +6 -0
- package/dist/service-entry.js.map +1 -0
- package/dist/session/anthropic.d.ts +17 -0
- package/dist/session/anthropic.d.ts.map +1 -0
- package/dist/session/anthropic.js +215 -0
- package/dist/session/anthropic.js.map +1 -0
- package/dist/session/charge.d.ts +13 -0
- package/dist/session/charge.d.ts.map +1 -0
- package/dist/session/charge.js +33 -0
- package/dist/session/charge.js.map +1 -0
- package/dist/session/data.d.ts +8 -0
- package/dist/session/data.d.ts.map +1 -0
- package/dist/session/data.js +20 -0
- package/dist/session/data.js.map +1 -0
- package/dist/session/eval.d.ts +41 -0
- package/dist/session/eval.d.ts.map +1 -0
- package/dist/session/eval.js +281 -0
- package/dist/session/eval.js.map +1 -0
- package/dist/session/executor.d.ts +80 -0
- package/dist/session/executor.d.ts.map +1 -0
- package/dist/session/executor.js +381 -0
- package/dist/session/executor.js.map +1 -0
- package/dist/session/fixture-world.d.ts +29 -0
- package/dist/session/fixture-world.d.ts.map +1 -0
- package/dist/session/fixture-world.js +708 -0
- package/dist/session/fixture-world.js.map +1 -0
- package/dist/session/live-world.d.ts +22 -0
- package/dist/session/live-world.d.ts.map +1 -0
- package/dist/session/live-world.js +329 -0
- package/dist/session/live-world.js.map +1 -0
- package/dist/session/ollama.d.ts +29 -0
- package/dist/session/ollama.d.ts.map +1 -0
- package/dist/session/ollama.js +177 -0
- package/dist/session/ollama.js.map +1 -0
- package/dist/session/provider.d.ts +79 -0
- package/dist/session/provider.d.ts.map +1 -0
- package/dist/session/provider.js +19 -0
- package/dist/session/provider.js.map +1 -0
- package/dist/session/scripted.d.ts +45 -0
- package/dist/session/scripted.d.ts.map +1 -0
- package/dist/session/scripted.js +236 -0
- package/dist/session/scripted.js.map +1 -0
- package/dist/session/selection.d.ts +37 -0
- package/dist/session/selection.d.ts.map +1 -0
- package/dist/session/selection.js +104 -0
- package/dist/session/selection.js.map +1 -0
- package/dist/session/service-check.d.ts +48 -0
- package/dist/session/service-check.d.ts.map +1 -0
- package/dist/session/service-check.js +86 -0
- package/dist/session/service-check.js.map +1 -0
- package/dist/session/service.d.ts +128 -0
- package/dist/session/service.d.ts.map +1 -0
- package/dist/session/service.js +150 -0
- package/dist/session/service.js.map +1 -0
- package/dist/session/session.d.ts +95 -0
- package/dist/session/session.d.ts.map +1 -0
- package/dist/session/session.js +312 -0
- package/dist/session/session.js.map +1 -0
- package/dist/session/toolbox.d.ts +69 -0
- package/dist/session/toolbox.d.ts.map +1 -0
- package/dist/session/toolbox.js +391 -0
- package/dist/session/toolbox.js.map +1 -0
- package/dist/session/tools.d.ts +45 -0
- package/dist/session/tools.d.ts.map +1 -0
- package/dist/session/tools.js +137 -0
- package/dist/session/tools.js.map +1 -0
- package/dist/session/world.d.ts +188 -0
- package/dist/session/world.d.ts.map +1 -0
- package/dist/session/world.js +2 -0
- package/dist/session/world.js.map +1 -0
- package/dist/sign.d.ts +19 -0
- package/dist/sign.d.ts.map +1 -0
- package/dist/sign.js +33 -0
- package/dist/sign.js.map +1 -0
- package/dist/supplier.d.ts +41 -0
- package/dist/supplier.d.ts.map +1 -0
- package/dist/supplier.js +57 -0
- package/dist/supplier.js.map +1 -0
- package/package.json +56 -3
package/dist/http.js
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import { API_PATHS, API_REQUEST_MAX_VALIDITY, contestStatementHash, describeReason, encodeApiBody, isApiAudience, newRequestId, parseApiError, parseCancellationAnswer, parseContestStatementAnswer, parseEnrollment, parseReserveApproval, parseSponsorship, parseSponsorshipRefusal, parseStartReceiptAnswer, parseStartRequestAnswer, parseSupplierAuthorizationAnswer, signApiRequest, } from '@avow-finance/spec';
|
|
2
|
+
/**
|
|
3
|
+
* Avow's API refused a request, answered with something the client can't use, or gave no definite
|
|
4
|
+
* answer in time. `status` is the HTTP status, 0 when no answer came, and `reason` the reason code
|
|
5
|
+
* Avow gave, or the parser's reason for an answer that doesn't parse. `requestId` names the
|
|
6
|
+
* operation. Sending the same signed bytes again is always safe, since Avow answers an identical
|
|
7
|
+
* repeat without acting again.
|
|
8
|
+
*/
|
|
9
|
+
export class AvowApiError extends Error {
|
|
10
|
+
status;
|
|
11
|
+
reason;
|
|
12
|
+
requestId;
|
|
13
|
+
/**
|
|
14
|
+
* Whether Avow answered definitely, with a refusal or an answer the client can't use. When it's
|
|
15
|
+
* false the client gave up with no definite answer, so Avow may still have recorded the request,
|
|
16
|
+
* and a caller shouldn't treat it as refused.
|
|
17
|
+
*/
|
|
18
|
+
definite;
|
|
19
|
+
/**
|
|
20
|
+
* What Avow's relay added to its refusal, such as when the key can ask again or which budget is
|
|
21
|
+
* spent, or null for any other answer.
|
|
22
|
+
*/
|
|
23
|
+
sponsorship;
|
|
24
|
+
constructor(message, details, options) {
|
|
25
|
+
super(message, options);
|
|
26
|
+
this.name = 'AvowApiError';
|
|
27
|
+
this.status = details.status;
|
|
28
|
+
this.reason = details.reason;
|
|
29
|
+
this.requestId = details.requestId;
|
|
30
|
+
this.definite = details.definite;
|
|
31
|
+
this.sponsorship = details.sponsorship ?? null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/** The reason Avow gave somewhere in an error's causes, such as its relay's refusal. */
|
|
35
|
+
export function relayReason(error) {
|
|
36
|
+
let e = error;
|
|
37
|
+
for (let i = 0; i < 6 && e instanceof Error; i++) {
|
|
38
|
+
if (e instanceof AvowApiError && e.reason)
|
|
39
|
+
return e.reason;
|
|
40
|
+
e = e.cause;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
/** The agent's access key as a request signer: its own address and its bare signature. */
|
|
45
|
+
export function agentRequestKey(key) {
|
|
46
|
+
return {
|
|
47
|
+
address: key.accessKeyAddress,
|
|
48
|
+
signDigest: (hash) => key.sign({ hash, raw: true }),
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* The business account's root key as a request signer. Its address is the account's own, and a
|
|
53
|
+
* root key signs bare, so Avow reads the signature as the account's without a keychain envelope.
|
|
54
|
+
*/
|
|
55
|
+
export function rootRequestKey(root) {
|
|
56
|
+
return { address: root.address, signDigest: (hash) => root.sign({ hash }) };
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* A supplier's signing key as a request signer, for the requests a supplier signs itself, such as
|
|
60
|
+
* asking Avow for an authorization or quoting the test customer. It's a plain key, so it signs bare.
|
|
61
|
+
*/
|
|
62
|
+
export function signingRequestKey(key) {
|
|
63
|
+
return { address: key.address, signDigest: (hash) => key.sign({ hash }) };
|
|
64
|
+
}
|
|
65
|
+
const MAX_WAIT = 15_000;
|
|
66
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
67
|
+
const describe = (e) => (e instanceof Error ? e.message : String(e)).slice(0, 300);
|
|
68
|
+
const systemClock = () => BigInt(Math.floor(Date.now() / 1000));
|
|
69
|
+
function readJson(text) {
|
|
70
|
+
try {
|
|
71
|
+
return JSON.parse(text);
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
/** The audience a base URL names, or a throw when it names more than an origin. */
|
|
78
|
+
function audienceOf(baseUrl) {
|
|
79
|
+
const url = new URL(baseUrl);
|
|
80
|
+
if ((url.pathname !== '/' && url.pathname !== '') || url.search !== '' || url.hash !== '') {
|
|
81
|
+
throw new RangeError(`${baseUrl} has to be an origin, with no path, query or fragment`);
|
|
82
|
+
}
|
|
83
|
+
if (url.username !== '' || url.password !== '') {
|
|
84
|
+
throw new RangeError(`${baseUrl} can't carry a user name or password`);
|
|
85
|
+
}
|
|
86
|
+
if (!isApiAudience(url.origin)) {
|
|
87
|
+
throw new RangeError(`${url.origin} isn't an http or https origin Avow can serve`);
|
|
88
|
+
}
|
|
89
|
+
return url.origin;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Sends operations to Avow's hosted API, each with a fresh request id and signed with `key` for the
|
|
93
|
+
* deployment at `baseUrl`. It sends the same bytes again after no answer, an answer of any status
|
|
94
|
+
* whose body isn't JSON, a 5xx or `request.in_progress`, until Avow answers definitely or
|
|
95
|
+
* `retryFor` runs out. Avow answers with JSON whatever the status, so bytes that aren't JSON never
|
|
96
|
+
* came whole from Avow. Avow answers an identical repeat with its first answer and does nothing
|
|
97
|
+
* again, so a lost answer never turns into a second action. It never follows a redirect, so a
|
|
98
|
+
* signed body goes nowhere but the deployment it was signed for. A refusal, which is any other 4xx
|
|
99
|
+
* with a JSON body, a 2xx answer `read` refuses, or no definite answer in time, throws an
|
|
100
|
+
* `AvowApiError` with the reason.
|
|
101
|
+
*/
|
|
102
|
+
export function avowRequests(config) {
|
|
103
|
+
const audience = audienceOf(config.baseUrl);
|
|
104
|
+
const { key } = config;
|
|
105
|
+
const validity = config.requestValidity ?? 120n;
|
|
106
|
+
if (validity <= 0n || validity > API_REQUEST_MAX_VALIDITY) {
|
|
107
|
+
throw new RangeError(`A request can stay valid for 1 to ${API_REQUEST_MAX_VALIDITY} seconds`);
|
|
108
|
+
}
|
|
109
|
+
const retryFor = config.retryFor ?? 600n;
|
|
110
|
+
const firstWait = config.retryWait ?? 1000;
|
|
111
|
+
const attemptTimeout = config.attemptTimeout ?? 30_000;
|
|
112
|
+
const now = config.now ?? systemClock;
|
|
113
|
+
const fetchOnce = config.fetch ?? fetch;
|
|
114
|
+
return async function send({ domain, path, body: value, read }) {
|
|
115
|
+
const body = encodeApiBody(value);
|
|
116
|
+
const requestId = newRequestId();
|
|
117
|
+
const { authorization, request, digest } = await signApiRequest({
|
|
118
|
+
key,
|
|
119
|
+
domain,
|
|
120
|
+
audience,
|
|
121
|
+
method: 'POST',
|
|
122
|
+
path,
|
|
123
|
+
body,
|
|
124
|
+
requestId,
|
|
125
|
+
expiry: now() + validity,
|
|
126
|
+
});
|
|
127
|
+
config.onSigned?.(request);
|
|
128
|
+
const giveUpAt = now() + retryFor;
|
|
129
|
+
let wait = firstWait;
|
|
130
|
+
let last = 'no attempt was made';
|
|
131
|
+
let lastStatus = 0;
|
|
132
|
+
let lastReason = null;
|
|
133
|
+
for (;;) {
|
|
134
|
+
try {
|
|
135
|
+
const response = await fetchOnce(`${audience}${path}`, {
|
|
136
|
+
method: 'POST',
|
|
137
|
+
headers: { authorization, 'content-type': 'application/json' },
|
|
138
|
+
body,
|
|
139
|
+
redirect: 'error',
|
|
140
|
+
signal: AbortSignal.timeout(attemptTimeout),
|
|
141
|
+
});
|
|
142
|
+
const json = readJson(await response.text());
|
|
143
|
+
lastStatus = response.status;
|
|
144
|
+
if (json === undefined) {
|
|
145
|
+
// Avow answers with JSON whatever the status, its refusals included, so bytes that aren't
|
|
146
|
+
// JSON were cut off or replaced on their way, as by a proxy's error page, and go again.
|
|
147
|
+
last = `The last answer was unreadable, a ${response.status} whose body isn't JSON.`;
|
|
148
|
+
lastReason = null;
|
|
149
|
+
}
|
|
150
|
+
else if (response.ok) {
|
|
151
|
+
const answer = read(json, { digest, audience });
|
|
152
|
+
if (answer.ok)
|
|
153
|
+
return answer.value;
|
|
154
|
+
throw new AvowApiError(`Avow's answer can't be used: ${describeReason(answer.reason)}`, {
|
|
155
|
+
status: lastStatus,
|
|
156
|
+
reason: answer.reason,
|
|
157
|
+
requestId,
|
|
158
|
+
definite: true,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
const error = parseApiError(json);
|
|
163
|
+
lastReason = error?.reason ?? null;
|
|
164
|
+
last = error?.message ?? `Avow's API answered ${response.status}`;
|
|
165
|
+
if (response.status < 500 && lastReason !== 'request.in_progress') {
|
|
166
|
+
throw new AvowApiError(last, {
|
|
167
|
+
status: lastStatus,
|
|
168
|
+
reason: lastReason,
|
|
169
|
+
requestId,
|
|
170
|
+
definite: true,
|
|
171
|
+
sponsorship: parseSponsorshipRefusal(json),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch (e) {
|
|
177
|
+
if (e instanceof AvowApiError)
|
|
178
|
+
throw e;
|
|
179
|
+
last = `Avow's API didn't answer: ${describe(e)}`;
|
|
180
|
+
lastStatus = 0;
|
|
181
|
+
lastReason = null;
|
|
182
|
+
}
|
|
183
|
+
if (now() >= giveUpAt) {
|
|
184
|
+
throw new AvowApiError(`No definite answer from Avow. ${last}`, {
|
|
185
|
+
status: lastStatus,
|
|
186
|
+
reason: lastReason,
|
|
187
|
+
requestId,
|
|
188
|
+
definite: false,
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
config.onResend?.({
|
|
192
|
+
requestId,
|
|
193
|
+
status: lastStatus,
|
|
194
|
+
reason: lastReason,
|
|
195
|
+
message: last,
|
|
196
|
+
waitMs: wait,
|
|
197
|
+
});
|
|
198
|
+
await sleep(wait);
|
|
199
|
+
wait = Math.min(wait * 2, MAX_WAIT);
|
|
200
|
+
}
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
/** Keeps a parsed answer's value as `pick` shapes it, or its refusal as it is. */
|
|
204
|
+
const shaped = (parsed, pick) => parsed.ok ? { ok: true, value: pick(parsed.value) } : parsed;
|
|
205
|
+
/**
|
|
206
|
+
* Avow's hosted API as an `AvowApi`, for an agent whose mandate names `domain`. Each call is one
|
|
207
|
+
* operation sent through `avowRequests`, so a lost answer never turns into a second approval, a
|
|
208
|
+
* second co-signature or a second record. It serves the reserve check, the relay and Avow's records
|
|
209
|
+
* of cancellations and job starts. Each record's answer carries Avow's timestamp, which has to name
|
|
210
|
+
* the digest of the request the agent signed and the time the answer gives. A commitment already
|
|
211
|
+
* cancelled answers with the first cancellation, whose timestamp names the request that carried it,
|
|
212
|
+
* so `cancel` gives the first cancellation's time to every later call too.
|
|
213
|
+
*/
|
|
214
|
+
export function httpAvowApi(config) {
|
|
215
|
+
const send = avowRequests(config);
|
|
216
|
+
const { domain } = config;
|
|
217
|
+
const expected = { chainId: domain.chainId, lock: domain.verifyingContract };
|
|
218
|
+
return {
|
|
219
|
+
records: {
|
|
220
|
+
cancel: ({ commitmentId }) => send({
|
|
221
|
+
domain,
|
|
222
|
+
path: API_PATHS.cancellations(commitmentId),
|
|
223
|
+
body: {},
|
|
224
|
+
read: (json, { digest, audience }) => shaped(parseCancellationAnswer(json, {
|
|
225
|
+
...expected,
|
|
226
|
+
audience,
|
|
227
|
+
commitmentId,
|
|
228
|
+
requestHash: digest,
|
|
229
|
+
}), (a) => ({ cancelledAt: a.cancelledAt })),
|
|
230
|
+
}),
|
|
231
|
+
requestStart: ({ commitmentId }) => send({
|
|
232
|
+
domain,
|
|
233
|
+
path: API_PATHS.startRequests(commitmentId),
|
|
234
|
+
body: {},
|
|
235
|
+
read: (json, { digest }) => shaped(parseStartRequestAnswer(json, { ...expected, requestHash: digest }), (a) => ({
|
|
236
|
+
startRequestHash: a.startRequestHash,
|
|
237
|
+
requestedAt: a.requestedAt,
|
|
238
|
+
})),
|
|
239
|
+
}),
|
|
240
|
+
recordStart: ({ commitmentId, receipt }) => send({
|
|
241
|
+
domain,
|
|
242
|
+
path: API_PATHS.startReceipts(commitmentId),
|
|
243
|
+
body: { receipt },
|
|
244
|
+
read: parseStartReceiptAnswer,
|
|
245
|
+
}),
|
|
246
|
+
},
|
|
247
|
+
approveReserve: (body) => send({
|
|
248
|
+
domain,
|
|
249
|
+
path: API_PATHS.reserveApprovals,
|
|
250
|
+
body,
|
|
251
|
+
read: (json) => parseReserveApproval(json, expected),
|
|
252
|
+
}),
|
|
253
|
+
sponsor: (transaction) => send({
|
|
254
|
+
domain,
|
|
255
|
+
path: API_PATHS.sponsorships,
|
|
256
|
+
body: { transaction },
|
|
257
|
+
read: (json) => {
|
|
258
|
+
const parsed = parseSponsorship(json);
|
|
259
|
+
return parsed.ok ? { ok: true, value: parsed.value.transaction } : parsed;
|
|
260
|
+
},
|
|
261
|
+
}),
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* Avow's hosted API as the records the business account's root key writes to, for the business
|
|
266
|
+
* whose lock `domain` names. Each call is one operation sent through `avowRequests`, signed with
|
|
267
|
+
* `key`, which `rootRequestKey` makes from the root key. A contest statement's answer has to name
|
|
268
|
+
* the hash of the statement sent.
|
|
269
|
+
*/
|
|
270
|
+
export function httpBusinessRecords(config) {
|
|
271
|
+
const send = avowRequests(config);
|
|
272
|
+
const { domain } = config;
|
|
273
|
+
return {
|
|
274
|
+
postContestStatement: (request) => send({
|
|
275
|
+
domain,
|
|
276
|
+
path: API_PATHS.contestStatements(request.statement.commitmentId),
|
|
277
|
+
body: request,
|
|
278
|
+
read: (json) => parseContestStatementAnswer(json, {
|
|
279
|
+
statementHash: contestStatementHash(request.statement),
|
|
280
|
+
}),
|
|
281
|
+
}),
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Enrolls an agent with Avow once its key is on the chain with the mandate's hash. The agent signs
|
|
286
|
+
* the request with that key, under the mandate's chain and lock, and gets back what Avow recorded.
|
|
287
|
+
*/
|
|
288
|
+
export function enrollAgent(config, enrollment) {
|
|
289
|
+
return avowRequests(config)({
|
|
290
|
+
domain: { chainId: enrollment.mandate.chainId, verifyingContract: enrollment.mandate.lock },
|
|
291
|
+
path: API_PATHS.enrollments,
|
|
292
|
+
body: enrollment,
|
|
293
|
+
read: parseEnrollment,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Asks Avow for a fresh authorization for a commitment's reserve, signed with the supplier's
|
|
298
|
+
* signing key under the buyer's lock. Avow answers with its authorization, the approval the reserve
|
|
299
|
+
* was funded under and the buyer's mandate, which the supplier's verifier checks before it accepts,
|
|
300
|
+
* or with the reason it signed none, such as a reserve not funded yet.
|
|
301
|
+
*/
|
|
302
|
+
export function requestSupplierAuthorization(config, commitment) {
|
|
303
|
+
const expected = { chainId: commitment.chainId, lock: commitment.lock };
|
|
304
|
+
return avowRequests(config)({
|
|
305
|
+
domain: { chainId: commitment.chainId, verifyingContract: commitment.lock },
|
|
306
|
+
path: API_PATHS.supplierAuthorizations(commitment.commitmentId),
|
|
307
|
+
body: {},
|
|
308
|
+
read: (json) => parseSupplierAuthorizationAnswer(json, expected),
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
//# sourceMappingURL=http.js.map
|
package/dist/http.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../src/http.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,SAAS,EACT,wBAAwB,EAGxB,oBAAoB,EACpB,cAAc,EAGd,aAAa,EACb,aAAa,EACb,YAAY,EAEZ,aAAa,EACb,uBAAuB,EACvB,2BAA2B,EAC3B,eAAe,EACf,oBAAoB,EACpB,gBAAgB,EAChB,uBAAuB,EACvB,uBAAuB,EACvB,uBAAuB,EACvB,gCAAgC,EAMhC,cAAc,GACf,MAAM,oBAAoB,CAAA;AAO3B;;;;;;GAMG;AACH,MAAM,OAAO,YAAa,SAAQ,KAAK;IAC5B,MAAM,CAAQ;IACd,MAAM,CAAmB;IACzB,SAAS,CAAK;IACvB;;;;OAIG;IACM,QAAQ,CAAS;IAC1B;;;OAGG;IACM,WAAW,CAA2B;IAE/C,YACE,OAAe,EACf,OAMC,EACD,OAA6B;QAE7B,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACvB,IAAI,CAAC,IAAI,GAAG,cAAc,CAAA;QAC1B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAA;QAC5B,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC,SAAS,CAAA;QAClC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAA;QAChC,IAAI,CAAC,WAAW,GAAG,OAAO,CAAC,WAAW,IAAI,IAAI,CAAA;IAChD,CAAC;CACF;AAED,wFAAwF;AACxF,MAAM,UAAU,WAAW,CAAC,KAAc;IACx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C/D,IAAI,EAAE,EAAE;QACR,IAAI,EAAE,CAAC,IAAI,EAAE,EAAE,CAAC,gCAAgC,CAAC,IAAI,EAAE,QAAQ,CAAC;KACjE,CAAC,CAAA;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Avow SDK for agents. An agent books with a supplier, has Avow approve the reserve, checks the
|
|
3
|
+
* approval against the terms and funds the reserve with its own key, then reads what the lock
|
|
4
|
+
* holds, and cancels or starts the job through an `AvowApi` that serves those records. The
|
|
5
|
+
* administrator's side signs for the business account in Tempo's keychain V2 envelope, which the
|
|
6
|
+
* lock and Avow's API check, and the account's root key contests a charge on its own lock.
|
|
7
|
+
*
|
|
8
|
+
* Pre-release and testnet only. The lock it calls is unaudited, and every interface may change
|
|
9
|
+
* before 1.0.
|
|
10
|
+
*/
|
|
11
|
+
export declare const packageName = "@avow-finance/sdk";
|
|
12
|
+
export { type HashSigner, keychainV2Hash, readKeychainEnvelope, signForAccount, wrapKeychainV2, } from '@avow-finance/spec';
|
|
13
|
+
export * from './agent.js';
|
|
14
|
+
export * from './agent-key.js';
|
|
15
|
+
export * from './avow-api.js';
|
|
16
|
+
export * from './contest.js';
|
|
17
|
+
export * from './http.js';
|
|
18
|
+
export * from './job/accept.js';
|
|
19
|
+
export * from './job/customer.js';
|
|
20
|
+
export * from './job/quote.js';
|
|
21
|
+
export * from './lock.js';
|
|
22
|
+
export * from './moderato.js';
|
|
23
|
+
export { dailyLimitOf, fundingRefusal, keyLeftToday, Refusal, type SandboxAgent, type SandboxAgentOptions, type SandboxBooking, type SandboxCheck, type SandboxCommitment, type SandboxCommitmentRecord, type SandboxPlatform, type SandboxSession, sandboxAgent, setSandboxPlatform, } from './sandbox/agent.js';
|
|
24
|
+
export * from './sandbox/business.js';
|
|
25
|
+
export { approvalLink, type ConnectEvent, type ConnectOptions, connectToBusiness, SANDBOX_CONSOLE_URL, } from './sandbox/connect.js';
|
|
26
|
+
export { loadSandboxDeployment, madeOn, trustedFactory } from './sandbox/deployment.js';
|
|
27
|
+
export { approvedSuppliers, liveKeys, lockCreatedIn, revokeKeyCalls, revokeOtherKeys, } from './sandbox/keys.js';
|
|
28
|
+
export { type BuyOutcome, type BuyStep, MAILBOX_ORDER_SECONDS, type MailboxOptions, type SandboxMailbox, type SellStep, sandboxMailbox, } from './sandbox/mailbox.js';
|
|
29
|
+
export * from './sandbox/mandate.js';
|
|
30
|
+
export { sandbox } from './sandbox/namespace.js';
|
|
31
|
+
export * from './sandbox/session.js';
|
|
32
|
+
export { cancelSandboxCommitment, type PreparedSandboxBusiness, type PrepareSandboxBusinessOptions, prepareSandboxBusiness, type SandboxBusinessConfig, type SandboxPrompts, } from './sandbox/setup.js';
|
|
33
|
+
export { memoryStore, type SandboxStore, type SandboxStoreItem, type SandboxSupplier, } from './sandbox/store.js';
|
|
34
|
+
export * from './sandbox/terms.js';
|
|
35
|
+
export * from './send.js';
|
|
36
|
+
export * from './sign.js';
|
|
37
|
+
export * from './supplier.js';
|
|
38
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,eAAO,MAAM,WAAW,sBAAsB,CAAA;AAE9C,OAAO,EACL,KAAK,UAAU,EACf,cAAc,EACd,oBAAoB,EACpB,cAAc,EACd,cAAc,GACf,MAAM,oBAAoB,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,OAAO,EACL,YAAY,EACZ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|
+
* Avow SDK for agents. An agent books with a supplier, has Avow approve the reserve, checks the
|
|
3
|
+
* approval against the terms and funds the reserve with its own key, then reads what the lock
|
|
4
|
+
* holds, and cancels or starts the job through an `AvowApi` that serves those records. The
|
|
5
|
+
* administrator's side signs for the business account in Tempo's keychain V2 envelope, which the
|
|
6
|
+
* lock and Avow's API check, and the account's root key contests a charge on its own lock.
|
|
7
|
+
*
|
|
8
|
+
* Pre-release and testnet only. The lock it calls is unaudited, and every interface may change
|
|
9
|
+
* before 1.0.
|
|
10
|
+
*/
|
|
11
|
+
export const packageName = '@avow-finance/sdk';
|
|
12
|
+
export { keychainV2Hash, readKeychainEnvelope, signForAccount, wrapKeychainV2, } from '@avow-finance/spec';
|
|
13
|
+
export * from './agent.js';
|
|
14
|
+
export * from './agent-key.js';
|
|
15
|
+
export * from './avow-api.js';
|
|
16
|
+
export * from './contest.js';
|
|
17
|
+
export * from './http.js';
|
|
18
|
+
export * from './job/accept.js';
|
|
19
|
+
export * from './job/customer.js';
|
|
20
|
+
export * from './job/quote.js';
|
|
21
|
+
export * from './lock.js';
|
|
22
|
+
export * from './moderato.js';
|
|
23
|
+
export { dailyLimitOf, fundingRefusal, keyLeftToday, Refusal, sandboxAgent, setSandboxPlatform, } from './sandbox/agent.js';
|
|
24
|
+
export * from './sandbox/business.js';
|
|
25
|
+
export { approvalLink, connectToBusiness, SANDBOX_CONSOLE_URL, } from './sandbox/connect.js';
|
|
26
|
+
export { loadSandboxDeployment, madeOn, trustedFactory } from './sandbox/deployment.js';
|
|
27
|
+
export { approvedSuppliers, liveKeys, lockCreatedIn, revokeKeyCalls, revokeOtherKeys, } from './sandbox/keys.js';
|
|
28
|
+
export { MAILBOX_ORDER_SECONDS, sandboxMailbox, } from './sandbox/mailbox.js';
|
|
29
|
+
export * from './sandbox/mandate.js';
|
|
30
|
+
export { sandbox } from './sandbox/namespace.js';
|
|
31
|
+
export * from './sandbox/session.js';
|
|
32
|
+
export { cancelSandboxCommitment, prepareSandboxBusiness, } from './sandbox/setup.js';
|
|
33
|
+
export { memoryStore, } from './sandbox/store.js';
|
|
34
|
+
export * from './sandbox/terms.js';
|
|
35
|
+
export * from './send.js';
|
|
36
|
+
export * from './sign.js';
|
|
37
|
+
export * from './supplier.js';
|
|
38
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG,mBAAmB,CAAA;AAE9C,OAAO,EAEL,cAAc,EACd,oBAAoB,EACpB,cAAc,EACd,cAAc,GACf,MAAM,oBAAoB,CAAA;AAC3B,cAAc,YAAY,CAAA;AAC1B,cAAc,gBAAgB,CAAA;AAC9B,cAAc,eAAe,CAAA;AAC7B,cAAc,cAAc,CAAA;AAC5B,cAAc,WAAW,CAAA;AACzB,cAAc,iBAAiB,CAAA;AAC/B,cAAc,mBAAmB,CAAA;AACjC,cAAc,gBAAgB,CAAA;AAC9B,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA;AAC7B,OAAO,EACL,YAAY,EACZ,cAAc,EACd,YAAY,EACZ,OAAO,EASP,YAAY,EACZ,kBAAkB,GACnB,MAAM,oBAAoB,CAAA;AAC3B,cAAc,uBAAuB,CAAA;AACrC,OAAO,EACL,YAAY,EAGZ,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,sBAAsB,CAAA;AAC7B,OAAO,EAAE,qBAAqB,EAAE,MAAM,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAA;AACvF,OAAO,EACL,iBAAiB,EACjB,QAAQ,EACR,aAAa,EACb,cAAc,EACd,eAAe,GAChB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAGL,qBAAqB,EAIrB,cAAc,GACf,MAAM,sBAAsB,CAAA;AAC7B,cAAc,sBAAsB,CAAA;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAChD,cAAc,sBAAsB,CAAA;AACpC,OAAO,EACL,uBAAuB,EAGvB,sBAAsB,GAGvB,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,WAAW,GAIZ,MAAM,oBAAoB,CAAA;AAC3B,cAAc,oBAAoB,CAAA;AAClC,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,cAAc,eAAe,CAAA"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type Acceptance, type CheckedDependency, type CommitmentRefusalLayer, type HashSigner, type JobBuyer, type PinnedBlock, type QuoteRequest, type ReasonCode, type Signed, type SupplierAuthorizationAnswer } from '@avow-finance/spec';
|
|
2
|
+
import { type AcceptanceConfig, type ChainReader, type DependencyDeps } from '@avow-finance/verifier';
|
|
3
|
+
import { type Address, type Hex } from 'viem';
|
|
4
|
+
import type { Landed } from '../agent.js';
|
|
5
|
+
/** What accepting a job's commitment needs, which the job process wires once. */
|
|
6
|
+
export interface JobAcceptanceDeps {
|
|
7
|
+
/** The verifier's settings, with the supplier's account, signing key and Avow's keys. */
|
|
8
|
+
config: AcceptanceConfig;
|
|
9
|
+
chain: ChainReader;
|
|
10
|
+
/** The readers of the locks the business's compute bookings sit in. */
|
|
11
|
+
dependencies: DependencyDeps;
|
|
12
|
+
/**
|
|
13
|
+
* Asks Avow for a fresh authorization for a commitment, signed with the signing key, as
|
|
14
|
+
* `requestSupplierAuthorization` does. A definite refusal throws an `AvowApiError`.
|
|
15
|
+
*/
|
|
16
|
+
authorize: (c: {
|
|
17
|
+
chainId: number;
|
|
18
|
+
lock: Address;
|
|
19
|
+
commitmentId: Hex;
|
|
20
|
+
}) => Promise<SupplierAuthorizationAnswer>;
|
|
21
|
+
/** The supplier's signing key, which signs the acceptance. */
|
|
22
|
+
signingKey: HashSigner;
|
|
23
|
+
/**
|
|
24
|
+
* Sends the acceptance's call and resolves once it lands, as `sendOwn` does from the signing
|
|
25
|
+
* key, which pays its own fee. It throws a `SendError` when the call reverts or its outcome is
|
|
26
|
+
* unknown, and a sender that goes through Avow's relay throws one carrying the relay's refusal.
|
|
27
|
+
*/
|
|
28
|
+
send: (call: {
|
|
29
|
+
to: Address;
|
|
30
|
+
data: Hex;
|
|
31
|
+
}) => Promise<Landed>;
|
|
32
|
+
now: () => bigint;
|
|
33
|
+
}
|
|
34
|
+
/** A job's commitment accepted, or why the developer's side refused it. */
|
|
35
|
+
export type JobAcceptance = {
|
|
36
|
+
ok: true;
|
|
37
|
+
/**
|
|
38
|
+
* The acceptance this call signed, the disclosure the job process confirmed before signing
|
|
39
|
+
* it, and the transaction it landed in. Null when the customer's lock already showed the
|
|
40
|
+
* commitment accepted under the supplier's signing key, as when an earlier call's acceptance
|
|
41
|
+
* landed after its send gave no answer.
|
|
42
|
+
*/
|
|
43
|
+
sent: {
|
|
44
|
+
acceptance: Signed<Acceptance>;
|
|
45
|
+
disclosureId: Hex;
|
|
46
|
+
landed: Landed;
|
|
47
|
+
} | null;
|
|
48
|
+
/** Each compute booking as this call read it again, with its block. */
|
|
49
|
+
checks: readonly CheckedDependency[];
|
|
50
|
+
} | {
|
|
51
|
+
ok: false;
|
|
52
|
+
code: ReasonCode;
|
|
53
|
+
layer: CommitmentRefusalLayer;
|
|
54
|
+
block: PinnedBlock | null;
|
|
55
|
+
index: number | null;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Checks the customer's commitment for a job and accepts it on the customer's lock. Avow's
|
|
59
|
+
* authorization comes with the approval the customer's reserve was funded under and the customer's
|
|
60
|
+
* mandate, and `prepareAcceptance` checks all three against the quote's terms and the lock, after
|
|
61
|
+
* reading each compute booking the terms rely on again, with the owner taken from that approval.
|
|
62
|
+
* Only then does the signing key sign the acceptance and send it.
|
|
63
|
+
*
|
|
64
|
+
* Each refusal names who made it. Avow declining to authorize is Avow's, a refusal by the checks is
|
|
65
|
+
* the developer's service's at the block the check read, and an acceptance the relay wouldn't pay
|
|
66
|
+
* for or the lock reverted is theirs. A send whose outcome is unknown throws, since the acceptance
|
|
67
|
+
* may have landed, and calling this again then is safe. A refusal that says the reserve is already
|
|
68
|
+
* accepted reads the lock, and a commitment it shows accepted under this supplier's account and
|
|
69
|
+
* signing key, for the quote's terms, counts as accepted, since only that key can accept it.
|
|
70
|
+
*/
|
|
71
|
+
export declare function acceptJob(deps: JobAcceptanceDeps, job: {
|
|
72
|
+
customer: JobBuyer;
|
|
73
|
+
quote: QuoteRequest;
|
|
74
|
+
}): Promise<JobAcceptance>;
|
|
75
|
+
/**
|
|
76
|
+
* Who refused an acceptance on its way to the customer's lock, read from the error its send threw.
|
|
77
|
+
* A transaction that landed and reverted is the lock's refusal, and a relay that wouldn't pay its fee
|
|
78
|
+
* gave its reason. Anything else is null, since the acceptance may still have landed.
|
|
79
|
+
*/
|
|
80
|
+
export declare function acceptanceSendRefusal(error: unknown): {
|
|
81
|
+
code: ReasonCode;
|
|
82
|
+
layer: 'relay' | 'lock';
|
|
83
|
+
} | null;
|
|
84
|
+
//# sourceMappingURL=accept.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accept.d.ts","sourceRoot":"","sources":["../../src/job/accept.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,UAAU,EACf,KAAK,iBAAiB,EACtB,KAAK,sBAAsB,EAC3B,KAAK,UAAU,EAEf,KAAK,QAAQ,EACb,KAAK,WAAW,EAChB,KAAK,YAAY,EACjB,KAAK,UAAU,EACf,KAAK,MAAM,EACX,KAAK,2BAA2B,EAEjC,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EACL,KAAK,gBAAgB,EAErB,KAAK,WAAW,EAChB,KAAK,cAAc,EAIpB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAAE,KAAK,OAAO,EAAE,KAAK,GAAG,EAAkB,MAAM,MAAM,CAAA;AAC7D,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AAIzC,iFAAiF;AACjF,MAAM,WAAW,iBAAiB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C,CAIxB;AA2GD;;;;GAIG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,OAAO,GACb;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAA;CAAE,GAAG,IAAI,CAOtD"}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { isChainedTerms, toWire, } from '@avow-finance/spec';
|
|
2
|
+
import { acceptCall, prepareAcceptance, signAcceptance, verifyDependencies, } from '@avow-finance/verifier';
|
|
3
|
+
import { isAddressEqual } from 'viem';
|
|
4
|
+
import { AvowApiError, relayReason } from '../http.js';
|
|
5
|
+
import { landedReverted, SendError, sendErrorIn } from '../send.js';
|
|
6
|
+
/**
|
|
7
|
+
* The refusals that can mean the lock already holds an acceptance, Avow's or the verifier's for a
|
|
8
|
+
* reserve already accepted, and the lock's revert of a second one.
|
|
9
|
+
*/
|
|
10
|
+
const MAY_BE_ACCEPTED = [
|
|
11
|
+
'chain.reserve_accepted',
|
|
12
|
+
'chain.acceptance_reverted',
|
|
13
|
+
];
|
|
14
|
+
/**
|
|
15
|
+
* Checks the customer's commitment for a job and accepts it on the customer's lock. Avow's
|
|
16
|
+
* authorization comes with the approval the customer's reserve was funded under and the customer's
|
|
17
|
+
* mandate, and `prepareAcceptance` checks all three against the quote's terms and the lock, after
|
|
18
|
+
* reading each compute booking the terms rely on again, with the owner taken from that approval.
|
|
19
|
+
* Only then does the signing key sign the acceptance and send it.
|
|
20
|
+
*
|
|
21
|
+
* Each refusal names who made it. Avow declining to authorize is Avow's, a refusal by the checks is
|
|
22
|
+
* the developer's service's at the block the check read, and an acceptance the relay wouldn't pay
|
|
23
|
+
* for or the lock reverted is theirs. A send whose outcome is unknown throws, since the acceptance
|
|
24
|
+
* may have landed, and calling this again then is safe. A refusal that says the reserve is already
|
|
25
|
+
* accepted reads the lock, and a commitment it shows accepted under this supplier's account and
|
|
26
|
+
* signing key, for the quote's terms, counts as accepted, since only that key can accept it.
|
|
27
|
+
*/
|
|
28
|
+
export async function acceptJob(deps, job) {
|
|
29
|
+
const outcome = await signAndSend(deps, job);
|
|
30
|
+
if (outcome.ok || !MAY_BE_ACCEPTED.includes(outcome.code))
|
|
31
|
+
return outcome;
|
|
32
|
+
return (await acceptedEarlier(deps, job)) ?? outcome;
|
|
33
|
+
}
|
|
34
|
+
async function signAndSend(deps, job) {
|
|
35
|
+
const refuse = (code, layer, block = null, index = null) => ({ ok: false, code, layer, block, index });
|
|
36
|
+
const { customer, quote } = job;
|
|
37
|
+
const a = quote.agreement;
|
|
38
|
+
let answer;
|
|
39
|
+
try {
|
|
40
|
+
answer = await deps.authorize({
|
|
41
|
+
chainId: customer.chainId,
|
|
42
|
+
lock: customer.lock,
|
|
43
|
+
commitmentId: a.commitmentId,
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
if (error instanceof AvowApiError && error.definite && error.reason) {
|
|
48
|
+
return refuse(error.reason, 'avow');
|
|
49
|
+
}
|
|
50
|
+
throw error;
|
|
51
|
+
}
|
|
52
|
+
if (!answer.ok)
|
|
53
|
+
return refuse(answer.reason, 'avow');
|
|
54
|
+
// The verifier reads Avow's messages as they travel, in JSON.
|
|
55
|
+
const prepared = await prepareAcceptance({
|
|
56
|
+
authorization: toWire(answer.authorization),
|
|
57
|
+
approval: toWire(answer.approval),
|
|
58
|
+
mandate: toWire(answer.mandate),
|
|
59
|
+
agreed: {
|
|
60
|
+
commitmentId: a.commitmentId,
|
|
61
|
+
termsHash: a.termsHash,
|
|
62
|
+
category: a.category,
|
|
63
|
+
buyerBusinessId: a.buyerBusinessId,
|
|
64
|
+
mandateHash: a.mandateHash,
|
|
65
|
+
},
|
|
66
|
+
terms: quote.terms,
|
|
67
|
+
disclosed: quote.disclosed,
|
|
68
|
+
now: deps.now(),
|
|
69
|
+
}, { config: deps.config, chain: deps.chain, dependencies: deps.dependencies });
|
|
70
|
+
if (!prepared.ok) {
|
|
71
|
+
return 'block' in prepared
|
|
72
|
+
? refuse(prepared.reason, 'service', prepared.block, prepared.index)
|
|
73
|
+
: refuse(prepared.reason, 'service');
|
|
74
|
+
}
|
|
75
|
+
// The job process accepts on the developer's behalf, so it confirms the disclosure it was given.
|
|
76
|
+
const disclosureId = prepared.disclosure.id;
|
|
77
|
+
const acceptance = await signAcceptance(prepared, deps.signingKey, { disclosureId });
|
|
78
|
+
let landed;
|
|
79
|
+
try {
|
|
80
|
+
landed = await deps.send(acceptCall(acceptance));
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
const refused = acceptanceSendRefusal(error);
|
|
84
|
+
if (refused)
|
|
85
|
+
return refuse(refused.code, refused.layer);
|
|
86
|
+
throw error;
|
|
87
|
+
}
|
|
88
|
+
return { ok: true, sent: { acceptance, disclosureId, landed }, checks: prepared.dependencies };
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The commitment accepted by an earlier call, with each compute booking read again, or null when
|
|
92
|
+
* the customer's lock doesn't show it accepted under this supplier's account and signing key for
|
|
93
|
+
* the quote's terms. The lock takes an acceptance only from the signing key Avow's approval named,
|
|
94
|
+
* so one it shows under this signing key came from this supplier. Throws when a booking no longer
|
|
95
|
+
* stands, since the commitment can't be refused once it's accepted.
|
|
96
|
+
*/
|
|
97
|
+
async function acceptedEarlier(deps, job) {
|
|
98
|
+
const { customer, quote } = job;
|
|
99
|
+
const a = quote.agreement;
|
|
100
|
+
if (!isChainedTerms(quote.terms))
|
|
101
|
+
return null;
|
|
102
|
+
const held = await deps.chain.backing(customer.lock, a.commitmentId, 'buyer');
|
|
103
|
+
if (held?.state !== 'accepted' ||
|
|
104
|
+
!isAddressEqual(held.supplierSigner, deps.config.signingKey) ||
|
|
105
|
+
!isAddressEqual(held.supplier, deps.config.account) ||
|
|
106
|
+
held.termsHash.toLowerCase() !== a.termsHash.toLowerCase()) {
|
|
107
|
+
return null;
|
|
108
|
+
}
|
|
109
|
+
const relied = await verifyDependencies({
|
|
110
|
+
terms: quote.terms,
|
|
111
|
+
approval: { side: 'buyer', supplier: held.supplier },
|
|
112
|
+
disclosed: quote.disclosed,
|
|
113
|
+
now: deps.now(),
|
|
114
|
+
}, deps.dependencies);
|
|
115
|
+
if (!relied.ok) {
|
|
116
|
+
const at = relied.block ? ` at block ${relied.block.number}` : '';
|
|
117
|
+
throw new Error(`The customer's lock shows commitment ${a.commitmentId} accepted under your signing key, but the compute booking it relies on doesn't stand${at} (${relied.reason}).`);
|
|
118
|
+
}
|
|
119
|
+
return { ok: true, sent: null, checks: relied.checked };
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Who refused an acceptance on its way to the customer's lock, read from the error its send threw.
|
|
123
|
+
* A transaction that landed and reverted is the lock's refusal, and a relay that wouldn't pay its fee
|
|
124
|
+
* gave its reason. Anything else is null, since the acceptance may still have landed.
|
|
125
|
+
*/
|
|
126
|
+
export function acceptanceSendRefusal(error) {
|
|
127
|
+
if (!(error instanceof SendError) || error.outcome !== 'failed')
|
|
128
|
+
return null;
|
|
129
|
+
if (landedReverted(error))
|
|
130
|
+
return { code: 'chain.acceptance_reverted', layer: 'lock' };
|
|
131
|
+
if (sendErrorIn(error)?.stage === 'altered')
|
|
132
|
+
return { code: 'sponsorship.altered', layer: 'relay' };
|
|
133
|
+
const relay = relayReason(error);
|
|
134
|
+
return relay ? { code: relay, layer: 'relay' } : null;
|
|
135
|
+
}
|
|
136
|
+
//# sourceMappingURL=accept.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accept.js","sourceRoot":"","sources":["../../src/job/accept.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,cAAc,EAOd,MAAM,GACP,MAAM,oBAAoB,CAAA;AAC3B,OAAO,EAEL,UAAU,EAGV,iBAAiB,EACjB,cAAc,EACd,kBAAkB,GACnB,MAAM,wBAAwB,CAAA;AAC/B,OAAO,EAA0B,cAAc,EAAE,MAAM,MAAM,CAAA;AAE7D,OAAO,EAAE,YAAY,EAA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