@sonordev/contracts 2.27.0 → 2.29.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.
@@ -30,6 +30,7 @@ export declare const CONFIRMATION_LOCKS_PER_DAY = 3;
30
30
  export declare const CONFIRMATION_RETRY_MAX_SECONDS = 86400;
31
31
  /** The marker the issue RPC requires in its command. Only a build that emails the verified login address and filters what it reads sends it; an older build is refused before anything is minted. */
32
32
  export declare const CONFIRMATION_CONTRACT: 'resonance-confirmation/1';
33
+ export declare const POLICY_CONFIRMATION_PURPOSE: 'operating_policy';
33
34
  /** Where the person said yes. */
34
35
  export declare const ON_THEIR_WORD_CHANNEL: 'mcp_chat';
35
36
  /** How the code reached them. */
@@ -38,9 +39,11 @@ export declare const ON_THEIR_WORD_CONFIRMATION_CHANNEL: 'email';
38
39
  export declare const ON_THEIR_WORD_QUOTE_MAX = 1000;
39
40
  export declare const CONFIRMATION_REQUEST_ROUTE = "campaigns/:campaignId/plans/:planId/mandate-confirmation";
40
41
  export declare const CONFIRMATION_REDEEM_ROUTE = "campaigns/:campaignId/mandate-confirmation/redeem";
42
+ export declare const CONFIRMATION_CLAIM_ROUTE = "campaigns/:campaignId/mandate-confirmation/claim";
41
43
  /** The request path with its ids filled in. */
42
44
  export declare function confirmationRequestPath(projectId: string, campaignId: string, planId: string): string;
43
45
  export declare function confirmationRedeemPath(projectId: string, campaignId: string): string;
46
+ export declare function confirmationClaimPath(projectId: string, campaignId: string): string;
44
47
  /** In every confirmation email's subject, so a mailbox reader can recognise it and leave it out. */
45
48
  export declare const CONFIRMATION_MAIL_SUBJECT_TAG = "[Sonor budget confirmation]";
46
49
  /** The header the email carries; its value is CONFIRMATION_MAIL_HEADER_VALUE. */
@@ -62,37 +65,58 @@ export declare class ConfirmationContractError extends Error {
62
65
  readonly failure: ConfirmationFailure;
63
66
  constructor(message: string, failure?: ConfirmationFailure);
64
67
  }
68
+ export interface PolicyConfirmationIdentity {
69
+ purpose: typeof POLICY_CONFIRMATION_PURPOSE;
70
+ ledgerActionId: string;
71
+ actionDigest: string;
72
+ }
73
+ type LegacyConfirmationIdentity = {
74
+ purpose?: never;
75
+ actionDigest?: never;
76
+ ledgerActionId?: never;
77
+ };
78
+ type ConfirmationIdentity = PolicyConfirmationIdentity | LegacyConfirmationIdentity;
79
+ /** The draft's own contract is applied by the caller after reading this envelope. */
80
+ export declare function parsePolicyConfirmationIssueRequest(raw: unknown): PolicyConfirmationIdentity & {
81
+ draft: Record<string, unknown>;
82
+ };
65
83
  /** What a person typed as the code: six digits, with the spaces, hyphens and dots people add when they read one out loud taken off. Null for anything else. */
66
84
  export declare function readConfirmationCode(typed: unknown): string | null;
67
- export interface ConfirmationRedeemRequest {
85
+ /** Scan the whole bounded quote. The lookahead also finds overlapping spellings. */
86
+ export declare function confirmationQuoteCodeCandidates(quote: unknown): string[];
87
+ /** Check the submitted code before redeeming it or persisting the approval quote. */
88
+ export declare function confirmationQuoteContainsCode(quote: string, typedCode: unknown): boolean;
89
+ interface ConfirmationRedeemFields {
68
90
  confirmationId: string;
69
91
  /** The ledger request the agent filed for this draft. */
70
92
  ledgerActionId: string;
71
93
  /** The six digits, as the person read them. */
72
94
  code: string;
73
95
  }
74
- /** The redeem body: exactly these three keys, the ids as uuids and the code as six digits (what a person typed is cleaned by readConfirmationCode). */
96
+ export type ConfirmationRedeemRequest = ConfirmationRedeemFields & (Pick<LegacyConfirmationIdentity, 'purpose' | 'actionDigest'> | PolicyConfirmationIdentity);
97
+ /** Redeem a budget code or a code bound to one saved operating policy action. */
75
98
  export declare function parseConfirmationRedeemRequest(raw: unknown): ConfirmationRedeemRequest;
76
- export interface ConfirmationReceipt {
99
+ interface ConfirmationReceiptFields {
77
100
  confirmationId: string;
78
101
  channel: typeof ON_THEIR_WORD_CONFIRMATION_CHANNEL;
79
102
  /** The address the code went to, masked by the server ("p***@example.com"). */
80
103
  sentTo: string;
81
- /** The real deadline: the shorter of ten minutes and the prepared plan's own expiry. Say THIS to the person, not "ten minutes". */
104
+ /** The actual deadline for using the issued code. */
82
105
  expiresAt: string;
83
106
  /** Whole minutes from the send to `expiresAt` (at least 2, at most 10). */
84
107
  minutes: number;
85
108
  triesAllowed: number;
86
109
  }
87
- /** The answer to a request for a code. Picks the six fields it knows, so anything else the server said (a code, if it ever did) goes no further. */
110
+ export type ConfirmationReceipt = ConfirmationReceiptFields & ConfirmationIdentity;
111
+ /** Project an issue receipt and require a complete identity for a policy confirmation. */
88
112
  export declare function parseConfirmationReceipt(raw: unknown): ConfirmationReceipt;
89
113
  export interface ConfirmationRedeemed {
90
114
  confirmation: {
91
115
  id: string;
92
116
  channel: typeof ON_THEIR_WORD_CONFIRMATION_CHANNEL;
93
- };
117
+ } & ConfirmationIdentity;
94
118
  }
95
- /** The answer to a redeem: the confirmation to keep in the evidence, its id and channel and nothing else. */
119
+ /** Project the redeemed confirmation to the fields the Ledger can keep. */
96
120
  export declare function parseConfirmationRedeemed(raw: unknown): ConfirmationRedeemed;
97
121
  export interface OnTheirWordEvidence {
98
122
  /** contacts.id of the agent that took the word and filed the request (created_by is `agent:<this>`). */
@@ -105,10 +129,7 @@ export interface OnTheirWordEvidence {
105
129
  review_digest: string;
106
130
  channel: typeof ON_THEIR_WORD_CHANNEL;
107
131
  /** The emailed code that was redeemed for this request: its id, never the code. */
108
- confirmation: {
109
- id: string;
110
- channel: typeof ON_THEIR_WORD_CONFIRMATION_CHANNEL;
111
- };
132
+ confirmation: ConfirmationRedeemed['confirmation'];
112
133
  }
113
134
  export declare const ON_THEIR_WORD_EVIDENCE_KEYS: readonly ['agent', 'quote', 'mandate_digest', 'review_digest', 'channel', 'confirmation'];
114
135
  /**
@@ -236,3 +257,4 @@ export declare function confirmationFailureOf(status: number | null | undefined,
236
257
  code: ConfirmationRefusalCode | null;
237
258
  retryAfterSeconds: number | null;
238
259
  };
260
+ export {};
@@ -14,17 +14,22 @@ var CONFIRMATION_MIN_PLAN_MINUTES = 2;
14
14
  var CONFIRMATION_LOCKS_PER_DAY = 3;
15
15
  var CONFIRMATION_RETRY_MAX_SECONDS = 86400;
16
16
  var CONFIRMATION_CONTRACT = "resonance-confirmation/1";
17
+ var POLICY_CONFIRMATION_PURPOSE = "operating_policy";
17
18
  var ON_THEIR_WORD_CHANNEL = "mcp_chat";
18
19
  var ON_THEIR_WORD_CONFIRMATION_CHANNEL = "email";
19
20
  var ON_THEIR_WORD_QUOTE_MAX = QUOTE_MAX_LENGTH;
20
21
  var CONFIRMATION_REQUEST_ROUTE = "campaigns/:campaignId/plans/:planId/mandate-confirmation";
21
22
  var CONFIRMATION_REDEEM_ROUTE = "campaigns/:campaignId/mandate-confirmation/redeem";
23
+ var CONFIRMATION_CLAIM_ROUTE = "campaigns/:campaignId/mandate-confirmation/claim";
22
24
  function confirmationRequestPath(projectId, campaignId, planId) {
23
25
  return `resonance/projects/${projectId}/campaigns/${campaignId}/plans/${planId}/mandate-confirmation`;
24
26
  }
25
27
  function confirmationRedeemPath(projectId, campaignId) {
26
28
  return `resonance/projects/${projectId}/campaigns/${campaignId}/mandate-confirmation/redeem`;
27
29
  }
30
+ function confirmationClaimPath(projectId, campaignId) {
31
+ return `resonance/projects/${projectId}/${CONFIRMATION_CLAIM_ROUTE.replace(":campaignId", () => campaignId)}`;
32
+ }
28
33
  var CONFIRMATION_MAIL_SUBJECT_TAG = "[Sonor budget confirmation]";
29
34
  var CONFIRMATION_MAIL_HEADER = "X-Sonor-Confirmation";
30
35
  var CONFIRMATION_MAIL_HEADER_VALUE = "resonance-mandate";
@@ -44,20 +49,54 @@ var ConfirmationContractError = class extends Error {
44
49
  }
45
50
  };
46
51
  var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
52
+ var DIGEST = /^[a-f0-9]{64}$/;
47
53
  var isUuid = (value) => typeof value === "string" && UUID.test(value);
48
54
  var isRecord = (value) => !!value && typeof value === "object" && !Array.isArray(value);
55
+ function readPolicyIdentity(value) {
56
+ const { purpose, ledgerActionId, actionDigest } = value;
57
+ if (purpose !== POLICY_CONFIRMATION_PURPOSE || !isUuid(ledgerActionId) || typeof actionDigest !== "string" || !DIGEST.test(actionDigest)) return null;
58
+ return { purpose, ledgerActionId: ledgerActionId.toLowerCase(), actionDigest };
59
+ }
60
+ function parsePolicyConfirmationIssueRequest(raw) {
61
+ if (!isRecord(raw) || Object.keys(raw).length !== 4 || Object.keys(raw).some((key) => !["purpose", "ledgerActionId", "actionDigest", "draft"].includes(key)) || !isRecord(raw.draft)) {
62
+ throw new ConfirmationContractError("Pass the exact saved policy action and its draft.");
63
+ }
64
+ const identity = readPolicyIdentity(raw);
65
+ if (!identity) throw new ConfirmationContractError("Pass the exact saved policy action and its draft.");
66
+ return { ...identity, draft: raw.draft };
67
+ }
68
+ function hasPolicyIdentity(value) {
69
+ return ["purpose", "ledgerActionId", "actionDigest"].some((key) => Object.prototype.hasOwnProperty.call(value, key));
70
+ }
71
+ var CONFIRMATION_CODE_SEPARATOR = /[\s.\-–—]/g;
49
72
  function readConfirmationCode(typed) {
50
73
  if (typeof typed !== "string" || typed.length > 32) return null;
51
- const code = typed.replace(/[\s.\-–—]/g, "");
74
+ const code = typed.replace(CONFIRMATION_CODE_SEPARATOR, "");
52
75
  return new RegExp(`^\\d{${CONFIRMATION_CODE_DIGITS}}$`).test(code) ? code : null;
53
76
  }
77
+ function confirmationQuoteCodeCandidates(quote) {
78
+ if (typeof quote !== "string") throw new ConfirmationContractError("Pass the exact approval quote.");
79
+ if (quote.length > ON_THEIR_WORD_QUOTE_MAX) throw new ConfirmationContractError("Pass only the sentence where they said yes, in 1,000 characters or fewer.", "quote_too_long");
80
+ const tail = `(?:${CONFIRMATION_CODE_SEPARATOR.source}*\\d){${CONFIRMATION_CODE_DIGITS - 1}}`;
81
+ const spelling = new RegExp(`(?<!\\p{Nd})(?=(\\d${tail})(?!\\p{Nd}))`, "gu");
82
+ const candidates = /* @__PURE__ */ new Set();
83
+ for (const [, spelled] of quote.matchAll(spelling)) candidates.add(spelled.replace(CONFIRMATION_CODE_SEPARATOR, ""));
84
+ return [...candidates];
85
+ }
86
+ function confirmationQuoteContainsCode(quote, typedCode) {
87
+ const code = readConfirmationCode(typedCode);
88
+ return code !== null && confirmationQuoteCodeCandidates(quote).includes(code);
89
+ }
54
90
  function parseConfirmationRedeemRequest(raw) {
55
- if (!isRecord(raw) || Object.keys(raw).some((key) => !["confirmationId", "ledgerActionId", "code"].includes(key)) || !isUuid(raw.confirmationId) || !isUuid(raw.ledgerActionId)) {
91
+ if (!isRecord(raw) || Object.keys(raw).some((key) => !["confirmationId", "ledgerActionId", "code", "purpose", "actionDigest"].includes(key)) || !isUuid(raw.confirmationId) || !isUuid(raw.ledgerActionId)) {
56
92
  throw new ConfirmationContractError("Pass the confirmation id, the ledger request it is for, and the six-digit code the person read back.");
57
93
  }
94
+ const policy = readPolicyIdentity(raw);
95
+ if (!policy && ("purpose" in raw || "actionDigest" in raw)) throw new ConfirmationContractError("Pass the exact saved policy action and its confirmation.");
58
96
  const code = readConfirmationCode(raw.code);
59
97
  if (code === null) throw new ConfirmationContractError("The code is six digits. Ask the person to read it back from the email.", "invalid_code");
60
- return { confirmationId: raw.confirmationId.toLowerCase(), ledgerActionId: raw.ledgerActionId.toLowerCase(), code };
98
+ const fields = { confirmationId: raw.confirmationId.toLowerCase(), ledgerActionId: raw.ledgerActionId.toLowerCase(), code };
99
+ return policy ? { ...fields, ...policy } : fields;
61
100
  }
62
101
  function parseConfirmationReceipt(raw) {
63
102
  if (!isRecord(raw)) throw new ConfirmationContractError("The confirmation receipt is not an object.", "unavailable");
@@ -65,22 +104,30 @@ function parseConfirmationReceipt(raw) {
65
104
  if (!isUuid(confirmationId) || channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL || typeof sentTo !== "string" || !/^[^\s@]{1,64}@[^\s@]{1,120}$/.test(sentTo) || typeof expiresAt !== "string" || !Number.isFinite(Date.parse(expiresAt)) || !Number.isSafeInteger(minutes) || minutes < 1 || minutes > 60 || !Number.isSafeInteger(triesAllowed) || triesAllowed < 1 || triesAllowed > 10) {
66
105
  throw new ConfirmationContractError("The confirmation receipt is not the one the server sends.", "unavailable");
67
106
  }
68
- return { confirmationId: confirmationId.toLowerCase(), channel, sentTo, expiresAt, minutes, triesAllowed };
107
+ const policy = readPolicyIdentity(raw);
108
+ if (!policy && hasPolicyIdentity(raw)) throw new ConfirmationContractError("The policy confirmation receipt is incomplete.", "unavailable");
109
+ const fields = { confirmationId: confirmationId.toLowerCase(), channel, sentTo, expiresAt, minutes, triesAllowed };
110
+ return policy ? { ...fields, ...policy } : fields;
69
111
  }
70
112
  function parseConfirmationRedeemed(raw) {
71
113
  const c = isRecord(raw) ? raw.confirmation : null;
72
114
  if (!isRecord(c) || !isUuid(c.id) || c.channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL) throw new ConfirmationContractError("That is not a redeemed confirmation.", "unavailable");
73
- return { confirmation: { id: c.id.toLowerCase(), channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL } };
115
+ const policy = readPolicyIdentity(c);
116
+ if (!policy && hasPolicyIdentity(c)) throw new ConfirmationContractError("The redeemed policy confirmation is incomplete.", "unavailable");
117
+ const fields = { id: c.id.toLowerCase(), channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL };
118
+ return { confirmation: policy ? { ...fields, ...policy } : fields };
74
119
  }
75
120
  var ON_THEIR_WORD_EVIDENCE_KEYS = ["agent", "quote", "mandate_digest", "review_digest", "channel", "confirmation"];
76
- var DIGEST = /^[a-f0-9]{64}$/;
77
121
  var CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/;
78
122
  function parseOnTheirWordEvidence(value) {
79
123
  if (!isRecord(value) || Object.keys(value).length !== ON_THEIR_WORD_EVIDENCE_KEYS.length) return null;
80
124
  const { agent, quote, mandate_digest: mandate, review_digest: review, channel, confirmation } = value;
81
125
  if (channel !== ON_THEIR_WORD_CHANNEL || typeof agent !== "string" || !isUuid(agent) || agent !== agent.toLowerCase() || typeof quote !== "string" || CONTROL.test(quote) || quote !== quote.trim() || quote.length > ON_THEIR_WORD_QUOTE_MAX || !/[\p{L}\p{N}]/u.test(quote) || typeof mandate !== "string" || !DIGEST.test(mandate) || typeof review !== "string" || !DIGEST.test(review)) return null;
82
- if (!isRecord(confirmation) || Object.keys(confirmation).length !== 2 || !isUuid(confirmation.id) || confirmation.id !== confirmation.id.toLowerCase() || confirmation.channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL) return null;
83
- return { agent, quote, mandate_digest: mandate, review_digest: review, channel, confirmation: { id: confirmation.id, channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL } };
126
+ if (!isRecord(confirmation) || !isUuid(confirmation.id) || confirmation.id !== confirmation.id.toLowerCase() || confirmation.channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL) return null;
127
+ const policy = readPolicyIdentity(confirmation);
128
+ if (policy ? Object.keys(confirmation).length !== 5 || confirmation.ledgerActionId !== policy.ledgerActionId : Object.keys(confirmation).length !== 2 || hasPolicyIdentity(confirmation)) return null;
129
+ const fields = { id: confirmation.id, channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL };
130
+ return { agent, quote, mandate_digest: mandate, review_digest: review, channel, confirmation: policy ? { ...fields, ...policy } : fields };
84
131
  }
85
132
  var CONFIRMATION_REFUSALS = {
86
133
  RESONANCE_CONFIRMATION_COMMAND: { status: 400, failure: "invalid_request", message: "That confirmation request was malformed. Ask for a new code." },
@@ -130,6 +177,7 @@ function confirmationFailureOf(status, body) {
130
177
  return { failure: "unavailable", attemptsLeft: null, code: null, retryAfterSeconds: null };
131
178
  }
132
179
 
180
+ exports.CONFIRMATION_CLAIM_ROUTE = CONFIRMATION_CLAIM_ROUTE;
133
181
  exports.CONFIRMATION_CODE_DIGITS = CONFIRMATION_CODE_DIGITS;
134
182
  exports.CONFIRMATION_CONTRACT = CONFIRMATION_CONTRACT;
135
183
  exports.CONFIRMATION_LOCKS_PER_DAY = CONFIRMATION_LOCKS_PER_DAY;
@@ -151,7 +199,11 @@ exports.ON_THEIR_WORD_CHANNEL = ON_THEIR_WORD_CHANNEL;
151
199
  exports.ON_THEIR_WORD_CONFIRMATION_CHANNEL = ON_THEIR_WORD_CONFIRMATION_CHANNEL;
152
200
  exports.ON_THEIR_WORD_EVIDENCE_KEYS = ON_THEIR_WORD_EVIDENCE_KEYS;
153
201
  exports.ON_THEIR_WORD_QUOTE_MAX = ON_THEIR_WORD_QUOTE_MAX;
202
+ exports.POLICY_CONFIRMATION_PURPOSE = POLICY_CONFIRMATION_PURPOSE;
203
+ exports.confirmationClaimPath = confirmationClaimPath;
154
204
  exports.confirmationFailureOf = confirmationFailureOf;
205
+ exports.confirmationQuoteCodeCandidates = confirmationQuoteCodeCandidates;
206
+ exports.confirmationQuoteContainsCode = confirmationQuoteContainsCode;
155
207
  exports.confirmationRedeemPath = confirmationRedeemPath;
156
208
  exports.confirmationRequestPath = confirmationRequestPath;
157
209
  exports.confirmationWaitWords = confirmationWaitWords;
@@ -160,4 +212,5 @@ exports.parseConfirmationReceipt = parseConfirmationReceipt;
160
212
  exports.parseConfirmationRedeemRequest = parseConfirmationRedeemRequest;
161
213
  exports.parseConfirmationRedeemed = parseConfirmationRedeemed;
162
214
  exports.parseOnTheirWordEvidence = parseOnTheirWordEvidence;
215
+ exports.parsePolicyConfirmationIssueRequest = parsePolicyConfirmationIssueRequest;
163
216
  exports.readConfirmationCode = readConfirmationCode;
@@ -12,17 +12,22 @@ var CONFIRMATION_MIN_PLAN_MINUTES = 2;
12
12
  var CONFIRMATION_LOCKS_PER_DAY = 3;
13
13
  var CONFIRMATION_RETRY_MAX_SECONDS = 86400;
14
14
  var CONFIRMATION_CONTRACT = "resonance-confirmation/1";
15
+ var POLICY_CONFIRMATION_PURPOSE = "operating_policy";
15
16
  var ON_THEIR_WORD_CHANNEL = "mcp_chat";
16
17
  var ON_THEIR_WORD_CONFIRMATION_CHANNEL = "email";
17
18
  var ON_THEIR_WORD_QUOTE_MAX = QUOTE_MAX_LENGTH;
18
19
  var CONFIRMATION_REQUEST_ROUTE = "campaigns/:campaignId/plans/:planId/mandate-confirmation";
19
20
  var CONFIRMATION_REDEEM_ROUTE = "campaigns/:campaignId/mandate-confirmation/redeem";
21
+ var CONFIRMATION_CLAIM_ROUTE = "campaigns/:campaignId/mandate-confirmation/claim";
20
22
  function confirmationRequestPath(projectId, campaignId, planId) {
21
23
  return `resonance/projects/${projectId}/campaigns/${campaignId}/plans/${planId}/mandate-confirmation`;
22
24
  }
23
25
  function confirmationRedeemPath(projectId, campaignId) {
24
26
  return `resonance/projects/${projectId}/campaigns/${campaignId}/mandate-confirmation/redeem`;
25
27
  }
28
+ function confirmationClaimPath(projectId, campaignId) {
29
+ return `resonance/projects/${projectId}/${CONFIRMATION_CLAIM_ROUTE.replace(":campaignId", () => campaignId)}`;
30
+ }
26
31
  var CONFIRMATION_MAIL_SUBJECT_TAG = "[Sonor budget confirmation]";
27
32
  var CONFIRMATION_MAIL_HEADER = "X-Sonor-Confirmation";
28
33
  var CONFIRMATION_MAIL_HEADER_VALUE = "resonance-mandate";
@@ -42,20 +47,54 @@ var ConfirmationContractError = class extends Error {
42
47
  }
43
48
  };
44
49
  var UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
50
+ var DIGEST = /^[a-f0-9]{64}$/;
45
51
  var isUuid = (value) => typeof value === "string" && UUID.test(value);
46
52
  var isRecord = (value) => !!value && typeof value === "object" && !Array.isArray(value);
53
+ function readPolicyIdentity(value) {
54
+ const { purpose, ledgerActionId, actionDigest } = value;
55
+ if (purpose !== POLICY_CONFIRMATION_PURPOSE || !isUuid(ledgerActionId) || typeof actionDigest !== "string" || !DIGEST.test(actionDigest)) return null;
56
+ return { purpose, ledgerActionId: ledgerActionId.toLowerCase(), actionDigest };
57
+ }
58
+ function parsePolicyConfirmationIssueRequest(raw) {
59
+ if (!isRecord(raw) || Object.keys(raw).length !== 4 || Object.keys(raw).some((key) => !["purpose", "ledgerActionId", "actionDigest", "draft"].includes(key)) || !isRecord(raw.draft)) {
60
+ throw new ConfirmationContractError("Pass the exact saved policy action and its draft.");
61
+ }
62
+ const identity = readPolicyIdentity(raw);
63
+ if (!identity) throw new ConfirmationContractError("Pass the exact saved policy action and its draft.");
64
+ return { ...identity, draft: raw.draft };
65
+ }
66
+ function hasPolicyIdentity(value) {
67
+ return ["purpose", "ledgerActionId", "actionDigest"].some((key) => Object.prototype.hasOwnProperty.call(value, key));
68
+ }
69
+ var CONFIRMATION_CODE_SEPARATOR = /[\s.\-–—]/g;
47
70
  function readConfirmationCode(typed) {
48
71
  if (typeof typed !== "string" || typed.length > 32) return null;
49
- const code = typed.replace(/[\s.\-–—]/g, "");
72
+ const code = typed.replace(CONFIRMATION_CODE_SEPARATOR, "");
50
73
  return new RegExp(`^\\d{${CONFIRMATION_CODE_DIGITS}}$`).test(code) ? code : null;
51
74
  }
75
+ function confirmationQuoteCodeCandidates(quote) {
76
+ if (typeof quote !== "string") throw new ConfirmationContractError("Pass the exact approval quote.");
77
+ if (quote.length > ON_THEIR_WORD_QUOTE_MAX) throw new ConfirmationContractError("Pass only the sentence where they said yes, in 1,000 characters or fewer.", "quote_too_long");
78
+ const tail = `(?:${CONFIRMATION_CODE_SEPARATOR.source}*\\d){${CONFIRMATION_CODE_DIGITS - 1}}`;
79
+ const spelling = new RegExp(`(?<!\\p{Nd})(?=(\\d${tail})(?!\\p{Nd}))`, "gu");
80
+ const candidates = /* @__PURE__ */ new Set();
81
+ for (const [, spelled] of quote.matchAll(spelling)) candidates.add(spelled.replace(CONFIRMATION_CODE_SEPARATOR, ""));
82
+ return [...candidates];
83
+ }
84
+ function confirmationQuoteContainsCode(quote, typedCode) {
85
+ const code = readConfirmationCode(typedCode);
86
+ return code !== null && confirmationQuoteCodeCandidates(quote).includes(code);
87
+ }
52
88
  function parseConfirmationRedeemRequest(raw) {
53
- if (!isRecord(raw) || Object.keys(raw).some((key) => !["confirmationId", "ledgerActionId", "code"].includes(key)) || !isUuid(raw.confirmationId) || !isUuid(raw.ledgerActionId)) {
89
+ if (!isRecord(raw) || Object.keys(raw).some((key) => !["confirmationId", "ledgerActionId", "code", "purpose", "actionDigest"].includes(key)) || !isUuid(raw.confirmationId) || !isUuid(raw.ledgerActionId)) {
54
90
  throw new ConfirmationContractError("Pass the confirmation id, the ledger request it is for, and the six-digit code the person read back.");
55
91
  }
92
+ const policy = readPolicyIdentity(raw);
93
+ if (!policy && ("purpose" in raw || "actionDigest" in raw)) throw new ConfirmationContractError("Pass the exact saved policy action and its confirmation.");
56
94
  const code = readConfirmationCode(raw.code);
57
95
  if (code === null) throw new ConfirmationContractError("The code is six digits. Ask the person to read it back from the email.", "invalid_code");
58
- return { confirmationId: raw.confirmationId.toLowerCase(), ledgerActionId: raw.ledgerActionId.toLowerCase(), code };
96
+ const fields = { confirmationId: raw.confirmationId.toLowerCase(), ledgerActionId: raw.ledgerActionId.toLowerCase(), code };
97
+ return policy ? { ...fields, ...policy } : fields;
59
98
  }
60
99
  function parseConfirmationReceipt(raw) {
61
100
  if (!isRecord(raw)) throw new ConfirmationContractError("The confirmation receipt is not an object.", "unavailable");
@@ -63,22 +102,30 @@ function parseConfirmationReceipt(raw) {
63
102
  if (!isUuid(confirmationId) || channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL || typeof sentTo !== "string" || !/^[^\s@]{1,64}@[^\s@]{1,120}$/.test(sentTo) || typeof expiresAt !== "string" || !Number.isFinite(Date.parse(expiresAt)) || !Number.isSafeInteger(minutes) || minutes < 1 || minutes > 60 || !Number.isSafeInteger(triesAllowed) || triesAllowed < 1 || triesAllowed > 10) {
64
103
  throw new ConfirmationContractError("The confirmation receipt is not the one the server sends.", "unavailable");
65
104
  }
66
- return { confirmationId: confirmationId.toLowerCase(), channel, sentTo, expiresAt, minutes, triesAllowed };
105
+ const policy = readPolicyIdentity(raw);
106
+ if (!policy && hasPolicyIdentity(raw)) throw new ConfirmationContractError("The policy confirmation receipt is incomplete.", "unavailable");
107
+ const fields = { confirmationId: confirmationId.toLowerCase(), channel, sentTo, expiresAt, minutes, triesAllowed };
108
+ return policy ? { ...fields, ...policy } : fields;
67
109
  }
68
110
  function parseConfirmationRedeemed(raw) {
69
111
  const c = isRecord(raw) ? raw.confirmation : null;
70
112
  if (!isRecord(c) || !isUuid(c.id) || c.channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL) throw new ConfirmationContractError("That is not a redeemed confirmation.", "unavailable");
71
- return { confirmation: { id: c.id.toLowerCase(), channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL } };
113
+ const policy = readPolicyIdentity(c);
114
+ if (!policy && hasPolicyIdentity(c)) throw new ConfirmationContractError("The redeemed policy confirmation is incomplete.", "unavailable");
115
+ const fields = { id: c.id.toLowerCase(), channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL };
116
+ return { confirmation: policy ? { ...fields, ...policy } : fields };
72
117
  }
73
118
  var ON_THEIR_WORD_EVIDENCE_KEYS = ["agent", "quote", "mandate_digest", "review_digest", "channel", "confirmation"];
74
- var DIGEST = /^[a-f0-9]{64}$/;
75
119
  var CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f]/;
76
120
  function parseOnTheirWordEvidence(value) {
77
121
  if (!isRecord(value) || Object.keys(value).length !== ON_THEIR_WORD_EVIDENCE_KEYS.length) return null;
78
122
  const { agent, quote, mandate_digest: mandate, review_digest: review, channel, confirmation } = value;
79
123
  if (channel !== ON_THEIR_WORD_CHANNEL || typeof agent !== "string" || !isUuid(agent) || agent !== agent.toLowerCase() || typeof quote !== "string" || CONTROL.test(quote) || quote !== quote.trim() || quote.length > ON_THEIR_WORD_QUOTE_MAX || !/[\p{L}\p{N}]/u.test(quote) || typeof mandate !== "string" || !DIGEST.test(mandate) || typeof review !== "string" || !DIGEST.test(review)) return null;
80
- if (!isRecord(confirmation) || Object.keys(confirmation).length !== 2 || !isUuid(confirmation.id) || confirmation.id !== confirmation.id.toLowerCase() || confirmation.channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL) return null;
81
- return { agent, quote, mandate_digest: mandate, review_digest: review, channel, confirmation: { id: confirmation.id, channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL } };
124
+ if (!isRecord(confirmation) || !isUuid(confirmation.id) || confirmation.id !== confirmation.id.toLowerCase() || confirmation.channel !== ON_THEIR_WORD_CONFIRMATION_CHANNEL) return null;
125
+ const policy = readPolicyIdentity(confirmation);
126
+ if (policy ? Object.keys(confirmation).length !== 5 || confirmation.ledgerActionId !== policy.ledgerActionId : Object.keys(confirmation).length !== 2 || hasPolicyIdentity(confirmation)) return null;
127
+ const fields = { id: confirmation.id, channel: ON_THEIR_WORD_CONFIRMATION_CHANNEL };
128
+ return { agent, quote, mandate_digest: mandate, review_digest: review, channel, confirmation: policy ? { ...fields, ...policy } : fields };
82
129
  }
83
130
  var CONFIRMATION_REFUSALS = {
84
131
  RESONANCE_CONFIRMATION_COMMAND: { status: 400, failure: "invalid_request", message: "That confirmation request was malformed. Ask for a new code." },
@@ -128,4 +175,4 @@ function confirmationFailureOf(status, body) {
128
175
  return { failure: "unavailable", attemptsLeft: null, code: null, retryAfterSeconds: null };
129
176
  }
130
177
 
131
- export { CONFIRMATION_CODE_DIGITS, CONFIRMATION_CONTRACT, CONFIRMATION_LOCKS_PER_DAY, CONFIRMATION_MAIL_HEADER, CONFIRMATION_MAIL_HEADER_VALUE, CONFIRMATION_MAIL_SUBJECT_TAG, CONFIRMATION_MAX_TRIES, CONFIRMATION_MINUTES, CONFIRMATION_MIN_PLAN_MINUTES, CONFIRMATION_PER_DAY, CONFIRMATION_PER_HOUR, CONFIRMATION_PER_HOUR_ANY_PROJECT, CONFIRMATION_REDEEM_ROUTE, CONFIRMATION_REFUSALS, CONFIRMATION_REQUEST_ROUTE, CONFIRMATION_RETRY_MAX_SECONDS, ConfirmationContractError, ON_THEIR_WORD_CHANNEL, ON_THEIR_WORD_CONFIRMATION_CHANNEL, ON_THEIR_WORD_EVIDENCE_KEYS, ON_THEIR_WORD_QUOTE_MAX, confirmationFailureOf, confirmationRedeemPath, confirmationRequestPath, confirmationWaitWords, isConfirmationMail, parseConfirmationReceipt, parseConfirmationRedeemRequest, parseConfirmationRedeemed, parseOnTheirWordEvidence, readConfirmationCode };
178
+ export { CONFIRMATION_CLAIM_ROUTE, CONFIRMATION_CODE_DIGITS, CONFIRMATION_CONTRACT, CONFIRMATION_LOCKS_PER_DAY, CONFIRMATION_MAIL_HEADER, CONFIRMATION_MAIL_HEADER_VALUE, CONFIRMATION_MAIL_SUBJECT_TAG, CONFIRMATION_MAX_TRIES, CONFIRMATION_MINUTES, CONFIRMATION_MIN_PLAN_MINUTES, CONFIRMATION_PER_DAY, CONFIRMATION_PER_HOUR, CONFIRMATION_PER_HOUR_ANY_PROJECT, CONFIRMATION_REDEEM_ROUTE, CONFIRMATION_REFUSALS, CONFIRMATION_REQUEST_ROUTE, CONFIRMATION_RETRY_MAX_SECONDS, ConfirmationContractError, ON_THEIR_WORD_CHANNEL, ON_THEIR_WORD_CONFIRMATION_CHANNEL, ON_THEIR_WORD_EVIDENCE_KEYS, ON_THEIR_WORD_QUOTE_MAX, POLICY_CONFIRMATION_PURPOSE, confirmationClaimPath, confirmationFailureOf, confirmationQuoteCodeCandidates, confirmationQuoteContainsCode, confirmationRedeemPath, confirmationRequestPath, confirmationWaitWords, isConfirmationMail, parseConfirmationReceipt, parseConfirmationRedeemRequest, parseConfirmationRedeemed, parseOnTheirWordEvidence, parsePolicyConfirmationIssueRequest, readConfirmationCode };