1
- import 'dart:convert' ;
2
1
import 'dart:developer' ;
3
- import 'dart:io' ;
4
2
import 'dart:math' as math;
5
3
import 'package:flutter/foundation.dart' ;
6
4
import 'package:web3_signers/web3_signers.dart' ;
7
5
import 'package:variance_dart/variance_dart.dart' ;
8
6
import 'package:web3dart/credentials.dart' ;
9
7
import 'package:web3dart/web3dart.dart' as w3d;
10
- import 'package:web3dart/crypto.dart' as w3d;
11
8
12
9
class WalletProvider extends ChangeNotifier {
13
10
final Chain _chain;
@@ -23,67 +20,56 @@ class WalletProvider extends ChangeNotifier {
23
20
final EthereumAddress erc20 =
24
21
EthereumAddress .fromHex ("0xAEaF19097D8a8da728438D6B57edd9Bc5DAc4795" );
25
22
final EthereumAddress deployer =
26
- EthereumAddress .fromHex ("0x218F6Bbc32Ef28F547A67c70AbCF8c2ea3b468BA" );
23
+ EthereumAddress .fromHex ("0xf5bb7f874d8e3f41821175c0aa9910d30d10e193" );
24
+
25
+ final salt = Uint256 .zero;
26
+ static const rpc =
27
+ "https://api.pimlico.io/v2/84532/rpc?apikey=pim_NuuL4a9tBdyfoogF5LtP5A" ;
27
28
28
29
WalletProvider ()
29
- : _chain = Chain (
30
- chainId: 31337 ,
31
- explorer: "https://sepolia.etherscan.io/" ,
32
- entrypoint: EntryPointAddress (
33
- 0.6 ,
34
- EthereumAddress .fromHex (
35
- "0x5165c9e79213e2208947589c6e1dcc80ee8d3d00" )))
36
- ..accountFactory = EthereumAddress .fromHex (
37
- "0x0ce83Bf5d20c539E77e1E607B8349E26c6b20133" ) // v07 p256 factory address
38
- ..jsonRpcUrl = "http://127.0.0.1:8545"
39
- ..bundlerUrl = "http://localhost:3000/rpc" ;
40
- // ..paymasterUrl =
41
- // "https://api.pimlico.io/v2/11155111/rpc?apikey=875f3458-a37c-4187-8ac5-d08bbfa0d501";
42
-
43
- // "0x402A266e92993EbF04a5B3fd6F0e2b21bFC83070" v06 p256 factory address
30
+ : _chain = Chains .getChain (Network .baseTestnet)
31
+ ..accountFactory = Constants .lightAccountFactoryAddressv07
32
+ ..bundlerUrl = rpc
33
+ ..paymasterUrl = rpc;
34
+
44
35
Future <void > registerWithPassKey (String name,
45
36
{bool ? requiresUserVerification}) async {
46
- final pkpSigner =
47
- PassKeySigner ("webauthn.io" , "webauthn" , "https://webauthn.io" );
48
- final hwdSigner = HardwareSigner .withTag (name);
37
+ _chain.accountFactory = Constants .safeProxyFactoryAddress;
49
38
50
- final salt = Uint256 .zero;
51
- Uint256 .fromHex (
52
- hexlify (w3d.keccak256 (Uint8List .fromList (utf8.encode (name)))));
39
+ final options = PassKeysOptions (
40
+ name: "variance" ,
41
+ namespace: "variance.space" ,
42
+ origin: "https://variance.space" ,
43
+ userVerification: "required" ,
44
+ requireResidentKey: true ,
45
+ sharedWebauthnSigner: EthereumAddress .fromHex (
46
+ "0xfD90FAd33ee8b58f32c00aceEad1358e4AFC23f9" ));
47
+ final pkpSigner = PassKeySigner (options: options);
53
48
54
49
try {
55
- // uses passkeys on android, secure enclave on iOS
56
- if (Platform .isAndroid) {
57
- final SmartWalletFactory walletFactory =
58
- SmartWalletFactory (_chain, pkpSigner);
59
- final keypair = await pkpSigner.register (name, name);
60
- _wallet =
61
- await walletFactory.createP256Account <PassKeyPair >(keypair, salt);
62
- } else if (Platform .isIOS) {
63
- final SmartWalletFactory walletFactory =
64
- SmartWalletFactory (_chain, hwdSigner);
65
- final keypair = await hwdSigner.generateKeyPair ();
66
- _wallet = await walletFactory.createP256Account <P256Credential >(
67
- keypair, salt);
68
- }
50
+ final SmartWalletFactory walletFactory =
51
+ SmartWalletFactory (_chain, pkpSigner);
52
+ final keypair = await pkpSigner.register (
53
+ "${DateTime .timestamp ().millisecondsSinceEpoch }@variance.space" ,
54
+ name);
55
+ _wallet = await walletFactory.createSafeAccountWithPasskey (
56
+ keypair, salt, options.sharedWebauthnSigner);
69
57
70
58
log ("wallet created ${_wallet ?.address .hex } " );
71
59
} catch (e) {
72
60
_errorMessage = e.toString ();
73
61
notifyListeners ();
74
62
log ("something happened: $e " );
63
+ rethrow ;
75
64
}
76
65
}
77
66
78
67
Future <void > createEOAWallet () async {
79
- _chain.accountFactory = Constants .lightAccountFactoryAddressv06;
80
-
81
- final signer = EOAWallet .createWallet ();
68
+ final signer = EOAWallet .createWallet (
69
+ WordLength .word_12, const SignatureOptions (prefix: [0 ]));
82
70
log ("signer: ${signer .getAddress ()}" );
83
71
84
72
final SmartWalletFactory walletFactory = SmartWalletFactory (_chain, signer);
85
- final salt = Uint256 .fromHex (hexlify (w3d
86
- .keccak256 (EthereumAddress .fromHex (signer.getAddress ()).addressBytes)));
87
73
88
74
try {
89
75
_wallet = await walletFactory.createAlchemyLightAccount (salt);
@@ -99,17 +85,13 @@ class WalletProvider extends ChangeNotifier {
99
85
final random = math.Random .secure ();
100
86
final privateKey = EthPrivateKey .createRandom (random);
101
87
102
- final signer = PrivateKeySigner .create (privateKey, "123456" , random);
88
+ final signer = PrivateKeySigner .create (privateKey, "123456" , random,
89
+ options: const SignatureOptions (prefix: [0 ]));
103
90
log ("signer: ${signer .getAddress ()}" );
104
91
log ("pk: ${hexlify (privateKey .privateKey )}" );
105
92
106
93
final SmartWalletFactory walletFactory = SmartWalletFactory (_chain, signer);
107
94
108
- final salt = Uint256 .fromHex (hexlify (w3d
109
- .keccak256 (EthereumAddress .fromHex (signer.getAddress ()).addressBytes)));
110
-
111
- log ("pk salt: ${salt .toHex ()}" );
112
-
113
95
try {
114
96
_wallet = await walletFactory.createAlchemyLightAccount (salt);
115
97
log ("pk wallet created ${_wallet ?.address .hex } " );
@@ -128,11 +110,6 @@ class WalletProvider extends ChangeNotifier {
128
110
129
111
final SmartWalletFactory walletFactory = SmartWalletFactory (_chain, signer);
130
112
131
- final salt = Uint256 .fromHex (hexlify (w3d
132
- .keccak256 (EthereumAddress .fromHex (signer.getAddress ()).addressBytes)));
133
-
134
- log ("salt: ${salt .toHex ()}" );
135
-
136
113
try {
137
114
_wallet = await walletFactory.createSafeAccount (salt);
138
115
log ("safe created ${_wallet ?.address .hex } " );
@@ -180,30 +157,16 @@ class WalletProvider extends ChangeNotifier {
180
157
181
158
Future <void > sendTransaction (String recipient, String amount) async {
182
159
if (_wallet != null ) {
183
- final response = await transferToken (
184
- EthereumAddress .fromHex (recipient),
185
- w3d.EtherAmount .fromBigInt (
186
- w3d.EtherUnit .wei, BigInt .from (20 * math.pow (10 , 6 ))));
187
-
188
- // final etherAmount = w3d.EtherAmount.fromBigInt(w3d.EtherUnit.wei,
189
- // BigInt.from(double.parse(amount) * math.pow(10, 18)));
160
+ final etherAmount = w3d.EtherAmount .fromBigInt (w3d.EtherUnit .wei,
161
+ BigInt .from (double .parse (amount) * math.pow (10 , 18 )));
190
162
191
- // final response =
192
- // await _wallet?.send(EthereumAddress.fromHex(recipient), etherAmount);
163
+ final response =
164
+ await _wallet? .send (EthereumAddress .fromHex (recipient), etherAmount);
193
165
final receipt = await response? .wait ();
194
166
195
167
log ("Transaction receipt Hash: ${receipt ?.userOpHash }" );
196
168
} else {
197
169
log ("No wallet available to send transaction" );
198
170
}
199
171
}
200
-
201
- Future <UserOperationResponse ?> transferToken (
202
- EthereumAddress recipient, w3d.EtherAmount amount) async {
203
- final erc20 =
204
- EthereumAddress .fromHex ("0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9" );
205
-
206
- return await _wallet? .sendTransaction (
207
- erc20, Contract .encodeERC20TransferCall (erc20, recipient, amount));
208
- }
209
172
}
0 commit comments