Skip to content

Commit

Permalink
Beta: Simplify Wallet Connect (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith authored Aug 10, 2024
1 parent 774abe2 commit ed42e81
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 55 deletions.
19 changes: 8 additions & 11 deletions src/beta.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
Hex,
Signature,
TransactionSerializable,
fromHex,
hashMessage,
hashTypedData,
Expand Down Expand Up @@ -43,7 +42,7 @@ export async function wcRouter(
chainId: string,
params: SessionRequestParams
): Promise<{
evmMessage: TransactionSerializable | string;
evmMessage: string;
payload: number[];
recoveryData: RecoveryData;
}> {
Expand Down Expand Up @@ -88,12 +87,13 @@ export async function wcRouter(
},
tx.from
);
const txHex = serializeTransaction(transaction);
return {
payload: toPayload(keccak256(serializeTransaction(transaction))),
evmMessage: transaction,
evmMessage: txHex,
recoveryData: {
type: "eth_sendTransaction",
data: serializeTransaction(transaction),
data: txHex,
},
};
}
Expand Down Expand Up @@ -158,14 +158,11 @@ export class Beta {
}

async respondSessionRequest(
recoveryData: RecoveryData,
signature: Signature
signature: Signature,
transaction?: Hex
): Promise<Hex> {
if (recoveryData.type === "eth_sendTransaction") {
const signedTx = addSignature({
transaction: recoveryData.data as Hex,
signature,
});
if (transaction) {
const signedTx = addSignature({ transaction, signature });
// Returns relayed transaction hash (without waiting for confirmation).
return this.adapter.relaySignedTransaction(signedTx, false);
}
Expand Down
49 changes: 5 additions & 44 deletions tests/unit/wc.handlers.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Hex, TransactionSerializable, toHex } from "viem";
import { Hex, isHex, toHex } from "viem";
import { wcRouter } from "../../src/beta";

describe("Wallet Connect", () => {
Expand Down Expand Up @@ -71,6 +71,7 @@ Challenge: 4113fc3ab2cc60f5d595b2e55349f1eec56fd0c70d4287081fe7156848263626`
});
});
describe("wcRouter: eth_sendTransaction", () => {
/// can't test payload: its non-deterministic because of gas values!
it("with value", async () => {
const { evmMessage } = await wcRouter("eth_sendTransaction", chainId, [
{
Expand All @@ -81,21 +82,7 @@ Challenge: 4113fc3ab2cc60f5d595b2e55349f1eec56fd0c70d4287081fe7156848263626`
data: "0xd0e30db0",
},
]);
const tx = evmMessage as TransactionSerializable;

delete tx.maxFeePerGas;
delete tx.maxPriorityFeePerGas;
delete tx.nonce;

expect(tx).toEqual({
account: from,
chainId: 11155111,
data: "0xd0e30db0",
gas: 54045n,
to,
value: 100000000000000000n,
});
/// can't test payload: its non-deterministic because of gas values!
expect(isHex(evmMessage)).toBe(true);
});

it("null value", async () => {
Expand All @@ -107,21 +94,8 @@ Challenge: 4113fc3ab2cc60f5d595b2e55349f1eec56fd0c70d4287081fe7156848263626`
data: "0x2e1a7d4d000000000000000000000000000000000000000000000000002386f26fc10000",
},
]);
const tx = evmMessage as TransactionSerializable;

delete tx.maxFeePerGas;
delete tx.maxPriorityFeePerGas;
delete tx.nonce;

expect(tx).toEqual({
account: from,
chainId: 11155111,
data: "0x2e1a7d4d000000000000000000000000000000000000000000000000002386f26fc10000",
gas: 43203n,
to,
value: 0n,
});
/// can't test payload: its non-deterministic because of gas values!
expect(isHex(evmMessage)).toBe(true);
});

it("null data", async () => {
Expand All @@ -133,21 +107,8 @@ Challenge: 4113fc3ab2cc60f5d595b2e55349f1eec56fd0c70d4287081fe7156848263626`
value: "0x01",
},
]);
const tx = evmMessage as TransactionSerializable;

delete tx.maxFeePerGas;
delete tx.maxPriorityFeePerGas;
delete tx.nonce;

expect(tx).toEqual({
account: from,
chainId: 11155111,
data: "0x",
gas: 43203n,
to,
value: 1n,
});
/// can't test payload: its non-deterministic because of gas values!
expect(isHex(evmMessage)).toBe(true);
});
});
describe("wcRouter: eth_signTypedData", () => {
Expand Down

0 comments on commit ed42e81

Please sign in to comment.