oasisprotocol / sapphire-paratime

Oasis Sapphire - the confidential EVM-compatible ParaTime for the Oasis Network
https://oasisprotocol.org/sapphire
Apache License 2.0
39 stars 27 forks source link

Fix Viem upstream: transaction serializers should support async #391

Open CedarMist opened 2 months ago

CedarMist commented 2 months ago

As per: https://github.com/wevm/viem/discussions/2696

Tried to fix upstream

CedarMist commented 1 month ago

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:

https://github.com/oasisprotocol/sapphire-paratime/blob/f708f3912424c84a6f03f87b9f6f928093da036a/integrations/viem-v2/src/index.ts#L125-L134

However, this isn't ideal.

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

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