Skip to content

Commit

Permalink
Merge pull request #231 from WalletConnect/bugfix/generated_namespaces
Browse files Browse the repository at this point in the history
Bugfix/generated namespaces
  • Loading branch information
quetool authored Dec 8, 2023
2 parents f84c5ad + 715d93d commit 241fe01
Show file tree
Hide file tree
Showing 25 changed files with 406 additions and 315 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 2.1.11

- Fixed an issue with `generatedNamespaces` during session proposal
- Small enhancements in example wallet/dapp.
- Minor changes and bug fixe

## 2.1.10

- License change
Expand Down
45 changes: 26 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@ ConnectResponse resp = await wcClient.connect(
requiredNamespaces: {
'eip155': RequiredNamespace(
chains: ['eip155:1'], // Ethereum chain
methods: ['eth_signTransaction'], // Requestable Methods
events: ['eth_sendTransaction'], // Requestable Events
methods: ['personal_sign'], // Requestable Methods, see MethodsConstants for reference
events: ['chainChanged'], // Requestable Events, see EventsConstants for reference
),
'kadena': RequiredNamespace(
chains: ['kadena:mainnet01'], // Kadena chain
methods: ['kadena_quicksign_v1'], // Requestable Methods
events: ['kadena_transaction_updated'], // Requestable Events
},
optionalNamespaces: {
'eip155': RequiredNamespace(
chains: ['eip155:1', 'eip155:5'], // Any other optional Ethereum chain
methods: ['eth_signTransaction'], // Optional requestable Methods, see MethodsConstants for reference
events: ['accountsChanged'], // Optional requestable events, see EventsConstants for reference
),
}
},
);
Uri? uri = resp.uri;
Expand All @@ -50,7 +52,7 @@ final dynamic signResponse = await wcClient.request(
chainId: 'eip155:1',
request: SessionRequestParams(
method: 'eth_signTransaction',
params: 'json serializable parameters',
params: '{json serializable parameters}',
),
);
// Unpack, or use the signResponse.
Expand Down Expand Up @@ -85,13 +87,13 @@ else {
// You can also respond to events from the wallet, like session events
wcClient.registerEventHandler(
chainId: 'eip155:1',
event: 'accountsChanged',
);
wcClient.onSessionEvent.subscribe((SessionEvent? session) {
// Do something with the event
});
wcClient.registerEventHandler(
chainId: 'kadena',
event: 'kadena_transaction_updated',
);
```

### Wallet Flow
Expand All @@ -112,7 +114,15 @@ late int id;
wcClient.onSessionProposal.subscribe((SessionProposal? args) async {
// Handle UI updates using the args.params
// Keep track of the args.id for the approval response
id = args!.id;
if (args != null) {
id = args!.id;
// To check VerifyAPI validation in regards of the dApp is trying to connnect you can check verifyContext
// More info about VerifyAPI https://docs.walletconnect.com/web3wallet/verify
final isScamApp = args.verifyContext?.validation.scam;
final isInvalidApp = args.verifyContext?.validation.invalid;
final isValidApp = args.verifyContext?.validation.valid;
final unknown = args.verifyContext?.validation.unknown;
}
});
// Also setup the methods and chains that your wallet supports
Expand Down Expand Up @@ -212,16 +222,13 @@ final walletNamespaces = {
'eip155': Namespace(
accounts: ['eip155:1:abc'],
methods: ['eth_signTransaction'],
),
'kadena': Namespace(
accounts: ['kadena:mainnet01:abc'],
methods: ['kadena_sign_v1', 'kadena_quicksign_v1'],
events: ['kadena_transaction_updated'],
events: ['accountsChanged'],
),
}
await wcClient.approveSession(
id: id,
namespaces: walletNamespaces // This will have the accounts requested in params
// If you registered correctly events emitters, methods handlers and accounts for your supported chains you can just us `args.params.generatedNamespaces!` value from SessionProposalEvent
);
// Or to reject...
// Error codes and reasons can be found here: https://docs.walletconnect.com/2.0/specs/clients/sign/error-codes
Expand All @@ -246,7 +253,7 @@ await wcClient.respondAuthRequest(
// Error codes and reasons can be found here: https://docs.walletconnect.com/2.0/specs/clients/sign/error-codes
await wcClient.respondAuthRequest(
id: args.id,
iss: 'did:pkh:eip155:1:0x06C6A22feB5f8CcEDA0db0D593e6F26A3611d5fa',
iss: 'did:pkh:eip155:1:ETH_ADDRESS',
error: Errors.getSdkError(Errors.USER_REJECTED_AUTH),
);
Expand Down
6 changes: 6 additions & 0 deletions example/dapp/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="myflutterdapp" />
</intent-filter>
</activity>
<!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
Expand Down
17 changes: 17 additions & 0 deletions example/dapp/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,22 @@
<true/>
<key>LSApplicationCategoryType</key>
<string></string>
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLName</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleURLSchemes</key>
<array>
<string>myflutterdapp</string>
</array>
</dict>
</array>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>myflutterwallet</string>
</array>
</dict>
</plist>
55 changes: 22 additions & 33 deletions example/dapp/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class _MyHomePageState extends State<MyHomePage> {
description: 'Flutter WalletConnect Dapp Example',
url: 'https://walletconnect.com/',
icons: ['https://walletconnect.com/walletconnect-logo.png'],
redirect: Redirect(
native: 'myflutterdapp://',
universal: 'https://walletconnect.com',
),
),
);

Expand All @@ -83,6 +87,8 @@ class _MyHomePageState extends State<MyHomePage> {
// Register event handlers
_web3App!.onSessionPing.subscribe(_onSessionPing);
_web3App!.onSessionEvent.subscribe(_onSessionEvent);
_web3App!.core.relayClient.onRelayClientConnect.subscribe(_setState);
_web3App!.core.relayClient.onRelayClientDisconnect.subscribe(_setState);

setState(() {
_pageDatas = [
Expand Down Expand Up @@ -115,8 +121,12 @@ class _MyHomePageState extends State<MyHomePage> {
// }
}

void _setState(dynamic args) => setState(() {});

@override
void dispose() {
_web3App!.core.relayClient.onRelayClientConnect.unsubscribe(_setState);
_web3App!.core.relayClient.onRelayClientDisconnect.unsubscribe(_setState);
_web3App!.onSessionPing.unsubscribe(_onSessionPing);
_web3App!.onSessionEvent.unsubscribe(_onSessionEvent);
super.dispose();
Expand Down Expand Up @@ -146,20 +156,18 @@ class _MyHomePageState extends State<MyHomePage> {
right: StyleConstants.magic20,
child: Row(
children: [
// Disconnect buttons for testing
_buildIconButton(
Icons.discord,
() {
_web3App!.core.relayClient.disconnect();
},
),
const SizedBox(
width: StyleConstants.magic20,
),
_buildIconButton(
Icons.connect_without_contact,
() {
_web3App!.core.relayClient.connect();
Text(_web3App!.core.relayClient.isConnected
? 'Relay Connected'
: 'Relay Disconnected'),
Switch(
value: _web3App!.core.relayClient.isConnected,
onChanged: (value) {
if (!value) {
_web3App!.core.relayClient.disconnect();
} else {
_web3App!.core.relayClient.connect();
}
setState(() {});
},
),
],
Expand Down Expand Up @@ -252,23 +260,4 @@ class _MyHomePageState extends State<MyHomePage> {
},
);
}

Widget _buildIconButton(IconData icon, void Function()? onPressed) {
return Container(
decoration: BoxDecoration(
color: StyleConstants.primaryColor,
borderRadius: BorderRadius.circular(
StyleConstants.linear48,
),
),
child: IconButton(
icon: Icon(
icon,
color: StyleConstants.titleTextColor,
),
iconSize: StyleConstants.linear24,
onPressed: onPressed,
),
);
}
}
Loading

0 comments on commit 241fe01

Please sign in to comment.