@esseyonsui/sui-sdk-v3 0.3.3 → 0.4.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 (129) hide show
  1. package/dist/config.d.ts +3 -0
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js +1 -0
  4. package/dist/errors.d.ts +3 -1
  5. package/dist/errors.d.ts.map +1 -1
  6. package/dist/errors.js +5 -0
  7. package/dist/essey.d.ts +157 -131
  8. package/dist/essey.d.ts.map +1 -1
  9. package/dist/essey.js +641 -823
  10. package/dist/index.d.ts +1 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +1 -1
  13. package/dist/v3/collateral/bodies.d.ts +23 -14
  14. package/dist/v3/collateral/bodies.d.ts.map +1 -1
  15. package/dist/v3/collateral/bodies.js +42 -35
  16. package/dist/v3/collateral/ext.d.ts +18 -13
  17. package/dist/v3/collateral/ext.d.ts.map +1 -1
  18. package/dist/v3/collateral/ext.js +5 -6
  19. package/dist/v3/collateral/health.d.ts +16 -10
  20. package/dist/v3/collateral/health.d.ts.map +1 -1
  21. package/dist/v3/collateral/health.js +55 -30
  22. package/dist/v3/collateral/math.d.ts +0 -2
  23. package/dist/v3/collateral/math.d.ts.map +1 -1
  24. package/dist/v3/collateral/math.js +0 -5
  25. package/dist/v3/collateral/openings.d.ts +5 -1
  26. package/dist/v3/collateral/openings.d.ts.map +1 -1
  27. package/dist/v3/collateral/openings.js +7 -12
  28. package/dist/v3/credential/membership.d.ts +16 -2
  29. package/dist/v3/credential/membership.d.ts.map +1 -1
  30. package/dist/v3/credential/membership.js +36 -3
  31. package/dist/v3/domains.d.ts +9 -0
  32. package/dist/v3/domains.d.ts.map +1 -1
  33. package/dist/v3/domains.js +5 -0
  34. package/dist/v3/fees.d.ts +13 -1
  35. package/dist/v3/fees.d.ts.map +1 -1
  36. package/dist/v3/fees.js +32 -2
  37. package/dist/v3/funds.d.ts +34 -0
  38. package/dist/v3/funds.d.ts.map +1 -0
  39. package/dist/v3/funds.js +60 -0
  40. package/dist/v3/keeper-payload.d.ts +29 -7
  41. package/dist/v3/keeper-payload.d.ts.map +1 -1
  42. package/dist/v3/keeper-payload.js +33 -22
  43. package/dist/v3/kinds.d.ts +6 -6
  44. package/dist/v3/kinds.d.ts.map +1 -1
  45. package/dist/v3/kinds.js +5 -13
  46. package/dist/v3/loans/bodies.d.ts +40 -68
  47. package/dist/v3/loans/bodies.d.ts.map +1 -1
  48. package/dist/v3/loans/bodies.js +50 -73
  49. package/dist/v3/loans/offer-payload.d.ts +109 -0
  50. package/dist/v3/loans/offer-payload.d.ts.map +1 -0
  51. package/dist/v3/loans/offer-payload.js +207 -0
  52. package/dist/v3/loans/offer-plan.d.ts +8 -60
  53. package/dist/v3/loans/offer-plan.d.ts.map +1 -1
  54. package/dist/v3/loans/offer-plan.js +19 -95
  55. package/dist/v3/loans/open-plan.d.ts +15 -5
  56. package/dist/v3/loans/open-plan.d.ts.map +1 -1
  57. package/dist/v3/loans/open-plan.js +18 -7
  58. package/dist/v3/loans/reclaim-plan.d.ts +31 -15
  59. package/dist/v3/loans/reclaim-plan.d.ts.map +1 -1
  60. package/dist/v3/loans/reclaim-plan.js +63 -32
  61. package/dist/v3/loans/refi-plan.d.ts +8 -34
  62. package/dist/v3/loans/refi-plan.d.ts.map +1 -1
  63. package/dist/v3/loans/refi-plan.js +30 -111
  64. package/dist/v3/loans/repay-plan.d.ts.map +1 -1
  65. package/dist/v3/loans/repay-plan.js +2 -2
  66. package/dist/v3/loans/settle-plan.d.ts +88 -53
  67. package/dist/v3/loans/settle-plan.d.ts.map +1 -1
  68. package/dist/v3/loans/settle-plan.js +141 -150
  69. package/dist/v3/locks.d.ts +21 -0
  70. package/dist/v3/locks.d.ts.map +1 -0
  71. package/dist/v3/locks.js +15 -0
  72. package/dist/v3/memo.d.ts +14 -4
  73. package/dist/v3/memo.d.ts.map +1 -1
  74. package/dist/v3/memo.js +14 -7
  75. package/dist/v3/notes/derive.d.ts +5 -0
  76. package/dist/v3/notes/derive.d.ts.map +1 -1
  77. package/dist/v3/notes/derive.js +5 -0
  78. package/dist/v3/pace.d.ts +0 -1
  79. package/dist/v3/pace.d.ts.map +1 -1
  80. package/dist/v3/pace.js +0 -1
  81. package/dist/v3/pads.d.ts +15 -0
  82. package/dist/v3/pads.d.ts.map +1 -0
  83. package/dist/v3/pads.js +25 -0
  84. package/dist/v3/prover.d.ts +1 -1
  85. package/dist/v3/prover.d.ts.map +1 -1
  86. package/dist/v3/prover.js +1 -0
  87. package/dist/v3/sales/auctioneer.d.ts +32 -27
  88. package/dist/v3/sales/auctioneer.d.ts.map +1 -1
  89. package/dist/v3/sales/auctioneer.js +67 -108
  90. package/dist/v3/sales/bid.d.ts +20 -49
  91. package/dist/v3/sales/bid.d.ts.map +1 -1
  92. package/dist/v3/sales/bid.js +28 -83
  93. package/dist/v3/sales/bodies.d.ts +4 -54
  94. package/dist/v3/sales/bodies.d.ts.map +1 -1
  95. package/dist/v3/sales/bodies.js +10 -52
  96. package/dist/v3/sales/claim.d.ts +0 -2
  97. package/dist/v3/sales/claim.d.ts.map +1 -1
  98. package/dist/v3/sales/claim.js +6 -8
  99. package/dist/v3/sales/index.d.ts +0 -1
  100. package/dist/v3/sales/index.d.ts.map +1 -1
  101. package/dist/v3/sales/index.js +0 -1
  102. package/dist/v3/sales/listing.d.ts +8 -6
  103. package/dist/v3/sales/listing.d.ts.map +1 -1
  104. package/dist/v3/sales/listing.js +27 -9
  105. package/dist/v3/sales/open-kind.d.ts +1 -3
  106. package/dist/v3/sales/open-kind.d.ts.map +1 -1
  107. package/dist/v3/sales/open-kind.js +2 -3
  108. package/dist/v3/sales/outcome.d.ts +5 -8
  109. package/dist/v3/sales/outcome.d.ts.map +1 -1
  110. package/dist/v3/sales/outcome.js +5 -9
  111. package/dist/v3/sales/package-gate.d.ts +3 -4
  112. package/dist/v3/sales/package-gate.d.ts.map +1 -1
  113. package/dist/v3/sales/package-gate.js +9 -7
  114. package/dist/v3/sales/rows.d.ts +9 -49
  115. package/dist/v3/sales/rows.d.ts.map +1 -1
  116. package/dist/v3/sales/rows.js +17 -91
  117. package/dist/v3/sales/seller.d.ts +2 -6
  118. package/dist/v3/sales/seller.d.ts.map +1 -1
  119. package/dist/v3/sales/seller.js +4 -11
  120. package/dist/v3/stage1.d.ts +8 -21
  121. package/dist/v3/stage1.d.ts.map +1 -1
  122. package/dist/v3/stage1.js +8 -76
  123. package/package.json +1 -1
  124. package/dist/v3/loans/lock-round.d.ts +0 -22
  125. package/dist/v3/loans/lock-round.d.ts.map +0 -1
  126. package/dist/v3/loans/lock-round.js +0 -20
  127. package/dist/v3/sales/reclaim.d.ts +0 -46
  128. package/dist/v3/sales/reclaim.d.ts.map +0 -1
  129. package/dist/v3/sales/reclaim.js +0 -57
@@ -0,0 +1,207 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { createRequire } from "node:module";
3
+ import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";
4
+ import { secp256k1 } from "@noble/curves/secp256k1.js";
5
+ import { hkdf } from "@noble/hashes/hkdf.js";
6
+ import { sha256 } from "@noble/hashes/sha2.js";
7
+ import { poseidonHash } from "@mysten/sui/zklogin";
8
+ import { R } from "../domains.js";
9
+ import { treasuryPoint } from "../fees.js";
10
+ import { ENC_BYTES } from "../kinds.js";
11
+ import { bidBlind, lock5, offerBlind } from "../locks.js";
12
+ const require = createRequire(import.meta.url);
13
+ const { buildBabyjub } = require("circomlibjs");
14
+ const EPH = 33;
15
+ const NONCE = 24;
16
+ const MAC = 16;
17
+ export const BOX_PLAIN = ENC_BYTES - EPH - NONCE - MAC;
18
+ export const PAYLOAD_BYTES = 104;
19
+ const INDEX_MAX = 1n << 56n;
20
+ const U64 = (1n << 64n) - 1n;
21
+ const NOW_STEP_S = 15n;
22
+ /** Freeze §6.1: HKDF labels of the three new boxes. The ordinary scanner tries only the note labels, so skips them. */
23
+ export const INFO = { offer: "ess3/offer-enc/v1", lockRec: "ess3/lock-rec/v1" };
24
+ /** Freeze §6.1, in `pool::transfer` leaf order: out0 (the lock), out1 (change), fee_note, pad. */
25
+ export const SLOT = { partA: 0, change: 1, partB: 2, recovery: 3 };
26
+ let babyPromise;
27
+ const baby = () => (babyPromise ??= buildBabyjub());
28
+ const cat = (...parts) => Uint8Array.from(parts.flatMap((p) => [...p]));
29
+ const num = (b) => (b.length === 0 ? 0n : BigInt(`0x${Buffer.from(b).toString("hex")}`));
30
+ function be(x, n) {
31
+ if (x < 0n || x >= 1n << BigInt(8 * n))
32
+ throw new Error(`value does not fit ${n} bytes`);
33
+ return Uint8Array.from({ length: n }, (_, i) => Number((x >> BigInt(8 * (n - 1 - i))) & 0xffn));
34
+ }
35
+ const field = (x, what) => {
36
+ if (x < 0n || x >= R)
37
+ throw new Error(`${what} is not a field element`);
38
+ return be(x, 32);
39
+ };
40
+ const u64 = (x, what) => {
41
+ if (x < 0n || x > U64)
42
+ throw new Error(`${what} is not a u64`);
43
+ return be(x, 8);
44
+ };
45
+ /** ownerPk (32) ‖ ns (32) ‖ aux (32) ‖ unlock_s (u64), big-endian. aux is the packed enc point or the bid amount. */
46
+ export function packPayload(p) {
47
+ if (p.aux.length !== 32)
48
+ throw new Error("aux is not 32 bytes");
49
+ return cat(field(p.ownerPk, "ownerPk"), field(p.ns, "ns"), p.aux, u64(p.unlockS, "unlock_s"));
50
+ }
51
+ export function unpackPayload(p) {
52
+ if (p.length !== PAYLOAD_BYTES)
53
+ return null;
54
+ const ownerPk = num(p.subarray(0, 32));
55
+ const ns = num(p.subarray(32, 64));
56
+ if (ownerPk >= R || ns >= R)
57
+ return null;
58
+ return { ownerPk, ns, aux: p.slice(64, 96), unlockS: num(p.subarray(96, 104)) };
59
+ }
60
+ /** amount (u64) ‖ field (32) ‖ unlock_s (u64) ‖ note index (7): sealed to the owner's own note key, slot 3. */
61
+ export function packRecovery(r) {
62
+ if (r.index < 0n || r.index >= INDEX_MAX)
63
+ throw new Error("index does not fit 7 bytes");
64
+ return cat(u64(r.amount, "amount"), field(r.field, "field"), u64(r.unlockS, "unlock_s"), be(r.index, 7));
65
+ }
66
+ export function unpackRecovery(p) {
67
+ if (p.length !== BOX_PLAIN)
68
+ return null;
69
+ const f = num(p.subarray(8, 40));
70
+ if (f >= R)
71
+ return null;
72
+ return {
73
+ amount: num(p.subarray(0, 8)),
74
+ field: f,
75
+ unlockS: num(p.subarray(40, 48)),
76
+ index: num(p.subarray(48)),
77
+ };
78
+ }
79
+ /** circomlibjs `packPoint`: y little-endian, bit 255 set when x > (p-1)/2. */
80
+ export async function packEncPoint(x, y) {
81
+ const bj = await baby();
82
+ return Uint8Array.from(bj.packPoint([bj.F.e(x), bj.F.e(y)]));
83
+ }
84
+ /** On the curve, in the prime-order subgroup and canonically packed, or null. */
85
+ export async function unpackEncPoint(aux) {
86
+ const bj = await baby();
87
+ const P = bj.unpackPoint(Uint8Array.from(aux));
88
+ if (!P || !bj.inSubgroup(P))
89
+ return null;
90
+ const x = bj.F.toObject(P[0]);
91
+ const y = bj.F.toObject(P[1]);
92
+ if (x === 0n && y === 1n)
93
+ return null;
94
+ const again = await packEncPoint(x, y);
95
+ return again.every((b, i) => b === aux[i]) ? { x, y } : null;
96
+ }
97
+ export const packAmount = (amount) => cat(new Uint8Array(24), u64(amount, "amount"));
98
+ export const unpackAmount = (aux) => aux.subarray(0, 24).some((b) => b !== 0) ? null : num(aux.subarray(24));
99
+ const boxKey = (shared, info) => hkdf(sha256, shared, undefined, new TextEncoder().encode(info), 32);
100
+ const sharedX = (secret, pub) => secp256k1.getSharedSecret(secret, pub, true).slice(1);
101
+ /** `notes/opening.ts` box (eph ‖ nonce ‖ XChaCha20-Poly1305) over 55 raw bytes, under `info`. */
102
+ export function sealBox(plain, recvPub, info) {
103
+ if (plain.length !== BOX_PLAIN)
104
+ throw new Error("a box carries exactly 55 bytes");
105
+ const eph = secp256k1.utils.randomSecretKey();
106
+ const nonce = randomBytes(NONCE);
107
+ const ct = xchacha20poly1305(boxKey(sharedX(eph, recvPub), info), nonce).encrypt(plain);
108
+ return cat(secp256k1.getPublicKey(eph, true), nonce, ct);
109
+ }
110
+ /** Never throws. */
111
+ export function openBox(enc, recvSecret, info) {
112
+ if (enc.length !== ENC_BYTES)
113
+ return null;
114
+ try {
115
+ const key = boxKey(sharedX(recvSecret, enc.subarray(0, EPH)), info);
116
+ return xchacha20poly1305(key, enc.subarray(EPH, EPH + NONCE)).decrypt(enc.subarray(EPH + NONCE));
117
+ }
118
+ catch {
119
+ return null;
120
+ }
121
+ }
122
+ /** Part A is payload bytes 0..55; part B is bytes 55..104 then six zero bytes. */
123
+ function splitPayload(p) {
124
+ return [p.slice(0, BOX_PLAIN), cat(p.subarray(BOX_PLAIN), new Uint8Array(2 * BOX_PLAIN - PAYLOAD_BYTES))];
125
+ }
126
+ function joinPayload(a, b) {
127
+ if (a.length !== BOX_PLAIN || b.length !== BOX_PLAIN)
128
+ return null;
129
+ if (b.subarray(PAYLOAD_BYTES - BOX_PLAIN).some((x) => x !== 0))
130
+ return null;
131
+ return cat(a, b.subarray(0, PAYLOAD_BYTES - BOX_PLAIN));
132
+ }
133
+ /** The four encs of an offer or bid transfer in leaf order; slot 1 is the ordinary change box. */
134
+ export function lockEncs(p) {
135
+ const [a, b] = splitPayload(packPayload(p.payload));
136
+ return [
137
+ sealBox(a, p.counterpartyPub, INFO.offer),
138
+ p.changeEnc,
139
+ sealBox(b, p.counterpartyPub, INFO.offer),
140
+ sealBox(packRecovery(p.recovery), p.ownPub, INFO.lockRec),
141
+ ];
142
+ }
143
+ /** R1's per-offer keys from the reserved index's (sk, blinding), with Z1's blind (ruling R5, freeze §6.4). */
144
+ export async function offerKeys(sk, blinding) {
145
+ const ns = poseidonHash([sk, blinding, 3n]);
146
+ const encSk = poseidonHash([sk, blinding, 6n]) >> 2n;
147
+ const enc = await treasuryPoint(encSk);
148
+ return {
149
+ ownerPk: poseidonHash([sk]),
150
+ ns,
151
+ encSk,
152
+ encX: enc.tx,
153
+ encY: enc.ty,
154
+ b: offerBlind(ns, enc.tx, enc.ty),
155
+ };
156
+ }
157
+ /** Freeze §6.5: the first 15 s grid second strictly after the deadline (`settle_by_ms`, `finish_by_ms`). */
158
+ export const unlockAfter = (deadlineMs) => (deadlineMs / 1000n / NOW_STEP_S + 1n) * NOW_STEP_S;
159
+ /** Erik's condition 4, borrower side: an on-time reclaim must be impossible while a settle still is. */
160
+ export const acceptUnlock = (unlockS, deadlineMs) => unlockS * 1000n > deadlineMs;
161
+ const bytes = (enc) => Uint8Array.from(Buffer.from(enc.replace(/^0x/, ""), "hex"));
162
+ /**
163
+ * The borrower's (or seller's) scan: trial-decrypt every commitment with the request (or listing) key, join parts A
164
+ * and B (two leaves on), and keep a lock that rebuilds to the slot-0 leaf under `field` and `amount`, whose unlock
165
+ * passes `acceptUnlock`, and whose nullifier is unspent. `open` reads the aux field into the lock's amount and blind
166
+ * (`offerOpen`, `bidOpen`), so a lock whose blind does not bind its enc point never reaches a proof.
167
+ */
168
+ export async function scanLocks(p) {
169
+ const byIndex = new Map(p.commitments.map((c) => [c.index, c]));
170
+ const spent = new Set(p.nullifiers);
171
+ const out = [];
172
+ for (const c of p.commitments) {
173
+ const a = openBox(bytes(c.enc), p.recvSecret, INFO.offer);
174
+ const partB = a && byIndex.get(c.index + 2n);
175
+ const b = partB && openBox(bytes(partB.enc), p.recvSecret, INFO.offer);
176
+ const joined = a && b && joinPayload(a, b);
177
+ const payload = joined && unpackPayload(joined);
178
+ if (!payload || !acceptUnlock(payload.unlockS, p.deadlineMs))
179
+ continue;
180
+ const opened = await p.open(payload);
181
+ if (!opened)
182
+ continue;
183
+ const l = lock5({ field: p.field, ...payload, amount: opened.amount, b: opened.b });
184
+ if (l.lock !== c.commitment || spent.has(l.nullifier))
185
+ continue;
186
+ out.push({ ...payload, index: c.index, lock: l.lock, nullifier: l.nullifier });
187
+ }
188
+ return out;
189
+ }
190
+ /** An offer locks the request's principal, and its blind binds the enc point in aux (ruling R5). */
191
+ export const offerOpen = (principal) => async (p) => {
192
+ const enc = await unpackEncPoint(p.aux);
193
+ return enc && { amount: principal, b: offerBlind(p.ns, enc.x, enc.y) };
194
+ };
195
+ /** A bid locks the amount in aux, at least the posted price, under a blind over `ns` alone. */
196
+ export const bidOpen = (price) => async (p) => {
197
+ const amount = unpackAmount(p.aux);
198
+ return amount !== null && amount >= price ? { amount, b: bidBlind(p.ns) } : null;
199
+ };
200
+ /** The lender's (or buyer's) own locks: every slot-3 recovery record the note key opens, rebuilt from the seed. */
201
+ export function scanRecoveries(commitments, encKey) {
202
+ return commitments.flatMap((c) => {
203
+ const plain = openBox(bytes(c.enc), encKey, INFO.lockRec);
204
+ const r = plain && unpackRecovery(plain);
205
+ return r && c.index >= 3n ? [{ ...r, lockIndex: c.index - 3n }] : [];
206
+ });
207
+ }
@@ -1,66 +1,14 @@
1
- import type { CollLockSpend } from "../collateral/bodies.ts";
2
- import { type FeeCommitInput } from "../fees.ts";
3
- import type { CircuitInput, SpendNote } from "../inputs.ts";
4
1
  import { type RequestPlain } from "./open-plan.ts";
5
- export declare const OFFER_PLAIN_LEN = 208;
6
- export declare const LOAN_OFFER_KIND = 12;
7
- /** The fields `requestPlaintext` wrote, minus its coin-types hash. A legacy request's collateral opening is skipped. */
8
- export declare function decodeRequest(plain: Uint8Array): Omit<RequestPlain, "coinTypes">;
2
+ export type DecodedRequest = Omit<RequestPlain, "coinTypes" | "offerEncPub"> & {
3
+ offerEncPub: Uint8Array | null;
4
+ };
5
+ /**
6
+ * The fields `requestPlaintext` wrote, minus its coin-types hash. A legacy request's collateral opening is skipped,
7
+ * and only a version 2 request carries the key offers are sealed to (Z1 freeze §6.3).
8
+ */
9
+ export declare function decodeRequest(plain: Uint8Array): DecodedRequest;
9
10
  /** `membership::seal_listing(room, viewer).bind`. */
10
11
  export declare const sealListingBind: (room: string, viewer: string) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>;
11
- /** `loan::offer_key_id`: blake2b256("ess3/offer" ‖ bcs(loan_key) ‖ bcs(round) ‖ bcs(seq)). */
12
- export declare const offerKeyId: (loanKey: bigint, round: bigint, seq: bigint) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>;
13
- /** `loan::lock_field`. */
14
- export declare const lockField: (loanKey: bigint, round: bigint) => bigint;
15
- /** `loan::offer_room`: the id of 2^255 + lock_field. */
16
- export declare const offerRoom: (loanKey: bigint, round: bigint) => string;
17
- export declare const offerCommit: (rate: bigint, salt: bigint, lenderPk: bigint, encX: bigint, encY: bigint) => bigint;
18
- /** Lock4 of `lib/lock.circom`: the principal locked to the round's field and the lender. */
19
- export declare function lock4(field: bigint, ownerPk: bigint, ns: bigint, amount: bigint, b: bigint): bigint;
20
- export type OfferPlain = {
21
- rateBps: bigint;
22
- salt: bigint;
23
- lenderPk: bigint;
24
- encX: bigint;
25
- encY: bigint;
26
- ns: bigint;
27
- b: bigint;
28
- amount: bigint;
29
- };
30
- /** A-SPEC §4 offer `sealed`, 208 bytes little-endian. */
31
- export declare const offerPlaintext: (o: OfferPlain) => Uint8Array<ArrayBuffer>;
32
12
  /** `rows::loan_ext`: keccak256("ess3/lext/" ‖ kind ‖ loan ‖ pool_l ‖ bcs(round) ‖ relayer ‖ bcs(encs) ‖ extra) mod R. */
33
13
  export declare function loanExt(kind: number, loan: string, poolL: string, round: bigint, relayer: string, encs: Uint8Array[], extra: Uint8Array): bigint;
34
- export type LoanOfferParams = {
35
- root: bigint;
36
- ins: [SpendNote, SpendNote];
37
- request: {
38
- principal: bigint;
39
- maxRateBps: bigint;
40
- termMs: bigint;
41
- borrowerPk: bigint;
42
- collNote: bigint;
43
- salt: bigint;
44
- };
45
- loanKey: bigint;
46
- round: bigint;
47
- offer: OfferPlain;
48
- change: {
49
- pk: bigint;
50
- blinding: bigint;
51
- };
52
- relayerFee: bigint;
53
- extHash: bigint;
54
- fee: FeeCommitInput;
55
- eph: bigint;
56
- };
57
- export type LoanOffer = {
58
- input: CircuitInput;
59
- publics: bigint[];
60
- spend: CollLockSpend;
61
- commit: bigint;
62
- changeAmount: bigint;
63
- };
64
- /** Witness for `circuits/v3/loan_offer/loan_offer.circom` (`ref.mjs` loanOfferCase). Publics: lock, change, fee_note, hCtx, root, n0, n1. */
65
- export declare function loanOffer(p: LoanOfferParams): Promise<LoanOffer>;
66
14
  //# sourceMappingURL=offer-plan.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"offer-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/offer-plan.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAG7D,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAG5D,OAAO,EAKL,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAC;AAExB,eAAO,MAAM,eAAe,MAAM,CAAC;AACnC,eAAO,MAAM,eAAe,KAAK,CAAC;AA2BlC,wHAAwH;AACxH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE,WAAW,CAAC,CAgBhF;AAED,qDAAqD;AACrD,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,QAAQ,MAAM,0DACuB,CAAC;AAEpF,8FAA8F;AAC9F,eAAO,MAAM,UAAU,GAAI,SAAS,MAAM,EAAE,OAAO,MAAM,EAAE,KAAK,MAAM,0DACM,CAAC;AAE7E,0BAA0B;AAC1B,eAAO,MAAM,SAAS,GAAI,SAAS,MAAM,EAAE,OAAO,MAAM,WAA8C,CAAC;AAEvG,wDAAwD;AACxD,eAAO,MAAM,SAAS,GAAI,SAAS,MAAM,EAAE,OAAO,MAAM,WAC0B,CAAC;AAEnF,eAAO,MAAM,WAAW,GAAI,MAAM,MAAM,EAAE,MAAM,MAAM,EAAE,UAAU,MAAM,EAAE,MAAM,MAAM,EAAE,MAAM,MAAM,W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EAAE,MAAM,CAAA;KAAE,CAAC;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,SAAS,GAAG;IACtB,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,KAAK,EAAE,aAAa,CAAC;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,YAAY,EAAE,MAAM,CAAC;CACtB,CAAC;AAEF,6IAA6I;AAC7I,wBAAsB,SAAS,CAAC,CAAC,EAAE,eAAe,GAAG,OAAO,CAAC,SAAS,CAAC,CAoEtE"}
1
+ {"version":3,"file":"offer-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/offer-plan.ts"],"names":[],"mappings":"AAMA,OAAO,EAML,KAAK,YAAY,EAClB,MAAM,gBAAgB,CAAC;AAqCxB,MAAM,MAAM,cAAc,GAAG,IAAI,CAAC,YAAY,EAAE,WAAW,GAAG,aAAa,CAAC,GAAG;IAC7E,WAAW,EAAE,UAAU,GAAG,IAAI,CAAC;CAChC,CAAC;AAEF;;;GAGG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,UAAU,GAAG,cAAc,CAiB/D;AAED,qDAAqD;AACrD,eAAO,MAAM,eAAe,GAAI,MAAM,MAAM,EAAE,QAAQ,MAAM,0DACuB,CAAC;AAEpF,yHAAyH;AACzH,wBAAgB,OAAO,CACrB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,UAAU,EAAE,EAClB,KAAK,EAAE,UAAU,GAChB,MAAM,CAYR"}
@@ -1,13 +1,8 @@
1
1
  import { blake2b } from "@noble/hashes/blake2.js";
2
2
  import { keccak_256 } from "@noble/hashes/sha3.js";
3
- import { poseidonHash } from "@mysten/sui/zklogin";
4
- import { DOM_A1, DOM_LOCK, DOM_OFFER, DOM_ROUND, R } from "../domains.js";
5
- import { feeWitness } from "../fee-witness.js";
6
- import { feeLane } from "../fees.js";
7
- import { noteCommitment, noteNullifier, notePk } from "../notes/keys.js";
8
- import { REQ_LAYOUT_LEGACY, REQ_LAYOUT_VERSION, REQ_PLAIN_LEN, REQ_VERSION_AT, } from "./open-plan.js";
9
- export const OFFER_PLAIN_LEN = 208;
10
- export const LOAN_OFFER_KIND = 12;
3
+ import { secp256k1 } from "@noble/curves/secp256k1.js";
4
+ import { R } from "../domains.js";
5
+ import { REQ_LAYOUT_LEGACY, REQ_LAYOUT_VERSION, REQ_OFFER_KEY_AT, REQ_PLAIN_LEN, REQ_VERSION_AT, } from "./open-plan.js";
11
6
  const ascii = (s) => Uint8Array.from(Buffer.from(s, "ascii"));
12
7
  const blake = (bytes) => blake2b(bytes, { dkLen: 32 });
13
8
  const cat = (...parts) => Uint8Array.from(parts.flatMap((p) => [...p]));
@@ -31,12 +26,25 @@ function uleb(n) {
31
26
  out.push((n & 0x7f) | 0x80);
32
27
  return [...out, n];
33
28
  }
34
- /** The fields `requestPlaintext` wrote, minus its coin-types hash. A legacy request's collateral opening is skipped. */
29
+ function offerKeyOf(plain) {
30
+ const pub = plain.slice(REQ_OFFER_KEY_AT, REQ_OFFER_KEY_AT + 33);
31
+ try {
32
+ secp256k1.Point.fromBytes(pub);
33
+ }
34
+ catch {
35
+ throw new Error("the request's offer key is not a secp256k1 point");
36
+ }
37
+ return pub;
38
+ }
39
+ /**
40
+ * The fields `requestPlaintext` wrote, minus its coin-types hash. A legacy request's collateral opening is skipped,
41
+ * and only a version 2 request carries the key offers are sealed to (Z1 freeze §6.3).
42
+ */
35
43
  export function decodeRequest(plain) {
36
44
  if (plain.length !== REQ_PLAIN_LEN)
37
45
  throw new Error("the request plaintext is not 232 bytes");
38
46
  const version = plain[REQ_VERSION_AT];
39
- if (version !== REQ_LAYOUT_LEGACY && version !== REQ_LAYOUT_VERSION)
47
+ if (version !== REQ_LAYOUT_LEGACY && version !== 1 && version !== REQ_LAYOUT_VERSION)
40
48
  throw new Error(`the request uses layout version ${version}, which this SDK does not read: upgrade the SDK`);
41
49
  return {
42
50
  principal: readLe(plain, 0, 8),
@@ -46,97 +54,13 @@ export function decodeRequest(plain) {
46
54
  salt: readLe(plain, 96, 32),
47
55
  closeMs: readLe(plain, 128, 8),
48
56
  market: `0x${Buffer.from(plain.slice(136, 168)).toString("hex")}`,
57
+ offerEncPub: version === REQ_LAYOUT_VERSION ? offerKeyOf(plain) : null,
49
58
  };
50
59
  }
51
60
  /** `membership::seal_listing(room, viewer).bind`. */
52
61
  export const sealListingBind = (room, viewer) => blake(cat(ascii("essey-v3/seal/list"), addressBytes(room), addressBytes(viewer)));
53
- /** `loan::offer_key_id`: blake2b256("ess3/offer" ‖ bcs(loan_key) ‖ bcs(round) ‖ bcs(seq)). */
54
- export const offerKeyId = (loanKey, round, seq) => blake(cat(ascii("ess3/offer"), le(loanKey, 32), le(round, 8), le(seq, 8)));
55
- /** `loan::lock_field`. */
56
- export const lockField = (loanKey, round) => poseidonHash([DOM_ROUND, loanKey, round]);
57
- /** `loan::offer_room`: the id of 2^255 + lock_field. */
58
- export const offerRoom = (loanKey, round) => `0x${((1n << 255n) + lockField(loanKey, round)).toString(16).padStart(64, "0")}`;
59
- export const offerCommit = (rate, salt, lenderPk, encX, encY) => poseidonHash([DOM_OFFER, rate, salt, lenderPk, poseidonHash([encX, encY])]);
60
- /** Lock4 of `lib/lock.circom`: the principal locked to the round's field and the lender. */
61
- export function lock4(field, ownerPk, ns, amount, b) {
62
- return noteCommitment(amount, poseidonHash([DOM_LOCK, field, ownerPk, poseidonHash([ns])]), b);
63
- }
64
- /** A-SPEC §4 offer `sealed`, 208 bytes little-endian. */
65
- export const offerPlaintext = (o) => cat(le(o.rateBps, 8), le(o.salt, 32), le(o.lenderPk, 32), le(o.encX, 32), le(o.encY, 32), le(o.ns, 32), le(o.b, 32), le(o.amount, 8));
66
62
  /** `rows::loan_ext`: keccak256("ess3/lext/" ‖ kind ‖ loan ‖ pool_l ‖ bcs(round) ‖ relayer ‖ bcs(encs) ‖ extra) mod R. */
67
63
  export function loanExt(kind, loan, poolL, round, relayer, encs, extra) {
68
64
  const bytes = cat(ascii("ess3/lext/"), Uint8Array.of(kind), addressBytes(loan), addressBytes(poolL), le(round, 8), addressBytes(relayer), Uint8Array.from([...uleb(encs.length), ...encs.flatMap((e) => [...uleb(e.length), ...e])]), extra);
69
65
  return toBig(keccak_256(bytes)) % R;
70
66
  }
71
- /** Witness for `circuits/v3/loan_offer/loan_offer.circom` (`ref.mjs` loanOfferCase). Publics: lock, change, fee_note, hCtx, root, n0, n1. */
72
- export async function loanOffer(p) {
73
- const { request: q, offer: o } = p;
74
- if (o.amount !== q.principal)
75
- throw new Error("the offer locks the requested principal");
76
- const lane = await feeLane({ ...p.fee, base: 0n, eph: p.eph });
77
- const changeAmount = p.ins[0].amount + p.ins[1].amount - q.principal - lane.fee - p.relayerFee;
78
- if (changeAmount < 0n)
79
- throw new Error("inputs do not cover the principal");
80
- const commitments = p.ins.map((n) => noteCommitment(n.amount, notePk(n.sk), n.blinding));
81
- const [n0, n1] = p.ins.map((n, i) => noteNullifier(commitments[i], n.sk, n.pathIndex));
82
- const field = lockField(p.loanKey, p.round);
83
- const lock = lock4(field, o.lenderPk, o.ns, q.principal, o.b);
84
- const change = noteCommitment(changeAmount, p.change.pk, p.change.blinding);
85
- const commit = offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY);
86
- const reqCommit = poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]);
87
- const hCtx = poseidonHash([
88
- DOM_A1,
89
- reqCommit,
90
- field,
91
- commit,
92
- lane.fee_commit,
93
- lane.ephX,
94
- lane.ephY,
95
- lane.fee_ct,
96
- p.extHash,
97
- p.relayerFee,
98
- ]);
99
- const input = {
100
- root: p.root.toString(),
101
- n0: n0.toString(),
102
- n1: n1.toString(),
103
- inAmount: p.ins.map((n) => n.amount.toString()),
104
- inPrivateKey: p.ins.map((n) => n.sk.toString()),
105
- inBlinding: p.ins.map((n) => n.blinding.toString()),
106
- inPathIndices: p.ins.map((n) => n.pathIndex.toString()),
107
- inPathElements: p.ins.map((n) => n.pathElements.map(String)),
108
- principal: q.principal.toString(),
109
- max_rate_bps: q.maxRateBps.toString(),
110
- term_ms: q.termMs.toString(),
111
- borrowerPk: q.borrowerPk.toString(),
112
- collNote0: q.collNote.toString(),
113
- salt_req: q.salt.toString(),
114
- loanKey: p.loanKey.toString(),
115
- round: p.round.toString(),
116
- rate_bps: o.rateBps.toString(),
117
- salt: o.salt.toString(),
118
- lenderPk: o.lenderPk.toString(),
119
- encX: o.encX.toString(),
120
- encY: o.encY.toString(),
121
- ns: o.ns.toString(),
122
- b: o.b.toString(),
123
- cAmt: changeAmount.toString(),
124
- cPk: p.change.pk.toString(),
125
- cBlind: p.change.blinding.toString(),
126
- relayer_fee: p.relayerFee.toString(),
127
- extHash: p.extHash.toString(),
128
- ...feeWitness(p.fee, 0n, p.eph, lane),
129
- };
130
- const spend = {
131
- root: p.root,
132
- n0,
133
- n1,
134
- note: lock,
135
- change,
136
- feeNote: lane.fee_note,
137
- ephX: lane.ephX,
138
- ephY: lane.ephY,
139
- feeCt: lane.fee_ct,
140
- };
141
- return { input, publics: [lock, change, lane.fee_note, hCtx, p.root, n0, n1], spend, commit, changeAmount };
142
- }
@@ -20,8 +20,9 @@ export type OpenArgs = {
20
20
  export declare function openAction(loanKey: bigint, a: OpenArgs): bigint;
21
21
  /** 0 is the 0.1.8 layout, which carried the collateral opening at bytes 56..95 and no version. */
22
22
  export declare const REQ_LAYOUT_LEGACY = 0;
23
- export declare const REQ_LAYOUT_VERSION = 1;
23
+ export declare const REQ_LAYOUT_VERSION = 2;
24
24
  export declare const REQ_VERSION_AT: number;
25
+ export declare const REQ_OFFER_KEY_AT = 56;
25
26
  export type RequestPlain = {
26
27
  principal: bigint;
27
28
  maxRateBps: bigint;
@@ -31,12 +32,13 @@ export type RequestPlain = {
31
32
  closeMs: bigint;
32
33
  market: string;
33
34
  coinTypes: [string, string];
35
+ offerEncPub: Uint8Array;
34
36
  };
35
37
  /**
36
38
  * A-SPEC §4 `req_sealed`, 232 bytes little-endian. The spec names a "coin types hash" without its form; this SDK
37
- * takes blake2b256 of the collateral type, "/", then the loan type. PRV-03: bytes 56..95 stay zero, so a member who
38
- * reads the request never learns the collateral opening; lenders rely on the OPEN health proof
39
- * (`health.circom:270-281`), and the winner gets the opening in the settle delivery (`loan_settle.circom:213-214`).
39
+ * takes blake2b256 of the collateral type, "/", then the loan type. PRV-03: the collateral opening is never here;
40
+ * lenders rely on the OPEN health proof and the winner gets it in the settle delivery. Version 2 (Z1 freeze §6.3)
41
+ * puts the compressed key offers are sealed to at bytes 56..89, so the length says nothing new.
40
42
  */
41
43
  export declare function requestPlaintext(q: RequestPlain): Uint8Array;
42
44
  export type CollLockParams = {
@@ -56,6 +58,14 @@ export type CollLockParams = {
56
58
  extHash: bigint;
57
59
  fee: FeeCommitInput;
58
60
  eph: bigint;
61
+ /** Freeze ruling Q1: two zero notes under keys from reserved note indices. */
62
+ pads: [{
63
+ pk: bigint;
64
+ blinding: bigint;
65
+ }, {
66
+ pk: bigint;
67
+ blinding: bigint;
68
+ }];
59
69
  };
60
70
  export type CollLock = {
61
71
  input: CircuitInput;
@@ -63,6 +73,6 @@ export type CollLock = {
63
73
  spend: CollLockSpend;
64
74
  changeAmount: bigint;
65
75
  };
66
- /** Witness for `circuits/v3/coll_lock/coll_lock.circom`. Publics: lock, change, fee_note, hCtx, root, n0, n1. */
76
+ /** Witness for `circuits/v3/coll_lock/coll_lock.circom` (padded, lane C4). Publics: lock, hOut, hCtx, root, n0, n1. */
67
77
  export declare function collLock(p: CollLockParams): Promise<CollLock>;
68
78
  //# sourceMappingURL=open-plan.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"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EAAE,MAAM,CAAA;KAAE,CAAC;IACvD,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,8EAA8E;IAC9E,IAAI,EAAE,CAAC;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;CAC5E,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,aAAa,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9G,uHAAuH;AACvH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC,CA4DnE"}
@@ -28,21 +28,24 @@ export function openAction(loanKey, a) {
28
28
  }
29
29
  /** 0 is the 0.1.8 layout, which carried the collateral opening at bytes 56..95 and no version. */
30
30
  export const REQ_LAYOUT_LEGACY = 0;
31
- export const REQ_LAYOUT_VERSION = 1;
31
+ export const REQ_LAYOUT_VERSION = 2;
32
32
  export const REQ_VERSION_AT = REQ_PLAIN_LEN - 1;
33
+ export const REQ_OFFER_KEY_AT = 56;
33
34
  /**
34
35
  * A-SPEC §4 `req_sealed`, 232 bytes little-endian. The spec names a "coin types hash" without its form; this SDK
35
- * takes blake2b256 of the collateral type, "/", then the loan type. PRV-03: bytes 56..95 stay zero, so a member who
36
- * reads the request never learns the collateral opening; lenders rely on the OPEN health proof
37
- * (`health.circom:270-281`), and the winner gets the opening in the settle delivery (`loan_settle.circom:213-214`).
36
+ * takes blake2b256 of the collateral type, "/", then the loan type. PRV-03: the collateral opening is never here;
37
+ * lenders rely on the OPEN health proof and the winner gets it in the settle delivery. Version 2 (Z1 freeze §6.3)
38
+ * puts the compressed key offers are sealed to at bytes 56..89, so the length says nothing new.
38
39
  */
39
40
  export function requestPlaintext(q) {
40
- const body = cat(le(q.principal, 8), le(q.maxRateBps, 8), le(q.termMs, 8), le(q.borrowerPk, 32), new Uint8Array(40), le(q.salt, 32), le(q.closeMs, 8), addressBytes(q.market), blake(ascii(`${q.coinTypes[0]}/${q.coinTypes[1]}`)));
41
+ if (q.offerEncPub.length !== 33)
42
+ throw new Error("the offer key is not a compressed point");
43
+ const body = cat(le(q.principal, 8), le(q.maxRateBps, 8), le(q.termMs, 8), le(q.borrowerPk, 32), q.offerEncPub, new Uint8Array(7), le(q.salt, 32), le(q.closeMs, 8), addressBytes(q.market), blake(ascii(`${q.coinTypes[0]}/${q.coinTypes[1]}`)));
41
44
  const out = cat(body, new Uint8Array(REQ_PLAIN_LEN - body.length));
42
45
  out[REQ_VERSION_AT] = REQ_LAYOUT_VERSION;
43
46
  return out;
44
47
  }
45
- /** Witness for `circuits/v3/coll_lock/coll_lock.circom`. Publics: lock, change, fee_note, hCtx, root, n0, n1. */
48
+ /** Witness for `circuits/v3/coll_lock/coll_lock.circom` (padded, lane C4). Publics: lock, hOut, hCtx, root, n0, n1. */
46
49
  export async function collLock(p) {
47
50
  for (const note of p.ins)
48
51
  if (note.pathElements.length !== COLL_LOCK_LEVELS)
@@ -65,6 +68,8 @@ export async function collLock(p) {
65
68
  p.extHash,
66
69
  p.relayerFee,
67
70
  ]);
71
+ const [pad0, pad1] = p.pads.map((x) => noteCommitment(0n, x.pk, x.blinding));
72
+ const hOut = poseidonHash([DOM_CL, change, lane.fee_note, pad0, pad1]);
68
73
  const input = {
69
74
  root: p.root.toString(),
70
75
  n0: n0.toString(),
@@ -83,6 +88,10 @@ export async function collLock(p) {
83
88
  relayer_fee: p.relayerFee.toString(),
84
89
  loanKey: p.loanKey.toString(),
85
90
  extHash: p.extHash.toString(),
91
+ padPk0: p.pads[0].pk.toString(),
92
+ padBlind0: p.pads[0].blinding.toString(),
93
+ padPk1: p.pads[1].pk.toString(),
94
+ padBlind1: p.pads[1].blinding.toString(),
86
95
  ...feeWitness(p.fee, 0n, p.eph, lane),
87
96
  };
88
97
  const spend = {
@@ -91,10 +100,12 @@ export async function collLock(p) {
91
100
  n1,
92
101
  note: lock,
93
102
  change,
103
+ pad0,
104
+ pad1,
94
105
  feeNote: lane.fee_note,
95
106
  ephX: lane.ephX,
96
107
  ephY: lane.ephY,
97
108
  feeCt: lane.fee_ct,
98
109
  };
99
- return { input, publics: [lock, change, lane.fee_note, hCtx, p.root, n0, n1], spend, changeAmount };
110
+ return { input, publics: [lock, hOut, hCtx, p.root, n0, n1], spend, changeAmount };
100
111
  }
@@ -1,6 +1,12 @@
1
1
  import { type FeeCommitInput } from "../fees.ts";
2
2
  import type { CircuitInput } from "../inputs.ts";
3
3
  export declare const RECLAIM_COLL_KIND = 17n;
4
+ export declare const RECLAIM_VERSION = 4n;
5
+ export declare const RECLAIM_LEVELS = 20;
6
+ type PadKey = {
7
+ pk: bigint;
8
+ blinding: bigint;
9
+ };
4
10
  /** `rows::enc_hash`: keccak256 of bcs(vector<vector<u8>>), read big-endian, mod R. */
5
11
  export declare function encHash(encs: Uint8Array[]): bigint;
6
12
  export type ReclaimCollParams = {
@@ -8,58 +14,68 @@ export type ReclaimCollParams = {
8
14
  collAmt: bigint;
9
15
  borrowerSk: bigint;
10
16
  collBlind: bigint;
11
- out: {
12
- pk: bigint;
13
- blinding: bigint;
14
- };
17
+ out: PadKey;
18
+ pads: [PadKey, PadKey];
19
+ dummySeed: bigint;
15
20
  encs: Uint8Array[];
16
21
  fee: FeeCommitInput;
17
22
  eph: bigint;
18
23
  };
19
- /** Witness for `circuits/v3/reclaim_coll/reclaim_coll.circom` (`ref.mjs`). Publics: lock, N, loan_key, outNote, hCtx. */
24
+ /** Witness for `circuits/v3/reclaim_coll/reclaim_coll.circom` (freeze §3.7). Publics: lock, N, nDummy, loanKeyPub, hOut, hCtx. */
20
25
  export declare function reclaimColl(p: ReclaimCollParams): Promise<{
21
26
  input: CircuitInput;
22
27
  publics: bigint[];
23
28
  lock: bigint;
24
29
  n: bigint;
30
+ nDummy: bigint;
25
31
  outNote: bigint;
26
32
  outAmt: bigint;
33
+ pad0: bigint;
34
+ pad1: bigint;
27
35
  feeNote: bigint;
28
36
  ephX: bigint;
29
37
  ephY: bigint;
30
38
  feeCt: bigint;
31
39
  }>;
32
40
  export type ReclaimLockParams = {
33
- locks: bigint[];
34
- slot: number;
35
41
  field: bigint;
36
42
  amount: bigint;
37
43
  ownerSk: bigint;
38
44
  ns: bigint;
39
45
  b: bigint;
40
- out: {
41
- pk: bigint;
42
- blinding: bigint;
43
- };
46
+ unlockS: bigint;
47
+ lockIndex: bigint;
48
+ lockPath: bigint[];
49
+ root: bigint;
50
+ nowS: bigint;
51
+ out: PadKey;
52
+ pads: [PadKey, PadKey];
53
+ dummySeed: bigint;
44
54
  relayer: string;
45
55
  relayerFee: bigint;
46
56
  encs: Uint8Array[];
47
57
  fee: FeeCommitInput;
48
58
  eph: bigint;
49
- /** A sale room's `residual`, which slot 16 selects. A loan round has none. */
50
- residual?: bigint;
51
59
  };
52
- /** Witness for `circuits/v3/reclaim/reclaim.circom`. Publics: hLock, residual, hCtx, N, hOut. */
60
+ /**
61
+ * Witness for `circuits/v3/reclaim/reclaim.circom` (freeze §3.6, D21): one planner for offer and bid locks. An
62
+ * early reclaim (`now_s < unlock_s`) pays the bond F, an on-time one 0. Publics: root, N, nDummy, hCtx, hOut.
63
+ */
53
64
  export declare function reclaimLock(p: ReclaimLockParams): Promise<{
54
65
  input: CircuitInput;
55
66
  publics: bigint[];
56
- n: bigint;
67
+ nLock: bigint;
68
+ nDummy: bigint;
57
69
  outNote: bigint;
58
70
  outAmt: bigint;
71
+ bond: bigint;
72
+ pad0: bigint;
73
+ pad1: bigint;
59
74
  feeCommit: bigint;
60
75
  feeNote: bigint;
61
76
  ephX: bigint;
62
77
  ephY: bigint;
63
78
  feeCt: bigint;
64
79
  }>;
80
+ export {};
65
81
  //# sourceMappingURL=reclaim-plan.d.ts.map