Skip to content

Commit

Permalink
squash commits
Browse files Browse the repository at this point in the history
  • Loading branch information
marshacb committed May 2, 2024
1 parent 00214f8 commit c4e1508
Show file tree
Hide file tree
Showing 79 changed files with 2,612 additions and 855 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/auto-merge.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
# will not occur.
- name: Dependabot metadata
id: dependabot-metadata
uses: dependabot/fetch-metadata@v2.0.0
uses: dependabot/fetch-metadata@v2.1.0
with:
github-token: "${{ secrets.GITHUB_TOKEN }}"
# Here the PR gets approved.
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
node-version: [18.x, 20.x]

steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
with:
node-version: ${{ matrix.node-version }}
Expand All @@ -25,7 +25,7 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- run: pnpm test:flaky
working-directory: ./packages/connect
Expand All @@ -35,7 +35,7 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- name: Build extension with Manifest v3 for Chrome
run: pnpm build:chrome
Expand All @@ -56,7 +56,7 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- run: pnpm playwright:install
working-directory: ./examples/light-client-extension-helpers-extension
Expand All @@ -74,7 +74,7 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- name: Build extension with Manifest v3 for Chrome
run: pnpm build:chrome
Expand All @@ -95,7 +95,7 @@ jobs:
timeout-minutes: 10
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- name: Get zombienet
run: |
Expand Down Expand Up @@ -129,7 +129,7 @@ jobs:
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- name: "@substrate/connect Publish"
id: publish_connect
Expand Down Expand Up @@ -167,7 +167,7 @@ jobs:
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: actions/upload-artifact@v4
with:
name: substrate-connect-extension-source-code
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/deploy-projects-and-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
if: github.ref == 'refs/heads/main'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
- uses: ./.github/actions/turbo-build
- name: Fetch gh-pages
run: |
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/update-chain-specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
fail-fast: false # Don't automatically cancel the jobs of the other RPC nodes if one fails

steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
with:
persist-credentials: false
path: repo
Expand Down Expand Up @@ -68,7 +68,7 @@ jobs:
if: ${{ always() }} # Run this job even if one of the steps of download-spec has failed
needs: download-spec
steps:
- uses: actions/checkout@v4.1.2
- uses: actions/checkout@v4.1.4
with:
path: repo
ref: main
Expand Down Expand Up @@ -107,7 +107,7 @@ jobs:
exit 1
- name: Create issue on failure
if: failure()
uses: actions/github-script@v6
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
Expand Down
72 changes: 72 additions & 0 deletions docs/multi-injected-wallet-provider-discovery.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Multi Injected Wallet Provider Discovery

This is a proposal that uses window [`CustomEvent`](https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent)s to announce injected Polkadot Wallet Providers.

Based on [EIP-6963](https://eips.ethereum.org/EIPS/eip-6963), it's an alternative discovery mechanism to [`window.injectedWeb3`](https://github.com/polkadot-js/extension?tab=readme-ov-file#injection-information) for PolkadotJS wallet providers.

The discovery is achieved by introducing a set of windows events to provide a two-way commnunication protocol between Polkadot Wallet Provider libraries and injected scripts providerd by browser extensions.

## How it works?

The exension [injects](/projects/wallet-template/src/content/index.ts#L4) an [inpage](/projects/wallet-template/src/inpage/index.ts#L55) script that

- registers a listener for the `unstableWallet:requestProvider` event and anounces the provider by invoking synchronously the `onProvider` callback from the event payload
- optionally, dispatches the `unstableWallet:announceProvider` event with the provider details when the script is loaded

For example:

```ts
const detail = Object.freeze({
info: {
uuid: crypto.randomUUID(),
name: "Substrate Connect Light Client",
icon: "data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg'/>",
rdns: "io.github.paritytech.SubstrateConnectLightClient",
},
provider: getProvider(), // getProvider returns a Promise<Provider>
})

window.addEventListener(
"unstableWallet:requestProvider",
({ detail: { onProvider } }) => onProvider(detail),
)

window.dispatchEvent(
new CustomEvent("unstableWallet:announceProvider", {
detail,
}),
)
```

Note:

- the `detail.provider` is a promise, this allows to announce the provider details while the provider is being initialized
- the `unstableWallet:requestProvider` event payload uses an `onProvider` callback to respond with the provider details synchronously to the DApp, this allows to get all the providers without needing to wait for any macrotasks (e.g. `setTimeout`), or microtasks to complete, or any arbitrary time to listen to an event (e.g. `unstableWallet:announceProvider`).

Then, the DApp

- dispatches the `unstableWallet:requestProvider` and stores the announced provider details
- optionally, registers a listener for the `unstableWallet:announceProvider` event and stores announced providers details

For example, all the available providers could be discovered with

```ts
export const getProviders = () => {
const providers = []
window.dispatchEvent(
new CustomEvent("unstableWallet:requestProvider", {
detail: {
onProvider(detail) {
providers.push(detail)
},
},
}),
)
return providers
}
```

Note:

- the event types and payloads are documented in [`packages/unstable-wallet-provider/UnstableWalletProviderDiscovery.d.ts`](/packages/unstable-wallet-provider/UnstableWalletProviderDiscovery.d.ts)
- the `unstableWallet:` event namespace should be changed to a better name
10 changes: 7 additions & 3 deletions examples/light-client-dapp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,17 @@
},
"dependencies": {
"@picocss/pico": "^2.0.6",
"@polkadot-api/observable-client": "^0.1.0",
"@polkadot-api/client": "^0.1.0",
"@polkadot-api/metadata-builders": "0.0.1",
"@polkadot-api/observable-client": "^0.1.1",
"@polkadot-api/substrate-bindings": "0.0.1",
"@polkadot-api/substrate-client": "0.0.1",
"@polkadot-api/utils": "0.0.1",
"@polkadot-labs/hdkd-helpers": "^0.0.6",
"@substrate/connect-known-chains": "workspace:^",
"@zag-js/react": "^0.48.0",
"@zag-js/select": "^0.48.0",
"@zag-js/toast": "^0.48.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"rxjs": "^7.8.1",
Expand All @@ -27,11 +31,11 @@
"@substrate/unstable-wallet-provider": "workspace:^",
"@types/react": "^18.2.78",
"@types/react-dom": "^18.2.25",
"@typescript-eslint/eslint-plugin": "^7.6.0",
"@typescript-eslint/eslint-plugin": "^7.7.0",
"@typescript-eslint/parser": "^7.7.0",
"@vitejs/plugin-react-swc": "^3.5.0",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.6",
"vite": "^5.2.8"
}
Expand Down
30 changes: 15 additions & 15 deletions examples/light-client-dapp/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { Transfer, ConnectedAccount } from "./components"
import { Transfer, ConnectedAccount, ChainSelect } from "./components"
import { ToastProvider } from "./components"
import { UnstableProviderProvider } from "./hooks/useUnstableProvider"

// FIXME: use dynamic chainId
// Westend chainId
const chainId =
"0xe143f23803ac50e8f6f8e62695d1ce9e4e1d68aa36c1cd2cfd15340213f3423e"
import { DEFAULT_CHAIN_ID } from "./settings"

export const App = () => {
return (
<UnstableProviderProvider chainId={chainId}>
<main className="container" style={{ maxWidth: "700px" }}>
<header>
<h1>Light Client DApp</h1>
</header>
<ConnectedAccount />
<Transfer />
</main>
</UnstableProviderProvider>
<ToastProvider>
<UnstableProviderProvider defaultChainId={DEFAULT_CHAIN_ID}>
<main className="container" style={{ maxWidth: "700px" }}>
<header>
<h1>Light Client DApp</h1>
</header>
<ChainSelect />
<ConnectedAccount />
<Transfer />
</main>
</UnstableProviderProvider>
</ToastProvider>
)
}
26 changes: 12 additions & 14 deletions examples/light-client-dapp/src/api/systemAccount$.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getDynamicBuilder } from "@polkadot-api/metadata-builders"

import { UnstableWallet } from "@substrate/unstable-wallet-provider"
import { distinct, filter, finalize, map, mergeMap } from "rxjs"
import { combineLatest, distinct, filter, finalize, map, mergeMap } from "rxjs"
import { getObservableClient } from "./getObservableClient"

export type SystemAccount = {
Expand All @@ -24,23 +24,21 @@ export const systemAccount$ = (
) => {
const client = getObservableClient(provider, chainId)
const { metadata$, finalized$, unfollow, storage$ } = client.chainHead$()
return metadata$.pipe(
filter(Boolean),
mergeMap((metadata) => {
return combineLatest([
metadata$.pipe(filter(Boolean)),
finalized$.pipe(filter(Boolean)),
]).pipe(
mergeMap(([metadata, blockInfo]) => {
const storageAccount = getDynamicBuilder(metadata).buildStorage(
"System",
"Account",
)
return finalized$.pipe(
mergeMap((blockInfo) =>
storage$(blockInfo.hash, "value", () =>
storageAccount.enc(address),
).pipe(
filter(Boolean),
distinct(),
map((value) => storageAccount.dec(value) as SystemAccount),
),
),
return storage$(blockInfo.hash, "value", () =>
storageAccount.enc(address),
).pipe(
filter(Boolean),
distinct(),
map((value) => storageAccount.dec(value) as SystemAccount),
)
}),
finalize(unfollow),
Expand Down
Loading

0 comments on commit c4e1508

Please sign in to comment.