This repository has been archived by the owner on Jan 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLibMuon.sol
219 lines (207 loc) · 8.16 KB
/
LibMuon.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// SPDX-License-Identifier: SYMM-Core-Business-Source-License-1.1
// This contract is licensed under the SYMM Core Business Source License 1.1
// Copyright (c) 2023 Symmetry Labs AG
// For more information, see https://docs.symm.io/legal-disclaimer/license
pragma solidity >=0.8.18;
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "../libraries/LibMuonV04ClientBase.sol";
import "../storages/MuonStorage.sol";
import "../storages/AccountStorage.sol";
library LibMuon {
using ECDSA for bytes32;
function getChainId() internal view returns (uint256 id) {
assembly {
id := chainid()
}
}
// CONTEXT for commented out lines
// We're utilizing muon signatures for asset pricing and user uPNLs calculations.
// Even though these signatures are necessary for full testing of the system, particularly when invoking various methods.
// The process of creating automated functional signature for tests has proven to be either impractical or excessively time-consuming. therefore, we've established commenting out the necessary code as a workaround specifically for testing.
// Essentially, during testing, we temporarily disable the code sections responsible for validating these signatures. The sections I'm referring to are located within the LibMuon file. Specifically, the body of the 'verifyTSSAndGateway' method is a prime candidate for temporary disablement. In addition, several 'require' statements within other functions of this file, which examine the signatures' expiration status, also need to be temporarily disabled.
// However, it is crucial to note that these lines should not be disabled in the production deployed version.
// We emphasize this because they are only disabled for testing purposes.
function verifyTSSAndGateway(
bytes32 hash,
SchnorrSign memory sign,
bytes memory gatewaySignature
) internal view {
// bool verified = LibMuonV04ClientBase.muonVerify(
// uint256(hash),
// sign,
// MuonStorage.layout().muonPublicKey
// );
// require(verified, "LibMuon: TSS not verified");
//
// hash = hash.toEthSignedMessageHash();
// address gatewaySignatureSigner = hash.recover(gatewaySignature);
//
// require(
// gatewaySignatureSigner == MuonStorage.layout().validGateway,
// "LibMuon: Gateway is not valid"
// );
}
function verifyPrices(PriceSig memory priceSig, address partyA) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
require(priceSig.prices.length == priceSig.symbolIds.length, "LibMuon: Invalid length");
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
priceSig.reqId,
address(this),
partyA,
priceSig.upnl,
priceSig.totalUnrealizedLoss,
priceSig.symbolIds,
priceSig.prices,
priceSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, priceSig.sigs, priceSig.gatewaySignature);
}
function verifyQuotePrices(QuotePriceSig memory priceSig) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
require(priceSig.prices.length == priceSig.quoteIds.length, "LibMuon: Invalid length");
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
priceSig.reqId,
address(this),
priceSig.quoteIds,
priceSig.prices,
priceSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, priceSig.sigs, priceSig.gatewaySignature);
}
function verifyPartyAUpnl(SingleUpnlSig memory upnlSig, address partyA) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
// require(
// block.timestamp <= upnlSig.timestamp + muonLayout.upnlValidTime,
// "LibMuon: Expired signature"
// );
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
upnlSig.reqId,
address(this),
partyA,
AccountStorage.layout().partyANonces[partyA],
upnlSig.upnl,
upnlSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, upnlSig.sigs, upnlSig.gatewaySignature);
}
function verifyPartyAUpnlAndPrice(
SingleUpnlAndPriceSig memory upnlSig,
address partyA,
uint256 symbolId
) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
// require(
// block.timestamp <= upnlSig.timestamp + muonLayout.upnlValidTime,
// "LibMuon: Expired signature"
// );
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
upnlSig.reqId,
address(this),
partyA,
AccountStorage.layout().partyANonces[partyA],
upnlSig.upnl,
symbolId,
upnlSig.price,
upnlSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, upnlSig.sigs, upnlSig.gatewaySignature);
}
function verifyPartyBUpnl(
SingleUpnlSig memory upnlSig,
address partyB,
address partyA
) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
// require(
// block.timestamp <= upnlSig.timestamp + muonLayout.upnlValidTime,
// "LibMuon: Expired signature"
// );
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
upnlSig.reqId,
address(this),
partyB,
partyA,
AccountStorage.layout().partyBNonces[partyB][partyA],
upnlSig.upnl,
upnlSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, upnlSig.sigs, upnlSig.gatewaySignature);
}
function verifyPairUpnlAndPrice(
PairUpnlAndPriceSig memory upnlSig,
address partyB,
address partyA,
uint256 symbolId
) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
// require(
// block.timestamp <= upnlSig.timestamp + muonLayout.upnlValidTime,
// "LibMuon: Expired signature"
// );
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
upnlSig.reqId,
address(this),
partyB,
partyA,
AccountStorage.layout().partyBNonces[partyB][partyA],
AccountStorage.layout().partyANonces[partyA],
upnlSig.upnlPartyB,
upnlSig.upnlPartyA,
symbolId,
upnlSig.price,
upnlSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, upnlSig.sigs, upnlSig.gatewaySignature);
}
function verifyPairUpnl(
PairUpnlSig memory upnlSig,
address partyB,
address partyA
) internal view {
MuonStorage.Layout storage muonLayout = MuonStorage.layout();
// require(
// block.timestamp <= upnlSig.timestamp + muonLayout.upnlValidTime,
// "LibMuon: Expired signature"
// );
bytes32 hash = keccak256(
abi.encodePacked(
muonLayout.muonAppId,
upnlSig.reqId,
address(this),
partyB,
partyA,
AccountStorage.layout().partyBNonces[partyB][partyA],
AccountStorage.layout().partyANonces[partyA],
upnlSig.upnlPartyB,
upnlSig.upnlPartyA,
upnlSig.timestamp,
getChainId()
)
);
verifyTSSAndGateway(hash, upnlSig.sigs, upnlSig.gatewaySignature);
}
}