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

fix(common): kms correctly serializes transactions #2721

Merged
merged 17 commits into from
Apr 25, 2024

Conversation

yonadaa
Copy link
Contributor

@yonadaa yonadaa commented Apr 24, 2024

While testing the KMS deployer in #2704 I realised that it's transactions were rejected by the RPC because they were not serialised properly. This PR fixes the KMS account by more closely mirroring the Viem signTransaction logic.

@yonadaa yonadaa requested review from alvrs and holic as code owners April 24, 2024 15:42
Copy link

changeset-bot bot commented Apr 24, 2024

⚠️ No Changeset found

Latest commit: 664c49a

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes changesets to release 24 packages
Name Type
@latticexyz/common Patch
@latticexyz/block-logs-stream Patch
@latticexyz/cli Patch
@latticexyz/config Patch
@latticexyz/dev-tools Patch
@latticexyz/faucet Patch
@latticexyz/protocol-parser Patch
@latticexyz/query Patch
@latticexyz/store-indexer Patch
@latticexyz/store-sync Patch
@latticexyz/store Patch
@latticexyz/world-modules Patch
@latticexyz/world Patch
mock-game-contracts Patch
@latticexyz/react Patch
@latticexyz/abi-ts Patch
create-mud Patch
@latticexyz/gas-report Patch
@latticexyz/recs Patch
@latticexyz/schema-type Patch
@latticexyz/services Patch
solhint-config-mud Patch
solhint-plugin-mud Patch
@latticexyz/utils Patch

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

holic
holic previously approved these changes Apr 24, 2024
Comment on lines 38 to 48
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const signableTransaction = (() => {
// For EIP-4844 Transactions, we want to sign the transaction payload body (tx_payload_body) without the sidecars (ie. without the network wrapper).
// See: https://github.com/ethereum/EIPs/blob/e00f4daa66bd56e2dbd5f1d36d09fd613811a48b/EIPS/eip-4844.md#networking
if (transaction.type === "eip4844")
return {
...transaction,
sidecars: false,
};
return transaction;
})();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooc why this and not

Suggested change
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const signableTransaction = (() => {
// For EIP-4844 Transactions, we want to sign the transaction payload body (tx_payload_body) without the sidecars (ie. without the network wrapper).
// See: https://github.com/ethereum/EIPs/blob/e00f4daa66bd56e2dbd5f1d36d09fd613811a48b/EIPS/eip-4844.md#networking
if (transaction.type === "eip4844")
return {
...transaction,
sidecars: false,
};
return transaction;
})();
// For EIP-4844 Transactions, we want to sign the transaction payload body (tx_payload_body) without the sidecars (ie. without the network wrapper).
// See: https://github.com/ethereum/EIPs/blob/e00f4daa66bd56e2dbd5f1d36d09fd613811a48b/EIPS/eip-4844.md#networking
const signableTransaction = transaction.type === "eip4844" ? { ...transaction, sidecars: false } : transaction;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied verbatim from the Viem function! It could be more encapsulated imo: https://github.com/wevm/viem/blob/main/src/accounts/utils/signTransaction.ts#L53-L62

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh got it!

maybe worth a note above about keeping this logic aligned with viem internals?

address,
});

return signature;
return serializer(transaction, signature);
Copy link
Member

@holic holic Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to confirm, we don't want to use signableTransaction here?

I see now we're following viem

Copy link
Contributor Author

@yonadaa yonadaa Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, again this is from viem. I think it's because the data you sign should (or shouldn't, idk) include some fields that are in the full transaction data.

import { signWithKms } from "./signWithKms";
import { getAddressFromKms } from "./getAddressFromKms";

export type kmsKeyToAccountOptions = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type kmsKeyToAccountOptions = {
export type KmsKeyToAccountOptions = {

@@ -1 +1 @@
export { createKmsAccount, type CreateKmsAccountOptions, type KMSAccount } from "../account/kms/createKmsAccount";
export { kmsKeyToAccount, type kmsKeyToAccountOptions, type KMSAccount } from "../account/kms/kmsKeyToAddress";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export { kmsKeyToAccount, type kmsKeyToAccountOptions, type KMSAccount } from "../account/kms/kmsKeyToAddress";
export { KmsKeyToAccount, type kmsKeyToAccountOptions, type KmsAccount } from "../account/kms/kmsKeyToAddress";

client?: KMSClient;
};

export type KMSAccount = LocalAccount<"aws-kms">;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export type KMSAccount = LocalAccount<"aws-kms">;
export type KmsAccount = LocalAccount<"aws-kms">;

export async function kmsKeyToAccount({
keyId,
client = new KMSClient(),
}: kmsKeyToAccountOptions): Promise<KMSAccount> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}: kmsKeyToAccountOptions): Promise<KMSAccount> {
}: kmsKeyToAccountOptions): Promise<KmsAccount> {

Comment on lines 125 to 132
let balance = await getBalance(adminClient, { address: account.address });
expect(balance).toEqual(0n);

let tx = await adminClient.sendTransaction({ to: account.address, value: parseEther("1") });
await waitForTransaction(tx);

balance = await getBalance(adminClient, { address: account.address });
expect(balance).toEqual(1000000000000000000n);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're using a mix of tree shakable and not-tree shakable actions, would be nice to consolidate (ideally on tree shakable)

and if we're doing all tree shakable, might make sense to just use createClient not createWalletClient

const adminClient = createWalletClient({
chain: foundry,
transport: http(anvilRpcUrl),
account: privateKeyToAccount("0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna make a note here about this being a default anvil user?

can also use testClient to set balance of ~any account

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testClient would be more intuitive, the funding stuff is a means to an end

@holic holic merged commit 182d706 into main Apr 25, 2024
12 checks passed
@holic holic deleted the yonadaaa/fix-kms-transaction branch April 25, 2024 12:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants