@@ -3,9 +3,8 @@ import { abort, getEmbeddedWallet } from '../utils';
3
3
import EmbeddedWallet from '..' ;
4
4
5
5
class EmbeddedEthersSigner extends ethers . AbstractSigner < ethers . JsonRpcProvider > {
6
- address = '' ;
7
6
wallet : EmbeddedWallet ;
8
- // override provider: ethers.JsonRpcProvider ;
7
+ internalSigner : InternalEmbeddedEthersSignerextends ;
9
8
10
9
constructor ( provider ?: ethers . JsonRpcProvider ) {
11
10
const w = getEmbeddedWallet ( ) ;
@@ -16,8 +15,94 @@ class EmbeddedEthersSigner extends ethers.AbstractSigner<ethers.JsonRpcProvider>
16
15
17
16
super ( provider || w . getRpcProviderForChainId ( w . defaultNetworkId ) ) ;
18
17
18
+ this . internalSigner = new InternalEmbeddedEthersSignerextends (
19
+ provider || w . getRpcProviderForChainId ( w . defaultNetworkId ) ,
20
+ w
21
+ ) ;
22
+
19
23
this . wallet = w ! ;
20
- // this.provider = provider;
24
+
25
+ /**
26
+ * Reinitialize signer with new provider when chain changes
27
+ */
28
+ w . events . on ( 'dataUpdated' , ( { name, newValue } ) => {
29
+ if ( name === 'defaultNetworkId' ) {
30
+ this . internalSigner = new InternalEmbeddedEthersSignerextends (
31
+ w . getRpcProviderForChainId ( newValue ) ,
32
+ this . wallet
33
+ ) ;
34
+ }
35
+ } ) ;
36
+ }
37
+
38
+ override connect ( ) : ethers . Signer {
39
+ return this . internalSigner ;
40
+ }
41
+
42
+ override async getAddress ( ) : Promise < string > {
43
+ const a = await this . wallet . getAccountAddress ( ) ;
44
+ return a || '' ;
45
+ }
46
+
47
+ override async signTransaction (
48
+ tx : ethers . TransactionRequest ,
49
+ mustConfirm = true
50
+ ) : Promise < string > {
51
+ return this . internalSigner . signTransaction ( tx , mustConfirm ) ;
52
+ }
53
+
54
+ override async signMessage ( message : string | Uint8Array , mustConfirm = true ) : Promise < string > {
55
+ return this . internalSigner . signMessage ( message , mustConfirm ) ;
56
+ }
57
+
58
+ override async sendTransaction (
59
+ tx : ethers . TransactionRequest
60
+ ) : Promise < ethers . TransactionResponse > {
61
+ return this . internalSigner . sendTransaction ( tx ) ;
62
+ }
63
+
64
+ /**
65
+ * NOT implemented
66
+ */
67
+ override async signTypedData (
68
+ domain : ethers . TypedDataDomain ,
69
+ types : Record < string , ethers . TypedDataField [ ] > ,
70
+ value : Record < string , any >
71
+ ) : Promise < string > {
72
+ console . error ( 'EmbeddedEthersSigner#signTypedData Not implemented' , { domain, types, value } ) ;
73
+ return '' ;
74
+ }
75
+
76
+ /**
77
+ * @deprecated v5 signer properties
78
+ */
79
+ _isSigner = true ;
80
+ async getBalance ( blockTag ?: ethers . BlockTag ) {
81
+ return this . internalSigner . getBalance ( blockTag ) ;
82
+ }
83
+ async getTransactionCount ( blockTag ?: ethers . BlockTag ) {
84
+ return this . internalSigner . getTransactionCount ( blockTag ) ;
85
+ }
86
+ async getChainId ( ) {
87
+ return this . internalSigner . getChainId ( ) ;
88
+ }
89
+ async getGasPrice ( ) {
90
+ return this . internalSigner . getGasPrice ( ) ;
91
+ }
92
+ async getFeeData ( ) {
93
+ return this . internalSigner . getFeeData ( ) ;
94
+ }
95
+ }
96
+
97
+ class InternalEmbeddedEthersSignerextends extends ethers . AbstractSigner < ethers . JsonRpcProvider > {
98
+ // address = '';
99
+ // override provider: ethers.JsonRpcProvider;
100
+
101
+ constructor (
102
+ provider : ethers . JsonRpcProvider ,
103
+ private wallet : EmbeddedWallet
104
+ ) {
105
+ super ( provider ) ;
21
106
}
22
107
23
108
override connect ( ) : ethers . Signer {
0 commit comments