@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.
- package/dist/config.d.ts +3 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +1 -0
- package/dist/errors.d.ts +3 -1
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +5 -0
- package/dist/essey.d.ts +157 -131
- package/dist/essey.d.ts.map +1 -1
- package/dist/essey.js +641 -823
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/network.d.ts +2 -0
- package/dist/network.d.ts.map +1 -1
- package/dist/network.js +17 -3
- package/dist/v3/collateral/bodies.d.ts +23 -14
- package/dist/v3/collateral/bodies.d.ts.map +1 -1
- package/dist/v3/collateral/bodies.js +42 -35
- package/dist/v3/collateral/ext.d.ts +18 -13
- package/dist/v3/collateral/ext.d.ts.map +1 -1
- package/dist/v3/collateral/ext.js +5 -6
- package/dist/v3/collateral/health.d.ts +16 -10
- package/dist/v3/collateral/health.d.ts.map +1 -1
- package/dist/v3/collateral/health.js +55 -30
- package/dist/v3/collateral/math.d.ts +0 -2
- package/dist/v3/collateral/math.d.ts.map +1 -1
- package/dist/v3/collateral/math.js +0 -5
- package/dist/v3/collateral/openings.d.ts +5 -1
- package/dist/v3/collateral/openings.d.ts.map +1 -1
- package/dist/v3/collateral/openings.js +7 -12
- package/dist/v3/credential/membership.d.ts +16 -2
- package/dist/v3/credential/membership.d.ts.map +1 -1
- package/dist/v3/credential/membership.js +36 -3
- package/dist/v3/domains.d.ts +9 -0
- package/dist/v3/domains.d.ts.map +1 -1
- package/dist/v3/domains.js +5 -0
- package/dist/v3/fees.d.ts +13 -1
- package/dist/v3/fees.d.ts.map +1 -1
- package/dist/v3/fees.js +32 -2
- package/dist/v3/funds.d.ts +34 -0
- package/dist/v3/funds.d.ts.map +1 -0
- package/dist/v3/funds.js +60 -0
- package/dist/v3/keeper-payload.d.ts +29 -7
- package/dist/v3/keeper-payload.d.ts.map +1 -1
- package/dist/v3/keeper-payload.js +33 -22
- package/dist/v3/kinds.d.ts +6 -6
- package/dist/v3/kinds.d.ts.map +1 -1
- package/dist/v3/kinds.js +5 -13
- package/dist/v3/loans/bodies.d.ts +40 -68
- package/dist/v3/loans/bodies.d.ts.map +1 -1
- package/dist/v3/loans/bodies.js +50 -73
- package/dist/v3/loans/offer-payload.d.ts +109 -0
- package/dist/v3/loans/offer-payload.d.ts.map +1 -0
- package/dist/v3/loans/offer-payload.js +207 -0
- package/dist/v3/loans/offer-plan.d.ts +8 -60
- package/dist/v3/loans/offer-plan.d.ts.map +1 -1
- package/dist/v3/loans/offer-plan.js +19 -95
- package/dist/v3/loans/open-plan.d.ts +15 -5
- package/dist/v3/loans/open-plan.d.ts.map +1 -1
- package/dist/v3/loans/open-plan.js +18 -7
- package/dist/v3/loans/reclaim-plan.d.ts +31 -15
- package/dist/v3/loans/reclaim-plan.d.ts.map +1 -1
- package/dist/v3/loans/reclaim-plan.js +63 -32
- package/dist/v3/loans/refi-plan.d.ts +8 -34
- package/dist/v3/loans/refi-plan.d.ts.map +1 -1
- package/dist/v3/loans/refi-plan.js +30 -111
- package/dist/v3/loans/repay-plan.d.ts.map +1 -1
- package/dist/v3/loans/repay-plan.js +2 -2
- package/dist/v3/loans/settle-plan.d.ts +88 -53
- package/dist/v3/loans/settle-plan.d.ts.map +1 -1
- package/dist/v3/loans/settle-plan.js +141 -150
- package/dist/v3/locks.d.ts +21 -0
- package/dist/v3/locks.d.ts.map +1 -0
- package/dist/v3/locks.js +15 -0
- package/dist/v3/memo.d.ts +14 -4
- package/dist/v3/memo.d.ts.map +1 -1
- package/dist/v3/memo.js +14 -7
- package/dist/v3/notes/derive.d.ts +5 -0
- package/dist/v3/notes/derive.d.ts.map +1 -1
- package/dist/v3/notes/derive.js +5 -0
- package/dist/v3/pace.d.ts +0 -1
- package/dist/v3/pace.d.ts.map +1 -1
- package/dist/v3/pace.js +0 -1
- package/dist/v3/pads.d.ts +15 -0
- package/dist/v3/pads.d.ts.map +1 -0
- package/dist/v3/pads.js +25 -0
- package/dist/v3/prover.d.ts +1 -1
- package/dist/v3/prover.d.ts.map +1 -1
- package/dist/v3/prover.js +1 -0
- package/dist/v3/relay/client.d.ts.map +1 -1
- package/dist/v3/relay/client.js +2 -2
- package/dist/v3/sales/auctioneer.d.ts +32 -27
- package/dist/v3/sales/auctioneer.d.ts.map +1 -1
- package/dist/v3/sales/auctioneer.js +67 -108
- package/dist/v3/sales/bid.d.ts +20 -49
- package/dist/v3/sales/bid.d.ts.map +1 -1
- package/dist/v3/sales/bid.js +28 -83
- package/dist/v3/sales/bodies.d.ts +4 -54
- package/dist/v3/sales/bodies.d.ts.map +1 -1
- package/dist/v3/sales/bodies.js +10 -52
- package/dist/v3/sales/claim.d.ts +0 -2
- package/dist/v3/sales/claim.d.ts.map +1 -1
- package/dist/v3/sales/claim.js +6 -8
- package/dist/v3/sales/index.d.ts +0 -1
- package/dist/v3/sales/index.d.ts.map +1 -1
- package/dist/v3/sales/index.js +0 -1
- package/dist/v3/sales/listing.d.ts +8 -6
- package/dist/v3/sales/listing.d.ts.map +1 -1
- package/dist/v3/sales/listing.js +27 -9
- package/dist/v3/sales/open-kind.d.ts +1 -3
- package/dist/v3/sales/open-kind.d.ts.map +1 -1
- package/dist/v3/sales/open-kind.js +2 -3
- package/dist/v3/sales/outcome.d.ts +5 -8
- package/dist/v3/sales/outcome.d.ts.map +1 -1
- package/dist/v3/sales/outcome.js +5 -9
- package/dist/v3/sales/package-gate.d.ts +3 -4
- package/dist/v3/sales/package-gate.d.ts.map +1 -1
- package/dist/v3/sales/package-gate.js +9 -7
- package/dist/v3/sales/rows.d.ts +9 -49
- package/dist/v3/sales/rows.d.ts.map +1 -1
- package/dist/v3/sales/rows.js +17 -91
- package/dist/v3/sales/seller.d.ts +2 -6
- package/dist/v3/sales/seller.d.ts.map +1 -1
- package/dist/v3/sales/seller.js +4 -11
- package/dist/v3/stage1.d.ts +8 -21
- package/dist/v3/stage1.d.ts.map +1 -1
- package/dist/v3/stage1.js +8 -76
- package/package.json +1 -1
- package/dist/v3/loans/lock-round.d.ts +0 -22
- package/dist/v3/loans/lock-round.d.ts.map +0 -1
- package/dist/v3/loans/lock-round.js +0 -20
- package/dist/v3/sales/reclaim.d.ts +0 -46
- package/dist/v3/sales/reclaim.d.ts.map +0 -1
- package/dist/v3/sales/reclaim.js +0 -57
|
@@ -1,54 +1,16 @@
|
|
|
1
1
|
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
-
import {
|
|
2
|
+
import { EsseyError } from "../../errors.js";
|
|
3
|
+
import { valueAt } from "../collateral/math.js";
|
|
4
|
+
import { termsOuter, termsRest } from "../collateral/openings.js";
|
|
5
|
+
import { DOM_A2, DOM_A2D, DOM_A2O, DOM_PRICE, DOM_TSALT, R } from "../domains.js";
|
|
3
6
|
import { feeWitness } from "../fee-witness.js";
|
|
4
|
-
import { ecdhBlind,
|
|
7
|
+
import { ecdhBlind, fieldAdd, flatFeeLane } from "../fees.js";
|
|
8
|
+
import { lock5, lockField, offerBlind } from "../locks.js";
|
|
5
9
|
import { noteCommitment } from "../notes/keys.js";
|
|
6
|
-
import {
|
|
7
|
-
export const SETTLE_SLOTS = 16;
|
|
10
|
+
import { dummyNull } from "../pads.js";
|
|
8
11
|
export const LOAN_SETTLE_KIND = 13;
|
|
12
|
+
export const LOCK_LEVELS = 20;
|
|
9
13
|
const INTEREST_DENOM = 315360000000n;
|
|
10
|
-
function readLe(bytes, at, size) {
|
|
11
|
-
let v = 0n;
|
|
12
|
-
for (let i = size - 1; i >= 0; i--)
|
|
13
|
-
v = (v << 8n) | BigInt(bytes[at + i]);
|
|
14
|
-
return v;
|
|
15
|
-
}
|
|
16
|
-
/** The 208-byte offer plaintext `offerPlaintext` writes. */
|
|
17
|
-
export function decodeOffer(plain) {
|
|
18
|
-
if (plain.length !== 208)
|
|
19
|
-
throw new Error("the offer plaintext is not 208 bytes");
|
|
20
|
-
return {
|
|
21
|
-
rateBps: readLe(plain, 0, 8),
|
|
22
|
-
salt: readLe(plain, 8, 32),
|
|
23
|
-
lenderPk: readLe(plain, 40, 32),
|
|
24
|
-
encX: readLe(plain, 72, 32),
|
|
25
|
-
encY: readLe(plain, 104, 32),
|
|
26
|
-
ns: readLe(plain, 136, 32),
|
|
27
|
-
b: readLe(plain, 168, 32),
|
|
28
|
-
amount: readLe(plain, 200, 8),
|
|
29
|
-
};
|
|
30
|
-
}
|
|
31
|
-
const BJJ_A = 168700n;
|
|
32
|
-
const BJJ_D = 168696n;
|
|
33
|
-
/** `membership::valid_point`. */
|
|
34
|
-
function validPoint(x, y) {
|
|
35
|
-
if (x >= R || y >= R || (x === 0n && y === 1n))
|
|
36
|
-
return false;
|
|
37
|
-
const x2 = (x * x) % R;
|
|
38
|
-
const y2 = (y * y) % R;
|
|
39
|
-
return (BJJ_A * x2 + y2) % R === (1n + BJJ_D * ((x2 * y2) % R)) % R;
|
|
40
|
-
}
|
|
41
|
-
/** `loan::opens`: an honest offer plaintext, the one `offer::slash` aborts 42_013 on. */
|
|
42
|
-
export function offerOpens(plain, field, commit, lock) {
|
|
43
|
-
if (plain.length !== 208)
|
|
44
|
-
return false;
|
|
45
|
-
const o = decodeOffer(plain);
|
|
46
|
-
if ([o.salt, o.lenderPk, o.encX, o.encY, o.ns, o.b].some((x) => x >= R))
|
|
47
|
-
return false;
|
|
48
|
-
return (offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY) === commit &&
|
|
49
|
-
validPoint(o.encX, o.encY) &&
|
|
50
|
-
lock4(field, o.lenderPk, o.ns, o.amount, o.b) === lock);
|
|
51
|
-
}
|
|
52
14
|
/** `crypto.mjs` interest: whole seconds of the term, and interest rounded up over 365 days of 10_000 bps. */
|
|
53
15
|
export function interest(principal, rateBps, termMs) {
|
|
54
16
|
const qTerm = termMs / 1000n;
|
|
@@ -61,87 +23,53 @@ export function interest(principal, rateBps, termMs) {
|
|
|
61
23
|
owed: principal + num / INTEREST_DENOM,
|
|
62
24
|
};
|
|
63
25
|
}
|
|
26
|
+
const refuse = (why) => {
|
|
27
|
+
throw new EsseyError("INVALID_ARGUMENT", `refused: ${why}; nothing was proved or sent`);
|
|
28
|
+
};
|
|
29
|
+
/** `lib/merkle.circom`: bit k of the index puts path[k] on the left. */
|
|
30
|
+
export function lockRoot(leaf, index, path) {
|
|
31
|
+
if (path.length !== LOCK_LEVELS)
|
|
32
|
+
throw new Error("the lock path is not 20 levels");
|
|
33
|
+
return path.reduce((node, sib, k) => ((index >> BigInt(k)) & 1n ? poseidonHash([sib, node]) : poseidonHash([node, sib])), leaf);
|
|
34
|
+
}
|
|
64
35
|
/**
|
|
65
|
-
*
|
|
66
|
-
*
|
|
36
|
+
* What settle and refinance share (freeze §3.1, §3.2): the lock opened under `field` at the posted principal and
|
|
37
|
+
* found at `root`, the posted-rate terms, the LTV line on `owed`, the flat fee and the collateral delivery.
|
|
38
|
+
* Each refusal is one the circuit has no witness for, so it is said before a proof is spent.
|
|
67
39
|
*/
|
|
68
|
-
export function
|
|
69
|
-
const
|
|
70
|
-
if (
|
|
71
|
-
|
|
72
|
-
const
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
const term = interest(q.principal, win.rateBps, q.termMs);
|
|
100
|
-
const maturityS = p.nowS + term.qTerm;
|
|
101
|
-
const tSalt = poseidonHash([DOM_TSALT, field, win.ns]);
|
|
102
|
-
const termsCommit = poseidonHash([
|
|
103
|
-
q.principal,
|
|
104
|
-
win.rateBps,
|
|
105
|
-
term.owed,
|
|
106
|
-
maturityS,
|
|
107
|
-
q.borrowerPk,
|
|
108
|
-
win.lenderPk,
|
|
109
|
-
poseidonHash([win.encX, win.encY]),
|
|
110
|
-
tSalt,
|
|
111
|
-
]);
|
|
112
|
-
const lane = await feeLane({ ...p.fee, base: q.principal, eph: p.eph });
|
|
113
|
-
const principalNote = noteCommitment(q.principal - lane.fee, p.borrowerNote.pk, p.borrowerNote.blinding);
|
|
114
|
-
const winNull = poseidonHash([DOM_LNULL, win.ns, slots[w].lock]);
|
|
115
|
-
const k = await ecdhBlind(win.encX, win.encY, p.e);
|
|
116
|
-
const ct1 = fieldAdd(q.collAmt, poseidonHash([k.sx, 1n]));
|
|
117
|
-
const ct2 = fieldAdd(q.collBlind, poseidonHash([k.sx, 2n]));
|
|
118
|
-
const reqCommit = poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]);
|
|
119
|
-
const d = [k.ephX, k.ephY, ct1, ct2];
|
|
120
|
-
const publics = [
|
|
121
|
-
poseidonHash(commits),
|
|
122
|
-
poseidonHash(locks),
|
|
123
|
-
poseidonHash([DOM_A2, reqCommit, field, actMask, p.nowS, lane.fee_commit, p.fee.version, p.extHash, 0n]),
|
|
124
|
-
poseidonHash([DOM_A2O, termsCommit, principalNote, lane.fee_note, winNull]),
|
|
125
|
-
poseidonHash([DOM_A2D, ...d, lane.ephX, lane.ephY, lane.fee_ct]),
|
|
40
|
+
export async function lockedTerms(p, field) {
|
|
41
|
+
const { request: q, offer: o, price } = p;
|
|
42
|
+
if (o.b !== offerBlind(o.ns, o.encX, o.encY))
|
|
43
|
+
refuse("the offer's lock blind does not bind its enc point");
|
|
44
|
+
const l = lock5({ field, ownerPk: o.lenderPk, ns: o.ns, amount: q.principal, b: o.b, unlockS: o.unlockS });
|
|
45
|
+
if (lockRoot(l.lock, p.lock.index, p.lock.path) !== p.lock.root)
|
|
46
|
+
refuse("the offer's lock is not at the root");
|
|
47
|
+
if (p.fee.bps !== 0n && q.principal < 10n * p.fee.min)
|
|
48
|
+
refuse("the principal is under 10 times the flat fee");
|
|
49
|
+
const term = interest(q.principal, q.maxRateBps, q.termMs);
|
|
50
|
+
const value = valueAt(q.collAmt, price.pClaim, { cDec: price.cDec, lDec: price.lDec, expo: price.expo });
|
|
51
|
+
if (term.owed * 10000n > value.q * price.ltvBps)
|
|
52
|
+
refuse("the debt is over the LTV line at the claimed price");
|
|
53
|
+
const terms = {
|
|
54
|
+
principal: q.principal,
|
|
55
|
+
rateBps: q.maxRateBps,
|
|
56
|
+
owed: term.owed,
|
|
57
|
+
maturityS: p.nowS + term.qTerm,
|
|
58
|
+
lenderPk: o.lenderPk,
|
|
59
|
+
lenderEncX: o.encX,
|
|
60
|
+
lenderEncY: o.encY,
|
|
61
|
+
salt: poseidonHash([DOM_TSALT, field, o.ns]),
|
|
62
|
+
};
|
|
63
|
+
const hRest = termsRest(terms);
|
|
64
|
+
const lane = await flatFeeLane({ ...p.fee, base: q.principal, eph: p.eph });
|
|
65
|
+
const k = await ecdhBlind(o.encX, o.encY, p.e);
|
|
66
|
+
const d = [
|
|
67
|
+
k.ephX,
|
|
68
|
+
k.ephY,
|
|
69
|
+
fieldAdd(q.collAmt, poseidonHash([k.sx, 1n])),
|
|
70
|
+
fieldAdd(q.collBlind, poseidonHash([k.sx, 2n])),
|
|
126
71
|
];
|
|
127
|
-
const s = (xs) => xs.map(String);
|
|
128
72
|
const input = {
|
|
129
|
-
commits: s(commits),
|
|
130
|
-
locks: s(locks),
|
|
131
|
-
rate: s(opened((o) => o.rateBps)),
|
|
132
|
-
salt: s(opened((o) => o.salt)),
|
|
133
|
-
lenderPk: s(opened((o) => o.lenderPk)),
|
|
134
|
-
encX: s(opened((o) => o.encX)),
|
|
135
|
-
encY: s(opened((o) => o.encY)),
|
|
136
|
-
actMask: actMask.toString(),
|
|
137
|
-
winner: String(w),
|
|
138
|
-
winRate: win.rateBps.toString(),
|
|
139
|
-
ns_w: win.ns.toString(),
|
|
140
|
-
b_w: win.b.toString(),
|
|
141
|
-
q_term: term.qTerm.toString(),
|
|
142
|
-
r_term: term.rTerm.toString(),
|
|
143
|
-
q_intr: term.qIntr.toString(),
|
|
144
|
-
r_intr: term.rIntr.toString(),
|
|
145
73
|
principal: q.principal.toString(),
|
|
146
74
|
max_rate_bps: q.maxRateBps.toString(),
|
|
147
75
|
term_ms: q.termMs.toString(),
|
|
@@ -150,39 +78,101 @@ export async function loanSettle(p) {
|
|
|
150
78
|
collBlind: q.collBlind.toString(),
|
|
151
79
|
salt_req: q.salt.toString(),
|
|
152
80
|
loanKey: p.loanKey.toString(),
|
|
81
|
+
lenderPk: o.lenderPk.toString(),
|
|
82
|
+
ns: o.ns.toString(),
|
|
83
|
+
b: o.b.toString(),
|
|
84
|
+
unlock_s: o.unlockS.toString(),
|
|
85
|
+
encX: o.encX.toString(),
|
|
86
|
+
encY: o.encY.toString(),
|
|
87
|
+
lockIndex: p.lock.index.toString(),
|
|
88
|
+
lockPath: p.lock.path.map(String),
|
|
89
|
+
root: p.lock.root.toString(),
|
|
153
90
|
pkB: p.borrowerNote.pk.toString(),
|
|
154
91
|
blindB: p.borrowerNote.blinding.toString(),
|
|
155
92
|
e: p.e.toString(),
|
|
93
|
+
q_term: term.qTerm.toString(),
|
|
94
|
+
r_term: term.rTerm.toString(),
|
|
95
|
+
q_intr: term.qIntr.toString(),
|
|
96
|
+
r_intr: term.rIntr.toString(),
|
|
156
97
|
now_s: p.nowS.toString(),
|
|
98
|
+
pClaim: price.pClaim.toString(),
|
|
99
|
+
expo: price.expo.toString(),
|
|
100
|
+
c_dec: price.cDec.toString(),
|
|
101
|
+
l_dec: price.lDec.toString(),
|
|
102
|
+
ltv_bps: price.ltvBps.toString(),
|
|
103
|
+
q_v: value.q.toString(),
|
|
104
|
+
r_v: value.r.toString(),
|
|
157
105
|
extHash: p.extHash.toString(),
|
|
158
106
|
relayer_fee: "0",
|
|
159
107
|
...feeWitness(p.fee, q.principal, p.eph, lane),
|
|
160
108
|
};
|
|
109
|
+
return {
|
|
110
|
+
input,
|
|
111
|
+
terms,
|
|
112
|
+
hRest,
|
|
113
|
+
termsCommit: termsOuter(terms, q.borrowerPk, hRest),
|
|
114
|
+
lane,
|
|
115
|
+
d,
|
|
116
|
+
nLock: l.nullifier,
|
|
117
|
+
reqCommit: poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]),
|
|
118
|
+
priceDigest: poseidonHash([DOM_PRICE, price.pClaim, price.expo, price.cDec, price.lDec, price.ltvBps]),
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
/** Witness and Move arguments of `circuits/v3/loan_settle/loan_settle.circom` (freeze §3.1), round 0. */
|
|
122
|
+
export async function loanSettle(p) {
|
|
123
|
+
const field = lockField(p.loanKey, 0n);
|
|
124
|
+
const t = await lockedTerms(p, field);
|
|
125
|
+
const { lane } = t;
|
|
126
|
+
const principalValue = p.request.principal - lane.fee;
|
|
127
|
+
const principalNote = noteCommitment(principalValue, p.borrowerNote.pk, p.borrowerNote.blinding);
|
|
128
|
+
const pads = p.pads.map((x) => noteCommitment(0n, x.pk, x.blinding));
|
|
129
|
+
const nDummy = dummyNull(p.dummySeed);
|
|
130
|
+
const hCtx = poseidonHash([
|
|
131
|
+
DOM_A2,
|
|
132
|
+
t.reqCommit,
|
|
133
|
+
field,
|
|
134
|
+
p.nowS,
|
|
135
|
+
lane.fee_commit,
|
|
136
|
+
p.fee.version,
|
|
137
|
+
p.extHash,
|
|
138
|
+
0n,
|
|
139
|
+
t.priceDigest,
|
|
140
|
+
]);
|
|
141
|
+
const publics = [
|
|
142
|
+
p.lock.root,
|
|
143
|
+
t.nLock,
|
|
144
|
+
nDummy,
|
|
145
|
+
hCtx,
|
|
146
|
+
poseidonHash([DOM_A2O, t.termsCommit, principalNote, lane.fee_note, pads[0], pads[1]]),
|
|
147
|
+
poseidonHash([DOM_A2D, ...t.d, lane.ephX, lane.ephY, lane.fee_ct]),
|
|
148
|
+
];
|
|
149
|
+
const input = {
|
|
150
|
+
...t.input,
|
|
151
|
+
padPk0: p.pads[0].pk.toString(),
|
|
152
|
+
padBlind0: p.pads[0].blinding.toString(),
|
|
153
|
+
padPk1: p.pads[1].pk.toString(),
|
|
154
|
+
padBlind1: p.pads[1].blinding.toString(),
|
|
155
|
+
dummySeed: p.dummySeed.toString(),
|
|
156
|
+
};
|
|
161
157
|
if (publics.some((x) => x >= R))
|
|
162
158
|
throw new Error("a public signal is not a field element");
|
|
163
159
|
return {
|
|
164
160
|
input,
|
|
165
161
|
publics,
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
rateBps: win.rateBps,
|
|
170
|
-
owed: term.owed,
|
|
171
|
-
maturityS,
|
|
172
|
-
lenderPk: win.lenderPk,
|
|
173
|
-
lenderEncX: win.encX,
|
|
174
|
-
lenderEncY: win.encY,
|
|
175
|
-
salt: tSalt,
|
|
176
|
-
},
|
|
177
|
-
termsCommit,
|
|
162
|
+
terms: t.terms,
|
|
163
|
+
hRest: t.hRest,
|
|
164
|
+
termsCommit: t.termsCommit,
|
|
178
165
|
principalNote,
|
|
179
|
-
principalValue
|
|
180
|
-
|
|
181
|
-
|
|
166
|
+
principalValue,
|
|
167
|
+
nLock: t.nLock,
|
|
168
|
+
nDummy,
|
|
169
|
+
pads,
|
|
170
|
+
d: t.d,
|
|
182
171
|
feeNote: lane.fee_note,
|
|
183
172
|
ephX: lane.ephX,
|
|
184
173
|
ephY: lane.ephY,
|
|
185
174
|
feeCt: lane.fee_ct,
|
|
175
|
+
priceDigest: t.priceDigest,
|
|
186
176
|
};
|
|
187
177
|
}
|
|
188
178
|
const ascii = (s) => Uint8Array.from(Buffer.from(s, "ascii"));
|
|
@@ -201,31 +191,32 @@ function uleb(n) {
|
|
|
201
191
|
}
|
|
202
192
|
const bcsBytes = (b) => cat(uleb(b.length), b);
|
|
203
193
|
const addressBytes = (id) => le(BigInt(id), 32).reverse();
|
|
194
|
+
const fields = (xs) => cat(uleb(xs.length), ...xs.map((x) => le(x, 32)));
|
|
195
|
+
const blobs = (xs) => cat(uleb(xs.length), ...xs.map(bcsBytes));
|
|
196
|
+
const words = (xs) => cat(...xs.map((x) => le(x, 32)));
|
|
204
197
|
/** `loan::action_hash`: blake2b256("ess3/act/" ‖ kind ‖ "/" ‖ bcs(loan_key) ‖ bcs(nonce) ‖ args) mod R. */
|
|
205
198
|
export function actionHash(blake, loanKey, nonce, kind, args) {
|
|
206
199
|
const digest = blake(cat(ascii(`ess3/act/${kind}/`), le(loanKey, 32), le(nonce, 8), args));
|
|
207
200
|
return BigInt(`0x${Buffer.from(digest).toString("hex")}`) % R;
|
|
208
201
|
}
|
|
209
|
-
/**
|
|
202
|
+
/**
|
|
203
|
+
* `settle::settle_args` (Z1): every argument in declaration order, shared objects by id, the reading as the bcs of
|
|
204
|
+
* the struct (`readingBytes`), vectors and the proof as bcs; the pass and the clock left out.
|
|
205
|
+
*/
|
|
210
206
|
export function settleArgs(a) {
|
|
211
|
-
return cat(addressBytes(a.loan), addressBytes(a.poolL),
|
|
207
|
+
return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), words([a.root, a.nLock, a.nDummy, a.termsCommit, a.principalNote, a.feeNote, a.pad0, a.pad1]), fields(a.d), words([a.ephX, a.ephY, a.feeCt]), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), le(a.nowS, 8), bcsBytes(a.memo));
|
|
212
208
|
}
|
|
213
|
-
/** `
|
|
214
|
-
export const
|
|
215
|
-
/** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request as bcs, `close_ms`. */
|
|
216
|
-
export const openRefiArgs = (a) => cat(addressBytes(a.loan), addressBytes(a.poolL), le(a.reqCommit, 32), bcsBytes(a.reqSealed), le(a.closeMs, 8));
|
|
209
|
+
/** `refi::open_refi_args`: the loan and pool by id, `req_commit`, the sealed request and memo as bcs, `close_ms`. */
|
|
210
|
+
export const openRefiArgs = (a) => cat(addressBytes(a.loan), addressBytes(a.poolL), le(a.reqCommit, 32), bcsBytes(a.reqSealed), bcsBytes(a.memo), le(a.closeMs, 8));
|
|
217
211
|
/** `liquidation::topup_args`: every `topup_by_borrower` argument after the pass, in order, by `settle_args`' rule. */
|
|
218
212
|
export function topupArgs(a) {
|
|
219
|
-
|
|
220
|
-
return cat(addressBytes(a.loan), addressBytes(a.poolC), addressBytes(a.market), bcsBytes(a.lockProof), ...a.fields.map((x) => le(x, 32)), bcsBytes(a.encChange), bcsBytes(a.encFee), bcsBytes(a.keeperCt), bcsBytes(a.proof), fields(a.outs), fields(a.delivery), cat(uleb(a.encs.length), ...a.encs.map(bcsBytes)));
|
|
213
|
+
return cat(addressBytes(a.loan), addressBytes(a.poolC), addressBytes(a.market), bcsBytes(a.lockProof), ...a.fields.map((x) => le(x, 32)), bcsBytes(a.keeperCt), bcsBytes(a.proof), fields(a.outs), fields(a.delivery), blobs(a.encs));
|
|
221
214
|
}
|
|
222
215
|
/** `refi::cancel_refi_args`: the loan and pool by id. */
|
|
223
216
|
export const cancelRefiArgs = (loan, poolL) => cat(addressBytes(loan), addressBytes(poolL));
|
|
224
|
-
/** `refi::settle_refi_args
|
|
217
|
+
/** `refi::settle_refi_args` (Z1): `settle_args`' rule with the payoff pair, the pad and one `encs` of 4. */
|
|
225
218
|
export function settleRefiArgs(a) {
|
|
226
|
-
|
|
227
|
-
const blobs = (xs) => cat(uleb(xs.length), ...xs.map(bcsBytes));
|
|
228
|
-
return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), ...[a.termsCommit, a.borrowerNote, a.feeNote, a.winNull].map((x) => le(x, 32)), fields(a.d), ...[a.ephX, a.ephY, a.feeCt, a.payoffNote, a.pad].map((x) => le(x, 32)), fields(a.ephP), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), blobs(a.encsP), le(a.nowS, 8), bcsBytes(a.memo));
|
|
219
|
+
return cat(addressBytes(a.loan), addressBytes(a.poolL), addressBytes(a.market), a.reading, bcsBytes(a.proof), words([a.root, a.nLock, a.nDummy, a.termsCommit, a.borrowerNote, a.feeNote]), fields(a.d), words([a.ephX, a.ephY, a.feeCt, a.payoffNote, a.pad]), fields(a.ephP), le(a.pClaim, 8), bcsBytes(a.keeperCt), blobs(a.encs), le(a.nowS, 8), bcsBytes(a.memo));
|
|
229
220
|
}
|
|
230
221
|
/** The bcs of `price::Reading`: the oracle type name, the feed id, spot, EMA, the exponent and `publish_s`. */
|
|
231
222
|
export const readingBytes = (r) => cat(bcsBytes(ascii(r.oracle)), addressBytes(r.feedId), le(r.spot, 8), le(r.ema, 8), Uint8Array.of(Number(r.expo)), le(r.publishS, 8));
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export type Lock5 = {
|
|
2
|
+
field: bigint;
|
|
3
|
+
ownerPk: bigint;
|
|
4
|
+
ns: bigint;
|
|
5
|
+
amount: bigint;
|
|
6
|
+
b: bigint;
|
|
7
|
+
unlockS: bigint;
|
|
8
|
+
};
|
|
9
|
+
/** `loan::lock_field`: a loan round's lock field. A bid's field is the room's `sale_key`. */
|
|
10
|
+
export declare const lockField: (loanKey: bigint, round: bigint) => bigint;
|
|
11
|
+
/** `lib/lock.circom` Lock5: a note shape, so js4's first output with `outPk0 = lockPk` is the leaf. */
|
|
12
|
+
export declare function lock5(l: Lock5): {
|
|
13
|
+
lockPk: bigint;
|
|
14
|
+
lock: bigint;
|
|
15
|
+
nullifier: bigint;
|
|
16
|
+
};
|
|
17
|
+
/** Ruling R5: an offer's blind binds the lender's enc point (`loan_settle.circom`, `loan_refi.circom`). */
|
|
18
|
+
export declare const offerBlind: (ns: bigint, encX: bigint, encY: bigint) => bigint;
|
|
19
|
+
/** A bid's blind binds `ns` alone: no ECDH reads a bid key. */
|
|
20
|
+
export declare const bidBlind: (ns: bigint) => bigint;
|
|
21
|
+
//# sourceMappingURL=locks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"locks.d.ts","sourceRoot":"","sources":["../../src/v3/locks.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,KAAK,GAAG;IAClB,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,CAAC;IACf,CAAC,EAAE,MAAM,CAAC;IACV,OAAO,EAAE,MAAM,CAAC;CACjB,CAAC;AAEF,6FAA6F;AAC7F,eAAO,MAAM,SAAS,GAAI,SAAS,MAAM,EAAE,OAAO,MAAM,WAA8C,CAAC;AAEvG,uGAAuG;AACvG,wBAAgB,KAAK,CAAC,CAAC,EAAE,KAAK,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAInF;AAED,2GAA2G;AAC3G,eAAO,MAAM,UAAU,GAAI,IAAI,MAAM,EAAE,MAAM,MAAM,EAAE,MAAM,MAAM,WACN,CAAC;AAE5D,+DAA+D;AAC/D,eAAO,MAAM,QAAQ,GAAI,IAAI,MAAM,WAAkC,CAAC"}
|
package/dist/v3/locks.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { DOM_LNULL, DOM_LOCK, DOM_LOCKB, DOM_ROUND } from "./domains.js";
|
|
3
|
+
import { noteCommitment } from "./notes/keys.js";
|
|
4
|
+
/** `loan::lock_field`: a loan round's lock field. A bid's field is the room's `sale_key`. */
|
|
5
|
+
export const lockField = (loanKey, round) => poseidonHash([DOM_ROUND, loanKey, round]);
|
|
6
|
+
/** `lib/lock.circom` Lock5: a note shape, so js4's first output with `outPk0 = lockPk` is the leaf. */
|
|
7
|
+
export function lock5(l) {
|
|
8
|
+
const lockPk = poseidonHash([DOM_LOCK, l.field, l.ownerPk, poseidonHash([l.ns]), l.unlockS]);
|
|
9
|
+
const lock = noteCommitment(l.amount, lockPk, l.b);
|
|
10
|
+
return { lockPk, lock, nullifier: poseidonHash([DOM_LNULL, l.ns, lock]) };
|
|
11
|
+
}
|
|
12
|
+
/** Ruling R5: an offer's blind binds the lender's enc point (`loan_settle.circom`, `loan_refi.circom`). */
|
|
13
|
+
export const offerBlind = (ns, encX, encY) => poseidonHash([DOM_LOCKB, ns, poseidonHash([encX, encY])]);
|
|
14
|
+
/** A bid's blind binds `ns` alone: no ECDH reads a bid key. */
|
|
15
|
+
export const bidBlind = (ns) => poseidonHash([DOM_LOCKB, ns]);
|
package/dist/v3/memo.d.ts
CHANGED
|
@@ -24,12 +24,22 @@ export declare const memoAad: (slot: MemoSlot) => Uint8Array;
|
|
|
24
24
|
export declare function sealMemo(key: Uint8Array, slot: MemoSlot, body: Uint8Array): Uint8Array;
|
|
25
25
|
/** The body of a memo sealed for `slot` under `key`, its first `bodyLen` bytes; null for anyone else's memo. */
|
|
26
26
|
export declare function openMemo(key: Uint8Array, slot: MemoSlot, memo: Uint8Array, bodyLen: number): Uint8Array | null;
|
|
27
|
-
/**
|
|
28
|
-
export type
|
|
27
|
+
/** The loan key's note index, the locked amount and the borrower key: what every borrower memo starts with. */
|
|
28
|
+
export type BorrowerMemo = {
|
|
29
29
|
keyIndex: number;
|
|
30
30
|
collAmt: bigint;
|
|
31
31
|
borrowerPk: bigint;
|
|
32
32
|
};
|
|
33
|
+
/**
|
|
34
|
+
* The borrower's request memo of round 0 (`request::new_request`) or of a refi round (`refi::open_refi`). It carries
|
|
35
|
+
* the posted request too, so the borrower settles on its own request from the seed alone, with no Seal read of
|
|
36
|
+
* `req_sealed` (and so no funds proof).
|
|
37
|
+
*/
|
|
38
|
+
export type RequestMemo = BorrowerMemo & {
|
|
39
|
+
principal: bigint;
|
|
40
|
+
maxRateBps: bigint;
|
|
41
|
+
termMs: bigint;
|
|
42
|
+
};
|
|
33
43
|
/** A lender's `OfferRecord`, every field present. */
|
|
34
44
|
export type OfferMemo = {
|
|
35
45
|
noteIndex: number;
|
|
@@ -39,7 +49,7 @@ export type OfferMemo = {
|
|
|
39
49
|
borrowerPk: bigint;
|
|
40
50
|
};
|
|
41
51
|
/** The borrower's record once settled. */
|
|
42
|
-
export type SettleMemo =
|
|
52
|
+
export type SettleMemo = BorrowerMemo & {
|
|
43
53
|
terms: KeeperTerms;
|
|
44
54
|
};
|
|
45
55
|
/** A seller's record: `saleSecrets` and the bounds salt derive from the note key at `noteIndex`; bounds are the price. */
|
|
@@ -62,7 +72,7 @@ export declare const sealSaleMemo: (key: Uint8Array, saleKey: bigint, m: SaleMem
|
|
|
62
72
|
export declare const sealBidMemo: (key: Uint8Array, saleKey: bigint, m: BidMemo) => Uint8Array<ArrayBufferLike>;
|
|
63
73
|
export declare function openSaleMemo(key: Uint8Array, saleKey: bigint, memo: Uint8Array): SaleMemo | null;
|
|
64
74
|
export declare function openBidMemo(key: Uint8Array, saleKey: bigint, memo: Uint8Array): BidMemo | null;
|
|
65
|
-
export declare function openRequestMemo(key: Uint8Array, loanKey: bigint, memo: Uint8Array): RequestMemo | null;
|
|
75
|
+
export declare function openRequestMemo(key: Uint8Array, loanKey: bigint, memo: Uint8Array, round?: bigint): RequestMemo | null;
|
|
66
76
|
export declare function openOfferMemo(key: Uint8Array, loanKey: bigint, round: bigint, memo: Uint8Array): OfferMemo | null;
|
|
67
77
|
export declare function openSettleMemo(key: Uint8Array, loanKey: bigint, round: bigint, memo: Uint8Array): SettleMemo | null;
|
|
68
78
|
//# sourceMappingURL=memo.d.ts.map
|
package/dist/v3/memo.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"memo.d.ts","sourceRoot":"","sources":["../../src/v3/memo.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAEvD,gHAAgH;AAChH,eAAO,MAAM,QAAQ,MAAM,CAAC;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o, bodyLen) {
|
|
|
53
53
|
return null;
|
|
54
54
|
return plain.slice(HEADER, HEADER + bodyLen);
|
|
55
55
|
}
|
|
56
|
-
const REQUEST_BODY =
|
|
56
|
+
const REQUEST_BODY = 68;
|
|
57
57
|
const OFFER_BODY = 60;
|
|
58
58
|
const SETTLE_BODY = 204;
|
|
59
59
|
const SALE_BODY = 12;
|
|
@@ -66,8 +66,8 @@ function reader(bytes) {
|
|
|
66
66
|
const take = (size) => fromBe(bytes.subarray(at, (at += size)));
|
|
67
67
|
return { index: () => Number(take(4)), u64: () => take(8), field: () => take(32) };
|
|
68
68
|
}
|
|
69
|
-
const
|
|
70
|
-
export const requestMemoBody = (m) => Uint8Array.from(
|
|
69
|
+
const borrowerBody = (m) => [...index(m.keyIndex), ...u64(m.collAmt), ...field(m.borrowerPk)];
|
|
70
|
+
export const requestMemoBody = (m) => Uint8Array.from([...borrowerBody(m), ...u64(m.principal), ...u64(m.maxRateBps), ...u64(m.termMs)]);
|
|
71
71
|
export const offerMemoBody = (m) => Uint8Array.from([
|
|
72
72
|
...index(m.noteIndex),
|
|
73
73
|
...u64(m.principal),
|
|
@@ -76,7 +76,7 @@ export const offerMemoBody = (m) => Uint8Array.from([
|
|
|
76
76
|
...field(m.borrowerPk),
|
|
77
77
|
]);
|
|
78
78
|
export const settleMemoBody = (m) => Uint8Array.from([
|
|
79
|
-
...
|
|
79
|
+
...borrowerBody(m),
|
|
80
80
|
...u64(m.terms.principal),
|
|
81
81
|
...u64(m.terms.rateBps),
|
|
82
82
|
...u64(m.terms.owed),
|
|
@@ -104,12 +104,19 @@ export function openBidMemo(key, saleKey, memo) {
|
|
|
104
104
|
const r = reader(body);
|
|
105
105
|
return { noteIndex: r.index(), v: r.u64(), amount: r.u64() };
|
|
106
106
|
}
|
|
107
|
-
export function openRequestMemo(key, loanKey, memo) {
|
|
108
|
-
const body = openMemo(key, { kind: MEMO_KIND.request, key: loanKey, round
|
|
107
|
+
export function openRequestMemo(key, loanKey, memo, round = 0n) {
|
|
108
|
+
const body = openMemo(key, { kind: MEMO_KIND.request, key: loanKey, round }, memo, REQUEST_BODY);
|
|
109
109
|
if (!body)
|
|
110
110
|
return null;
|
|
111
111
|
const r = reader(body);
|
|
112
|
-
return {
|
|
112
|
+
return {
|
|
113
|
+
keyIndex: r.index(),
|
|
114
|
+
collAmt: r.u64(),
|
|
115
|
+
borrowerPk: r.field(),
|
|
116
|
+
principal: r.u64(),
|
|
117
|
+
maxRateBps: r.u64(),
|
|
118
|
+
termMs: r.u64(),
|
|
119
|
+
};
|
|
113
120
|
}
|
|
114
121
|
export function openOfferMemo(key, loanKey, round, memo) {
|
|
115
122
|
const body = openMemo(key, { kind: MEMO_KIND.offer, key: loanKey, round }, memo, OFFER_BODY);
|
|
@@ -16,6 +16,11 @@ export declare function receiveKey(seed: Uint8Array, index: number): {
|
|
|
16
16
|
C: Uint8Array;
|
|
17
17
|
D: Uint8Array;
|
|
18
18
|
};
|
|
19
|
+
/** Z1 freeze §6.3: the key offers (or bids) to the request (or listing) of record `index` are sealed to. */
|
|
20
|
+
export declare function offerRecvKey(seed: Uint8Array, index: number): {
|
|
21
|
+
secret: Uint8Array;
|
|
22
|
+
pub: Uint8Array;
|
|
23
|
+
};
|
|
19
24
|
/** D-SPEC:564, :596: the fresh one-time sender of deposit call `index`. */
|
|
20
25
|
export declare function depositSigner(seed: Uint8Array, index: number): Ed25519Keypair;
|
|
21
26
|
/** PRV-35: the single-use address sale item `index` (a claim, a badge, an item to sell) lands at and opens from. */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"derive.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/derive.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAI9D,MAAM,MAAM,OAAO,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,eAAO,MAAM,UAAU,KAAK,CAA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EAAE,UAAU,CAAC;IAAC,CAAC,EAAE,UAAU,CAAA;CAAE,CAM5F;AAED,4GAA4G;AAC5G,wBAAgB,YAAY,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,GAAG,EAAE,UAAU,CAAA;CAAE,CAKrG;AAED,2EAA2E;AAC3E,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,CAE7E;AAED,oHAAoH;AACpH,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,CAE1E"}
|
package/dist/v3/notes/derive.js
CHANGED
|
@@ -34,6 +34,11 @@ export function receiveKey(seed, index) {
|
|
|
34
34
|
const D = secp256k1.getPublicKey(d, true);
|
|
35
35
|
return { C: secp256k1.getSharedSecret(noteEncKey(seed).encKey, D, true), D };
|
|
36
36
|
}
|
|
37
|
+
/** Z1 freeze §6.3: the key offers (or bids) to the request (or listing) of record `index` are sealed to. */
|
|
38
|
+
export function offerRecvKey(seed, index) {
|
|
39
|
+
const secret = secp256k1.utils.randomSecretKey(digest(seed, "ess3/offer-recv/v1", u64be(index), secp256k1.lengths.seed));
|
|
40
|
+
return { secret, pub: secp256k1.getPublicKey(secret, true) };
|
|
41
|
+
}
|
|
37
42
|
/** D-SPEC:564, :596: the fresh one-time sender of deposit call `index`. */
|
|
38
43
|
export function depositSigner(seed, index) {
|
|
39
44
|
return Ed25519Keypair.fromSecretKey(digest(seed, "ess3/signer", u64be(index), 32));
|
package/dist/v3/pace.d.ts
CHANGED
package/dist/v3/pace.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pace.d.ts","sourceRoot":"","sources":["../../src/v3/pace.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEpD,eAAO,MAAM,KAAK
|
|
1
|
+
{"version":3,"file":"pace.d.ts","sourceRoot":"","sources":["../../src/v3/pace.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,SAAS,GAAG,MAAM,GAAG,QAAQ,GAAG,OAAO,CAAC;AAEpD,eAAO,MAAM,KAAK;;;;;;;;;;;;;;;;;;;CAmB4B,CAAC;AAE/C,MAAM,MAAM,WAAW,GAAG,MAAM,OAAO,KAAK,CAAC;AAE7C,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,MAAM,EAAE,MAAM,CAAC,CAIvE,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;AAExD,iHAAiH;AACjH,MAAM,MAAM,KAAK,GAAG;IAAE,MAAM,EAAE,WAAW,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnG,eAAO,MAAM,WAAW,GAAI,KAAK,MAAM,EAAE,KAAK,MAAM,WAA4B,CAAC;AAEjF,0GAA0G;AAC1G,wBAAgB,SAAS,CACvB,MAAM,EAAE,WAAW,EACnB,KAAK,EAAE,MAAM,EACb,QAAQ,EAAE,QAAQ,GAAG,IAAI,EACzB,QAAQ,EAAE,MAAM,EAChB,IAAI,GAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,MAAoB,GACvD,MAAM,CAYR"}
|
package/dist/v3/pace.js
CHANGED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type Pad = {
|
|
2
|
+
pk: bigint;
|
|
3
|
+
blinding: bigint;
|
|
4
|
+
leaf: bigint;
|
|
5
|
+
};
|
|
6
|
+
/** `lib/pad.circom` PadLeaf: `Note(0, pk, blinding)`, from a reserved note index so a recovered wallet knows it. */
|
|
7
|
+
export declare function padNote(seed: Uint8Array, index: number): Pad;
|
|
8
|
+
/** Two equal pad leaves in one transaction would fingerprint it on chain. */
|
|
9
|
+
export declare function assertDistinctPads(...leaves: bigint[]): void;
|
|
10
|
+
/** The two pads of one entry, each from its own freshly reserved note index. */
|
|
11
|
+
export declare function padPair(seed: Uint8Array, reserve: () => number): [Pad, Pad];
|
|
12
|
+
/** `lib/pad.circom` DummyNull. A fresh random seed per proof: a chosen dummy could squat a pending real nullifier. */
|
|
13
|
+
export declare const dummyNull: (seed: bigint) => bigint;
|
|
14
|
+
export declare const drawDummySeed: () => bigint;
|
|
15
|
+
//# sourceMappingURL=pads.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pads.d.ts","sourceRoot":"","sources":["../../src/v3/pads.ts"],"names":[],"mappings":"AAQA,MAAM,MAAM,GAAG,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjE,oHAAoH;AACpH,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,GAAG,CAI5D;AAED,6EAA6E;AAC7E,wBAAgB,kBAAkB,CAAC,GAAG,MAAM,EAAE,MAAM,EAAE,GAAG,IAAI,CAE5D;AAED,gFAAgF;AAChF,wBAAgB,OAAO,CAAC,IAAI,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,MAAM,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,CAI3E;AAED,sHAAsH;AACtH,eAAO,MAAM,SAAS,GAAI,MAAM,MAAM,WAAoC,CAAC;AAE3E,eAAO,MAAM,aAAa,QAAO,MAA4D,CAAC"}
|
package/dist/v3/pads.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
3
|
+
import { DOM_DUMMY, R } from "./domains.js";
|
|
4
|
+
import { deriveNoteKey } from "./notes/derive.js";
|
|
5
|
+
import { noteCommitment, notePk } from "./notes/keys.js";
|
|
6
|
+
/** `lib/pad.circom` PadLeaf: `Note(0, pk, blinding)`, from a reserved note index so a recovered wallet knows it. */
|
|
7
|
+
export function padNote(seed, index) {
|
|
8
|
+
const { sk, blinding } = deriveNoteKey(seed, index);
|
|
9
|
+
const pk = notePk(sk);
|
|
10
|
+
return { pk, blinding, leaf: noteCommitment(0n, pk, blinding) };
|
|
11
|
+
}
|
|
12
|
+
/** Two equal pad leaves in one transaction would fingerprint it on chain. */
|
|
13
|
+
export function assertDistinctPads(...leaves) {
|
|
14
|
+
if (new Set(leaves).size !== leaves.length)
|
|
15
|
+
throw new Error("the pads of one entry repeat a leaf");
|
|
16
|
+
}
|
|
17
|
+
/** The two pads of one entry, each from its own freshly reserved note index. */
|
|
18
|
+
export function padPair(seed, reserve) {
|
|
19
|
+
const pads = [padNote(seed, reserve()), padNote(seed, reserve())];
|
|
20
|
+
assertDistinctPads(pads[0].leaf, pads[1].leaf);
|
|
21
|
+
return pads;
|
|
22
|
+
}
|
|
23
|
+
/** `lib/pad.circom` DummyNull. A fresh random seed per proof: a chosen dummy could squat a pending real nullifier. */
|
|
24
|
+
export const dummyNull = (seed) => poseidonHash([DOM_DUMMY, seed]);
|
|
25
|
+
export const drawDummySeed = () => BigInt(`0x${randomBytes(32).toString("hex")}`) % R;
|
package/dist/v3/prover.d.ts
CHANGED
|
@@ -17,7 +17,7 @@ export declare const sha256Hex: (bytes: Uint8Array) => string;
|
|
|
17
17
|
/** Downloads one artifact and refuses it unless its sha256 is the pinned one. */
|
|
18
18
|
export declare function fetchPinned(fetch: Fetch, url: string, sha256: string): Promise<Uint8Array>;
|
|
19
19
|
/** Circuits `Essey` proves. Each value is the public URL and the sha256 `fetchPinned` checks. */
|
|
20
|
-
export declare const SDK_CIRCUITS: readonly ["membership", "deposit", "js4", "coll_lock", "health", "loan_offer", "loan_settle", "loan_repay", "reclaim", "reclaim_coll", "loan_refi", "lock_bid", "sale_settle", "item_claim"];
|
|
20
|
+
export declare const SDK_CIRCUITS: readonly ["membership", "deposit", "js4", "coll_lock", "health", "loan_offer", "loan_settle", "loan_repay", "reclaim", "reclaim_coll", "loan_refi", "lock_bid", "sale_settle", "item_claim", "funds_read"];
|
|
21
21
|
export type PinnedArtifact = {
|
|
22
22
|
url: string;
|
|
23
23
|
sha256: string;
|