From 38fcfe3022b2c3fa8d6dbe4540e3f11b89dfba7f Mon Sep 17 00:00:00 2001 From: bferenc Date: Fri, 22 Nov 2024 18:17:40 +0100 Subject: [PATCH] feat: parse individually --- examples/warp-tmp-test.ts | 37 +++++++++++++++++++ src/serializable/codec/manager.ts | 5 +++ .../pvm/warp/payload/addressedCall.ts | 3 +- src/serializable/pvm/warp/unsignedMessage.ts | 21 ++++++----- 4 files changed, 55 insertions(+), 11 deletions(-) create mode 100644 examples/warp-tmp-test.ts diff --git a/examples/warp-tmp-test.ts b/examples/warp-tmp-test.ts new file mode 100644 index 000000000..401894300 --- /dev/null +++ b/examples/warp-tmp-test.ts @@ -0,0 +1,37 @@ +import type { L1ValidatorWeight } from '../src/serializable/pvm/warp'; +import type { AddressedCall } from '../src/serializable/pvm/warp'; +import { + getWarpManager, + WarpUnsignedMessage, +} from '../src/serializable/pvm/warp'; +import { getWarpMessageManager } from '../src/serializable/pvm/warp/message/'; +import { getWarpPayloadManager } from '../src/serializable/pvm/warp/payload'; +import { hexToBuffer } from '../src/utils/buffer'; + +const main = () => { + const unsignedMessageHex = + '0000004c00000000000000000000000000000000000000000000000000000000000000000000000000010000000000000036000000000003ab1644a54f81d7722005b0d5c20d4d1fb072f826be080895d604b74e2336bda2000000000000000e000000000000000f'; + const unsignedMessageBytes = hexToBuffer(unsignedMessageHex); + const [unsignedMessage] = WarpUnsignedMessage.fromBytes( + unsignedMessageBytes, + getWarpManager().getDefaultCodec(), + ); + + console.log(unsignedMessage); + + const warpPayloadManager = getWarpPayloadManager(); + const addressedCall = warpPayloadManager.unpackPrefix( + unsignedMessage.payload, + ); + + console.log(addressedCall); + + const warpMessageManager = getWarpMessageManager(); + const message = warpMessageManager.unpackPrefix( + addressedCall.payload.bytes, + ); + + console.log(message); +}; + +main(); diff --git a/src/serializable/codec/manager.ts b/src/serializable/codec/manager.ts index 582a19205..cea24c9ea 100644 --- a/src/serializable/codec/manager.ts +++ b/src/serializable/codec/manager.ts @@ -29,6 +29,11 @@ export class Manager { return unpacker.fromBytes(rest, codec)[0] as FromBytesReturn; }; + unpackPrefix = (buff: Uint8Array): T => { + const [codec, rest] = this.getCodecFromBuffer(buff); + return codec.UnpackPrefix(rest)[0]; + }; + unpackTransaction = (buff: Uint8Array): Transaction => { const [codec, rest] = this.getCodecFromBuffer(buff); return codec.UnpackPrefix(rest)[0]; diff --git a/src/serializable/pvm/warp/payload/addressedCall.ts b/src/serializable/pvm/warp/payload/addressedCall.ts index ae01f0f82..ccdcd3239 100644 --- a/src/serializable/pvm/warp/payload/addressedCall.ts +++ b/src/serializable/pvm/warp/payload/addressedCall.ts @@ -17,8 +17,7 @@ export class AddressedCall { bytes: Uint8Array, codec: Codec, ): [AddressedCall, Uint8Array] { - const [sourceAddress, payloadBytes] = unpack(bytes, [Bytes, Bytes], codec); - const [payload, rest] = codec.UnpackPrefix(payloadBytes.bytes); + const [sourceAddress, payload, rest] = unpack(bytes, [Bytes, Bytes], codec); return [new AddressedCall(sourceAddress, payload), rest]; } diff --git a/src/serializable/pvm/warp/unsignedMessage.ts b/src/serializable/pvm/warp/unsignedMessage.ts index cc7c14399..93a4f4a01 100644 --- a/src/serializable/pvm/warp/unsignedMessage.ts +++ b/src/serializable/pvm/warp/unsignedMessage.ts @@ -3,7 +3,8 @@ import type { Codec } from '../../codec'; import { serializable } from '../../common/types'; import { TypeSymbols } from '../../constants'; import { Id } from '../../fxs/common'; -import { Bytes, Int } from '../../primitives'; +import { Int } from '../../primitives'; +import { concatBytes } from '../../../utils/buffer'; @serializable() export class WarpUnsignedMessage { @@ -12,23 +13,25 @@ export class WarpUnsignedMessage { constructor( public readonly networkId: Int, public readonly sourceChainId: Id, - public readonly payload: Bytes, + public readonly payload: Uint8Array, ) {} static fromBytes( bytes: Uint8Array, codec: Codec, ): [WarpUnsignedMessage, Uint8Array] { - const [networkId, sourceChainId, payload, rest] = unpack( - bytes, - [Int, Id, Bytes], - codec, - ); + const [networkId, sourceChainId, payload] = unpack(bytes, [Int, Id], codec); - return [new WarpUnsignedMessage(networkId, sourceChainId, payload), rest]; + return [ + new WarpUnsignedMessage(networkId, sourceChainId, payload), + new Uint8Array([]), + ]; } toBytes(codec: Codec) { - return pack([this.networkId, this.sourceChainId, this.payload], codec); + return concatBytes( + pack([this.networkId, this.sourceChainId], codec), + this.payload, + ); } }