Skip to content

Commit

Permalink
Merge pull request #294 from WalletConnect/chore/one_click_auth_fallb…
Browse files Browse the repository at this point in the history
…ack_mecanism

Chore/one click auth fallback mecanism
  • Loading branch information
quetool committed Jun 11, 2024
2 parents 461f1aa + d27f923 commit 513ebc1
Show file tree
Hide file tree
Showing 19 changed files with 170 additions and 151 deletions.
18 changes: 6 additions & 12 deletions example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,9 @@ class _MyHomePageState extends State<MyHomePage> {
}

Future<void> initialize() async {
// try {
debugPrint('Project ID: ${DartDefines.projectId}');
_web3App = await Web3App.createInstance(
projectId: DartDefines.projectId,
logLevel: LogLevel.info,
logLevel: LogLevel.error,
metadata: const PairingMetadata(
name: 'Sample dApp Flutter',
description: 'WalletConnect\'s sample dapp with Flutter',
Expand All @@ -80,7 +78,6 @@ class _MyHomePageState extends State<MyHomePage> {
for (final ChainMetadata chain in ChainData.allChains) {
// Loop through the events for that chain
for (final event in getChainEvents(chain.type)) {
debugPrint('registerEventHandler $event for chain ${chain.chainId}');
_web3App!.registerEventHandler(
chainId: chain.chainId,
event: event,
Expand Down Expand Up @@ -127,9 +124,6 @@ class _MyHomePageState extends State<MyHomePage> {

_initializing = false;
});
// } on WalletConnectError catch (e) {
// print(e.message);
// }
}

void _setState(dynamic args) => setState(() {});
Expand Down Expand Up @@ -240,7 +234,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _onSessionPing(SessionPing? args) {
debugPrint('[$runtimeType] _onSessionPing $args');
debugPrint('[SampleDapp] _onSessionPing $args');
showDialog(
context: context,
builder: (BuildContext context) {
Expand All @@ -253,7 +247,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _onSessionEvent(SessionEvent? args) {
debugPrint('[$runtimeType] _onSessionEvent $args');
debugPrint('[SampleDapp] _onSessionEvent $args');
showDialog(
context: context,
builder: (BuildContext context) {
Expand All @@ -267,7 +261,7 @@ class _MyHomePageState extends State<MyHomePage> {
}

void _onSessionUpdate(SessionUpdate? args) {
debugPrint('[$runtimeType] _onSessionUpdate $args');
debugPrint('[SampleDapp] _onSessionUpdate $args');
}

void _onRelayMessage(MessageEvent? args) async {
Expand All @@ -278,9 +272,9 @@ class _MyHomePageState extends State<MyHomePage> {
args.message,
);
final data = jsonDecode(payloadString ?? '{}') as Map<String, dynamic>;
debugPrint('[$runtimeType] _onRelayMessage data $data');
debugPrint('[SampleDapp] _onRelayMessage data $data');
} catch (e) {
debugPrint('[$runtimeType] _onRelayMessage error $e');
debugPrint('[SampleDapp] _onRelayMessage error $e');
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions example/dapp/lib/pages/connect_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ConnectPageState extends State<ConnectPage> {
}
_updateNamespaces();

debugPrint('$optionalNamespaces');
debugPrint('[SampleDapp] ${jsonEncode(optionalNamespaces)}');
});
}

Expand Down Expand Up @@ -285,7 +285,7 @@ class ConnectPageState extends State<ConnectPage> {
Function(String message)? showToast,
VoidCallback? closeModal,
}) async {
debugPrint('Creating connection and session');
debugPrint('[SampleDapp] Creating connection and session');
// It is currently safer to send chains approvals on optionalNamespaces
// but depending on Wallet implementation you may need to send some (for innstance eip155:1) as required
final connectResponse = await widget.web3App.connect(
Expand Down Expand Up @@ -323,7 +323,7 @@ class ConnectPageState extends State<ConnectPage> {
_showQrCode(connectResponse.uri.toString());
}

debugPrint('Awaiting session proposal settlement');
debugPrint('[SampleDapp] Awaiting session proposal settlement');
final _ = await connectResponse.session.future;

showToast?.call(StringConstants.connectionEstablished);
Expand All @@ -332,7 +332,7 @@ class ConnectPageState extends State<ConnectPage> {

Future<void> _showQrCode(String uri) async {
// Show the QR code
debugPrint('Showing QR Code: $uri');
debugPrint('[SampleDapp] Showing QR Code: $uri');
_shouldDismissQrCode = true;
if (kIsWeb) {
await showDialog(
Expand Down Expand Up @@ -401,7 +401,7 @@ class ConnectPageState extends State<ConnectPage> {
);
},
);
if (!shouldAuth) return;
if (shouldAuth != true) return;

try {
final pairingTopic = event?.session.pairingTopic;
Expand Down
2 changes: 1 addition & 1 deletion example/dapp/lib/pages/pairings_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class PairingsPageState extends State<PairingsPage> {
);
Navigator.of(context).pop();
} catch (e) {
debugPrint(e.toString());
debugPrint('[SampleDapp] ${e.toString()}');
}
},
),
Expand Down
4 changes: 2 additions & 2 deletions example/dapp/lib/utils/crypto/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ String getChainName(String chain) {
.first
.name;
} catch (e) {
debugPrint('Invalid chain');
debugPrint('[SampleDapp] Invalid chain');
}
return 'Unknown';
}
Expand All @@ -23,7 +23,7 @@ ChainMetadata getChainMetadataFromChain(String chain) {
.where((element) => element.chainId == chain)
.first;
} catch (e) {
debugPrint('Invalid chain');
debugPrint('[SampleDapp] Invalid chain');
}
return ChainData.eip155Chains[0];
}
Expand Down
5 changes: 2 additions & 3 deletions example/dapp/lib/widgets/auth_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@ class AuthItem extends StatelessWidget {
auth.p.domain,
style: StyleConstants.paragraph,
),
Text(
auth.p.iss,
),
Text(auth.p.iss),
Text('iat: ${auth.p.iat}'),
],
),
),
Expand Down
27 changes: 13 additions & 14 deletions example/wallet/lib/dependencies/web3wallet_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ class Web3WalletService extends IWeb3WalletService {
for (final chainId in chainKey.chains) {
if (chainId.startsWith('kadena')) {
final account = '$chainId:k**${chainKey.address}';
debugPrint('[$runtimeType] registerAccount $account');
debugPrint('[SampleWallet] registerAccount $account');
_web3Wallet!.registerAccount(
chainId: chainId,
accountAddress: 'k**${chainKey.address}',
);
} else {
final account = '$chainId:${chainKey.address}';
debugPrint('[$runtimeType] registerAccount $account');
debugPrint('[SampleWallet] registerAccount $account');
_web3Wallet!.registerAccount(
chainId: chainId,
accountAddress: chainKey.address,
Expand All @@ -73,7 +73,7 @@ class Web3WalletService extends IWeb3WalletService {
}

// Setup our listeners
debugPrint('[WALLET] [$runtimeType] create');
debugPrint('[SampleWallet] create');
_web3Wallet!.core.pairing.onPairingInvalid.subscribe(_onPairingInvalid);
_web3Wallet!.core.pairing.onPairingCreate.subscribe(_onPairingCreate);
_web3Wallet!.onSessionProposal.subscribe(_onSessionProposal);
Expand All @@ -91,20 +91,18 @@ class Web3WalletService extends IWeb3WalletService {
@override
Future<void> init() async {
// Await the initialization of the web3wallet
debugPrint('[$runtimeType] [WALLET] init');
await _web3Wallet!.init();
}

void _logListener(LogEvent event) {
debugPrint('[WALLET] ${event.level.name}: ${event.message}');
debugPrint('[SampleWallet] ${event.level.name}: ${event.message}');
if (event.level == Level.error) {
// TODO send to mixpanel
}
}

@override
FutureOr onDispose() {
debugPrint('[$runtimeType] [WALLET] dispose');
_web3Wallet!.core.removeLogListener(_logListener);
_web3Wallet!.core.pairing.onPairingInvalid.unsubscribe(_onPairingInvalid);
_web3Wallet!.core.pairing.onPairingCreate.unsubscribe(_onPairingCreate);
Expand All @@ -126,7 +124,7 @@ class Web3WalletService extends IWeb3WalletService {
void _onRelayClientMessage(MessageEvent? event) async {
if (event != null) {
final jsonObject = await EthUtils.decodeMessageEvent(event);
debugPrint('[$runtimeType] [WALLET] _onRelayClientMessage $jsonObject');
debugPrint('[SampleWallet] _onRelayClientMessage $jsonObject');
if (jsonObject is JsonRpcRequest) {
if (jsonObject.method == 'wc_sessionPropose' ||
jsonObject.method == 'wc_sessionRequest') {
Expand All @@ -150,7 +148,7 @@ class Web3WalletService extends IWeb3WalletService {

void _onSessionProposal(SessionProposalEvent? args) async {
if (args != null) {
log('[$runtimeType] [WALLET] _onSessionProposal ${jsonEncode(args.params)}');
log('[SampleWallet] _onSessionProposal ${jsonEncode(args.params)}');
final approved = await _bottomSheetHandler.queueBottomSheet(
widget: WCRequestWidget(
child: WCConnectionRequestWidget(
Expand Down Expand Up @@ -191,7 +189,7 @@ class Web3WalletService extends IWeb3WalletService {
}

void _onSessionProposalError(SessionProposalErrorEvent? args) async {
debugPrint('[$runtimeType] [WALLET] _onSessionProposalError $args');
log('[SampleWallet] _onSessionProposalError $args');
DeepLinkHandler.waiting.value = false;
if (args != null) {
String errorMessage = args.error.message;
Expand Down Expand Up @@ -230,26 +228,27 @@ class Web3WalletService extends IWeb3WalletService {

void _onSessionConnect(SessionConnect? args) {
if (args != null) {
log('[$runtimeType] [WALLET] _onSessionConnect ${jsonEncode(args.session)}');
log('[SampleWallet] _onSessionConnect ${jsonEncode(args.session)}');
final scheme = args.session.peer.metadata.redirect?.native ?? '';
DeepLinkHandler.goTo(scheme);
}
}

void _onRelayClientError(ErrorEvent? args) {
debugPrint('[$runtimeType] [WALLET] _onRelayClientError ${args?.error}');
debugPrint(
'[$runtimeType] [SampleWallet] _onRelayClientError ${args?.error}');
}

void _onPairingInvalid(PairingInvalidEvent? args) {
debugPrint('[$runtimeType] [WALLET] _onPairingInvalid $args');
debugPrint('[$runtimeType] [SampleWallet] _onPairingInvalid $args');
}

void _onPairingCreate(PairingEvent? args) {
debugPrint('[$runtimeType] [WALLET] _onPairingCreate $args');
debugPrint('[$runtimeType] [SampleWallet] _onPairingCreate $args');
}

Future<void> _onAuthRequest(AuthRequest? args) async {
debugPrint('[$runtimeType] [WALLET] _onAuthRequest $args');
log('[SampleWallet] _onAuthRequest $args');
if (args != null) {
final chainKeys = GetIt.I<IKeyService>().getKeysForChain('eip155:1');
// Create the message to be signed
Expand Down
1 change: 1 addition & 0 deletions lib/apis/core/pairing/pairing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ class Pairing implements IPairing {
symKey: symKey,
relay: relay,
methods: methods,
expiry: expiry,
);

onPairingCreate.broadcast(
Expand Down
4 changes: 2 additions & 2 deletions lib/apis/sign_api/i_sign_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ abstract class ISignClient {
// FORMER AUTH ENGINE PROPERTY
abstract final Event<AuthResponse> onAuthResponse;
// NEW 1-CA PROPERTY
abstract final Event<OCAResponse> onOCAResponse;
abstract final Event<OCAuthResponse> onOCAuthResponse;

Future<void> init();
Future<ConnectResponse> connect({
Expand Down Expand Up @@ -166,7 +166,7 @@ abstract class ISignClient {
WalletConnectError? error,
});

// FORMER AUTH ENGINE METHOD DAPP
// FORMER AUTH ENGINE METHOD WALLET
// query all pending requests
Map<int, PendingAuthRequest> getPendingAuthRequests();

Expand Down
2 changes: 1 addition & 1 deletion lib/apis/sign_api/i_sign_engine_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ abstract class ISignEngineApp extends ISignEngineCommon {
// FORMER AUTH ENGINE PROPERTY
abstract final Event<AuthResponse> onAuthResponse;
// NEW 1-CA PROPERTY
abstract final Event<OCAResponse> onOCAResponse;
abstract final Event<OCAuthResponse> onOCAuthResponse;

Future<ConnectResponse> connect({
Map<String, RequiredNamespace>? requiredNamespaces,
Expand Down
54 changes: 27 additions & 27 deletions lib/apis/sign_api/models/auth/auth_client_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,30 @@ class PendingAuthRequest with _$PendingAuthRequest {
_$PendingAuthRequestFromJson(json);
}

class AuthRequestCompleter {
final int id;
final String pairingTopic;
final String responseTopic;
final PendingAuthRequest request;
final Completer<Cacao> completer;

AuthRequestCompleter({
required this.id,
required this.pairingTopic,
required this.responseTopic,
required this.request,
required this.completer,
});
}

class RespondParams {
final int id;
final CacaoSignature? signature;
final WalletConnectError? error;

RespondParams({
required this.id,
this.signature,
this.error,
});
}
// class AuthRequestCompleter {
// final int id;
// final String pairingTopic;
// final String responseTopic;
// final PendingAuthRequest request;
// final Completer<Cacao> completer;

// AuthRequestCompleter({
// required this.id,
// required this.pairingTopic,
// required this.responseTopic,
// required this.request,
// required this.completer,
// });
// }

// class RespondParams {
// final int id;
// final CacaoSignature? signature;
// final WalletConnectError? error;

// RespondParams({
// required this.id,
// this.signature,
// this.error,
// });
// }
6 changes: 3 additions & 3 deletions lib/apis/sign_api/models/auth/one_click_auth_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ import 'package:walletconnect_flutter_v2/apis/models/json_rpc_error.dart';
import 'package:walletconnect_flutter_v2/apis/sign_api/models/auth/auth_common_models.dart';
import 'package:walletconnect_flutter_v2/apis/sign_api/models/session_models.dart';

class OCARequest extends EventArgs {
class OCAuthRequest extends EventArgs {
// TODO to be implemented for wallet usage
}

class OCAResponse extends EventArgs {
class OCAuthResponse extends EventArgs {
final int id;
final String topic;
final List<Cacao>? auths;
final SessionData? session;
final WalletConnectError? error;
final JsonRpcError? jsonRpcError;

OCAResponse({
OCAuthResponse({
required this.id,
required this.topic,
this.auths,
Expand Down
3 changes: 2 additions & 1 deletion lib/apis/sign_api/models/auth/one_click_auth_models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import 'package:walletconnect_flutter_v2/apis/sign_api/models/auth/one_click_aut
part 'one_click_auth_models.g.dart';
part 'one_click_auth_models.freezed.dart';

// TODO this should be under sign_client_models.dart probably
class OCARequestResponse {
final int id;
final String pairingTopic;
final Completer<OCAResponse> completer;
final Completer<OCAuthResponse> completer;
final Uri? uri;

OCARequestResponse({
Expand Down
Loading

0 comments on commit 513ebc1

Please sign in to comment.