@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,31 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
3
|
+
import { R } from "../domains.js";
|
|
4
|
+
import { JS4_LEVELS } from "../inputs.js";
|
|
5
|
+
/** `privacy_v3::merkle_tree::zero_leaf` (`move/v3/pool/sources/merkle_tree.move:101-103`). */
|
|
6
|
+
export const ZERO_LEAF = BigInt(`0x${Buffer.from(keccak_256(new TextEncoder().encode("tornado"))).toString("hex")}`) % R;
|
|
7
|
+
const ZEROS = (() => {
|
|
8
|
+
const z = [ZERO_LEAF];
|
|
9
|
+
for (let i = 1; i < JS4_LEVELS; i++)
|
|
10
|
+
z.push(poseidonHash([z[i - 1], z[i - 1]]));
|
|
11
|
+
return z;
|
|
12
|
+
})();
|
|
13
|
+
/** The tree `merkle_tree::insert_pair` builds from these leaves, in index order. */
|
|
14
|
+
export function poolTree(leaves) {
|
|
15
|
+
const levels = [leaves];
|
|
16
|
+
for (let l = 0; l < JS4_LEVELS; l++) {
|
|
17
|
+
const below = levels[l];
|
|
18
|
+
const up = [];
|
|
19
|
+
for (let i = 0; i < below.length; i += 2)
|
|
20
|
+
up.push(poseidonHash([below[i], below[i + 1] ?? ZEROS[l]]));
|
|
21
|
+
levels.push(up.length ? up : [poseidonHash([ZEROS[l], ZEROS[l]])]);
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
root: levels[JS4_LEVELS][0],
|
|
25
|
+
path(index) {
|
|
26
|
+
if (!Number.isInteger(index) || index < 0 || index >= leaves.length)
|
|
27
|
+
throw new Error("no such leaf");
|
|
28
|
+
return levels.slice(0, JS4_LEVELS).map((row, l) => row[(index >> l) ^ 1] ?? ZEROS[l]);
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { OwnNote } from "./scan.ts";
|
|
2
|
+
export type FeeRates = {
|
|
3
|
+
bps: bigint;
|
|
4
|
+
min: bigint;
|
|
5
|
+
max: bigint;
|
|
6
|
+
};
|
|
7
|
+
export declare const withdrawCost: (wd: bigint, rates: FeeRates, relayerFee: bigint) => bigint;
|
|
8
|
+
/** The largest withdraw that `total` covers with its fee and the relayer fee on top. 0 when none fits. */
|
|
9
|
+
export declare function exitValue(total: bigint, rates: FeeRates, relayerFee: bigint): bigint;
|
|
10
|
+
/** js4 spends two notes. One note that covers `need` is preferred; otherwise the two largest. */
|
|
11
|
+
export declare function pickInputs(owned: OwnNote[], need: bigint): OwnNote[];
|
|
12
|
+
//# sourceMappingURL=withdraw.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"withdraw.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/withdraw.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEzC,MAAM,MAAM,QAAQ,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAEjE,eAAO,MAAM,YAAY,GAAI,IAAI,MAAM,EAAE,OAAO,QAAQ,EAAE,YAAY,MAAM,WACvB,CAAC;AAEtD,0GAA0G;AAC1G,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,GAAG,MAAM,CASpF;AAED,iGAAiG;AACjG,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,EAAE,CAOpE"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { EsseyError } from "../../errors.js";
|
|
2
|
+
import { feeOf } from "../fees.js";
|
|
3
|
+
export const withdrawCost = (wd, rates, relayerFee) => wd + feeOf({ base: wd, ...rates }).fee + relayerFee;
|
|
4
|
+
/** The largest withdraw that `total` covers with its fee and the relayer fee on top. 0 when none fits. */
|
|
5
|
+
export function exitValue(total, rates, relayerFee) {
|
|
6
|
+
let lo = 0n;
|
|
7
|
+
let hi = total;
|
|
8
|
+
while (lo < hi) {
|
|
9
|
+
const mid = (lo + hi + 1n) / 2n;
|
|
10
|
+
if (withdrawCost(mid, rates, relayerFee) <= total)
|
|
11
|
+
lo = mid;
|
|
12
|
+
else
|
|
13
|
+
hi = mid - 1n;
|
|
14
|
+
}
|
|
15
|
+
return lo;
|
|
16
|
+
}
|
|
17
|
+
/** js4 spends two notes. One note that covers `need` is preferred; otherwise the two largest. */
|
|
18
|
+
export function pickInputs(owned, need) {
|
|
19
|
+
const sorted = [...owned].sort((a, b) => (a.value > b.value ? -1 : a.value < b.value ? 1 : 0));
|
|
20
|
+
const one = [...sorted].reverse().find((n) => n.value >= need);
|
|
21
|
+
if (one)
|
|
22
|
+
return [one];
|
|
23
|
+
const two = sorted.slice(0, 2);
|
|
24
|
+
if (two.length === 2 && two[0].value + two[1].value >= need)
|
|
25
|
+
return two;
|
|
26
|
+
throw new EsseyError("INVALID_ARGUMENT", "refused: two notes do not cover the amount and its fees");
|
|
27
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { SnarkProof } from "./bodies.ts";
|
|
2
|
+
/** The 128 arkworks-compressed bytes `sui::groth16` reads, the layout of `packages/sdk/src/membership/sui-bytes.ts:37`. */
|
|
3
|
+
export declare function proofBytes(p: SnarkProof): Uint8Array;
|
|
4
|
+
//# sourceMappingURL=proof-bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proof-bytes.d.ts","sourceRoot":"","sources":["../../src/v3/proof-bytes.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAsB9C,2HAA2H;AAC3H,wBAAgB,UAAU,CAAC,CAAC,EAAE,UAAU,GAAG,UAAU,CAEpD"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const Q = 21888242871839275222246405745257275088696311157297823662689037894645226208583n;
|
|
2
|
+
function le(x, n) {
|
|
3
|
+
return Array.from({ length: n }, (_, i) => Number((x >> BigInt(8 * i)) & 0xffn));
|
|
4
|
+
}
|
|
5
|
+
function g1([x, y]) {
|
|
6
|
+
const b = le(BigInt(x), 32);
|
|
7
|
+
if (BigInt(y) > Q - BigInt(y))
|
|
8
|
+
b[31] |= 0x80;
|
|
9
|
+
return b;
|
|
10
|
+
}
|
|
11
|
+
function g2([[x0, x1], [y0, y1]]) {
|
|
12
|
+
const b = [...le(BigInt(x0), 32), ...le(BigInt(x1), 32)];
|
|
13
|
+
const [c0, c1] = [BigInt(y0), BigInt(y1)];
|
|
14
|
+
const [n0, n1] = [(Q - c0) % Q, (Q - c1) % Q];
|
|
15
|
+
if (c1 > n1 || (c1 === n1 && c0 > n0))
|
|
16
|
+
b[63] |= 0x80;
|
|
17
|
+
return b;
|
|
18
|
+
}
|
|
19
|
+
/** The 128 arkworks-compressed bytes `sui::groth16` reads, the layout of `packages/sdk/src/membership/sui-bytes.ts:37`. */
|
|
20
|
+
export function proofBytes(p) {
|
|
21
|
+
return Uint8Array.from([...g1(p.pi_a), ...g2(p.pi_b), ...g1(p.pi_c)]);
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { Fetch } from "../network.ts";
|
|
2
|
+
import type { Proved } from "./bodies.ts";
|
|
3
|
+
import type { CircuitInput } from "./inputs.ts";
|
|
4
|
+
/** One circuit's proving files. The bytes never leave this process. */
|
|
5
|
+
export type Artifacts = {
|
|
6
|
+
wasm: Uint8Array;
|
|
7
|
+
zkey: Uint8Array;
|
|
8
|
+
};
|
|
9
|
+
/** The slice of `snarkjs.groth16` a prover runs on. The caller supplies it. */
|
|
10
|
+
export type Groth16Engine = {
|
|
11
|
+
fullProve(input: CircuitInput, wasm: Uint8Array, zkey: Uint8Array): Promise<Proved>;
|
|
12
|
+
};
|
|
13
|
+
export type Prover = {
|
|
14
|
+
prove(circuit: string, input: CircuitInput): Promise<Proved>;
|
|
15
|
+
};
|
|
16
|
+
export declare const sha256Hex: (bytes: Uint8Array) => string;
|
|
17
|
+
/** Downloads one artifact and refuses it unless its sha256 is the pinned one. */
|
|
18
|
+
export declare function fetchPinned(fetch: Fetch, url: string, sha256: string): Promise<Uint8Array>;
|
|
19
|
+
/** Circuits `Essey` proves. Each value is the public URL and the sha256 `fetchPinned` checks. */
|
|
20
|
+
export declare const SDK_CIRCUITS: readonly ["membership", "deposit", "js4", "coll_lock", "health", "loan_offer", "loan_settle", "loan_repay", "reclaim", "reclaim_coll", "loan_refi", "lock_bid", "sale_settle", "item_claim"];
|
|
21
|
+
export type PinnedArtifact = {
|
|
22
|
+
url: string;
|
|
23
|
+
sha256: string;
|
|
24
|
+
};
|
|
25
|
+
/** The wasm and zkey pins for one circuit in the deployment `circuits` object. */
|
|
26
|
+
export declare function circuitPins(circuits: Record<string, unknown>, circuit: string): {
|
|
27
|
+
wasm: PinnedArtifact;
|
|
28
|
+
zkey: PinnedArtifact;
|
|
29
|
+
};
|
|
30
|
+
/** Downloads the wasm and zkey `fetchPinned` will accept for `circuit`. */
|
|
31
|
+
export declare function loadCircuitArtifacts(fetch: Fetch, circuits: Record<string, unknown>, circuit: string): Promise<Artifacts>;
|
|
32
|
+
/** Loads each circuit's artifacts once, then proves with them. */
|
|
33
|
+
export declare function groth16Prover(engine: Groth16Engine, artifacts: (circuit: string) => Promise<Artifacts>): Prover;
|
|
34
|
+
//# sourceMappingURL=prover.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prover.d.ts","sourceRoot":"","sources":["../../src/v3/prover.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAEhD,uEAAuE;AACvE,MAAM,MAAM,SAAS,GAAG;IAAE,IAAI,EAAE,UAAU,CAAC;IAAC,IAAI,EAAE,UAAU,CAAA;CAAE,CAAC;AAE/D,+EAA+E;AAC/E,MAAM,MAAM,aAAa,GAAG;IAC1B,SAAS,CAAC,KAAK,EAAE,YAAY,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CACrF,CAAC;AAEF,MAAM,MAAM,MAAM,GAAG;IAAE,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY,GAAG,OAAO,CAAC,MAAM,CAAC,CAAA;CAAE,CAAC;AAEtF,eAAO,MAAM,SAAS,GAAI,OAAO,UAAU,WAAqD,CAAC;AAEjG,iFAAiF;AACjF,wBAAsB,WAAW,CAAC,KAAK,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAchG;AAED,iGAAiG;AACjG,eAAO,MAAM,YAAY,8LAef,CAAC;AAEX,MAAM,MAAM,cAAc,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AAS7D,kFAAkF;AAClF,wBAAgB,WAAW,CACzB,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,OAAO,EAAE,MAAM,GACd;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,IAAI,EAAE,cAAc,CAAA;CAAE,CAIhD;AAED,2EAA2E;AAC3E,wBAAsB,oBAAoB,CACxC,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACjC,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,SAAS,CAAC,CAOpB;AAED,kEAAkE;AAClE,wBAAgB,aAAa,CAC3B,MAAM,EAAE,aAAa,EACrB,SAAS,EAAE,CAAC,OAAO,EAAE,MAAM,KAAK,OAAO,CAAC,SAAS,CAAC,GACjD,MAAM,CAcR"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { EsseyError } from "../errors.js";
|
|
3
|
+
export const sha256Hex = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
|
4
|
+
/** Downloads one artifact and refuses it unless its sha256 is the pinned one. */
|
|
5
|
+
export async function fetchPinned(fetch, url, sha256) {
|
|
6
|
+
if (!/^[0-9a-f]{64}$/.test(sha256))
|
|
7
|
+
throw new EsseyError("INVALID_ARGUMENT", "pin is not a sha256");
|
|
8
|
+
let res;
|
|
9
|
+
try {
|
|
10
|
+
res = await fetch(url);
|
|
11
|
+
}
|
|
12
|
+
catch {
|
|
13
|
+
throw new EsseyError("SERVICE_UNREACHABLE", "artifact host did not answer");
|
|
14
|
+
}
|
|
15
|
+
if (res.status !== 200)
|
|
16
|
+
throw new EsseyError("SERVICE_REFUSED", "artifact was not served", { status: res.status });
|
|
17
|
+
const bytes = new Uint8Array(await res.arrayBuffer());
|
|
18
|
+
if (sha256Hex(bytes) !== sha256)
|
|
19
|
+
throw new EsseyError("ARTIFACT_MISMATCH", "refused: artifact bytes do not match the pinned sha256");
|
|
20
|
+
return bytes;
|
|
21
|
+
}
|
|
22
|
+
/** Circuits `Essey` proves. Each value is the public URL and the sha256 `fetchPinned` checks. */
|
|
23
|
+
export const SDK_CIRCUITS = [
|
|
24
|
+
"membership",
|
|
25
|
+
"deposit",
|
|
26
|
+
"js4",
|
|
27
|
+
"coll_lock",
|
|
28
|
+
"health",
|
|
29
|
+
"loan_offer",
|
|
30
|
+
"loan_settle",
|
|
31
|
+
"loan_repay",
|
|
32
|
+
"reclaim",
|
|
33
|
+
"reclaim_coll",
|
|
34
|
+
"loan_refi",
|
|
35
|
+
"lock_bid",
|
|
36
|
+
"sale_settle",
|
|
37
|
+
"item_claim",
|
|
38
|
+
];
|
|
39
|
+
function pinnedFile(value, what) {
|
|
40
|
+
const row = value;
|
|
41
|
+
if (!row || typeof row.url !== "string" || typeof row.sha256 !== "string")
|
|
42
|
+
throw new EsseyError("NOT_SET_UP", `${what} is not a pinned artifact`);
|
|
43
|
+
return { url: row.url, sha256: row.sha256 };
|
|
44
|
+
}
|
|
45
|
+
/** The wasm and zkey pins for one circuit in the deployment `circuits` object. */
|
|
46
|
+
export function circuitPins(circuits, circuit) {
|
|
47
|
+
const row = circuits[circuit];
|
|
48
|
+
if (!row)
|
|
49
|
+
throw new EsseyError("NOT_SET_UP", `no prover: ${circuit} artifacts are not in the config yet`);
|
|
50
|
+
return { wasm: pinnedFile(row.wasm, `${circuit} wasm`), zkey: pinnedFile(row.zkey, `${circuit} zkey`) };
|
|
51
|
+
}
|
|
52
|
+
/** Downloads the wasm and zkey `fetchPinned` will accept for `circuit`. */
|
|
53
|
+
export async function loadCircuitArtifacts(fetch, circuits, circuit) {
|
|
54
|
+
const pins = circuitPins(circuits, circuit);
|
|
55
|
+
const [wasm, zkey] = await Promise.all([
|
|
56
|
+
fetchPinned(fetch, pins.wasm.url, pins.wasm.sha256),
|
|
57
|
+
fetchPinned(fetch, pins.zkey.url, pins.zkey.sha256),
|
|
58
|
+
]);
|
|
59
|
+
return { wasm, zkey };
|
|
60
|
+
}
|
|
61
|
+
/** Loads each circuit's artifacts once, then proves with them. */
|
|
62
|
+
export function groth16Prover(engine, artifacts) {
|
|
63
|
+
const loaded = new Map();
|
|
64
|
+
return {
|
|
65
|
+
async prove(circuit, input) {
|
|
66
|
+
let files = loaded.get(circuit);
|
|
67
|
+
if (!files) {
|
|
68
|
+
files = artifacts(circuit);
|
|
69
|
+
loaded.set(circuit, files);
|
|
70
|
+
files.catch(() => loaded.delete(circuit));
|
|
71
|
+
}
|
|
72
|
+
const { wasm, zkey } = await files;
|
|
73
|
+
return engine.fullProve(input, wasm, zkey);
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { EsseyError, type ServiceDetail } from "../../errors.ts";
|
|
2
|
+
import { type Fetch, type JsonReply } from "../../network.ts";
|
|
3
|
+
import type { RelayBody } from "../bodies.ts";
|
|
4
|
+
export type Landed = {
|
|
5
|
+
digest: string;
|
|
6
|
+
ticket: string;
|
|
7
|
+
};
|
|
8
|
+
export type Queued = {
|
|
9
|
+
ticket: string;
|
|
10
|
+
etaMs: number;
|
|
11
|
+
};
|
|
12
|
+
/** A failed ticket carries `error` and `submitted` once the relayer sends them; `OUTCOME_UNKNOWN` may still land. */
|
|
13
|
+
export type TicketState = {
|
|
14
|
+
state: "queued" | "landed" | "failed" | "expired";
|
|
15
|
+
digest?: string;
|
|
16
|
+
error?: string;
|
|
17
|
+
submitted?: boolean;
|
|
18
|
+
};
|
|
19
|
+
/** `GET /v3/relayer`. `address` is null until the relayer has a key. */
|
|
20
|
+
export type RelayerInfo = {
|
|
21
|
+
address: string | null;
|
|
22
|
+
pools: Record<string, {
|
|
23
|
+
poolId: string | null;
|
|
24
|
+
coinType: string;
|
|
25
|
+
relayerFee: string;
|
|
26
|
+
}>;
|
|
27
|
+
kinds: Record<string, {
|
|
28
|
+
proofs: string[];
|
|
29
|
+
args: string[];
|
|
30
|
+
calls: string[];
|
|
31
|
+
}>;
|
|
32
|
+
};
|
|
33
|
+
export type RelayClientOptions = {
|
|
34
|
+
url: string;
|
|
35
|
+
fetch: Fetch;
|
|
36
|
+
/** D §4.4: a uniform random wait in `[min, max]` ms before each submit. */
|
|
37
|
+
submitDelayMs?: [number, number];
|
|
38
|
+
sleep?: (ms: number) => Promise<void>;
|
|
39
|
+
/** Uniform integer in `[min, max]`. */
|
|
40
|
+
random?: (min: number, max: number) => number;
|
|
41
|
+
};
|
|
42
|
+
/** The error a relayer's reply or failed ticket stands for; `unnamed` labels one that carries no `error`. */
|
|
43
|
+
export declare function failure(detail: ServiceDetail, unnamed?: string): EsseyError;
|
|
44
|
+
export declare const refusal: (reply: JsonReply) => EsseyError;
|
|
45
|
+
export declare class RelayClient {
|
|
46
|
+
private readonly base;
|
|
47
|
+
private readonly fetch;
|
|
48
|
+
private readonly delay;
|
|
49
|
+
private readonly sleep;
|
|
50
|
+
private readonly random;
|
|
51
|
+
constructor(o: RelayClientOptions);
|
|
52
|
+
info(): Promise<RelayerInfo>;
|
|
53
|
+
/** 200 is a landed transaction, 202 a queued one whose ticket `status` follows. */
|
|
54
|
+
submit(body: RelayBody): Promise<Landed | Queued>;
|
|
55
|
+
status(ticket: string): Promise<TicketState>;
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../../../src/v3/relay/client.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAE,KAAK,aAAa,EAAE,MAAM,iBAAiB,CAAC;AACjE,OAAO,EAAiC,KAAK,KAAK,EAAE,KAAK,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAE9C,MAAM,MAAM,MAAM,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC;AACxD,MAAM,MAAM,MAAM,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AACvD,qHAAqH;AACrH,MAAM,MAAM,WAAW,GAAG;IACxB,KAAK,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,CAAC;IAClD,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,OAAO,CAAC;CACrB,CAAC;AAEF,wEAAwE;AACxE,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAA;KAAE,CAAC,CAAC;IACvF,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE;QAAE,MAAM,EAAE,MAAM,EAAE,CAAC;QAAC,IAAI,EAAE,MAAM,EAAE,CAAC;QAAC,KAAK,EAAE,MAAM,EAAE,CAAA;KAAE,CAAC,CAAC;CAC9E,CAAC;AAEF,MAAM,MAAM,kBAAkB,GAAG;IAC/B,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,KAAK,CAAC;IACb,2EAA2E;IAC3E,aAAa,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,uCAAuC;IACvC,MAAM,CAAC,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,KAAK,MAAM,CAAC;CAC/C,CAAC;AAeF,6GAA6G;AAC7G,wBAAgB,OAAO,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,SAAwB,GAAG,UAAU,CAW1F;AAED,eAAO,MAAM,OAAO,GAAI,OAAO,SAAS,KAAG,UAAsC,CAAC;AAUlF,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAAS;IAC9B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAQ;IAC9B,OAAO,CAAC,QAAQ,CAAC,KAAK,CAA+B;IACrD,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAgC;IACtD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuC;gBAElD,CAAC,EAAE,kBAAkB;IAQ3B,IAAI,IAAI,OAAO,CAAC,WAAW,CAAC;IAOlC,mFAAmF;IAC7E,MAAM,CAAC,IAAI,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,MAAM,CAAC;IAUjD,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;CAanD"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { randomInt } from "node:crypto";
|
|
2
|
+
import { EsseyError } from "../../errors.js";
|
|
3
|
+
import { getJson, postJson, serviceUrl } from "../../network.js";
|
|
4
|
+
const TICKET = /^t_[0-9a-f]{32}$/;
|
|
5
|
+
const STATES = new Set(["queued", "landed", "failed", "expired"]);
|
|
6
|
+
function detailOf(reply) {
|
|
7
|
+
const { error, submitted, abort } = reply.body;
|
|
8
|
+
return {
|
|
9
|
+
status: reply.status,
|
|
10
|
+
error: typeof error === "string" ? error : undefined,
|
|
11
|
+
submitted: submitted === true,
|
|
12
|
+
abort: abort && typeof abort === "object" ? abort : undefined,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** The error a relayer's reply or failed ticket stands for; `unnamed` labels one that carries no `error`. */
|
|
16
|
+
export function failure(detail, unnamed = String(detail.status)) {
|
|
17
|
+
const label = detail.error ?? unnamed;
|
|
18
|
+
if (detail.error === "OUTCOME_UNKNOWN")
|
|
19
|
+
return new EsseyError("RELAY_OUTCOME_UNKNOWN", "transaction may still land: reconcile it before any new spend", detail);
|
|
20
|
+
if (detail.submitted)
|
|
21
|
+
return new EsseyError("RELAY_FAILED", `transaction failed on chain: ${label}`, detail);
|
|
22
|
+
return new EsseyError("RELAY_REFUSED", `relayer refused: ${label}`, detail);
|
|
23
|
+
}
|
|
24
|
+
export const refusal = (reply) => failure(detailOf(reply));
|
|
25
|
+
function delayRange(range) {
|
|
26
|
+
if (range === undefined)
|
|
27
|
+
return undefined;
|
|
28
|
+
const [min, max] = range;
|
|
29
|
+
if (!Number.isInteger(min) || !Number.isInteger(max) || min < 0 || max < min)
|
|
30
|
+
throw new EsseyError("INVALID_ARGUMENT", "submitDelayMs must be [min, max] in whole milliseconds");
|
|
31
|
+
return [min, max];
|
|
32
|
+
}
|
|
33
|
+
export class RelayClient {
|
|
34
|
+
base;
|
|
35
|
+
fetch;
|
|
36
|
+
delay;
|
|
37
|
+
sleep;
|
|
38
|
+
random;
|
|
39
|
+
constructor(o) {
|
|
40
|
+
this.base = serviceUrl(o.url, "relayer url").origin;
|
|
41
|
+
this.fetch = o.fetch;
|
|
42
|
+
this.delay = delayRange(o.submitDelayMs);
|
|
43
|
+
this.sleep = o.sleep ?? ((ms) => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
44
|
+
this.random = o.random ?? ((min, max) => randomInt(min, max + 1));
|
|
45
|
+
}
|
|
46
|
+
async info() {
|
|
47
|
+
const reply = await getJson(this.fetch, `${this.base}/v3/relayer`);
|
|
48
|
+
if (reply.status !== 200 || typeof reply.body.pools !== "object" || typeof reply.body.kinds !== "object")
|
|
49
|
+
throw refusal(reply);
|
|
50
|
+
return reply.body;
|
|
51
|
+
}
|
|
52
|
+
/** 200 is a landed transaction, 202 a queued one whose ticket `status` follows. */
|
|
53
|
+
async submit(body) {
|
|
54
|
+
if (this.delay)
|
|
55
|
+
await this.sleep(this.random(...this.delay));
|
|
56
|
+
const reply = await postJson(this.fetch, `${this.base}/v3/relay`, body);
|
|
57
|
+
const { ok, digest, ticket, etaMs } = reply.body;
|
|
58
|
+
if (ok !== true || typeof ticket !== "string" || !TICKET.test(ticket))
|
|
59
|
+
throw refusal(reply);
|
|
60
|
+
if (reply.status === 200 && typeof digest === "string")
|
|
61
|
+
return { digest, ticket };
|
|
62
|
+
if (reply.status === 202 && typeof etaMs === "number")
|
|
63
|
+
return { ticket, etaMs };
|
|
64
|
+
throw refusal(reply);
|
|
65
|
+
}
|
|
66
|
+
async status(ticket) {
|
|
67
|
+
if (!TICKET.test(ticket))
|
|
68
|
+
throw new EsseyError("INVALID_ARGUMENT", "ticket is malformed");
|
|
69
|
+
const reply = await postJson(this.fetch, `${this.base}/v3/status`, { ticket });
|
|
70
|
+
const { ok, state, digest, error, submitted } = reply.body;
|
|
71
|
+
if (reply.status !== 200 || ok !== true || typeof state !== "string" || !STATES.has(state))
|
|
72
|
+
throw refusal(reply);
|
|
73
|
+
return {
|
|
74
|
+
state: state,
|
|
75
|
+
...(typeof digest === "string" ? { digest } : {}),
|
|
76
|
+
...(typeof error === "string" ? { error } : {}),
|
|
77
|
+
...(typeof submitted === "boolean" ? { submitted } : {}),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { Signer } from "@mysten/sui/cryptography";
|
|
2
|
+
/** The `possession` field of a `/v3/sponsor` body (BORIS-RULING 0204, DECISIONS #303). */
|
|
3
|
+
export type Possession = {
|
|
4
|
+
nonce: string;
|
|
5
|
+
signature: string;
|
|
6
|
+
};
|
|
7
|
+
/** Equal to the relayer's `POSSESSION_DOMAIN` (`services/relayer-v3/src/sponsor.ts`). */
|
|
8
|
+
export declare const POSSESSION_DOMAIN = "essey/v3/sponsor-pop/v1";
|
|
9
|
+
/** The expiry in ms as 8 big-endian bytes, then 24 CSPRNG bytes, as the relayer's lowercase hex. */
|
|
10
|
+
export declare function possessionNonce(expiryMs: number, random?: Uint8Array): string;
|
|
11
|
+
/** domain || sender || blake2b256(kind) || relayer || nonce: the 151 bytes the relayer verifies. */
|
|
12
|
+
export declare function possessionMessage(sender: string, kind: Uint8Array, relayer: string, nonce: string): Uint8Array;
|
|
13
|
+
/** Proves `signer` holds the sender key for `kind`. The key is used only through `signPersonalMessage`. */
|
|
14
|
+
export declare function provePossession(o: {
|
|
15
|
+
signer: Signer;
|
|
16
|
+
kind: Uint8Array;
|
|
17
|
+
relayer: string;
|
|
18
|
+
expiryMs: number;
|
|
19
|
+
random?: Uint8Array;
|
|
20
|
+
}): Promise<Possession>;
|
|
21
|
+
//# sourceMappingURL=possession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"possession.d.ts","sourceRoot":"","sources":["../../../src/v3/relay/possession.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,0BAA0B,CAAC;AAKvD,0FAA0F;AAC1F,MAAM,MAAM,UAAU,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,CAAC;AAE9D,yFAAyF;AACzF,eAAO,MAAM,iBAAiB,4BAA4B,CAAC;AAW3D,oGAAoG;AACpG,wBAAgB,eAAe,CAC7B,QAAQ,EAAE,MAAM,EAChB,MAAM,GAAE,UAA4C,GACnD,MAAM,CAQR;AAED,oGAAoG;AACpG,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,UAAU,EAChB,OAAO,EAAE,MAAM,EACf,KAAK,EAAE,MAAM,GACZ,UAAU,CASZ;AAED,2GAA2G;AAC3G,wBAAsB,eAAe,CAAC,CAAC,EAAE;IACvC,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,UAAU,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,MAAM,CAAC,EAAE,UAAU,CAAC;CACrB,GAAG,OAAO,CAAC,UAAU,CAAC,CAOtB"}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { blake2b } from "@noble/hashes/blake2.js";
|
|
3
|
+
import { isValidSuiAddress, normalizeSuiAddress } from "@mysten/sui/utils";
|
|
4
|
+
import { EsseyError } from "../../errors.js";
|
|
5
|
+
/** Equal to the relayer's `POSSESSION_DOMAIN` (`services/relayer-v3/src/sponsor.ts`). */
|
|
6
|
+
export const POSSESSION_DOMAIN = "essey/v3/sponsor-pop/v1";
|
|
7
|
+
const NONCE_RANDOM_BYTES = 24;
|
|
8
|
+
const NONCE = /^0x[0-9a-f]{64}$/;
|
|
9
|
+
function address32(value, what) {
|
|
10
|
+
const normal = normalizeSuiAddress(value);
|
|
11
|
+
if (!isValidSuiAddress(normal))
|
|
12
|
+
throw new EsseyError("INVALID_ARGUMENT", `${what} is not a Sui address`);
|
|
13
|
+
return Buffer.from(normal.slice(2), "hex");
|
|
14
|
+
}
|
|
15
|
+
/** The expiry in ms as 8 big-endian bytes, then 24 CSPRNG bytes, as the relayer's lowercase hex. */
|
|
16
|
+
export function possessionNonce(expiryMs, random = randomBytes(NONCE_RANDOM_BYTES)) {
|
|
17
|
+
if (!Number.isSafeInteger(expiryMs) || expiryMs <= 0)
|
|
18
|
+
throw new EsseyError("INVALID_ARGUMENT", "expiryMs must be a positive whole number of milliseconds");
|
|
19
|
+
if (random.length !== NONCE_RANDOM_BYTES)
|
|
20
|
+
throw new EsseyError("INVALID_ARGUMENT", `the nonce takes exactly ${NONCE_RANDOM_BYTES} random bytes`);
|
|
21
|
+
const head = Buffer.alloc(8);
|
|
22
|
+
head.writeBigUInt64BE(BigInt(expiryMs));
|
|
23
|
+
return `0x${head.toString("hex")}${Buffer.from(random).toString("hex")}`;
|
|
24
|
+
}
|
|
25
|
+
/** domain || sender || blake2b256(kind) || relayer || nonce: the 151 bytes the relayer verifies. */
|
|
26
|
+
export function possessionMessage(sender, kind, relayer, nonce) {
|
|
27
|
+
if (!NONCE.test(nonce))
|
|
28
|
+
throw new EsseyError("INVALID_ARGUMENT", "nonce is not 32 lowercase hex bytes");
|
|
29
|
+
return Buffer.concat([
|
|
30
|
+
Buffer.from(POSSESSION_DOMAIN, "utf8"),
|
|
31
|
+
address32(sender, "sender"),
|
|
32
|
+
blake2b(kind, { dkLen: 32 }),
|
|
33
|
+
address32(relayer, "relayer"),
|
|
34
|
+
Buffer.from(nonce.slice(2), "hex"),
|
|
35
|
+
]);
|
|
36
|
+
}
|
|
37
|
+
/** Proves `signer` holds the sender key for `kind`. The key is used only through `signPersonalMessage`. */
|
|
38
|
+
export async function provePossession(o) {
|
|
39
|
+
if (o.signer.getKeyScheme() !== "ED25519")
|
|
40
|
+
throw new EsseyError("INVALID_ARGUMENT", "the relayer accepts an Ed25519 sender only");
|
|
41
|
+
const nonce = possessionNonce(o.expiryMs, o.random);
|
|
42
|
+
const message = possessionMessage(o.signer.toSuiAddress(), o.kind, o.relayer, nonce);
|
|
43
|
+
const { signature } = await o.signer.signPersonalMessage(message);
|
|
44
|
+
return { nonce, signature };
|
|
45
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { type FeeCommitInput } from "../fees.ts";
|
|
2
|
+
import type { CircuitInput } from "../inputs.ts";
|
|
3
|
+
export type PlanParams = {
|
|
4
|
+
/** `SaleRoom` as read from chain: its `sale_key`, both commits, and each `BidEntry` in seq order. */
|
|
5
|
+
room: {
|
|
6
|
+
saleKey: bigint;
|
|
7
|
+
boundsCommit: bigint;
|
|
8
|
+
sellerCommit: bigint;
|
|
9
|
+
bids: {
|
|
10
|
+
commit: bigint;
|
|
11
|
+
lock: bigint;
|
|
12
|
+
active: boolean;
|
|
13
|
+
}[];
|
|
14
|
+
};
|
|
15
|
+
/** The decrypted sealed bid of each seq; absent where decryption gave nothing. */
|
|
16
|
+
plains: (Uint8Array | undefined)[];
|
|
17
|
+
seller: {
|
|
18
|
+
sellerSk: bigint;
|
|
19
|
+
sellerSecret: bigint;
|
|
20
|
+
};
|
|
21
|
+
bounds: {
|
|
22
|
+
lo: bigint;
|
|
23
|
+
hi: bigint;
|
|
24
|
+
salt: bigint;
|
|
25
|
+
};
|
|
26
|
+
/** `[seller note, fee note]`, the order `finish` hands `room_insert`. */
|
|
27
|
+
encs: [Uint8Array, Uint8Array];
|
|
28
|
+
fee: FeeCommitInput;
|
|
29
|
+
eph: bigint;
|
|
30
|
+
};
|
|
31
|
+
/** The `u256` arguments of `sale_room::finish`, under Move's own names and in its order. */
|
|
32
|
+
export type SettleStatement = {
|
|
33
|
+
seller_out: bigint;
|
|
34
|
+
fee_out: bigint;
|
|
35
|
+
residual: bigint;
|
|
36
|
+
item_claim: bigint;
|
|
37
|
+
win_null: bigint;
|
|
38
|
+
eph_x: bigint;
|
|
39
|
+
eph_y: bigint;
|
|
40
|
+
fee_ct: bigint;
|
|
41
|
+
fee_commit: bigint;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* Witness for `circuits/v3/sale_settle/sale_settle.circom`. Public signals, in order: hSet, hLock, hCtx, hOut.
|
|
45
|
+
* `slash` lists the active bids whose plaintext does not open their commit and lock. The witness already
|
|
46
|
+
* leaves them out of the mask, so it proves only once each has been slashed on chain.
|
|
47
|
+
* hCtx carries relayer `NO_ADDRESS` and fee 0, the only pair `sale_room::finish` rebuilds.
|
|
48
|
+
*/
|
|
49
|
+
export declare function plan(p: PlanParams): Promise<{
|
|
50
|
+
slash: number[];
|
|
51
|
+
input: CircuitInput;
|
|
52
|
+
publics: bigint[];
|
|
53
|
+
statement: SettleStatement;
|
|
54
|
+
/** The opening of `seller_out`, which the seller seals to its own note key. */
|
|
55
|
+
sellerNote: {
|
|
56
|
+
value: bigint;
|
|
57
|
+
blinding: bigint;
|
|
58
|
+
};
|
|
59
|
+
}>;
|
|
60
|
+
//# sourceMappingURL=auctioneer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auctioneer.d.ts","sourceRoot":"","sources":["../../../src/v3/sales/auctioneer.ts"],"names":[],"mappings":"AAMA,OAAO,EAAW,KAAK,cAAc,EAAE,MAAM,YAAY,CAAC;AAC1D,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAUjD,MAAM,MAAM,UAAU,GAAG;IACvB,qGAAqG;IACrG,IAAI,EAAE;QACJ,OAAO,EAAE,MAAM,CAAC;QAChB,YAAY,EAAE,MAAM,CAAC;QACrB,YAAY,EAAE,MAAM,CAAC;QACrB,IAAI,EAAE;YAAE,MAAM,EAAE,MAAM,CAAC;YAAC,IAAI,EAAE,MAAM,CAAC;YAAC,MAAM,EAAE,OAAO,CAAA;SAAE,EAAE,CAAC;KAC3D,CAAC;IACF,kFAAkF;IAClF,MAAM,EAAE,CAAC,UAAU,GAAG,SAAS,CAAC,EAAE,CAAC;IACnC,MAAM,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,MAAM,CAAA;KAAE,CAAC;IACnD,MAAM,EAAE;QAAE,EAAE,EAAE,MAAM,CAAC;QAAC,EAAE,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,CAAA;KAAE,CAAC;IACjD,yEAAyE;IACzE,IAAI,EAAE,CAAC,UAAU,EAAE,UAAU,CAAC,CAAC;IAC/B,GAAG,EAAE,cAAc,CAAC;IACpB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAEF,4FAA4F;AAC5F,MAAM,MAAM,eAAe,GAAG;IAC5B,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,QAAQ,EAAE,MAAM,CAAC;IACjB,UAAU,EAAE,MAAM,CAAC;IACnB,QAAQ,EAAE,MAAM,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAiCF;;;;;GAKG;AACH,wBAAsB,IAAI,CAAC,CAAC,EAAE,UAAU,GAAG,OAAO,CAAC;IACjD,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,EAAE,MAAM,EAAE,CAAC;IAClB,SAAS,EAAE,eAAe,CAAC;IAC3B,+EAA+E;IAC/E,UAAU,EAAE;QAAE,KAAK,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;CACjD,CAAC,CAqFD"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { poseidonHash } from "@mysten/sui/zklogin";
|
|
2
|
+
import { addrField } from "../addr-field.js";
|
|
3
|
+
import { DOM_BOUNDS, DOM_DUMMY, DOM_LNULL, DOM_SBLIND, DOM_SELLER } from "../domains.js";
|
|
4
|
+
import { NO_ADDRESS } from "../ext.js";
|
|
5
|
+
import { feeWitness } from "../fee-witness.js";
|
|
6
|
+
import { feeLane } from "../fees.js";
|
|
7
|
+
import { noteCommitment, notePk } from "../notes/keys.js";
|
|
8
|
+
import { itemClaimCommit, noWinnerSalt, winnerSalt } from "./claim.js";
|
|
9
|
+
import { encHash, lock4, openBid, padSlots, residualLock } from "./rows.js";
|
|
10
|
+
/** `sale_settle.circom` VERSION and RULE_HIGHEST, the constants `rows::fold_settle_ctx` folds. */
|
|
11
|
+
const VERSION = 3n;
|
|
12
|
+
const RULE_HIGHEST = 1n;
|
|
13
|
+
/** AuctionCore rule 1: the highest value in [lo, hi] wins, the earliest seq on a tie. -1 is no winner. */
|
|
14
|
+
function pickWinner(opened, lo, hi) {
|
|
15
|
+
let winner = -1;
|
|
16
|
+
opened.forEach((o, i) => {
|
|
17
|
+
if (!o || o.v < lo || o.v > hi)
|
|
18
|
+
return;
|
|
19
|
+
if (winner < 0 || o.v > opened[winner].v)
|
|
20
|
+
winner = i;
|
|
21
|
+
});
|
|
22
|
+
return winner;
|
|
23
|
+
}
|
|
24
|
+
function winnerBranch(roomField, won) {
|
|
25
|
+
return {
|
|
26
|
+
residual: residualLock(roomField, won.ownerPk, won.ns, won.amount - won.v).lock,
|
|
27
|
+
item_claim: itemClaimCommit(won.ownerPk, winnerSalt(won.ns)),
|
|
28
|
+
win_null: lock4(roomField, won.ownerPk, won.ns, won.amount, won.b).nullifier,
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
/** C §4.4, §4.6: with no winner the residual is a zero lock and the item claim is the seller's. */
|
|
32
|
+
function dummyBranch(roomField, sellerPk, sellerSecret) {
|
|
33
|
+
const ns = poseidonHash([DOM_DUMMY, sellerSecret]);
|
|
34
|
+
const b = poseidonHash([DOM_DUMMY, poseidonHash([sellerSecret, roomField])]);
|
|
35
|
+
return {
|
|
36
|
+
residual: lock4(roomField, sellerPk, ns, 0n, b).lock,
|
|
37
|
+
item_claim: itemClaimCommit(sellerPk, noWinnerSalt(sellerSecret, roomField)),
|
|
38
|
+
win_null: poseidonHash([DOM_LNULL, poseidonHash([DOM_DUMMY, sellerSecret, roomField])]),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Witness for `circuits/v3/sale_settle/sale_settle.circom`. Public signals, in order: hSet, hLock, hCtx, hOut.
|
|
43
|
+
* `slash` lists the active bids whose plaintext does not open their commit and lock. The witness already
|
|
44
|
+
* leaves them out of the mask, so it proves only once each has been slashed on chain.
|
|
45
|
+
* hCtx carries relayer `NO_ADDRESS` and fee 0, the only pair `sale_room::finish` rebuilds.
|
|
46
|
+
*/
|
|
47
|
+
export async function plan(p) {
|
|
48
|
+
const { room, bounds } = p;
|
|
49
|
+
const { sellerSecret } = p.seller;
|
|
50
|
+
const sellerPk = notePk(p.seller.sellerSk);
|
|
51
|
+
if (poseidonHash([DOM_BOUNDS, bounds.lo, bounds.hi, bounds.salt]) !== room.boundsCommit)
|
|
52
|
+
throw new Error("the bounds do not open this room");
|
|
53
|
+
if (poseidonHash([DOM_SELLER, sellerPk, poseidonHash([sellerSecret])]) !== room.sellerCommit)
|
|
54
|
+
throw new Error("the seller keys do not open this room");
|
|
55
|
+
const commits = padSlots(room.bids.map((b) => b.commit));
|
|
56
|
+
const locks = padSlots(room.bids.map((b) => b.lock));
|
|
57
|
+
const opened = room.bids.map((b, i) => b.active ? openBid(p.plains[i], room.saleKey, b.commit, b.lock) : undefined);
|
|
58
|
+
const slash = room.bids.flatMap((b, i) => (b.active && !opened[i] ? [i] : []));
|
|
59
|
+
const activeMask = opened.reduce((m, o, i) => (o ? m | (1n << BigInt(i)) : m), 0n);
|
|
60
|
+
const w = pickWinner(opened, bounds.lo, bounds.hi);
|
|
61
|
+
const won = opened[w];
|
|
62
|
+
const price = won?.v ?? 0n;
|
|
63
|
+
const lane = await feeLane({ ...p.fee, base: price, eph: p.eph });
|
|
64
|
+
const branch = won ? winnerBranch(room.saleKey, won) : dummyBranch(room.saleKey, sellerPk, sellerSecret);
|
|
65
|
+
const sBlind = poseidonHash([DOM_SBLIND, sellerSecret, room.saleKey]);
|
|
66
|
+
const statement = {
|
|
67
|
+
seller_out: noteCommitment(price - lane.fee, sellerPk, sBlind),
|
|
68
|
+
fee_out: lane.fee_note,
|
|
69
|
+
...branch,
|
|
70
|
+
eph_x: lane.ephX,
|
|
71
|
+
eph_y: lane.ephY,
|
|
72
|
+
fee_ct: lane.fee_ct,
|
|
73
|
+
fee_commit: lane.fee_commit,
|
|
74
|
+
};
|
|
75
|
+
const n = BigInt(room.bids.length);
|
|
76
|
+
const relayerField = addrField(NO_ADDRESS);
|
|
77
|
+
const enc = encHash(p.encs);
|
|
78
|
+
const s = statement;
|
|
79
|
+
const hCtx = poseidonHash([
|
|
80
|
+
n,
|
|
81
|
+
activeMask,
|
|
82
|
+
room.boundsCommit,
|
|
83
|
+
room.sellerCommit,
|
|
84
|
+
lane.fee_commit,
|
|
85
|
+
RULE_HIGHEST,
|
|
86
|
+
room.saleKey,
|
|
87
|
+
VERSION,
|
|
88
|
+
relayerField,
|
|
89
|
+
0n,
|
|
90
|
+
enc,
|
|
91
|
+
]);
|
|
92
|
+
const hOut = poseidonHash([
|
|
93
|
+
s.seller_out,
|
|
94
|
+
s.fee_out,
|
|
95
|
+
s.residual,
|
|
96
|
+
s.item_claim,
|
|
97
|
+
s.win_null,
|
|
98
|
+
s.eph_x,
|
|
99
|
+
s.eph_y,
|
|
100
|
+
s.fee_ct,
|
|
101
|
+
]);
|
|
102
|
+
const input = {
|
|
103
|
+
commits: commits.map(String),
|
|
104
|
+
locks: locks.map(String),
|
|
105
|
+
values: padSlots(opened.map((o) => o?.v ?? 0n)).map(String),
|
|
106
|
+
salts: padSlots(opened.map((o) => o?.s ?? 0n)).map(String),
|
|
107
|
+
n: n.toString(),
|
|
108
|
+
activeMask: activeMask.toString(),
|
|
109
|
+
lo: bounds.lo.toString(),
|
|
110
|
+
hi: bounds.hi.toString(),
|
|
111
|
+
boundsSalt: bounds.salt.toString(),
|
|
112
|
+
hasWinner: won ? "1" : "0",
|
|
113
|
+
winner: won ? w.toString() : "0",
|
|
114
|
+
price: price.toString(),
|
|
115
|
+
amount_w: (won?.amount ?? 0n).toString(),
|
|
116
|
+
ownerPk_w: (won?.ownerPk ?? 0n).toString(),
|
|
117
|
+
ns_w: (won?.ns ?? 0n).toString(),
|
|
118
|
+
b_w: (won?.b ?? 0n).toString(),
|
|
119
|
+
sellerPk: sellerPk.toString(),
|
|
120
|
+
sellerSecret: sellerSecret.toString(),
|
|
121
|
+
roomField: room.saleKey.toString(),
|
|
122
|
+
...feeWitness(p.fee, price, p.eph, lane),
|
|
123
|
+
relayerField: relayerField.toString(),
|
|
124
|
+
relayer_fee: "0",
|
|
125
|
+
encHash: enc.toString(),
|
|
126
|
+
};
|
|
127
|
+
const sellerNote = { value: price - lane.fee, blinding: sBlind };
|
|
128
|
+
const publics = [poseidonHash(commits), poseidonHash(locks), hCtx, hOut];
|
|
129
|
+
return { slash, input, publics, statement, sellerNote };
|
|
130
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bcs.d.ts","sourceRoot":"","sources":["../../../src/v3/sales/bcs.ts"],"names":[],"mappings":"AAEA,wBAAgB,EAAE,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAGtE;AAQD,eAAO,MAAM,QAAQ,GAAI,OAAO,UAAU,KAAG,MAAM,EAAuC,CAAC;AAE3F,eAAO,MAAM,YAAY,GAAI,KAAK,MAAM,EAAE,MAAM,MAAM,KAAG,MAAM,EAE9D,CAAC"}
|