Skip to content

Commit e8fef4e

Browse files
ryanleecodejsdw
andauthored
chore: improve docs (#2380)
Co-authored-by: James Wilson <james@jsdw.me>
1 parent 500f0d8 commit e8fef4e

File tree

7 files changed

+80
-29
lines changed

7 files changed

+80
-29
lines changed

.changeset/plenty-taxis-wait.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@substrate/connect-discovery": patch
3+
"@substrate/smoldot-discovery": patch
4+
"@substrate/discovery": patch
5+
"@substrate/connect": patch
6+
---
7+
8+
update docs

README.md

+35-17
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212

1313
- [Table of contents](#table-of-contents)
1414
- [Introduction](#introduction)
15-
- [Overcoming Browser Limitations](#overcoming-browser-limitations)
16-
- [Seamless Integration](#seamless-integration)
15+
- [Write Secure and Effective dApps with the Polkadot Network](#write-secure-and-effective-dapps-with-the-polkadot-network)
16+
- [Additional Resources](#additional-resources)
17+
- [Why Embed a Light Client in Browser Extensions?](#why-embed-a-light-client-in-browser-extensions)
1718
- [Comprehensive API Documentation](#comprehensive-api-documentation)
1819
- [Repository Structure](#repository-structure)
1920
- [Packages](#packages)
@@ -27,16 +28,33 @@
2728

2829
## Introduction
2930

30-
Substrate Connect offers an innovative way to interact with [Substrate](https://substrate.dev/)-based blockchains directly in your browser, eliminating the need for an RPC server. By leveraging the [smoldot](https://github.com/smol-dot/smoldot) WASM light client, it ensures a secure and efficient connection to the blockchain network without dependency on specific third parties.
31+
Substrate Connect provides a groundbreaking method to engage with [Substrate](https://substrate.dev/)-based blockchains directly in your browser, eliminating the need for an RPC server. By utilizing the [smoldot](https://github.com/smol-dot/smoldot) WASM light client, it ensures a secure and efficient connection to the blockchain network without reliance on specific third parties.
3132

32-
### Overcoming Browser Limitations
33+
### Write Secure and Effective dApps with the Polkadot Network
3334

34-
Browser limitations on websockets from HTTPS pages make establishing a robust number of peers challenging, as many nodes must be available with TLS.
35-
Substrate Connect addresses this issue through a powerful browser extension, allowing chains to stay synced in the background, thereby significantly enhancing the performance of your applications.
35+
The aim of this repository is to offer NPM packages that can be used to:
3636

37-
### Seamless Integration
37+
- **Provide a secure and efficient connection to the Polkadot network** via [`@substrate/connect`](./packages/connect/), leveraging the excellent [Smoldot](https://github.com/smol-dot/smoldot) WASM light client.
38+
- **Discover browser extensions** that implement [the discovery protocol](./packages/discovery/), including those which expose a light client via [`@substrate/smoldot-discovery`](./packages/smoldot-discovery/). Substrate Connect will automatically leverage these where possible.
39+
- **Easily enhance a browser extension with a light client** via [`@substrate/light-client-extension-helpers`](./packages/light-client-extension-helpers).
3840

39-
When building an application with Substrate Connect, it automatically detects whether the user has the extension installed and utilizes it. If not, it seamlessly creates the WASM light client in-page for them. Built on [Polkadot JS](https://polkadot.js.org/docs/api), Substrate Connect ensures that your development experience is as smooth as using a traditional RPC server node.
41+
We also provide example projects using the above packages, including:
42+
43+
- **[Basic light client demo](./projects/demo)**: a demo using `@substrate/connect` to obtain information about chains on the Polkadot network.
44+
- **[Light client extension demo](./projects/extension/)**: an example of a browser extension that provides a light client.
45+
- **[Wallet extension demo](./projects/wallet-template/)**: an example of a browser extension that provides a full Polkadot wallet leveraging a light client.
46+
47+
### Additional Resources
48+
49+
- A [step-by-step guide](./projects/wallet-template/STEP-BY-STEP-GUIDE.md) on how to integrate a light client into a browser extension.
50+
- Details on [the discovery protocol](./packages/discovery/), including how to implement it on the browser or extension side.
51+
52+
### Why Embed a Light Client in Browser Extensions?
53+
54+
Embedding a light client in browser extensions offers several advantages:
55+
56+
- **Shared Light Client Across Multiple dApps:** By sharing a single light client among various decentralized applications (dApps), the time spent on startup and syncing is reduced. This avoids slowing down individual dApps and enhances overall efficiency.
57+
- **Overcoming Browser Limitations:** Browser limitations on WebSockets from HTTPS pages make it challenging to establish a robust number of peers, as many nodes must be available with TLS. Substrate Connect addresses this issue through a powerful browser extension, enabling chains to stay synced in the background and significantly improving the performance of your applications. This ensures a more robust connection to the Polkadot ecosystem.
4058

4159
## Comprehensive API Documentation
4260

@@ -45,7 +63,7 @@ For detailed API usage, refer to the [Substrate Connect API documentation](https
4563
## Repository Structure
4664

4765
### [Packages](./packages/README.md)
48-
The core implementations of `@subtrate/connect` and `@substrate/discovery`, and some auxiliary packages.
66+
The core implementations of `@subtrate/connect` and `@substrate/discovery`, and some auxiliary packages.
4967
- **[@substrate/discovery](./packages/discovery/)**
5068
- **[@substrate/connect](./packages/connect/)**
5169
- **[@substrate/connect-known-chains](./packages/connect-known-chains/)**
@@ -61,7 +79,7 @@ For detailed API usage, refer to the [Substrate Connect API documentation](https
6179

6280

6381
### [Examples](./examples/)
64-
dApp and Extensions example implementations of `@substrate/connect` and `@substrate/discovery`.
82+
dApp and Extensions example implementations of `@substrate/connect` and `@substrate/discovery`.
6583

6684
## Development
6785

@@ -75,22 +93,22 @@ Follow these steps to install everything and launch a demo if you're hacking on
7593
- Node.js (node) v20.9.0
7694
- pnpm 9.0.6 (`npm install -g pnpm`)
7795
- corepack 0.20.0 (bundled with recent Node.js versions)
78-
96+
7997
2. **Clone the Repository**:
8098
- `git clone https://github.com/paritytech/substrate-connect.git`
8199
- Navigate to the repository root: `cd substrate-connect`
82-
100+
83101
3. **Install Dependencies**:
84102
- `corepack pnpm install`
85-
103+
86104
4. **Run the Extension in Development Mode**:
87105
- In terminal A: `cd projects/extension && corepack pnpm dev`
88-
106+
89107
5. **Launch the Extension**:
90108
- In terminal B: `cd projects/extension && corepack pnpm start`
91-
- This opens a Chrome browser window with the extension pre-loaded. Ensure
109+
- This opens a Chrome browser window with the extension pre-loaded. Ensure
92110
the extension is running.
93-
111+
94112
6. **Run the Demo Application**:
95113
- In terminal C: `cd projects/demo && corepack pnpm dev`
96114
- Navigate to the URL logged in the Chrome browser opened in step 5. You should see the extension activate and the demo app log the latest blocks.
@@ -109,7 +127,7 @@ corepack pnpm deep-clean
109127

110128
## Releasing
111129

112-
For releasing a new version of the extension, follow the steps outlined in
130+
For releasing a new version of the extension, follow the steps outlined in
113131
[the release doc](./DEPLOY-RELEASE.md).
114132

115133
## Useful Links

packages/connect-discovery/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414

1515
<br /><br />
1616

17+
<div align="center" style="padding: 10px; border: 2px solid red; color: red; font-weight: bold; background-color: #ffe6e6;">
18+
⚠️ WARNING: This interface is currently unstable and is likely to change ⚠️
19+
</div>
20+
21+
<br /><br />
22+
1723
A TypeScript package extended from the [`@substrate/discovery`](../discovery/README.md) npm module, that allows to discover and filter Substrate Connect Extension providers from a list of providers.
1824

1925
## Installation

packages/connect/README.md

+10-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ The main implementation of the light-client provider for a given substrate-based
1919

2020
## Using `@substrate/connect` for library authors
2121

22-
Provide a well-known chain name ('polkadot', 'ksmcc3', 'westend2', 'rococo_v2_2'):
22+
The `connect` package will look for a light client provider via the discovery protocol. If it can't find one, it will spawn a smoldot instance in the user's browser tab.
23+
24+
### Example Usage
25+
26+
To use a well-known chain ('polkadot', 'ksmcc3', 'westend2', 'rococo_v2_2'):
2327

2428
```js
2529
import { createScClient, WellKnownChain } from '@substrate/connect';
@@ -37,7 +41,7 @@ chain.sendJsonRpc(
3741
);
3842
```
3943

40-
...or provide your custom substrate chain's name and chainspec:
44+
To use a custom substrate chain's name and chainspec:
4145

4246
```js
4347
import { createScClient } from '@substrate/connect';
@@ -58,10 +62,9 @@ chain.sendJsonRpc(
5862
);
5963
```
6064

61-
In order to connect to a parachain, you must first instantiate the relay chain
62-
this parachain is connected to, then instantiate the parachain on the same
63-
relay chain. The following example connects to a parachain of the Westend test
64-
network:
65+
### Connecting to a Parachain
66+
67+
To connect to a parachain, you must first instantiate the relay chain this parachain is connected to, and then instantiate the parachain on the same relay chain. The following example connects to a parachain of the Westend test network:
6568

6669
```js
6770
import { createScClient, WellKnownChain } from '@substrate/connect';
@@ -70,7 +73,7 @@ import jsonParachainSpec from './myParaChainSpec.json';
7073
const parachainSpec = JSON.stringify(jsonParachainSpec);
7174

7275
const scClient = createScClient();
73-
const relayChain = await scClient.addWellKnownChain(WellKnownChain.westend2)
76+
const relayChain = await scClient.addWellKnownChain(WellKnownChain.westend2);
7477
const parachain = await relayChain.addChain(
7578
parachainSpec,
7679
function jsonRpcCallback(response) {

packages/connect/src/connector/index.ts

+9
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ export interface Config {
3434
* Returns a {@link ScClient} that connects to chains, either through the substrate-connect
3535
* extension or by executing a light client directly from JavaScript, depending on whether the
3636
* extension is installed and available.
37+
*
38+
* The substrate-connect extension is identified via the `@substrate/discovery` protocol.
39+
*
40+
* It must:
41+
*
42+
* 1. Be compliant `@substrate/smoldot-discovery` interface
43+
* 2. Include an rdns label starting with `io.github.paritytech.SubstrateConnect`
44+
*
3745
*/
3846
export const createScClient = (config?: Config): ScClient => {
3947
if (config?.forceEmbeddedNode)
@@ -66,6 +74,7 @@ function getSmoldotProviderPromise(): Promise<SmoldotExtensionAPI> | undefined {
6674
if (typeof document !== "object" || typeof CustomEvent !== "function") return
6775
const lightClientProvider = getSmoldotExtensionProviders()
6876
.filter((detail) =>
77+
// Filter for Substrate Connect to find the correct provider among multiple providers.
6978
detail.info.rdns.startsWith("io.github.paritytech.SubstrateConnect"),
7079
)
7180
.map((detail) => detail.provider)[0]

packages/discovery/README.md

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@ The main export is a function called `getProviders`. This function dispatches an
2222

2323
## How It Works
2424

25-
The extension injects an inpage script that:
25+
The discovery protocol is quite simple and can be implemented in these steps:
2626

27-
- Registers a listener for the `substrateDiscovery:requestProvider` event and announces the provider by invoking synchronously the `onProvider` callback from the event payload.
28-
- Optionally, dispatches the `substrateDiscovery:announceProvider` event with the provider details when the script is loaded.
27+
1. The extension injects an inpage script that registers a listener for the `substrateDiscovery:requestProvider` event.
28+
2. The listener announces the provider by invoking the `onProvider` callback from the event payload synchronously.
29+
3. Optionally, the script can dispatch the `substrateDiscovery:announceProvider` event with the provider details when the script is loaded.
2930

30-
## Basic Example
31+
Refer to `src/index.ts` in this package for an implementation of this protocol.
32+
33+
## Basic Usage Example
3134

3235
```ts
3336
import { getProviders } from "@substrate/discovery"
@@ -40,6 +43,8 @@ console.log(firstProvider)
4043

4144
## Example with rDNS Filter
4245

46+
This example demonstrates how to filter providers based on a specific rDNS value. This approach is useful when you need to target specific extensions rather than all extensions matching a certain interface.
47+
4348
```ts
4449
import { getProviders } from "@substrate/discovery"
4550

packages/smoldot-discovery/README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414

1515
<br /><br />
1616

17-
A TypeScript package extended from the [`@substrate/discovery`](../discovery/README.md) NPM package, that allows to discover and filter Smoldot extension providers from a list of providers.
17+
A TypeScript package extended from the [`@substrate/discovery`](../discovery/README.md) NPM package, which enables the discovery and filtering of Smoldot extension providers from a list of providers.
18+
19+
To be utilized by dApps, extensions should implement the `SmoldotExtensionProviderDetail` interface. This can be achieved by following the extension side of the discovery protocol as detailed [here](../discovery/README.md), and then returning the implemented interface.
1820

1921
## Installation
2022

0 commit comments

Comments
 (0)