@substrate/connect
Advanced tools
Comparing version 0.8.5 to 0.8.6
@@ -7,2 +7,9 @@ # Changelog | ||
## 0.8.6 - 2024-02-21 | ||
### Changed | ||
- Update smoldot@[version 2.0.21](https://github.com/smol-dot/smoldot/blob/main/wasm-node/CHANGELOG.md#2021---2024-02-06) [#1843](https://github.com/paritytech/substrate-connect/pull/1843) | ||
- Use LightClient provider announced by the extension with custom events [#1845](https://github.com/paritytech/substrate-connect/pull/1845) | ||
## 0.8.5 - 2024-01-18 | ||
@@ -9,0 +16,0 @@ |
@@ -196,19 +196,2 @@ /** | ||
/** | ||
* `true` if the substrate-connect extension is installed and available. | ||
* | ||
* Always `false` when outside of a browser environment. | ||
* | ||
* We detect this based on the presence of a DOM element with a specific `id`. See | ||
* `connect-extension-protocol`. | ||
* | ||
* Note that the value is determined at initialization and will not change even if the user | ||
* enables, disables, installs, or uninstalls the extension while the script is running. These | ||
* situations are very niche, and handling them properly would add a lot of complexity that isn't | ||
* worth it. | ||
* | ||
* This constant is mostly for informative purposes, for example to display a message in a UI | ||
* encouraging the user to install the extension. | ||
*/ | ||
declare const isExtensionPresent: boolean; | ||
/** | ||
* Configuration that can be passed to {createScClient}. | ||
@@ -237,4 +220,4 @@ */ | ||
*/ | ||
declare function createScClient(config?: Config): ScClient; | ||
declare const createScClient: (config?: Config) => ScClient; | ||
export { type AddChain, type AddWellKnownChain, AlreadyDestroyedError, type Chain, type Config, CrashError, type Config$1 as EmbeddedNodeConfig, type JsonRpcCallback, JsonRpcDisabledError, type ScClient, WellKnownChain, createScClient, isExtensionPresent }; | ||
export { type AddChain, type AddWellKnownChain, AlreadyDestroyedError, type Chain, type Config, CrashError, type Config$1 as EmbeddedNodeConfig, type JsonRpcCallback, JsonRpcDisabledError, type ScClient, WellKnownChain, createScClient }; |
@@ -331,3 +331,2 @@ var __defProp = Object.defineProperty; | ||
// src/connector/extension.ts | ||
import { DOM_ELEMENT_ID } from "@substrate/connect-extension-protocol"; | ||
var wellKnownChainGenesisHashes = { | ||
@@ -339,8 +338,3 @@ polkadot: "0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3", | ||
}; | ||
var lightClientProviderPromise; | ||
var createScClient2 = () => { | ||
if (!lightClientProviderPromise) | ||
lightClientProviderPromise = import("@substrate/light-client-extension-helpers/web-page").then( | ||
({ getLightClientProvider }) => getLightClientProvider(DOM_ELEMENT_ID) | ||
); | ||
var createScClient2 = (lightClientProviderPromise) => { | ||
const internalAddChain = (isWellKnown, chainSpecOrWellKnownName, jsonRpcCallback = () => { | ||
@@ -388,10 +382,44 @@ }, relayChainGenesisHash) => __async(void 0, null, function* () { | ||
// src/connector/index.ts | ||
import { DOM_ELEMENT_ID as DOM_ELEMENT_ID2 } from "@substrate/connect-extension-protocol"; | ||
var _a; | ||
var isExtensionPresent = typeof document === "object" && typeof document.getElementById === "function" && !!document.getElementById(DOM_ELEMENT_ID2) && ((_a = document.getElementById(DOM_ELEMENT_ID2)) == null ? void 0 : _a.getAttribute("channelid")) === DOM_ELEMENT_ID2; | ||
function createScClient3(config) { | ||
const forceEmbedded = config == null ? void 0 : config.forceEmbeddedNode; | ||
if (!forceEmbedded && isExtensionPresent) | ||
return createScClient2(); | ||
return createScClient(config == null ? void 0 : config.embeddedNodeConfig); | ||
var createScClient3 = (config) => { | ||
if (config == null ? void 0 : config.forceEmbeddedNode) | ||
return createScClient(config == null ? void 0 : config.embeddedNodeConfig); | ||
const lightClientProviderPromise = getExtensionLightClientProviderPromise(); | ||
const client = lightClientProviderPromise ? createScClient2(lightClientProviderPromise) : createScClient(config == null ? void 0 : config.embeddedNodeConfig); | ||
return { | ||
addChain(chainSpec, jsonRpcCallback, databaseContent) { | ||
return __async(this, null, function* () { | ||
return (yield client).addChain( | ||
chainSpec, | ||
jsonRpcCallback, | ||
databaseContent | ||
); | ||
}); | ||
}, | ||
addWellKnownChain(id, jsonRpcCallback, databaseContent) { | ||
return __async(this, null, function* () { | ||
return (yield client).addWellKnownChain( | ||
id, | ||
jsonRpcCallback, | ||
databaseContent | ||
); | ||
}); | ||
} | ||
}; | ||
}; | ||
function getExtensionLightClientProviderPromise() { | ||
if (typeof document !== "object" || typeof CustomEvent !== "function") | ||
return; | ||
let lightClientProviderPromise; | ||
window.dispatchEvent( | ||
new CustomEvent("lightClient:requestProvider", { | ||
detail: { | ||
onProvider(detail) { | ||
if (detail.info.rdns === "io.github.paritytech.SubstrateConnectLightClient") { | ||
lightClientProviderPromise = detail.provider; | ||
} | ||
} | ||
} | ||
}) | ||
); | ||
return lightClientProviderPromise; | ||
} | ||
@@ -403,5 +431,4 @@ export { | ||
WellKnownChain, | ||
createScClient3 as createScClient, | ||
isExtensionPresent | ||
createScClient3 as createScClient | ||
}; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@substrate/connect", | ||
"version": "0.8.5", | ||
"version": "0.8.6", | ||
"description": "Substrate-connect to Smoldot clients. Using either substrate extension with predefined clients or an internal smoldot client based on chainSpecs provided.", | ||
@@ -37,15 +37,18 @@ "author": "Parity Team <admin@parity.io>", | ||
"dependencies": { | ||
"smoldot": "2.0.17", | ||
"smoldot": "2.0.21", | ||
"@substrate/light-client-extension-helpers": "^0.0.2", | ||
"@substrate/connect-known-chains": "^1.0.6", | ||
"@substrate/connect-extension-protocol": "^2.0.0" | ||
"@substrate/connect-extension-protocol": "^2.0.0", | ||
"@substrate/connect-known-chains": "^1.0.8" | ||
}, | ||
"devDependencies": { | ||
"@polkadot-api/json-rpc-provider-proxy": "0.0.1-12c4b0432a814086c3c1a3b8052b31c72c2c9ad3.1.0", | ||
"@polkadot-api/substrate-client": "0.0.1-12c4b0432a814086c3c1a3b8052b31c72c2c9ad3.1.0", | ||
"eslint": "^8.53.0", | ||
"jsdom": "^23.2.0", | ||
"vitest": "^1.1.3" | ||
"jsdom": "^24.0.0", | ||
"vitest": "^1.3.1" | ||
}, | ||
"scripts": { | ||
"pretest": "pnpm build", | ||
"test": "vitest", | ||
"test": "vitest --dangerouslyIgnoreUnhandledErrors --environment jsdom --exclude \"test/flaky/**\"", | ||
"test:flaky": "vitest test/flaky", | ||
"deep-clean": "pnpm clean && rm -rf node_modules", | ||
@@ -52,0 +55,0 @@ "clean": "rm -rf dist", |
@@ -1,3 +0,2 @@ | ||
import { DOM_ELEMENT_ID } from "@substrate/connect-extension-protocol" | ||
import { type Chain, type JsonRpcCallback, type ScClient } from "./types.js" | ||
import type { Chain, JsonRpcCallback, ScClient } from "./types.js" | ||
import type { | ||
@@ -19,4 +18,2 @@ RawChain, | ||
let lightClientProviderPromise: Promise<LightClientProvider> | ||
/** | ||
@@ -31,9 +28,5 @@ * Returns a {@link ScClient} that connects to chains by asking the substrate-connect extension | ||
*/ | ||
export const createScClient = (): ScClient => { | ||
if (!lightClientProviderPromise) | ||
lightClientProviderPromise = import( | ||
"@substrate/light-client-extension-helpers/web-page" | ||
).then(({ getLightClientProvider }) => | ||
getLightClientProvider(DOM_ELEMENT_ID), | ||
) | ||
export const createScClient = ( | ||
lightClientProviderPromise: Promise<LightClientProvider>, | ||
): ScClient => { | ||
const internalAddChain = async ( | ||
@@ -40,0 +33,0 @@ isWellKnown: boolean, |
@@ -6,3 +6,7 @@ import { | ||
import { createScClient as extensionScClient } from "./extension.js" | ||
import { DOM_ELEMENT_ID } from "@substrate/connect-extension-protocol" | ||
import type { ScClient } from "./types.js" | ||
import type { | ||
LightClientProvider, | ||
LightClientOnProvider, | ||
} from "@substrate/light-client-extension-helpers/web-page" | ||
@@ -13,25 +17,2 @@ export * from "./types.js" | ||
/** | ||
* `true` if the substrate-connect extension is installed and available. | ||
* | ||
* Always `false` when outside of a browser environment. | ||
* | ||
* We detect this based on the presence of a DOM element with a specific `id`. See | ||
* `connect-extension-protocol`. | ||
* | ||
* Note that the value is determined at initialization and will not change even if the user | ||
* enables, disables, installs, or uninstalls the extension while the script is running. These | ||
* situations are very niche, and handling them properly would add a lot of complexity that isn't | ||
* worth it. | ||
* | ||
* This constant is mostly for informative purposes, for example to display a message in a UI | ||
* encouraging the user to install the extension. | ||
*/ | ||
export const isExtensionPresent = | ||
typeof document === "object" && | ||
typeof document.getElementById === "function" && | ||
!!document.getElementById(DOM_ELEMENT_ID) && | ||
document.getElementById(DOM_ELEMENT_ID)?.getAttribute("channelid") === | ||
DOM_ELEMENT_ID | ||
/** | ||
* Configuration that can be passed to {createScClient}. | ||
@@ -62,7 +43,49 @@ */ | ||
*/ | ||
export function createScClient(config?: Config) { | ||
const forceEmbedded = config?.forceEmbeddedNode | ||
export const createScClient = (config?: Config): ScClient => { | ||
if (config?.forceEmbeddedNode) | ||
return smoldotScClient(config?.embeddedNodeConfig) | ||
if (!forceEmbedded && isExtensionPresent) return extensionScClient() | ||
return smoldotScClient(config?.embeddedNodeConfig) | ||
const lightClientProviderPromise = getExtensionLightClientProviderPromise() | ||
const client = lightClientProviderPromise | ||
? extensionScClient(lightClientProviderPromise) | ||
: smoldotScClient(config?.embeddedNodeConfig) | ||
return { | ||
async addChain(chainSpec, jsonRpcCallback, databaseContent) { | ||
return (await client).addChain( | ||
chainSpec, | ||
jsonRpcCallback, | ||
databaseContent, | ||
) | ||
}, | ||
async addWellKnownChain(id, jsonRpcCallback, databaseContent) { | ||
return (await client).addWellKnownChain( | ||
id, | ||
jsonRpcCallback, | ||
databaseContent, | ||
) | ||
}, | ||
} | ||
} | ||
function getExtensionLightClientProviderPromise(): | ||
| Promise<LightClientProvider> | ||
| undefined { | ||
if (typeof document !== "object" || typeof CustomEvent !== "function") return | ||
let lightClientProviderPromise: Promise<LightClientProvider> | undefined | ||
window.dispatchEvent( | ||
new CustomEvent<LightClientOnProvider>("lightClient:requestProvider", { | ||
detail: { | ||
onProvider(detail) { | ||
if ( | ||
detail.info.rdns === | ||
"io.github.paritytech.SubstrateConnectLightClient" | ||
) { | ||
lightClientProviderPromise = detail.provider | ||
} | ||
}, | ||
}, | ||
}), | ||
) | ||
return lightClientProviderPromise | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
220436
37
2220
1
5
+ Addedsmoldot@2.0.21(transitive)
- Removedsmoldot@2.0.17(transitive)
Updatedsmoldot@2.0.21