@avow-finance/spec 0.0.0-stage → 0.1.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 +119 -2
- package/dist/agent-connect.d.ts +82 -0
- package/dist/agent-connect.d.ts.map +1 -0
- package/dist/agent-connect.js +91 -0
- package/dist/agent-connect.js.map +1 -0
- package/dist/agent-session.d.ts +791 -0
- package/dist/agent-session.d.ts.map +1 -0
- package/dist/agent-session.js +176 -0
- package/dist/agent-session.js.map +1 -0
- package/dist/ap2.d.ts +79 -0
- package/dist/ap2.d.ts.map +1 -0
- package/dist/ap2.js +151 -0
- package/dist/ap2.js.map +1 -0
- package/dist/api-request.d.ts +144 -0
- package/dist/api-request.d.ts.map +1 -0
- package/dist/api-request.js +246 -0
- package/dist/api-request.js.map +1 -0
- package/dist/api.d.ts +288 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +332 -0
- package/dist/api.js.map +1 -0
- package/dist/canonical.d.ts +22 -0
- package/dist/canonical.d.ts.map +1 -0
- package/dist/canonical.js +66 -0
- package/dist/canonical.js.map +1 -0
- package/dist/commitment-card.d.ts +78 -0
- package/dist/commitment-card.d.ts.map +1 -0
- package/dist/commitment-card.js +224 -0
- package/dist/commitment-card.js.map +1 -0
- package/dist/commitment.d.ts +185 -0
- package/dist/commitment.d.ts.map +1 -0
- package/dist/commitment.js +148 -0
- package/dist/commitment.js.map +1 -0
- package/dist/compute-offers.d.ts +128 -0
- package/dist/compute-offers.d.ts.map +1 -0
- package/dist/compute-offers.js +111 -0
- package/dist/compute-offers.js.map +1 -0
- package/dist/contest.d.ts +88 -0
- package/dist/contest.d.ts.map +1 -0
- package/dist/contest.js +131 -0
- package/dist/contest.js.map +1 -0
- package/dist/demo.d.ts +69 -0
- package/dist/demo.d.ts.map +1 -0
- package/dist/demo.js +152 -0
- package/dist/demo.js.map +1 -0
- package/dist/dependencies.d.ts +308 -0
- package/dist/dependencies.d.ts.map +1 -0
- package/dist/dependencies.js +133 -0
- package/dist/dependencies.js.map +1 -0
- package/dist/eip712.d.ts +1569 -0
- package/dist/eip712.d.ts.map +1 -0
- package/dist/eip712.js +550 -0
- package/dist/eip712.js.map +1 -0
- package/dist/elided-terms.d.ts +37 -0
- package/dist/elided-terms.d.ts.map +1 -0
- package/dist/elided-terms.js +48 -0
- package/dist/elided-terms.js.map +1 -0
- package/dist/erc8183.d.ts +78 -0
- package/dist/erc8183.d.ts.map +1 -0
- package/dist/erc8183.js +88 -0
- package/dist/erc8183.js.map +1 -0
- package/dist/facts.d.ts +70 -0
- package/dist/facts.d.ts.map +1 -0
- package/dist/facts.js +33 -0
- package/dist/facts.js.map +1 -0
- package/dist/gpu-reservation.d.ts +79 -0
- package/dist/gpu-reservation.d.ts.map +1 -0
- package/dist/gpu-reservation.js +135 -0
- package/dist/gpu-reservation.js.map +1 -0
- package/dist/index.d.ts +49 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/job-session.d.ts +981 -0
- package/dist/job-session.d.ts.map +1 -0
- package/dist/job-session.js +170 -0
- package/dist/job-session.js.map +1 -0
- package/dist/job-verification.d.ts +244 -0
- package/dist/job-verification.d.ts.map +1 -0
- package/dist/job-verification.js +89 -0
- package/dist/job-verification.js.map +1 -0
- package/dist/job-wording.d.ts +48 -0
- package/dist/job-wording.d.ts.map +1 -0
- package/dist/job-wording.js +134 -0
- package/dist/job-wording.js.map +1 -0
- package/dist/job.d.ts +117 -0
- package/dist/job.d.ts.map +1 -0
- package/dist/job.js +166 -0
- package/dist/job.js.map +1 -0
- package/dist/json-fields.d.ts +13 -0
- package/dist/json-fields.d.ts.map +1 -0
- package/dist/json-fields.js +30 -0
- package/dist/json-fields.js.map +1 -0
- package/dist/keychain.d.ts +51 -0
- package/dist/keychain.d.ts.map +1 -0
- package/dist/keychain.js +133 -0
- package/dist/keychain.js.map +1 -0
- package/dist/mailbox.d.ts +1650 -0
- package/dist/mailbox.d.ts.map +1 -0
- package/dist/mailbox.js +87 -0
- package/dist/mailbox.js.map +1 -0
- package/dist/mandate.d.ts +53 -0
- package/dist/mandate.d.ts.map +1 -0
- package/dist/mandate.js +97 -0
- package/dist/mandate.js.map +1 -0
- package/dist/networks.d.ts +18 -0
- package/dist/networks.d.ts.map +1 -0
- package/dist/networks.js +24 -0
- package/dist/networks.js.map +1 -0
- package/dist/profiles.d.ts +31 -0
- package/dist/profiles.d.ts.map +1 -0
- package/dist/profiles.js +33 -0
- package/dist/profiles.js.map +1 -0
- package/dist/rail.d.ts +48 -0
- package/dist/rail.d.ts.map +1 -0
- package/dist/rail.js +28 -0
- package/dist/rail.js.map +1 -0
- package/dist/reasons.d.ts +173 -0
- package/dist/reasons.d.ts.map +1 -0
- package/dist/reasons.js +174 -0
- package/dist/reasons.js.map +1 -0
- package/dist/reference-link.d.ts +23 -0
- package/dist/reference-link.d.ts.map +1 -0
- package/dist/reference-link.js +56 -0
- package/dist/reference-link.js.map +1 -0
- package/dist/references.d.ts +125 -0
- package/dist/references.d.ts.map +1 -0
- package/dist/references.js +108 -0
- package/dist/references.js.map +1 -0
- package/dist/replay.d.ts +30 -0
- package/dist/replay.d.ts.map +1 -0
- package/dist/replay.js +27 -0
- package/dist/replay.js.map +1 -0
- package/dist/sandbox-registration.d.ts +200 -0
- package/dist/sandbox-registration.d.ts.map +1 -0
- package/dist/sandbox-registration.js +172 -0
- package/dist/sandbox-registration.js.map +1 -0
- package/dist/sandbox.d.ts +3478 -0
- package/dist/sandbox.d.ts.map +1 -0
- package/dist/sandbox.js +316 -0
- package/dist/sandbox.js.map +1 -0
- package/dist/session-fields.d.ts +203 -0
- package/dist/session-fields.d.ts.map +1 -0
- package/dist/session-fields.js +43 -0
- package/dist/session-fields.js.map +1 -0
- package/dist/supplier-adapter.d.ts +65 -0
- package/dist/supplier-adapter.d.ts.map +1 -0
- package/dist/supplier-adapter.js +91 -0
- package/dist/supplier-adapter.js.map +1 -0
- package/dist/terms-v1.d.ts +74 -0
- package/dist/terms-v1.d.ts.map +1 -0
- package/dist/terms-v1.js +156 -0
- package/dist/terms-v1.js.map +1 -0
- package/dist/terms.d.ts +341 -0
- package/dist/terms.d.ts.map +1 -0
- package/dist/terms.js +715 -0
- package/dist/terms.js.map +1 -0
- package/dist/test-customer.d.ts +1525 -0
- package/dist/test-customer.d.ts.map +1 -0
- package/dist/test-customer.js +260 -0
- package/dist/test-customer.js.map +1 -0
- package/dist/types.d.ts +272 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +32 -0
- package/dist/types.js.map +1 -0
- package/dist/version.d.ts +51 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +66 -0
- package/dist/version.js.map +1 -0
- package/dist/wire.d.ts +2698 -0
- package/dist/wire.d.ts.map +1 -0
- package/dist/wire.js +678 -0
- package/dist/wire.js.map +1 -0
- package/package.json +37 -3
- package/vectors/api-request-v1.json +53 -0
- package/vectors/contest-statement-v1.json +42 -0
- package/vectors/terms-v2.json +398 -0
- package/vectors/terms-v3.json +2103 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-session.d.ts","sourceRoot":"","sources":["../src/agent-session.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,MAAM,CAAA;AAC/B,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,KAAK,aAAa,EAAqB,MAAM,qBAAqB,CAAA;AAC3E,OAAO,EAAE,KAAK,WAAW,EAAmB,MAAM,mBAAmB,CAAA;AACrE,OAAO,EAAE,KAAK,QAAQ,EAA4B,KAAK,UAAU,EAAE,MAAM,YAAY,CAAA;AACrF,OAAO,EAAE,KAAK,eAAe,EAAuB,MAAM,uBAAuB,CAAA;AAEjF,OAAO,EAAe,KAAK,SAAS,EAAE,MAAM,WAAW,CAAA;AACvD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA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;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;mBA0E9B,CAAA;AAEV,sCAAsC;AACtC,eAAO,MAAM,yBAAyB,qEAMmB,CAAA"}
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { computeOffersJson } from './compute-offers.js';
|
|
3
|
+
import { pinnedBlockJson } from './dependencies.js';
|
|
4
|
+
import { factBodyShape, VERIFIERS } from './facts.js';
|
|
5
|
+
import { jobVerificationJson } from './job-verification.js';
|
|
6
|
+
import { bytes32, u256 } from './json-fields.js';
|
|
7
|
+
import { RAIL_STATES } from './rail.js';
|
|
8
|
+
import { commitmentReferenceJson } from './references.js';
|
|
9
|
+
import { agentSourceJson, REFUSAL_LAYERS, reasonCodeJson, runJson, } from './session-fields.js';
|
|
10
|
+
import { orderIdJson } from './test-customer.js';
|
|
11
|
+
/**
|
|
12
|
+
* The events of the developer's agent session, beside a job's own events, so the terminal and the
|
|
13
|
+
* screen draw the same lines from the same events. Speech, tool calls with their results, facts and
|
|
14
|
+
* receipts are kinds of their own, so speech is never drawn as evidence and evidence never as
|
|
15
|
+
* speech. Each names its run, and the order when it belongs to a job. Each job is its own run, and
|
|
16
|
+
* a turn the developer starts is its own run with no order.
|
|
17
|
+
*
|
|
18
|
+
* A signed event is an attributed statement. The agent key's signature says who posted it, never
|
|
19
|
+
* that what it says is true. So each fact and receipt names who verified it, the commitment on the
|
|
20
|
+
* chain and the block, and a reader checks from the chain what it can. The developer's own
|
|
21
|
+
* messages stay in their terminal and are never posted as events, but the agent's replies can
|
|
22
|
+
* repeat them, since `said` carries the model's words.
|
|
23
|
+
*/
|
|
24
|
+
/** The tools the agent session gives a model, and nothing else. */
|
|
25
|
+
export const JOB_TOOLS = [
|
|
26
|
+
'read_order',
|
|
27
|
+
'compute_offers',
|
|
28
|
+
'book_compute',
|
|
29
|
+
'cancel_compute',
|
|
30
|
+
'submit_quote',
|
|
31
|
+
'authority',
|
|
32
|
+
'commitments',
|
|
33
|
+
];
|
|
34
|
+
/**
|
|
35
|
+
* The form a model's name takes wherever it's drawn: a letter or a digit, then up to 63 more
|
|
36
|
+
* letters, digits, spaces and `._:'/@+-`, as `speakerName` makes one from any model id. The
|
|
37
|
+
* terminal draws a speaker's name by the same rule.
|
|
38
|
+
*/
|
|
39
|
+
export const SPEAKER_NAME = /^[A-Za-z0-9][A-Za-z0-9 ._:'/@+-]{0,63}$/;
|
|
40
|
+
/**
|
|
41
|
+
* A model id in the form `SPEAKER_NAME` takes: any other character replaced, anything before its
|
|
42
|
+
* first letter or digit dropped, and cut to 64.
|
|
43
|
+
*/
|
|
44
|
+
export function speakerName(model) {
|
|
45
|
+
const named = model
|
|
46
|
+
.replace(/[^A-Za-z0-9 ._:'/@+-]/g, '-')
|
|
47
|
+
.replace(/^[^A-Za-z0-9]+/, '')
|
|
48
|
+
.slice(0, 64)
|
|
49
|
+
.trimEnd();
|
|
50
|
+
return named === '' ? 'model' : named;
|
|
51
|
+
}
|
|
52
|
+
/** The model id scripted runs speak as, so every line they draw says it was scripted. */
|
|
53
|
+
export const SCRIPTED_MODEL = 'scripted';
|
|
54
|
+
/**
|
|
55
|
+
* Why a result can't be the one a call got, or null when it can. A call and its result name the
|
|
56
|
+
* same run, call number and tool, and a result shown again pairs with the call that first made it,
|
|
57
|
+
* never with this one. A booking call whose input wasn't in the tool's form names no offer, so its
|
|
58
|
+
* result is a refusal by `service`, and a reader refuses a pair that shows one booked.
|
|
59
|
+
*/
|
|
60
|
+
export function toolResultProblem(call, result) {
|
|
61
|
+
if (call.run !== result.run || call.call !== result.call || call.tool !== result.tool) {
|
|
62
|
+
return 'message.invalid';
|
|
63
|
+
}
|
|
64
|
+
if (result.recalled !== null)
|
|
65
|
+
return 'message.invalid';
|
|
66
|
+
if (call.tool === 'book_compute' && call.offer === null) {
|
|
67
|
+
return !result.ok && result.layer === 'service' ? null : 'message.invalid';
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
/** The longest speech one event carries. A longer answer goes in several. */
|
|
72
|
+
export const SAID_TEXT_MAX = 4000;
|
|
73
|
+
/** The longest summary a tool call or result carries. */
|
|
74
|
+
export const SUMMARY_MAX = 500;
|
|
75
|
+
const base = { run: runJson, source: agentSourceJson, orderId: orderIdJson.nullable() };
|
|
76
|
+
const call = z.number().int().positive().max(10_000);
|
|
77
|
+
const summary = z.string().min(1).max(SUMMARY_MAX);
|
|
78
|
+
const otherTools = JOB_TOOLS.filter((t) => t !== 'book_compute');
|
|
79
|
+
const limitsJson = z.strictObject({
|
|
80
|
+
budgetPerGpuHour: u256,
|
|
81
|
+
computeSwitch: z.enum(['on', 'off']),
|
|
82
|
+
capPerCommitment: u256,
|
|
83
|
+
capOpenReserves: u256,
|
|
84
|
+
openReservesLeft: u256,
|
|
85
|
+
});
|
|
86
|
+
const toolResultFields = {
|
|
87
|
+
...base,
|
|
88
|
+
type: z.literal('tool_result'),
|
|
89
|
+
call,
|
|
90
|
+
tool: z.enum(JOB_TOOLS),
|
|
91
|
+
summary,
|
|
92
|
+
recalled: z.strictObject({ run: runJson, call }).nullable(),
|
|
93
|
+
listing: computeOffersJson.nullable(),
|
|
94
|
+
limits: limitsJson.nullable(),
|
|
95
|
+
};
|
|
96
|
+
/** A listing and its limits come with a successful `compute_offers` result, and with no other. */
|
|
97
|
+
const listingOnly = (r) => (r.tool === 'compute_offers' && r.ok) === (r.listing !== null) &&
|
|
98
|
+
(r.listing !== null) === (r.limits !== null);
|
|
99
|
+
/** Each agent session event's JSON form, which the session's event schema takes beside its own. */
|
|
100
|
+
export const AGENT_SESSION_EVENT_SCHEMAS = [
|
|
101
|
+
z.strictObject({
|
|
102
|
+
...base,
|
|
103
|
+
type: z.literal('said'),
|
|
104
|
+
speaker: z.discriminatedUnion('kind', [
|
|
105
|
+
z.strictObject({ kind: z.literal('model'), model: z.string().regex(SPEAKER_NAME) }),
|
|
106
|
+
z.strictObject({ kind: z.literal('customer'), index: z.number().int().min(0).max(31) }),
|
|
107
|
+
]),
|
|
108
|
+
text: z.string().min(1).max(SAID_TEXT_MAX),
|
|
109
|
+
}),
|
|
110
|
+
z.union([
|
|
111
|
+
z.strictObject({
|
|
112
|
+
...base,
|
|
113
|
+
type: z.literal('tool_call'),
|
|
114
|
+
call,
|
|
115
|
+
tool: z.literal('book_compute'),
|
|
116
|
+
summary,
|
|
117
|
+
offer: z.string().min(1).max(64),
|
|
118
|
+
quantity: z.number().int().positive().max(1024),
|
|
119
|
+
}),
|
|
120
|
+
z.strictObject({
|
|
121
|
+
...base,
|
|
122
|
+
type: z.literal('tool_call'),
|
|
123
|
+
call,
|
|
124
|
+
tool: z.literal('book_compute'),
|
|
125
|
+
summary,
|
|
126
|
+
offer: z.null(),
|
|
127
|
+
quantity: z.null(),
|
|
128
|
+
}),
|
|
129
|
+
z.strictObject({
|
|
130
|
+
...base,
|
|
131
|
+
type: z.literal('tool_call'),
|
|
132
|
+
call,
|
|
133
|
+
tool: z.enum(otherTools),
|
|
134
|
+
summary,
|
|
135
|
+
}),
|
|
136
|
+
]),
|
|
137
|
+
z.union([
|
|
138
|
+
z.strictObject({ ...toolResultFields, ok: z.literal(true) }).refine(listingOnly),
|
|
139
|
+
z
|
|
140
|
+
.strictObject({
|
|
141
|
+
...toolResultFields,
|
|
142
|
+
ok: z.literal(false),
|
|
143
|
+
code: reasonCodeJson,
|
|
144
|
+
layer: z.enum(REFUSAL_LAYERS),
|
|
145
|
+
})
|
|
146
|
+
.refine(listingOnly),
|
|
147
|
+
]),
|
|
148
|
+
z.strictObject({ ...base, type: z.literal('fact'), ...factBodyShape }),
|
|
149
|
+
z
|
|
150
|
+
.strictObject({
|
|
151
|
+
...base,
|
|
152
|
+
type: z.literal('commitment'),
|
|
153
|
+
role: z.enum(['compute', 'job']),
|
|
154
|
+
reference: commitmentReferenceJson,
|
|
155
|
+
amount: u256,
|
|
156
|
+
state: z.enum(RAIL_STATES),
|
|
157
|
+
verifiedBy: z.enum(VERIFIERS),
|
|
158
|
+
block: pinnedBlockJson,
|
|
159
|
+
transactionHash: bytes32.nullable(),
|
|
160
|
+
verification: jobVerificationJson.nullable(),
|
|
161
|
+
})
|
|
162
|
+
.refine((c) => (c.role === 'job') === (c.verification !== null), 'a job, and only a job, carries its verification')
|
|
163
|
+
.refine((c) => c.verification === null ||
|
|
164
|
+
(c.verification.commitment.lock.toLowerCase() === c.reference.lock.toLowerCase() &&
|
|
165
|
+
c.verification.commitment.commitmentId.toLowerCase() ===
|
|
166
|
+
c.reference.commitmentId.toLowerCase()), 'the verification names the receipt’s commitment'),
|
|
167
|
+
];
|
|
168
|
+
/** Every agent session event type. */
|
|
169
|
+
export const AGENT_SESSION_EVENT_TYPES = [
|
|
170
|
+
'said',
|
|
171
|
+
'tool_call',
|
|
172
|
+
'tool_result',
|
|
173
|
+
'fact',
|
|
174
|
+
'commitment',
|
|
175
|
+
];
|
|
176
|
+
//# sourceMappingURL=agent-session.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-session.js","sourceRoot":"","sources":["../src/agent-session.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAsB,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AAC3E,OAAO,EAAoB,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACrE,OAAO,EAAiB,aAAa,EAAE,SAAS,EAAmB,MAAM,YAAY,CAAA;AACrF,OAAO,EAAwB,mBAAmB,EAAE,MAAM,uBAAuB,CAA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YAAY,CAAC,WAAW,EAAE,CAAC,EAC7C,iDAAiD,CAClD;CACK,CAAA;AAEV,sCAAsC;AACtC,MAAM,CAAC,MAAM,yBAAyB,GAAG;IACvC,MAAM;IACN,WAAW;IACX,aAAa;IACb,MAAM;IACN,YAAY;CAC2C,CAAA"}
|
package/dist/ap2.d.ts
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import type { Mandate } from './types.js';
|
|
3
|
+
import { type Parsed } from './wire.js';
|
|
4
|
+
/**
|
|
5
|
+
* AP2's intent mandate, version 0.1, as `toAp2IntentMandate` writes it. Version 0.2 replaced the
|
|
6
|
+
* intent mandate with checkout and payment mandates, which this package doesn't map yet.
|
|
7
|
+
*/
|
|
8
|
+
export interface Ap2IntentMandate {
|
|
9
|
+
user_cart_confirmation_required: boolean;
|
|
10
|
+
natural_language_description: string;
|
|
11
|
+
merchants: string[] | null;
|
|
12
|
+
skus: string[] | null;
|
|
13
|
+
requires_refundability: boolean;
|
|
14
|
+
/** ISO 8601 */
|
|
15
|
+
intent_expiry: string;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The parts of an Avow mandate AP2 0.1 has no field for, carried beside the intent mandate. Actions
|
|
19
|
+
* and currency use the mandate's own schemas, and every other value stays a string here and parses
|
|
20
|
+
* through the mandate's schema once it's mapped. Caps are micros and payment limits are the token's
|
|
21
|
+
* base units, as decimal strings.
|
|
22
|
+
*/
|
|
23
|
+
declare const avowExtensionJson: z.ZodObject<{
|
|
24
|
+
mandate_id: z.ZodString;
|
|
25
|
+
business_id: z.ZodString;
|
|
26
|
+
agent_id: z.ZodString;
|
|
27
|
+
account: z.ZodString;
|
|
28
|
+
agent_key: z.ZodString;
|
|
29
|
+
actions: z.ZodArray<z.ZodEnum<{
|
|
30
|
+
cancel: "cancel";
|
|
31
|
+
pay: "pay";
|
|
32
|
+
reserve: "reserve";
|
|
33
|
+
start: "start";
|
|
34
|
+
}>>;
|
|
35
|
+
categories: z.ZodArray<z.ZodString>;
|
|
36
|
+
currency: z.ZodEnum<{
|
|
37
|
+
USD: "USD";
|
|
38
|
+
}>;
|
|
39
|
+
cap_per_commitment: z.ZodString;
|
|
40
|
+
cap_open_reserves: z.ZodString;
|
|
41
|
+
payment_limits: z.ZodArray<z.ZodObject<{
|
|
42
|
+
token: z.ZodString;
|
|
43
|
+
amount: z.ZodString;
|
|
44
|
+
period_seconds: z.ZodString;
|
|
45
|
+
}, z.core.$strict>>;
|
|
46
|
+
approval_rule: z.ZodUnion<readonly [z.ZodEnum<{
|
|
47
|
+
always: "always";
|
|
48
|
+
never: "never";
|
|
49
|
+
}>, z.ZodObject<{
|
|
50
|
+
above: z.ZodString;
|
|
51
|
+
}, z.core.$strict>]>;
|
|
52
|
+
nonce: z.ZodString;
|
|
53
|
+
}, z.core.$strict>;
|
|
54
|
+
export type AvowMandateExtension = z.input<typeof avowExtensionJson>;
|
|
55
|
+
/**
|
|
56
|
+
* Maps an Avow mandate onto AP2 0.1. Supplier business ids become AP2 merchants and the expiry
|
|
57
|
+
* becomes the intent expiry. Every pair requires cart confirmation, whatever the approval rule.
|
|
58
|
+
* Caps, limits, categories and the approval rule have no AP2 field, so they travel in the `avow`
|
|
59
|
+
* extension. A mandate that expires after the year 9999, which an ISO 8601 time can't write, is
|
|
60
|
+
* refused as `message.validity_too_long`.
|
|
61
|
+
*/
|
|
62
|
+
export declare function toAp2IntentMandate(m: Mandate, description: string): Parsed<{
|
|
63
|
+
intent_mandate: Ap2IntentMandate;
|
|
64
|
+
avow: AvowMandateExtension;
|
|
65
|
+
}>;
|
|
66
|
+
/**
|
|
67
|
+
* Reads an Avow mandate back from a JSON value holding an AP2 0.1 intent mandate and the extension
|
|
68
|
+
* it travels with, as an adapter that accepts AP2 would. The suppliers come from AP2's merchants
|
|
69
|
+
* and the expiry from its intent expiry, and everything else from the extension, through the same
|
|
70
|
+
* parser as any mandate. A shape either schema doesn't define, a time without an offset or with a
|
|
71
|
+
* part of a second, and a value no mandate can hold are refused as `message.invalid`. A pair whose
|
|
72
|
+
* intent mandate waives cart confirmation, names SKUs, requires refundability in either spelling
|
|
73
|
+
* or allows no merchant says something the mandate doesn't, and is refused as
|
|
74
|
+
* `mandate.ap2_mismatch`. The pair carries no signature, so the mandate it returns binds nobody
|
|
75
|
+
* until the administrator's signature over it checks out.
|
|
76
|
+
*/
|
|
77
|
+
export declare function fromAp2IntentMandate(json: unknown): Parsed<Mandate>;
|
|
78
|
+
export {};
|
|
79
|
+
//# sourceMappingURL=ap2.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ap2.d.ts","sourceRoot":"","sources":["../src/ap2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,YAAY,CAAA;AACzC,OAAO,EAAe,KAAK,MAAM,EAAE,MAAM,WAAW,CAAA;AAEpD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B,+BAA+B,EAAE,OAAO,CAAA;IACxC,4BAA4B,EAAE,MAAM,CAAA;IACpC,SAAS,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;IAC1B,IAAI,EAAE,MAAM,EAAE,GAAG,IAAI,CAAA;IACrB,sBAAsB,EAAE,OAAO,CAAA;IAC/B,eAAe;IACf,aAAa,EAAE,MAAM,CAAA;CACtB;AA6BD;;;;;GAKG;AACH,QAAA,MAAM,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;kBAiBrB,CAAA;AACF,MAAM,MAAM,oBAAoB,GAAG,CAAC,CAAC,KAAK,CAAC,OAAO,iBAAiB,CAAC,CAAA;AAIpE;;;;;;GAMG;AACH,wBAAgB,kBAAkB,CAChC,CAAC,EAAE,OAAO,EACV,WAAW,EAAE,MAAM,GAClB,MAAM,CAAC;IAAE,cAAc,EAAE,gBAAgB,CAAC;IAAC,IAAI,EAAE,oBAAoB,CAAA;CAAE,CAAC,CAmC1E;AASD;;;;;;;;;;GAUG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC,CAoCnE"}
|
package/dist/ap2.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { mandateJson } from './wire.js';
|
|
3
|
+
/**
|
|
4
|
+
* The cart confirmation flag every pair carries. AP2 0.1 requires it on any intent mandate the
|
|
5
|
+
* user hasn't signed, and its intent mandate has no field for a signature, so a pair never carries
|
|
6
|
+
* one. Asking the user to confirm every cart is at least as strict as any approval rule.
|
|
7
|
+
*/
|
|
8
|
+
const CART_CONFIRMATION_REQUIRED = true;
|
|
9
|
+
/** The last moment an ISO 8601 time with a four-digit year can write, 9999-12-31T23:59:59Z. */
|
|
10
|
+
const LAST_ISO_SECOND = 253402300799n;
|
|
11
|
+
/**
|
|
12
|
+
* AP2 0.1's intent mandate as a reader takes it. A field AP2 leaves optional reads as AP2 defines
|
|
13
|
+
* it when it's left out. Merchants and SKUs read as null, which allows any, refundability as not
|
|
14
|
+
* required, and the confirmation flag as true. AP2's reference implementation spells refundability
|
|
15
|
+
* `requires_refundability`, and the example on its A2A extension spells it
|
|
16
|
+
* `required_refundability`, so the reader takes both.
|
|
17
|
+
*/
|
|
18
|
+
const intentMandateJson = z.strictObject({
|
|
19
|
+
user_cart_confirmation_required: z.boolean().default(true),
|
|
20
|
+
natural_language_description: z.string(),
|
|
21
|
+
merchants: z.array(z.string()).nullable().default(null),
|
|
22
|
+
skus: z.array(z.string()).nullable().default(null),
|
|
23
|
+
requires_refundability: z.boolean().nullable().default(null),
|
|
24
|
+
required_refundability: z.boolean().nullable().default(null),
|
|
25
|
+
intent_expiry: z.iso.datetime({ offset: true }),
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* The parts of an Avow mandate AP2 0.1 has no field for, carried beside the intent mandate. Actions
|
|
29
|
+
* and currency use the mandate's own schemas, and every other value stays a string here and parses
|
|
30
|
+
* through the mandate's schema once it's mapped. Caps are micros and payment limits are the token's
|
|
31
|
+
* base units, as decimal strings.
|
|
32
|
+
*/
|
|
33
|
+
const avowExtensionJson = z.strictObject({
|
|
34
|
+
mandate_id: z.string(),
|
|
35
|
+
business_id: z.string(),
|
|
36
|
+
agent_id: z.string(),
|
|
37
|
+
/** The business's account, whose root key signs the Avow mandate. */
|
|
38
|
+
account: z.string(),
|
|
39
|
+
agent_key: z.string(),
|
|
40
|
+
actions: mandateJson.shape.actions,
|
|
41
|
+
categories: z.array(z.string()),
|
|
42
|
+
currency: mandateJson.shape.currency,
|
|
43
|
+
cap_per_commitment: z.string(),
|
|
44
|
+
cap_open_reserves: z.string(),
|
|
45
|
+
payment_limits: z.array(z.strictObject({ token: z.string(), amount: z.string(), period_seconds: z.string() })),
|
|
46
|
+
approval_rule: z.union([z.enum(['never', 'always']), z.strictObject({ above: z.string() })]),
|
|
47
|
+
nonce: z.string(),
|
|
48
|
+
});
|
|
49
|
+
const ap2PairJson = z.strictObject({ intent_mandate: intentMandateJson, avow: avowExtensionJson });
|
|
50
|
+
/**
|
|
51
|
+
* Maps an Avow mandate onto AP2 0.1. Supplier business ids become AP2 merchants and the expiry
|
|
52
|
+
* becomes the intent expiry. Every pair requires cart confirmation, whatever the approval rule.
|
|
53
|
+
* Caps, limits, categories and the approval rule have no AP2 field, so they travel in the `avow`
|
|
54
|
+
* extension. A mandate that expires after the year 9999, which an ISO 8601 time can't write, is
|
|
55
|
+
* refused as `message.validity_too_long`.
|
|
56
|
+
*/
|
|
57
|
+
export function toAp2IntentMandate(m, description) {
|
|
58
|
+
if (m.expiry > LAST_ISO_SECOND)
|
|
59
|
+
return { ok: false, reason: 'message.validity_too_long' };
|
|
60
|
+
const rule = m.approvalRule;
|
|
61
|
+
return {
|
|
62
|
+
ok: true,
|
|
63
|
+
value: {
|
|
64
|
+
intent_mandate: {
|
|
65
|
+
user_cart_confirmation_required: CART_CONFIRMATION_REQUIRED,
|
|
66
|
+
natural_language_description: description,
|
|
67
|
+
merchants: m.suppliers.length > 0 ? [...m.suppliers] : null,
|
|
68
|
+
skus: null,
|
|
69
|
+
requires_refundability: false,
|
|
70
|
+
intent_expiry: new Date(Number(m.expiry) * 1000).toISOString(),
|
|
71
|
+
},
|
|
72
|
+
avow: {
|
|
73
|
+
mandate_id: m.mandateId,
|
|
74
|
+
business_id: m.businessId,
|
|
75
|
+
agent_id: m.agentId,
|
|
76
|
+
account: m.account,
|
|
77
|
+
agent_key: m.agentKey,
|
|
78
|
+
actions: [...m.actions],
|
|
79
|
+
categories: [...m.categories],
|
|
80
|
+
currency: m.currency,
|
|
81
|
+
cap_per_commitment: m.capPerCommitment.toString(),
|
|
82
|
+
cap_open_reserves: m.capOpenReserves.toString(),
|
|
83
|
+
payment_limits: m.paymentLimits.map((l) => ({
|
|
84
|
+
token: l.token,
|
|
85
|
+
amount: l.amount.toString(),
|
|
86
|
+
period_seconds: l.period.toString(),
|
|
87
|
+
})),
|
|
88
|
+
approval_rule: rule.kind === 'above' ? { above: rule.amount.toString() } : rule.kind,
|
|
89
|
+
nonce: m.nonce.toString(),
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** An ISO 8601 time as Unix seconds in a decimal string, or null when it has a part of a second. */
|
|
95
|
+
function wholeSeconds(iso) {
|
|
96
|
+
if (/\.\d*[1-9]/.test(iso))
|
|
97
|
+
return null;
|
|
98
|
+
const ms = Date.parse(iso);
|
|
99
|
+
return Number.isFinite(ms) ? String(ms / 1000) : null;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Reads an Avow mandate back from a JSON value holding an AP2 0.1 intent mandate and the extension
|
|
103
|
+
* it travels with, as an adapter that accepts AP2 would. The suppliers come from AP2's merchants
|
|
104
|
+
* and the expiry from its intent expiry, and everything else from the extension, through the same
|
|
105
|
+
* parser as any mandate. A shape either schema doesn't define, a time without an offset or with a
|
|
106
|
+
* part of a second, and a value no mandate can hold are refused as `message.invalid`. A pair whose
|
|
107
|
+
* intent mandate waives cart confirmation, names SKUs, requires refundability in either spelling
|
|
108
|
+
* or allows no merchant says something the mandate doesn't, and is refused as
|
|
109
|
+
* `mandate.ap2_mismatch`. The pair carries no signature, so the mandate it returns binds nobody
|
|
110
|
+
* until the administrator's signature over it checks out.
|
|
111
|
+
*/
|
|
112
|
+
export function fromAp2IntentMandate(json) {
|
|
113
|
+
const pair = ap2PairJson.safeParse(json);
|
|
114
|
+
if (!pair.success)
|
|
115
|
+
return { ok: false, reason: 'message.invalid' };
|
|
116
|
+
const { intent_mandate: intent, avow } = pair.data;
|
|
117
|
+
const expiry = wholeSeconds(intent.intent_expiry);
|
|
118
|
+
if (expiry === null)
|
|
119
|
+
return { ok: false, reason: 'message.invalid' };
|
|
120
|
+
const rule = avow.approval_rule;
|
|
121
|
+
const parsed = mandateJson.safeParse({
|
|
122
|
+
mandateId: avow.mandate_id,
|
|
123
|
+
businessId: avow.business_id,
|
|
124
|
+
agentId: avow.agent_id,
|
|
125
|
+
account: avow.account,
|
|
126
|
+
agentKey: avow.agent_key,
|
|
127
|
+
actions: avow.actions,
|
|
128
|
+
currency: avow.currency,
|
|
129
|
+
capPerCommitment: avow.cap_per_commitment,
|
|
130
|
+
capOpenReserves: avow.cap_open_reserves,
|
|
131
|
+
suppliers: intent.merchants ?? [],
|
|
132
|
+
categories: avow.categories,
|
|
133
|
+
paymentLimits: avow.payment_limits.map((l) => ({
|
|
134
|
+
token: l.token,
|
|
135
|
+
amount: l.amount,
|
|
136
|
+
period: l.period_seconds,
|
|
137
|
+
})),
|
|
138
|
+
approvalRule: typeof rule === 'string' ? { kind: rule } : { kind: 'above', amount: rule.above },
|
|
139
|
+
expiry,
|
|
140
|
+
nonce: avow.nonce,
|
|
141
|
+
});
|
|
142
|
+
if (!parsed.success)
|
|
143
|
+
return { ok: false, reason: 'message.invalid' };
|
|
144
|
+
const agrees = intent.user_cart_confirmation_required === CART_CONFIRMATION_REQUIRED &&
|
|
145
|
+
intent.skus === null &&
|
|
146
|
+
!intent.requires_refundability &&
|
|
147
|
+
!intent.required_refundability &&
|
|
148
|
+
intent.merchants?.length !== 0;
|
|
149
|
+
return agrees ? { ok: true, value: parsed.data } : { ok: false, reason: 'mandate.ap2_mismatch' };
|
|
150
|
+
}
|
|
151
|
+
//# sourceMappingURL=ap2.js.map
|
package/dist/ap2.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ap2.js","sourceRoot":"","sources":["../src/ap2.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,WAAW,EAAe,MAAM,WAAW,CAAA;AAgBpD;;;;GAIG;AACH,MAAM,0BAA0B,GAAG,IAAI,CAAA;AAEvC,+FAA+F;AAC/F,MAAM,eAAe,GAAG,aAAgB,CAAA;AAExC;;;;;;GAMG;AACH,MAAM,iBAAiB,GAAG,CAAC,CAAC,YAAY,CAAC;IACvC,+BAA+B,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IAC1D,4BAA4B,EAAE,CAAC,CAAC,MAAM,EAAE;IACxC,SAAS,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IACvD,IAAI,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IAClD,sBAAsB,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IAC5D,sBAAsB,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC,QAAQ,EAAE,CAAC,OAAO,CAAC,IAAI,CAAC;IAC5D,aAAa,EAAE,CAAC,CA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,sBAAsB;QAC9B,CAAC,MAAM,CAAC,sBAAsB;QAC9B,MAAM,CAAC,SAAS,EAAE,MAAM,KAAK,CAAC,CAAA;IAChC,OAAO,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,sBAAsB,EAAE,CAAA;AAClG,CAAC"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
import { type Address, type Hex } from 'viem';
|
|
2
|
+
import { type ApiRequest, type AvowDomain, type Signed } from './eip712.js';
|
|
3
|
+
import type { ReasonCode } from './reasons.js';
|
|
4
|
+
/** The scheme name a signed request puts in its Authorization header. */
|
|
5
|
+
export declare const API_AUTH_SCHEME = "Avow";
|
|
6
|
+
/** The longest a signed request may stay valid, in seconds from when Avow receives it. */
|
|
7
|
+
export declare const API_REQUEST_MAX_VALIDITY = 300n;
|
|
8
|
+
/** The largest body a signed request may carry, in bytes. */
|
|
9
|
+
export declare const API_BODY_MAX_BYTES = 65536;
|
|
10
|
+
/** The methods a signed request may use. A GET carries no body. */
|
|
11
|
+
export declare const API_METHODS: readonly ['GET', 'POST'];
|
|
12
|
+
/**
|
|
13
|
+
* The request targets a signed request may name: `/v1` and one or more segments of lower-case
|
|
14
|
+
* letters, digits, `-` and `_`. No query, fragment, trailing slash, dot segment or
|
|
15
|
+
* percent-encoding, so the target a client signs is the target a server reads and routes on,
|
|
16
|
+
* whatever either side's URL parser does.
|
|
17
|
+
*/
|
|
18
|
+
export declare const API_PATH_PATTERN: RegExp;
|
|
19
|
+
/**
|
|
20
|
+
* Whether a string is a deployment's audience in its one canonical form: an http or https origin
|
|
21
|
+
* in lower case, with a port only when it isn't the scheme's default, and no path, query or
|
|
22
|
+
* trailing slash. `https://api.testnet.avow.finance` is one, and `https://api.testnet.avow.finance:443`
|
|
23
|
+
* isn't, so one deployment never has two audiences.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isApiAudience(audience: string): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* A key that signs requests to Avow's API with its bare signature, the way the lock reads a
|
|
28
|
+
* signing key's: secp256k1, P-256 or WebAuthn, outside any keychain envelope.
|
|
29
|
+
*/
|
|
30
|
+
export interface RequestKey {
|
|
31
|
+
/** The key's own address, which Avow knows as an enrolled agent key or a supplier's signing key. */
|
|
32
|
+
readonly address: Address;
|
|
33
|
+
signDigest(digest: Hex): Promise<Hex>;
|
|
34
|
+
}
|
|
35
|
+
/** keccak256 of a request body's exact bytes. A request with no body hashes no bytes. */
|
|
36
|
+
export declare function apiBodyHash(body: Uint8Array): Hex;
|
|
37
|
+
/** A fresh request id, 32 random bytes. */
|
|
38
|
+
export declare function newRequestId(): Hex;
|
|
39
|
+
/**
|
|
40
|
+
* A request body as a client sends it: the value's canonical JSON, with bigints as decimal
|
|
41
|
+
* strings, in UTF-8. Canonical JSON has one encoding for each value, so the bytes a client signs
|
|
42
|
+
* are the only bytes Avow accepts for that value.
|
|
43
|
+
*/
|
|
44
|
+
export declare function encodeApiBody(value: unknown): Uint8Array;
|
|
45
|
+
/**
|
|
46
|
+
* Reads a body Avow received. It has to be well-formed UTF-8 holding canonical JSON, so a body with
|
|
47
|
+
* a repeated key, extra whitespace or keys out of order is refused rather than read one way here
|
|
48
|
+
* and another way by whoever reads the stored copy. No bytes read as no body.
|
|
49
|
+
*/
|
|
50
|
+
export declare function readApiBody(body: Uint8Array): {
|
|
51
|
+
ok: true;
|
|
52
|
+
value: unknown;
|
|
53
|
+
} | {
|
|
54
|
+
ok: false;
|
|
55
|
+
reason: ReasonCode;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Why a request can't be signed or checked as it stands, or null when it can. Both the client and
|
|
59
|
+
* the API run this, so neither signs or verifies a request the other would read differently.
|
|
60
|
+
* `path` is the request target exactly as sent on the wire, which a server reads before any
|
|
61
|
+
* routing or decoding. A signed request's target is only ever a path.
|
|
62
|
+
*/
|
|
63
|
+
export declare function apiRequestProblem(r: {
|
|
64
|
+
audience: string;
|
|
65
|
+
method: string;
|
|
66
|
+
path: string;
|
|
67
|
+
body: Uint8Array;
|
|
68
|
+
}): ReasonCode | null;
|
|
69
|
+
/**
|
|
70
|
+
* Why Avow won't start a request with this expiry, or null when it will. A request starts only
|
|
71
|
+
* while its expiry is after Avow's clock and at most `API_REQUEST_MAX_VALIDITY` ahead of it. Avow
|
|
72
|
+
* checks this after looking up the request id, so an identical repeat of a request it already
|
|
73
|
+
* answered gets that answer even once the request has expired.
|
|
74
|
+
*/
|
|
75
|
+
export declare function apiRequestExpiryProblem(expiry: bigint, now: bigint): ReasonCode | null;
|
|
76
|
+
/**
|
|
77
|
+
* What a signed request's Authorization header carries. The API rebuilds the rest of the signed
|
|
78
|
+
* message from the request it received: its own audience, the method, the path and the body's
|
|
79
|
+
* hash.
|
|
80
|
+
*/
|
|
81
|
+
export interface ApiCredentials {
|
|
82
|
+
signer: Address;
|
|
83
|
+
/** The lock whose domain the request was signed under. */
|
|
84
|
+
lock: Address;
|
|
85
|
+
requestId: Hex;
|
|
86
|
+
expiry: bigint;
|
|
87
|
+
signature: Hex;
|
|
88
|
+
}
|
|
89
|
+
export declare function formatApiAuthorization(c: ApiCredentials): string;
|
|
90
|
+
/**
|
|
91
|
+
* Reads a signed request's Authorization header, or returns null when it isn't one. The fields are
|
|
92
|
+
* separated by a comma and one space, each appears once and nothing else may, so two readers can't
|
|
93
|
+
* take different values from one header. Hex comes back in lower case.
|
|
94
|
+
*/
|
|
95
|
+
export declare function parseApiAuthorization(header: string | null | undefined): ApiCredentials | null;
|
|
96
|
+
/** A request whose signature Avow has checked, ready for the request-id lookup. */
|
|
97
|
+
export interface VerifiedApiRequest {
|
|
98
|
+
request: Signed<ApiRequest>;
|
|
99
|
+
/** The EIP-712 digest, which names this exact request. */
|
|
100
|
+
digest: Hex;
|
|
101
|
+
credentials: ApiCredentials;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* The first two steps Avow takes with every request, in order. It checks the request's form and
|
|
105
|
+
* reads the header, then rebuilds the signed message from its own audience and chain, the header's
|
|
106
|
+
* lock, and the method, path and body it received, and checks the signature. A refusal here
|
|
107
|
+
* comes before Avow looks anything up or records anything. What comes next, in order, is the
|
|
108
|
+
* request-id lookup, the expiry check, the route's own checks, and one transaction that records
|
|
109
|
+
* the request with what it did.
|
|
110
|
+
*/
|
|
111
|
+
export declare function verifyApiRequest(r: {
|
|
112
|
+
audience: string;
|
|
113
|
+
chainId: number;
|
|
114
|
+
method: string;
|
|
115
|
+
path: string;
|
|
116
|
+
body: Uint8Array;
|
|
117
|
+
authorization: string | null | undefined;
|
|
118
|
+
}): {
|
|
119
|
+
ok: true;
|
|
120
|
+
value: VerifiedApiRequest;
|
|
121
|
+
} | {
|
|
122
|
+
ok: false;
|
|
123
|
+
reason: ReasonCode;
|
|
124
|
+
};
|
|
125
|
+
/**
|
|
126
|
+
* Signs one request to Avow's API. It returns the Authorization header to send with exactly these
|
|
127
|
+
* body bytes, and the signed request, the record of what the client asked. It throws for a request
|
|
128
|
+
* `apiRequestProblem` refuses, since the API would refuse it too.
|
|
129
|
+
*/
|
|
130
|
+
export declare function signApiRequest(args: {
|
|
131
|
+
key: RequestKey;
|
|
132
|
+
domain: AvowDomain;
|
|
133
|
+
audience: string;
|
|
134
|
+
method: string;
|
|
135
|
+
path: string;
|
|
136
|
+
body: Uint8Array;
|
|
137
|
+
requestId: Hex;
|
|
138
|
+
expiry: bigint;
|
|
139
|
+
}): Promise<{
|
|
140
|
+
authorization: string;
|
|
141
|
+
request: Signed<ApiRequest>;
|
|
142
|
+
digest: Hex;
|
|
143
|
+
}>;
|
|
144
|
+
//# sourceMappingURL=api-request.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api-request.d.ts","sourceRoot":"","sources":["../src/api-request.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,E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