The home for Hyperlane core contracts, sdk packages, and other infrastructure
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hyperlane-monorepo/solidity/test/lib/mailboxes.ts

88 lines
1.9 KiB

import { expect } from 'chai';
import { ethers } from 'ethers';
import { utils } from '@hyperlane-xyz/utils';
import { MailboxV2, TestOutbox } from '../../types';
import { DispatchEvent } from '../../types/contracts/Outbox';
export const dispatchMessage = async (
outbox: TestOutbox,
destination: number,
recipient: string,
messageStr: string,
) => {
const tx = await outbox.dispatch(
destination,
recipient,
ethers.utils.toUtf8Bytes(messageStr),
);
const receipt = await tx.wait();
const dispatch = receipt.events![0] as DispatchEvent;
expect(dispatch.event).to.equal('Dispatch');
return dispatch.args!;
};
export const dispatchMessageAndReturnProof = async (
outbox: TestOutbox,
destination: number,
recipient: string,
messageStr: string,
): Promise<MerkleProof> => {
const { leafIndex, message } = await dispatchMessage(
outbox,
destination,
recipient,
messageStr,
);
const index = leafIndex.toNumber();
const messageHash = utils.messageHash(message, index);
const root = await outbox.root();
const proof = await outbox.proof();
return {
root,
proof: proof,
leaf: messageHash,
index,
message,
};
};
export interface MerkleProof {
root: string;
proof: string[];
leaf: string;
index: number;
message: string;
}
export const inferMessageValues = async (
mailbox: MailboxV2,
sender: string,
destination: number,
recipient: string,
messageStr: string,
version?: number,
) => {
const body = utils.ensure0x(
Buffer.from(ethers.utils.toUtf8Bytes(messageStr)).toString('hex'),
);
const nonce = await mailbox.count();
const localDomain = await mailbox.localDomain();
const message = utils.formatMessageV2(
version ?? (await mailbox.VERSION()),
nonce,
localDomain,
sender,
destination,
recipient,
body,
);
const id = utils.messageIdV2(message);
return {
message,
id,
body,
};
};