|
| 1 | +import { toChain } from "@wormhole-foundation/sdk-base"; |
| 2 | +import { |
| 3 | + createVAA, |
| 4 | + serialize, |
| 5 | + UniversalAddress, |
| 6 | + type VAA, |
| 7 | +} from "@wormhole-foundation/sdk-definitions"; |
| 8 | +import { mocks } from "@wormhole-foundation/sdk-definitions/testing"; |
| 9 | +import { |
| 10 | + createPublicClient, |
| 11 | + getContract, |
| 12 | + http, |
| 13 | + isAddressEqual, |
| 14 | + padHex, |
| 15 | + parseEventLogs, |
| 16 | + toBytes, |
| 17 | + type Hex, |
| 18 | +} from "viem"; |
| 19 | +import { anvil } from "viem/chains"; |
| 20 | +import { CORE_ABI } from "./abis/core"; |
| 21 | +import { EVM_PRIVATE_KEY } from "./consts"; |
| 22 | + |
| 23 | +async function getWormholeMessage( |
| 24 | + rpc: string, |
| 25 | + txHash: Hex, |
| 26 | + coreContractAddress: Hex |
| 27 | +): Promise<VAA<"Uint8Array"> | undefined> { |
| 28 | + console.log(`Mocking guardian signatures for ${rpc} ${txHash}`); |
| 29 | + const transport = http(rpc); |
| 30 | + const client = createPublicClient({ |
| 31 | + chain: anvil, |
| 32 | + transport, |
| 33 | + }); |
| 34 | + const transaction = await client.getTransactionReceipt({ |
| 35 | + hash: txHash, |
| 36 | + }); |
| 37 | + const coreContract = getContract({ |
| 38 | + address: coreContractAddress, |
| 39 | + abi: CORE_ABI, |
| 40 | + client, |
| 41 | + }); |
| 42 | + const chainId = await coreContract.read.chainId(); |
| 43 | + const guardianSetIndex = await coreContract.read.getCurrentGuardianSetIndex(); |
| 44 | + const topics = parseEventLogs({ |
| 45 | + eventName: "LogMessagePublished", |
| 46 | + abi: CORE_ABI, |
| 47 | + logs: transaction.logs, |
| 48 | + }); |
| 49 | + for (const topic of topics) { |
| 50 | + if ( |
| 51 | + topic.removed === false && |
| 52 | + isAddressEqual(topic.address, coreContractAddress) |
| 53 | + ) { |
| 54 | + const emitter = topic.args.sender; |
| 55 | + return createVAA("Uint8Array", { |
| 56 | + guardianSet: guardianSetIndex, |
| 57 | + timestamp: Number( |
| 58 | + ( |
| 59 | + await client.getBlock({ |
| 60 | + blockHash: transaction.blockHash, |
| 61 | + includeTransactions: false, |
| 62 | + }) |
| 63 | + ).timestamp |
| 64 | + ), |
| 65 | + // NOTE: the Wormhole SDK requires this be a known chain, though that is not strictly necessary for our use case. |
| 66 | + emitterChain: toChain(chainId), |
| 67 | + emitterAddress: new UniversalAddress( |
| 68 | + toBytes(padHex(emitter, { dir: "left", size: 32 })) |
| 69 | + ), |
| 70 | + consistencyLevel: topic.args.consistencyLevel, |
| 71 | + sequence: topic.args.sequence, |
| 72 | + nonce: topic.args.nonce, |
| 73 | + signatures: [], |
| 74 | + payload: toBytes(topic.args.payload), |
| 75 | + }); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * returns a base64 string like a guardian /v1/signed_vaa/ |
| 82 | + */ |
| 83 | +export async function mockWormhole( |
| 84 | + rpc: string, |
| 85 | + txHash: Hex, |
| 86 | + coreContractAddress: Hex |
| 87 | +): Promise<string> { |
| 88 | + const vaa = await getWormholeMessage(rpc, txHash, coreContractAddress); |
| 89 | + if (vaa) { |
| 90 | + const guardianSet = new mocks.MockGuardians(0, [EVM_PRIVATE_KEY]); |
| 91 | + const signedVaa = guardianSet.addSignatures(vaa); |
| 92 | + const base64 = Buffer.from(serialize(signedVaa)).toString("base64"); |
| 93 | + return base64; |
| 94 | + } |
| 95 | + return ""; |
| 96 | +} |
0 commit comments