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

Bump @cartesi/rollups to 2.0.0-rc.3 #16

Merged
merged 1 commit into from
Jun 12, 2024
Merged
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
"test": "jest"
},
"dependencies": {
"@cartesi/rollups": "2.0.0-rc.2",
"@cartesi/rollups": "2.0.0-rc.3",
"@commander-js/extra-typings": "^12.0.1",
"commander": "^12.0.0",
"ethers": "^6.11.1",
Expand Down
59 changes: 49 additions & 10 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Input,
Notice,
Voucher,
DelegateCallVoucher,
} from "../src";

import { version } from "../package.json";
Expand Down Expand Up @@ -134,6 +135,11 @@ encodeCommand
"the timestamp of the block",
parseBigInt,
)
.requiredOption(
"--prev-randao <uint256>",
"the latest RANDAO mix of the post beacon state of the previous block",
parseBigInt,
)
.requiredOption(
"--index <uint256>",
"the index of the input in the input box",
Expand Down Expand Up @@ -179,6 +185,25 @@ encodeCommand
writeHexToStdout(encodeOutputBlob(notice), binary);
});

encodeCommand
.command("delegatecallvoucher")
.description("Encodes a delegate call voucher blob")
.requiredOption(
"--destination <address>",
"the destination address",
parseAddress,
)
.requiredOption("--payload <bytes>", "the destination address", parseHex)
.option("-b, --binary", "write to stdout as binary data", false)
.action((options) => {
const delegatecallvoucher: DelegateCallVoucher = {
type: "delegatecallvoucher",
...options,
};
const { binary } = options;
writeHexToStdout(encodeOutputBlob(delegatecallvoucher), binary);
});

const decodeCommand = program.command("decode");

decodeCommand.description("Decodes a blob");
Expand Down
33 changes: 32 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Input {
msgSender: Address;
blockNumber: bigint;
blockTimestamp: bigint;
prevRandao: bigint;
index: bigint;
payload: Hex;
}
Expand All @@ -19,6 +20,7 @@ export const encodeInputBlob = (input: Input): Hex => {
msgSender,
blockNumber,
blockTimestamp,
prevRandao,
index,
payload,
} = input;
Expand All @@ -32,6 +34,7 @@ export const encodeInputBlob = (input: Input): Hex => {
msgSender,
blockNumber,
blockTimestamp,
prevRandao,
index,
payload,
],
Expand All @@ -54,6 +57,7 @@ export const decodeInputBlob = (blob: Hex): Input => {
msgSender,
blockNumber,
blockTimestamp,
prevRandao,
index,
payload,
] = args;
Expand All @@ -64,6 +68,7 @@ export const decodeInputBlob = (blob: Hex): Input => {
msgSender,
blockNumber,
blockTimestamp,
prevRandao,
index,
payload,
};
Expand All @@ -83,7 +88,13 @@ export interface Voucher {
payload: Hex;
}

export type Output = Notice | Voucher;
export interface DelegateCallVoucher {
type: "delegatecallvoucher";
destination: Address;
payload: Hex;
}

export type Output = Notice | Voucher | DelegateCallVoucher;

export const encodeOutputBlob = (output: Output): Hex => {
switch (output.type) {
Expand All @@ -107,6 +118,17 @@ export const encodeOutputBlob = (output: Output): Hex => {
args: [destination, value, payload],
});

return blob;
}
case "delegatecallvoucher": {
const { destination, payload } = output;

const blob = encodeFunctionData({
abi: Outputs__factory.abi,
functionName: "DelegateCallVoucher",
args: [destination, payload],
});

return blob;
}
}
Expand Down Expand Up @@ -137,5 +159,14 @@ export const decodeOutputBlob = (blob: Hex): Output => {
payload,
};
}
case "DelegateCallVoucher": {
const [destination, payload] = args;

return {
type: "delegatecallvoucher",
destination,
payload,
};
}
}
};
52 changes: 50 additions & 2 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { describe, expect, test } from "@jest/globals";
describe("decode/encode", () => {
test("input", () => {
const blob =
"0xcc7dee1f000000000000000000000000000000000000000000000000000000000000000100000000000000000000000070ac08179605af2d9e75782b8decdd3c22aa4d0c000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266000000000000000000000000000000000000000000000000000000000129bbad0000000000000000000000000000000000000000000000000000000066019521000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000004deadbeef00000000000000000000000000000000000000000000000000000000";
"0x415bf363000000000000000000000000000000000000000000000000000000000000000100000000000000000000000070ac08179605af2d9e75782b8decdd3c22aa4d0c000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266000000000000000000000000000000000000000000000000000000000129bbad00000000000000000000000000000000000000000000000000000000660195215a41539c3688747a1a8c7811b98b0427331ff73aab018eb5c9921993d617f314000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000004deadbeef00000000000000000000000000000000000000000000000000000000";

const input = decodeInputBlob(blob);

Expand All @@ -25,13 +25,16 @@ describe("decode/encode", () => {
);
expect(input.blockNumber).toEqual(19512237n);
expect(input.blockTimestamp).toEqual(1711379745n);
expect(input.prevRandao).toEqual(
40823578488146031703637781058841789769586951870728503003341100870835983872788n,
);
expect(input.index).toEqual(42n);
expect(input.payload).toEqual("0xdeadbeef");
});

test("input with empty payload", () => {
const blob =
"0xcc7dee1f000000000000000000000000000000000000000000000000000000000000000100000000000000000000000070ac08179605af2d9e75782b8decdd3c22aa4d0c000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266000000000000000000000000000000000000000000000000000000000129bbad0000000000000000000000000000000000000000000000000000000066019521000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000000e00000000000000000000000000000000000000000000000000000000000000000";
"0x415bf363000000000000000000000000000000000000000000000000000000000000000100000000000000000000000070ac08179605af2d9e75782b8decdd3c22aa4d0c000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb92266000000000000000000000000000000000000000000000000000000000129bbad00000000000000000000000000000000000000000000000000000000660195215a41539c3688747a1a8c7811b98b0427331ff73aab018eb5c9921993d617f314000000000000000000000000000000000000000000000000000000000000002a00000000000000000000000000000000000000000000000000000000000001000000000000000000000000000000000000000000000000000000000000000000";

const input = decodeInputBlob(blob);

Expand All @@ -46,6 +49,9 @@ describe("decode/encode", () => {
);
expect(input.blockNumber).toEqual(19512237n);
expect(input.blockTimestamp).toEqual(1711379745n);
expect(input.prevRandao).toEqual(
40823578488146031703637781058841789769586951870728503003341100870835983872788n,
);
expect(input.index).toEqual(42n);
expect(input.payload).toEqual("0x");
});
Expand Down Expand Up @@ -129,4 +135,46 @@ describe("decode/encode", () => {
throw new Error("expected output to be a voucher");
}
});

test("delegate call voucher", () => {
const blob =
"0x10321e8b000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb9226600000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000003fafafa0000000000000000000000000000000000000000000000000000000000";

const output = decodeOutputBlob(blob);

expect(encodeOutputBlob(output)).toEqual(blob);

switch (output.type) {
case "delegatecallvoucher": {
expect(output.destination).toEqual(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
);
expect(output.payload).toEqual("0xfafafa");
break;
}
default:
throw new Error("expected output to be a voucher");
}
});

test("delegate call voucher with empty payload", () => {
const blob =
"0x10321e8b000000000000000000000000f39fd6e51aad88f6f4ce6ab8827279cfffb9226600000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000000";

const output = decodeOutputBlob(blob);

expect(encodeOutputBlob(output)).toEqual(blob);

switch (output.type) {
case "delegatecallvoucher": {
expect(output.destination).toEqual(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
);
expect(output.payload).toEqual("0x");
break;
}
default:
throw new Error("expected output to be a voucher");
}
});
});