@@ -4,8 +4,29 @@ const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
4
4
5
5
const AxelarHelper = require ( './axelar/AxelarHelper' ) ;
6
6
7
- const keccak256AbiEncode = ( types , values ) => ethers . keccak256 ( ethers . AbiCoder . defaultAbiCoder ( ) . encode ( types , values ) ) ;
8
- const sortBytes32 = values => values . sort ( ( a , b ) => ( BigInt ( a ) < BigInt ( b ) ? - 1 : 1 ) ) ;
7
+ const buildBridgeHash = ( ...chains ) => {
8
+ const cmp = ( a , b ) => ( BigInt ( a ) < BigInt ( b ) ? - 1 : 1 ) ;
9
+ const chainIds = chains . map ( ( { token, flags = ethers . ZeroHash , links } ) =>
10
+ ethers . keccak256 (
11
+ ethers . AbiCoder . defaultAbiCoder ( ) . encode (
12
+ [ 'bytes' , 'bytes32' , 'bytes32[]' ] ,
13
+ [
14
+ token ,
15
+ flags ,
16
+ links
17
+ . map ( ( { gateway, remote } ) =>
18
+ ethers . keccak256 ( ethers . AbiCoder . defaultAbiCoder ( ) . encode ( [ 'bytes' , 'bytes' ] , [ gateway , remote ] ) ) ,
19
+ )
20
+ . sort ( cmp ) ,
21
+ ] ,
22
+ ) ,
23
+ ) ,
24
+ ) ;
25
+ return {
26
+ id : ethers . solidityPackedKeccak256 ( [ 'bytes32[]' ] , [ chainIds . toSorted ( cmp ) ] ) ,
27
+ chainIds,
28
+ } ;
29
+ } ;
9
30
10
31
async function fixture ( ) {
11
32
const [ admin , ...accounts ] = await ethers . getSigners ( ) ;
@@ -20,34 +41,26 @@ async function fixture() {
20
41
const bridgeB = await ethers . deployContract ( '$ERC7802Bridge' ) ;
21
42
const tokenB = await ethers . deployContract ( '$ERC20BridgeableMock' , [ 'Token B' , 'TB' , admin ] ) ;
22
43
23
- // Bridge side identifiers
24
- const chainAId = keccak256AbiEncode (
25
- [ 'bytes' , 'bytes32' , 'bytes32[]' ] ,
26
- [
27
- chain . toErc7930 ( tokenA ) ,
28
- '0x0000000000000000000000000000000000000000000000000000000000000001' , // custodial
29
- sortBytes32 ( [ keccak256AbiEncode ( [ 'bytes' , 'bytes' ] , [ chain . toErc7930 ( gatewayA ) , chain . toErc7930 ( bridgeB ) ] ) ] ) ,
30
- ] ,
44
+ // Compute bridge identifier and local hashes
45
+ const { id, chainIds } = buildBridgeHash (
46
+ {
47
+ token : chain . toErc7930 ( tokenA ) ,
48
+ flags : '0x0000000000000000000000000000000000000000000000000000000000000001' ,
49
+ links : [ { gateway : chain . toErc7930 ( gatewayA ) , remote : chain . toErc7930 ( bridgeB ) } ] ,
50
+ } ,
51
+ {
52
+ token : chain . toErc7930 ( tokenB ) ,
53
+ flags : '0x0000000000000000000000000000000000000000000000000000000000000000' ,
54
+ links : [ { gateway : chain . toErc7930 ( gatewayB ) , remote : chain . toErc7930 ( bridgeA ) } ] ,
55
+ } ,
31
56
) ;
32
57
33
- const chainBId = keccak256AbiEncode (
34
- [ 'bytes' , 'bytes32' , 'bytes32[]' ] ,
35
- [
36
- chain . toErc7930 ( tokenB ) ,
37
- '0x0000000000000000000000000000000000000000000000000000000000000000' , // crosschain
38
- sortBytes32 ( [ keccak256AbiEncode ( [ 'bytes' , 'bytes' ] , [ chain . toErc7930 ( gatewayB ) , chain . toErc7930 ( bridgeA ) ] ) ] ) ,
39
- ] ,
40
- ) ;
41
-
42
- // Bridge global identifier
43
- const id = ethers . solidityPackedKeccak256 ( [ 'bytes32[]' ] , [ sortBytes32 ( [ chainAId , chainBId ] ) ] ) ;
44
-
45
58
// Register bridge
46
59
await expect (
47
60
bridgeA . createBridge (
48
61
tokenA ,
49
62
true , // is custodial
50
- [ { id : chainBId , gateway : gatewayA , remote : chain . toErc7930 ( bridgeB ) } ] , // link to B + id of B
63
+ [ { id : chainIds [ 1 ] , gateway : gatewayA , remote : chain . toErc7930 ( bridgeB ) } ] , // link to B + id of B
51
64
) ,
52
65
)
53
66
. to . emit ( bridgeA , 'NewBridge' )
@@ -57,7 +70,7 @@ async function fixture() {
57
70
bridgeB . createBridge (
58
71
tokenB ,
59
72
false , // is crosschain
60
- [ { id : chainAId , gateway : gatewayB , remote : chain . toErc7930 ( bridgeA ) } ] , // link to B + id of B
73
+ [ { id : chainIds [ 0 ] , gateway : gatewayB , remote : chain . toErc7930 ( bridgeA ) } ] , // link to B + id of B
61
74
) ,
62
75
)
63
76
. to . emit ( bridgeB , 'NewBridge' )
0 commit comments