-
Notifications
You must be signed in to change notification settings - Fork 403
/
mailboxes.ts
176 lines (166 loc) · 4.12 KB
/
mailboxes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { expect } from 'chai';
import { ethers } from 'ethers';
import {
Address,
HexString,
MerkleProof,
Validator,
addressToBytes32,
ensure0x,
formatLegacyMultisigIsmMetadata,
formatMessage,
messageId,
parseMessage,
} from '@hyperlane-xyz/utils';
import {
LegacyMultisigIsm,
TestMailbox,
TestMerkleTreeHook,
} from '../../types';
import { DispatchEvent } from '../../types/contracts/Mailbox';
export type MessageAndProof = {
proof: MerkleProof;
message: string;
};
export type MessageAndMetadata = {
message: string;
metadata: string;
};
export const dispatchMessage = async (
mailbox: TestMailbox,
destination: number,
recipient: string,
messageStr: string,
utf8 = true,
) => {
const tx = await mailbox['dispatch(uint32,bytes32,bytes)'](
destination,
recipient,
utf8 ? ethers.utils.toUtf8Bytes(messageStr) : 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 (
mailbox: TestMailbox,
merkleHook: TestMerkleTreeHook,
destination: number,
recipient: string,
messageStr: string,
utf8 = true,
): Promise<MessageAndProof> => {
const nonce = await mailbox.nonce();
const { message } = await dispatchMessage(
mailbox,
destination,
addressToBytes32(recipient),
messageStr,
utf8,
);
const mid = messageId(message);
const proof = await merkleHook.proof();
return {
proof: {
branch: proof,
leaf: mid,
index: nonce,
},
message,
};
};
// Signs a checkpoint with the provided validators and returns
// the signatures ordered by validator index
export async function signCheckpoint(
root: HexString,
index: number,
mailbox: Address,
orderedValidators: Validator[],
): Promise<string[]> {
const signedCheckpoints = await Promise.all(
orderedValidators.map((validator) => validator.signCheckpoint(root, index)),
);
return signedCheckpoints.map(
(signedCheckpoint) => signedCheckpoint.signature as string, // cast is safe because signCheckpoint serializes to hex
);
}
export async function dispatchMessageAndReturnMetadata(
mailbox: TestMailbox,
merkleHook: TestMerkleTreeHook,
multisigIsm: LegacyMultisigIsm,
destination: number,
recipient: string,
messageStr: string,
orderedValidators: Validator[],
threshold?: number,
utf8 = true,
): Promise<MessageAndMetadata> {
// Checkpoint indices are 0 indexed, so we pull the count before
// we dispatch the message.
const index = await mailbox.nonce();
const proofAndMessage = await dispatchMessageAndReturnProof(
mailbox,
merkleHook,
destination,
recipient,
messageStr,
utf8,
);
const root = await merkleHook.root();
const signatures = await signCheckpoint(
root,
index,
mailbox.address,
orderedValidators,
);
const origin = parseMessage(proofAndMessage.message).origin;
const metadata = formatLegacyMultisigIsmMetadata({
checkpointRoot: root,
checkpointIndex: index,
originMailbox: mailbox.address,
proof: proofAndMessage.proof.branch,
signatures,
validators: await multisigIsm.validators(origin),
});
return { metadata, message: proofAndMessage.message };
}
export function getCommitment(
threshold: number,
validators: Address[],
): string {
const packed = ethers.utils.solidityPack(
['uint8', 'address[]'],
[threshold, validators],
);
return ethers.utils.solidityKeccak256(['bytes'], [packed]);
}
export const inferMessageValues = async (
mailbox: TestMailbox,
sender: string,
destination: number,
recipient: string,
messageStr: string,
version?: number,
) => {
const body = ensure0x(
Buffer.from(ethers.utils.toUtf8Bytes(messageStr)).toString('hex'),
);
const nonce = await mailbox.nonce();
const localDomain = await mailbox.localDomain();
const message = formatMessage(
version ?? (await mailbox.VERSION()),
nonce,
localDomain,
sender,
destination,
recipient,
body,
);
const id = messageId(message);
return {
message,
id,
body,
};
};