1- import 'dart:convert' ;
21import 'dart:developer' ;
3- import 'dart:io' ;
42import 'dart:math' as math;
53import 'package:flutter/foundation.dart' ;
64import 'package:web3_signers/web3_signers.dart' ;
75import 'package:variance_dart/variance_dart.dart' ;
86import 'package:web3dart/credentials.dart' ;
97import 'package:web3dart/web3dart.dart' as w3d;
10- import 'package:web3dart/crypto.dart' as w3d;
118
129class WalletProvider extends ChangeNotifier {
1310 final Chain _chain;
@@ -23,67 +20,56 @@ class WalletProvider extends ChangeNotifier {
2320 final EthereumAddress erc20 =
2421 EthereumAddress .fromHex ("0xAEaF19097D8a8da728438D6B57edd9Bc5DAc4795" );
2522 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" ;
2728
2829 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+
4435 Future <void > registerWithPassKey (String name,
4536 {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;
4938
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);
5348
5449 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);
6957
7058 log ("wallet created ${_wallet ?.address .hex } " );
7159 } catch (e) {
7260 _errorMessage = e.toString ();
7361 notifyListeners ();
7462 log ("something happened: $e " );
63+ rethrow ;
7564 }
7665 }
7766
7867 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 ]));
8270 log ("signer: ${signer .getAddress ()}" );
8371
8472 final SmartWalletFactory walletFactory = SmartWalletFactory (_chain, signer);
85- final salt = Uint256 .fromHex (hexlify (w3d
86- .keccak256 (EthereumAddress .fromHex (signer.getAddress ()).addressBytes)));
8773
8874 try {
8975 _wallet = await walletFactory.createAlchemyLightAccount (salt);
@@ -99,17 +85,13 @@ class WalletProvider extends ChangeNotifier {
9985 final random = math.Random .secure ();
10086 final privateKey = EthPrivateKey .createRandom (random);
10187
102- final signer = PrivateKeySigner .create (privateKey, "123456" , random);
88+ final signer = PrivateKeySigner .create (privateKey, "123456" , random,
89+ options: const SignatureOptions (prefix: [0 ]));
10390 log ("signer: ${signer .getAddress ()}" );
10491 log ("pk: ${hexlify (privateKey .privateKey )}" );
10592
10693 final SmartWalletFactory walletFactory = SmartWalletFactory (_chain, signer);
10794
108- final salt = Uint256 .fromHex (hexlify (w3d
109- .keccak256 (EthereumAddress .fromHex (signer.getAddress ()).addressBytes)));
110-
111- log ("pk salt: ${salt .toHex ()}" );
112-
11395 try {
11496 _wallet = await walletFactory.createAlchemyLightAccount (salt);
11597 log ("pk wallet created ${_wallet ?.address .hex } " );
@@ -128,11 +110,6 @@ class WalletProvider extends ChangeNotifier {
128110
129111 final SmartWalletFactory walletFactory = SmartWalletFactory (_chain, signer);
130112
131- final salt = Uint256 .fromHex (hexlify (w3d
132- .keccak256 (EthereumAddress .fromHex (signer.getAddress ()).addressBytes)));
133-
134- log ("salt: ${salt .toHex ()}" );
135-
136113 try {
137114 _wallet = await walletFactory.createSafeAccount (salt);
138115 log ("safe created ${_wallet ?.address .hex } " );
@@ -180,30 +157,16 @@ class WalletProvider extends ChangeNotifier {
180157
181158 Future <void > sendTransaction (String recipient, String amount) async {
182159 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 )));
190162
191- // final response =
192- // await _wallet?.send(EthereumAddress.fromHex(recipient), etherAmount);
163+ final response =
164+ await _wallet? .send (EthereumAddress .fromHex (recipient), etherAmount);
193165 final receipt = await response? .wait ();
194166
195167 log ("Transaction receipt Hash: ${receipt ?.userOpHash }" );
196168 } else {
197169 log ("No wallet available to send transaction" );
198170 }
199171 }
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- }
209172}
0 commit comments