|
| 1 | +import { expect } from 'chai'; |
| 2 | +import hre from 'hardhat'; |
| 3 | +import sinon from 'sinon'; |
| 4 | + |
| 5 | +import { TestCcipReadIsm__factory } from '@hyperlane-xyz/core'; |
| 6 | +import { WithAddress } from '@hyperlane-xyz/utils'; |
| 7 | + |
| 8 | +import { HyperlaneCore } from '../../core/HyperlaneCore.js'; |
| 9 | +import { TestCoreDeployer } from '../../core/TestCoreDeployer.js'; |
| 10 | +import { TestRecipientDeployer } from '../../core/TestRecipientDeployer.js'; |
| 11 | +import { HyperlaneProxyFactoryDeployer } from '../../deploy/HyperlaneProxyFactoryDeployer.js'; |
| 12 | +import { MultiProvider } from '../../providers/MultiProvider.js'; |
| 13 | +import { EvmIsmReader } from '../EvmIsmReader.js'; |
| 14 | +import { HyperlaneIsmFactory } from '../HyperlaneIsmFactory.js'; |
| 15 | +import { CCIPReadIsmConfig } from '../types.js'; |
| 16 | + |
| 17 | +import { BaseMetadataBuilder } from './builder.js'; |
| 18 | +import type { MetadataContext } from './types.js'; |
| 19 | + |
| 20 | +describe('CCIP-Read ISM Integration', () => { |
| 21 | + let core: HyperlaneCore; |
| 22 | + let multiProvider: MultiProvider; |
| 23 | + let testRecipient: any; |
| 24 | + let ccipReadIsm: any; |
| 25 | + let metadataBuilder: BaseMetadataBuilder; |
| 26 | + let ismFactory: HyperlaneIsmFactory; |
| 27 | + let fetchStub: sinon.SinonStub; |
| 28 | + |
| 29 | + before(async () => { |
| 30 | + // Set up a local test multi-provider and Hyperlane core |
| 31 | + const signers = await hre.ethers.getSigners(); |
| 32 | + multiProvider = MultiProvider.createTestMultiProvider({ |
| 33 | + signer: signers[0], |
| 34 | + }); |
| 35 | + const ismFactoryDeployer = new HyperlaneProxyFactoryDeployer(multiProvider); |
| 36 | + const contractsMap = await ismFactoryDeployer.deploy( |
| 37 | + multiProvider.mapKnownChains(() => ({})), |
| 38 | + ); |
| 39 | + ismFactory = new HyperlaneIsmFactory(contractsMap, multiProvider); |
| 40 | + core = await new TestCoreDeployer(multiProvider, ismFactory).deployApp(); |
| 41 | + |
| 42 | + // Deploy a TestRecipient on test1 |
| 43 | + const deployments = await new TestRecipientDeployer(multiProvider).deploy({ |
| 44 | + test2: {}, |
| 45 | + }); |
| 46 | + testRecipient = (deployments.test2 as any).testRecipient; |
| 47 | + |
| 48 | + // Deploy the TestCcipReadIsm on test1 domain |
| 49 | + const domain = multiProvider.getDomainId('test1'); |
| 50 | + ccipReadIsm = await multiProvider.handleDeploy( |
| 51 | + domain, |
| 52 | + new TestCcipReadIsm__factory(), |
| 53 | + // Pass in desired offchain URLs for the ISM constructor: |
| 54 | + [['http://example.com/{data}']], |
| 55 | + ); |
| 56 | + |
| 57 | + // Configure the TestRecipient to use the CCIP-Read ISM |
| 58 | + await testRecipient.setInterchainSecurityModule(ccipReadIsm.address); |
| 59 | + |
| 60 | + // Prepare the metadata builder |
| 61 | + metadataBuilder = new BaseMetadataBuilder(core); |
| 62 | + |
| 63 | + fetchStub = sinon.stub(global, 'fetch').resolves({ |
| 64 | + ok: true, |
| 65 | + json: async () => ({ |
| 66 | + data: '0x0000000000000000000000000000000000000000000000000000000000000001', |
| 67 | + }), |
| 68 | + } as Response); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should process a message protected by CCIP-Read ISM', async () => { |
| 72 | + // Send a message from test1 to test2 |
| 73 | + const { dispatchTx, message } = await core.sendMessage( |
| 74 | + 'test1', |
| 75 | + 'test2', |
| 76 | + testRecipient.address, |
| 77 | + '0x1234', |
| 78 | + ); |
| 79 | + |
| 80 | + // Derive the on-chain ISM config for CCIP-Read |
| 81 | + const derivedIsm = (await new EvmIsmReader( |
| 82 | + multiProvider, |
| 83 | + 'test2', |
| 84 | + ).deriveIsmConfig(ccipReadIsm.address)) as WithAddress<CCIPReadIsmConfig>; |
| 85 | + |
| 86 | + // Build the metadata using the CCIP-Read builder |
| 87 | + const context: MetadataContext<WithAddress<CCIPReadIsmConfig>> = { |
| 88 | + ism: derivedIsm, |
| 89 | + message, |
| 90 | + dispatchTx, |
| 91 | + hook: {} as any, |
| 92 | + }; |
| 93 | + const metadata = await metadataBuilder.build(context); |
| 94 | + |
| 95 | + // Finally, call mailbox.process on test2 with the metadata and message |
| 96 | + const mailbox = core.getContracts('test2').mailbox; |
| 97 | + await expect(mailbox.process(metadata, message.message)).to.not.be.reverted; |
| 98 | + }); |
| 99 | + |
| 100 | + after(() => { |
| 101 | + fetchStub.restore(); |
| 102 | + }); |
| 103 | +}); |
0 commit comments