@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.
Files changed (179) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +119 -2
  3. package/dist/agent-connect.d.ts +82 -0
  4. package/dist/agent-connect.d.ts.map +1 -0
  5. package/dist/agent-connect.js +91 -0
  6. package/dist/agent-connect.js.map +1 -0
  7. package/dist/agent-session.d.ts +791 -0
  8. package/dist/agent-session.d.ts.map +1 -0
  9. package/dist/agent-session.js +176 -0
  10. package/dist/agent-session.js.map +1 -0
  11. package/dist/ap2.d.ts +79 -0
  12. package/dist/ap2.d.ts.map +1 -0
  13. package/dist/ap2.js +151 -0
  14. package/dist/ap2.js.map +1 -0
  15. package/dist/api-request.d.ts +144 -0
  16. package/dist/api-request.d.ts.map +1 -0
  17. package/dist/api-request.js +246 -0
  18. package/dist/api-request.js.map +1 -0
  19. package/dist/api.d.ts +288 -0
  20. package/dist/api.d.ts.map +1 -0
  21. package/dist/api.js +332 -0
  22. package/dist/api.js.map +1 -0
  23. package/dist/canonical.d.ts +22 -0
  24. package/dist/canonical.d.ts.map +1 -0
  25. package/dist/canonical.js +66 -0
  26. package/dist/canonical.js.map +1 -0
  27. package/dist/commitment-card.d.ts +78 -0
  28. package/dist/commitment-card.d.ts.map +1 -0
  29. package/dist/commitment-card.js +224 -0
  30. package/dist/commitment-card.js.map +1 -0
  31. package/dist/commitment.d.ts +185 -0
  32. package/dist/commitment.d.ts.map +1 -0
  33. package/dist/commitment.js +148 -0
  34. package/dist/commitment.js.map +1 -0
  35. package/dist/compute-offers.d.ts +128 -0
  36. package/dist/compute-offers.d.ts.map +1 -0
  37. package/dist/compute-offers.js +111 -0
  38. package/dist/compute-offers.js.map +1 -0
  39. package/dist/contest.d.ts +88 -0
  40. package/dist/contest.d.ts.map +1 -0
  41. package/dist/contest.js +131 -0
  42. package/dist/contest.js.map +1 -0
  43. package/dist/demo.d.ts +69 -0
  44. package/dist/demo.d.ts.map +1 -0
  45. package/dist/demo.js +152 -0
  46. package/dist/demo.js.map +1 -0
  47. package/dist/dependencies.d.ts +308 -0
  48. package/dist/dependencies.d.ts.map +1 -0
  49. package/dist/dependencies.js +133 -0
  50. package/dist/dependencies.js.map +1 -0
  51. package/dist/eip712.d.ts +1569 -0
  52. package/dist/eip712.d.ts.map +1 -0
  53. package/dist/eip712.js +550 -0
  54. package/dist/eip712.js.map +1 -0
  55. package/dist/elided-terms.d.ts +37 -0
  56. package/dist/elided-terms.d.ts.map +1 -0
  57. package/dist/elided-terms.js +48 -0
  58. package/dist/elided-terms.js.map +1 -0
  59. package/dist/erc8183.d.ts +78 -0
  60. package/dist/erc8183.d.ts.map +1 -0
  61. package/dist/erc8183.js +88 -0
  62. package/dist/erc8183.js.map +1 -0
  63. package/dist/facts.d.ts +70 -0
  64. package/dist/facts.d.ts.map +1 -0
  65. package/dist/facts.js +33 -0
  66. package/dist/facts.js.map +1 -0
  67. package/dist/gpu-reservation.d.ts +79 -0
  68. package/dist/gpu-reservation.d.ts.map +1 -0
  69. package/dist/gpu-reservation.js +135 -0
  70. package/dist/gpu-reservation.js.map +1 -0
  71. package/dist/index.d.ts +49 -0
  72. package/dist/index.d.ts.map +1 -0
  73. package/dist/index.js +49 -0
  74. package/dist/index.js.map +1 -0
  75. package/dist/job-session.d.ts +981 -0
  76. package/dist/job-session.d.ts.map +1 -0
  77. package/dist/job-session.js +170 -0
  78. package/dist/job-session.js.map +1 -0
  79. package/dist/job-verification.d.ts +244 -0
  80. package/dist/job-verification.d.ts.map +1 -0
  81. package/dist/job-verification.js +89 -0
  82. package/dist/job-verification.js.map +1 -0
  83. package/dist/job-wording.d.ts +48 -0
  84. package/dist/job-wording.d.ts.map +1 -0
  85. package/dist/job-wording.js +134 -0
  86. package/dist/job-wording.js.map +1 -0
  87. package/dist/job.d.ts +117 -0
  88. package/dist/job.d.ts.map +1 -0
  89. package/dist/job.js +166 -0
  90. package/dist/job.js.map +1 -0
  91. package/dist/json-fields.d.ts +13 -0
  92. package/dist/json-fields.d.ts.map +1 -0
  93. package/dist/json-fields.js +30 -0
  94. package/dist/json-fields.js.map +1 -0
  95. package/dist/keychain.d.ts +51 -0
  96. package/dist/keychain.d.ts.map +1 -0
  97. package/dist/keychain.js +133 -0
  98. package/dist/keychain.js.map +1 -0
  99. package/dist/mailbox.d.ts +1650 -0
  100. package/dist/mailbox.d.ts.map +1 -0
  101. package/dist/mailbox.js +87 -0
  102. package/dist/mailbox.js.map +1 -0
  103. package/dist/mandate.d.ts +53 -0
  104. package/dist/mandate.d.ts.map +1 -0
  105. package/dist/mandate.js +97 -0
  106. package/dist/mandate.js.map +1 -0
  107. package/dist/networks.d.ts +18 -0
  108. package/dist/networks.d.ts.map +1 -0
  109. package/dist/networks.js +24 -0
  110. package/dist/networks.js.map +1 -0
  111. package/dist/profiles.d.ts +31 -0
  112. package/dist/profiles.d.ts.map +1 -0
  113. package/dist/profiles.js +33 -0
  114. package/dist/profiles.js.map +1 -0
  115. package/dist/rail.d.ts +48 -0
  116. package/dist/rail.d.ts.map +1 -0
  117. package/dist/rail.js +28 -0
  118. package/dist/rail.js.map +1 -0
  119. package/dist/reasons.d.ts +173 -0
  120. package/dist/reasons.d.ts.map +1 -0
  121. package/dist/reasons.js +174 -0
  122. package/dist/reasons.js.map +1 -0
  123. package/dist/reference-link.d.ts +23 -0
  124. package/dist/reference-link.d.ts.map +1 -0
  125. package/dist/reference-link.js +56 -0
  126. package/dist/reference-link.js.map +1 -0
  127. package/dist/references.d.ts +125 -0
  128. package/dist/references.d.ts.map +1 -0
  129. package/dist/references.js +108 -0
  130. package/dist/references.js.map +1 -0
  131. package/dist/replay.d.ts +30 -0
  132. package/dist/replay.d.ts.map +1 -0
  133. package/dist/replay.js +27 -0
  134. package/dist/replay.js.map +1 -0
  135. package/dist/sandbox-registration.d.ts +200 -0
  136. package/dist/sandbox-registration.d.ts.map +1 -0
  137. package/dist/sandbox-registration.js +172 -0
  138. package/dist/sandbox-registration.js.map +1 -0
  139. package/dist/sandbox.d.ts +3478 -0
  140. package/dist/sandbox.d.ts.map +1 -0
  141. package/dist/sandbox.js +316 -0
  142. package/dist/sandbox.js.map +1 -0
  143. package/dist/session-fields.d.ts +203 -0
  144. package/dist/session-fields.d.ts.map +1 -0
  145. package/dist/session-fields.js +43 -0
  146. package/dist/session-fields.js.map +1 -0
  147. package/dist/supplier-adapter.d.ts +65 -0
  148. package/dist/supplier-adapter.d.ts.map +1 -0
  149. package/dist/supplier-adapter.js +91 -0
  150. package/dist/supplier-adapter.js.map +1 -0
  151. package/dist/terms-v1.d.ts +74 -0
  152. package/dist/terms-v1.d.ts.map +1 -0
  153. package/dist/terms-v1.js +156 -0
  154. package/dist/terms-v1.js.map +1 -0
  155. package/dist/terms.d.ts +341 -0
  156. package/dist/terms.d.ts.map +1 -0
  157. package/dist/terms.js +715 -0
  158. package/dist/terms.js.map +1 -0
  159. package/dist/test-customer.d.ts +1525 -0
  160. package/dist/test-customer.d.ts.map +1 -0
  161. package/dist/test-customer.js +260 -0
  162. package/dist/test-customer.js.map +1 -0
  163. package/dist/types.d.ts +272 -0
  164. package/dist/types.d.ts.map +1 -0
  165. package/dist/types.js +32 -0
  166. package/dist/types.js.map +1 -0
  167. package/dist/version.d.ts +51 -0
  168. package/dist/version.d.ts.map +1 -0
  169. package/dist/version.js +66 -0
  170. package/dist/version.js.map +1 -0
  171. package/dist/wire.d.ts +2698 -0
  172. package/dist/wire.d.ts.map +1 -0
  173. package/dist/wire.js +678 -0
  174. package/dist/wire.js.map +1 -0
  175. package/package.json +37 -3
  176. package/vectors/api-request-v1.json +53 -0
  177. package/vectors/contest-statement-v1.json +42 -0
  178. package/vectors/terms-v2.json +398 -0
  179. package/vectors/terms-v3.json +2103 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"references.d.ts","sourceRoot":"","sources":["../src/references.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,OAAO,EAAuB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B,OAAO,EAAE,MAAM,CAAA;IACf,eAAe,EAAE,GAAG,CAAA;IACpB,QAAQ,EAAE,MAAM,CAAA;IAChB,yCAAyC;IACzC,SAAS,EAAE,OAAO,CAAA;IAClB,oDAAoD;IACpD,SAAS,EAAE,OAAO,CAAA;IAClB,yCAAyC;IACzC,OAAO,EAAE,OAAO,CAAA;IAChB,KAAK,EAAE;QAAE,IAAI,EAAE,OAAO,CAAC;QAAC,EAAE,EAAE,OAAO,CAAC;QAAC,MAAM,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,GAAG,IAAI,CAAA;CACxE;AAED,0DAA0D;AAC1D,MAAM,WAAW,YAAY;IAC3B,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,OAAO,CAAA;CACf;AAED,4FAA4F;AAC5F,wBAAgB,mBAAmB,CACjC,GAAG,EAAE,IAAI,CAAC,gBAAgB,EAAE,SAAS,GAAG,OAAO,CAAC,EAChD,QAAQ,EAAE,SAAS,YAAY,EAAE,GAChC,OAAO,CAET;AAED;;;;;;;GAOG;AACH,wBAAgB,qBAAqB,CACnC,GAAG,EAAE,gBAAgB,EACrB,IAAI,EAAE,gBAAgB,GAAG,IAAI,EAC7B,QAAQ,EAAE,SAAS,YAAY,EAAE,GAChC,UAAU,GAAG,IAAI,CAmBnB;AAED;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,OAAO,EAAE,gBAAgB,CAAA;IACzB,UAAU,EAAE,mBAAmB,CAAA;IAC/B,GAAG,EAAE,UAAU,GAAG,OAAO,CAAA;CAC1B;AAED;;;;;;;;;GASG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,SAAS,WAAW,EAAE,GAAG,UAAU,GAAG,IAAI,CASzF"}
@@ -0,0 +1,108 @@
1
+ import { encodeAbiParameters, isAddressEqual, keccak256 } from 'viem';
2
+ import { z } from 'zod';
3
+ import { canonicalJson } from './canonical.js';
4
+ import { address, bytes32, side } from './json-fields.js';
5
+ /** A commitment reference's JSON form, with hex in lower case. */
6
+ export const commitmentReferenceJson = z.strictObject({
7
+ kind: z.literal('commitment'),
8
+ chainId: z.number().int().positive(),
9
+ lock: address,
10
+ commitmentId: bytes32,
11
+ side,
12
+ termsHash: bytes32,
13
+ });
14
+ /**
15
+ * A payment's canonical id: its chain, transaction hash and log index. Records that cite the same
16
+ * transfer share it, whatever else they say about the transfer.
17
+ */
18
+ export function paymentId(p) {
19
+ return `${p.chainId}:${p.transactionHash.toLowerCase()}:${p.logIndex}`;
20
+ }
21
+ /** The label a reference's hash starts from. */
22
+ export const REFERENCE_LABEL = 'avow:reference:v1';
23
+ /**
24
+ * A reference's hash: keccak256 of the ABI encoding of the string `avow:reference:v1` and the
25
+ * reference's canonical JSON, with every address and hash in lower case. It names a reference in
26
+ * 32 bytes.
27
+ */
28
+ export function referenceHash(r) {
29
+ const lower = (h) => h.toLowerCase();
30
+ const canonical = r.kind === 'commitment'
31
+ ? {
32
+ ...r,
33
+ lock: lower(r.lock),
34
+ commitmentId: lower(r.commitmentId),
35
+ termsHash: lower(r.termsHash),
36
+ }
37
+ : {
38
+ ...r,
39
+ transactionHash: lower(r.transactionHash),
40
+ token: lower(r.token),
41
+ from: lower(r.from),
42
+ to: lower(r.to),
43
+ memo: lower(r.memo),
44
+ };
45
+ return keccak256(encodeAbiParameters([{ type: 'string' }, { type: 'string' }], [REFERENCE_LABEL, canonicalJson(canonical)]));
46
+ }
47
+ /** Whether the reference names a token the reader accepts, on the reference's own chain. */
48
+ export function acceptsPaymentToken(ref, accepted) {
49
+ return accepted.some((t) => t.chainId === ref.chainId && isAddressEqual(t.token, ref.token));
50
+ }
51
+ /**
52
+ * Checks a payment reference against what a chain reader found. The reference names its own token,
53
+ * so the reader says which tokens it accepts, and any contract can emit an event shaped like a
54
+ * token's. It passes only when the token is one the reader accepts, the transaction succeeded and
55
+ * is finalized, and the log at the reference's index is that token's own `TransferWithMemo` event
56
+ * with exactly the sender, recipient, amount and memo the reference states. A plain transfer with
57
+ * no memo doesn't count. Returns the first problem, or null.
58
+ */
59
+ export function checkPaymentReference(ref, seen, accepted) {
60
+ if (!acceptsPaymentToken(ref, accepted))
61
+ return 'payment.token_not_accepted';
62
+ if (!seen ||
63
+ seen.chainId !== ref.chainId ||
64
+ seen.transactionHash.toLowerCase() !== ref.transactionHash.toLowerCase() ||
65
+ seen.logIndex !== ref.logIndex) {
66
+ return 'payment.not_found';
67
+ }
68
+ if (!seen.succeeded)
69
+ return 'payment.failed';
70
+ if (!seen.finalized)
71
+ return 'payment.not_final';
72
+ if (!seen.event)
73
+ return 'payment.not_memo_transfer';
74
+ if (!isAddressEqual(seen.emitter, ref.token))
75
+ return 'payment.token_mismatch';
76
+ if (!isAddressEqual(seen.event.from, ref.from))
77
+ return 'payment.sender_mismatch';
78
+ if (!isAddressEqual(seen.event.to, ref.to))
79
+ return 'payment.recipient_mismatch';
80
+ if (seen.event.amount !== ref.amount)
81
+ return 'payment.amount_mismatch';
82
+ if (seen.event.memo.toLowerCase() !== ref.memo.toLowerCase())
83
+ return 'payment.memo_mismatch';
84
+ return null;
85
+ }
86
+ /**
87
+ * Checks a set of attributions against the rule that a payment's value counts at most once. Citing
88
+ * a payment as evidence any number of times passes. A second attribution of the same payment's
89
+ * value refuses, whichever obligation it names, since this release allows exclusive attribution
90
+ * only and no partial allocations. No attribution backs a commitment.
91
+ *
92
+ * This proves uniqueness only within the set it's given. It can't see an attribution another
93
+ * service or a later request makes. Before any version lets a payment's value be allocated, Avow
94
+ * needs a persistent registry that records each attribution atomically.
95
+ */
96
+ export function checkAttributions(attributions) {
97
+ const valued = new Set();
98
+ for (const a of attributions) {
99
+ if (a.use !== 'value')
100
+ continue;
101
+ const id = paymentId(a.payment);
102
+ if (valued.has(id))
103
+ return 'payment.attributed_twice';
104
+ valued.add(id);
105
+ }
106
+ return null;
107
+ }
108
+ //# sourceMappingURL=references.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"references.js","sourceRoot":"","sources":["../src/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
1
+ /**
2
+ * Remembers which signed messages it has seen, so a replayed one is refused. Readers check
3
+ * whether a key is recorded before they act, and record it only after every other check passes
4
+ * and any signing is done, so a refused request never uses up an approval.
5
+ */
6
+ export interface ReplayGuard {
7
+ /** True when the key is already recorded. */
8
+ isRecorded(key: string): Promise<boolean>;
9
+ /** Records every key, or none of them if any is already recorded. True when all were new. */
10
+ recordAll(entries: readonly {
11
+ key: string;
12
+ expiry: bigint;
13
+ }[]): Promise<boolean>;
14
+ }
15
+ /**
16
+ * A replay guard in memory, for tests and a single process. Production stores keys in the
17
+ * database with a unique index and records a set in one transaction, so two workers can't both
18
+ * record one.
19
+ */
20
+ export declare class MemoryReplayGuard implements ReplayGuard {
21
+ #private;
22
+ isRecorded(key: string): Promise<boolean>;
23
+ recordAll(entries: readonly {
24
+ key: string;
25
+ expiry: bigint;
26
+ }[]): Promise<boolean>;
27
+ /** Forgets keys whose messages have expired, which readers refuse on expiry anyway. */
28
+ prune(now: bigint): void;
29
+ }
30
+ //# sourceMappingURL=replay.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.d.ts","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,WAAW,WAAW;IAC1B,6CAA6C;IAC7C,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IACzC,6FAA6F;IAC7F,SAAS,CAAC,OAAO,EAAE,SAAS;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;CACjF;AAED;;;;GAIG;AACH,qBAAa,iBAAkB,YAAW,WAAW;;IAG7C,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAE9C;IAEK,SAAS,CAAC,OAAO,EAAE,SAAS;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,MAAM,EAAE,MAAM,CAAA;KAAE,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC,CAKrF;IAED,uFAAuF;IACvF,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAIvB;CACF"}
package/dist/replay.js ADDED
@@ -0,0 +1,27 @@
1
+ /**
2
+ * A replay guard in memory, for tests and a single process. Production stores keys in the
3
+ * database with a unique index and records a set in one transaction, so two workers can't both
4
+ * record one.
5
+ */
6
+ export class MemoryReplayGuard {
7
+ #seen = new Map();
8
+ async isRecorded(key) {
9
+ return this.#seen.has(key);
10
+ }
11
+ async recordAll(entries) {
12
+ const keys = entries.map((e) => e.key);
13
+ if (new Set(keys).size !== keys.length || keys.some((k) => this.#seen.has(k)))
14
+ return false;
15
+ for (const e of entries)
16
+ this.#seen.set(e.key, e.expiry);
17
+ return true;
18
+ }
19
+ /** Forgets keys whose messages have expired, which readers refuse on expiry anyway. */
20
+ prune(now) {
21
+ for (const [key, expiry] of this.#seen) {
22
+ if (expiry <= now)
23
+ this.#seen.delete(key);
24
+ }
25
+ }
26
+ }
27
+ //# sourceMappingURL=replay.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"replay.js","sourceRoot":"","sources":["../src/replay.ts"],"names":[],"mappings":"AAYA;;;;GAIG;AACH,MAAM,OAAO,iBAAiB;IACnB,KAAK,GAAG,IAAI,GAAG,EAAkB,CAAA;IAE1C,KAAK,CAAC,UAAU,CAAC,GAAW;QAC1B,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;IAC5B,CAAC;IAED,KAAK,CAAC,SAAS,CAAC,OAAmD;QACjE,MAAM,IAAI,GAAG,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;QACtC,IAAI,IAAI,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,KAAK,IAAI,CAAC,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAA;QAC3F,KAAK,MAAM,CAAC,IAAI,OAAO;YAAE,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,MAAM,CAAC,CAAA;QACxD,OAAO,IAAI,CAAA;IACb,CAAC;IAED,uFAAuF;IACvF,KAAK,CAAC,GAAW;QACf,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YACvC,IAAI,MAAM,IAAI,GAAG;gBAAE,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC3C,CAAC;IACH,CAAC;CACF"}
@@ -0,0 +1,200 @@
1
+ import { type Address, type Hex } from 'viem';
2
+ import { z } from 'zod';
3
+ import type { HashSigner } from './keychain.js';
4
+ import type { Parsed } from './wire.js';
5
+ /**
6
+ * The registrations a sandbox business or supplier signs, defined once for Avow's API that checks
7
+ * them and the SDK that signs them. A business registers at `SANDBOX_PATHS.businesses` and a
8
+ * supplier at `SANDBOX_PATHS.suppliers`.
9
+ */
10
+ /**
11
+ * The environment every sandbox registration names, so a signature made for it can never stand
12
+ * for a registration anywhere else.
13
+ */
14
+ export declare const SANDBOX_ENVIRONMENT = "sandbox";
15
+ /**
16
+ * The EIP-712 domain every sandbox registration is signed under. Its name is its own, apart from
17
+ * the `Avow` domain every other message Avow and its locks read is signed under, so no signature
18
+ * on a registration can stand for another message, and none on another message for a
19
+ * registration. It names this chain and the lock factory, the one contract the sandbox serves.
20
+ */
21
+ export declare const registrationDomain: (chainId: number, factory: Address) => {
22
+ readonly name: 'Avow sandbox registration';
23
+ readonly version: '1';
24
+ readonly chainId: number;
25
+ readonly verifyingContract: `0x${string}`;
26
+ };
27
+ export declare const REGISTRATION_TYPES: {
28
+ readonly BusinessRegistration: readonly [{
29
+ readonly name: 'environment';
30
+ readonly type: 'string';
31
+ }, {
32
+ readonly name: 'audience';
33
+ readonly type: 'string';
34
+ }, {
35
+ readonly name: 'account';
36
+ readonly type: 'address';
37
+ }, {
38
+ readonly name: 'lock';
39
+ readonly type: 'address';
40
+ }, {
41
+ readonly name: 'legalName';
42
+ readonly type: 'string';
43
+ }, {
44
+ readonly name: 'country';
45
+ readonly type: 'string';
46
+ }, {
47
+ readonly name: 'nonce';
48
+ readonly type: 'bytes32';
49
+ }, {
50
+ readonly name: 'expiry';
51
+ readonly type: 'uint64';
52
+ }];
53
+ readonly SupplierRegistration: readonly [{
54
+ readonly name: 'environment';
55
+ readonly type: 'string';
56
+ }, {
57
+ readonly name: 'audience';
58
+ readonly type: 'string';
59
+ }, {
60
+ readonly name: 'account';
61
+ readonly type: 'address';
62
+ }, {
63
+ readonly name: 'payee';
64
+ readonly type: 'address';
65
+ }, {
66
+ readonly name: 'signingKey';
67
+ readonly type: 'address';
68
+ }, {
69
+ readonly name: 'nonce';
70
+ readonly type: 'bytes32';
71
+ }, {
72
+ readonly name: 'expiry';
73
+ readonly type: 'uint64';
74
+ }];
75
+ };
76
+ /** What a business's account signs to register it in the sandbox. */
77
+ export interface BusinessRegistration {
78
+ environment: typeof SANDBOX_ENVIRONMENT;
79
+ audience: string;
80
+ account: Address;
81
+ lock: Address;
82
+ legalName: string;
83
+ country: string;
84
+ nonce: Hex;
85
+ /** Unix seconds after which Avow won't take it. */
86
+ expiry: bigint;
87
+ }
88
+ /** What a supplier's account and its signing key both sign to register it in the sandbox. */
89
+ export interface SupplierRegistration {
90
+ environment: typeof SANDBOX_ENVIRONMENT;
91
+ audience: string;
92
+ account: Address;
93
+ payee: Address;
94
+ signingKey: Address;
95
+ nonce: Hex;
96
+ expiry: bigint;
97
+ }
98
+ /** The digest each signer of a registration signs, under the sandbox's own domain. */
99
+ export declare function registrationDigest(domain: ReturnType<typeof registrationDomain>, r: {
100
+ kind: 'business';
101
+ registration: BusinessRegistration;
102
+ } | {
103
+ kind: 'supplier';
104
+ registration: SupplierRegistration;
105
+ }): Hex;
106
+ /** The body of a business's registration request, as the API reads it. */
107
+ export declare const businessRegistrationBody: z.ZodObject<{
108
+ registration: z.ZodObject<{
109
+ environment: z.ZodLiteral<"sandbox">;
110
+ audience: z.ZodString;
111
+ account: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
112
+ nonce: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
113
+ expiry: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
114
+ lock: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
115
+ legalName: z.ZodString;
116
+ country: z.ZodString;
117
+ }, z.core.$strict>;
118
+ accountSignature: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
119
+ }, z.core.$strict>;
120
+ /** The body of a supplier's registration request, as the API reads it. */
121
+ export declare const supplierRegistrationBody: z.ZodObject<{
122
+ registration: z.ZodObject<{
123
+ environment: z.ZodLiteral<"sandbox">;
124
+ audience: z.ZodString;
125
+ account: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
126
+ nonce: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
127
+ expiry: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
128
+ payee: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
129
+ signingKey: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
130
+ }, z.core.$strict>;
131
+ accountSignature: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
132
+ signingKeySignature: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
133
+ }, z.core.$strict>;
134
+ /**
135
+ * A business's registration, signed by its account's root key, as the body of the request that
136
+ * registers it. The request around it has to be signed by the same root key, naming the business's
137
+ * lock. It's valid for `validity` seconds from `now`, which the API holds to at most five minutes.
138
+ */
139
+ export declare function signBusinessRegistration(r: {
140
+ chainId: number;
141
+ factory: Address;
142
+ /** The API's origin, which the registration names as its audience. */
143
+ audience: string;
144
+ account: Address;
145
+ lock: Address;
146
+ legalName: string;
147
+ country: string;
148
+ now: bigint;
149
+ validity: bigint;
150
+ rootKey: HashSigner;
151
+ }): Promise<{
152
+ registration: BusinessRegistration;
153
+ accountSignature: Hex;
154
+ }>;
155
+ /**
156
+ * A supplier's registration, signed by its account's root key and by its signing key over the same
157
+ * digest, so nobody registers a signing key they don't hold. The request around it has to be
158
+ * signed by the same root key, naming the lock factory, since a supplier has no lock of its own.
159
+ * It's valid for `validity` seconds from `now`, which the API holds to at most five minutes.
160
+ */
161
+ export declare function signSupplierRegistration(r: {
162
+ chainId: number;
163
+ factory: Address;
164
+ /** The API's origin, which the registration names as its audience. */
165
+ audience: string;
166
+ account: Address;
167
+ /** Where charges the supplier reports are paid out. */
168
+ payee: Address;
169
+ rootKey: HashSigner;
170
+ /** The supplier's signing key, which signs its agreements, acceptances and requests. */
171
+ signingKey: HashSigner & {
172
+ address: Address;
173
+ };
174
+ now: bigint;
175
+ validity: bigint;
176
+ }): Promise<{
177
+ registration: SupplierRegistration;
178
+ accountSignature: Hex;
179
+ signingKeySignature: Hex;
180
+ }>;
181
+ /** What Avow answers a sandbox business's registration with: its id and the test dollars it paid. */
182
+ export interface SandboxRegistrationAnswer {
183
+ businessId: string;
184
+ testData: true;
185
+ payout: {
186
+ transactionHash: Hex;
187
+ amount: bigint;
188
+ };
189
+ }
190
+ export declare const sandboxRegistrationAnswerJson: z.ZodObject<{
191
+ businessId: z.ZodString;
192
+ testData: z.ZodLiteral<true>;
193
+ payout: z.ZodObject<{
194
+ transactionHash: z.ZodPipe<z.ZodString, z.ZodTransform<`0x${string}`, string>>;
195
+ amount: z.ZodPipe<z.ZodString, z.ZodTransform<bigint, string>>;
196
+ }, z.core.$strip>;
197
+ }, z.core.$strip>;
198
+ /** Reads Avow's answer to a sandbox business's registration, ignoring fields it doesn't use. */
199
+ export declare function parseSandboxRegistrationAnswer(json: unknown): Parsed<SandboxRegistrationAnswer>;
200
+ //# sourceMappingURL=sandbox-registration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sandbox-registration.d.ts","sourceRoot":"","sources":["../src/sandbox-registration.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,OAAO,EAEZ,KAAK,GAAG,EAKT,MAAM,MAAM,CAAA;AACb,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAC/C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,WAAW,CAAA;AAEvC;;;;GAIG;AAEH;;;GAGG;AACH,eAAO,MAAM,mBAAmB,YAAY,CAAA;AAE5C;;;;;GAKG;AACH,eAAO,MAAM,kBAAkB,YAAa,MAAM,WAAW,OAAO;mBAE1D,2BAA2B;sBACxB,GAAG;;;CAGH,CAAA;AAEb,eAAO,MAAM,kBAAkB;aAC7B,oBAAoB;iBAChB,IAAI,EAAE,aAAa;iBAAE,IAAI,EAAE,QAAQ;;iBACnC,IAAI,EAAE,UAAU;iBAAE,IAAI,EAAE,QAAQ;;iBAChC,IAAI,EAAE,SAAS;iBAAE,IAAI,EAAE,SAAS;;iBAChC,IAAI,EAAE,MAAM;iBAAE,IAAI,EAAE,SAAS;;iBAC7B,IAAI,EAAE,WAAW;iBAAE,IAAI,EAAE,QAAQ;;iBACjC,IAAI,EAAE,SAAS;iBAAE,IAAI,EAAE,QAAQ;;iBAC/B,IAAI,EAAE,OAAO;iBAAE,IAAI,EAAE,SAAS;;iBAC9B,IAAI,EAAE,QAAQ;iBAAE,IAAI,EAAE,QAAQ;;aAElC,oBAAoB;iBAChB,IAAI,EAAE,aAAa;iBAAE,IAAI,EAAE,QAAQ;;iBACnC,IAAI,EAAE,UAAU;iBAAE,IAAI,EAAE,QAAQ;;iBAChC,IAAI,EAAE,SAAS;iBAAE,IAAI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B;;;;;;;iBAaxC,CAAA;AAEF,gGAAgG;AAChG,wBAAgB,8BAA8B,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,CAAC,yBAAyB,CAAC,CAK/F"}
@@ -0,0 +1,172 @@
1
+ import { getAddress, hashTypedData, isAddressEqual, toHex, zeroAddress, } from 'viem';
2
+ import { z } from 'zod';
3
+ /**
4
+ * The registrations a sandbox business or supplier signs, defined once for Avow's API that checks
5
+ * them and the SDK that signs them. A business registers at `SANDBOX_PATHS.businesses` and a
6
+ * supplier at `SANDBOX_PATHS.suppliers`.
7
+ */
8
+ /**
9
+ * The environment every sandbox registration names, so a signature made for it can never stand
10
+ * for a registration anywhere else.
11
+ */
12
+ export const SANDBOX_ENVIRONMENT = 'sandbox';
13
+ /**
14
+ * The EIP-712 domain every sandbox registration is signed under. Its name is its own, apart from
15
+ * the `Avow` domain every other message Avow and its locks read is signed under, so no signature
16
+ * on a registration can stand for another message, and none on another message for a
17
+ * registration. It names this chain and the lock factory, the one contract the sandbox serves.
18
+ */
19
+ export const registrationDomain = (chainId, factory) => ({
20
+ name: 'Avow sandbox registration',
21
+ version: '1',
22
+ chainId,
23
+ verifyingContract: factory,
24
+ });
25
+ export const REGISTRATION_TYPES = {
26
+ BusinessRegistration: [
27
+ { name: 'environment', type: 'string' },
28
+ { name: 'audience', type: 'string' },
29
+ { name: 'account', type: 'address' },
30
+ { name: 'lock', type: 'address' },
31
+ { name: 'legalName', type: 'string' },
32
+ { name: 'country', type: 'string' },
33
+ { name: 'nonce', type: 'bytes32' },
34
+ { name: 'expiry', type: 'uint64' },
35
+ ],
36
+ SupplierRegistration: [
37
+ { name: 'environment', type: 'string' },
38
+ { name: 'audience', type: 'string' },
39
+ { name: 'account', type: 'address' },
40
+ { name: 'payee', type: 'address' },
41
+ { name: 'signingKey', type: 'address' },
42
+ { name: 'nonce', type: 'bytes32' },
43
+ { name: 'expiry', type: 'uint64' },
44
+ ],
45
+ };
46
+ /** The digest each signer of a registration signs, under the sandbox's own domain. */
47
+ export function registrationDigest(domain, r) {
48
+ return r.kind === 'business'
49
+ ? hashTypedData({
50
+ domain,
51
+ types: REGISTRATION_TYPES,
52
+ primaryType: 'BusinessRegistration',
53
+ message: r.registration,
54
+ })
55
+ : hashTypedData({
56
+ domain,
57
+ types: REGISTRATION_TYPES,
58
+ primaryType: 'SupplierRegistration',
59
+ message: r.registration,
60
+ });
61
+ }
62
+ const address = z
63
+ .string()
64
+ .regex(/^0x[0-9a-fA-F]{40}$/)
65
+ .transform((s) => getAddress(s))
66
+ .refine((a) => !isAddressEqual(a, zeroAddress), 'not the zero address');
67
+ const bytes32 = z
68
+ .string()
69
+ .regex(/^0x[0-9a-fA-F]{64}$/)
70
+ .transform((s) => s.toLowerCase());
71
+ const u64 = z
72
+ .string()
73
+ .regex(/^(0|[1-9][0-9]*)$/)
74
+ .transform((s) => BigInt(s))
75
+ .refine((v) => v < 2n ** 64n);
76
+ const signature = z
77
+ .string()
78
+ .regex(/^0x([0-9a-fA-F]{2})+$/)
79
+ .transform((s) => s);
80
+ const common = {
81
+ environment: z.literal(SANDBOX_ENVIRONMENT),
82
+ audience: z.string().min(1).max(256),
83
+ account: address,
84
+ nonce: bytes32,
85
+ expiry: u64,
86
+ };
87
+ /** The body of a business's registration request, as the API reads it. */
88
+ export const businessRegistrationBody = z.strictObject({
89
+ registration: z.strictObject({
90
+ ...common,
91
+ lock: address,
92
+ legalName: z.string().min(1).max(200),
93
+ country: z.string().regex(/^[A-Z]{2}$/),
94
+ }),
95
+ accountSignature: signature,
96
+ });
97
+ /** The body of a supplier's registration request, as the API reads it. */
98
+ export const supplierRegistrationBody = z.strictObject({
99
+ registration: z.strictObject({ ...common, payee: address, signingKey: address }),
100
+ accountSignature: signature,
101
+ signingKeySignature: signature,
102
+ });
103
+ /**
104
+ * A business's registration, signed by its account's root key, as the body of the request that
105
+ * registers it. The request around it has to be signed by the same root key, naming the business's
106
+ * lock. It's valid for `validity` seconds from `now`, which the API holds to at most five minutes.
107
+ */
108
+ export async function signBusinessRegistration(r) {
109
+ const registration = {
110
+ environment: SANDBOX_ENVIRONMENT,
111
+ audience: r.audience,
112
+ account: r.account,
113
+ lock: r.lock,
114
+ legalName: r.legalName,
115
+ country: r.country,
116
+ nonce: toHex(crypto.getRandomValues(new Uint8Array(32))),
117
+ expiry: r.now + r.validity,
118
+ };
119
+ const digest = registrationDigest(registrationDomain(r.chainId, r.factory), {
120
+ kind: 'business',
121
+ registration,
122
+ });
123
+ return { registration, accountSignature: await r.rootKey.sign({ hash: digest }) };
124
+ }
125
+ /**
126
+ * A supplier's registration, signed by its account's root key and by its signing key over the same
127
+ * digest, so nobody registers a signing key they don't hold. The request around it has to be
128
+ * signed by the same root key, naming the lock factory, since a supplier has no lock of its own.
129
+ * It's valid for `validity` seconds from `now`, which the API holds to at most five minutes.
130
+ */
131
+ export async function signSupplierRegistration(r) {
132
+ const registration = {
133
+ environment: SANDBOX_ENVIRONMENT,
134
+ audience: r.audience,
135
+ account: r.account,
136
+ payee: r.payee,
137
+ signingKey: r.signingKey.address,
138
+ nonce: toHex(crypto.getRandomValues(new Uint8Array(32))),
139
+ expiry: r.now + r.validity,
140
+ };
141
+ const hash = registrationDigest(registrationDomain(r.chainId, r.factory), {
142
+ kind: 'supplier',
143
+ registration,
144
+ });
145
+ return {
146
+ registration,
147
+ accountSignature: await r.rootKey.sign({ hash }),
148
+ signingKeySignature: await r.signingKey.sign({ hash }),
149
+ };
150
+ }
151
+ export const sandboxRegistrationAnswerJson = z.object({
152
+ businessId: z.string().min(1),
153
+ testData: z.literal(true),
154
+ payout: z.object({
155
+ transactionHash: z
156
+ .string()
157
+ .regex(/^0x[0-9a-fA-F]{64}$/)
158
+ .transform((s) => s.toLowerCase()),
159
+ amount: z
160
+ .string()
161
+ .regex(/^(0|[1-9][0-9]*)$/)
162
+ .transform((s) => BigInt(s)),
163
+ }),
164
+ });
165
+ /** Reads Avow's answer to a sandbox business's registration, ignoring fields it doesn't use. */
166
+ export function parseSandboxRegistrationAnswer(json) {
167
+ const parsed = sandboxRegistrationAnswerJson.safeParse(json);
168
+ return parsed.success
169
+ ? { ok: true, value: parsed.data }
170
+ : { ok: false, reason: 'message.invalid' };
171
+ }
172
+ //# sourceMappingURL=sandbox-registration.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sandbox-registration.js","sourceRoot":"","sources":["../src/sandbox-registration.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,UAAU,EAEV,aAAa,EACb,cAAc,EACd,KAAK,EACL,WAAW,GACZ,MAAM,MAAM,CAAA;AACb,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB;;;;GAIG;AAEH;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,SAAS,CAAA;AAE5C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,OAAe,EAAE,OAAgB,EAAE,EAAE,CACtE,CAAC;IACC,IAAI,EAAE,2BAA2B;IACjC,OAAO,EAAE,GAAG;IACZ,OAAO;IACP,iBAAiB,EAAE,OAAO;CAC3B,CAAU,CAAA;AAEb,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,oBAAoB,EAAE;QACpB,EAAE,IAAI,EAAE,aAAa,EAAE,IAAI,EAAE,QAAQ,EAAE;QACvC,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,QAAQ,EAAE;QACpC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,SAAS,EAAE;QACpC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,SAAS,EAAE;QACjC,EAAE,IAAI,EAAE,WAAW,EAAE,IAAI,EAAE,QAAQ,EAAE;QACrC,EAAE,IAAI,EAAE,SAAS,EAAE,IAAI,EAAE,QAAQ,EAAE;QACnC,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE;QAClC,EAAE,IAAI,EAAE,QAAQ,EAAE,IAAI,EAAE,QAAQ,EAAE;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