@esseyonsui/sui-sdk-v3 0.0.0-stage → 0.1.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/README.md +45 -2
- package/dist/config.d.ts +82 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +180 -0
- package/dist/errors.d.ts +38 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +32 -0
- package/dist/essey.d.ts +866 -0
- package/dist/essey.d.ts.map +1 -0
- package/dist/essey.js +3592 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +4 -0
- package/dist/keeper.d.ts +54 -0
- package/dist/keeper.d.ts.map +1 -0
- package/dist/keeper.js +44 -0
- package/dist/network.d.ts +17 -0
- package/dist/network.d.ts.map +1 -0
- package/dist/network.js +62 -0
- package/dist/v3/addr-field.d.ts +3 -0
- package/dist/v3/addr-field.d.ts.map +1 -0
- package/dist/v3/addr-field.js +10 -0
- package/dist/v3/bodies.d.ts +54 -0
- package/dist/v3/bodies.d.ts.map +1 -0
- package/dist/v3/bodies.js +68 -0
- package/dist/v3/body-args.d.ts +29 -0
- package/dist/v3/body-args.d.ts.map +1 -0
- package/dist/v3/body-args.js +78 -0
- package/dist/v3/collateral/bodies.d.ts +78 -0
- package/dist/v3/collateral/bodies.d.ts.map +1 -0
- package/dist/v3/collateral/bodies.js +136 -0
- package/dist/v3/collateral/ext.d.ts +21 -0
- package/dist/v3/collateral/ext.d.ts.map +1 -0
- package/dist/v3/collateral/ext.js +19 -0
- package/dist/v3/collateral/health.d.ts +115 -0
- package/dist/v3/collateral/health.d.ts.map +1 -0
- package/dist/v3/collateral/health.js +255 -0
- package/dist/v3/collateral/index.d.ts +9 -0
- package/dist/v3/collateral/index.d.ts.map +1 -0
- package/dist/v3/collateral/index.js +8 -0
- package/dist/v3/collateral/local-health.d.ts +23 -0
- package/dist/v3/collateral/local-health.d.ts.map +1 -0
- package/dist/v3/collateral/local-health.js +27 -0
- package/dist/v3/collateral/math.d.ts +33 -0
- package/dist/v3/collateral/math.d.ts.map +1 -0
- package/dist/v3/collateral/math.js +38 -0
- package/dist/v3/collateral/openings.d.ts +27 -0
- package/dist/v3/collateral/openings.d.ts.map +1 -0
- package/dist/v3/collateral/openings.js +28 -0
- package/dist/v3/collateral/scan.d.ts +41 -0
- package/dist/v3/collateral/scan.d.ts.map +1 -0
- package/dist/v3/collateral/scan.js +76 -0
- package/dist/v3/collateral/seal.d.ts +31 -0
- package/dist/v3/collateral/seal.d.ts.map +1 -0
- package/dist/v3/collateral/seal.js +59 -0
- package/dist/v3/credential/issue.d.ts +27 -0
- package/dist/v3/credential/issue.d.ts.map +1 -0
- package/dist/v3/credential/issue.js +109 -0
- package/dist/v3/credential/membership.d.ts +37 -0
- package/dist/v3/credential/membership.d.ts.map +1 -0
- package/dist/v3/credential/membership.js +34 -0
- package/dist/v3/credential/pow.d.ts +10 -0
- package/dist/v3/credential/pow.d.ts.map +1 -0
- package/dist/v3/credential/pow.js +22 -0
- package/dist/v3/domains.d.ts +141 -0
- package/dist/v3/domains.d.ts.map +1 -0
- package/dist/v3/domains.js +158 -0
- package/dist/v3/ext.d.ts +29 -0
- package/dist/v3/ext.d.ts.map +1 -0
- package/dist/v3/ext.js +82 -0
- package/dist/v3/faucet.d.ts +66 -0
- package/dist/v3/faucet.d.ts.map +1 -0
- package/dist/v3/faucet.js +64 -0
- package/dist/v3/fee-witness.d.ts +5 -0
- package/dist/v3/fee-witness.d.ts.map +1 -0
- package/dist/v3/fee-witness.js +18 -0
- package/dist/v3/fees.d.ts +78 -0
- package/dist/v3/fees.d.ts.map +1 -0
- package/dist/v3/fees.js +190 -0
- package/dist/v3/index.d.ts +24 -0
- package/dist/v3/index.d.ts.map +1 -0
- package/dist/v3/index.js +23 -0
- package/dist/v3/inputs.d.ts +89 -0
- package/dist/v3/inputs.d.ts.map +1 -0
- package/dist/v3/inputs.js +152 -0
- package/dist/v3/keeper-badge.d.ts +48 -0
- package/dist/v3/keeper-badge.d.ts.map +1 -0
- package/dist/v3/keeper-badge.js +49 -0
- package/dist/v3/keeper-payload.d.ts +36 -0
- package/dist/v3/keeper-payload.d.ts.map +1 -0
- package/dist/v3/keeper-payload.js +116 -0
- package/dist/v3/kinds.d.ts +23 -0
- package/dist/v3/kinds.d.ts.map +1 -0
- package/dist/v3/kinds.js +50 -0
- package/dist/v3/loans/bodies.d.ts +215 -0
- package/dist/v3/loans/bodies.d.ts.map +1 -0
- package/dist/v3/loans/bodies.js +255 -0
- package/dist/v3/loans/offer-plan.d.ts +66 -0
- package/dist/v3/loans/offer-plan.d.ts.map +1 -0
- package/dist/v3/loans/offer-plan.js +141 -0
- package/dist/v3/loans/open-plan.d.ts +62 -0
- package/dist/v3/loans/open-plan.d.ts.map +1 -0
- package/dist/v3/loans/open-plan.js +92 -0
- package/dist/v3/loans/reclaim-plan.d.ts +65 -0
- package/dist/v3/loans/reclaim-plan.d.ts.map +1 -0
- package/dist/v3/loans/reclaim-plan.js +125 -0
- package/dist/v3/loans/refi-plan.d.ts +62 -0
- package/dist/v3/loans/refi-plan.d.ts.map +1 -0
- package/dist/v3/loans/refi-plan.js +157 -0
- package/dist/v3/loans/repay-plan.d.ts +40 -0
- package/dist/v3/loans/repay-plan.d.ts.map +1 -0
- package/dist/v3/loans/repay-plan.js +77 -0
- package/dist/v3/loans/settle-plan.d.ts +152 -0
- package/dist/v3/loans/settle-plan.d.ts.map +1 -0
- package/dist/v3/loans/settle-plan.js +210 -0
- package/dist/v3/market.d.ts +38 -0
- package/dist/v3/market.d.ts.map +1 -0
- package/dist/v3/market.js +30 -0
- package/dist/v3/notes/denominations.d.ts +13 -0
- package/dist/v3/notes/denominations.d.ts.map +1 -0
- package/dist/v3/notes/denominations.js +35 -0
- package/dist/v3/notes/deposit.d.ts +57 -0
- package/dist/v3/notes/deposit.d.ts.map +1 -0
- package/dist/v3/notes/deposit.js +103 -0
- package/dist/v3/notes/derive.d.ts +16 -0
- package/dist/v3/notes/derive.d.ts.map +1 -0
- package/dist/v3/notes/derive.js +34 -0
- package/dist/v3/notes/faucet.d.ts +3 -0
- package/dist/v3/notes/faucet.d.ts.map +1 -0
- package/dist/v3/notes/faucet.js +8 -0
- package/dist/v3/notes/index.d.ts +9 -0
- package/dist/v3/notes/index.d.ts.map +1 -0
- package/dist/v3/notes/index.js +8 -0
- package/dist/v3/notes/keys.d.ts +5 -0
- package/dist/v3/notes/keys.d.ts.map +1 -0
- package/dist/v3/notes/keys.js +11 -0
- package/dist/v3/notes/opening.d.ts +16 -0
- package/dist/v3/notes/opening.d.ts.map +1 -0
- package/dist/v3/notes/opening.js +62 -0
- package/dist/v3/notes/scan.d.ts +32 -0
- package/dist/v3/notes/scan.d.ts.map +1 -0
- package/dist/v3/notes/scan.js +71 -0
- package/dist/v3/notes/tree.d.ts +9 -0
- package/dist/v3/notes/tree.d.ts.map +1 -0
- package/dist/v3/notes/tree.js +31 -0
- package/dist/v3/notes/withdraw.d.ts +12 -0
- package/dist/v3/notes/withdraw.d.ts.map +1 -0
- package/dist/v3/notes/withdraw.js +27 -0
- package/dist/v3/proof-bytes.d.ts +4 -0
- package/dist/v3/proof-bytes.d.ts.map +1 -0
- package/dist/v3/proof-bytes.js +22 -0
- package/dist/v3/prover.d.ts +34 -0
- package/dist/v3/prover.d.ts.map +1 -0
- package/dist/v3/prover.js +76 -0
- package/dist/v3/relay/client.d.ts +57 -0
- package/dist/v3/relay/client.d.ts.map +1 -0
- package/dist/v3/relay/client.js +80 -0
- package/dist/v3/relay/possession.d.ts +21 -0
- package/dist/v3/relay/possession.d.ts.map +1 -0
- package/dist/v3/relay/possession.js +45 -0
- package/dist/v3/sales/auctioneer.d.ts +60 -0
- package/dist/v3/sales/auctioneer.d.ts.map +1 -0
- package/dist/v3/sales/auctioneer.js +130 -0
- package/dist/v3/sales/bcs.d.ts +4 -0
- package/dist/v3/sales/bcs.d.ts.map +1 -0
- package/dist/v3/sales/bcs.js +16 -0
- package/dist/v3/sales/bid.d.ts +51 -0
- package/dist/v3/sales/bid.d.ts.map +1 -0
- package/dist/v3/sales/bid.js +85 -0
- package/dist/v3/sales/bodies.d.ts +83 -0
- package/dist/v3/sales/bodies.d.ts.map +1 -0
- package/dist/v3/sales/bodies.js +78 -0
- package/dist/v3/sales/claim.d.ts +40 -0
- package/dist/v3/sales/claim.d.ts.map +1 -0
- package/dist/v3/sales/claim.js +44 -0
- package/dist/v3/sales/index.d.ts +10 -0
- package/dist/v3/sales/index.d.ts.map +1 -0
- package/dist/v3/sales/index.js +9 -0
- package/dist/v3/sales/open-kind.d.ts +42 -0
- package/dist/v3/sales/open-kind.d.ts.map +1 -0
- package/dist/v3/sales/open-kind.js +43 -0
- package/dist/v3/sales/outcome.d.ts +20 -0
- package/dist/v3/sales/outcome.d.ts.map +1 -0
- package/dist/v3/sales/outcome.js +21 -0
- package/dist/v3/sales/reclaim.d.ts +46 -0
- package/dist/v3/sales/reclaim.d.ts.map +1 -0
- package/dist/v3/sales/reclaim.js +57 -0
- package/dist/v3/sales/rows.d.ts +55 -0
- package/dist/v3/sales/rows.d.ts.map +1 -0
- package/dist/v3/sales/rows.js +98 -0
- package/dist/v3/sales/seller.d.ts +39 -0
- package/dist/v3/sales/seller.d.ts.map +1 -0
- package/dist/v3/sales/seller.js +67 -0
- package/dist/v3/stage1.d.ts +204 -0
- package/dist/v3/stage1.d.ts.map +1 -0
- package/dist/v3/stage1.js +575 -0
- package/package.json +33 -4
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import type { CollLockSpend } from "../collateral/bodies.ts";
|
|
2
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
3
|
+
import type { CircuitInput, SpendNote } from "../inputs.ts";
|
|
4
|
+
import { type RequestPlain } from "./open-plan.ts";
|
|
5
|
+
export declare const OFFER_PLAIN_LEN = 208;
|
|
6
|
+
export declare const LOAN_OFFER_KIND = 12;
|
|
7
|
+
/** The fields `requestPlaintext` wrote, minus its coin-types hash. */
|
|
8
|
+
export declare function decodeRequest(plain: Uint8Array): Omit<RequestPlain, "coinTypes">;
|
|
9
|
+
/** `membership::seal_listing(room, viewer).bind`. */
|
|
10
|
+
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
|
+
/** `rows::loan_ext`: keccak256("ess3/lext/" ‖ kind ‖ loan ‖ pool_l ‖ bcs(round) ‖ relayer ‖ bcs(encs) ‖ extra) mod R. */
|
|
33
|
+
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
|
+
//# sourceMappingURL=offer-plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"offer-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/offer-plan.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAG7D,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAG5D,OAAO,EAAiB,KAAK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
|
|
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_PLAIN_LEN } from "./open-plan.js";
|
|
9
|
+
export const OFFER_PLAIN_LEN = 208;
|
|
10
|
+
export const LOAN_OFFER_KIND = 12;
|
|
11
|
+
const ascii = (s) => Uint8Array.from(Buffer.from(s, "ascii"));
|
|
12
|
+
const blake = (bytes) => blake2b(bytes, { dkLen: 32 });
|
|
13
|
+
const cat = (...parts) => Uint8Array.from(parts.flatMap((p) => [...p]));
|
|
14
|
+
const toBig = (bytes) => BigInt(`0x${Buffer.from(bytes).toString("hex")}`);
|
|
15
|
+
function le(value, size) {
|
|
16
|
+
const out = new Uint8Array(size);
|
|
17
|
+
for (let i = 0; i < size; i++)
|
|
18
|
+
out[i] = Number((value >> BigInt(8 * i)) & 0xffn);
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
function readLe(bytes, at, size) {
|
|
22
|
+
let v = 0n;
|
|
23
|
+
for (let i = size - 1; i >= 0; i--)
|
|
24
|
+
v = (v << 8n) | BigInt(bytes[at + i]);
|
|
25
|
+
return v;
|
|
26
|
+
}
|
|
27
|
+
const addressBytes = (id) => le(BigInt(id), 32).reverse();
|
|
28
|
+
function uleb(n) {
|
|
29
|
+
const out = [];
|
|
30
|
+
for (; n >= 0x80; n >>>= 7)
|
|
31
|
+
out.push((n & 0x7f) | 0x80);
|
|
32
|
+
return [...out, n];
|
|
33
|
+
}
|
|
34
|
+
/** The fields `requestPlaintext` wrote, minus its coin-types hash. */
|
|
35
|
+
export function decodeRequest(plain) {
|
|
36
|
+
if (plain.length !== REQ_PLAIN_LEN)
|
|
37
|
+
throw new Error("the request plaintext is not 232 bytes");
|
|
38
|
+
return {
|
|
39
|
+
principal: readLe(plain, 0, 8),
|
|
40
|
+
maxRateBps: readLe(plain, 8, 8),
|
|
41
|
+
termMs: readLe(plain, 16, 8),
|
|
42
|
+
borrowerPk: readLe(plain, 24, 32),
|
|
43
|
+
collAmt: readLe(plain, 56, 8),
|
|
44
|
+
collBlind: readLe(plain, 64, 32),
|
|
45
|
+
salt: readLe(plain, 96, 32),
|
|
46
|
+
closeMs: readLe(plain, 128, 8),
|
|
47
|
+
market: `0x${Buffer.from(plain.slice(136, 168)).toString("hex")}`,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/** `membership::seal_listing(room, viewer).bind`. */
|
|
51
|
+
export const sealListingBind = (room, viewer) => blake(cat(ascii("essey-v3/seal/list"), addressBytes(room), addressBytes(viewer)));
|
|
52
|
+
/** `loan::offer_key_id`: blake2b256("ess3/offer" ‖ bcs(loan_key) ‖ bcs(round) ‖ bcs(seq)). */
|
|
53
|
+
export const offerKeyId = (loanKey, round, seq) => blake(cat(ascii("ess3/offer"), le(loanKey, 32), le(round, 8), le(seq, 8)));
|
|
54
|
+
/** `loan::lock_field`. */
|
|
55
|
+
export const lockField = (loanKey, round) => poseidonHash([DOM_ROUND, loanKey, round]);
|
|
56
|
+
/** `loan::offer_room`: the id of 2^255 + lock_field. */
|
|
57
|
+
export const offerRoom = (loanKey, round) => `0x${((1n << 255n) + lockField(loanKey, round)).toString(16).padStart(64, "0")}`;
|
|
58
|
+
export const offerCommit = (rate, salt, lenderPk, encX, encY) => poseidonHash([DOM_OFFER, rate, salt, lenderPk, poseidonHash([encX, encY])]);
|
|
59
|
+
/** Lock4 of `lib/lock.circom`: the principal locked to the round's field and the lender. */
|
|
60
|
+
export function lock4(field, ownerPk, ns, amount, b) {
|
|
61
|
+
return noteCommitment(amount, poseidonHash([DOM_LOCK, field, ownerPk, poseidonHash([ns])]), b);
|
|
62
|
+
}
|
|
63
|
+
/** A-SPEC §4 offer `sealed`, 208 bytes little-endian. */
|
|
64
|
+
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));
|
|
65
|
+
/** `rows::loan_ext`: keccak256("ess3/lext/" ‖ kind ‖ loan ‖ pool_l ‖ bcs(round) ‖ relayer ‖ bcs(encs) ‖ extra) mod R. */
|
|
66
|
+
export function loanExt(kind, loan, poolL, round, relayer, encs, extra) {
|
|
67
|
+
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);
|
|
68
|
+
return toBig(keccak_256(bytes)) % R;
|
|
69
|
+
}
|
|
70
|
+
/** Witness for `circuits/v3/loan_offer/loan_offer.circom` (`ref.mjs` loanOfferCase). Publics: lock, change, fee_note, hCtx, root, n0, n1. */
|
|
71
|
+
export async function loanOffer(p) {
|
|
72
|
+
const { request: q, offer: o } = p;
|
|
73
|
+
if (o.amount !== q.principal)
|
|
74
|
+
throw new Error("the offer locks the requested principal");
|
|
75
|
+
const lane = await feeLane({ ...p.fee, base: 0n, eph: p.eph });
|
|
76
|
+
const changeAmount = p.ins[0].amount + p.ins[1].amount - q.principal - lane.fee - p.relayerFee;
|
|
77
|
+
if (changeAmount < 0n)
|
|
78
|
+
throw new Error("inputs do not cover the principal");
|
|
79
|
+
const commitments = p.ins.map((n) => noteCommitment(n.amount, notePk(n.sk), n.blinding));
|
|
80
|
+
const [n0, n1] = p.ins.map((n, i) => noteNullifier(commitments[i], n.sk, n.pathIndex));
|
|
81
|
+
const field = lockField(p.loanKey, p.round);
|
|
82
|
+
const lock = lock4(field, o.lenderPk, o.ns, q.principal, o.b);
|
|
83
|
+
const change = noteCommitment(changeAmount, p.change.pk, p.change.blinding);
|
|
84
|
+
const commit = offerCommit(o.rateBps, o.salt, o.lenderPk, o.encX, o.encY);
|
|
85
|
+
const reqCommit = poseidonHash([q.principal, q.maxRateBps, q.termMs, q.borrowerPk, q.collNote, q.salt]);
|
|
86
|
+
const hCtx = poseidonHash([
|
|
87
|
+
DOM_A1,
|
|
88
|
+
reqCommit,
|
|
89
|
+
field,
|
|
90
|
+
commit,
|
|
91
|
+
lane.fee_commit,
|
|
92
|
+
lane.ephX,
|
|
93
|
+
lane.ephY,
|
|
94
|
+
lane.fee_ct,
|
|
95
|
+
p.extHash,
|
|
96
|
+
p.relayerFee,
|
|
97
|
+
]);
|
|
98
|
+
const input = {
|
|
99
|
+
root: p.root.toString(),
|
|
100
|
+
n0: n0.toString(),
|
|
101
|
+
n1: n1.toString(),
|
|
102
|
+
inAmount: p.ins.map((n) => n.amount.toString()),
|
|
103
|
+
inPrivateKey: p.ins.map((n) => n.sk.toString()),
|
|
104
|
+
inBlinding: p.ins.map((n) => n.blinding.toString()),
|
|
105
|
+
inPathIndices: p.ins.map((n) => n.pathIndex.toString()),
|
|
106
|
+
inPathElements: p.ins.map((n) => n.pathElements.map(String)),
|
|
107
|
+
principal: q.principal.toString(),
|
|
108
|
+
max_rate_bps: q.maxRateBps.toString(),
|
|
109
|
+
term_ms: q.termMs.toString(),
|
|
110
|
+
borrowerPk: q.borrowerPk.toString(),
|
|
111
|
+
collNote0: q.collNote.toString(),
|
|
112
|
+
salt_req: q.salt.toString(),
|
|
113
|
+
loanKey: p.loanKey.toString(),
|
|
114
|
+
round: p.round.toString(),
|
|
115
|
+
rate_bps: o.rateBps.toString(),
|
|
116
|
+
salt: o.salt.toString(),
|
|
117
|
+
lenderPk: o.lenderPk.toString(),
|
|
118
|
+
encX: o.encX.toString(),
|
|
119
|
+
encY: o.encY.toString(),
|
|
120
|
+
ns: o.ns.toString(),
|
|
121
|
+
b: o.b.toString(),
|
|
122
|
+
cAmt: changeAmount.toString(),
|
|
123
|
+
cPk: p.change.pk.toString(),
|
|
124
|
+
cBlind: p.change.blinding.toString(),
|
|
125
|
+
relayer_fee: p.relayerFee.toString(),
|
|
126
|
+
extHash: p.extHash.toString(),
|
|
127
|
+
...feeWitness(p.fee, 0n, p.eph, lane),
|
|
128
|
+
};
|
|
129
|
+
const spend = {
|
|
130
|
+
root: p.root,
|
|
131
|
+
n0,
|
|
132
|
+
n1,
|
|
133
|
+
note: lock,
|
|
134
|
+
change,
|
|
135
|
+
feeNote: lane.fee_note,
|
|
136
|
+
ephX: lane.ephX,
|
|
137
|
+
ephY: lane.ephY,
|
|
138
|
+
feeCt: lane.fee_ct,
|
|
139
|
+
};
|
|
140
|
+
return { input, publics: [lock, change, lane.fee_note, hCtx, p.root, n0, n1], spend, commit, changeAmount };
|
|
141
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { CollLockSpend } from "../collateral/bodies.ts";
|
|
2
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
3
|
+
import type { CircuitInput, SpendNote } from "../inputs.ts";
|
|
4
|
+
export declare const REQ_PLAIN_LEN = 232;
|
|
5
|
+
export declare const COLL_LOCK_LEVELS = 20;
|
|
6
|
+
/** `keys::id_of_key`: the open pass room, and the loan id the OPEN exts bind (`liquidation.move` lock_collateral). */
|
|
7
|
+
export declare const keyId: (loanKey: bigint) => string;
|
|
8
|
+
/** `loan::request_key_id`: blake2b256("ess3/req" ‖ bcs(loan_key) ‖ "req"). */
|
|
9
|
+
export declare const requestKeyId: (loanKey: bigint) => Uint8Array<ArrayBufferLike> & Uint8Array<ArrayBuffer>;
|
|
10
|
+
export type OpenArgs = {
|
|
11
|
+
reqCommit: bigint;
|
|
12
|
+
reqSealed: Uint8Array;
|
|
13
|
+
closeMs: bigint;
|
|
14
|
+
market: string;
|
|
15
|
+
collateralCommit: bigint;
|
|
16
|
+
};
|
|
17
|
+
/** `loan::action_hash(loan_key, 0, "open", loan::open_args(l))`, the n0 the borrower pass binds. */
|
|
18
|
+
export declare function openAction(loanKey: bigint, a: OpenArgs): bigint;
|
|
19
|
+
export type RequestPlain = {
|
|
20
|
+
principal: bigint;
|
|
21
|
+
maxRateBps: bigint;
|
|
22
|
+
termMs: bigint;
|
|
23
|
+
borrowerPk: bigint;
|
|
24
|
+
collAmt: bigint;
|
|
25
|
+
collBlind: bigint;
|
|
26
|
+
salt: bigint;
|
|
27
|
+
closeMs: bigint;
|
|
28
|
+
market: string;
|
|
29
|
+
coinTypes: [string, string];
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* A-SPEC §4 `req_sealed`, 232 bytes little-endian. The spec names a "coin types hash" without its form; this SDK
|
|
33
|
+
* takes blake2b256 of the collateral type, "/", then the loan type.
|
|
34
|
+
*/
|
|
35
|
+
export declare function requestPlaintext(q: RequestPlain): Uint8Array;
|
|
36
|
+
export type CollLockParams = {
|
|
37
|
+
root: bigint;
|
|
38
|
+
ins: [SpendNote, SpendNote];
|
|
39
|
+
lock: {
|
|
40
|
+
amount: bigint;
|
|
41
|
+
pk: bigint;
|
|
42
|
+
blinding: bigint;
|
|
43
|
+
};
|
|
44
|
+
change: {
|
|
45
|
+
pk: bigint;
|
|
46
|
+
blinding: bigint;
|
|
47
|
+
};
|
|
48
|
+
relayerFee: bigint;
|
|
49
|
+
loanKey: bigint;
|
|
50
|
+
extHash: bigint;
|
|
51
|
+
fee: FeeCommitInput;
|
|
52
|
+
eph: bigint;
|
|
53
|
+
};
|
|
54
|
+
export type CollLock = {
|
|
55
|
+
input: CircuitInput;
|
|
56
|
+
publics: bigint[];
|
|
57
|
+
spend: CollLockSpend;
|
|
58
|
+
changeAmount: bigint;
|
|
59
|
+
};
|
|
60
|
+
/** Witness for `circuits/v3/coll_lock/coll_lock.circom`. Publics: lock, change, fee_note, hCtx, root, n0, n1. */
|
|
61
|
+
export declare function collLock(p: CollLockParams): Promise<CollLock>;
|
|
62
|
+
//# sourceMappingURL=open-plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3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c,GAAG;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,GAAG,EAAE,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC;IAC5B,IAAI,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACvD,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;IACzC,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,QAAQ,GAAG;IAAE,KAAK,EAAE,YAAY,CAAC;IAAC,OAAO,EAAE,MAAM,EAAE,CAAC;IAAC,KAAK,EAAE,aAAa,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9G,iHAAiH;AACjH,wBAAsB,QAAQ,CAAC,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoDnE"}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { blake2b } from "@noble/hashes/blake2.js";
|
|
2
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
3
|
+
import { DOM_CL, R } from "../domains.js";
|
|
4
|
+
import { feeWitness } from "../fee-witness.js";
|
|
5
|
+
import { feeLane } from "../fees.js";
|
|
6
|
+
import { noteCommitment, noteNullifier, notePk } from "../notes/keys.js";
|
|
7
|
+
export const REQ_PLAIN_LEN = 232;
|
|
8
|
+
export const COLL_LOCK_LEVELS = 20;
|
|
9
|
+
const ascii = (s) => Uint8Array.from(Buffer.from(s, "ascii"));
|
|
10
|
+
const blake = (bytes) => blake2b(bytes, { dkLen: 32 });
|
|
11
|
+
const cat = (...parts) => Uint8Array.from(parts.flatMap((p) => [...p]));
|
|
12
|
+
function le(value, size) {
|
|
13
|
+
const out = new Uint8Array(size);
|
|
14
|
+
for (let i = 0; i < size; i++)
|
|
15
|
+
out[i] = Number((value >> BigInt(8 * i)) & 0xffn);
|
|
16
|
+
return out;
|
|
17
|
+
}
|
|
18
|
+
const addressBytes = (id) => le(BigInt(id), 32).reverse();
|
|
19
|
+
const toBig = (bytes) => BigInt(`0x${Buffer.from(bytes).toString("hex")}`);
|
|
20
|
+
/** `keys::id_of_key`: the open pass room, and the loan id the OPEN exts bind (`liquidation.move` lock_collateral). */
|
|
21
|
+
export const keyId = (loanKey) => `0x${loanKey.toString(16).padStart(64, "0")}`;
|
|
22
|
+
/** `loan::request_key_id`: blake2b256("ess3/req" ‖ bcs(loan_key) ‖ "req"). */
|
|
23
|
+
export const requestKeyId = (loanKey) => blake(cat(ascii("ess3/req"), le(loanKey, 32), ascii("req")));
|
|
24
|
+
/** `loan::action_hash(loan_key, 0, "open", loan::open_args(l))`, the n0 the borrower pass binds. */
|
|
25
|
+
export function openAction(loanKey, a) {
|
|
26
|
+
const args = cat(le(a.reqCommit, 32), blake(a.reqSealed), le(a.closeMs, 8), addressBytes(a.market), le(a.collateralCommit, 32));
|
|
27
|
+
return toBig(blake(cat(ascii("ess3/act/open/"), le(loanKey, 32), le(0n, 8), args))) % R;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A-SPEC §4 `req_sealed`, 232 bytes little-endian. The spec names a "coin types hash" without its form; this SDK
|
|
31
|
+
* takes blake2b256 of the collateral type, "/", then the loan type.
|
|
32
|
+
*/
|
|
33
|
+
export function requestPlaintext(q) {
|
|
34
|
+
const body = cat(le(q.principal, 8), le(q.maxRateBps, 8), le(q.termMs, 8), le(q.borrowerPk, 32), le(q.collAmt, 8), le(q.collBlind, 32), le(q.salt, 32), le(q.closeMs, 8), addressBytes(q.market), blake(ascii(`${q.coinTypes[0]}/${q.coinTypes[1]}`)));
|
|
35
|
+
return cat(body, new Uint8Array(REQ_PLAIN_LEN - body.length));
|
|
36
|
+
}
|
|
37
|
+
/** Witness for `circuits/v3/coll_lock/coll_lock.circom`. Publics: lock, change, fee_note, hCtx, root, n0, n1. */
|
|
38
|
+
export async function collLock(p) {
|
|
39
|
+
for (const note of p.ins)
|
|
40
|
+
if (note.pathElements.length !== COLL_LOCK_LEVELS)
|
|
41
|
+
throw new Error("a spend needs 20 path elements");
|
|
42
|
+
const lane = await feeLane({ ...p.fee, base: 0n, eph: p.eph });
|
|
43
|
+
const changeAmount = p.ins[0].amount + p.ins[1].amount - p.lock.amount - lane.fee - p.relayerFee;
|
|
44
|
+
if (changeAmount < 0n)
|
|
45
|
+
throw new Error("inputs do not cover the collateral");
|
|
46
|
+
const commitments = p.ins.map((n) => noteCommitment(n.amount, notePk(n.sk), n.blinding));
|
|
47
|
+
const [n0, n1] = p.ins.map((n, i) => noteNullifier(commitments[i], n.sk, n.pathIndex));
|
|
48
|
+
const lock = noteCommitment(p.lock.amount, p.lock.pk, p.lock.blinding);
|
|
49
|
+
const change = noteCommitment(changeAmount, p.change.pk, p.change.blinding);
|
|
50
|
+
const hCtx = poseidonHash([
|
|
51
|
+
DOM_CL,
|
|
52
|
+
p.loanKey,
|
|
53
|
+
lane.fee_commit,
|
|
54
|
+
lane.ephX,
|
|
55
|
+
lane.ephY,
|
|
56
|
+
lane.fee_ct,
|
|
57
|
+
p.extHash,
|
|
58
|
+
p.relayerFee,
|
|
59
|
+
]);
|
|
60
|
+
const input = {
|
|
61
|
+
root: p.root.toString(),
|
|
62
|
+
n0: n0.toString(),
|
|
63
|
+
n1: n1.toString(),
|
|
64
|
+
inAmount: p.ins.map((n) => n.amount.toString()),
|
|
65
|
+
inPrivateKey: p.ins.map((n) => n.sk.toString()),
|
|
66
|
+
inBlinding: p.ins.map((n) => n.blinding.toString()),
|
|
67
|
+
inPathIndices: p.ins.map((n) => n.pathIndex.toString()),
|
|
68
|
+
inPathElements: p.ins.map((n) => n.pathElements.map(String)),
|
|
69
|
+
amt: p.lock.amount.toString(),
|
|
70
|
+
lockPk: p.lock.pk.toString(),
|
|
71
|
+
b: p.lock.blinding.toString(),
|
|
72
|
+
cAmt: changeAmount.toString(),
|
|
73
|
+
cPk: p.change.pk.toString(),
|
|
74
|
+
cBlind: p.change.blinding.toString(),
|
|
75
|
+
relayer_fee: p.relayerFee.toString(),
|
|
76
|
+
loanKey: p.loanKey.toString(),
|
|
77
|
+
extHash: p.extHash.toString(),
|
|
78
|
+
...feeWitness(p.fee, 0n, p.eph, lane),
|
|
79
|
+
};
|
|
80
|
+
const spend = {
|
|
81
|
+
root: p.root,
|
|
82
|
+
n0,
|
|
83
|
+
n1,
|
|
84
|
+
note: lock,
|
|
85
|
+
change,
|
|
86
|
+
feeNote: lane.fee_note,
|
|
87
|
+
ephX: lane.ephX,
|
|
88
|
+
ephY: lane.ephY,
|
|
89
|
+
feeCt: lane.fee_ct,
|
|
90
|
+
};
|
|
91
|
+
return { input, publics: [lock, change, lane.fee_note, hCtx, p.root, n0, n1], spend, changeAmount };
|
|
92
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
2
|
+
import type { CircuitInput } from "../inputs.ts";
|
|
3
|
+
export declare const RECLAIM_COLL_KIND = 17n;
|
|
4
|
+
/** `rows::enc_hash`: keccak256 of bcs(vector<vector<u8>>), read big-endian, mod R. */
|
|
5
|
+
export declare function encHash(encs: Uint8Array[]): bigint;
|
|
6
|
+
export type ReclaimCollParams = {
|
|
7
|
+
loanKey: bigint;
|
|
8
|
+
collAmt: bigint;
|
|
9
|
+
borrowerSk: bigint;
|
|
10
|
+
collBlind: bigint;
|
|
11
|
+
out: {
|
|
12
|
+
pk: bigint;
|
|
13
|
+
blinding: bigint;
|
|
14
|
+
};
|
|
15
|
+
encs: Uint8Array[];
|
|
16
|
+
fee: FeeCommitInput;
|
|
17
|
+
eph: bigint;
|
|
18
|
+
};
|
|
19
|
+
/** Witness for `circuits/v3/reclaim_coll/reclaim_coll.circom` (`ref.mjs`). Publics: lock, N, loan_key, outNote, hCtx. */
|
|
20
|
+
export declare function reclaimColl(p: ReclaimCollParams): Promise<{
|
|
21
|
+
input: CircuitInput;
|
|
22
|
+
publics: bigint[];
|
|
23
|
+
lock: bigint;
|
|
24
|
+
n: bigint;
|
|
25
|
+
outNote: bigint;
|
|
26
|
+
outAmt: bigint;
|
|
27
|
+
feeNote: bigint;
|
|
28
|
+
ephX: bigint;
|
|
29
|
+
ephY: bigint;
|
|
30
|
+
feeCt: bigint;
|
|
31
|
+
}>;
|
|
32
|
+
export type ReclaimLockParams = {
|
|
33
|
+
locks: bigint[];
|
|
34
|
+
slot: number;
|
|
35
|
+
field: bigint;
|
|
36
|
+
amount: bigint;
|
|
37
|
+
ownerSk: bigint;
|
|
38
|
+
ns: bigint;
|
|
39
|
+
b: bigint;
|
|
40
|
+
out: {
|
|
41
|
+
pk: bigint;
|
|
42
|
+
blinding: bigint;
|
|
43
|
+
};
|
|
44
|
+
relayer: string;
|
|
45
|
+
relayerFee: bigint;
|
|
46
|
+
encs: Uint8Array[];
|
|
47
|
+
fee: FeeCommitInput;
|
|
48
|
+
eph: bigint;
|
|
49
|
+
/** A sale room's `residual`, which slot 16 selects. A loan round has none. */
|
|
50
|
+
residual?: bigint;
|
|
51
|
+
};
|
|
52
|
+
/** Witness for `circuits/v3/reclaim/reclaim.circom`. Publics: hLock, residual, hCtx, N, hOut. */
|
|
53
|
+
export declare function reclaimLock(p: ReclaimLockParams): Promise<{
|
|
54
|
+
input: CircuitInput;
|
|
55
|
+
publics: bigint[];
|
|
56
|
+
n: bigint;
|
|
57
|
+
outNote: bigint;
|
|
58
|
+
outAmt: bigint;
|
|
59
|
+
feeCommit: bigint;
|
|
60
|
+
feeNote: bigint;
|
|
61
|
+
ephX: bigint;
|
|
62
|
+
ephY: bigint;
|
|
63
|
+
feeCt: bigint;
|
|
64
|
+
}>;
|
|
65
|
+
//# sourceMappingURL=reclaim-plan.d.ts.map
|
|
@@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Ac,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;IACZ,8EAA8E;IAC9E,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,iGAAiG;AACjG,wBAAsB,WAAW,CAAC,CAAC,EAAE,iBAAiB;;;;;;;;;;;GAgDrD"}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
2
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
3
|
+
import { addrField } from "../addr-field.js";
|
|
4
|
+
import { collLockPk } from "../collateral/openings.js";
|
|
5
|
+
import { DOM_CNULL, DOM_LNULL, DOM_LOCK, R } from "../domains.js";
|
|
6
|
+
import { feeWitness } from "../fee-witness.js";
|
|
7
|
+
import { feeLane } from "../fees.js";
|
|
8
|
+
import { noteCommitment, notePk } from "../notes/keys.js";
|
|
9
|
+
export const RECLAIM_COLL_KIND = 17n;
|
|
10
|
+
const NO_ADDRESS = `0x${"0".repeat(64)}`;
|
|
11
|
+
function uleb(n) {
|
|
12
|
+
const out = [];
|
|
13
|
+
for (; n >= 0x80; n >>>= 7)
|
|
14
|
+
out.push((n & 0x7f) | 0x80);
|
|
15
|
+
return [...out, n];
|
|
16
|
+
}
|
|
17
|
+
/** `rows::enc_hash`: keccak256 of bcs(vector<vector<u8>>), read big-endian, mod R. */
|
|
18
|
+
export function encHash(encs) {
|
|
19
|
+
const bcs = Uint8Array.from([...uleb(encs.length), ...encs.flatMap((e) => [...uleb(e.length), ...e])]);
|
|
20
|
+
return BigInt(`0x${Buffer.from(keccak_256(bcs)).toString("hex")}`) % R;
|
|
21
|
+
}
|
|
22
|
+
/** Witness for `circuits/v3/reclaim_coll/reclaim_coll.circom` (`ref.mjs`). Publics: lock, N, loan_key, outNote, hCtx. */
|
|
23
|
+
export async function reclaimColl(p) {
|
|
24
|
+
const lane = await feeLane({ ...p.fee, base: 0n, eph: p.eph });
|
|
25
|
+
const outAmt = p.collAmt - lane.fee;
|
|
26
|
+
if (outAmt < 0n)
|
|
27
|
+
throw new Error("the fee is above the collateral");
|
|
28
|
+
const lock = noteCommitment(p.collAmt, collLockPk(p.loanKey, notePk(p.borrowerSk)), p.collBlind);
|
|
29
|
+
const n = poseidonHash([DOM_CNULL, p.loanKey, lock]);
|
|
30
|
+
const outNote = noteCommitment(outAmt, p.out.pk, p.out.blinding);
|
|
31
|
+
const relayerField = addrField(NO_ADDRESS);
|
|
32
|
+
const hash = encHash(p.encs);
|
|
33
|
+
const hCtx = poseidonHash([
|
|
34
|
+
RECLAIM_COLL_KIND,
|
|
35
|
+
p.loanKey,
|
|
36
|
+
outNote,
|
|
37
|
+
lane.fee_commit,
|
|
38
|
+
lane.fee_note,
|
|
39
|
+
lane.ephX,
|
|
40
|
+
lane.ephY,
|
|
41
|
+
lane.fee_ct,
|
|
42
|
+
relayerField,
|
|
43
|
+
0n,
|
|
44
|
+
hash,
|
|
45
|
+
]);
|
|
46
|
+
const input = {
|
|
47
|
+
loan_key: p.loanKey.toString(),
|
|
48
|
+
collAmt: p.collAmt.toString(),
|
|
49
|
+
borrowerSk: p.borrowerSk.toString(),
|
|
50
|
+
collBlind: p.collBlind.toString(),
|
|
51
|
+
outAmt: outAmt.toString(),
|
|
52
|
+
outPk: p.out.pk.toString(),
|
|
53
|
+
outB: p.out.blinding.toString(),
|
|
54
|
+
relayerField: relayerField.toString(),
|
|
55
|
+
relayer_fee: "0",
|
|
56
|
+
encHash: hash.toString(),
|
|
57
|
+
...feeWitness(p.fee, 0n, p.eph, lane),
|
|
58
|
+
};
|
|
59
|
+
return {
|
|
60
|
+
input,
|
|
61
|
+
publics: [lock, n, p.loanKey, outNote, hCtx],
|
|
62
|
+
lock,
|
|
63
|
+
n,
|
|
64
|
+
outNote,
|
|
65
|
+
outAmt,
|
|
66
|
+
feeNote: lane.fee_note,
|
|
67
|
+
ephX: lane.ephX,
|
|
68
|
+
ephY: lane.ephY,
|
|
69
|
+
feeCt: lane.fee_ct,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const RECLAIM_VERSION = 3n;
|
|
73
|
+
const LOCK_SLOTS = 16;
|
|
74
|
+
/** Witness for `circuits/v3/reclaim/reclaim.circom`. Publics: hLock, residual, hCtx, N, hOut. */
|
|
75
|
+
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;
|
|
83
|
+
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]);
|
|
90
|
+
const outNote = noteCommitment(outAmt, p.out.pk, p.out.blinding);
|
|
91
|
+
const relayerField = addrField(p.relayer);
|
|
92
|
+
const hash = encHash(p.encs);
|
|
93
|
+
const s = (xs) => xs.map(String);
|
|
94
|
+
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
|
+
field: p.field.toString(),
|
|
99
|
+
amount: p.amount.toString(),
|
|
100
|
+
ownerSk: p.ownerSk.toString(),
|
|
101
|
+
nsEff: p.ns.toString(),
|
|
102
|
+
b: p.b.toString(),
|
|
103
|
+
outAmt: outAmt.toString(),
|
|
104
|
+
outPk: p.out.pk.toString(),
|
|
105
|
+
outB: p.out.blinding.toString(),
|
|
106
|
+
relayerField: relayerField.toString(),
|
|
107
|
+
relayer_fee: p.relayerFee.toString(),
|
|
108
|
+
encHash: hash.toString(),
|
|
109
|
+
...feeWitness(p.fee, 0n, p.eph, lane),
|
|
110
|
+
};
|
|
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
|
+
return {
|
|
114
|
+
input,
|
|
115
|
+
publics: [poseidonHash(locks), residual, hCtx, n, hOut],
|
|
116
|
+
n,
|
|
117
|
+
outNote,
|
|
118
|
+
outAmt,
|
|
119
|
+
feeCommit: lane.fee_commit,
|
|
120
|
+
feeNote: lane.fee_note,
|
|
121
|
+
ephX: lane.ephX,
|
|
122
|
+
ephY: lane.ephY,
|
|
123
|
+
feeCt: lane.fee_ct,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
2
|
+
import type { CircuitInput } from "../inputs.ts";
|
|
3
|
+
import type { KeeperTerms } from "../keeper-payload.ts";
|
|
4
|
+
import { type SettleSlot } from "./settle-plan.ts";
|
|
5
|
+
export declare const LOAN_REFI_KIND = 15;
|
|
6
|
+
export type LoanRefiParams = {
|
|
7
|
+
slots: SettleSlot[];
|
|
8
|
+
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
|
+
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
|
+
pad: {
|
|
33
|
+
pk: bigint;
|
|
34
|
+
blinding: bigint;
|
|
35
|
+
};
|
|
36
|
+
e: bigint;
|
|
37
|
+
eP: bigint;
|
|
38
|
+
nowS: bigint;
|
|
39
|
+
extHash: bigint;
|
|
40
|
+
fee: FeeCommitInput;
|
|
41
|
+
eph: bigint;
|
|
42
|
+
};
|
|
43
|
+
/** Witness and Move arguments of `circuits/v3/loan_refi/loan_refi.circom` (`ref.mjs` loanRefiCase). */
|
|
44
|
+
export declare function loanRefi(p: LoanRefiParams): Promise<{
|
|
45
|
+
input: CircuitInput;
|
|
46
|
+
publics: bigint[];
|
|
47
|
+
winner: number;
|
|
48
|
+
terms: KeeperTerms;
|
|
49
|
+
termsCommit: bigint;
|
|
50
|
+
borrowerNote: bigint;
|
|
51
|
+
borrowerValue: bigint;
|
|
52
|
+
feeNote: bigint;
|
|
53
|
+
winNull: bigint;
|
|
54
|
+
d: [bigint, bigint, bigint, bigint];
|
|
55
|
+
ephX: bigint;
|
|
56
|
+
ephY: bigint;
|
|
57
|
+
feeCt: bigint;
|
|
58
|
+
payoffNote: bigint;
|
|
59
|
+
pad: bigint;
|
|
60
|
+
ephP: [bigint, bigint];
|
|
61
|
+
}>;
|
|
62
|
+
//# sourceMappingURL=refi-plan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"refi-plan.d.ts","sourceRoot":"","sources":["../../../src/v3/loans/refi-plan.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAg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