Skip to content

Commit be5e5d1

Browse files
committed
Merge branch 'docs/js-notify-rework' into web3inbox
2 parents 4552d8a + e956c43 commit be5e5d1

File tree

1 file changed

+102
-74
lines changed

1 file changed

+102
-74
lines changed

docs/api/notify/usage.mdx

Lines changed: 102 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,16 @@ The above step is not required if you are using the SDK on the browser-side.
3535

3636
```javascript
3737
import { Core } from '@walletconnect/core'
38-
import { SyncClient, SyncStore } from '@walletconnect/sync-client'
3938
import { NotifyClient } from '@walletconnect/notify-client'
4039

4140
// Initialize core separately to allow sharing it between Sync and Notify
4241
const core = new Core({
4342
projectId: '<YOUR PROJECT ID>'
4443
})
4544

46-
// SyncClient enables syncing data across devices
47-
const syncClient = await SyncClient.init({
48-
projectId: '<YOUR PROJECT ID>',
49-
core // <- pass the shared `core` instance
50-
})
51-
5245
const notifyClient = await NotifyClient.init({
5346
core, // <- pass the shared `core` instance
54-
SyncStoreController: SyncStore,
55-
syncClient: syncClient
47+
projectId: '<YOUR PROJECT ID>'
5648
})
5749
```
5850

@@ -61,46 +53,52 @@ const notifyClient = await NotifyClient.init({
6153
```javascript
6254
// Handle response to a `notifyClient.subscribe(...)` call
6355
notifyClient.on('notify_subscription', async ({ params }) => {
64-
const { error, subscription } = params
56+
const { error } = params
6557

66-
if (subscription) {
67-
// New subscription was successfully created.
68-
// Inform the user and/or update app state to reflect the new subscription.
69-
console.log(`Successfully subscribed to ${subscription.metadata.name}.`)
70-
} else if (error) {
58+
if (error) {
7159
// Setting up the subscription failed.
7260
// Inform the user of the error and/or clean up app state.
7361
console.error('Setting up subscription failed: ', error)
62+
} else {
63+
// New subscription was successfully created.
64+
// Inform the user and/or update app state to reflect the new subscription.
65+
console.log(`Successfully subscribed to ${subscription.metadata.name}.`)
7466
}
7567
})
7668

7769
// Handle an incoming notification
78-
notifyClient.on('notify_message', async ({ params }) => {
70+
notifyClient.on('notify_message', ({ params }) => {
7971
const { message } = params
8072
// e.g. build a notification using the metadata from `message` and show to the user.
8173
})
8274

83-
8475
// Handle response to a `notifyClient.update(...)` call
85-
notifyClient.on('notify_update', async ({ params }) => {
86-
const { error, subscription } = params
76+
notifyClient.on('notify_update', ({ params }) => {
77+
const { error } = params
8778

88-
if (subscription) {
89-
// Subscription's scope was updated successfully.
90-
// Inform the user and/or update app state to reflect the updated subscription.
91-
console.log(`Successfully updated subscription scope for ${subscription.metadata.name}.`)
92-
console.log("New subscription scope is: ", subscription.scope)
93-
} else if (error) {
79+
if (error) {
9480
// Updating the subscription's scope failed.
9581
// Inform the user of the error and/or clean up app state.
9682
console.error('Setting up subscription failed: ', error)
83+
} else {
84+
// Subscription's scope was updated successfully.
85+
// Inform the user and/or update app state to reflect the updated subscription.
86+
console.log(`Successfully updated subscription scope.`)
9787
}
88+
})
89+
90+
notifyClient.on('notify_subscriptions_changed', ({ params }) => {
91+
const { subscriptions } = params
92+
// e.g. update app state to reflect changes in specific subscriptions
93+
// `subscriptions` will contain any *changed* subscriptions since the last time this event was emitted.
94+
// To get a full list of subscriptions for a given account you can use `notifyClient.getActiveSubscriptions({ account: 'eip155:1:0x63Be...' })`
95+
})
9896
```
9997

10098
## Register an identity key for cross-device account syncing
10199

102100
:::note
103-
This is a one-time action that does not need to be repeated after initial registration of the new account.
101+
This is a one-time action per account. It does not need to be repeated after initial registration of the new account.
104102
:::
105103

106104
To register an identity key, you must provide a callback to the `onSign: (message: string) => string` parameter of the `register` method.
@@ -111,13 +109,31 @@ Some suggested ways to implement the `onSign` callback are via:
111109
- Ethers.js [`Wallet.signMessage` method](https://docs.ethers.org/v5/api/signer/#Signer-signMessage)
112110
- The [`signMessage` method](https://wagmi.sh/core/actions/signMessage) in `@wagmi/core`
113111

112+
### Registering as a dapp
113+
114114
```javascript
115115
const account = `eip155:1:0x63Be2c680685d2A9620c11b0068291261aa62d76`
116116
const onSign = (message: string) => ethersWallet.signMessage(message)
117117

118118
await notifyClient.register({
119119
account,
120-
onSign
120+
onSign,
121+
domain: 'app.mydomain.com', // pass the domain (i.e. the hostname) where your dapp is hosted.
122+
isLimited: true // The user will be prompted to authorize this dapp to send and receive messages on their behalf for this domain using their WalletConnect identity.
123+
})
124+
```
125+
126+
### Registering as a wallet
127+
128+
```javascript
129+
const account = `eip155:1:0x63Be2c680685d2A9620c11b0068291261aa62d76`
130+
const onSign = (message: string) => ethersWallet.signMessage(message)
131+
132+
await notifyClient.register({
133+
account,
134+
onSign,
135+
domain: 'com.mydomain.app.rn', // pass your app's bundle identifier.
136+
isLimited: false // The user will be prompted to authorize this wallet to send and receive messages on their behalf for ALL domains using their WalletConnect identity.
121137
})
122138
```
123139

@@ -128,35 +144,32 @@ await notifyClient.register({
128144
:::info
129145
To identify dapps that can be subscribed to via Notify, we can query the following Explorer API endpoint:
130146

131-
https://explorer-api.walletconnect.com/v3/dapps?projectId={YOUR_PROJECT_ID}&is_notify_enabled=true
147+
https://explorer-api.walletconnect.com/v3/dapps?projectId=YOUR_PROJECT_ID&is_notify_enabled=true
132148
:::
133149

134150
```javascript
135151
// Subscribe to `fetchedExplorerDapp` by passing the account to be subscribed and the relevant metadata for the target dapp.
136152
await notifyClient.subscribe({
137153
account,
138-
metadata: {
139-
name: fetchedExplorerDapp.name,
140-
description: fetchedExplorerDapp.description,
141-
icons: Object.values(fetchedExplorerDapp.image_url),
142-
url: fetchedExplorerDapp.homepage
143-
}
154+
appDomain
144155
})
145156

146-
// -> Result will be received via the `notify_subscription` event registered previously.
157+
// -> Success/Failure will be received via the `notify_update` event registered previously.
158+
// -> New subscription will be emitted via the `notify_subscriptions_changed` watcher event.
147159
```
148160

149161
#### Updating notification types on an existing subscription
150162

151163
```javascript
152164
// `topic` - subscription topic of the subscription that should be updated.
153-
// `scope` - an array of notification types that should be enabled going forward. The current scope is found under `subscription.scope`.
165+
// `scope` - an array of notification types that should be enabled going forward. The current scopes can be found under `subscription.scope`.
154166
await notifyClient.update({
155167
topic,
156168
scope: ['alerts']
157169
})
158170

159-
// -> Result will be received via the `notify_update` event registered previously.
171+
// -> Success/Failure will be received via the `notify_update` event registered previously.
172+
// -> Updated subscription will be emitted via the `notify_subscriptions_changed` watcher event.
160173
```
161174

162175
#### Removing an existing subscription
@@ -541,11 +554,13 @@ val walletDelegate = object : NotifyClient.Delegate {
541554

542555
NotifyClient.setDelegate(walletDelegate)
543556
```
557+
544558
#### Register Identity at a Keyserver
545559

546560
In order to use Notify SDK account must register identity in [Keyserver](https://docs.walletconnect.com/2.0/specs/servers/keys/). To verify ownership over blockchain account when registering identities in [Keyserver](https://docs.walletconnect.com/2.0/specs/servers/keys/) user's must sign message provided on `onSign(message: String)` callback. Currenlty only [`EIP191`](https://eips.ethereum.org/EIPS/eip-191) signatures are supported in [Keyserver](https://docs.walletconnect.com/2.0/specs/servers/keys/)
547561

548562
##### NotifyClient.register
563+
549564
```kotlin
550565
val params = Notify.Params.Registration(
551566
account = /*[CAIP-10](https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md) compatible accountId*/,
@@ -674,24 +689,16 @@ npm install @walletconnect/notify-client @walletconnect/react-native-compat
674689

675690
```javascript
676691
import { Core } from '@walletconnect/core'
677-
import { SyncClient, SyncStore } from '@walletconnect/sync-client'
678692
import { NotifyClient } from '@walletconnect/notify-client'
679693

680694
// Initialize core separately to allow sharing it between Sync and Notify
681695
const core = new Core({
682696
projectId: '<YOUR PROJECT ID>'
683697
})
684698

685-
// SyncClient enables syncing data across devices
686-
const syncClient = await SyncClient.init({
687-
projectId: '<YOUR PROJECT ID>',
688-
core // <- pass the shared `core` instance
689-
})
690-
691699
const notifyClient = await NotifyClient.init({
692700
core, // <- pass the shared `core` instance
693-
SyncStoreController: SyncStore,
694-
syncClient: syncClient
701+
projectId: '<YOUR PROJECT ID>'
695702
})
696703
```
697704

@@ -700,46 +707,52 @@ const notifyClient = await NotifyClient.init({
700707
```javascript
701708
// Handle response to a `notifyClient.subscribe(...)` call
702709
notifyClient.on('notify_subscription', async ({ params }) => {
703-
const { error, subscription } = params
710+
const { error } = params
704711

705-
if (subscription) {
706-
// New subscription was successfully created.
707-
// Inform the user and/or update app state to reflect the new subscription.
708-
console.log(`Successfully subscribed to ${subscription.metadata.name}.`)
709-
} else if (error) {
712+
if (error) {
710713
// Setting up the subscription failed.
711714
// Inform the user of the error and/or clean up app state.
712715
console.error('Setting up subscription failed: ', error)
716+
} else {
717+
// New subscription was successfully created.
718+
// Inform the user and/or update app state to reflect the new subscription.
719+
console.log(`Successfully subscribed to ${subscription.metadata.name}.`)
713720
}
714721
})
715722

716723
// Handle an incoming notification
717-
notifyClient.on('notify_message', async ({ params }) => {
724+
notifyClient.on('notify_message', ({ params }) => {
718725
const { message } = params
719726
// e.g. build a notification using the metadata from `message` and show to the user.
720727
})
721728

722-
723729
// Handle response to a `notifyClient.update(...)` call
724-
notifyClient.on('notify_update', async ({ params }) => {
725-
const { error, subscription } = params
730+
notifyClient.on('notify_update', ({ params }) => {
731+
const { error } = params
726732

727-
if (subscription) {
728-
// Subscription's scope was updated successfully.
729-
// Inform the user and/or update app state to reflect the updated subscription.
730-
console.log(`Successfully updated subscription scope for ${subscription.metadata.name}.`)
731-
console.log("New subscription scope is: ", subscription.scope)
732-
} else if (error) {
733+
if (error) {
733734
// Updating the subscription's scope failed.
734735
// Inform the user of the error and/or clean up app state.
735736
console.error('Setting up subscription failed: ', error)
737+
} else {
738+
// Subscription's scope was updated successfully.
739+
// Inform the user and/or update app state to reflect the updated subscription.
740+
console.log(`Successfully updated subscription scope.`)
736741
}
742+
})
743+
744+
notifyClient.on('notify_subscriptions_changed', ({ params }) => {
745+
const { subscriptions } = params
746+
// e.g. update app state to reflect changes in specific subscriptions
747+
// `subscriptions` will contain any *changed* subscriptions since the last time this event was emitted.
748+
// To get a full list of subscriptions for a given account you can use `notifyClient.getActiveSubscriptions({ account: 'eip155:1:0x63Be...' })`
749+
})
737750
```
738751

739752
## Register an identity key for cross-device account syncing
740753

741754
:::note
742-
This is a one-time action that does not need to be repeated after initial registration of the new account.
755+
This is a one-time action per account. It does not need to be repeated after initial registration of the new account.
743756
:::
744757

745758
To register an identity key, you must provide a callback to the `onSign: (message: string) => string` parameter of the `register` method.
@@ -750,13 +763,31 @@ Some suggested ways to implement the `onSign` callback are via:
750763
- Ethers.js [`Wallet.signMessage` method](https://docs.ethers.org/v5/api/signer/#Signer-signMessage)
751764
- The [`signMessage` method](https://wagmi.sh/core/actions/signMessage) in `@wagmi/core`
752765

766+
### Registering as a dapp
767+
753768
```javascript
754769
const account = `eip155:1:0x63Be2c680685d2A9620c11b0068291261aa62d76`
755770
const onSign = (message: string) => ethersWallet.signMessage(message)
756771

757772
await notifyClient.register({
758773
account,
759-
onSign
774+
onSign,
775+
domain: 'app.mydomain.com', // pass the domain (i.e. the hostname) where your dapp is hosted.
776+
isLimited: true // The user will be prompted to authorize this dapp to send and receive messages on their behalf for this domain using their WalletConnect identity.
777+
})
778+
```
779+
780+
### Registering as a wallet
781+
782+
```javascript
783+
const account = `eip155:1:0x63Be2c680685d2A9620c11b0068291261aa62d76`
784+
const onSign = (message: string) => ethersWallet.signMessage(message)
785+
786+
await notifyClient.register({
787+
account,
788+
onSign,
789+
domain: 'com.mydomain.app.rn', // pass your app's bundle identifier.
790+
isLimited: false // The user will be prompted to authorize this wallet to send and receive messages on their behalf for ALL domains using their WalletConnect identity.
760791
})
761792
```
762793

@@ -767,35 +798,32 @@ await notifyClient.register({
767798
:::info
768799
To identify dapps that can be subscribed to via Notify, we can query the following Explorer API endpoint:
769800

770-
https://explorer-api.walletconnect.com/v3/dapps?projectId={YOUR_PROJECT_ID}&is_notify_enabled=true
801+
https://explorer-api.walletconnect.com/v3/dapps?projectId=YOUR_PROJECT_ID&is_notify_enabled=true
771802
:::
772803

773804
```javascript
774805
// Subscribe to `fetchedExplorerDapp` by passing the account to be subscribed and the relevant metadata for the target dapp.
775806
await notifyClient.subscribe({
776807
account,
777-
metadata: {
778-
name: fetchedExplorerDapp.name,
779-
description: fetchedExplorerDapp.description,
780-
icons: Object.values(fetchedExplorerDapp.image_url),
781-
url: fetchedExplorerDapp.homepage
782-
}
808+
appDomain
783809
})
784810

785-
// -> Result will be received via the `notify_subscription` event registered previously.
811+
// -> Success/Failure will be received via the `notify_update` event registered previously.
812+
// -> New subscription will be emitted via the `notify_subscriptions_changed` watcher event.
786813
```
787814

788815
#### Updating notification types on an existing subscription
789816

790817
```javascript
791818
// `topic` - subscription topic of the subscription that should be updated.
792-
// `scope` - an array of notification types that should be enabled going forward. The current scope is found under `subscription.scope`.
819+
// `scope` - an array of notification types that should be enabled going forward. The current scopes can be found under `subscription.scope`.
793820
await notifyClient.update({
794821
topic,
795822
scope: ['alerts']
796823
})
797824

798-
// -> Result will be received via the `notify_update` event registered previously.
825+
// -> Success/Failure will be received via the `notify_update` event registered previously.
826+
// -> Updated subscription will be emitted via the `notify_subscriptions_changed` watcher event.
799827
```
800828

801829
#### Removing an existing subscription

0 commit comments

Comments
 (0)