@esseyonsui/sui-sdk-v3 0.3.2 → 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 (134) 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/network.d.ts +2 -0
  14. package/dist/network.d.ts.map +1 -1
  15. package/dist/network.js +17 -3
  16. package/dist/v3/collateral/bodies.d.ts +23 -14
  17. package/dist/v3/collateral/bodies.d.ts.map +1 -1
  18. package/dist/v3/collateral/bodies.js +42 -35
  19. package/dist/v3/collateral/ext.d.ts +18 -13
  20. package/dist/v3/collateral/ext.d.ts.map +1 -1
  21. package/dist/v3/collateral/ext.js +5 -6
  22. package/dist/v3/collateral/health.d.ts +16 -10
  23. package/dist/v3/collateral/health.d.ts.map +1 -1
  24. package/dist/v3/collateral/health.js +55 -30
  25. package/dist/v3/collateral/math.d.ts +0 -2
  26. package/dist/v3/collateral/math.d.ts.map +1 -1
  27. package/dist/v3/collateral/math.js +0 -5
  28. package/dist/v3/collateral/openings.d.ts +5 -1
  29. package/dist/v3/collateral/openings.d.ts.map +1 -1
  30. package/dist/v3/collateral/openings.js +7 -12
  31. package/dist/v3/credential/membership.d.ts +16 -2
  32. package/dist/v3/credential/membership.d.ts.map +1 -1
  33. package/dist/v3/credential/membership.js +36 -3
  34. package/dist/v3/domains.d.ts +9 -0
  35. package/dist/v3/domains.d.ts.map +1 -1
  36. package/dist/v3/domains.js +5 -0
  37. package/dist/v3/fees.d.ts +13 -1
  38. package/dist/v3/fees.d.ts.map +1 -1
  39. package/dist/v3/fees.js +32 -2
  40. package/dist/v3/funds.d.ts +34 -0
  41. package/dist/v3/funds.d.ts.map +1 -0
  42. package/dist/v3/funds.js +60 -0
  43. package/dist/v3/keeper-payload.d.ts +29 -7
  44. package/dist/v3/keeper-payload.d.ts.map +1 -1
  45. package/dist/v3/keeper-payload.js +33 -22
  46. package/dist/v3/kinds.d.ts +6 -6
  47. package/dist/v3/kinds.d.ts.map +1 -1
  48. package/dist/v3/kinds.js +5 -13
  49. package/dist/v3/loans/bodies.d.ts +40 -68
  50. package/dist/v3/loans/bodies.d.ts.map +1 -1
  51. package/dist/v3/loans/bodies.js +50 -73
  52. package/dist/v3/loans/offer-payload.d.ts +109 -0
  53. package/dist/v3/loans/offer-payload.d.ts.map +1 -0
  54. package/dist/v3/loans/offer-payload.js +207 -0
  55. package/dist/v3/loans/offer-plan.d.ts +8 -60
  56. package/dist/v3/loans/offer-plan.d.ts.map +1 -1
  57. package/dist/v3/loans/offer-plan.js +19 -95
  58. package/dist/v3/loans/open-plan.d.ts +15 -5
  59. package/dist/v3/loans/open-plan.d.ts.map +1 -1
  60. package/dist/v3/loans/open-plan.js +18 -7
  61. package/dist/v3/loans/reclaim-plan.d.ts +31 -15
  62. package/dist/v3/loans/reclaim-plan.d.ts.map +1 -1
  63. package/dist/v3/loans/reclaim-plan.js +63 -32
  64. package/dist/v3/loans/refi-plan.d.ts +8 -34
  65. package/dist/v3/loans/refi-plan.d.ts.map +1 -1
  66. package/dist/v3/loans/refi-plan.js +30 -111
  67. package/dist/v3/loans/repay-plan.d.ts.map +1 -1
  68. package/dist/v3/loans/repay-plan.js +2 -2
  69. package/dist/v3/loans/settle-plan.d.ts +88 -53
  70. package/dist/v3/loans/settle-plan.d.ts.map +1 -1
  71. package/dist/v3/loans/settle-plan.js +141 -150
  72. package/dist/v3/locks.d.ts +21 -0
  73. package/dist/v3/locks.d.ts.map +1 -0
  74. package/dist/v3/locks.js +15 -0
  75. package/dist/v3/memo.d.ts +14 -4
  76. package/dist/v3/memo.d.ts.map +1 -1
  77. package/dist/v3/memo.js +14 -7
  78. package/dist/v3/notes/derive.d.ts +5 -0
  79. package/dist/v3/notes/derive.d.ts.map +1 -1
  80. package/dist/v3/notes/derive.js +5 -0
  81. package/dist/v3/pace.d.ts +0 -1
  82. package/dist/v3/pace.d.ts.map +1 -1
  83. package/dist/v3/pace.js +0 -1
  84. package/dist/v3/pads.d.ts +15 -0
  85. package/dist/v3/pads.d.ts.map +1 -0
  86. package/dist/v3/pads.js +25 -0
  87. package/dist/v3/prover.d.ts +1 -1
  88. package/dist/v3/prover.d.ts.map +1 -1
  89. package/dist/v3/prover.js +1 -0
  90. package/dist/v3/relay/client.d.ts.map +1 -1
  91. package/dist/v3/relay/client.js +2 -2
  92. package/dist/v3/sales/auctioneer.d.ts +32 -27
  93. package/dist/v3/sales/auctioneer.d.ts.map +1 -1
  94. package/dist/v3/sales/auctioneer.js +67 -108
  95. package/dist/v3/sales/bid.d.ts +20 -49
  96. package/dist/v3/sales/bid.d.ts.map +1 -1
  97. package/dist/v3/sales/bid.js +28 -83
  98. package/dist/v3/sales/bodies.d.ts +4 -54
  99. package/dist/v3/sales/bodies.d.ts.map +1 -1
  100. package/dist/v3/sales/bodies.js +10 -52
  101. package/dist/v3/sales/claim.d.ts +0 -2
  102. package/dist/v3/sales/claim.d.ts.map +1 -1
  103. package/dist/v3/sales/claim.js +6 -8
  104. package/dist/v3/sales/index.d.ts +0 -1
  105. package/dist/v3/sales/index.d.ts.map +1 -1
  106. package/dist/v3/sales/index.js +0 -1
  107. package/dist/v3/sales/listing.d.ts +8 -6
  108. package/dist/v3/sales/listing.d.ts.map +1 -1
  109. package/dist/v3/sales/listing.js +27 -9
  110. package/dist/v3/sales/open-kind.d.ts +1 -3
  111. package/dist/v3/sales/open-kind.d.ts.map +1 -1
  112. package/dist/v3/sales/open-kind.js +2 -3
  113. package/dist/v3/sales/outcome.d.ts +5 -8
  114. package/dist/v3/sales/outcome.d.ts.map +1 -1
  115. package/dist/v3/sales/outcome.js +5 -9
  116. package/dist/v3/sales/package-gate.d.ts +3 -4
  117. package/dist/v3/sales/package-gate.d.ts.map +1 -1
  118. package/dist/v3/sales/package-gate.js +9 -7
  119. package/dist/v3/sales/rows.d.ts +9 -49
  120. package/dist/v3/sales/rows.d.ts.map +1 -1
  121. package/dist/v3/sales/rows.js +17 -91
  122. package/dist/v3/sales/seller.d.ts +2 -6
  123. package/dist/v3/sales/seller.d.ts.map +1 -1
  124. package/dist/v3/sales/seller.js +4 -11
  125. package/dist/v3/stage1.d.ts +8 -21
  126. package/dist/v3/stage1.d.ts.map +1 -1
  127. package/dist/v3/stage1.js +8 -76
  128. package/package.json +1 -1
  129. package/dist/v3/loans/lock-round.d.ts +0 -22
  130. package/dist/v3/loans/lock-round.d.ts.map +0 -1
  131. package/dist/v3/loans/lock-round.js +0 -20
  132. package/dist/v3/sales/reclaim.d.ts +0 -46
  133. package/dist/v3/sales/reclaim.d.ts.map +0 -1
  134. package/dist/v3/sales/reclaim.js +0 -57
@@ -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":"open-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/open-plan.ts"],"names":[],"mappings":"AAGA,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,eAAO,MAAM,aAAa,MAAM,CAAC;AACjC,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAenC,sHAAsH;AACtH,eAAO,MAAM,KAAK,GAAI,SAAS,MAAM,WAAkD,CAAC;AAExF,8EAA8E;AAC9E,eAAO,MAAM,YAAY,GAAI,SAAS,MAAM,0DAAiE,CAAC;AAE9G,MAAM,MAAM,QAAQ,GAAG;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,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
@@ -1 +1 @@
1
- {"version":3,"file":"reclaim-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/reclaim-plan.ts"],"names":[],"mappings":"AAOA,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAGjD,eAAO,MAAM,iBAAiB,MAAM,CAAC;AASrC,sFAAsF;AACtF,wBAAgB,OAAO,CAAC,IAAI,EAAE,UA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M,CAAC,CAAC;IACvB,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,UAAU,EAAE,CAAC;IACnB,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,iBAAiB;;;;;;;;;;;;;;;GAwDrD"}
@@ -2,11 +2,16 @@ import { keccak_256 } from "@noble/hashes/sha3.js";
2
2
  import { poseidonHash } from "@mysten/sui/zklogin";
3
3
  import { addrField } from "../addr-field.js";
4
4
  import { collLockPk } from "../collateral/openings.js";
5
- import { DOM_CNULL, DOM_LNULL, DOM_LOCK, R } from "../domains.js";
5
+ import { DOM_CNULL, R } from "../domains.js";
6
6
  import { feeWitness } from "../fee-witness.js";
7
- import { feeLane } from "../fees.js";
7
+ import { feeLane, flatFeeOf, flatFeeLane, minHiddenBase } from "../fees.js";
8
+ import { lock5 } from "../locks.js";
8
9
  import { noteCommitment, notePk } from "../notes/keys.js";
10
+ import { dummyNull } from "../pads.js";
9
11
  export const RECLAIM_COLL_KIND = 17n;
12
+ export const RECLAIM_VERSION = 4n;
13
+ export const RECLAIM_LEVELS = 20;
14
+ const NOW_STEP_S = 15n;
10
15
  const NO_ADDRESS = `0x${"0".repeat(64)}`;
11
16
  function uleb(n) {
12
17
  const out = [];
@@ -19,7 +24,17 @@ export function encHash(encs) {
19
24
  const bcs = Uint8Array.from([...uleb(encs.length), ...encs.flatMap((e) => [...uleb(e.length), ...e])]);
20
25
  return BigInt(`0x${Buffer.from(keccak_256(bcs)).toString("hex")}`) % R;
21
26
  }
22
- /** Witness for `circuits/v3/reclaim_coll/reclaim_coll.circom` (`ref.mjs`). Publics: lock, N, loan_key, outNote, hCtx. */
27
+ /** `lib/merkle.circom`: bit k of the index puts `path[k]` on the left. */
28
+ const rootOf = (leaf, index, path) => path.reduce((node, sib, k) => ((index >> BigInt(k)) & 1n ? poseidonHash([sib, node]) : poseidonHash([node, sib])), leaf);
29
+ const padLeaf = (p) => noteCommitment(0n, p.pk, p.blinding);
30
+ const padInputs = (pads, dummySeed) => ({
31
+ padPk0: pads[0].pk.toString(),
32
+ padBlind0: pads[0].blinding.toString(),
33
+ padPk1: pads[1].pk.toString(),
34
+ padBlind1: pads[1].blinding.toString(),
35
+ dummySeed: dummySeed.toString(),
36
+ });
37
+ /** Witness for `circuits/v3/reclaim_coll/reclaim_coll.circom` (freeze §3.7). Publics: lock, N, nDummy, loanKeyPub, hOut, hCtx. */
23
38
  export async function reclaimColl(p) {
24
39
  const lane = await feeLane({ ...p.fee, base: 0n, eph: p.eph });
25
40
  const outAmt = p.collAmt - lane.fee;
@@ -27,15 +42,16 @@ export async function reclaimColl(p) {
27
42
  throw new Error("the fee is above the collateral");
28
43
  const lock = noteCommitment(p.collAmt, collLockPk(p.loanKey, notePk(p.borrowerSk)), p.collBlind);
29
44
  const n = poseidonHash([DOM_CNULL, p.loanKey, lock]);
45
+ const nDummy = dummyNull(p.dummySeed);
30
46
  const outNote = noteCommitment(outAmt, p.out.pk, p.out.blinding);
47
+ const [pad0, pad1] = p.pads.map(padLeaf);
31
48
  const relayerField = addrField(NO_ADDRESS);
32
49
  const hash = encHash(p.encs);
50
+ const hOut = poseidonHash([RECLAIM_COLL_KIND, outNote, lane.fee_note, pad0, pad1]);
33
51
  const hCtx = poseidonHash([
34
52
  RECLAIM_COLL_KIND,
35
53
  p.loanKey,
36
- outNote,
37
54
  lane.fee_commit,
38
- lane.fee_note,
39
55
  lane.ephX,
40
56
  lane.ephY,
41
57
  lane.fee_ct,
@@ -54,68 +70,83 @@ export async function reclaimColl(p) {
54
70
  relayerField: relayerField.toString(),
55
71
  relayer_fee: "0",
56
72
  encHash: hash.toString(),
73
+ ...padInputs(p.pads, p.dummySeed),
57
74
  ...feeWitness(p.fee, 0n, p.eph, lane),
58
75
  };
59
76
  return {
60
77
  input,
61
- publics: [lock, n, p.loanKey, outNote, hCtx],
78
+ publics: [lock, n, nDummy, p.loanKey, hOut, hCtx],
62
79
  lock,
63
80
  n,
81
+ nDummy,
64
82
  outNote,
65
83
  outAmt,
84
+ pad0,
85
+ pad1,
66
86
  feeNote: lane.fee_note,
67
87
  ephX: lane.ephX,
68
88
  ephY: lane.ephY,
69
89
  feeCt: lane.fee_ct,
70
90
  };
71
91
  }
72
- const RECLAIM_VERSION = 3n;
73
- const LOCK_SLOTS = 16;
74
- /** Witness for `circuits/v3/reclaim/reclaim.circom`. Publics: hLock, residual, hCtx, N, hOut. */
92
+ /**
93
+ * Witness for `circuits/v3/reclaim/reclaim.circom` (freeze §3.6, D21): one planner for offer and bid locks. An
94
+ * early reclaim (`now_s < unlock_s`) pays the bond F, an on-time one 0. Publics: root, N, nDummy, hCtx, hOut.
95
+ */
75
96
  export async function reclaimLock(p) {
76
- const residual = p.residual ?? 0n;
77
- const onResidual = p.residual !== undefined && p.slot === LOCK_SLOTS;
78
- if (p.locks.length > LOCK_SLOTS || p.slot < 0 || (p.slot >= p.locks.length && !onResidual))
79
- throw new Error("the slot is not a lock");
80
- const locks = Array.from({ length: LOCK_SLOTS }, (_, i) => p.locks[i] ?? 0n);
81
- const lane = await feeLane({ ...p.fee, base: 0n, eph: p.eph });
82
- const outAmt = p.amount - lane.fee - p.relayerFee;
97
+ if (p.lockPath.length !== RECLAIM_LEVELS)
98
+ throw new Error("the lock path is not 20 levels");
99
+ if (p.nowS % NOW_STEP_S !== 0n)
100
+ throw new Error("now_s is not on the 15 s grid");
101
+ const early = p.nowS < p.unlockS;
102
+ if (early && p.amount < minHiddenBase(p.fee))
103
+ throw new Error("refused: a lock under 10 F cannot leave early; reclaim it after its unlock time");
104
+ const bond = flatFeeOf({ base: p.amount, bps: p.fee.bps, min: p.fee.min, max: p.fee.max, on: early }).fee;
105
+ const outAmt = p.amount - bond - p.relayerFee;
83
106
  if (outAmt < 0n)
84
- throw new Error("the fees are above the lock");
85
- const pk = poseidonHash([DOM_LOCK, p.field, notePk(p.ownerSk), poseidonHash([p.ns])]);
86
- const lock = noteCommitment(p.amount, pk, p.b);
87
- if ((onResidual ? residual : locks[p.slot]) !== lock)
88
- throw new Error("the slot does not hold this lock");
89
- const n = poseidonHash([DOM_LNULL, p.ns, lock]);
107
+ throw new Error("refused: the bond and the relayer fee are above the lock");
108
+ const ownerPk = notePk(p.ownerSk);
109
+ const l = lock5({ field: p.field, ownerPk, ns: p.ns, amount: p.amount, b: p.b, unlockS: p.unlockS });
110
+ if (rootOf(l.lock, p.lockIndex, p.lockPath) !== p.root)
111
+ throw new Error("the lock is not under the root");
112
+ const lane = await flatFeeLane({ ...p.fee, base: p.amount, eph: p.eph, on: early });
90
113
  const outNote = noteCommitment(outAmt, p.out.pk, p.out.blinding);
114
+ const [pad0, pad1] = p.pads.map(padLeaf);
115
+ const nDummy = dummyNull(p.dummySeed);
91
116
  const relayerField = addrField(p.relayer);
92
117
  const hash = encHash(p.encs);
93
- const s = (xs) => xs.map(String);
118
+ const hCtx = poseidonHash([RECLAIM_VERSION, lane.fee_commit, relayerField, p.relayerFee, p.nowS, hash]);
119
+ const hOut = poseidonHash([outNote, lane.fee_note, pad0, pad1, lane.ephX, lane.ephY, lane.fee_ct]);
94
120
  const input = {
95
- locks: s(locks),
96
- residual: residual.toString(),
97
- sel: s(Array.from({ length: LOCK_SLOTS + 1 }, (_, i) => (i === p.slot ? 1n : 0n))),
98
121
  field: p.field.toString(),
99
122
  amount: p.amount.toString(),
100
123
  ownerSk: p.ownerSk.toString(),
101
- nsEff: p.ns.toString(),
124
+ ns: p.ns.toString(),
102
125
  b: p.b.toString(),
126
+ unlock_s: p.unlockS.toString(),
127
+ lockIndex: p.lockIndex.toString(),
128
+ lockPath: p.lockPath.map(String),
129
+ root: p.root.toString(),
130
+ now_s: p.nowS.toString(),
103
131
  outAmt: outAmt.toString(),
104
132
  outPk: p.out.pk.toString(),
105
133
  outB: p.out.blinding.toString(),
134
+ ...padInputs(p.pads, p.dummySeed),
106
135
  relayerField: relayerField.toString(),
107
136
  relayer_fee: p.relayerFee.toString(),
108
137
  encHash: hash.toString(),
109
- ...feeWitness(p.fee, 0n, p.eph, lane),
138
+ ...feeWitness(p.fee, p.amount, p.eph, lane),
110
139
  };
111
- const hCtx = poseidonHash([p.field, lane.fee_commit, relayerField, p.relayerFee, RECLAIM_VERSION, hash]);
112
- const hOut = poseidonHash([outNote, lane.fee_note, lane.ephX, lane.ephY, lane.fee_ct]);
113
140
  return {
114
141
  input,
115
- publics: [poseidonHash(locks), residual, hCtx, n, hOut],
116
- n,
142
+ publics: [p.root, l.nullifier, nDummy, hCtx, hOut],
143
+ nLock: l.nullifier,
144
+ nDummy,
117
145
  outNote,
118
146
  outAmt,
147
+ bond,
148
+ pad0,
149
+ pad1,
119
150
  feeCommit: lane.fee_commit,
120
151
  feeNote: lane.fee_note,
121
152
  ephX: lane.ephX,
@@ -1,56 +1,29 @@
1
- import { type FeeCommitInput } from "../fees.ts";
2
1
  import type { CircuitInput } from "../inputs.ts";
3
2
  import type { KeeperTerms } from "../keeper-payload.ts";
4
- import { type SettleSlot } from "./settle-plan.ts";
3
+ import { type LockedParams } from "./settle-plan.ts";
5
4
  export declare const LOAN_REFI_KIND = 15;
6
- export type LoanRefiParams = {
7
- slots: SettleSlot[];
5
+ export type LoanRefiParams = LockedParams & {
8
6
  round: bigint;
9
- request: {
10
- principal: bigint;
11
- maxRateBps: bigint;
12
- termMs: bigint;
13
- borrowerPk: bigint;
14
- collAmt: bigint;
15
- collBlind: bigint;
16
- collNote: bigint;
17
- salt: bigint;
18
- };
19
7
  old: KeeperTerms;
20
- price: {
21
- pClaim: bigint;
22
- expo: bigint;
23
- cDec: bigint;
24
- lDec: bigint;
25
- ltvBps: bigint;
26
- };
27
- loanKey: bigint;
28
- borrowerNote: {
29
- pk: bigint;
30
- blinding: bigint;
31
- };
32
8
  pad: {
33
9
  pk: bigint;
34
10
  blinding: bigint;
35
11
  };
36
- e: bigint;
12
+ dummySeed: bigint;
37
13
  eP: bigint;
38
- nowS: bigint;
39
- extHash: bigint;
40
- fee: FeeCommitInput;
41
- eph: bigint;
42
14
  };
43
- /** Witness and Move arguments of `circuits/v3/loan_refi/loan_refi.circom` (`ref.mjs` loanRefiCase). */
15
+ /** Witness and Move arguments of `circuits/v3/loan_refi/loan_refi.circom` (freeze §3.2). */
44
16
  export declare function loanRefi(p: LoanRefiParams): Promise<{
45
17
  input: CircuitInput;
46
18
  publics: bigint[];
47
- winner: number;
48
19
  terms: KeeperTerms;
20
+ hRest: bigint;
49
21
  termsCommit: bigint;
50
22
  borrowerNote: bigint;
51
23
  borrowerValue: bigint;
52
24
  feeNote: bigint;
53
- winNull: bigint;
25
+ nLock: bigint;
26
+ nDummy: bigint;
54
27
  d: [bigint, bigint, bigint, bigint];
55
28
  ephX: bigint;
56
29
  ephY: bigint;
@@ -58,5 +31,6 @@ export declare function loanRefi(p: LoanRefiParams): Promise<{
58
31
  payoffNote: bigint;
59
32
  pad: bigint;
60
33
  ephP: [bigint, bigint];
34
+ priceDigest: bigint;
61
35
  }>;
62
36
  //# sourceMappingURL=refi-plan.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"refi-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/refi-plan.ts"],"names":[],"mappings":"AAiBA,OAAO,EAAgC,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAC/E,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAIxD,OAAO,EAAsC,KAAK,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEvF,eAAO,MAAM,cAAc,KAAK,CAAC;AAEjC,MAAM,MAAM,cAAc,GAAG;IAC3B,KAAK,EAAE,UAAU,EAAE,CAAC;IACpB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE;QACP,SAAS,EAAE,MAAM,CAAC;QAClB,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