11import * as ethers6 from "ethers6" ;
2- import { describe , expect , test } from "vitest" ;
2+ import { describe , expect , it , test } from "vitest" ;
33import { ANVIL_CHAIN } from "../../test/src/chains.js" ;
44import { TEST_CLIENT } from "../../test/src/test-clients.js" ;
55import { ANVIL_PKEY_A , TEST_ACCOUNT_B } from "../../test/src/test-wallets.js" ;
6+ import { sendTransaction } from "../transaction/actions/send-transaction.js" ;
7+ import { prepareTransaction } from "../transaction/prepare-transaction.js" ;
68import { randomBytesBuffer } from "../utils/random.js" ;
79import { privateKeyToAccount } from "../wallets/private-key.js" ;
8- import { toEthersSigner } from "./ethers6.js" ;
10+ import { fromEthersSigner , toEthersSigner } from "./ethers6.js" ;
911
1012const account = privateKeyToAccount ( {
1113 privateKey : ANVIL_PKEY_A ,
@@ -14,12 +16,7 @@ const account = privateKeyToAccount({
1416
1517describe ( "toEthersSigner" , ( ) => {
1618 test ( "should return an ethers 6 signer" , async ( ) => {
17- const signer = await toEthersSigner (
18- ethers6 ,
19- TEST_CLIENT ,
20- account ,
21- ANVIL_CHAIN ,
22- ) ;
19+ const signer = toEthersSigner ( ethers6 , TEST_CLIENT , account , ANVIL_CHAIN ) ;
2320 expect ( signer ) . toBeDefined ( ) ;
2421 expect ( signer . signMessage ) . toBeDefined ( ) ;
2522 } ) ;
@@ -40,12 +37,7 @@ describe("toEthersSigner", () => {
4037 } ) ;
4138
4239 test ( "should sign typed data" , async ( ) => {
43- const signer = await toEthersSigner (
44- ethers6 ,
45- TEST_CLIENT ,
46- account ,
47- ANVIL_CHAIN ,
48- ) ;
40+ const signer = toEthersSigner ( ethers6 , TEST_CLIENT , account , ANVIL_CHAIN ) ;
4941 expect ( signer . signTypedData ) . toBeDefined ( ) ;
5042
5143 // All properties on a domain are optional
@@ -88,16 +80,100 @@ describe("toEthersSigner", () => {
8880 } ) ;
8981
9082 test ( "should send a tx" , async ( ) => {
91- const signer = await toEthersSigner (
92- ethers6 ,
93- TEST_CLIENT ,
94- account ,
95- ANVIL_CHAIN ,
96- ) ;
83+ const signer = toEthersSigner ( ethers6 , TEST_CLIENT , account , ANVIL_CHAIN ) ;
9784 const txResponse = await signer . sendTransaction ( {
9885 to : TEST_ACCOUNT_B . address ,
9986 value : 100 ,
10087 } ) ;
10188 expect ( txResponse . hash . length ) . toBe ( 66 ) ;
10289 } ) ;
10390} ) ;
91+
92+ describe ( "fromEthersSigner" , ( ) => {
93+ it ( "should convert an ethers6 Signer to an Account" , async ( ) => {
94+ const wallet = new ethers6 . Wallet ( ANVIL_PKEY_A ) ;
95+ const account = await fromEthersSigner ( wallet ) ;
96+
97+ expect ( account ) . toBeDefined ( ) ;
98+ expect ( account . address ) . toBe ( await wallet . getAddress ( ) ) ;
99+ } ) ;
100+
101+ it ( "should sign a message" , async ( ) => {
102+ const wallet = new ethers6 . Wallet ( ANVIL_PKEY_A ) ;
103+ const account = await fromEthersSigner ( wallet ) ;
104+
105+ const message = "Hello, world!" ;
106+ const signature = await account . signMessage ( { message } ) ;
107+
108+ expect ( signature ) . toBe ( await wallet . signMessage ( message ) ) ;
109+ } ) ;
110+
111+ it ( "should sign a transaction" , async ( ) => {
112+ const wallet = new ethers6 . Wallet (
113+ ANVIL_PKEY_A ,
114+ ethers6 . getDefaultProvider ( ) ,
115+ ) ;
116+ const account = await fromEthersSigner ( wallet ) ;
117+
118+ const transaction = {
119+ to : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
120+ value : 1n ,
121+ } ;
122+
123+ const signedTransaction = await account . signTransaction ?.( transaction ) ;
124+
125+ expect ( signedTransaction ) . toBe ( await wallet . signTransaction ( transaction ) ) ;
126+ } ) ;
127+
128+ it ( "should send a transaction" , async ( ) => {
129+ const wallet = new ethers6 . Wallet (
130+ ANVIL_PKEY_A ,
131+ ethers6 . getDefaultProvider ( ANVIL_CHAIN . rpc ) ,
132+ ) ;
133+ const account = await fromEthersSigner ( wallet ) ;
134+
135+ const transaction = prepareTransaction ( {
136+ client : TEST_CLIENT ,
137+ chain : ANVIL_CHAIN ,
138+ to : TEST_ACCOUNT_B . address ,
139+ value : 1n ,
140+ } ) ;
141+
142+ const txResponse = await sendTransaction ( { transaction, account } ) ;
143+
144+ expect ( txResponse . transactionHash . length ) . toBe ( 66 ) ;
145+ } ) ;
146+
147+ it ( "should sign typed data" , async ( ) => {
148+ const wallet = new ethers6 . Wallet ( ANVIL_PKEY_A ) ;
149+ const account = await fromEthersSigner ( wallet ) ;
150+
151+ const domain = {
152+ name : "Ether Mail" ,
153+ version : "1" ,
154+ chainId : 1 ,
155+ verifyingContract : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
156+ } ;
157+
158+ const types = {
159+ Person : [
160+ { name : "name" , type : "string" } ,
161+ { name : "wallet" , type : "address" } ,
162+ ] ,
163+ } ;
164+
165+ const value = {
166+ name : "Alice" ,
167+ wallet : "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC" ,
168+ } ;
169+
170+ const signature = await account . signTypedData ( {
171+ primaryType : "Person" ,
172+ domain,
173+ types,
174+ message : value ,
175+ } ) ;
176+
177+ expect ( signature ) . toBeDefined ( ) ;
178+ } ) ;
179+ } ) ;
0 commit comments