Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea #923

Open
wants to merge 1 commit into
base: chore/l1-validator-weight-attempt
Choose a base branch
from
Open

Idea #923

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions examples/warp-tmp-test.ts
Original file line number Diff line number Diff line change
@@ -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<AddressedCall>(
unsignedMessage.payload,
);

console.log(addressedCall);

const warpMessageManager = getWarpMessageManager();
const message = warpMessageManager.unpackPrefix<L1ValidatorWeight>(
addressedCall.payload.bytes,
);

console.log(message);
};

main();
5 changes: 5 additions & 0 deletions src/serializable/codec/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export class Manager {
return unpacker.fromBytes(rest, codec)[0] as FromBytesReturn<T>;
};

unpackPrefix = <T extends Serializable>(buff: Uint8Array): T => {
const [codec, rest] = this.getCodecFromBuffer(buff);
return codec.UnpackPrefix<T>(rest)[0];
};

unpackTransaction = (buff: Uint8Array): Transaction => {
const [codec, rest] = this.getCodecFromBuffer(buff);
return codec.UnpackPrefix<Transaction>(rest)[0];
Expand Down
3 changes: 1 addition & 2 deletions src/serializable/pvm/warp/payload/addressedCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bytes>(payloadBytes.bytes);
const [sourceAddress, payload, rest] = unpack(bytes, [Bytes, Bytes], codec);
return [new AddressedCall(sourceAddress, payload), rest];
}

Expand Down
21 changes: 12 additions & 9 deletions src/serializable/pvm/warp/unsignedMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
);
}
}
Loading