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 Viem upstream: transaction serializers should support async #391

Open
CedarMist opened this issue Sep 10, 2024 · 1 comment
Open

Fix Viem upstream: transaction serializers should support async #391

CedarMist opened this issue Sep 10, 2024 · 1 comment
Labels
blocked Pull requests that are blocked by another issue bug dependencies Pull requests that update a dependency file javascript Pull requests that update JavaScript code

Comments

@CedarMist
Copy link
Member

CedarMist commented Sep 10, 2024

As per: wevm/viem#2696

Tried to fix upstream

@CedarMist CedarMist added dependencies Pull requests that update a dependency file blocked Pull requests that are blocked by another issue javascript Pull requests that update JavaScript code bug labels Sep 10, 2024
@CedarMist
Copy link
Member Author

A suggestion was made on how to implement this cleanly with Viem:

import {
  keccak256,
  serializeTransaction,
  type TransactionSerializable,
  type Hex,
  type Signature,
} from "viem";
import { sign, privateKeyToAccount, toAccount } from "viem/accounts";

async function serializerAsync(
  transaction: TransactionSerializable,
  signature?: Signature | undefined,
): Promise<Hex> {
  // ... perform async operations
  return serializeTransaction(transaction, signature);
}

const privateKey = "0x..." as const;

const account = toAccount({
  ...privateKeyToAccount(privateKey),
  async signTransaction(transaction) {
    const signature = await sign({
      hash: keccak256(await serializerAsync(transaction)),
      privateKey,
    });
    return serializerAsync(transaction, signature);
  },
});

Currently the Viem integration has a workaround to fetch the calldata public keys in the background because the transaction serializer isn't async:

const fetcher = new KeyFetcher();
const provider = client as EthereumProvider;
await fetcher.fetch(provider);
// The fetcher runs in the background, routinely fetching the keys
// This means when the serializer requests a calldata public key one will
// have been retrieved pre-emptively.
const intervalId: NodeJS.Timeout | number = setInterval(async () => {
await fetcher.fetch(provider);
}, fetcher.timeoutMilliseconds);

However, this isn't ideal.

There are other sore-spots in the Viem & Wagmi integrations:

  • Wagmi supports support EIP-6963 via the MIPD package: https://github.com/wevm/mipd
    • Couldn't figure out a clean way of auto-wrapping via MIPD, so eth_call and eth_estimateGas wouldn't be encrypted as they aren't wrapped.
  • TBD: remember more

Overall, we can't just focus on Viem, need to make sure it works well with Wagmi in the way people commonly use it, and with the third-party RPC providers & SDKs like WalletConnect & RainbowKit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
blocked Pull requests that are blocked by another issue bug dependencies Pull requests that update a dependency file javascript Pull requests that update JavaScript code
Projects
None yet
Development

No branches or pull requests

1 participant