-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Overhaul LayerZero collector implementation
- Loading branch information
1 parent
135ccd1
commit 98ef71b
Showing
3 changed files
with
345 additions
and
287 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { keccak256 } from "ethers6"; | ||
|
||
|
||
export interface LayerZeroPacket { | ||
nonce: number; | ||
srcEid: number; | ||
sender: string; | ||
dstEid: number; | ||
receiver: string; | ||
guid: string; | ||
message: string; | ||
} | ||
|
||
export interface LayerZeroHeader { | ||
version: number; | ||
nonce: number; | ||
srcEid: number; | ||
sender: string; | ||
dstEid: number; | ||
receiver: string; | ||
} | ||
|
||
|
||
|
||
export function decodePacket(encodedPacket: string): LayerZeroPacket { | ||
return { | ||
nonce: Number('0x' + encodedPacket.slice(2 + 2, 2 + 2 + 16)), | ||
srcEid: Number('0x' + encodedPacket.slice(2 + 18, 2 + 18 + 8)), | ||
// NOTE: keep only the last 20 bytes of the 'sender' field. | ||
sender: ('0x' + encodedPacket.slice(2 + 26 + 24, 2 + 26 + 64)).toLowerCase(), | ||
dstEid: Number('0x' + encodedPacket.slice(2 + 90, 2 + 90 + 8)), | ||
// NOTE: keep only the last 20 bytes of the 'receiver' field. | ||
receiver: ('0x' + encodedPacket.slice(2 + 98 + 24, 2 + 98 + 64)).toLowerCase(), | ||
guid: '0x' + encodedPacket.slice(2 + 162, 2 + 162 + 64), | ||
message: '0x' + encodedPacket.slice(2 + 226), | ||
}; | ||
} | ||
|
||
export function decodeHeader(encodedHeader: string): LayerZeroHeader { | ||
return { | ||
version: Number('0x' + encodedHeader.slice(2, 2 + 2)), | ||
nonce: Number('0x' + encodedHeader.slice(2 + 2, 2 + 2 + 16)), | ||
srcEid: Number('0x' + encodedHeader.slice(2 + 18, 2 + 18 + 8)), | ||
// NOTE: keep only the last 20 bytes of the 'sender' field. | ||
sender: '0x' + encodedHeader.slice(2 + 26 + 24, 2 + 26 + 64).toLowerCase(), | ||
dstEid: Number('0x' + encodedHeader.slice(2 + 90, 2 + 90 + 8)), | ||
// NOTE: keep only the last 20 bytes of the 'receiver' field. | ||
receiver: '0x' + encodedHeader.slice(2 + 98 + 24, 2 + 98 + 64).toLowerCase(), | ||
} | ||
} | ||
|
||
|
||
export function calculatePayloadHash(guid: string, message: string): string { | ||
const payload = `${guid}${message.slice(2)}`; // 'slice(2)' used to remove the '0x' from the 'message' | ||
return keccak256(payload); | ||
} |
Oops, something went wrong.