@@ -9,7 +9,10 @@ import (
99 "github.com/ethereum/go-ethereum"
1010 "github.com/ethereum/go-ethereum/accounts/abi"
1111 "github.com/ethereum/go-ethereum/common"
12+ "github.com/ethereum/go-ethereum/common/math"
1213 "github.com/ethereum/go-ethereum/core/types"
14+ "github.com/ethereum/go-ethereum/crypto"
15+ "github.com/ethereum/go-ethereum/signer/core/apitypes"
1316 "github.com/libp2p/go-libp2p/core/host"
1417 "github.com/libp2p/go-libp2p/core/peer"
1518 "github.com/rs/zerolog/log"
@@ -24,6 +27,11 @@ import (
2427
2528const (
2629 AcrossMessage = "AcrossMessage"
30+
31+ DOMAIN_NAME = ""
32+ VERSION = "v1.0.0"
33+ BORROW_TYPEHASH = "Borrow(address borrowToken,uint256 amount,address target,bytes targetCallData,uint256 nonce,uint256 deadline)"
34+ PROTOCOL_ID = 1
2735)
2836
2937type EventFilterer interface {
@@ -45,10 +53,16 @@ func NewAcrossMessage(source uint8, destination uint8, acrossData AcrossData) *m
4553 }
4654}
4755
56+ type Coordinator interface {
57+ Execute (ctx context.Context , tssProcesses []tss.TssProcess , resultChn chan interface {}, coordinator peer.ID ) error
58+ }
59+
4860type AcrossMessageHandler struct {
49- client EventFilterer
61+ client EventFilterer
62+ sourceChainID * big.Int
5063
5164 across common.Address
65+ pools map [uint64 ]common.Address
5266 abi abi.ABI
5367
5468 coordinator * tss.Coordinator
@@ -106,8 +120,13 @@ func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Prop
106120 return nil , err
107121 }
108122
123+ unlockHash , err := h .unlockHash (d )
124+ if err != nil {
125+ return nil , err
126+ }
127+
109128 signing , err := signing .NewSigning (
110- d . DepositId ,
129+ new (big. Int ). SetBytes ( unlockHash ) ,
111130 data .depositId .Text (16 ),
112131 data .depositId .Text (16 ),
113132 h .host ,
@@ -161,3 +180,62 @@ func (h *AcrossMessageHandler) parseDeposit(l types.Log) (*events.AcrossDeposit,
161180 err := h .abi .UnpackIntoInterface (& d , "V3FundsDeposited" , l .Data )
162181 return d , err
163182}
183+
184+ func (h * AcrossMessageHandler ) unlockHash (deposit * events.AcrossDeposit ) ([]byte , error ) {
185+ calldata , err := deposit .ToV3RelayData (h .sourceChainID ).Calldata ()
186+ if err != nil {
187+ return []byte {}, nil
188+ }
189+
190+ encodedData := crypto .Keccak256 (
191+ crypto .Keccak256 (
192+ []byte (
193+ "Borrow(address borrowToken,uint256 amount,address target,bytes targetCallData,uint256 nonce,uint256 deadline)" ,
194+ ),
195+ ),
196+ deposit .OutputToken [12 :],
197+ deposit .OutputAmount .Bytes (),
198+ deposit .Recipient [12 :],
199+ calldata ,
200+ common .LeftPadBytes (h .nonce (deposit ).Bytes (), 32 ),
201+ new (big.Int ).SetUint64 (uint64 (deposit .FillDeadline )).Bytes (),
202+ )
203+
204+ poolAddress := h .pools [h .sourceChainID .Uint64 ()]
205+ typedData := apitypes.TypedData {
206+ Domain : apitypes.TypedDataDomain {
207+ Name : DOMAIN_NAME ,
208+ ChainId : math .NewHexOrDecimal256 (deposit .DestinationChainId .Int64 ()),
209+ Version : VERSION ,
210+ VerifyingContract : poolAddress .Hex (),
211+ },
212+ }
213+ domainSeparator , err := typedData .HashStruct ("EIP712Domain" , typedData .Domain .Map ())
214+ if err != nil {
215+ return []byte {}, err
216+ }
217+
218+ rawData := []byte (fmt .Sprintf ("\x19 \x01 %s%s" , string (domainSeparator ), string (encodedData )))
219+ return crypto .Keccak256 (rawData ), nil
220+ }
221+
222+ // nonce creates a unique ID from the across deposit id, origin chain id and protocol id.
223+ // Resulting id has this format: [originChainID (8 bits)][protocolID (8 bits)][nonce (240 bits)].
224+ func (h * AcrossMessageHandler ) nonce (deposit * events.AcrossDeposit ) * big.Int {
225+ // Create a new big.Int
226+ nonce := new (big.Int )
227+
228+ // Set originChainID (64 bits)
229+ nonce .SetInt64 (h .sourceChainID .Int64 ())
230+ nonce .Lsh (nonce , 256 ) // Shift left by 320 bits (248 + 8)
231+
232+ // Add protocolID in the middle (shifted left by 248 bits)
233+ protocolInt := big .NewInt (PROTOCOL_ID )
234+ protocolInt .Lsh (protocolInt , 248 )
235+ nonce .Or (nonce , protocolInt )
236+
237+ // Add nonce at the end
238+ nonce .Or (nonce , deposit .DepositId )
239+
240+ return nonce
241+ }
0 commit comments