@morpho-org/midnight-sdk
Version:
Viem-based package for Morpho Midnight that exports protocol utilities, fetch helpers, and Midnight API utilities under a dedicated API subpath.
985 lines (984 loc) • 41.8 kB
JavaScript
import { deepFreeze } from "@morpho-org/morpho-ts";
import { decodeAbiParameters, encodeAbiParameters, keccak256, parseSignature, recoverAddress, serializeSignature, verifyHash, } from "viem";
import { signTypedData } from "viem/actions";
import { EIP712_DOMAIN_TYPEHASH } from "../constants.js";
import { ChainIdMismatchError, InvalidEcrecoverSignatureError, InvalidEcrecoverSignatureVError, InvalidTreeError, InvalidTreeHeightError, InvalidTypedDataSignatureError, } from "../errors.js";
import { MarketParams } from "../market/index.js";
import { Offer } from "../offers/index.js";
import { eip712Digest } from "./eip712.js";
import { Ratifier } from "./Ratifier.js";
import { parseStandardRatifierTree } from "./standardRatifierInternal.js";
import { TreeUtils } from "./TreeUtils.js";
const treeTypeHashes = [
"0x270da1ebafc0f24637af3612fb8c3a1d828fcb56d3637c24e86dd006b12ca7f9",
"0x828b9cdf8326a1cf234328e4d5229546a98fb72ef73624f5b6b31538e555b96c",
"0xfcb7a3ca4094246b8185620c4cf025c93032b6f0384805aa3f22afe04290e982",
"0xcc97cb1955496a5269b5a7afca62ba694edcab26ba838a1adbd257931249de92",
"0xda3feb08db360ad9e09540132ff04d2b6a596fdaa4747892217aaa4c7c9bcc31",
"0x15bd6e2aa1a7a61614187ac16d2cbf8610c8f2f3c3d9eaa380ae7a501ee3cf06",
"0xb726cb7fab1a24c28213cbd482fa5a301f127fb25feb01da341919983a72711a",
"0xcea9cd557c6f821868ea287304199d0e0554af630bfa8fe36c64eb3bbacca418",
"0xf7dbde8234e8e345cec8fc0a8ac5909ee336b214882751ecd51e7b37df4f6cdd",
"0x5400a5d43d39e6bfe910af8cb84ac77bf501d310413769dffd62ccecda8b00c6",
"0x0754209b60d99d0822b3ecd5a970f9db09df9c8998a8441e24b81f06d6c76fee",
"0xf5d561d88647c3b38ed6636709d3166819fc66f8ed52a0daf4ae186387b4646c",
"0x5801c07a6c7df039ce00a7a2b8bd92aa1cf333c30b0bc3d78768590b6063d09e",
"0xc9da7190eaf4b14c7cb1c14f9898256c0adb6b1dc303afe79594dea64fe199c0",
"0xa47534c85ac57c583568465d40fd46683d2d558d8129fe1aca01e93023afca92",
"0xb1e841691fb54f4ef85e2ed9de45d610e57f49e1e6eb2510ceead16e447dd519",
"0x4fa4f16f09f0c36c7670449a4032073380d28a60071e12ee8874bb3e5a8318fc",
"0x817bbaac8bb863670f488b454cdd5d0990d9d81871a68e9df381c3c13d3f2ba2",
"0xc447f06079bddf4b011523c4bce119e9e90fdf937de4ee88f48010406560e9c1",
"0x1608d5eb56943c667c34b413f9f8a1c24a84ddfe1301a9c25487e638de1f5822",
"0x3a677100d2e855c24a62d1e9c365bff90d02287f066a07064843ca1ee70ea113",
];
const signatureAbi = [
{
type: "tuple",
components: [
{ name: "v", type: "uint8" },
{ name: "r", type: "bytes32" },
{ name: "s", type: "bytes32" },
],
},
{ name: "root", type: "bytes32" },
{ name: "leafIndex", type: "uint256" },
{ name: "proof", type: "bytes32[]" },
];
const domainSeparatorAbi = [
{ name: "typehash", type: "bytes32" },
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
];
const treeStructHashAbi = [
{ name: "typehash", type: "bytes32" },
{ name: "root", type: "bytes32" },
];
const typedDataTypes = {
EIP712Domain: [
{ name: "chainId", type: "uint256" },
{ name: "verifyingContract", type: "address" },
],
CollateralParams: [
{ name: "token", type: "address" },
{ name: "lltv", type: "uint256" },
{ name: "liquidationCursor", type: "uint256" },
{ name: "oracle", type: "address" },
],
Market: [
{ name: "chainId", type: "uint256" },
{ name: "midnight", type: "address" },
{ name: "loanToken", type: "address" },
{ name: "collateralParams", type: "CollateralParams[]" },
{ name: "maturity", type: "uint256" },
{ name: "rcfThreshold", type: "uint256" },
{ name: "enterGate", type: "address" },
{ name: "liquidatorGate", type: "address" },
],
Offer: [
{ name: "market", type: "Market" },
{ name: "buy", type: "bool" },
{ name: "maker", type: "address" },
{ name: "start", type: "uint256" },
{ name: "expiry", type: "uint256" },
{ name: "tick", type: "uint256" },
{ name: "group", type: "bytes32" },
{ name: "callback", type: "address" },
{ name: "callbackData", type: "bytes" },
{ name: "receiverIfMakerIsSeller", type: "address" },
{ name: "ratifier", type: "address" },
{ name: "reduceOnly", type: "bool" },
{ name: "maxUnits", type: "uint128" },
{ name: "maxAssets", type: "uint128" },
{ name: "continuousFeeCap", type: "uint256" },
],
};
const buildTreeValue = (offers) => {
if (offers.length === 1)
return offers[0];
const mid = offers.length / 2;
return [
buildTreeValue(offers.slice(0, mid)),
buildTreeValue(offers.slice(mid)),
];
};
function toCanonicalYParity(v) {
if (v === 27)
return 0;
if (v === 28)
return 1;
throw new InvalidEcrecoverSignatureVError(v);
}
/**
* EcrecoverRatifier-specific pure utilities.
*
* Use this route for EOA and EIP-7702 makers. The make-side sequence is:
* create offers with the Ecrecover ratifier address, build the group/tree,
* validate the tree, sign the typed data, call `ratify`, then pass the returned
* items to `Payload.encode`. Ecrecover trees must contain one ratifier; split
* trees by ratifier before signing. The signer may be the maker or an address
* authorized by every maker in the tree. Ratifier helpers accept tree-like
* inputs rather than requiring the `Tree` class. Passing an existing `Tree`
* remains the optimal path because its cached offers, leaves, root, and height
* are reused for typed data and per-leaf proofs.
*
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
*
* console.log(EcrecoverRatifierUtils.treeTypeHash(0));
* ```
* @deprecated Use {@link EcrecoverRatifier} with a tagged tree.
*/
export var EcrecoverRatifierUtils;
(function (EcrecoverRatifierUtils) {
/**
* Returns the Solidity HashLib tree typehash for a tree height.
*
* @param height - Tree height.
* @returns Tree typehash.
* @throws {InvalidTreeHeightError} when height exceeds 20.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
*
* console.log(EcrecoverRatifierUtils.treeTypeHash(0));
* ```
*/
function treeTypeHash(height) {
const typehash = treeTypeHashes[height];
if (typehash == null)
throw new InvalidTreeHeightError(height);
return typehash;
}
EcrecoverRatifierUtils.treeTypeHash = treeTypeHash;
/**
* Builds EcrecoverRatifier typed data for a tree-like input.
*
* Use after the tree is built and validated, before requesting the signer
* signature. The EIP-712 verifier is derived from the shared ratifier address
* on the tree offers. `ratify` calls this for you when given a client and
* account.
*
* @param params.tree - Ecrecover-ratified offer tree-like input to sign.
* @param params.chainId - EIP-155 chain id included in the EIP-712 domain.
* @returns EIP-712 typed-data descriptor.
* @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
* @throws {InvalidTreeHeightError} when the tree height is unsupported.
* @example
* ```ts
* import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
* import { Offer } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
*
* const typedData = EcrecoverRatifierUtils.typedData({
* tree: Tree.create([offer]),
* chainId: 8453n,
* });
* console.log(typedData.primaryType);
* ```
*/
function typedData(params) {
const { tree, ratifier: verifyingContract } = Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Ecrecover",
});
const treeType = tree.height === 0 ? "Offer" : `Offer${"[2]".repeat(tree.height)}`;
return deepFreeze({
domain: {
chainId: BigInt(params.chainId),
verifyingContract,
},
types: {
...typedDataTypes,
OfferTree: [{ name: "offerTree", type: treeType }],
},
primaryType: "OfferTree",
message: {
offerTree: buildTreeValue(tree.paddedOffers),
},
});
}
EcrecoverRatifierUtils.typedData = typedData;
/**
* Builds the EcrecoverRatifier digest used by the Solidity ratifier.
*
* @param params.tree - Ecrecover-ratified offer tree-like input to hash.
* @param params.chainId - EIP-155 chain id included in the EIP-712 domain.
* @returns EIP-712 digest.
* @throws {InvalidTreeError} when the tree contains multiple ratifiers.
* @throws {InvalidTreeHeightError} when height exceeds 20.
* @example
* ```ts
* import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
* import { Offer } from "@morpho-org/midnight-sdk";
* import { zeroAddress } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
*
* const digest = EcrecoverRatifierUtils.digest({
* tree: Tree.create([offer]),
* chainId: 8453n,
* });
* console.log(digest);
* ```
*/
function digest(params) {
const { tree, ratifier: verifyingContract } = Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Ecrecover",
});
const domainSeparator = keccak256(encodeAbiParameters(domainSeparatorAbi, [
EIP712_DOMAIN_TYPEHASH,
BigInt(params.chainId),
verifyingContract,
]));
const structHash = keccak256(encodeAbiParameters(treeStructHashAbi, [
treeTypeHash(tree.height),
tree.root,
]));
return eip712Digest(domainSeparator, structHash);
}
EcrecoverRatifierUtils.digest = digest;
/**
* Builds the EcrecoverRatifier digest from a payload offer, root, and proof height.
*
* Use this on the take-side or in indexers after decoding ratifier data, when
* the original full tree is not available but the payload item includes the
* offer and proof metadata needed to reconstruct the signed digest. Pass the
* observed log or execution chain id so cross-chain-spoofed payloads are
* rejected before signature recovery.
*
* @param params.chainId - Observed EIP-155 chain id from the log or execution context.
* @param params.offer - Offer carried by the payload item.
* @param params.root - Merkle root embedded in the ratifier data.
* @param params.proofLength - Number of sibling hashes in the Merkle proof.
* @returns EIP-712 digest signed by the maker or authorized signer.
* @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
* @throws {InvalidTreeHeightError} when `proofLength` exceeds 20.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
* import { zeroHash } from "viem";
*
* const digest = EcrecoverRatifierUtils.digestForRoot({
* chainId: 8453n,
* offer,
* root: zeroHash,
* proofLength: 0,
* });
* console.log(digest);
* ```
*/
function digestForRoot(params) {
const offer = Offer.from(params.offer);
const market = MarketParams.from(offer.market);
const chainId = BigInt(params.chainId);
if (chainId !== market.chainId) {
throw new ChainIdMismatchError(Number(chainId), market.chainId);
}
const domainSeparator = keccak256(encodeAbiParameters(domainSeparatorAbi, [
EIP712_DOMAIN_TYPEHASH,
chainId,
offer.ratifier,
]));
const structHash = keccak256(encodeAbiParameters(treeStructHashAbi, [
treeTypeHash(params.proofLength),
params.root,
]));
return eip712Digest(domainSeparator, structHash);
}
EcrecoverRatifierUtils.digestForRoot = digestForRoot;
/**
* Builds the EcrecoverRatifier digest from one payload item's encoded ratifier data.
*
* @param params.chainId - Observed EIP-155 chain id from the log or execution context.
* @param params.offer - Offer carried by the payload item.
* @param params.ratifierData - ABI-encoded Ecrecover ratifier data.
* @returns EIP-712 digest signed by the maker or authorized signer.
* @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
* @throws {InvalidTreeHeightError} when the proof height exceeds 20.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
*
* const digest = EcrecoverRatifierUtils.digestRatifierData({
* chainId: 8453n,
* offer,
* ratifierData,
* });
* console.log(digest);
* ```
*/
function digestRatifierData(params) {
const decoded = decodeRatifierData(params.ratifierData);
return digestForRoot({
chainId: params.chainId,
offer: params.offer,
root: decoded.root,
proofLength: decoded.proof.length,
});
}
EcrecoverRatifierUtils.digestRatifierData = digestRatifierData;
/**
* Verifies an Ecrecover ratifier-data proof and recovers its signer.
*
* This helper intentionally does not check `Midnight.isAuthorized` or
* `EcrecoverRatifierUtils.isRootCanceled` state. Consumers must query both at the
* observed block before treating a recovered item as executable: the returned
* signer must be the maker or authorized by the maker, the offer ratifier must
* be authorized by the maker, and `isRootCanceled(maker, root)` must be false.
*
* @param params.chainId - Observed EIP-155 chain id from the log or execution context.
* @param params.offer - Offer carried by the payload item.
* @param params.ratifierData - ABI-encoded Ecrecover ratifier data.
* @returns Decoded ratifier data plus recovered signer.
* @throws {ChainIdMismatchError} when `params.chainId` does not match `offer.market.chainId`.
* @throws {InvalidEcrecoverSignatureVError} when the decoded signature `v` is not 27 or 28.
* @throws {InvalidEcrecoverSignatureError} when signature recovery fails, preserving the original error as `cause`.
* @throws {InvalidTreeError} when the proof does not include `offer` in `root`.
* @throws {InvalidTreeHeightError} when the proof height exceeds 20.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
*
* const verified = await EcrecoverRatifierUtils.verifyRatifierData({
* chainId: 8453n,
* offer,
* ratifierData,
* });
* console.log(verified.signer);
* ```
*/
async function verifyRatifierData(params) {
const offer = Offer.from(params.offer);
const decoded = decodeRatifierData(params.ratifierData);
if (!TreeUtils.verifyProof({
offer,
root: decoded.root,
leafIndex: decoded.leafIndex,
proof: decoded.proof,
})) {
throw new InvalidTreeError("Ratifier data proof does not include offer.");
}
const hash = digestForRoot({
chainId: params.chainId,
offer,
root: decoded.root,
proofLength: decoded.proof.length,
});
const yParity = toCanonicalYParity(decoded.signature.v);
let signer;
try {
signer = await recoverAddress({
hash,
signature: serializeSignature({
r: decoded.signature.r,
s: decoded.signature.s,
yParity,
}),
});
}
catch (cause) {
throw new InvalidEcrecoverSignatureError(cause);
}
return deepFreeze({
...decoded,
signer,
});
}
EcrecoverRatifierUtils.verifyRatifierData = verifyRatifierData;
/**
* Signs EcrecoverRatifier typed data through a viem client.
*
* Use when app code wants the signature separately from payload item
* construction. If you only need payload items, call `ratify` with the same
* client and account. The account may be the maker or an address authorized
* by every maker in the tree; the protocol checks that authorization onchain.
*
* @param params.tree - Ecrecover-ratified offer tree-like input to sign.
* @param params.client - Viem client whose transport signs the tree typed data.
* @param params.account - Account used to sign the tree typed data.
* @returns Signature returned by the client.
* @throws {InvalidTreeError} when the tree is invalid, contains multiple ratifiers, or spans multiple chain ids.
* @throws {InvalidTreeHeightError} when the tree height is unsupported.
* @throws {ChainIdMismatchError} when `params.client.chain?.id` does not match the tree offer chain id.
* @throws {InvalidTypedDataSignatureError} when the returned signature does not recover to `params.account`.
* @example
* ```ts
* import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
* import { Offer } from "@morpho-org/midnight-sdk";
* import { createWalletClient, http, zeroAddress } from "viem";
* import { base } from "viem/chains";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const client = createWalletClient({
* chain: base,
* transport: http(),
* });
*
* const signature = await EcrecoverRatifierUtils.sign({
* tree: Tree.create([offer]),
* client,
* account: offer.maker,
* });
* console.log(signature);
* ```
*/
async function sign(params) {
const { tree } = Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Ecrecover",
});
const expectedChainId = MarketParams.from(tree.offers[0].market).chainId;
for (const offer of tree.offers.slice(1)) {
const offerChainId = MarketParams.from(offer.market).chainId;
if (offerChainId !== expectedChainId) {
throw new InvalidTreeError(`All offers in an Ecrecover tree must use one chain id; expected "${expectedChainId}", got "${offerChainId}". Build separate trees per chain.`);
}
}
const clientChainId = params.client.chain?.id;
if (clientChainId == null || BigInt(clientChainId) !== expectedChainId) {
throw new ChainIdMismatchError(clientChainId, expectedChainId);
}
const signer = typeof params.account === "string"
? params.account
: params.account.address;
const data = typedData({
tree,
chainId: expectedChainId,
});
const signature = await signTypedData(params.client, {
account: params.account,
...data,
});
let isValidSignature = false;
try {
isValidSignature = await verifyHash({
address: signer,
hash: digest({ tree, chainId: expectedChainId }),
signature,
});
}
catch (cause) {
throw new InvalidTypedDataSignatureError(signer, cause);
}
if (!isValidSignature) {
throw new InvalidTypedDataSignatureError(signer);
}
return signature;
}
EcrecoverRatifierUtils.sign = sign;
/**
* Converts a hex ECDSA signature into the Solidity tuple shape.
*
* @param signature - Hex string or tuple signature.
* @returns Signature tuple.
* @throws {InvalidEcrecoverSignatureError} when the serialized signature cannot be parsed, preserving the original error as `cause`.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
* import { zeroHash } from "viem";
*
* const signature = EcrecoverRatifierUtils.toSignature({
* v: 27,
* r: zeroHash,
* s: zeroHash,
* });
* console.log(signature.v);
* ```
*/
function toSignature(signature) {
if (typeof signature !== "string") {
const v = signature.v != null ? Number(signature.v) : Number(signature.yParity);
return {
v: v < 27 ? v + 27 : v,
r: signature.r,
s: signature.s,
};
}
let parsed;
try {
parsed = parseSignature(signature);
}
catch (cause) {
throw new InvalidEcrecoverSignatureError(cause);
}
const v = Number(parsed.v ?? BigInt(parsed.yParity));
return {
v: v < 27 ? v + 27 : v,
r: parsed.r,
s: parsed.s,
};
}
EcrecoverRatifierUtils.toSignature = toSignature;
/**
* Encodes EcrecoverRatifier ratifier data.
*
* Use only when you already have a signature and proof. Most maker flows call
* `ratifierData` for one leaf or `ratify` for every leaf in the tree.
*
* @param params.signature - Signature tuple encoded into the ratifier data.
* @param params.root - Merkle root approved by the signature.
* @param params.leafIndex - Leaf index proven by `params.proof`.
* @param params.proof - Merkle proof siblings for the leaf.
* @returns ABI-encoded ratifier data.
* @throws {InvalidEcrecoverSignatureError} when signature parsing fails, preserving the original error as `cause`.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
*
* const data = EcrecoverRatifierUtils.encodeRatifierData({
* signature: {
* v: 27,
* r: "0x0000000000000000000000000000000000000000000000000000000000000000",
* s: "0x0000000000000000000000000000000000000000000000000000000000000000",
* },
* root: "0x0000000000000000000000000000000000000000000000000000000000000000",
* leafIndex: 0n,
* proof: [],
* });
* console.log(data);
* ```
*/
function encodeRatifierData(params) {
const signature = toSignature(params.signature);
return encodeAbiParameters(signatureAbi, [
{
v: signature.v,
r: signature.r,
s: signature.s,
},
params.root,
BigInt(params.leafIndex),
params.proof,
]);
}
EcrecoverRatifierUtils.encodeRatifierData = encodeRatifierData;
/**
* Decodes EcrecoverRatifier ratifier data.
*
* Use on the take-side or in tests after `Payload.decode` to inspect the
* proof/signature attached to a published offer.
*
* @param data - ABI-encoded ratifier data.
* @returns Decoded Ecrecover ratifier data.
* @example
* ```ts
* import { EcrecoverRatifierUtils } from "@morpho-org/midnight-sdk";
* import { zeroHash } from "viem";
*
* const data = EcrecoverRatifierUtils.encodeRatifierData({
* signature: { v: 27, r: zeroHash, s: zeroHash },
* root: zeroHash,
* leafIndex: 0n,
* proof: [],
* });
* const decoded = EcrecoverRatifierUtils.decodeRatifierData(data);
* console.log(decoded.leafIndex);
* ```
*/
function decodeRatifierData(data) {
const [signature, root, leafIndex, proof] = decodeAbiParameters(signatureAbi, data);
return deepFreeze({
signature: {
v: signature.v,
r: signature.r,
s: signature.s,
},
root,
leafIndex,
proof: [...proof],
});
}
EcrecoverRatifierUtils.decodeRatifierData = decodeRatifierData;
/**
* Builds one ratifier-data value for a tree leaf.
*
* Use after a tree has been signed when a caller needs data for one offer
* leaf. Use `ratify` to produce payload-ready items for the whole tree.
*
* @param params.tree - Ecrecover-ratified offer tree-like input that produced the proof.
* @param params.leafIndex - Leaf index to prove.
* @param params.signature - Ecrecover signature for the tree root.
* @returns ABI-encoded EcrecoverRatifier data.
* @throws {InvalidTreeError} when the leaf index is outside the tree or the tree contains multiple ratifiers.
* @throws {InvalidEcrecoverSignatureError} when the supplied serialized signature cannot be parsed.
* @example
* ```ts
* import { EcrecoverRatifierUtils, Offer, Tree } from "@morpho-org/midnight-sdk";
* import { zeroAddress, zeroHash } from "viem";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const data = EcrecoverRatifierUtils.ratifierData({
* tree: Tree.create([offer]),
* leafIndex: 0n,
* signature: { v: 27, r: zeroHash, s: zeroHash },
* });
* console.log(data);
* ```
*/
function ratifierData(params) {
const { tree } = Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Ecrecover",
});
const proof = TreeUtils.buildProof({ tree, leafIndex: params.leafIndex });
return encodeRatifierData({
signature: toSignature(params.signature),
root: proof.root,
leafIndex: proof.leafIndex,
proof: proof.proof,
});
}
EcrecoverRatifierUtils.ratifierData = ratifierData;
/**
* Signs or consumes a tree signature and returns payload-ready items.
*
* Use after `Tree.mempoolValidate` and before `Payload.encode`.
* The returned items preserve tree leaf order and include ratifier data
* required by takers. The group id is stored on each inline offer.
*
* @param params.tree - Ecrecover-ratified offer tree-like input to ratify.
* @param params.client - Optional viem client whose transport signs typed data built from `params.tree`.
* @param params.account - Optional account used to sign typed data built from `params.tree`.
* @param params.signature - Optional precomputed signature for `params.tree`.
* @returns Items containing each offer and its ratifier data.
* @throws {InvalidTreeError} when the tree is invalid or contains multiple ratifiers.
* @throws {InvalidTreeHeightError} when the tree height is unsupported.
* @throws {InvalidTypedDataSignatureError} when verification fails after signing or after parsing a precomputed signature, including a signer mismatch.
* @throws {InvalidEcrecoverSignatureError} when a precomputed serialized signature cannot be parsed.
* @example
* ```ts
* import { EcrecoverRatifierUtils, Tree } from "@morpho-org/midnight-sdk";
* import { Offer } from "@morpho-org/midnight-sdk";
* import { createWalletClient, http, zeroAddress } from "viem";
* import { base } from "viem/chains";
*
* const offer = Offer.create({
* market: {
* chainId: 8453,
* midnight: "0x0000000000000000000000000000000000001000",
* loanToken: "0x0000000000000000000000000000000000006000",
* collateralParams: [
* {
* token: "0x0000000000000000000000000000000000007000",
* lltv: 770000000000000000n,
* liquidationCursor: 250000000000000000n,
* oracle: "0x0000000000000000000000000000000000008000",
* },
* ],
* maturity: 54_000n,
* rcfThreshold: 0n,
* enterGate: zeroAddress,
* liquidatorGate: zeroAddress,
* },
* buy: true,
* maker: "0x0000000000000000000000000000000000009000",
* tick: 5_000n,
* expiry: 3_600n,
* ratifier: "0x0000000000000000000000000000000000004000",
* maxUnits: 100n,
* });
* const client = createWalletClient({
* chain: base,
* transport: http(),
* });
*
* const items = await EcrecoverRatifierUtils.ratify({
* tree: Tree.create([offer]),
* client,
* account: offer.maker,
* });
* console.log(items.length);
* ```
*/
async function ratify(params) {
const { tree } = Ratifier.normalizeRatifierTree({
tree: params.tree,
label: "Ecrecover",
});
let signature;
if (params.signature != null) {
signature = toSignature(params.signature);
const expectedChainId = MarketParams.from(tree.offers[0].market).chainId;
for (const offer of tree.offers.slice(1)) {
const offerChainId = MarketParams.from(offer.market).chainId;
if (offerChainId !== expectedChainId) {
throw new InvalidTreeError(`All offers in an Ecrecover tree must use one chain id; expected "${expectedChainId}", got "${offerChainId}". Build separate trees per chain.`);
}
}
const signer = typeof params.account === "string"
? params.account
: params.account.address;
let isValidSignature = false;
try {
isValidSignature = await verifyHash({
address: signer,
hash: digest({ tree, chainId: expectedChainId }),
signature: serializeSignature({
r: signature.r,
s: signature.s,
yParity: signature.v - 27,
}),
});
}
catch (cause) {
throw new InvalidTypedDataSignatureError(signer, cause);
}
if (!isValidSignature) {
throw new InvalidTypedDataSignatureError(signer);
}
}
else {
signature = toSignature(await sign({
tree,
client: params.client,
account: params.account,
}));
}
const proofs = TreeUtils.buildProofs({
tree,
count: tree.offers.length,
});
return tree.offers.map((offer, leafIndex) => ({
offer,
ratifierData: encodeRatifierData({
signature,
...proofs[leafIndex],
}),
}));
}
EcrecoverRatifierUtils.ratify = ratify;
})(EcrecoverRatifierUtils || (EcrecoverRatifierUtils = {}));
/**
* Tagged-tree API for EcrecoverRatifier with validated portable leaf commitments.
* Legacy untagged inputs remain available through {@link EcrecoverRatifierUtils}.
*/
export var EcrecoverRatifier;
(function (EcrecoverRatifier) {
/**
* Builds Ecrecover signing data from a tagged tree.
* @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
* @param request.chainId - Chain defining the EIP-712 domain.
* @returns EIP-712 domain, type definitions, and offer-tree message.
* @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
* @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
* @example
* ```ts
* import { EcrecoverRatifier, type EcrecoverRatifierTypedDataRequest } from "@morpho-org/midnight-sdk";
* function execute(request: EcrecoverRatifierTypedDataRequest) {
* return EcrecoverRatifier.typedData(request);
* }
* ```
*/
function typedData(request) {
const tree = parseStandardRatifierTree(request.tree, "ecrecover");
return EcrecoverRatifierUtils.typedData({ ...request, tree });
}
EcrecoverRatifier.typedData = typedData;
/**
* Computes the signing digest of a tagged Ecrecover tree.
* @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
* @param request.chainId - Chain defining the EIP-712 domain.
* @returns The EIP-712 signing digest.
* @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
* @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
* @example
* ```ts
* import { EcrecoverRatifier, type EcrecoverRatifierTypedDataRequest } from "@morpho-org/midnight-sdk";
* function execute(request: EcrecoverRatifierTypedDataRequest) {
* return EcrecoverRatifier.digest(request);
* }
* ```
*/
function digest(request) {
const tree = parseStandardRatifierTree(request.tree, "ecrecover");
return EcrecoverRatifierUtils.digest({ ...request, tree });
}
EcrecoverRatifier.digest = digest;
/**
* Signs a tagged Ecrecover tree using the supplied client and account.
* @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
* @param request.account - Maker or authorized signer.
* @param request.client - Signing client; omit when ratify receives a precomputed signature.
* @returns A verified hex signature over the tree.
* @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
* @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
* @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
* @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
* @example
* ```ts
* import { EcrecoverRatifier, type EcrecoverRatifierSignRequest } from "@morpho-org/midnight-sdk";
* function execute(request: EcrecoverRatifierSignRequest) {
* return EcrecoverRatifier.sign(request);
* }
* ```
*/
async function sign(request) {
const tree = parseStandardRatifierTree(request.tree, "ecrecover");
return EcrecoverRatifierUtils.sign({ ...request, tree });
}
EcrecoverRatifier.sign = sign;
/**
* Encodes the signature and proof for one tagged Ecrecover leaf.
* @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
* @param request.signature - Tree signature; ratify may instead request one through the supplied client.
* @param request.leafIndex - Leaf to prove.
* @returns ABI-encoded ratifier data for the requested leaf.
* @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
* @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
* @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
* @throws {InvalidEcrecoverSignatureError} When a supplied serialized signature cannot be parsed.
* @example
* ```ts
* import { EcrecoverRatifier, type EcrecoverRatifierDataRequest } from "@morpho-org/midnight-sdk";
* function execute(request: EcrecoverRatifierDataRequest) {
* return EcrecoverRatifier.ratifierData(request);
* }
* ```
*/
function ratifierData(request) {
const tree = parseStandardRatifierTree(request.tree, "ecrecover");
return EcrecoverRatifierUtils.ratifierData({ ...request, tree });
}
EcrecoverRatifier.ratifierData = ratifierData;
/**
* Builds payload items from a tagged tree and a verified signature.
* @param request.tree - Matching tagged tree or portable snapshot; commitments are validated before use.
* @param request.account - Maker or authorized signer.
* @param request.client - Signing client; omit when ratify receives a precomputed signature.
* @param request.signature - Tree signature; ratify may instead request one through the supplied client.
* @returns One payload item per non-padding offer, including its ratifier data.
* @throws {InvalidTreeError} When the tag, commitments, or root are invalid.
* @throws {InvalidTreeHeightError} When the descriptor height is unsupported.
* @throws {ChainIdMismatchError} When the signing client's chain differs from the offers.
* @throws {InvalidTypedDataSignatureError} When the signature does not recover to the supplied account.
* @throws {InvalidEcrecoverSignatureVError} When a supplied signature has an unsupported recovery value.
* @throws {InvalidEcrecoverSignatureError} When a precomputed serialized signature cannot be parsed.
* @example
* ```ts
* import { EcrecoverRatifier, type EcrecoverRatifierRatifyRequest } from "@morpho-org/midnight-sdk";
* function execute(request: EcrecoverRatifierRatifyRequest) {
* return EcrecoverRatifier.ratify(request);
* }
* ```
*/
async function ratify(request) {
const tree = parseStandardRatifierTree(request.tree, "ecrecover");
return EcrecoverRatifierUtils.ratify({ ...request, tree });
}
EcrecoverRatifier.ratify = ratify;
/** {@inheritDoc EcrecoverRatifierUtils.treeTypeHash} */
EcrecoverRatifier.treeTypeHash = EcrecoverRatifierUtils.treeTypeHash;
/** {@inheritDoc EcrecoverRatifierUtils.digestForRoot} */
EcrecoverRatifier.digestForRoot = EcrecoverRatifierUtils.digestForRoot;
/** {@inheritDoc EcrecoverRatifierUtils.digestRatifierData} */
EcrecoverRatifier.digestRatifierData = EcrecoverRatifierUtils.digestRatifierData;
/** {@inheritDoc EcrecoverRatifierUtils.verifyRatifierData} */
EcrecoverRatifier.verifyRatifierData = EcrecoverRatifierUtils.verifyRatifierData;
/** {@inheritDoc EcrecoverRatifierUtils.toSignature} */
EcrecoverRatifier.toSignature = EcrecoverRatifierUtils.toSignature;
/** {@inheritDoc EcrecoverRatifierUtils.encodeRatifierData} */
EcrecoverRatifier.encodeRatifierData = EcrecoverRatifierUtils.encodeRatifierData;
/** {@inheritDoc EcrecoverRatifierUtils.decodeRatifierData} */
EcrecoverRatifier.decodeRatifierData = EcrecoverRatifierUtils.decodeRatifierData;
})(EcrecoverRatifier || (EcrecoverRatifier = {}));