@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
package/dist/v3/loans/bodies.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { address } from "../../network.js";
|
|
2
2
|
import { bytesOf, encOf, envelope, fieldOf, hex, invalid, keyArgs, passArgs, proofOf, u64Of, } from "../body-args.js";
|
|
3
|
-
import { claimDefault as claimDefaultBody, openLockArgs } from "../collateral/bodies.js";
|
|
3
|
+
import { claimDefault as claimDefaultBody, distinctPads, openLockArgs, } from "../collateral/bodies.js";
|
|
4
4
|
import { VK } from "../kinds.js";
|
|
5
5
|
import { MEMO_LEN } from "../memo.js";
|
|
6
6
|
/** `services/relayer-v3/src/field.ts`: sealed blobs and a Pyth update. */
|
|
@@ -8,8 +8,8 @@ const SEALED_MAX = 1024;
|
|
|
8
8
|
const UPDATE_MAX = 4096;
|
|
9
9
|
const memoOf = (memo) => bytesOf(memo, "memo", MEMO_LEN, MEMO_LEN);
|
|
10
10
|
function checkStatement(s, what) {
|
|
11
|
-
if (!s || s.outs?.length !== 4 || s.delivery?.length !==
|
|
12
|
-
invalid(`${what} statement has 4 outs and
|
|
11
|
+
if (!s || s.outs?.length !== 4 || s.delivery?.length !== 8)
|
|
12
|
+
invalid(`${what} statement has 4 outs and 8 delivery rows`);
|
|
13
13
|
s.outs.forEach((n, i) => fieldOf(n, `${what} out ${i}`));
|
|
14
14
|
s.delivery.forEach((n, i) => fieldOf(n, `${what} delivery ${i}`));
|
|
15
15
|
}
|
|
@@ -20,13 +20,15 @@ function checkLock(lock) {
|
|
|
20
20
|
fieldOf(lock.note, "note");
|
|
21
21
|
fieldOf(lock.change, "change");
|
|
22
22
|
fieldOf(lock.feeNote, "feeNote");
|
|
23
|
+
fieldOf(lock.pad0, "pad0");
|
|
24
|
+
fieldOf(lock.pad1, "pad1");
|
|
23
25
|
fieldOf(lock.ephX, "ephX");
|
|
24
26
|
fieldOf(lock.ephY, "ephY");
|
|
25
27
|
fieldOf(lock.feeCt, "feeCt");
|
|
26
28
|
}
|
|
27
29
|
const prefixed = (prefix) => (name) => `${prefix}${name[0].toUpperCase()}${name.slice(1)}`;
|
|
28
30
|
const OUTS = ["out0", "out1", "out2", "feeNote"];
|
|
29
|
-
const DELIVERY = ["ephX", "ephY", "feeCt", "lEx", "lEy", "lCt"];
|
|
31
|
+
const DELIVERY = ["ephX", "ephY", "feeCt", "lEx", "lEy", "lCt", "nDummy0", "nDummy1"];
|
|
30
32
|
function healthRows(s, key) {
|
|
31
33
|
return Object.fromEntries([
|
|
32
34
|
...OUTS.map((name, i) => [key(name), s.outs[i].toString()]),
|
|
@@ -41,8 +43,6 @@ export function open(p) {
|
|
|
41
43
|
bytesOf(p.reqSealed, "reqSealed", 1, SEALED_MAX);
|
|
42
44
|
bytesOf(p.keeperCt, "keeperCt", 1, SEALED_MAX);
|
|
43
45
|
bytesOf(p.priceUpdate, "priceUpdate", 1, UPDATE_MAX);
|
|
44
|
-
encOf(p.encs[0], "encChange");
|
|
45
|
-
encOf(p.encs[1], "encFee");
|
|
46
46
|
return envelope("loan_open", p.pool, [proofOf(VK.membership, p.pass.proved), proofOf(VK.collLock, p.collLock), proofOf(VK.health, p.health)], {
|
|
47
47
|
...passArgs(p.pass),
|
|
48
48
|
market: address(p.market, "market"),
|
|
@@ -62,54 +62,41 @@ export function open(p) {
|
|
|
62
62
|
}),
|
|
63
63
|
}, p.relayerFee, p.wait);
|
|
64
64
|
}
|
|
65
|
-
/** `
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
tag: fieldOf(p.tag, "tag"),
|
|
71
|
-
expiryMs: u64Of(p.expiryMs, "expiryMs"),
|
|
72
|
-
passRoom: address(p.passRoom, "passRoom"),
|
|
73
|
-
root: fieldOf(p.root, "root"),
|
|
74
|
-
nullifier0: fieldOf(p.nullifier0, "nullifier0"),
|
|
75
|
-
nullifier1: fieldOf(p.nullifier1, "nullifier1"),
|
|
76
|
-
lock: fieldOf(p.lock, "lock"),
|
|
77
|
-
change: fieldOf(p.change, "change"),
|
|
78
|
-
feeNote: fieldOf(p.feeNote, "feeNote"),
|
|
79
|
-
ephX: fieldOf(p.ephX, "ephX"),
|
|
80
|
-
ephY: fieldOf(p.ephY, "ephY"),
|
|
81
|
-
feeCt: fieldOf(p.feeCt, "feeCt"),
|
|
82
|
-
commit: fieldOf(p.commit, "commit"),
|
|
83
|
-
sealed: hex(p.sealed),
|
|
84
|
-
memo: memoOf(p.memo),
|
|
85
|
-
encChange: encOf(p.encChange, "encChange"),
|
|
86
|
-
encFee: encOf(p.encFee, "encFee"),
|
|
87
|
-
}, p.relayerFee, p.wait);
|
|
65
|
+
/** Z1 `pool::room_apply` takes 4 encs in leaf order; on the wire they are `enc0`..`enc3`. */
|
|
66
|
+
function encs4(encs, what) {
|
|
67
|
+
if (encs?.length !== 4)
|
|
68
|
+
invalid(`${what} carries 4 encs in leaf order`);
|
|
69
|
+
return Object.fromEntries(encs.map((e, i) => [`enc${i}`, encOf(e, `enc${i}`)]));
|
|
88
70
|
}
|
|
89
|
-
/** `loan_settle` pays no relayer (`payload.ts` `FEE_FREE`). */
|
|
71
|
+
/** `loan_settle` pays no relayer (`payload.ts` `FEE_FREE`). Arguments of Z1 `settle::settle`, by name. */
|
|
90
72
|
export function settle(p) {
|
|
91
73
|
if (p.d?.length !== 4)
|
|
92
74
|
invalid("settle carries 4 d rows");
|
|
75
|
+
distinctPads(p.pad0, p.pad1);
|
|
93
76
|
bytesOf(p.keeperCt, "keeperCt", 1, SEALED_MAX);
|
|
77
|
+
bytesOf(p.priceUpdate, "priceUpdate", 1, UPDATE_MAX);
|
|
94
78
|
return envelope("loan_settle", p.pool, [proofOf(VK.membership, p.pass.proved), proofOf(VK.loanSettle, p.settle)], {
|
|
95
79
|
loan: address(p.loan, "loan"),
|
|
80
|
+
market: address(p.market, "market"),
|
|
96
81
|
...passArgs(p.pass),
|
|
82
|
+
root: fieldOf(p.root, "root"),
|
|
83
|
+
nLock: fieldOf(p.nLock, "nLock"),
|
|
84
|
+
nDummy: fieldOf(p.nDummy, "nDummy"),
|
|
97
85
|
termsCommit: fieldOf(p.termsCommit, "termsCommit"),
|
|
98
86
|
principalNote: fieldOf(p.principalNote, "principalNote"),
|
|
99
87
|
feeNote: fieldOf(p.feeNote, "feeNote"),
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
d2: fieldOf(p.d[2], "d2"),
|
|
104
|
-
d3: fieldOf(p.d[3], "d3"),
|
|
88
|
+
pad0: fieldOf(p.pad0, "pad0"),
|
|
89
|
+
pad1: fieldOf(p.pad1, "pad1"),
|
|
90
|
+
...Object.fromEntries(p.d.map((x, i) => [`d${i}`, fieldOf(x, `d${i}`)])),
|
|
105
91
|
ephX: fieldOf(p.ephX, "ephX"),
|
|
106
92
|
ephY: fieldOf(p.ephY, "ephY"),
|
|
107
93
|
feeCt: fieldOf(p.feeCt, "feeCt"),
|
|
94
|
+
pClaim: u64Of(p.pClaim, "pClaim"),
|
|
108
95
|
keeperCt: hex(p.keeperCt),
|
|
109
|
-
|
|
110
|
-
encFee: encOf(p.encFee, "encFee"),
|
|
96
|
+
...encs4(p.encs, "settle"),
|
|
111
97
|
nowS: u64Of(p.nowS, "nowS"),
|
|
112
98
|
memo: memoOf(p.memo),
|
|
99
|
+
priceUpdate: hex(p.priceUpdate),
|
|
113
100
|
}, 0n, p.wait);
|
|
114
101
|
}
|
|
115
102
|
/** `loan_repay`. The Health proof is optional; its rows are required exactly with it. */
|
|
@@ -119,8 +106,9 @@ export function repay(p) {
|
|
|
119
106
|
const release = p.release;
|
|
120
107
|
if (release) {
|
|
121
108
|
checkStatement(release.statement, "release");
|
|
122
|
-
|
|
123
|
-
|
|
109
|
+
if (release.encs?.length !== 4)
|
|
110
|
+
invalid("release carries 4 note ciphertexts");
|
|
111
|
+
release.encs.forEach((e, i) => encOf(e, `releaseEnc${i}`));
|
|
124
112
|
}
|
|
125
113
|
const key = prefixed("release");
|
|
126
114
|
return envelope("loan_repay", p.pool, release
|
|
@@ -144,62 +132,51 @@ export function repay(p) {
|
|
|
144
132
|
? {
|
|
145
133
|
market: address(release.market, "market"),
|
|
146
134
|
...healthRows(release.statement, key),
|
|
147
|
-
[key(
|
|
148
|
-
[key("encFee")]: hex(release.encs[1]),
|
|
135
|
+
...Object.fromEntries(release.encs.map((e, i) => [key(`enc${i}`), hex(e)])),
|
|
149
136
|
}
|
|
150
137
|
: {}),
|
|
151
138
|
}, p.relayerFee, p.wait);
|
|
152
139
|
}
|
|
153
|
-
/** `
|
|
140
|
+
/** `reclaim::reclaim`, one entry for offer and bid locks: the pool only, no loan or room. Pays the pool constant. */
|
|
154
141
|
export function reclaim(p) {
|
|
142
|
+
distinctPads(p.pad0, p.pad1);
|
|
155
143
|
return envelope("loan_reclaim", p.pool, [proofOf(VK.reclaim, p.proved)], {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
144
|
+
root: fieldOf(p.root, "root"),
|
|
145
|
+
nLock: fieldOf(p.nLock, "nLock"),
|
|
146
|
+
nDummy: fieldOf(p.nDummy, "nDummy"),
|
|
159
147
|
refund: fieldOf(p.refund, "refund"),
|
|
160
|
-
|
|
148
|
+
feeNote: fieldOf(p.feeNote, "feeNote"),
|
|
149
|
+
pad0: fieldOf(p.pad0, "pad0"),
|
|
150
|
+
pad1: fieldOf(p.pad1, "pad1"),
|
|
161
151
|
ephX: fieldOf(p.ephX, "ephX"),
|
|
162
152
|
ephY: fieldOf(p.ephY, "ephY"),
|
|
163
153
|
feeCt: fieldOf(p.feeCt, "feeCt"),
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
154
|
+
feeCommit: fieldOf(p.feeCommit, "feeCommit"),
|
|
155
|
+
nowS: u64Of(p.nowS, "nowS"),
|
|
156
|
+
...encs4(p.encs, "loan_reclaim"),
|
|
167
157
|
}, p.relayerFee, p.wait);
|
|
168
158
|
}
|
|
169
|
-
/** `
|
|
159
|
+
/** `reclaim_coll::reclaim_coll` pays no relayer (`FEE_FREE`). */
|
|
170
160
|
export function reclaimColl(p) {
|
|
161
|
+
distinctPads(p.pad0, p.pad1);
|
|
171
162
|
return envelope("loan_reclaim_coll", p.pool, [proofOf(VK.reclaimColl, p.proved)], {
|
|
172
163
|
loan: address(p.loan, "loan"),
|
|
173
164
|
nullifier: fieldOf(p.nullifier, "nullifier"),
|
|
165
|
+
nDummy: fieldOf(p.nDummy, "nDummy"),
|
|
174
166
|
outNote: fieldOf(p.outNote, "outNote"),
|
|
175
167
|
feeNote: fieldOf(p.feeNote, "feeNote"),
|
|
168
|
+
pad0: fieldOf(p.pad0, "pad0"),
|
|
169
|
+
pad1: fieldOf(p.pad1, "pad1"),
|
|
176
170
|
ephX: fieldOf(p.ephX, "ephX"),
|
|
177
171
|
ephY: fieldOf(p.ephY, "ephY"),
|
|
178
172
|
feeCt: fieldOf(p.feeCt, "feeCt"),
|
|
179
|
-
|
|
180
|
-
encFee: encOf(p.encFee, "encFee"),
|
|
173
|
+
...encs4(p.encs, "loan_reclaim_coll"),
|
|
181
174
|
}, 0n, p.wait);
|
|
182
175
|
}
|
|
183
176
|
/** `loan_cancel` pays no relayer. */
|
|
184
177
|
export function cancel(p) {
|
|
185
178
|
return envelope("loan_cancel", p.pool, p.pass ? [proofOf(VK.membership, p.pass.proved)] : [], { loan: address(p.loan, "loan"), ...(p.pass ? passArgs(p.pass) : {}) }, 0n, p.wait);
|
|
186
179
|
}
|
|
187
|
-
/** `loan_slash` (`offer::slash`) takes no proof and pays no relayer: the derived keys are the authorization. */
|
|
188
|
-
export function slash(p) {
|
|
189
|
-
return envelope("loan_slash", p.pool, [], {
|
|
190
|
-
loan: address(p.loan, "loan"),
|
|
191
|
-
round: u64Of(p.round, "round"),
|
|
192
|
-
seq: u64Of(p.seq, "seq"),
|
|
193
|
-
...keyArgs(p.keys),
|
|
194
|
-
}, 0n, p.wait);
|
|
195
|
-
}
|
|
196
|
-
/**
|
|
197
|
-
* `loan_pick` (`pick::pick`): no proof, no pass, no relayer fee. It takes `Random` (0x8), so it is the last call of
|
|
198
|
-
* its PTB and the relayer sends it alone (`move/v3/loan/sources/pick.move:1-3`).
|
|
199
|
-
*/
|
|
200
|
-
export function pick(p) {
|
|
201
|
-
return envelope("loan_pick", p.pool, [], { loan: address(p.loan, "loan") }, 0n, p.wait);
|
|
202
|
-
}
|
|
203
180
|
/** `loan_ct_fault` (`keeper::prove_ct_fault`), as `slash`: keys for the retained borrower ciphertext's key id. */
|
|
204
181
|
export function ctFault(p) {
|
|
205
182
|
return envelope("loan_ct_fault", p.pool, [], { loan: address(p.loan, "loan"), ...keyArgs(p.keys) }, 0n, p.wait);
|
|
@@ -212,6 +189,7 @@ export function refinance(p) {
|
|
|
212
189
|
...passArgs(p.pass),
|
|
213
190
|
reqCommit: fieldOf(p.reqCommit, "reqCommit"),
|
|
214
191
|
reqSealed: hex(p.reqSealed),
|
|
192
|
+
memo: memoOf(p.memo),
|
|
215
193
|
closeMs: u64Of(p.closeMs, "closeMs"),
|
|
216
194
|
}, 0n, p.wait);
|
|
217
195
|
}
|
|
@@ -229,10 +207,12 @@ export function settleRefi(p) {
|
|
|
229
207
|
loan: address(p.loan, "loan"),
|
|
230
208
|
market: address(p.market, "market"),
|
|
231
209
|
...passArgs(p.pass),
|
|
210
|
+
root: fieldOf(p.root, "root"),
|
|
211
|
+
nLock: fieldOf(p.nLock, "nLock"),
|
|
212
|
+
nDummy: fieldOf(p.nDummy, "nDummy"),
|
|
232
213
|
termsCommit: fieldOf(p.termsCommit, "termsCommit"),
|
|
233
214
|
borrowerNote: fieldOf(p.borrowerNote, "borrowerNote"),
|
|
234
215
|
feeNote: fieldOf(p.feeNote, "feeNote"),
|
|
235
|
-
winNull: fieldOf(p.winNull, "winNull"),
|
|
236
216
|
...Object.fromEntries(p.d.map((x, i) => [`d${i}`, fieldOf(x, `d${i}`)])),
|
|
237
217
|
ephX: fieldOf(p.ephX, "ephX"),
|
|
238
218
|
ephY: fieldOf(p.ephY, "ephY"),
|
|
@@ -243,10 +223,7 @@ export function settleRefi(p) {
|
|
|
243
223
|
ephP1: fieldOf(p.ephP[1], "ephP1"),
|
|
244
224
|
pClaim: u64Of(p.pClaim, "pClaim"),
|
|
245
225
|
keeperCt: hex(p.keeperCt),
|
|
246
|
-
|
|
247
|
-
encFee: encOf(p.encs[1], "encFee"),
|
|
248
|
-
encPayoff: encOf(p.encsP[0], "encPayoff"),
|
|
249
|
-
encPad: encOf(p.encsP[1], "encPad"),
|
|
226
|
+
...encs4(p.encs, "settle_refi"),
|
|
250
227
|
nowS: u64Of(p.nowS, "nowS"),
|
|
251
228
|
memo: memoOf(p.memo),
|
|
252
229
|
priceUpdate: hex(p.priceUpdate),
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { PoolCommitment } from "../notes/scan.ts";
|
|
2
|
+
export declare const BOX_PLAIN: number;
|
|
3
|
+
export declare const PAYLOAD_BYTES = 104;
|
|
4
|
+
/** Freeze §6.1: HKDF labels of the three new boxes. The ordinary scanner tries only the note labels, so skips them. */
|
|
5
|
+
export declare const INFO: {
|
|
6
|
+
readonly offer: "ess3/offer-enc/v1";
|
|
7
|
+
readonly lockRec: "ess3/lock-rec/v1";
|
|
8
|
+
};
|
|
9
|
+
/** Freeze §6.1, in `pool::transfer` leaf order: out0 (the lock), out1 (change), fee_note, pad. */
|
|
10
|
+
export declare const SLOT: {
|
|
11
|
+
readonly partA: 0;
|
|
12
|
+
readonly change: 1;
|
|
13
|
+
readonly partB: 2;
|
|
14
|
+
readonly recovery: 3;
|
|
15
|
+
};
|
|
16
|
+
export type Payload = {
|
|
17
|
+
ownerPk: bigint;
|
|
18
|
+
ns: bigint;
|
|
19
|
+
aux: Uint8Array;
|
|
20
|
+
unlockS: bigint;
|
|
21
|
+
};
|
|
22
|
+
export type Recovery = {
|
|
23
|
+
amount: bigint;
|
|
24
|
+
field: bigint;
|
|
25
|
+
unlockS: bigint;
|
|
26
|
+
index: bigint;
|
|
27
|
+
};
|
|
28
|
+
/** ownerPk (32) ‖ ns (32) ‖ aux (32) ‖ unlock_s (u64), big-endian. aux is the packed enc point or the bid amount. */
|
|
29
|
+
export declare function packPayload(p: Payload): Uint8Array;
|
|
30
|
+
export declare function unpackPayload(p: Uint8Array): Payload | null;
|
|
31
|
+
/** amount (u64) ‖ field (32) ‖ unlock_s (u64) ‖ note index (7): sealed to the owner's own note key, slot 3. */
|
|
32
|
+
export declare function packRecovery(r: Recovery): Uint8Array;
|
|
33
|
+
export declare function unpackRecovery(p: Uint8Array): Recovery | null;
|
|
34
|
+
/** circomlibjs `packPoint`: y little-endian, bit 255 set when x > (p-1)/2. */
|
|
35
|
+
export declare function packEncPoint(x: bigint, y: bigint): Promise<Uint8Array>;
|
|
36
|
+
/** On the curve, in the prime-order subgroup and canonically packed, or null. */
|
|
37
|
+
export declare function unpackEncPoint(aux: Uint8Array): Promise<{
|
|
38
|
+
x: bigint;
|
|
39
|
+
y: bigint;
|
|
40
|
+
} | null>;
|
|
41
|
+
export declare const packAmount: (amount: bigint) => Uint8Array<ArrayBuffer>;
|
|
42
|
+
export declare const unpackAmount: (aux: Uint8Array) => bigint | null;
|
|
43
|
+
/** `notes/opening.ts` box (eph ‖ nonce ‖ XChaCha20-Poly1305) over 55 raw bytes, under `info`. */
|
|
44
|
+
export declare function sealBox(plain: Uint8Array, recvPub: Uint8Array, info: string): Uint8Array;
|
|
45
|
+
/** Never throws. */
|
|
46
|
+
export declare function openBox(enc: Uint8Array, recvSecret: Uint8Array, info: string): Uint8Array | null;
|
|
47
|
+
/** The four encs of an offer or bid transfer in leaf order; slot 1 is the ordinary change box. */
|
|
48
|
+
export declare function lockEncs(p: {
|
|
49
|
+
payload: Payload;
|
|
50
|
+
changeEnc: Uint8Array;
|
|
51
|
+
recovery: Recovery;
|
|
52
|
+
counterpartyPub: Uint8Array;
|
|
53
|
+
ownPub: Uint8Array;
|
|
54
|
+
}): Uint8Array[];
|
|
55
|
+
/** R1's per-offer keys from the reserved index's (sk, blinding), with Z1's blind (ruling R5, freeze §6.4). */
|
|
56
|
+
export declare function offerKeys(sk: bigint, blinding: bigint): Promise<{
|
|
57
|
+
ownerPk: bigint;
|
|
58
|
+
ns: bigint;
|
|
59
|
+
encSk: bigint;
|
|
60
|
+
encX: bigint;
|
|
61
|
+
encY: bigint;
|
|
62
|
+
b: bigint;
|
|
63
|
+
}>;
|
|
64
|
+
/** Freeze §6.5: the first 15 s grid second strictly after the deadline (`settle_by_ms`, `finish_by_ms`). */
|
|
65
|
+
export declare const unlockAfter: (deadlineMs: bigint) => bigint;
|
|
66
|
+
/** Erik's condition 4, borrower side: an on-time reclaim must be impossible while a settle still is. */
|
|
67
|
+
export declare const acceptUnlock: (unlockS: bigint, deadlineMs: bigint) => boolean;
|
|
68
|
+
/** A lock leaf, opened from slots 0 and 2 of its transfer, rebuilt and checked: the leaf a settle can prove. */
|
|
69
|
+
export type ScannedLock = Payload & {
|
|
70
|
+
index: bigint;
|
|
71
|
+
lock: bigint;
|
|
72
|
+
nullifier: bigint;
|
|
73
|
+
};
|
|
74
|
+
/**
|
|
75
|
+
* The borrower's (or seller's) scan: trial-decrypt every commitment with the request (or listing) key, join parts A
|
|
76
|
+
* and B (two leaves on), and keep a lock that rebuilds to the slot-0 leaf under `field` and `amount`, whose unlock
|
|
77
|
+
* passes `acceptUnlock`, and whose nullifier is unspent. `open` reads the aux field into the lock's amount and blind
|
|
78
|
+
* (`offerOpen`, `bidOpen`), so a lock whose blind does not bind its enc point never reaches a proof.
|
|
79
|
+
*/
|
|
80
|
+
export declare function scanLocks(p: {
|
|
81
|
+
commitments: PoolCommitment[];
|
|
82
|
+
nullifiers: bigint[];
|
|
83
|
+
recvSecret: Uint8Array;
|
|
84
|
+
field: bigint;
|
|
85
|
+
open: (payload: Payload) => Promise<{
|
|
86
|
+
amount: bigint;
|
|
87
|
+
b: bigint;
|
|
88
|
+
} | null>;
|
|
89
|
+
deadlineMs: bigint;
|
|
90
|
+
}): Promise<ScannedLock[]>;
|
|
91
|
+
/** An offer locks the request's principal, and its blind binds the enc point in aux (ruling R5). */
|
|
92
|
+
export declare const offerOpen: (principal: bigint) => (p: Payload) => Promise<{
|
|
93
|
+
amount: bigint;
|
|
94
|
+
b: bigint;
|
|
95
|
+
} | null>;
|
|
96
|
+
/** A bid locks the amount in aux, at least the posted price, under a blind over `ns` alone. */
|
|
97
|
+
export declare const bidOpen: (price: bigint) => (p: Payload) => Promise<{
|
|
98
|
+
amount: bigint;
|
|
99
|
+
b: bigint;
|
|
100
|
+
} | null>;
|
|
101
|
+
/** The lender's (or buyer's) own locks: every slot-3 recovery record the note key opens, rebuilt from the seed. */
|
|
102
|
+
export declare function scanRecoveries(commitments: PoolCommitment[], encKey: Uint8Array): {
|
|
103
|
+
lockIndex: bigint;
|
|
104
|
+
amount: bigint;
|
|
105
|
+
field: bigint;
|
|
106
|
+
unlockS: bigint;
|
|
107
|
+
index: bigint;
|
|
108
|
+
}[];
|
|
109
|
+
//# sourceMappingURL=offer-payload.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"offer-payload.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/offer-payload.ts"],"names":[],"mappings":"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"}
|
|
@@ -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
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
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":"
|
|
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"}
|