@layerzerolabs/lz-v2-utilities 2.0.11

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/dist/index.cjs ADDED
@@ -0,0 +1,606 @@
1
+ 'use strict';
2
+
3
+ var web3_js = require('@solana/web3.js');
4
+ var ethers = require('ethers');
5
+ var utils = require('ethers/lib/utils');
6
+ var invariant = require('tiny-invariant');
7
+
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
9
+
10
+ var invariant__default = /*#__PURE__*/_interopDefault(invariant);
11
+
12
+ // src/utils/utils.ts
13
+ async function sleep(timeout) {
14
+ await new Promise((resolve) => setTimeout(resolve, timeout));
15
+ }
16
+ function hexZeroPadTo32(addr) {
17
+ return ethers.ethers.utils.hexZeroPad(addr, 32);
18
+ }
19
+ function bytes32ToEthAddress(bytes32) {
20
+ if (bytes32 instanceof Uint8Array) {
21
+ bytes32 = ethers.ethers.utils.hexlify(bytes32);
22
+ }
23
+ return ethers.ethers.utils.getAddress(bytes32.slice(-40));
24
+ }
25
+ function trim0x(str) {
26
+ return str.replace(/^0x/, "");
27
+ }
28
+ function addressToBytes32(address) {
29
+ if (isSolanaAddress(address)) {
30
+ return new web3_js.PublicKey(address).toBytes();
31
+ } else if (address.startsWith("0x") && address.length <= 66) {
32
+ return utils.arrayify(hexZeroPadTo32(address));
33
+ }
34
+ throw new Error("Invalid address");
35
+ }
36
+ var solanaAddressRegex = /^([1-9A-HJ-NP-Za-km-z]{32,44})$/;
37
+ function isSolanaAddress(address) {
38
+ return solanaAddressRegex.test(address);
39
+ }
40
+
41
+ // src/utils/precrime.ts
42
+ var CONFIG_VERSION_OFFSET = 0;
43
+ var MAX_BATCH_SIZE_OFFSET = 2;
44
+ var NUMBER_OF_PEERS_OFFSET = 10;
45
+ var PEERS_OFFSET = 12;
46
+ function parsePrecrimeConfig(precrimeConfig) {
47
+ const data = trim0x(precrimeConfig);
48
+ const version = parseInt(data.slice(0, 4), 16);
49
+ if (version === 1) {
50
+ return parsePrecrimeConfigV1(precrimeConfig);
51
+ } else if (version === 2) {
52
+ return parsePrecrimeConfigV2(precrimeConfig);
53
+ }
54
+ throw new Error(`Unsupported precrime config version: ${version}`);
55
+ }
56
+ function parsePrecrimeConfigV1(precrimeConfig) {
57
+ const data = trim0x(precrimeConfig);
58
+ const version = parseInt(data.slice(0, 4), 16);
59
+ const maxBatchSize = parseInt(data.slice(4, 20), 16);
60
+ const remoteChainsLength = parseInt(data.slice(20, 84), 16);
61
+ const remoteChainsBytes = data.slice(84, 84 + remoteChainsLength * 64);
62
+ const remoteAddressesBytes = data.slice(
63
+ 84 + remoteChainsLength * 64,
64
+ 84 + remoteChainsLength * 64 + remoteChainsLength * 64
65
+ );
66
+ const remoteChainsBytesArray = [];
67
+ const remoteAddressesBytesArray = [];
68
+ let start = 0;
69
+ let end = 64;
70
+ for (let i = 0; i < remoteChainsLength; i++) {
71
+ remoteChainsBytesArray.push(parseInt(remoteChainsBytes.slice(start, end), 16));
72
+ remoteAddressesBytesArray.push(`0x${remoteAddressesBytes.slice(start, end)}`);
73
+ start += 64;
74
+ end += 64;
75
+ }
76
+ return { version, maxBatchSize, remoteEids: remoteChainsBytesArray, remoteAddresses: remoteAddressesBytesArray };
77
+ }
78
+ function parsePrecrimeConfigV2(precrimeConfig) {
79
+ const buffer = Buffer.from(trim0x(precrimeConfig), "hex");
80
+ const version = buffer.readUInt16BE(CONFIG_VERSION_OFFSET);
81
+ const maxBatchSize = buffer.readBigUint64BE(MAX_BATCH_SIZE_OFFSET);
82
+ const peers = new Array();
83
+ if (buffer.length > NUMBER_OF_PEERS_OFFSET) {
84
+ const numOfPeers = buffer.readUInt16BE(NUMBER_OF_PEERS_OFFSET);
85
+ for (let i = 0; i < numOfPeers; i++) {
86
+ const offset = PEERS_OFFSET + i * (4 + 2 * 32);
87
+ const eid = buffer.readUInt32BE(offset);
88
+ const preCrimeAddress = "0x" + buffer.slice(offset + 4, offset + 4 + 32).toString("hex");
89
+ const oappAddress = "0x" + buffer.slice(offset + 4 + 32, offset + 4 + 32 + 32).toString("hex");
90
+ peers.push({
91
+ eid,
92
+ preCrimeAddress,
93
+ oappAddress
94
+ });
95
+ }
96
+ }
97
+ return {
98
+ version,
99
+ maxBatchSize,
100
+ peers
101
+ };
102
+ }
103
+ var parseError = (errorData, intf) => {
104
+ const buildInError = parseBuildInError(errorData);
105
+ if (buildInError !== void 0) {
106
+ return buildInError;
107
+ }
108
+ if (intf) {
109
+ try {
110
+ return intf.parseError(errorData);
111
+ } catch (e) {
112
+ console.error(e);
113
+ }
114
+ }
115
+ };
116
+ function parseBuildInError(errorData) {
117
+ if (errorData.startsWith("0x08c379a0")) {
118
+ const content = `0x${errorData.substring(10)}`;
119
+ const reason = ethers.ethers.utils.defaultAbiCoder.decode(["string"], content);
120
+ return reason[0];
121
+ }
122
+ if (errorData.startsWith("0x4e487b71")) {
123
+ const content = `0x${errorData.substring(10)}`;
124
+ const code = ethers.ethers.utils.defaultAbiCoder.decode(["uint"], content);
125
+ return code[0];
126
+ }
127
+ if (errorData === "0x") {
128
+ return "";
129
+ }
130
+ }
131
+ var MAX_UINT_128 = ethers.ethers.BigNumber.from("0xffffffffffffffffffffffffffffffff");
132
+ function optionsType1(_extraGas) {
133
+ const extraGas = ethers.ethers.BigNumber.from(_extraGas);
134
+ invariant__default.default(extraGas.lte(MAX_UINT_128), "extraGas should be less than MAX_UINT_128");
135
+ return ethers.ethers.utils.solidityPack(["uint16", "uint256"], [1 /* TYPE_1 */, extraGas]);
136
+ }
137
+ function optionsType2(_extraGas, _dstNativeAmt, _dstNativeAddress) {
138
+ const extraGas = ethers.ethers.BigNumber.from(_extraGas);
139
+ invariant__default.default(extraGas.lte(MAX_UINT_128), "extraGas should be less than MAX_UINT_128");
140
+ const dstNativeAmt = ethers.ethers.BigNumber.from(_dstNativeAmt);
141
+ invariant__default.default(dstNativeAmt.lte(MAX_UINT_128), "dstNativeAmt should be less than MAX_UINT_128");
142
+ return ethers.ethers.utils.solidityPack(
143
+ ["uint16", "uint256", "uint256", "bytes"],
144
+ [2 /* TYPE_2 */, ethers.ethers.BigNumber.from(extraGas), ethers.ethers.BigNumber.from(dstNativeAmt), _dstNativeAddress]
145
+ );
146
+ }
147
+ var WorkerId = /* @__PURE__ */ ((WorkerId2) => {
148
+ WorkerId2[WorkerId2["EXECUTOR"] = 1] = "EXECUTOR";
149
+ WorkerId2[WorkerId2["VERIFIER"] = 2] = "VERIFIER";
150
+ WorkerId2[WorkerId2["TREASURY"] = 255] = "TREASURY";
151
+ return WorkerId2;
152
+ })(WorkerId || {});
153
+ var ExecutorOptionType = /* @__PURE__ */ ((ExecutorOptionType2) => {
154
+ ExecutorOptionType2[ExecutorOptionType2["LZ_RECEIVE"] = 1] = "LZ_RECEIVE";
155
+ ExecutorOptionType2[ExecutorOptionType2["NATIVE_DROP"] = 2] = "NATIVE_DROP";
156
+ ExecutorOptionType2[ExecutorOptionType2["COMPOSE"] = 3] = "COMPOSE";
157
+ ExecutorOptionType2[ExecutorOptionType2["ORDERED"] = 4] = "ORDERED";
158
+ return ExecutorOptionType2;
159
+ })(ExecutorOptionType || {});
160
+ var VerifierOptionType = /* @__PURE__ */ ((VerifierOptionType2) => {
161
+ VerifierOptionType2[VerifierOptionType2["PRECRIME"] = 1] = "PRECRIME";
162
+ return VerifierOptionType2;
163
+ })(VerifierOptionType || {});
164
+ var Options = class _Options {
165
+ workerOptions = [];
166
+ // dissuade public instantiation
167
+ constructor() {
168
+ }
169
+ /**
170
+ * Create a new options instance.
171
+ */
172
+ static newOptions() {
173
+ return new _Options();
174
+ }
175
+ /**
176
+ * Create an options instance from a hex string.
177
+ * @param {string} optionsHex The hex string to decode.
178
+ */
179
+ static fromOptions(optionsHex) {
180
+ const options = new _Options();
181
+ const optionsBytes = ethers.ethers.utils.arrayify(optionsHex);
182
+ const optionsType = ethers.ethers.BigNumber.from(optionsBytes.slice(0, 2)).toNumber();
183
+ if (optionsType === 3 /* TYPE_3 */) {
184
+ let cursor = 2;
185
+ while (cursor < optionsBytes.byteLength) {
186
+ const workerId = ethers.ethers.BigNumber.from(optionsBytes.slice(cursor, cursor + 1)).toNumber();
187
+ cursor += 1;
188
+ const size = ethers.ethers.BigNumber.from(optionsBytes.slice(cursor, cursor + 2)).toNumber();
189
+ cursor += 2;
190
+ if (workerId === 1 /* EXECUTOR */) {
191
+ const optionType = ethers.ethers.BigNumber.from(optionsBytes.slice(cursor, cursor + 1)).toNumber();
192
+ cursor += 1;
193
+ const params = optionsBytes.slice(cursor, cursor + size - 1);
194
+ cursor += size - 1;
195
+ options.addOption(workerId, { type: optionType, params: ethers.ethers.utils.hexlify(params) });
196
+ } else if (workerId === 2 /* VERIFIER */) {
197
+ const verifierIdx = ethers.ethers.BigNumber.from(optionsBytes.slice(cursor, cursor + 1)).toNumber();
198
+ cursor += 1;
199
+ const optionType = ethers.ethers.BigNumber.from(optionsBytes.slice(cursor, cursor + 1)).toNumber();
200
+ cursor += 1;
201
+ const params = optionsBytes.slice(cursor, cursor + size - 2);
202
+ cursor += size - 2;
203
+ options.addOption(workerId, {
204
+ type: optionType,
205
+ index: verifierIdx,
206
+ params: ethers.ethers.utils.hexlify(params)
207
+ });
208
+ }
209
+ }
210
+ } else if (optionsType === 2 /* TYPE_2 */) {
211
+ const extraGas = ethers.ethers.BigNumber.from(optionsBytes.slice(2, 34)).toNumber();
212
+ const dstNativeAmt = ethers.ethers.BigNumber.from(optionsBytes.slice(34, 66)).toNumber();
213
+ const dstNativeAddress = ethers.ethers.utils.hexlify(optionsBytes.slice(66, optionsBytes.byteLength));
214
+ options.addExecutorLzReceiveOption(extraGas).addExecutorNativeDropOption(dstNativeAmt, dstNativeAddress);
215
+ } else if (optionsType === 1 /* TYPE_1 */) {
216
+ const extraGas = ethers.ethers.BigNumber.from(optionsBytes.slice(2, 34)).toNumber();
217
+ options.addExecutorLzReceiveOption(extraGas);
218
+ }
219
+ return options;
220
+ }
221
+ /**
222
+ * Add ExecutorOptionType.LZ_RECEIVE option.
223
+ * @param {GasLimit} gasLimit
224
+ * @param {NativeDrop} nativeDrop
225
+ */
226
+ addExecutorLzReceiveOption(gasLimit, nativeDrop = 0) {
227
+ const gasLimitBN = ethers.ethers.BigNumber.from(gasLimit);
228
+ invariant__default.default(gasLimitBN.lte(MAX_UINT_128), "gasLimit shouldn't be greater than MAX_UINT_128");
229
+ const nativeDropBN = ethers.ethers.BigNumber.from(nativeDrop);
230
+ invariant__default.default(nativeDropBN.lte(MAX_UINT_128), "value shouldn't be greater than MAX_UINT_128");
231
+ this.addOption(1 /* EXECUTOR */, {
232
+ type: 1 /* LZ_RECEIVE */,
233
+ params: nativeDropBN.eq(0) ? ethers.ethers.utils.solidityPack(["uint128"], [gasLimitBN]) : ethers.ethers.utils.solidityPack(["uint128", "uint128"], [gasLimitBN, nativeDropBN])
234
+ });
235
+ return this;
236
+ }
237
+ /**
238
+ * Add ExecutorOptionType.NATIVE_DROP option.
239
+ * @param {NativeDrop} nativeDrop
240
+ * @param {string} receiver
241
+ */
242
+ addExecutorNativeDropOption(nativeDrop, receiver) {
243
+ const amountBN = ethers.ethers.BigNumber.from(nativeDrop);
244
+ invariant__default.default(amountBN.lte(MAX_UINT_128), "nativeDrop shouldn't be greater than MAX_UINT_128");
245
+ this.addOption(1 /* EXECUTOR */, {
246
+ type: 2 /* NATIVE_DROP */,
247
+ params: ethers.ethers.utils.solidityPack(["uint128", "bytes32"], [amountBN, hexZeroPadTo32(receiver)])
248
+ });
249
+ return this;
250
+ }
251
+ /**
252
+ * Add ExecutorOptionType.COMPOSE option.
253
+ * @param {number} index
254
+ * @param {GasLimit} gasLimit
255
+ * @param {NativeDrop} nativeDrop
256
+ */
257
+ addExecutorComposeOption(index, gasLimit, nativeDrop = 0) {
258
+ const gasLimitBN = ethers.ethers.BigNumber.from(gasLimit);
259
+ invariant__default.default(gasLimitBN.lte(MAX_UINT_128), "gasLimit shouldn't be greater than MAX_UINT_128");
260
+ const nativeDropBN = ethers.ethers.BigNumber.from(nativeDrop);
261
+ invariant__default.default(nativeDropBN.lte(MAX_UINT_128), "nativeDrop shouldn't be greater than MAX_UINT_128");
262
+ const option = nativeDropBN.gt(0) ? {
263
+ type: 3 /* COMPOSE */,
264
+ params: ethers.ethers.utils.solidityPack(
265
+ ["uint16", "uint128", "uint128"],
266
+ [index, gasLimitBN, nativeDropBN]
267
+ )
268
+ } : {
269
+ type: 3 /* COMPOSE */,
270
+ params: ethers.ethers.utils.solidityPack(["uint16", "uint128"], [index, gasLimitBN])
271
+ };
272
+ this.addOption(1 /* EXECUTOR */, option);
273
+ return this;
274
+ }
275
+ /**
276
+ * Add ExecutorOptionType.ORDERED option.
277
+ */
278
+ addExecutorOrderedExecutionOption() {
279
+ this.addOption(1 /* EXECUTOR */, {
280
+ type: 4 /* ORDERED */,
281
+ params: "0x"
282
+ });
283
+ return this;
284
+ }
285
+ /**
286
+ * Add VerifierOptionType.PRECRIME option.
287
+ * @param {number} verifierIdx
288
+ */
289
+ addVerifierPrecrimeOption(verifierIdx) {
290
+ this.addOption(2 /* VERIFIER */, {
291
+ type: 1 /* PRECRIME */,
292
+ index: verifierIdx,
293
+ params: "0x"
294
+ });
295
+ return this;
296
+ }
297
+ /**
298
+ * Serialize Options to hex string.
299
+ */
300
+ toHex() {
301
+ let hex = ethers.ethers.utils.solidityPack(["uint16"], [3 /* TYPE_3 */]);
302
+ this.workerOptions.forEach((w) => {
303
+ for (const option of w.options) {
304
+ if (w.workerId === 1 /* EXECUTOR */) {
305
+ hex += trim0x(
306
+ ethers.ethers.utils.solidityPack(
307
+ ["uint8", "uint16", "uint8", "bytes"],
308
+ [w.workerId, trim0x(option.params).length / 2 + 1, option.type, option.params]
309
+ )
310
+ );
311
+ } else if (w.workerId === 2 /* VERIFIER */) {
312
+ const verifierOption = option;
313
+ hex += trim0x(
314
+ ethers.ethers.utils.solidityPack(
315
+ ["uint8", "uint16", "uint8", "uint8", "bytes"],
316
+ [
317
+ w.workerId,
318
+ trim0x(option.params).length / 2 + 2,
319
+ verifierOption.index,
320
+ verifierOption.type,
321
+ verifierOption.params
322
+ ]
323
+ )
324
+ );
325
+ }
326
+ }
327
+ });
328
+ return hex;
329
+ }
330
+ /**
331
+ * Serialize Options to Uint8Array.
332
+ */
333
+ toBytes() {
334
+ return ethers.ethers.utils.arrayify(this.toHex());
335
+ }
336
+ addOption(workerId, option) {
337
+ const worker = this.workerOptions.find((w) => w.workerId === workerId);
338
+ if (worker) {
339
+ worker.options.push(option);
340
+ } else {
341
+ this.workerOptions.push({ workerId, options: [option] });
342
+ }
343
+ }
344
+ /**
345
+ * Decode ExecutorOptionType.LZ_RECEIVE option. Returns undefined if the option is not present.
346
+ */
347
+ decodeExecutorLzReceiveOption() {
348
+ const options = this.findOptions(1 /* EXECUTOR */, 1 /* LZ_RECEIVE */);
349
+ if (!options || options.length === 0) {
350
+ return;
351
+ }
352
+ let totalGas = ethers.ethers.BigNumber.from(0).toBigInt();
353
+ let totalValue = ethers.ethers.BigNumber.from(0).toBigInt();
354
+ for (const option of options) {
355
+ const buffer = Buffer.from(trim0x(option.params), "hex");
356
+ const gas = ethers.ethers.BigNumber.from(buffer.subarray(0, 16)).toBigInt();
357
+ if (buffer.length === 16) {
358
+ return { gas, value: ethers.ethers.BigNumber.from(0).toBigInt() };
359
+ }
360
+ const value = ethers.ethers.BigNumber.from(buffer.subarray(16, 32)).toBigInt();
361
+ totalGas = totalGas + gas;
362
+ totalValue = totalValue + value;
363
+ }
364
+ return { gas: totalGas, value: totalValue };
365
+ }
366
+ /**
367
+ * Decode ExecutorOptionType.NATIVE_DROP options. Returns undefined if the options is not present.
368
+ */
369
+ decodeExecutorNativeDropOption() {
370
+ const options = this.findOptions(1 /* EXECUTOR */, 2 /* NATIVE_DROP */);
371
+ if (!options || options.length === 0) {
372
+ return [];
373
+ }
374
+ const results = options.reduce((acc, cur) => {
375
+ const buffer = Buffer.from(trim0x(cur.params), "hex");
376
+ const amount = ethers.ethers.BigNumber.from(buffer.subarray(0, 16)).toBigInt();
377
+ const receiver = ethers.ethers.utils.hexlify(buffer.subarray(16, 48));
378
+ if (acc[receiver]) {
379
+ acc[receiver]["amount"] = acc[receiver].amount + amount;
380
+ } else {
381
+ acc[receiver] = { amount, receiver };
382
+ }
383
+ return acc;
384
+ }, {});
385
+ return Object.values(results);
386
+ }
387
+ /**
388
+ * Decode ExecutorOptionType.COMPOSE options. Returns undefined if the options is not present.
389
+ */
390
+ decodeExecutorComposeOption() {
391
+ const options = this.findOptions(1 /* EXECUTOR */, 3 /* COMPOSE */);
392
+ if (!options || options.length === 0) {
393
+ return [];
394
+ }
395
+ const results = options.reduce(
396
+ (acc, cur) => {
397
+ const buffer = Buffer.from(trim0x(cur.params), "hex");
398
+ const index = ethers.ethers.BigNumber.from(buffer.subarray(0, 2)).toNumber();
399
+ const gas = ethers.ethers.BigNumber.from(buffer.subarray(2, 18)).toBigInt();
400
+ const value = (buffer.length === 34 ? ethers.ethers.BigNumber.from(buffer.subarray(18, 34)) : ethers.ethers.BigNumber.from(0)).toBigInt();
401
+ if (acc[index]) {
402
+ acc[index]["gas"] = acc[index].gas + gas;
403
+ acc[index]["value"] = acc[index].value + value;
404
+ } else {
405
+ acc[index] = { index, gas, value };
406
+ }
407
+ return acc;
408
+ },
409
+ {}
410
+ );
411
+ return Object.values(results);
412
+ }
413
+ /**
414
+ * Decode ExecutorOptionType.ORDERED options. Returns undefined if the options is not present.
415
+ */
416
+ decodeExecutorOrderedExecutionOption() {
417
+ const option = this.findOptions(1 /* EXECUTOR */, 4 /* ORDERED */);
418
+ return option !== void 0;
419
+ }
420
+ findOptions(workerId, optionType) {
421
+ const worker = this.workerOptions.find((w) => w.workerId === workerId);
422
+ if (worker) {
423
+ if (optionType === 4 /* ORDERED */) {
424
+ return worker.options.find((o) => o.type === optionType);
425
+ }
426
+ return worker.options.filter((o) => o.type === optionType);
427
+ }
428
+ }
429
+ /**
430
+ * Find VerifierOption by verifierIdx and optionType. Returns undefined if the option is not present.
431
+ * @param {number} verifierIdx
432
+ * @param {number} optionType
433
+ */
434
+ findVerifierOption(verifierIdx, optionType) {
435
+ const worker = this.workerOptions.find((w) => w.workerId === 2 /* VERIFIER */);
436
+ if (worker) {
437
+ const opt = worker.options.find((o) => o.type === optionType && o.index === verifierIdx);
438
+ if (opt) {
439
+ return opt;
440
+ }
441
+ }
442
+ }
443
+ };
444
+
445
+ // src/model/packet.ts
446
+ function packetToMessageOrigin(packet) {
447
+ return {
448
+ srcEid: packet.srcEid,
449
+ sender: packet.sender,
450
+ nonce: packet.nonce
451
+ };
452
+ }
453
+ var PACKET_VERSION_OFFSET = 0;
454
+ var NONCE_OFFSET = 1;
455
+ var SRC_CHAIN_OFFSET = 9;
456
+ var SRC_ADDRESS_OFFSET = 13;
457
+ var DST_CHAIN_OFFSET = 45;
458
+ var DST_ADDRESS_OFFSET = 49;
459
+ var GUID_OFFSET = 81;
460
+ var MESSAGE_OFFSET = 113;
461
+ var PacketV1Codec = class _PacketV1Codec {
462
+ buffer;
463
+ static from(payloadEncoded) {
464
+ return new _PacketV1Codec(payloadEncoded);
465
+ }
466
+ static fromBytes(payload) {
467
+ return new _PacketV1Codec("0x" + Buffer.from(payload).toString("hex"));
468
+ }
469
+ constructor(payloadEncoded) {
470
+ this.buffer = Buffer.from(trim0x(payloadEncoded), "hex");
471
+ }
472
+ /**
473
+ * encode packet to hex string
474
+ */
475
+ static encode(packet) {
476
+ const message = trim0x(packet.message);
477
+ const buffer = Buffer.alloc(MESSAGE_OFFSET + message.length / 2);
478
+ buffer.writeUInt8(packet.version, PACKET_VERSION_OFFSET);
479
+ buffer.writeBigUInt64BE(BigInt(packet.nonce), NONCE_OFFSET);
480
+ buffer.writeUInt32BE(packet.srcEid, SRC_CHAIN_OFFSET);
481
+ buffer.write(trim0x(hexZeroPadTo32(packet.sender)), SRC_ADDRESS_OFFSET, 32, "hex");
482
+ buffer.writeUInt32BE(packet.dstEid, DST_CHAIN_OFFSET);
483
+ buffer.write(trim0x(hexZeroPadTo32(packet.receiver)), DST_ADDRESS_OFFSET, 32, "hex");
484
+ buffer.write(trim0x(packet.guid), GUID_OFFSET, 32, "hex");
485
+ buffer.write(message, MESSAGE_OFFSET, message.length / 2, "hex");
486
+ return "0x" + buffer.toString("hex");
487
+ }
488
+ version() {
489
+ return this.buffer.readUInt8(PACKET_VERSION_OFFSET);
490
+ }
491
+ nonce() {
492
+ return this.buffer.readBigUint64BE(NONCE_OFFSET).toString();
493
+ }
494
+ srcEid() {
495
+ return this.buffer.readUint32BE(SRC_CHAIN_OFFSET);
496
+ }
497
+ sender() {
498
+ return "0x" + this.buffer.slice(SRC_ADDRESS_OFFSET, DST_CHAIN_OFFSET).toString("hex");
499
+ }
500
+ senderAddressB20() {
501
+ return bytes32ToEthAddress(this.sender());
502
+ }
503
+ dstEid() {
504
+ return this.buffer.readUint32BE(DST_CHAIN_OFFSET);
505
+ }
506
+ receiver() {
507
+ return "0x" + this.buffer.slice(DST_ADDRESS_OFFSET, GUID_OFFSET).toString("hex");
508
+ }
509
+ receiverAddressB20() {
510
+ return bytes32ToEthAddress(this.receiver());
511
+ }
512
+ guid() {
513
+ return "0x" + this.buffer.slice(GUID_OFFSET, MESSAGE_OFFSET).toString("hex");
514
+ }
515
+ message() {
516
+ return "0x" + this.buffer.slice(MESSAGE_OFFSET).toString("hex");
517
+ }
518
+ payloadHash() {
519
+ return keccak256(this.payload());
520
+ }
521
+ payload() {
522
+ return "0x" + this.buffer.slice(GUID_OFFSET).toString("hex");
523
+ }
524
+ header() {
525
+ return "0x" + this.buffer.slice(0, GUID_OFFSET).toString("hex");
526
+ }
527
+ headerHash() {
528
+ return keccak256(this.header());
529
+ }
530
+ /**
531
+ * deserialize packet from hex string
532
+ * @deprecated use toPacket instead
533
+ */
534
+ decode() {
535
+ return this.toPacket();
536
+ }
537
+ toPacket() {
538
+ return {
539
+ version: this.version(),
540
+ nonce: this.nonce(),
541
+ srcEid: this.srcEid(),
542
+ sender: this.sender(),
543
+ dstEid: this.dstEid(),
544
+ receiver: this.receiver(),
545
+ guid: this.guid(),
546
+ message: this.message(),
547
+ // derived
548
+ payload: this.payload()
549
+ };
550
+ }
551
+ };
552
+ function calculateGuid(packetHead) {
553
+ return keccak256(
554
+ ethers.ethers.utils.solidityPack(
555
+ ["uint64", "uint32", "bytes32", "uint32", "bytes32"],
556
+ [
557
+ ethers.ethers.BigNumber.from(packetHead.nonce),
558
+ packetHead.srcEid,
559
+ addressToBytes32(packetHead.sender),
560
+ packetHead.dstEid,
561
+ addressToBytes32(packetHead.receiver)
562
+ ]
563
+ )
564
+ );
565
+ }
566
+ function keccak256(message) {
567
+ return ethers.ethers.utils.keccak256(message);
568
+ }
569
+
570
+ // src/codec/packet-serializer.ts
571
+ var PacketSerializer = class {
572
+ static serialize(packet) {
573
+ return PacketV1Codec.encode(packet);
574
+ }
575
+ static deserialize(bytesLike) {
576
+ let codec;
577
+ if (bytesLike instanceof Uint8Array) {
578
+ codec = PacketV1Codec.fromBytes(bytesLike);
579
+ } else {
580
+ codec = PacketV1Codec.from(bytesLike);
581
+ }
582
+ return codec.toPacket();
583
+ }
584
+ };
585
+
586
+ exports.ExecutorOptionType = ExecutorOptionType;
587
+ exports.Options = Options;
588
+ exports.PacketSerializer = PacketSerializer;
589
+ exports.PacketV1Codec = PacketV1Codec;
590
+ exports.VerifierOptionType = VerifierOptionType;
591
+ exports.WorkerId = WorkerId;
592
+ exports.addressToBytes32 = addressToBytes32;
593
+ exports.bytes32ToEthAddress = bytes32ToEthAddress;
594
+ exports.calculateGuid = calculateGuid;
595
+ exports.hexZeroPadTo32 = hexZeroPadTo32;
596
+ exports.isSolanaAddress = isSolanaAddress;
597
+ exports.keccak256 = keccak256;
598
+ exports.optionsType1 = optionsType1;
599
+ exports.optionsType2 = optionsType2;
600
+ exports.packetToMessageOrigin = packetToMessageOrigin;
601
+ exports.parseError = parseError;
602
+ exports.parsePrecrimeConfig = parsePrecrimeConfig;
603
+ exports.sleep = sleep;
604
+ exports.trim0x = trim0x;
605
+ //# sourceMappingURL=out.js.map
606
+ //# sourceMappingURL=index.cjs.map