11import * as ethers5 from "ethers5" ;
2+ import * as ethers6 from "ethers6" ;
23import { describe , expect , it , test } from "vitest" ;
34import { USDT_CONTRACT } from "~test/test-contracts.js" ;
45import { ANVIL_CHAIN , FORKED_ETHEREUM_CHAIN } from "../../test/src/chains.js" ;
@@ -10,6 +11,7 @@ import { randomBytesBuffer } from "../utils/random.js";
1011import { privateKeyToAccount } from "../wallets/private-key.js" ;
1112import {
1213 fromEthersContract ,
14+ fromEthersSigner ,
1315 toEthersContract ,
1416 toEthersProvider ,
1517 toEthersSigner ,
@@ -149,4 +151,94 @@ describe("ethers5 adapter", () => {
149151 const _decimals = await decimals ( { contract : thirdwebContract } ) ;
150152 expect ( _decimals ) . toBe ( 6 ) ;
151153 } ) ;
154+
155+ test ( "toEthersProvider should return a valid provider" , async ( ) => {
156+ const provider = toEthersProvider ( ethers5 , TEST_CLIENT , ANVIL_CHAIN ) ;
157+ expect ( provider ) . toBeDefined ( ) ;
158+ expect ( provider . getBlockNumber ) . toBeDefined ( ) ;
159+
160+ // Test if the provider can fetch the block number
161+ const blockNumber = await provider . getBlockNumber ( ) ;
162+ expect ( typeof blockNumber ) . toBe ( "number" ) ;
163+ } ) ;
164+
165+ test ( "toEthersProvider should throw error with invalid ethers version" , ( ) => {
166+ const invalidEthers = ethers6 ;
167+ expect ( ( ) =>
168+ // biome-ignore lint/suspicious/noExplicitAny: Testing invalid data
169+ toEthersProvider ( invalidEthers as any , TEST_CLIENT , ANVIL_CHAIN ) ,
170+ ) . toThrow (
171+ "You seem to be using ethers@6, please use the `ethers6Adapter()`" ,
172+ ) ;
173+ } ) ;
174+ } ) ;
175+
176+ describe ( "fromEthersSigner" , ( ) => {
177+ it ( "should convert an ethers5 Signer to an Account" , async ( ) => {
178+ const wallet = new ethers5 . Wallet ( ANVIL_PKEY_A ) ;
179+ const account = await fromEthersSigner ( wallet ) ;
180+
181+ expect ( account ) . toBeDefined ( ) ;
182+ expect ( account . address ) . toBe ( await wallet . getAddress ( ) ) ;
183+ } ) ;
184+
185+ it ( "should sign a message" , async ( ) => {
186+ const wallet = new ethers5 . Wallet ( ANVIL_PKEY_A ) ;
187+ const account = await fromEthersSigner ( wallet ) ;
188+
189+ const message = "Hello, world!" ;
190+ const signature = await account . signMessage ( { message } ) ;
191+
192+ expect ( signature ) . toBe ( await wallet . signMessage ( message ) ) ;
193+ } ) ;
194+
195+ it ( "should sign a transaction" , async ( ) => {
196+ const wallet = new ethers5 . Wallet (
197+ ANVIL_PKEY_A ,
198+ ethers5 . getDefaultProvider ( ) ,
199+ ) ;
200+ const account = await fromEthersSigner ( wallet ) ;
201+
202+ const transaction = {
203+ to : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
204+ value : 1n ,
205+ } ;
206+
207+ const signedTransaction = await account . signTransaction ?.( transaction ) ;
208+
209+ expect ( signedTransaction ) . toBe ( await wallet . signTransaction ( transaction ) ) ;
210+ } ) ;
211+
212+ it ( "should sign typed data" , async ( ) => {
213+ const wallet = new ethers5 . Wallet ( ANVIL_PKEY_A ) ;
214+ const account = await fromEthersSigner ( wallet ) ;
215+
216+ const domain = {
217+ name : "Ether Mail" ,
218+ version : "1" ,
219+ chainId : 1 ,
220+ verifyingContract : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
221+ } ;
222+
223+ const types = {
224+ Person : [
225+ { name : "name" , type : "string" } ,
226+ { name : "wallet" , type : "address" } ,
227+ ] ,
228+ } ;
229+
230+ const value = {
231+ name : "Alice" ,
232+ wallet : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
233+ } ;
234+
235+ const signature = await account . signTypedData ( {
236+ primaryType : "Person" ,
237+ domain,
238+ types,
239+ message : value ,
240+ } ) ;
241+
242+ expect ( signature ) . toBeDefined ( ) ;
243+ } ) ;
152244} ) ;
0 commit comments