Skip to content

Commit 46e97f8

Browse files
authored
feat: match contract signature (#22)
* fix: admin contract * Use liqudity pool address from the api request * Add caller to signing request * Remove prints * Lint * Remove print
1 parent 015b0ad commit 46e97f8

File tree

7 files changed

+171
-52
lines changed

7 files changed

+171
-52
lines changed

api/handlers/signing.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"math/big"
99
"net/http"
1010

11+
"github.com/ethereum/go-ethereum/common"
1112
"github.com/gorilla/mux"
1213
across "github.com/sprintertech/sprinter-signing/chains/evm/message"
1314
"github.com/sygmaprotocol/sygma-core/relayer/message"
@@ -20,9 +21,11 @@ const (
2021
)
2122

2223
type SigningBody struct {
23-
ChainId uint64
24-
DepositId *BigInt `json:"depositId"`
25-
Protocol ProtocolType `json:"protocol"`
24+
ChainId uint64
25+
DepositId *BigInt `json:"depositId"`
26+
Protocol ProtocolType `json:"protocol"`
27+
LiquidityPool string `json:"liquidityPool"`
28+
Caller string `json:"caller"`
2629
}
2730

2831
type SigningHandler struct {
@@ -61,8 +64,10 @@ func (h *SigningHandler) HandleSigning(w http.ResponseWriter, r *http.Request) {
6164
case AcrossProtocol:
6265
{
6366
m = across.NewAcrossMessage(0, b.ChainId, across.AcrossData{
64-
DepositId: b.DepositId.Int,
65-
ErrChn: errChn,
67+
DepositId: b.DepositId.Int,
68+
LiquidityPool: common.HexToAddress(b.LiquidityPool),
69+
Caller: common.HexToAddress(b.Caller),
70+
ErrChn: errChn,
6671
})
6772
}
6873
default:
@@ -91,6 +96,14 @@ func (h *SigningHandler) validate(b *SigningBody, vars map[string]string) error
9196
return fmt.Errorf("missing field 'depositId'")
9297
}
9398

99+
if b.LiquidityPool == "" {
100+
return fmt.Errorf("missing field 'liquidityPool'")
101+
}
102+
103+
if b.Caller == "" {
104+
return fmt.Errorf("missing field 'caller'")
105+
}
106+
94107
if b.ChainId == 0 {
95108
return fmt.Errorf("missing field 'chainId'")
96109
}

api/handlers/signing_test.go

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingDepositID() {
4242
handler := handlers.NewSigningHandler(msgChn, s.chains)
4343

4444
input := handlers.SigningBody{
45-
Protocol: "across",
45+
Protocol: "across",
46+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
47+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
4648
}
4749
body, _ := json.Marshal(input)
4850

@@ -65,13 +67,75 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingDepositID() {
6567
s.Equal(http.StatusBadRequest, recorder.Code)
6668
}
6769

68-
func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidChainID() {
70+
func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingCaller() {
71+
msgChn := make(chan []*message.Message)
72+
handler := handlers.NewSigningHandler(msgChn, s.chains)
73+
74+
input := handlers.SigningBody{
75+
Protocol: "across",
76+
DepositId: &handlers.BigInt{big.NewInt(1000)},
77+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
78+
}
79+
body, _ := json.Marshal(input)
80+
81+
req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/signatures", bytes.NewReader(body))
82+
req.Header.Set("Content-Type", "application/json")
83+
req = mux.SetURLVars(req, map[string]string{
84+
"chainId": "1",
85+
})
86+
87+
recorder := httptest.NewRecorder()
88+
89+
go func() {
90+
msg := <-msgChn
91+
ad := msg[0].Data.(across.AcrossData)
92+
ad.ErrChn <- fmt.Errorf("error handling message")
93+
}()
94+
95+
handler.HandleSigning(recorder, req)
96+
97+
s.Equal(http.StatusBadRequest, recorder.Code)
98+
}
99+
100+
func (s *SigningHandlerTestSuite) Test_HandleSigning_MissingLiquidityPool() {
69101
msgChn := make(chan []*message.Message)
70102
handler := handlers.NewSigningHandler(msgChn, s.chains)
71103

72104
input := handlers.SigningBody{
73-
DepositId: &handlers.BigInt{big.NewInt(1000)},
74105
Protocol: "across",
106+
DepositId: &handlers.BigInt{big.NewInt(1000)},
107+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
108+
}
109+
body, _ := json.Marshal(input)
110+
111+
req := httptest.NewRequest(http.MethodPost, "/v1/chains/1/signatures", bytes.NewReader(body))
112+
req.Header.Set("Content-Type", "application/json")
113+
req = mux.SetURLVars(req, map[string]string{
114+
"chainId": "1",
115+
})
116+
117+
recorder := httptest.NewRecorder()
118+
119+
go func() {
120+
msg := <-msgChn
121+
ad := msg[0].Data.(across.AcrossData)
122+
ad.ErrChn <- fmt.Errorf("error handling message")
123+
}()
124+
125+
handler.HandleSigning(recorder, req)
126+
127+
s.Equal(http.StatusBadRequest, recorder.Code)
128+
}
129+
130+
func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidChainID() {
131+
msgChn := make(chan []*message.Message)
132+
handler := handlers.NewSigningHandler(msgChn, s.chains)
133+
134+
input := handlers.SigningBody{
135+
DepositId: &handlers.BigInt{big.NewInt(1000)},
136+
Protocol: "across",
137+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
138+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
75139
}
76140
body, _ := json.Marshal(input)
77141

@@ -99,8 +163,10 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ChainNotSupported() {
99163
handler := handlers.NewSigningHandler(msgChn, s.chains)
100164

101165
input := handlers.SigningBody{
102-
DepositId: &handlers.BigInt{big.NewInt(1000)},
103-
Protocol: "across",
166+
DepositId: &handlers.BigInt{big.NewInt(1000)},
167+
Protocol: "across",
168+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
169+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
104170
}
105171
body, _ := json.Marshal(input)
106172

@@ -128,8 +194,10 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_InvalidProtocol() {
128194
handler := handlers.NewSigningHandler(msgChn, s.chains)
129195

130196
input := handlers.SigningBody{
131-
DepositId: &handlers.BigInt{big.NewInt(1000)},
132-
Protocol: "invalid",
197+
DepositId: &handlers.BigInt{big.NewInt(1000)},
198+
Protocol: "invalid",
199+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
200+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
133201
}
134202
body, _ := json.Marshal(input)
135203

@@ -157,8 +225,10 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_ErrorHandlingMessage() {
157225
handler := handlers.NewSigningHandler(msgChn, s.chains)
158226

159227
input := handlers.SigningBody{
160-
DepositId: &handlers.BigInt{big.NewInt(1000)},
161-
Protocol: "across",
228+
DepositId: &handlers.BigInt{big.NewInt(1000)},
229+
Protocol: "across",
230+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
231+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
162232
}
163233
body, _ := json.Marshal(input)
164234

@@ -186,8 +256,10 @@ func (s *SigningHandlerTestSuite) Test_HandleSigning_Success() {
186256
handler := handlers.NewSigningHandler(msgChn, s.chains)
187257

188258
input := handlers.SigningBody{
189-
DepositId: &handlers.BigInt{big.NewInt(1000)},
190-
Protocol: "across",
259+
DepositId: &handlers.BigInt{big.NewInt(1000)},
260+
Protocol: "across",
261+
LiquidityPool: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
262+
Caller: "0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657",
191263
}
192264
body, _ := json.Marshal(input)
193265

chains/evm/message/across.go

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@ import (
2929
const (
3030
AcrossMessage = "AcrossMessage"
3131

32-
DOMAIN_NAME = "LiquidityPool"
33-
VERSION = "v1.0.0"
34-
BORROW_TYPEHASH = "Borrow(address borrowToken,uint256 amount,address target,bytes targetCallData,uint256 nonce,uint256 deadline)"
35-
PROTOCOL_ID = 1
36-
LIQUIDITY_POOL = "0x5c7BCd6E7De5423a257D81B442095A1a6ced35C5"
37-
BLOCK_RANGE = 1000
32+
DOMAIN_NAME = "LiquidityPool"
33+
VERSION = "1.0.0"
34+
PROTOCOL_ID = 1
35+
BLOCK_RANGE = 1000
3836
)
3937

4038
type EventFilterer interface {
@@ -43,9 +41,11 @@ type EventFilterer interface {
4341
}
4442

4543
type AcrossData struct {
46-
DepositId *big.Int
47-
Coordinator peer.ID
48-
ErrChn chan error
44+
DepositId *big.Int
45+
LiquidityPool common.Address
46+
Caller common.Address
47+
Coordinator peer.ID
48+
ErrChn chan error
4949
}
5050

5151
func NewAcrossMessage(source, destination uint64, acrossData AcrossData) *message.Message {
@@ -110,8 +110,11 @@ func (h *AcrossMessageHandler) Listen(ctx context.Context) {
110110
}
111111

112112
msg := NewAcrossMessage(acrossMsg.Source, acrossMsg.Destination, AcrossData{
113-
DepositId: acrossMsg.DepositId,
114-
Coordinator: wMsg.From,
113+
DepositId: acrossMsg.DepositId,
114+
Coordinator: wMsg.From,
115+
LiquidityPool: common.HexToAddress(acrossMsg.LiqudityPool),
116+
Caller: common.HexToAddress(acrossMsg.Caller),
117+
ErrChn: make(chan error),
115118
})
116119
_, err = h.HandleMessage(msg)
117120
if err != nil {
@@ -149,7 +152,7 @@ func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Prop
149152
return nil, err
150153
}
151154

152-
unlockHash, err := h.unlockHash(d, sourceChainID)
155+
unlockHash, err := h.unlockHash(d, sourceChainID, data)
153156
if err != nil {
154157
data.ErrChn <- err
155158
return nil, err
@@ -182,7 +185,12 @@ func (h *AcrossMessageHandler) HandleMessage(m *message.Message) (*proposal.Prop
182185
}
183186

184187
func (h *AcrossMessageHandler) notify(m *message.Message, data AcrossData) error {
185-
msgBytes, err := tssMessage.MarshalAcrossMessage(data.DepositId, m.Source, m.Destination)
188+
msgBytes, err := tssMessage.MarshalAcrossMessage(
189+
data.DepositId,
190+
data.LiquidityPool.Hex(),
191+
data.Caller.Hex(),
192+
m.Source,
193+
m.Destination)
186194
if err != nil {
187195
return err
188196
}
@@ -243,13 +251,20 @@ func (h *AcrossMessageHandler) parseDeposit(l types.Log) (*events.AcrossDeposit,
243251
return d, err
244252
}
245253

246-
func (h *AcrossMessageHandler) unlockHash(deposit *events.AcrossDeposit, sourceChainId uint64) ([]byte, error) {
247-
lpAddress := common.HexToAddress(LIQUIDITY_POOL)
248-
calldata, err := deposit.ToV3RelayData(new(big.Int).SetUint64(sourceChainId)).Calldata(deposit.DestinationChainId, lpAddress)
254+
func (h *AcrossMessageHandler) unlockHash(
255+
deposit *events.AcrossDeposit,
256+
sourceChainId uint64,
257+
data AcrossData,
258+
) ([]byte, error) {
259+
calldata, err := deposit.ToV3RelayData(
260+
new(big.Int).SetUint64(sourceChainId),
261+
).Calldata(deposit.DestinationChainId, data.LiquidityPool)
249262
if err != nil {
250263
return []byte{}, err
251264
}
265+
252266
msg := apitypes.TypedDataMessage{
267+
"caller": data.Caller.Hex(),
253268
"borrowToken": common.BytesToAddress(deposit.OutputToken[12:]).Hex(),
254269
"amount": deposit.OutputAmount,
255270
"target": common.BytesToAddress(deposit.Recipient[12:]).Hex(),
@@ -267,6 +282,7 @@ func (h *AcrossMessageHandler) unlockHash(deposit *events.AcrossDeposit, sourceC
267282
{Name: "verifyingContract", Type: "address"},
268283
},
269284
"Borrow": []apitypes.Type{
285+
{Name: "caller", Type: "address"},
270286
{Name: "borrowToken", Type: "address"},
271287
{Name: "amount", Type: "uint256"},
272288
{Name: "target", Type: "address"},
@@ -280,7 +296,7 @@ func (h *AcrossMessageHandler) unlockHash(deposit *events.AcrossDeposit, sourceC
280296
Name: DOMAIN_NAME,
281297
ChainId: math.NewHexOrDecimal256(deposit.DestinationChainId.Int64()),
282298
Version: VERSION,
283-
VerifyingContract: lpAddress.Hex(),
299+
VerifyingContract: data.LiquidityPool.Hex(),
284300
},
285301
Message: msg,
286302
}

chains/evm/message/across_test.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_FailedLogQuery() {
8888

8989
errChn := make(chan error, 1)
9090
ad := message.AcrossData{
91-
ErrChn: errChn,
92-
DepositId: big.NewInt(100),
91+
ErrChn: errChn,
92+
DepositId: big.NewInt(100),
93+
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
94+
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
9395
}
9496
m := &coreMessage.Message{
9597
Data: ad,
@@ -120,8 +122,10 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_LogMissing() {
120122

121123
errChn := make(chan error, 1)
122124
ad := message.AcrossData{
123-
ErrChn: errChn,
124-
DepositId: big.NewInt(100),
125+
ErrChn: errChn,
126+
DepositId: big.NewInt(100),
127+
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
128+
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
125129
}
126130
m := &coreMessage.Message{
127131
Data: ad,
@@ -157,8 +161,10 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_IgnoreRemovedLogs() {
157161

158162
errChn := make(chan error, 1)
159163
ad := message.AcrossData{
160-
ErrChn: errChn,
161-
DepositId: big.NewInt(100),
164+
ErrChn: errChn,
165+
DepositId: big.NewInt(100),
166+
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
167+
Caller: common.HexToAddress("0xde526bA5d1ad94cC59D7A79d99A59F607d31A657"),
162168
}
163169
m := &coreMessage.Message{
164170
Data: ad,
@@ -201,8 +207,10 @@ func (s *AcrossMessageHandlerTestSuite) Test_HandleMessage_ValidLog() {
201207

202208
errChn := make(chan error, 1)
203209
ad := message.AcrossData{
204-
ErrChn: errChn,
205-
DepositId: big.NewInt(100),
210+
ErrChn: errChn,
211+
DepositId: big.NewInt(100),
212+
LiquidityPool: common.HexToAddress("0xbe526bA5d1ad94cC59D7A79d99A59F607d31A657"),
213+
Caller: common.HexToAddress("0x5ECF7351930e4A251193aA022Ef06249C6cBfa27"),
206214
}
207215
m := &coreMessage.Message{
208216
Data: ad,

contracts/Admin.sol

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import "@openzeppelin/contracts/access/Ownable.sol";
99
@author ChainSafe Systems.
1010
*/
1111
contract Admin is Ownable {
12+
address public _MPCAddress;
13+
1214
event StartKeygen();
1315
event EndKeygen();
1416
event KeyRefresh(string hash);
@@ -49,4 +51,4 @@ contract Admin is Ownable {
4951
function refreshKey(string memory hash) external onlyOwner {
5052
emit KeyRefresh(hash);
5153
}
52-
}
54+
}

tss/message/message.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,20 @@ func UnmarshalSignatureMessage(msgBytes []byte) (*SignatureMessage, error) {
9494
}
9595

9696
type AcrossMessage struct {
97-
DepositId *big.Int `json:"depositId"`
98-
Source uint64 `json:"source"`
99-
Destination uint64 `json:"destination"`
97+
DepositId *big.Int `json:"depositId"`
98+
LiqudityPool string `json:"liqudityPool"`
99+
Caller string `json:"caller"`
100+
Source uint64 `json:"source"`
101+
Destination uint64 `json:"destination"`
100102
}
101103

102-
func MarshalAcrossMessage(depositId *big.Int, source, destination uint64) ([]byte, error) {
104+
func MarshalAcrossMessage(depositId *big.Int, pool, caller string, source, destination uint64) ([]byte, error) {
103105
signatureMessage := &AcrossMessage{
104-
DepositId: depositId,
105-
Source: source,
106-
Destination: destination,
106+
LiqudityPool: pool,
107+
Caller: caller,
108+
DepositId: depositId,
109+
Source: source,
110+
Destination: destination,
107111
}
108112

109113
msgBytes, err := json.Marshal(signatureMessage)

0 commit comments

Comments
 (0)