@layerzerolabs/lz-evm-oapp-v2 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/artifacts/contracts/oapp/OApp.sol/OApp.json +332 -0
- package/artifacts/contracts/oapp/OAppCore.sol/OAppCore.json +195 -0
- package/artifacts/contracts/oapp/OAppReceiver.sol/OAppReceiver.json +316 -0
- package/artifacts/contracts/oapp/OAppSender.sol/OAppSender.json +211 -0
- package/artifacts/contracts/oapp/examples/OmniCounter.sol/MsgCodec.json +10 -0
- package/artifacts/contracts/oapp/examples/OmniCounter.sol/OmniCounter.json +874 -0
- package/artifacts/contracts/oapp/examples/OmniCounterPreCrime.sol/OmniCounterPreCrime.json +373 -0
- package/artifacts/contracts/oapp/interfaces/IOAppComposer.sol/IOAppComposer.json +44 -0
- package/artifacts/contracts/oapp/interfaces/IOAppCore.sol/IOAppCore.json +143 -0
- package/artifacts/contracts/oapp/interfaces/IOAppMsgInspector.sol/IOAppMsgInspector.json +51 -0
- package/artifacts/contracts/oapp/interfaces/IOAppOptionsType3.sol/IOAppOptionsType3.json +111 -0
- package/artifacts/contracts/oapp/libs/OAppOptionsType3.sol/OAppOptionsType3.json +187 -0
- package/artifacts/contracts/oapp/libs/OptionsBuilder.sol/OptionsBuilder.json +38 -0
- package/artifacts/contracts/oft/OFT.sol/OFT.json +1466 -0
- package/artifacts/contracts/oft/OFTAdapter.sol/OFTAdapter.json +1204 -0
- package/artifacts/contracts/oft/OFTCore.sol/OFTCore.json +1170 -0
- package/artifacts/contracts/oft/interfaces/IOFT.sol/IOFT.json +472 -0
- package/artifacts/contracts/oft/libs/OFTComposeMsgCodec.sol/OFTComposeMsgCodec.json +10 -0
- package/artifacts/contracts/oft/libs/OFTMsgCodec.sol/OFTMsgCodec.json +10 -0
- package/artifacts/contracts/precrime/OAppPreCrimeSimulator.sol/OAppPreCrimeSimulator.json +277 -0
- package/artifacts/contracts/precrime/PreCrime.sol/PreCrime.json +352 -0
- package/artifacts/contracts/precrime/extensions/PreCrimeE1.sol/PreCrimeE1.json +352 -0
- package/artifacts/contracts/precrime/interfaces/IOAppPreCrimeSimulator.sol/IOAppPreCrimeSimulator.json +175 -0
- package/artifacts/contracts/precrime/interfaces/IPreCrime.sol/IPreCrime.json +188 -0
- package/artifacts/contracts/precrime/libs/Packet.sol/PacketDecoder.json +10 -0
- package/contracts/oapp/OApp.sol +39 -0
- package/contracts/oapp/OAppCore.sol +68 -0
- package/contracts/oapp/OAppReceiver.sol +101 -0
- package/contracts/oapp/OAppSender.sol +124 -0
- package/contracts/oapp/examples/OmniCounter.sol +283 -0
- package/contracts/oapp/examples/OmniCounterPreCrime.sol +102 -0
- package/contracts/oapp/interfaces/IOAppComposer.sol +12 -0
- package/contracts/oapp/interfaces/IOAppCore.sol +51 -0
- package/contracts/oapp/interfaces/IOAppMsgInspector.sol +22 -0
- package/contracts/oapp/interfaces/IOAppOptionsType3.sol +43 -0
- package/contracts/oapp/libs/OAppOptionsType3.sol +82 -0
- package/contracts/oapp/libs/OptionsBuilder.sol +200 -0
- package/contracts/oft/OFT.sol +116 -0
- package/contracts/oft/OFTAdapter.sol +131 -0
- package/contracts/oft/OFTCore.sol +456 -0
- package/contracts/oft/OFTPrecrime.sol +99 -0
- package/contracts/oft/interfaces/IOFT.sol +168 -0
- package/contracts/oft/libs/OFTComposeMsgCodec.sol +91 -0
- package/contracts/oft/libs/OFTMsgCodec.sol +83 -0
- package/contracts/precrime/OAppPreCrimeSimulator.sol +125 -0
- package/contracts/precrime/PreCrime.sol +208 -0
- package/contracts/precrime/extensions/PreCrimeE1.sol +30 -0
- package/contracts/precrime/interfaces/IOAppPreCrimeSimulator.sol +55 -0
- package/contracts/precrime/interfaces/IPreCrime.sol +40 -0
- package/contracts/precrime/libs/Packet.sol +61 -0
- package/package.json +35 -0
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
pragma solidity ^0.8.22;
|
|
4
|
+
|
|
5
|
+
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
|
|
6
|
+
import { BytesLib } from "solidity-bytes-utils/contracts/BytesLib.sol";
|
|
7
|
+
import { ILayerZeroEndpointV2 } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
|
|
8
|
+
|
|
9
|
+
import { IPreCrime, PreCrimePeer } from "./interfaces/IPreCrime.sol";
|
|
10
|
+
import { IOAppPreCrimeSimulator } from "./interfaces/IOAppPreCrimeSimulator.sol";
|
|
11
|
+
import { InboundPacket, PacketDecoder } from "./libs/Packet.sol";
|
|
12
|
+
|
|
13
|
+
abstract contract PreCrime is Ownable, IPreCrime {
|
|
14
|
+
using BytesLib for bytes;
|
|
15
|
+
|
|
16
|
+
uint16 internal constant CONFIG_VERSION = 2;
|
|
17
|
+
address internal constant OFF_CHAIN_CALLER = address(0xDEAD);
|
|
18
|
+
|
|
19
|
+
address internal immutable lzEndpoint;
|
|
20
|
+
address public immutable simulator;
|
|
21
|
+
address public immutable oApp;
|
|
22
|
+
|
|
23
|
+
// preCrime config
|
|
24
|
+
uint64 public maxBatchSize;
|
|
25
|
+
PreCrimePeer[] internal preCrimePeers;
|
|
26
|
+
|
|
27
|
+
/// @dev getConfig(), simulate() and preCrime() are not view functions because it is more flexible to be able to
|
|
28
|
+
/// update state for some complex logic. So onlyOffChain() modifier is to make sure they are only called
|
|
29
|
+
/// by the off-chain.
|
|
30
|
+
modifier onlyOffChain() {
|
|
31
|
+
if (msg.sender != OFF_CHAIN_CALLER) revert OnlyOffChain();
|
|
32
|
+
_;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
constructor(address _endpoint, address _simulator, address _owner) {
|
|
36
|
+
_transferOwnership(_owner);
|
|
37
|
+
lzEndpoint = _endpoint;
|
|
38
|
+
simulator = _simulator;
|
|
39
|
+
oApp = IOAppPreCrimeSimulator(_simulator).oApp();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function setMaxBatchSize(uint64 _maxBatchSize) external onlyOwner {
|
|
43
|
+
maxBatchSize = _maxBatchSize;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function setPreCrimePeers(PreCrimePeer[] calldata _preCrimePeers) external onlyOwner {
|
|
47
|
+
delete preCrimePeers;
|
|
48
|
+
for (uint256 i = 0; i < _preCrimePeers.length; ++i) {
|
|
49
|
+
preCrimePeers.push(_preCrimePeers[i]);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function getPreCrimePeers() external view returns (PreCrimePeer[] memory) {
|
|
54
|
+
return preCrimePeers;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function getConfig(
|
|
58
|
+
bytes[] calldata _packets,
|
|
59
|
+
uint256[] calldata _packetMsgValues
|
|
60
|
+
) external onlyOffChain returns (bytes memory) {
|
|
61
|
+
bytes memory config = abi.encodePacked(CONFIG_VERSION, maxBatchSize);
|
|
62
|
+
|
|
63
|
+
// if no packets, return config with all peers
|
|
64
|
+
PreCrimePeer[] memory peers = _packets.length == 0
|
|
65
|
+
? preCrimePeers
|
|
66
|
+
: _getPreCrimePeers(PacketDecoder.decode(_packets, _packetMsgValues));
|
|
67
|
+
|
|
68
|
+
if (peers.length > 0) {
|
|
69
|
+
uint16 size = uint16(peers.length);
|
|
70
|
+
config = abi.encodePacked(config, size);
|
|
71
|
+
|
|
72
|
+
for (uint256 i = 0; i < size; ++i) {
|
|
73
|
+
config = abi.encodePacked(config, peers[i].eid, peers[i].preCrime, peers[i].oApp);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return config;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// @dev _packetMsgValues refers to the 'lzReceive' option passed per packet
|
|
81
|
+
function simulate(
|
|
82
|
+
bytes[] calldata _packets,
|
|
83
|
+
uint256[] calldata _packetMsgValues
|
|
84
|
+
) external payable override onlyOffChain returns (bytes memory) {
|
|
85
|
+
InboundPacket[] memory packets = PacketDecoder.decode(_packets, _packetMsgValues);
|
|
86
|
+
_checkPacketSizeAndOrder(packets);
|
|
87
|
+
return _simulate(packets);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function preCrime(
|
|
91
|
+
bytes[] calldata _packets,
|
|
92
|
+
uint256[] calldata _packetMsgValues,
|
|
93
|
+
bytes[] calldata _simulations
|
|
94
|
+
) external onlyOffChain {
|
|
95
|
+
InboundPacket[] memory packets = PacketDecoder.decode(_packets, _packetMsgValues);
|
|
96
|
+
uint32[] memory eids = new uint32[](_simulations.length);
|
|
97
|
+
bytes[] memory simulations = new bytes[](_simulations.length);
|
|
98
|
+
|
|
99
|
+
for (uint256 i = 0; i < _simulations.length; ++i) {
|
|
100
|
+
bytes calldata simulation = _simulations[i];
|
|
101
|
+
eids[i] = uint32(bytes4(simulation[0:4]));
|
|
102
|
+
simulations[i] = simulation[4:];
|
|
103
|
+
}
|
|
104
|
+
_checkResultsCompleteness(packets, eids);
|
|
105
|
+
|
|
106
|
+
_preCrime(packets, eids, simulations);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function version() external pure returns (uint64 major, uint8 minor) {
|
|
110
|
+
return (2, 0);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
function _checkResultsCompleteness(InboundPacket[] memory _packets, uint32[] memory _eids) internal {
|
|
114
|
+
// check if all peers result included
|
|
115
|
+
if (_packets.length > 0) {
|
|
116
|
+
PreCrimePeer[] memory peers = _getPreCrimePeers(_packets);
|
|
117
|
+
for (uint256 i = 0; i < peers.length; i++) {
|
|
118
|
+
uint32 expectedEid = peers[i].eid;
|
|
119
|
+
if (!_isContain(_eids, expectedEid)) revert SimulationResultNotFound(expectedEid);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// check if local result included
|
|
124
|
+
uint32 localEid = _getLocalEid();
|
|
125
|
+
if (!_isContain(_eids, localEid)) revert SimulationResultNotFound(localEid);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function _isContain(uint32[] memory _array, uint32 _item) internal pure returns (bool) {
|
|
129
|
+
for (uint256 i = 0; i < _array.length; i++) {
|
|
130
|
+
if (_array[i] == _item) return true;
|
|
131
|
+
}
|
|
132
|
+
return false;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function _checkPacketSizeAndOrder(InboundPacket[] memory _packets) internal view {
|
|
136
|
+
if (_packets.length > maxBatchSize) revert PacketOversize(maxBatchSize, _packets.length);
|
|
137
|
+
|
|
138
|
+
// check packets nonce, sequence order
|
|
139
|
+
// packets should group by srcEid and sender, then sort by nonce ascending
|
|
140
|
+
if (_packets.length > 0) {
|
|
141
|
+
uint32 srcEid;
|
|
142
|
+
bytes32 sender;
|
|
143
|
+
uint64 nonce;
|
|
144
|
+
for (uint256 i = 0; i < _packets.length; i++) {
|
|
145
|
+
InboundPacket memory packet = _packets[i];
|
|
146
|
+
|
|
147
|
+
// skip if not from trusted peer
|
|
148
|
+
if (!IOAppPreCrimeSimulator(simulator).isPeer(packet.origin.srcEid, packet.origin.sender)) continue;
|
|
149
|
+
|
|
150
|
+
// start from a new chain or a new source oApp
|
|
151
|
+
if (packet.origin.srcEid != srcEid || packet.origin.sender != sender) {
|
|
152
|
+
srcEid = packet.origin.srcEid;
|
|
153
|
+
sender = packet.origin.sender;
|
|
154
|
+
nonce = _getInboundNonce(srcEid, sender);
|
|
155
|
+
}
|
|
156
|
+
// TODO ??
|
|
157
|
+
// Wont the nonce order not matter and enforced at the OApp level? the simulation will revert?
|
|
158
|
+
|
|
159
|
+
// the following packet's nonce add 1 in order
|
|
160
|
+
if (packet.origin.nonce != ++nonce) revert PacketUnsorted();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function _simulate(InboundPacket[] memory _packets) internal virtual returns (bytes memory) {
|
|
166
|
+
(bool success, bytes memory returnData) = simulator.call{ value: msg.value }(
|
|
167
|
+
abi.encodeWithSelector(IOAppPreCrimeSimulator.lzReceiveAndRevert.selector, _packets)
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
bytes memory result = _parseRevertResult(success, returnData);
|
|
171
|
+
return abi.encodePacked(_getLocalEid(), result); // add localEid at the first of the result
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function _parseRevertResult(bool _success, bytes memory _returnData) internal pure returns (bytes memory result) {
|
|
175
|
+
// should always revert with LzReceiveRevert
|
|
176
|
+
if (_success) revert SimulationFailed("no revert");
|
|
177
|
+
|
|
178
|
+
// if not expected selector, bubble up error
|
|
179
|
+
if (bytes4(_returnData) != IOAppPreCrimeSimulator.SimulationResult.selector) {
|
|
180
|
+
revert SimulationFailed(_returnData);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Slice the sighash. Remove the selector which is the first 4 bytes
|
|
184
|
+
result = _returnData.slice(4, _returnData.length - 4);
|
|
185
|
+
result = abi.decode(result, (bytes));
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// to be compatible with EndpointV1
|
|
189
|
+
function _getLocalEid() internal view virtual returns (uint32) {
|
|
190
|
+
return ILayerZeroEndpointV2(lzEndpoint).eid();
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// to be compatible with EndpointV1
|
|
194
|
+
function _getInboundNonce(uint32 _srcEid, bytes32 _sender) internal view virtual returns (uint64) {
|
|
195
|
+
return ILayerZeroEndpointV2(lzEndpoint).inboundNonce(oApp, _srcEid, _sender);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// ----------------- to be implemented -----------------
|
|
199
|
+
function buildSimulationResult() external view virtual override returns (bytes memory);
|
|
200
|
+
|
|
201
|
+
function _getPreCrimePeers(InboundPacket[] memory _packets) internal virtual returns (PreCrimePeer[] memory peers);
|
|
202
|
+
|
|
203
|
+
function _preCrime(
|
|
204
|
+
InboundPacket[] memory _packets,
|
|
205
|
+
uint32[] memory _eids,
|
|
206
|
+
bytes[] memory _simulations
|
|
207
|
+
) internal virtual;
|
|
208
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
pragma solidity ^0.8.22;
|
|
4
|
+
|
|
5
|
+
import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol";
|
|
6
|
+
|
|
7
|
+
import { ILayerZeroEndpoint } from "@layerzerolabs/lz-evm-v1-0.7/contracts/interfaces/ILayerZeroEndpoint.sol";
|
|
8
|
+
|
|
9
|
+
import { PreCrime } from "../PreCrime.sol";
|
|
10
|
+
|
|
11
|
+
abstract contract PreCrimeE1 is PreCrime {
|
|
12
|
+
using SafeCast for uint32;
|
|
13
|
+
|
|
14
|
+
uint32 internal immutable localEid;
|
|
15
|
+
|
|
16
|
+
constructor(uint32 _localEid, address _endpoint, address _simulator) PreCrime(_endpoint, _simulator, msg.sender) {
|
|
17
|
+
localEid = _localEid;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function _getLocalEid() internal view override returns (uint32) {
|
|
21
|
+
return localEid;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function _getInboundNonce(uint32 _srcEid, bytes32 _sender) internal view override returns (uint64) {
|
|
25
|
+
bytes memory path = _getPath(_srcEid, _sender);
|
|
26
|
+
return ILayerZeroEndpoint(lzEndpoint).getInboundNonce(_srcEid.toUint16(), path);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function _getPath(uint32 _srcEid, bytes32 _sender) internal view virtual returns (bytes memory);
|
|
30
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
pragma solidity ^0.8.22;
|
|
4
|
+
|
|
5
|
+
// @dev Import the Origin so it's exposed to OAppPreCrimeSimulator implementers.
|
|
6
|
+
// solhint-disable-next-line no-unused-import
|
|
7
|
+
import { InboundPacket, Origin } from "../libs/Packet.sol";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @title IOAppPreCrimeSimulator Interface
|
|
11
|
+
* @dev Interface for the preCrime simulation functionality in an OApp.
|
|
12
|
+
*/
|
|
13
|
+
interface IOAppPreCrimeSimulator {
|
|
14
|
+
// @dev simulation result used in PreCrime implementation
|
|
15
|
+
error SimulationResult(bytes result);
|
|
16
|
+
error OnlySelf();
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* @dev Emitted when the preCrime contract address is set.
|
|
20
|
+
* @param preCrimeAddress The address of the preCrime contract.
|
|
21
|
+
*/
|
|
22
|
+
event PreCrimeSet(address preCrimeAddress);
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @dev Retrieves the address of the preCrime contract implementation.
|
|
26
|
+
* @return The address of the preCrime contract.
|
|
27
|
+
*/
|
|
28
|
+
function preCrime() external view returns (address);
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @dev Retrieves the address of the OApp contract.
|
|
32
|
+
* @return The address of the OApp contract.
|
|
33
|
+
*/
|
|
34
|
+
function oApp() external view returns (address);
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @dev Sets the preCrime contract address.
|
|
38
|
+
* @param _preCrime The address of the preCrime contract.
|
|
39
|
+
*/
|
|
40
|
+
function setPreCrime(address _preCrime) external;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @dev Mocks receiving a packet, then reverts with a series of data to infer the state/result.
|
|
44
|
+
* @param _packets An array of LayerZero InboundPacket objects representing received packets.
|
|
45
|
+
*/
|
|
46
|
+
function lzReceiveAndRevert(InboundPacket[] calldata _packets) external payable;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* @dev checks if the specified peer is considered 'trusted' by the OApp.
|
|
50
|
+
* @param _eid The endpoint Id to check.
|
|
51
|
+
* @param _peer The peer to check.
|
|
52
|
+
* @return Whether the peer passed is considered 'trusted' by the OApp.
|
|
53
|
+
*/
|
|
54
|
+
function isPeer(uint32 _eid, bytes32 _peer) external view returns (bool);
|
|
55
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
pragma solidity ^0.8.22;
|
|
4
|
+
struct PreCrimePeer {
|
|
5
|
+
uint32 eid;
|
|
6
|
+
bytes32 preCrime;
|
|
7
|
+
bytes32 oApp;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
// TODO not done yet
|
|
11
|
+
interface IPreCrime {
|
|
12
|
+
error OnlyOffChain();
|
|
13
|
+
|
|
14
|
+
// for simulate()
|
|
15
|
+
error PacketOversize(uint256 max, uint256 actual);
|
|
16
|
+
error PacketUnsorted();
|
|
17
|
+
error SimulationFailed(bytes reason);
|
|
18
|
+
|
|
19
|
+
// for preCrime()
|
|
20
|
+
error SimulationResultNotFound(uint32 eid);
|
|
21
|
+
error InvalidSimulationResult(uint32 eid, bytes reason);
|
|
22
|
+
error CrimeFound(bytes crime);
|
|
23
|
+
|
|
24
|
+
function getConfig(bytes[] calldata _packets, uint256[] calldata _packetMsgValues) external returns (bytes memory);
|
|
25
|
+
|
|
26
|
+
function simulate(
|
|
27
|
+
bytes[] calldata _packets,
|
|
28
|
+
uint256[] calldata _packetMsgValues
|
|
29
|
+
) external payable returns (bytes memory);
|
|
30
|
+
|
|
31
|
+
function buildSimulationResult() external view returns (bytes memory);
|
|
32
|
+
|
|
33
|
+
function preCrime(
|
|
34
|
+
bytes[] calldata _packets,
|
|
35
|
+
uint256[] calldata _packetMsgValues,
|
|
36
|
+
bytes[] calldata _simulations
|
|
37
|
+
) external;
|
|
38
|
+
|
|
39
|
+
function version() external view returns (uint64 major, uint8 minor);
|
|
40
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// SPDX-License-Identifier: MIT
|
|
2
|
+
|
|
3
|
+
pragma solidity ^0.8.22;
|
|
4
|
+
|
|
5
|
+
import { Origin } from "@layerzerolabs/lz-evm-protocol-v2/contracts/interfaces/ILayerZeroEndpointV2.sol";
|
|
6
|
+
import { PacketV1Codec } from "@layerzerolabs/lz-evm-protocol-v2/contracts/messagelib/libs/PacketV1Codec.sol";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @title InboundPacket
|
|
10
|
+
* @dev Structure representing an inbound packet received by the contract.
|
|
11
|
+
*/
|
|
12
|
+
struct InboundPacket {
|
|
13
|
+
Origin origin; // Origin information of the packet.
|
|
14
|
+
uint32 dstEid; // Destination endpointId of the packet.
|
|
15
|
+
address receiver; // Receiver address for the packet.
|
|
16
|
+
bytes32 guid; // Unique identifier of the packet.
|
|
17
|
+
uint256 value; // msg.value of the packet.
|
|
18
|
+
address executor; // Executor address for the packet.
|
|
19
|
+
bytes message; // Message payload of the packet.
|
|
20
|
+
bytes extraData; // Additional arbitrary data for the packet.
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @title PacketDecoder
|
|
25
|
+
* @dev Library for decoding LayerZero packets.
|
|
26
|
+
*/
|
|
27
|
+
library PacketDecoder {
|
|
28
|
+
using PacketV1Codec for bytes;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @dev Decode an inbound packet from the given packet data.
|
|
32
|
+
* @param _packet The packet data to decode.
|
|
33
|
+
* @return packet An InboundPacket struct representing the decoded packet.
|
|
34
|
+
*/
|
|
35
|
+
function decode(bytes calldata _packet) internal pure returns (InboundPacket memory packet) {
|
|
36
|
+
packet.origin = Origin(_packet.srcEid(), _packet.sender(), _packet.nonce());
|
|
37
|
+
packet.dstEid = _packet.dstEid();
|
|
38
|
+
packet.receiver = _packet.receiverB20();
|
|
39
|
+
packet.guid = _packet.guid();
|
|
40
|
+
packet.message = _packet.message();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* @dev Decode multiple inbound packets from the given packet data and associated message values.
|
|
45
|
+
* @param _packets An array of packet data to decode.
|
|
46
|
+
* @param _packetMsgValues An array of associated message values for each packet.
|
|
47
|
+
* @return packets An array of InboundPacket structs representing the decoded packets.
|
|
48
|
+
*/
|
|
49
|
+
function decode(
|
|
50
|
+
bytes[] calldata _packets,
|
|
51
|
+
uint256[] memory _packetMsgValues
|
|
52
|
+
) internal pure returns (InboundPacket[] memory packets) {
|
|
53
|
+
packets = new InboundPacket[](_packets.length);
|
|
54
|
+
for (uint256 i = 0; i < _packets.length; i++) {
|
|
55
|
+
bytes calldata packet = _packets[i];
|
|
56
|
+
packets[i] = PacketDecoder.decode(packet);
|
|
57
|
+
// @dev Allows the verifier to specify the msg.value that gets passed in lzReceive.
|
|
58
|
+
packets[i].value = _packetMsgValues[i];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@layerzerolabs/lz-evm-oapp-v2",
|
|
3
|
+
"version": "2.0.2",
|
|
4
|
+
"license": "LZBL-1.2",
|
|
5
|
+
"files": [
|
|
6
|
+
"artifacts/contracts/**/!(*.dbg).json",
|
|
7
|
+
"contracts/**/*"
|
|
8
|
+
],
|
|
9
|
+
"scripts": {
|
|
10
|
+
"postpack": "node ./scripts/pack.mjs --postpack"
|
|
11
|
+
},
|
|
12
|
+
"dependencies": {
|
|
13
|
+
"@layerzerolabs/lz-evm-messagelib-v2": "^2.0.2",
|
|
14
|
+
"@layerzerolabs/lz-evm-protocol-v2": "^2.0.2",
|
|
15
|
+
"@layerzerolabs/lz-evm-v1-0.7": "^2.0.2"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"@openzeppelin/contracts": "^4.8.1",
|
|
19
|
+
"@openzeppelin/contracts-upgradeable": "^4.8.1",
|
|
20
|
+
"hardhat-deploy": "^0.11.44",
|
|
21
|
+
"hardhat-deploy-ethers": "^0.3.0-beta.13",
|
|
22
|
+
"solidity-bytes-utils": "^0.8.0"
|
|
23
|
+
},
|
|
24
|
+
"peerDependencies": {
|
|
25
|
+
"solidity-bytes-utils": "^0.8.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "restricted"
|
|
29
|
+
},
|
|
30
|
+
"lzVersions": {
|
|
31
|
+
"default": [
|
|
32
|
+
"v2"
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
}
|