@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,33 @@
|
|
|
1
|
+
export declare const GRACE_S = 300n;
|
|
2
|
+
export declare const HB_STEP_BPS = 500n;
|
|
3
|
+
export declare const K_MIN = 1;
|
|
4
|
+
export declare const K_MAX = 8;
|
|
5
|
+
/** The market's two decimals and the reading's exponent, each 0..18 (`circuits/v3/lib/value.circom`). */
|
|
6
|
+
export type Scale = {
|
|
7
|
+
cDec: bigint;
|
|
8
|
+
lDec: bigint;
|
|
9
|
+
expo: bigint;
|
|
10
|
+
};
|
|
11
|
+
export type Reading = {
|
|
12
|
+
spot: bigint;
|
|
13
|
+
ema: bigint;
|
|
14
|
+
expo: bigint;
|
|
15
|
+
};
|
|
16
|
+
/** `ValueAt`: floor(amount · price · 10^l_dec / 10^(c_dec + expo)), exact, with the remainder the circuit takes. */
|
|
17
|
+
export declare function valueAt(amount: bigint, price: bigint, s: Scale): {
|
|
18
|
+
q: bigint;
|
|
19
|
+
r: bigint;
|
|
20
|
+
};
|
|
21
|
+
/** `OverLine`: debt · 10000 > value · bps. Equality is on the healthy side. */
|
|
22
|
+
export declare const overLine: (value: bigint, debt: bigint, bps: bigint) => boolean;
|
|
23
|
+
/**
|
|
24
|
+
* `CollFor`: floor(value · 10^(c_dec + expo) / (price · 10^l_dec)). The circuit divides after cancelling the
|
|
25
|
+
* common power of ten, so the remainder is the reduced one (`circuits/v3/health/ref.mjs:58-65`).
|
|
26
|
+
*/
|
|
27
|
+
export declare function collFor(value: bigint, price: bigint, s: Scale): {
|
|
28
|
+
q: bigint;
|
|
29
|
+
r: bigint;
|
|
30
|
+
};
|
|
31
|
+
/** `heartbeat_floor` (`liquidation.move:451-455`): floor(min(spot, ema) · (10000 - k · 500) / 10000), k in 1..8. */
|
|
32
|
+
export declare function heartbeatFloor(r: Pick<Reading, "spot" | "ema">, k: number): bigint;
|
|
33
|
+
//# sourceMappingURL=math.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/math.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,OAAO,OAAO,CAAC;AAC5B,eAAO,MAAM,WAAW,OAAO,CAAC;AAChC,eAAO,MAAM,KAAK,IAAI,CAAC;AACvB,eAAO,MAAM,KAAK,IAAI,CAAC;AAIvB,yGAAyG;AACzG,MAAM,MAAM,KAAK,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjE,MAAM,MAAM,OAAO,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAOlE,oHAAoH;AACpH,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAKzF;AAED,+EAA+E;AAC/E,eAAO,MAAM,QAAQ,GAAI,OAAO,MAAM,EAAE,MAAM,MAAM,EAAE,KAAK,MAAM,YAAmC,CAAC;AAErG;;;GAGG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,GAAG;IAAE,CAAC,EAAE,MAAM,CAAC;IAAC,CAAC,EAAE,MAAM,CAAA;CAAE,CAMxF;AAED,oHAAoH;AACpH,wBAAgB,cAAc,CAAC,CAAC,EAAE,IAAI,CAAC,OAAO,EAAE,MAAM,GAAG,KAAK,CAAC,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAIlF"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { BPS_DENOM } from "../fees.js";
|
|
2
|
+
export const GRACE_S = 300n;
|
|
3
|
+
export const HB_STEP_BPS = 500n;
|
|
4
|
+
export const K_MIN = 1;
|
|
5
|
+
export const K_MAX = 8;
|
|
6
|
+
const MAX_DEC = 18n;
|
|
7
|
+
function assertScale(s) {
|
|
8
|
+
for (const [name, value] of Object.entries(s))
|
|
9
|
+
if (value < 0n || value > MAX_DEC)
|
|
10
|
+
throw new Error(`${name} is not in 0..18`);
|
|
11
|
+
}
|
|
12
|
+
/** `ValueAt`: floor(amount · price · 10^l_dec / 10^(c_dec + expo)), exact, with the remainder the circuit takes. */
|
|
13
|
+
export function valueAt(amount, price, s) {
|
|
14
|
+
assertScale(s);
|
|
15
|
+
const n = amount * price * 10n ** s.lDec;
|
|
16
|
+
const d = 10n ** (s.cDec + s.expo);
|
|
17
|
+
return { q: n / d, r: n % d };
|
|
18
|
+
}
|
|
19
|
+
/** `OverLine`: debt · 10000 > value · bps. Equality is on the healthy side. */
|
|
20
|
+
export const overLine = (value, debt, bps) => debt * BPS_DENOM > value * bps;
|
|
21
|
+
/**
|
|
22
|
+
* `CollFor`: floor(value · 10^(c_dec + expo) / (price · 10^l_dec)). The circuit divides after cancelling the
|
|
23
|
+
* common power of ten, so the remainder is the reduced one (`circuits/v3/health/ref.mjs:58-65`).
|
|
24
|
+
*/
|
|
25
|
+
export function collFor(value, price, s) {
|
|
26
|
+
assertScale(s);
|
|
27
|
+
const shift = s.cDec + s.expo - s.lDec;
|
|
28
|
+
const num = shift >= 0n ? value * 10n ** shift : value;
|
|
29
|
+
const den = shift >= 0n ? price : price * 10n ** -shift;
|
|
30
|
+
return { q: num / den, r: num % den };
|
|
31
|
+
}
|
|
32
|
+
/** `heartbeat_floor` (`liquidation.move:451-455`): floor(min(spot, ema) · (10000 - k · 500) / 10000), k in 1..8. */
|
|
33
|
+
export function heartbeatFloor(r, k) {
|
|
34
|
+
if (!Number.isInteger(k) || k < K_MIN || k > K_MAX)
|
|
35
|
+
throw new Error("k is not a heartbeat bucket");
|
|
36
|
+
const low = r.spot < r.ema ? r.spot : r.ema;
|
|
37
|
+
return (low * (BPS_DENOM - BigInt(k) * HB_STEP_BPS)) / BPS_DENOM;
|
|
38
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { KeeperTerms } from "../keeper-payload.ts";
|
|
2
|
+
/** `nonce` is the collateral nonce the next Health proof over this lock folds. */
|
|
3
|
+
export type CollOpening = {
|
|
4
|
+
loanKey: bigint;
|
|
5
|
+
borrowerPk: bigint;
|
|
6
|
+
amount: bigint;
|
|
7
|
+
blinding: bigint;
|
|
8
|
+
nonce: bigint;
|
|
9
|
+
};
|
|
10
|
+
export type ReqOpening = {
|
|
11
|
+
principal: bigint;
|
|
12
|
+
maxRateBps: bigint;
|
|
13
|
+
termMs: bigint;
|
|
14
|
+
salt: bigint;
|
|
15
|
+
};
|
|
16
|
+
/** E-1 (`circuits/v3/lib/lock.circom` CollLockNote): a pk nobody holds a key for. */
|
|
17
|
+
export declare const collLockPk: (loanKey: bigint, borrowerPk: bigint) => bigint;
|
|
18
|
+
export declare const collNote: (o: Omit<CollOpening, "nonce">) => bigint;
|
|
19
|
+
/** `OutBlind(i)` of `health.circom:45-56`: a borrower or lender can always recompute an output's blinding. */
|
|
20
|
+
export declare const outputBlind: (collBlind: bigint, i: 0 | 1, nonce: bigint) => bigint;
|
|
21
|
+
/** `health.circom:226-237`. */
|
|
22
|
+
export declare const termsCommit: (t: KeeperTerms, borrowerPk: bigint) => bigint;
|
|
23
|
+
/** `health.circom:238-244`. */
|
|
24
|
+
export declare const requestCommit: (q: ReqOpening, borrowerPk: bigint, note: bigint) => bigint;
|
|
25
|
+
/** The lock a HEARTBEAT, or a TOPUP of `added`, leaves behind (`health.circom:354-373`). */
|
|
26
|
+
export declare const relocked: (o: CollOpening, added?: bigint) => CollOpening;
|
|
27
|
+
//# sourceMappingURL=openings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openings.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/openings.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AAGxD,kFAAkF;AAClF,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF,MAAM,MAAM,UAAU,GAAG;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjG,qFAAqF;AACrF,eAAO,MAAM,UAAU,GAAI,SAAS,MAAM,EAAE,YAAY,MAAM,WACN,CAAC;AAEzD,eAAO,MAAM,QAAQ,GAAI,GAAG,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,WACqB,CAAC;AAE5E,8GAA8G;AAC9G,eAAO,MAAM,WAAW,GAAI,WAAW,MAAM,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,OAAO,MAAM,WACd,CAAC;AAEzD,+BAA+B;AAC/B,eAAO,MAAM,WAAW,GAAI,GAAG,WAAW,EAAE,YAAY,MAAM,WAU1D,CAAC;AAEL,+BAA+B;AAC/B,eAAO,MAAM,aAAa,GAAI,GAAG,UAAU,EAAE,YAAY,MAAM,EAAE,MAAM,MAAM,WACE,CAAC;AAEhF,4FAA4F;AAC5F,eAAO,MAAM,QAAQ,GAAI,GAAG,WAAW,EAAE,cAAU,KAAG,WAKpD,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { DOM_BLIND, DOM_LOCK_COLL } from "../domains.js";
|
|
3
|
+
import { noteCommitment } from "../notes/keys.js";
|
|
4
|
+
/** E-1 (`circuits/v3/lib/lock.circom` CollLockNote): a pk nobody holds a key for. */
|
|
5
|
+
export const collLockPk = (loanKey, borrowerPk) => poseidonHash([DOM_LOCK_COLL, loanKey, borrowerPk, 0n]);
|
|
6
|
+
export const collNote = (o) => noteCommitment(o.amount, collLockPk(o.loanKey, o.borrowerPk), o.blinding);
|
|
7
|
+
/** `OutBlind(i)` of `health.circom:45-56`: a borrower or lender can always recompute an output's blinding. */
|
|
8
|
+
export const outputBlind = (collBlind, i, nonce) => poseidonHash([DOM_BLIND, collBlind, BigInt(i), nonce]);
|
|
9
|
+
/** `health.circom:226-237`. */
|
|
10
|
+
export const termsCommit = (t, borrowerPk) => poseidonHash([
|
|
11
|
+
t.principal,
|
|
12
|
+
t.rateBps,
|
|
13
|
+
t.owed,
|
|
14
|
+
t.maturityS,
|
|
15
|
+
borrowerPk,
|
|
16
|
+
t.lenderPk,
|
|
17
|
+
poseidonHash([t.lenderEncX, t.lenderEncY]),
|
|
18
|
+
t.salt,
|
|
19
|
+
]);
|
|
20
|
+
/** `health.circom:238-244`. */
|
|
21
|
+
export const requestCommit = (q, borrowerPk, note) => poseidonHash([q.principal, q.maxRateBps, q.termMs, borrowerPk, note, q.salt]);
|
|
22
|
+
/** The lock a HEARTBEAT, or a TOPUP of `added`, leaves behind (`health.circom:354-373`). */
|
|
23
|
+
export const relocked = (o, added = 0n) => ({
|
|
24
|
+
...o,
|
|
25
|
+
amount: o.amount + added,
|
|
26
|
+
blinding: outputBlind(o.blinding, 0, o.nonce),
|
|
27
|
+
nonce: o.nonce + 1n,
|
|
28
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { Reading } from "./math.ts";
|
|
2
|
+
/** One page of `ShieldedLoan` objects as the chain serves them: the object id and its Move fields as JSON. */
|
|
3
|
+
/** `next` omitted, null, or "" means this is the last page. */
|
|
4
|
+
/**
|
|
5
|
+
* `json` is the `ShieldedLoan` fields. `round` is the current `Round` (`loan.move:88-98`),
|
|
6
|
+
* a dynamic field of `rounds`, which the loan object does not inline.
|
|
7
|
+
*/
|
|
8
|
+
export type LoanPage = {
|
|
9
|
+
loans: {
|
|
10
|
+
id: string;
|
|
11
|
+
json: unknown;
|
|
12
|
+
round?: unknown;
|
|
13
|
+
}[];
|
|
14
|
+
next?: string | null;
|
|
15
|
+
};
|
|
16
|
+
/** Reads the chain for the caller. `cursor` is null on the first page. */
|
|
17
|
+
export type LoanReader = (cursor: string | null) => Promise<LoanPage>;
|
|
18
|
+
/** How many pages a walk reads before it refuses a cursor that never ends. */
|
|
19
|
+
export declare const MAX_LIST_PAGES = 32;
|
|
20
|
+
/** How many pages a keeper scan reads before it stops with the loans found so far. */
|
|
21
|
+
export declare const MAX_SCAN_PAGES = 10000;
|
|
22
|
+
/** Missing, null, or "" ends the walk. Any other non-string is not a cursor. */
|
|
23
|
+
export declare function nextCursor(next: unknown): string | null;
|
|
24
|
+
/** As `nextCursor`, and a full cap with another page is refused. */
|
|
25
|
+
export declare function afterPage(next: unknown, pagesRead: number): string | null;
|
|
26
|
+
/** Why a scan stopped before the last page; null when it read them all. */
|
|
27
|
+
export type ScanStop = "CURSOR_REPEATED" | "PAGE_CAP" | null;
|
|
28
|
+
/** B §7.4: what a keeper needs to queue work, all of it public loan state. */
|
|
29
|
+
export type Coverage = {
|
|
30
|
+
loanId: string;
|
|
31
|
+
state: number;
|
|
32
|
+
floorPrice: bigint;
|
|
33
|
+
floorExpo: number;
|
|
34
|
+
hbExpiryMs: bigint;
|
|
35
|
+
nonce: bigint;
|
|
36
|
+
};
|
|
37
|
+
/** Walks every page the reader serves. It takes no key and reads five public fields of each loan. */
|
|
38
|
+
export declare function scanCoverage(read: LoanReader, maxPages?: number): AsyncGenerator<Coverage, ScanStop>;
|
|
39
|
+
/** `collateral::covered` and `at_or_above_floor` (`collateral.move:202-214`); throws where Move aborts 43_303. */
|
|
40
|
+
export declare const covered: (c: Coverage, r: Reading, nowMs: bigint) => boolean;
|
|
41
|
+
//# sourceMappingURL=scan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scan.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/scan.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC,8GAA8G;AAC9G,+DAA+D;AAC/D;;;GAGG;AACH,MAAM,MAAM,QAAQ,GAAG;IAAE,KAAK,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,OAAO,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,CAAA;KAAE,EAAE,CAAC;IAAC,IAAI,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;CAAE,CAAC;AAEzG,0EAA0E;AAC1E,MAAM,MAAM,UAAU,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;AAEtE,8EAA8E;AAC9E,eAAO,MAAM,cAAc,KAAK,CAAC;AAEjC,sFAAsF;AACtF,eAAO,MAAM,cAAc,QAAS,CAAC;AAErC,gFAAgF;AAChF,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAIvD;AAED,oEAAoE;AACpE,wBAAgB,SAAS,CAAC,IAAI,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAKzE;AAED,2EAA2E;AAC3E,MAAM,MAAM,QAAQ,GAAG,iBAAiB,GAAG,UAAU,GAAG,IAAI,CAAC;AAE7D,8EAA8E;AAC9E,MAAM,MAAM,QAAQ,GAAG;IACrB,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AA8BF,qGAAqG;AACrG,wBAAuB,YAAY,CACjC,IAAI,EAAE,UAAU,EAChB,QAAQ,SAAiB,GACxB,cAAc,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAYpC;AAED,kHAAkH;AAClH,eAAO,MAAM,OAAO,GAAI,GAAG,QAAQ,EAAE,GAAG,OAAO,EAAE,OAAO,MAAM,YACK,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { EsseyError } from "../../errors.js";
|
|
2
|
+
import { address, decimal } from "../../network.js";
|
|
3
|
+
/** How many pages a walk reads before it refuses a cursor that never ends. */
|
|
4
|
+
export const MAX_LIST_PAGES = 32;
|
|
5
|
+
/** How many pages a keeper scan reads before it stops with the loans found so far. */
|
|
6
|
+
export const MAX_SCAN_PAGES = 10_000;
|
|
7
|
+
/** Missing, null, or "" ends the walk. Any other non-string is not a cursor. */
|
|
8
|
+
export function nextCursor(next) {
|
|
9
|
+
if (next === undefined || next === null || next === "")
|
|
10
|
+
return null;
|
|
11
|
+
if (typeof next !== "string")
|
|
12
|
+
throw new EsseyError("CT_MISMATCH", "refused: tampered view data");
|
|
13
|
+
return next;
|
|
14
|
+
}
|
|
15
|
+
/** As `nextCursor`, and a full cap with another page is refused. */
|
|
16
|
+
export function afterPage(next, pagesRead) {
|
|
17
|
+
const cursor = nextCursor(next);
|
|
18
|
+
if (cursor !== null && pagesRead >= MAX_LIST_PAGES)
|
|
19
|
+
throw new EsseyError("SERVICE_REFUSED", "refused: the reader returned too many pages");
|
|
20
|
+
return cursor;
|
|
21
|
+
}
|
|
22
|
+
const MAX_EXPO_GAP = 18n;
|
|
23
|
+
const MAX_EXPO = 18n;
|
|
24
|
+
const u8 = (value, what) => {
|
|
25
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0 || value > 0xff)
|
|
26
|
+
throw new EsseyError("INVALID_ARGUMENT", `${what} is not a u8`);
|
|
27
|
+
return value;
|
|
28
|
+
};
|
|
29
|
+
const record = (value, what) => {
|
|
30
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
31
|
+
throw new EsseyError("INVALID_ARGUMENT", `${what} is not an object`);
|
|
32
|
+
return value;
|
|
33
|
+
};
|
|
34
|
+
function coverage(id, json) {
|
|
35
|
+
const loan = record(json, "loan");
|
|
36
|
+
const coll = record(loan.coll, "loan.coll");
|
|
37
|
+
return {
|
|
38
|
+
loanId: address(id, "loan id"),
|
|
39
|
+
state: u8(loan.state, "loan.state"),
|
|
40
|
+
floorPrice: decimal(coll.floor_price, "coll.floor_price"),
|
|
41
|
+
floorExpo: u8(coll.floor_expo, "coll.floor_expo"),
|
|
42
|
+
hbExpiryMs: decimal(coll.hb_expiry_ms, "coll.hb_expiry_ms"),
|
|
43
|
+
nonce: decimal(coll.nonce, "coll.nonce"),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** Walks every page the reader serves. It takes no key and reads five public fields of each loan. */
|
|
47
|
+
export async function* scanCoverage(read, maxPages = MAX_SCAN_PAGES) {
|
|
48
|
+
const seen = new Set();
|
|
49
|
+
let cursor = null;
|
|
50
|
+
for (let pages = 1;; pages++) {
|
|
51
|
+
const page = await read(cursor);
|
|
52
|
+
for (const { id, json } of page.loans)
|
|
53
|
+
yield coverage(id, json);
|
|
54
|
+
cursor = nextCursor(page.next);
|
|
55
|
+
if (cursor === null)
|
|
56
|
+
return null;
|
|
57
|
+
if (seen.has(cursor))
|
|
58
|
+
return "CURSOR_REPEATED";
|
|
59
|
+
if (pages >= maxPages)
|
|
60
|
+
return "PAGE_CAP";
|
|
61
|
+
seen.add(cursor);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/** `collateral::covered` and `at_or_above_floor` (`collateral.move:202-214`); throws where Move aborts 43_303. */
|
|
65
|
+
export const covered = (c, r, nowMs) => nowMs < c.hbExpiryMs && c.floorPrice > 0n && atOrAboveFloor(c, r);
|
|
66
|
+
function atOrAboveFloor(c, r) {
|
|
67
|
+
if (r.expo < 0n || r.expo > MAX_EXPO)
|
|
68
|
+
throw new EsseyError("INVALID_ARGUMENT", "reading.expo is not in 0..18 (price.move:48)");
|
|
69
|
+
const low = r.spot < r.ema ? r.spot : r.ema;
|
|
70
|
+
const floorExpo = BigInt(c.floorExpo);
|
|
71
|
+
const gap = r.expo >= floorExpo ? r.expo - floorExpo : floorExpo - r.expo;
|
|
72
|
+
if (gap > MAX_EXPO_GAP)
|
|
73
|
+
throw new EsseyError("STATE_REFUSED", "refused: the reading and floor exponents differ by more than 18");
|
|
74
|
+
const scale = 10n ** gap;
|
|
75
|
+
return r.expo >= floorExpo ? low >= c.floorPrice * scale : low * scale >= c.floorPrice;
|
|
76
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type KeeperPayload } from "../keeper-payload.ts";
|
|
2
|
+
import { type CollOpening } from "./openings.ts";
|
|
3
|
+
/**
|
|
4
|
+
* The Seal client a keeper supplies. It holds the session key and the badge and does the network round trips;
|
|
5
|
+
* this SDK opens no socket and sees neither. It returns the 288-byte keeper plaintext.
|
|
6
|
+
*/
|
|
7
|
+
export type KeeperSeal = {
|
|
8
|
+
decrypt(request: {
|
|
9
|
+
loan: string;
|
|
10
|
+
keyId: Uint8Array;
|
|
11
|
+
sealed: Uint8Array;
|
|
12
|
+
}): Promise<Uint8Array>;
|
|
13
|
+
};
|
|
14
|
+
/** The public fields of a locked loan a keeper reads before asking Seal. */
|
|
15
|
+
export type SealedLoan = {
|
|
16
|
+
loan: string;
|
|
17
|
+
loanKey: bigint;
|
|
18
|
+
keeperCt: Uint8Array;
|
|
19
|
+
keeperCtVersion: bigint;
|
|
20
|
+
};
|
|
21
|
+
/** E-36, `collateral::keeper_key_id`: blake2b256("ess3/kpr" ‖ bcs(u256 loan_key) ‖ bcs(u64 version)). */
|
|
22
|
+
export declare function keeperKeyId(loanKey: bigint, version: bigint): Uint8Array;
|
|
23
|
+
/** Asks the injected client for the plaintext of the loan's current keeper ciphertext and refuses one that is not this loan's. */
|
|
24
|
+
export declare function fetchKeeperPlaintext(l: SealedLoan, seal: KeeperSeal): Promise<KeeperPayload>;
|
|
25
|
+
export declare const openingOf: (p: KeeperPayload) => CollOpening;
|
|
26
|
+
/**
|
|
27
|
+
* B §7.3: heartbeats write no ciphertext, so a keeper rolls the opening forward one re-lock per nonce.
|
|
28
|
+
* The result must open the loan's stored `coll_note`, or the plaintext is not this lock's.
|
|
29
|
+
*/
|
|
30
|
+
export declare function rollTo(o: CollOpening, collNoteOnChain: bigint, nonce: bigint): CollOpening;
|
|
31
|
+
//# sourceMappingURL=seal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"seal.d.ts","sourceRoot":"","sources":["../../../src/v3/collateral/seal.ts"],"names":[],"mappings":"AAKA,OAAO,EAA8C,KAAK,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAEtG,OAAO,EAAsB,KAAK,WAAW,EAAE,MAAM,eAAe,CAAC;AAErE;;;GAGG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB,OAAO,CAAC,OAAO,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,UAAU,CAAC;QAAC,MAAM,EAAE,UAAU,CAAA;KAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;CAChG,CAAC;AAEF,4EAA4E;AAC5E,MAAM,MAAM,UAAU,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,UAAU,CAAC;IAAC,eAAe,EAAE,MAAM,CAAA;CAAE,CAAC;AAQ1G,yGAAyG;AACzG,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,UAAU,CAOxE;AAMD,kIAAkI;AAClI,wBAAsB,oBAAoB,CAAC,CAAC,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,aAAa,CAAC,CAQlG;AAED,eAAO,MAAM,SAAS,GAAI,GAAG,aAAa,KAAG,WAM3C,CAAC;AAEH;;;GAGG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW,CAM1F"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { blake2b } from "@noble/hashes/blake2.js";
|
|
2
|
+
import { EsseyError } from "../../errors.js";
|
|
3
|
+
import { R, TAGS } from "../domains.js";
|
|
4
|
+
import { assertU64 } from "../fees.js";
|
|
5
|
+
import { KEEPER_LAYOUT_VERSION, decodeKeeperPayload } from "../keeper-payload.js";
|
|
6
|
+
import { collNote, relocked } from "./openings.js";
|
|
7
|
+
function littleEndian(value, size) {
|
|
8
|
+
const out = new Uint8Array(size);
|
|
9
|
+
for (let i = 0; i < size; i++)
|
|
10
|
+
out[i] = Number((value >> BigInt(8 * i)) & 0xffn);
|
|
11
|
+
return out;
|
|
12
|
+
}
|
|
13
|
+
/** E-36, `collateral::keeper_key_id`: blake2b256("ess3/kpr" ‖ bcs(u256 loan_key) ‖ bcs(u64 version)). */
|
|
14
|
+
export function keeperKeyId(loanKey, version) {
|
|
15
|
+
if (loanKey < 0n || loanKey >= R)
|
|
16
|
+
throw new Error("loanKey is not a field element");
|
|
17
|
+
assertU64("version", version);
|
|
18
|
+
const tag = Buffer.from(TAGS.DOM_KPR, "ascii");
|
|
19
|
+
return blake2b(Uint8Array.from([...tag, ...littleEndian(loanKey, 32), ...littleEndian(version, 8)]), {
|
|
20
|
+
dkLen: 32,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
const mismatch = (what) => {
|
|
24
|
+
throw new EsseyError("CT_MISMATCH", `refused: ${what}`);
|
|
25
|
+
};
|
|
26
|
+
/** Asks the injected client for the plaintext of the loan's current keeper ciphertext and refuses one that is not this loan's. */
|
|
27
|
+
export async function fetchKeeperPlaintext(l, seal) {
|
|
28
|
+
const keyId = keeperKeyId(l.loanKey, l.keeperCtVersion);
|
|
29
|
+
const decoded = decodeKeeperPayload(await seal.decrypt({ loan: l.loan, keyId, sealed: l.keeperCt }));
|
|
30
|
+
if (!decoded.ok)
|
|
31
|
+
return mismatch(`the keeper plaintext does not decode (${decoded.error})`);
|
|
32
|
+
const { payload } = decoded;
|
|
33
|
+
if (payload.version !== KEEPER_LAYOUT_VERSION)
|
|
34
|
+
mismatch("the keeper plaintext has another layout version");
|
|
35
|
+
if (payload.loanKey !== l.loanKey)
|
|
36
|
+
mismatch("the keeper plaintext is for another loan");
|
|
37
|
+
return payload;
|
|
38
|
+
}
|
|
39
|
+
export const openingOf = (p) => ({
|
|
40
|
+
loanKey: p.loanKey,
|
|
41
|
+
borrowerPk: p.borrowerPk,
|
|
42
|
+
amount: p.collAmt,
|
|
43
|
+
blinding: p.collBlind,
|
|
44
|
+
nonce: p.nonceAtCt,
|
|
45
|
+
});
|
|
46
|
+
/**
|
|
47
|
+
* B §7.3: heartbeats write no ciphertext, so a keeper rolls the opening forward one re-lock per nonce.
|
|
48
|
+
* The result must open the loan's stored `coll_note`, or the plaintext is not this lock's.
|
|
49
|
+
*/
|
|
50
|
+
export function rollTo(o, collNoteOnChain, nonce) {
|
|
51
|
+
if (nonce < o.nonce)
|
|
52
|
+
mismatch("the keeper plaintext is newer than the loan");
|
|
53
|
+
let cur = o;
|
|
54
|
+
while (cur.nonce < nonce)
|
|
55
|
+
cur = relocked(cur);
|
|
56
|
+
if (collNote(cur) !== collNoteOnChain)
|
|
57
|
+
mismatch("the keeper plaintext does not open the locked note");
|
|
58
|
+
return cur;
|
|
59
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { type Fetch } from "../../network.ts";
|
|
2
|
+
import { type Credential } from "./membership.ts";
|
|
3
|
+
/** The request that pinned this commitment. Asking again with it refetches or renews (D §5.2). */
|
|
4
|
+
export type Slot = {
|
|
5
|
+
challengeId: string;
|
|
6
|
+
powNonce: string;
|
|
7
|
+
} | {
|
|
8
|
+
invite: string;
|
|
9
|
+
};
|
|
10
|
+
export type Role = "member" | "keeper";
|
|
11
|
+
export declare function parseCredential(raw: unknown): Credential;
|
|
12
|
+
export declare const DAY_MS = 86400000;
|
|
13
|
+
/** The end of the 7-day epoch holding `nowMs`. Must match `epochEnd` in services/v3api/src/credential.ts:23-25. */
|
|
14
|
+
export declare function epochEnd(nowMs: number): bigint;
|
|
15
|
+
/** How far this clock may be from the desk's. The coordinator's choice (card D9-r3), not a ruling. */
|
|
16
|
+
export declare const CLOCK_TOLERANCE_MS: number;
|
|
17
|
+
/**
|
|
18
|
+
* True for an expiry the desk signs at some instant within the tolerance of `nowMs`. Any other expiry
|
|
19
|
+
* is a public lane value that singles the member out, so it is never stored.
|
|
20
|
+
*/
|
|
21
|
+
export declare function expiryAllowed(expiryMs: bigint, nowMs: number): boolean;
|
|
22
|
+
/** True when the issuer key in the credential signed Poseidon(commitment, expiryMs, epoch, role): the message the circuit checks. */
|
|
23
|
+
export declare function credentialSigned(c: Credential, commitment: bigint): Promise<boolean>;
|
|
24
|
+
export declare function solveChallenge(fetch: Fetch, api: string): Promise<Slot>;
|
|
25
|
+
/** Returns the credential only if it is signed over this commitment, for the role asked, with an expiry the epoch rule allows. */
|
|
26
|
+
export declare function requestCredential(fetch: Fetch, api: string, slot: Slot, commitment: bigint, role: Role, nowMs: number): Promise<Credential>;
|
|
27
|
+
//# sourceMappingURL=issue.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"issue.d.ts","sourceRoot":"","sources":["../../../src/v3/credential/issue.ts"],"names":[],"mappings":"AAGA,OAAO,EAAqB,KAAK,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAGjE,OAAO,EAAqB,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAiBrE,kGAAkG;AAClG,MAAM,MAAM,IAAI,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAElF,MAAM,MAAM,IAAI,GAAG,QAAQ,GAAG,QAAQ,CAAC;AAiBvC,wBAAgB,eAAe,CAAC,GAAG,EAAE,OAAO,GAAG,UAAU,CAYxD;AAED,eAAO,MAAM,MAAM,WAAa,CAAC;AAGjC,mHAAmH;AACnH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE9C;AAED,sGAAsG;AACtG,eAAO,MAAM,kBAAkB,QAAgB,CAAC;AAQhD;;;GAGG;AACH,wBAAgB,aAAa,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAItE;AAED,qIAAqI;AACrI,wBAAsB,gBAAgB,CAAC,CAAC,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC,CAU1F;AAYD,wBAAsB,cAAc,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAW7E;AAED,kIAAkI;AAClI,wBAAsB,iBAAiB,CACrC,KAAK,EAAE,KAAK,EACZ,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,IAAI,EACV,UAAU,EAAE,MAAM,EAClB,IAAI,EAAE,IAAI,EACV,KAAK,EAAE,MAAM,GACZ,OAAO,CAAC,UAAU,CAAC,CAQrB"}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
import { EsseyError } from "../../errors.js";
|
|
3
|
+
import { decimal, postJson } from "../../network.js";
|
|
4
|
+
import { R } from "../domains.js";
|
|
5
|
+
import { credentialMessage } from "./membership.js";
|
|
6
|
+
import { solvePow } from "./pow.js";
|
|
7
|
+
const require = createRequire(import.meta.url);
|
|
8
|
+
let eddsaPromise;
|
|
9
|
+
function eddsa() {
|
|
10
|
+
eddsaPromise ??= require("circomlibjs").buildEddsa();
|
|
11
|
+
return eddsaPromise;
|
|
12
|
+
}
|
|
13
|
+
const refused = (what) => {
|
|
14
|
+
throw new EsseyError("CREDENTIAL_REFUSED", `refused: credential ${what}`);
|
|
15
|
+
};
|
|
16
|
+
function part(value, what) {
|
|
17
|
+
if (value === null || typeof value !== "object" || Array.isArray(value))
|
|
18
|
+
return refused(what);
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
function fieldOf(value, what) {
|
|
22
|
+
const n = decimal(value, `credential ${what}`);
|
|
23
|
+
if (n >= R)
|
|
24
|
+
refused(what);
|
|
25
|
+
return n;
|
|
26
|
+
}
|
|
27
|
+
export function parseCredential(raw) {
|
|
28
|
+
const c = part(raw, "is not an object");
|
|
29
|
+
const sig = part(c.sig, "sig");
|
|
30
|
+
const pk = part(c.issuerPk, "issuerPk");
|
|
31
|
+
if (c.role !== "member" && c.role !== "keeper")
|
|
32
|
+
refused("role");
|
|
33
|
+
return {
|
|
34
|
+
sig: { R8x: fieldOf(sig.R8x, "sig.R8x"), R8y: fieldOf(sig.R8y, "sig.R8y"), S: fieldOf(sig.S, "sig.S") },
|
|
35
|
+
expiryMs: decimal(c.expiryMs, "credential expiryMs"),
|
|
36
|
+
epoch: decimal(c.epoch, "credential epoch"),
|
|
37
|
+
issuerPk: { ax: fieldOf(pk.ax, "issuerPk.ax"), ay: fieldOf(pk.ay, "issuerPk.ay") },
|
|
38
|
+
role: c.role,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
export const DAY_MS = 86_400_000;
|
|
42
|
+
const EPOCH_MS = 7 * DAY_MS;
|
|
43
|
+
/** The end of the 7-day epoch holding `nowMs`. Must match `epochEnd` in services/v3api/src/credential.ts:23-25. */
|
|
44
|
+
export function epochEnd(nowMs) {
|
|
45
|
+
return BigInt((Math.floor(nowMs / EPOCH_MS) + 1) * EPOCH_MS);
|
|
46
|
+
}
|
|
47
|
+
/** How far this clock may be from the desk's. The coordinator's choice (card D9-r3), not a ruling. */
|
|
48
|
+
export const CLOCK_TOLERANCE_MS = 5 * 60 * 1000;
|
|
49
|
+
/** The expiry the desk signs at `nowMs`. Must match `expiryFor` in services/v3api/src/credential.ts:28-31. */
|
|
50
|
+
function expiryFor(nowMs) {
|
|
51
|
+
const end = epochEnd(nowMs);
|
|
52
|
+
return end - BigInt(nowMs) <= BigInt(DAY_MS) ? end + BigInt(EPOCH_MS) : end;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* True for an expiry the desk signs at some instant within the tolerance of `nowMs`. Any other expiry
|
|
56
|
+
* is a public lane value that singles the member out, so it is never stored.
|
|
57
|
+
*/
|
|
58
|
+
export function expiryAllowed(expiryMs, nowMs) {
|
|
59
|
+
if (!Number.isSafeInteger(nowMs))
|
|
60
|
+
return false;
|
|
61
|
+
const earliest = expiryFor(nowMs - CLOCK_TOLERANCE_MS);
|
|
62
|
+
return expiryMs === earliest || expiryMs === expiryFor(nowMs + CLOCK_TOLERANCE_MS);
|
|
63
|
+
}
|
|
64
|
+
/** True when the issuer key in the credential signed Poseidon(commitment, expiryMs, epoch, role): the message the circuit checks. */
|
|
65
|
+
export async function credentialSigned(c, commitment) {
|
|
66
|
+
const signer = await eddsa();
|
|
67
|
+
const { F } = signer;
|
|
68
|
+
const msg = F.e(credentialMessage(commitment, c.expiryMs, c.epoch, c.role));
|
|
69
|
+
const sig = { R8: [F.e(c.sig.R8x), F.e(c.sig.R8y)], S: c.sig.S };
|
|
70
|
+
try {
|
|
71
|
+
return signer.verifyPoseidon(msg, sig, [F.e(c.issuerPk.ax), F.e(c.issuerPk.ay)]) === true;
|
|
72
|
+
}
|
|
73
|
+
catch {
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
async function ask(fetch, url, body) {
|
|
78
|
+
const reply = await postJson(fetch, url, body);
|
|
79
|
+
if (reply.status === 200)
|
|
80
|
+
return reply.body;
|
|
81
|
+
const error = typeof reply.body.error === "string" ? reply.body.error : undefined;
|
|
82
|
+
throw new EsseyError("SERVICE_REFUSED", `v3api refused: ${error ?? reply.status}`, {
|
|
83
|
+
status: reply.status,
|
|
84
|
+
error,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
export async function solveChallenge(fetch, api) {
|
|
88
|
+
const c = (await ask(fetch, `${api}/v3/credential/challenge`, {}));
|
|
89
|
+
if (typeof c.challengeId !== "string" ||
|
|
90
|
+
typeof c.powSalt !== "string" ||
|
|
91
|
+
!Number.isInteger(c.difficulty) ||
|
|
92
|
+
c.difficulty < 0 ||
|
|
93
|
+
c.difficulty > 32)
|
|
94
|
+
return refused("challenge");
|
|
95
|
+
return { challengeId: c.challengeId, powNonce: await solvePow(c.powSalt, c.difficulty) };
|
|
96
|
+
}
|
|
97
|
+
/** Returns the credential only if it is signed over this commitment, for the role asked, with an expiry the epoch rule allows. */
|
|
98
|
+
export async function requestCredential(fetch, api, slot, commitment, role, nowMs) {
|
|
99
|
+
const body = { ...slot, commitment: commitment.toString(), role };
|
|
100
|
+
const reply = await ask(fetch, `${api}/v3/credential/issue`, body);
|
|
101
|
+
const credential = parseCredential(reply.credential);
|
|
102
|
+
if (!(await credentialSigned(credential, commitment)))
|
|
103
|
+
refused("signature does not cover this commitment");
|
|
104
|
+
if (credential.role !== role)
|
|
105
|
+
refused("role is not the one asked for");
|
|
106
|
+
if (!expiryAllowed(credential.expiryMs, nowMs))
|
|
107
|
+
refused("expiry is not an epoch end in use");
|
|
108
|
+
return credential;
|
|
109
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
export declare const DOMAIN_SENDER = 1n;
|
|
2
|
+
export declare const DOMAIN_SPEND = 2n;
|
|
3
|
+
export declare const DOMAIN_KEEPER = 3n;
|
|
4
|
+
/** Only the member holds these. The issuer sees `memberCommitment` and nothing else. */
|
|
5
|
+
export type MemberSecrets = {
|
|
6
|
+
secret: bigint;
|
|
7
|
+
nullSecret: bigint;
|
|
8
|
+
};
|
|
9
|
+
/** What `POST /v3/credential/issue` returns, as bigints. */
|
|
10
|
+
export type Credential = {
|
|
11
|
+
sig: {
|
|
12
|
+
R8x: bigint;
|
|
13
|
+
R8y: bigint;
|
|
14
|
+
S: bigint;
|
|
15
|
+
};
|
|
16
|
+
expiryMs: bigint;
|
|
17
|
+
epoch: bigint;
|
|
18
|
+
issuerPk: {
|
|
19
|
+
ax: bigint;
|
|
20
|
+
ay: bigint;
|
|
21
|
+
};
|
|
22
|
+
role: "member" | "keeper";
|
|
23
|
+
};
|
|
24
|
+
/** Above r, so `keys::register` refuses it as a sale or loan key (`move/v3/config/sources/keys.move:111`). */
|
|
25
|
+
export declare const FEE_PARAMS_ROOM: string;
|
|
26
|
+
/** `(lo, hi)` 16-byte halves of 32 big-endian bytes, the split of `privacy_v3::halves::split`. */
|
|
27
|
+
export declare function halves(hex32: string): [bigint, bigint];
|
|
28
|
+
/**
|
|
29
|
+
* The lender pass room of a round, `loan::offer_room` (`move/v3/loan/sources/loan.move:419-421`):
|
|
30
|
+
* 2^255 + `lock_field`, so it is never the scope of a sale or loan key (`keys.move:111`).
|
|
31
|
+
*/
|
|
32
|
+
export declare function offerRoom(loanKey: bigint, round: bigint): string;
|
|
33
|
+
export declare function memberCommitment(s: MemberSecrets): bigint;
|
|
34
|
+
export declare function credentialMessage(commitment: bigint, expiryMs: bigint, epoch: bigint, role: Credential["role"]): bigint;
|
|
35
|
+
export declare function memberTag(nullSecret: bigint, room: string, nonce?: bigint): bigint;
|
|
36
|
+
export declare function passCtx(domain: bigint, room: string, bind: string): bigint;
|
|
37
|
+
//# sourceMappingURL=membership.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"membership.d.ts","sourceRoot":"","sources":["../../../src/v3/credential/membership.ts"],"names":[],"mappings":"AAIA,eAAO,MAAM,aAAa,KAAK,CAAC;AAChC,eAAO,MAAM,YAAY,KAAK,CAAC;AAC/B,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,wFAAwF;AACxF,MAAM,MAAM,aAAa,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC;AAEnE,4DAA4D;AAC5D,MAAM,MAAM,UAAU,GAAG;IACvB,GAAG,EAAE;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,GAAG,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC7C,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAA;KAAE,CAAC;IACrC,IAAI,EAAE,QAAQ,GAAG,QAAQ,CAAC;CAC3B,CAAC;AAEF,8GAA8G;AAC9G,eAAO,MAAM,eAAe,QAAyD,CAAC;AAEtF,kGAAkG;AAClG,wBAAgB,MAAM,CAAC,KAAK,EAAE,MAAM,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAGtD;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,aAAa,GAAG,MAAM,CAEzD;AAKD,wBAAgB,iBAAiB,CAC/B,UAAU,EAAE,MAAM,EAClB,QAAQ,EAAE,MAAM,EAChB,KAAK,EAAE,MAAM,EACb,IAAI,EAAE,UAAU,CAAC,MAAM,CAAC,GACvB,MAAM,CAER;AAED,wBAAgB,SAAS,CAAC,UAAU,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,SAAK,GAAG,MAAM,CAE9E;AAED,wBAAgB,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE1E"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { DOM_ROUND, FEE_PARAMS_DOMAIN } from "../domains.js";
|
|
3
|
+
export const DOMAIN_SENDER = 1n;
|
|
4
|
+
export const DOMAIN_SPEND = 2n;
|
|
5
|
+
export const DOMAIN_KEEPER = 3n;
|
|
6
|
+
/** Above r, so `keys::register` refuses it as a sale or loan key (`move/v3/config/sources/keys.move:111`). */
|
|
7
|
+
export const FEE_PARAMS_ROOM = `0x${((1n << 255n) + FEE_PARAMS_DOMAIN).toString(16)}`;
|
|
8
|
+
/** `(lo, hi)` 16-byte halves of 32 big-endian bytes, the split of `privacy_v3::halves::split`. */
|
|
9
|
+
export function halves(hex32) {
|
|
10
|
+
if (!/^0x[0-9a-f]{64}$/.test(hex32))
|
|
11
|
+
throw new Error("expected 32 lowercase hex bytes");
|
|
12
|
+
return [BigInt(`0x${hex32.slice(34)}`), BigInt(`0x${hex32.slice(2, 34)}`)];
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The lender pass room of a round, `loan::offer_room` (`move/v3/loan/sources/loan.move:419-421`):
|
|
16
|
+
* 2^255 + `lock_field`, so it is never the scope of a sale or loan key (`keys.move:111`).
|
|
17
|
+
*/
|
|
18
|
+
export function offerRoom(loanKey, round) {
|
|
19
|
+
return `0x${((1n << 255n) + poseidonHash([DOM_ROUND, loanKey, round])).toString(16)}`;
|
|
20
|
+
}
|
|
21
|
+
export function memberCommitment(s) {
|
|
22
|
+
return poseidonHash([s.secret, s.nullSecret]);
|
|
23
|
+
}
|
|
24
|
+
/** The role field of the signed message, constrained to {0, 1} by membership.circom (BORIS-RULING 0067 §11). */
|
|
25
|
+
const ROLE_FIELD = { member: 0n, keeper: 1n };
|
|
26
|
+
export function credentialMessage(commitment, expiryMs, epoch, role) {
|
|
27
|
+
return poseidonHash([commitment, expiryMs, epoch, ROLE_FIELD[role]]);
|
|
28
|
+
}
|
|
29
|
+
export function memberTag(nullSecret, room, nonce = 0n) {
|
|
30
|
+
return poseidonHash([nullSecret, ...halves(room), nonce]);
|
|
31
|
+
}
|
|
32
|
+
export function passCtx(domain, room, bind) {
|
|
33
|
+
return poseidonHash([domain, ...halves(room), ...halves(bind)]);
|
|
34
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export type PowChallenge = {
|
|
2
|
+
challengeId: string;
|
|
3
|
+
powSalt: string;
|
|
4
|
+
difficulty: number;
|
|
5
|
+
expiresMs: number;
|
|
6
|
+
};
|
|
7
|
+
export declare function leadingZeroBits(digest: Uint8Array): number;
|
|
8
|
+
/** sha256(UTF-8(powSalt || nonce)) with `difficulty` leading zero bits (D §5.2). The nonce is a decimal counter. */
|
|
9
|
+
export declare function solvePow(powSalt: string, difficulty: number, maxTries?: number): Promise<string>;
|
|
10
|
+
//# sourceMappingURL=pow.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pow.d.ts","sourceRoot":"","sources":["../../../src/v3/credential/pow.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,YAAY,GAAG;IAAE,WAAW,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3G,wBAAgB,eAAe,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,CAO1D;AAED,oHAAoH;AACpH,wBAAsB,QAAQ,CAAC,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,EAAE,QAAQ,SAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAOvG"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { EsseyError } from "../../errors.js";
|
|
3
|
+
export function leadingZeroBits(digest) {
|
|
4
|
+
let bits = 0;
|
|
5
|
+
for (const byte of digest) {
|
|
6
|
+
if (byte !== 0)
|
|
7
|
+
return bits + Math.clz32(byte) - 24;
|
|
8
|
+
bits += 8;
|
|
9
|
+
}
|
|
10
|
+
return bits;
|
|
11
|
+
}
|
|
12
|
+
/** sha256(UTF-8(powSalt || nonce)) with `difficulty` leading zero bits (D §5.2). The nonce is a decimal counter. */
|
|
13
|
+
export async function solvePow(powSalt, difficulty, maxTries = 2 ** 32) {
|
|
14
|
+
for (let n = 0; n < maxTries; n++) {
|
|
15
|
+
const digest = createHash("sha256").update(`${powSalt}${n}`).digest();
|
|
16
|
+
if (leadingZeroBits(digest) >= difficulty)
|
|
17
|
+
return String(n);
|
|
18
|
+
if ((n & 0xffff) === 0xffff)
|
|
19
|
+
await new Promise((resolve) => setImmediate(resolve));
|
|
20
|
+
}
|
|
21
|
+
throw new EsseyError("POW_EXHAUSTED", "no proof-of-work nonce found within the try limit");
|
|
22
|
+
}
|