1
1
import * as ethers6 from "ethers6" ;
2
- import { describe , expect , test } from "vitest" ;
2
+ import { describe , expect , it , test } from "vitest" ;
3
3
import { ANVIL_CHAIN } from "../../test/src/chains.js" ;
4
4
import { TEST_CLIENT } from "../../test/src/test-clients.js" ;
5
5
import { 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" ;
6
8
import { randomBytesBuffer } from "../utils/random.js" ;
7
9
import { privateKeyToAccount } from "../wallets/private-key.js" ;
8
- import { toEthersSigner } from "./ethers6.js" ;
10
+ import { fromEthersSigner , toEthersSigner } from "./ethers6.js" ;
9
11
10
12
const account = privateKeyToAccount ( {
11
13
privateKey : ANVIL_PKEY_A ,
@@ -14,12 +16,7 @@ const account = privateKeyToAccount({
14
16
15
17
describe ( "toEthersSigner" , ( ) => {
16
18
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 ) ;
23
20
expect ( signer ) . toBeDefined ( ) ;
24
21
expect ( signer . signMessage ) . toBeDefined ( ) ;
25
22
} ) ;
@@ -40,12 +37,7 @@ describe("toEthersSigner", () => {
40
37
} ) ;
41
38
42
39
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 ) ;
49
41
expect ( signer . signTypedData ) . toBeDefined ( ) ;
50
42
51
43
// All properties on a domain are optional
@@ -88,16 +80,100 @@ describe("toEthersSigner", () => {
88
80
} ) ;
89
81
90
82
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 ) ;
97
84
const txResponse = await signer . sendTransaction ( {
98
85
to : TEST_ACCOUNT_B . address ,
99
86
value : 100 ,
100
87
} ) ;
101
88
expect ( txResponse . hash . length ) . toBe ( 66 ) ;
102
89
} ) ;
103
90
} ) ;
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