-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathsend-attest.ts
More file actions
221 lines (189 loc) · 6.24 KB
/
send-attest.ts
File metadata and controls
221 lines (189 loc) · 6.24 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import { config as dotenvConfig } from "dotenv";
import {
ChainSlug,
DeploymentAddresses,
IntegrationTypes,
getAllAddresses,
} from "../../src";
import { mode, overrides } from "../deploy/config/config";
import FastSwitchboardArtifact from "../../out/FastSwitchboard.sol/FastSwitchboard.json";
import { getProviderFromChainSlug } from "../constants";
import { ethers, Wallet } from "ethers";
import { defaultAbiCoder, keccak256 } from "ethers/lib/utils";
import { getAwsKmsSigner } from "@socket.tech/dl-common";
dotenvConfig();
/**
* Usage
*
* --source Specify the source chain slug.
* This flag is required.
* Eg. npx --source=1 --destination=10 --packetid=0x... --proposalcount=1 --root=0x... --kmskeyid=abc-123 ts-node scripts/admin/send-attest.ts
*
* --destination Specify the destination chain slug.
* This flag is required.
*
* --packetid Specify the packet ID.
* This flag is required.
*
* --proposalcount Specify the proposal count.
* This flag is required.
*
* --root Specify the root hash.
* This flag is required.
*
* --kmskeyid Specify the AWS KMS key ID.
* This flag is required.
*
* --sendtx Send attest tx along with signature generation.
* Default is only generate and display signature.
* Eg. npx --source=1 --destination=10 --packetid=0x... --proposalcount=1 --root=0x... --kmskeyid=abc-123 --sendtx ts-node scripts/admin/send-attest.ts
*/
const sourceChainSlug = process.env.npm_config_source;
if (!sourceChainSlug) {
console.error("Error: source flag is required");
process.exit(1);
}
const destinationChainSlug = process.env.npm_config_destination;
if (!destinationChainSlug) {
console.error("Error: destination flag is required");
process.exit(1);
}
const packetId = process.env.npm_config_packetid;
if (!packetId) {
console.error("Error: packetid flag is required");
process.exit(1);
}
const proposalCount = process.env.npm_config_proposalcount;
if (!proposalCount) {
console.error("Error: proposalcount flag is required");
process.exit(1);
}
const root = process.env.npm_config_root;
if (!root) {
console.error("Error: root flag is required");
process.exit(1);
}
const kmsKeyId = process.env.npm_config_kmskeyid;
if (!kmsKeyId) {
console.error("Error: kmskeyid flag is required");
process.exit(1);
}
const sendTx = process.env.npm_config_sendtx === "true";
const signerKey = process.env.SOCKET_SIGNER_KEY;
if (sendTx && !signerKey) {
console.error("Error: SOCKET_SIGNER_KEY is required when sending tx");
process.exit(1);
}
const addresses: DeploymentAddresses = getAllAddresses(mode);
export const main = async () => {
const sourceChain = sourceChainSlug;
const destinationChain = destinationChainSlug;
console.log(
`\nProcessing attest for path: ${sourceChain} -> ${destinationChain}\n`
);
// Get addresses from prod_addresses.json
const destinationAddresses = addresses[destinationChain];
if (!destinationAddresses) {
console.error(
`Error: No addresses found for destination chain ${destinationChain}`
);
process.exit(1);
}
const integration = destinationAddresses.integrations?.[sourceChain];
if (!integration) {
console.error(
`Error: No integration found for ${destinationChain} -> ${sourceChain}`
);
process.exit(1);
}
// Get FAST integration switchboard
const fastIntegration = integration[IntegrationTypes.fast];
if (!fastIntegration) {
console.error(
`Error: No FAST integration found for ${destinationChain} -> ${sourceChain}`
);
process.exit(1);
}
const switchboardAddress = fastIntegration.switchboard;
console.log("Addresses:");
console.log(` Switchboard: ${switchboardAddress}\n`);
// Get provider
const provider = getProviderFromChainSlug(
parseInt(destinationChain) as ChainSlug
);
// Get AWS KMS signer
console.log("Getting AWS KMS signer...");
const kmsSigner = (await getAwsKmsSigner(kmsKeyId)).connect(provider);
const kmsAddress = await kmsSigner.getAddress();
console.log(`KMS Address: ${kmsAddress}\n`);
// Prepare message hash for signing
const messageHash = keccak256(
defaultAbiCoder.encode(
["address", "uint32", "bytes32", "uint256", "bytes32"],
[
switchboardAddress,
parseInt(destinationChain),
packetId,
proposalCount,
root,
]
)
);
console.log("Message hash:", messageHash);
// Sign with KMS
console.log("\nSigning with AWS KMS...");
const signature = await kmsSigner.signMessage(
ethers.utils.arrayify(messageHash)
);
console.log("Signature:", signature);
// Prepare transaction data
const switchboardInterface = new ethers.utils.Interface(
FastSwitchboardArtifact.abi
);
const calldata = switchboardInterface.encodeFunctionData("attest", [
packetId,
proposalCount,
root,
signature,
]);
console.log("\n=== Transaction Details ===");
console.log(`Chain ID: ${(await provider.getNetwork()).chainId}`);
console.log(`Target: ${switchboardAddress}`);
console.log(`Value: 0`);
console.log(`Calldata: ${calldata}`);
console.log("===========================\n");
if (sendTx) {
console.log("Sending attest transaction...");
const wallet = new Wallet(signerKey!, provider);
const switchboardContract = new ethers.Contract(
switchboardAddress,
FastSwitchboardArtifact.abi,
wallet
);
const tx = await switchboardContract.attest(
packetId,
proposalCount,
root,
signature,
{
...(await overrides(parseInt(destinationChain))),
}
);
console.log("Transaction hash:", tx.hash);
const receipt = await tx.wait();
console.log("Transaction confirmed in block:", receipt.blockNumber);
console.log("Gas used:", receipt.gasUsed.toString());
} else {
console.log("To send the attest transaction, add --sendtx flag");
console.log(
"You can use the transaction details above to manually send, simulate, or audit the transaction."
);
}
console.log("\nScript completed.");
};
main()
.then(() => process.exit(0))
.catch((error: Error) => {
console.error(error);
process.exit(1);
});