Skip to content

Commit

Permalink
fix: ethers adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm committed Apr 6, 2023
1 parent 4b3c233 commit 4c0a0ca
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/adapters/ethers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Wallet } from 'ethers'
import { test } from 'vitest'
import { accounts } from '../_test'
import { ethersWalletToAccount } from './ethers'

test('ethersWalletToAccount', async () => {
const wallet = new Wallet(accounts[0].privateKey)
const account = ethersWalletToAccount(wallet)
await account.signTransaction({ type: 'legacy' })
await account.signTransaction({ type: 'eip1559', chainId: 1 })
await account.signTransaction({ type: 'eip2930', chainId: 1 })
})
27 changes: 26 additions & 1 deletion src/adapters/ethers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,34 @@ 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,
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

0 comments on commit 4c0a0ca

Please sign in to comment.