From c2668d1224110bff90339dfb8224b8a1361ff739 Mon Sep 17 00:00:00 2001 From: Chris Chinchilla Date: Thu, 7 Dec 2023 11:10:40 +0100 Subject: [PATCH] Draft Signed-off-by: Chris Chinchilla --- .../01_sdk/04_integrate/04_extension_api.md | 215 +++++++++++++----- 1 file changed, 160 insertions(+), 55 deletions(-) diff --git a/docs/develop/01_sdk/04_integrate/04_extension_api.md b/docs/develop/01_sdk/04_integrate/04_extension_api.md index 438f674ee..756e84361 100644 --- a/docs/develop/01_sdk/04_integrate/04_extension_api.md +++ b/docs/develop/01_sdk/04_integrate/04_extension_api.md @@ -3,93 +3,198 @@ id: kilt-extension-api title: KILT Extension API --- -KILT Extension API is a JavaScript/TypeScript library that provides helper functions for interacting with KILT enabled extensions. -It facilitates seamless communication between your application and KILT extensions. +import TsJsBlock from '@site/src/components/TsJsBlock'; +import Tabs from '@theme/Tabs'; +import TabItem from '@theme/TabItem'; -## Getting Started +The KILT Extension API is a JavaScript and TypeScript library that provides helper functions for interacting with KILT extensions. +It facilitates communication between your application and KILT extensions. -Before you can communicate with KILT extensions, you must call the `initializeKiltExtensionAPI()` function to signal the API versions supported by your application. -This is crucial for the extension to inject the appropriate scripts into the website. + -```ts -import { initializeKiltExtensionAPI } from 'kilt-extension-api' +## Installation -initializeKiltExtensionAPI() -``` +Add the package: + + + + + ```bash + yarn add @kiltprotocol/kilt-extension-api + ``` + + + + + ```bash + npm install --save @kiltprotocol/kilt-extension-api + ``` + + + + +## Initialize Extension API + +Before your application can communicate with KILT extensions, call the `initializeKiltExtensionAPI()` method to signal the API versions supported by your application so the extension can inject the appropriate scripts. + + + + + ```ts + import { initializeKiltExtensionAPI } from 'kilt-extension-api' + + initializeKiltExtensionAPI() + ``` + + + + + ```js + import { initializeKiltExtensionAPI } from 'kilt-extension-api' + + initializeKiltExtensionAPI() + ``` + + + ## Get Extensions -The `getExtensions()` function returns a list of extensions currently injected into the website. +The `getExtensions()` method returns a list of extensions injected into the browser. -```ts -import { getExtensions } from 'kilt-extension-api' + + -const extensions = getExtensions() -``` + ```ts + import { getExtensions } from 'kilt-extension-api' -## Watch Extensions + const extensions = getExtensions() + console.log(extensions) + ``` -Extensions may take longer to load than the website. -Therefore, the first call to `getExtensions()` might not return all available extensions. -To receive updates on additional extensions as they load, you can use `watchExtensions`. + + -Here's an example of how you can use this function in a React application: + ```js + import { getExtensions } from 'kilt-extension-api' -```ts -import { watchExtensions, Types } from 'kilt-extension-api' - -export default function Home(): JSX.Element { - const [extensions, setExtensions] = useState< - Types.InjectedWindowProvider[] - >([]) - - useEffect(() => { - watchExtensions((extensions) => { - setExtensions(extensions) - }) - }, []) - - return ( - <> -

Extensions

-
    - {extensions.map((ext, i) => ( -
  • {ext.name}
  • - ))} -
- - ) -} -``` + const extensions = getExtensions() + console.log(extensions) + ``` + +
+
+ +## Watch Extensions + +Extensions can take longer to load than the host application, so the first call to `getExtensions()` might not return all available extensions. +To receive updates on extensions as they load, use the `watchExtensions` method. + +The following is an example of how you can use this method in a React application: + + + + + ```ts + import { watchExtensions, Types } from 'kilt-extension-api' + + export default function Home(): JSX.Element { + const [extensions, setExtensions] = useState< + Types.InjectedWindowProvider[] + >([]) + + useEffect(() => { + watchExtensions((extensions) => { + setExtensions(extensions) + }) + }, []) + + return ( + <> +

Extensions

+
    + {extensions.map((ext, i) => ( +
  • {ext.name}
  • + ))} +
+ + ) + } + ``` + +
+ + + ```js + import { watchExtensions } from "kilt-extension-api" + + function App() { + const [extensions, setExtensions] = useState([]) + + useEffect(() => { + watchExtensions(extensions => { + setExtensions(extensions) + }) + }, []) + + return ( + <> +

Extensions

+
    + {extensions.map((ext, i) => ( +
  • {ext.name}
  • + ))} +
+ + ) + } + ``` + +
+
+ + ## Well-Known DID Configuration -This library also aids in setting up the [Well-Known DID Configuration](https://identity.foundation/.well-known/resources/did-configuration/) as required by the [KILT Credential API specification](/~https://github.com/KILTprotocol/spec-ext-credential-api). +This library helps set up the [Well-Known DID Configuration](https://identity.foundation/.well-known/resources/did-configuration/) as required by the [KILT Credential API specification](/~https://github.com/KILTprotocol/spec-ext-credential-api). ### Using the CLI Tool -A CLI tool is included in this library to create a [DID Configuration Resource](https://identity.foundation/.well-known/resources/did-configuration/#did-configuration-resource) as specified in the above documentation. This resource is necessary to establish a secure, end-to-end encrypted communication channel between a conforming browser extension and the application backend. +This library includes a CLI tool to create a [DID Configuration Resource](https://identity.foundation/.well-known/resources/did-configuration/#did-configuration-resource). This resource is necessary to establish a secure, end-to-end encrypted communication channel between a conforming browser extension and the application backend. + +:::warning KILT Account -To start using this tool, you can add this package to your application using `yarn add --dev kilt-extension-api` or install it globally if needed (`yarn global add kilt-extension-api`). +The `createDidConfig` CLI tool **only** works if you installed the package with Yarn. -You can run the CLI tool using Yarn as follows: +::: + +Run the CLI tool using Yarn as follows: ```bash yarn createDidConfig --did --origin --assertionMethod --seed ``` -For additional commands and configuration options, refer to the CLI tool's helper: +:::info + +- `did`: DID of the issuer (and subject) of the Domain Linkage Credential. If omitted, the tool attempts to infer this from the `assertionMethod`. +- `seed`: Mnemonic or seed for the `assertionMethod` key used for issuing a new credential. +- `origin`: Domain of the application that will be using the DID Configuration Resource. +- `assertionMethod`: ID of the `assertionMethod` key used for issuing a new credential. +::: + +Use the tool's `--help` flag to see all available options: ```bash yarn createDidConfig --help ``` -### Integration into Your App +### Integration into an App -Similar functionality to the CLI tool is available for import into your Node.js scripts using the subpath `kilt-extension-api/wellKnownDidConfiguration`: +Similar functionality to the CLI tool is available for use in application code using the `@kiltprotocol/extension-api/wellKnownDidConfiguration` subpath: ```ts -import { createCredential, didConfigResourceFromCredential } from './wellKnownDidConfiguration/index.js' +import { createCredential, didConfigResourceFromCredential } from '@kiltprotocol/extension-api/wellKnownDidConfiguration' const credential = await createCredential( ({ data }) => { @@ -105,9 +210,9 @@ const didConfigResource = didConfigResourceFromCredential(credential) This module also assists in verifying a DID configuration resource within an extension context: ```ts -import { verifyDidConfigResource } from './wellKnownDidConfiguration/index.js' +import { verifyDidConfigResource } from '@kiltprotocol/extension-api/wellKnownDidConfiguration' -// load didConfigResource from https://example.com/.well-known/did-configuration.json +load didConfigResource from https://example.com/.well-known/did-configuration.json const didLinkedToOrigin = await verifyDidConfigResource(didConfigResource, 'https://example.com')