Skip to content

Commit

Permalink
fix: ethers adapter signTransaction properties (#322)
Browse files Browse the repository at this point in the history
* fix: ethers adapter

* test: boost coverage
  • Loading branch information
tmm authored Apr 6, 2023
1 parent 4b3c233 commit ea019d7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/heavy-lies-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"viem": patch
---

Fixed properties passed to ethers adapter `signTransaction`
40 changes: 40 additions & 0 deletions src/adapters/ethers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Wallet } from 'ethers'
import { expect, test } from 'vitest'
import { accounts, typedData } from '../_test'
import { ethersWalletToAccount } from './ethers'
const wallet = new Wallet(accounts[0].privateKey)

test('ethersWalletToAccount', async () => {
const account = ethersWalletToAccount(wallet)
expect(account).toBeDefined()
})

test('signMessage', async () => {
const account = ethersWalletToAccount(wallet)
await account.signMessage({ message: 'foo bar baz' })
})

test('signTransaction', async () => {
const account = ethersWalletToAccount(wallet)
// with types
await account.signTransaction({ type: 'legacy' })
await account.signTransaction({ type: 'eip1559', chainId: 1 })
await account.signTransaction({ type: 'eip2930', chainId: 1 })

// with conditional properties
await account.signTransaction({ type: 'eip1559', chainId: 1, accessList: [] })
await account.signTransaction({
type: 'eip1559',
chainId: 1,
maxPriorityFeePerGas: 100n,
maxFeePerGas: 100n,
})
})

test('signTypedData', async () => {
const account = ethersWalletToAccount(wallet)
await account.signTypedData({
...typedData.basic,
primaryType: 'Mail',
})
})
28 changes: 27 additions & 1 deletion src/adapters/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,35 @@ export const ethersWalletToAccount = (wallet: EthersWallet) =>
return (await wallet.signMessage(toBytes(message))) as Hash
},
async signTransaction(txn) {
// ethers type mappings
// https://github.com/ethers-io/ethers.js/blob/0802b70a724321f56d4c170e4c8a46b7804dfb48/src.ts/transaction/transaction.ts#L394
let type = null
if (txn.type === 'legacy') {
type = 0
} else if (txn.type === 'eip1559') {
type = 2
} else if (txn.type === 'eip2930') {
type = 1
}
return (await wallet.signTransaction({
...txn,
// allowed fields for `ethers.TransactionRequest`
chainId: txn.chainId,
data: txn.data,
gasLimit: txn.gas,
gasPrice: txn.gasPrice,
nonce: txn.nonce,
to: txn.to,
type,
value: txn.value,
// untyped transactions do not support accessList
...(txn.type && txn.accessList ? { accessList: txn.accessList } : {}),
// eip1559 properties
...(txn.type === 'eip1559' && txn.maxPriorityFeePerGas
? { maxPriorityFeePerGas: txn.maxPriorityFeePerGas }
: {}),
...(txn.type === 'eip1559' && txn.maxFeePerGas
? { maxFeePerGas: txn.maxFeePerGas }
: {}),
})) as Hash
},
async signTypedData({ domain, types: types_, message }) {
Expand Down

2 comments on commit ea019d7

@vercel
Copy link

@vercel vercel bot commented on ea019d7 Apr 6, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

viem-playground – ./playgrounds/browser

viem-playground.vercel.app
viem-playground-wagmi-dev.vercel.app
viem-playground-git-main-wagmi-dev.vercel.app

@vercel
Copy link

@vercel vercel bot commented on ea019d7 Apr 6, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.