|
| 1 | +--- |
| 2 | +id: kilt-extension-api |
| 3 | +title: KILT Extension API |
| 4 | +--- |
| 5 | + |
| 6 | +KILT Extension API is a JavaScript/TypeScript library that provides helper functions for interacting with KILT enabled extensions. |
| 7 | +It facilitates seamless communication between your application and KILT extensions. |
| 8 | + |
| 9 | +## Getting Started |
| 10 | + |
| 11 | +Before you can communicate with KILT extensions, you must call the `initializeKiltExtensionAPI()` function to signal the API versions supported by your application. |
| 12 | +This is crucial for the extension to inject the appropriate scripts into the website. |
| 13 | + |
| 14 | +```ts |
| 15 | +import { initializeKiltExtensionAPI } from 'kilt-extension-api' |
| 16 | + |
| 17 | +initializeKiltExtensionAPI() |
| 18 | +``` |
| 19 | + |
| 20 | +## Get Extensions |
| 21 | + |
| 22 | +The `getExtensions()` function returns a list of extensions currently injected into the website. |
| 23 | + |
| 24 | +```ts |
| 25 | +import { getExtensions } from 'kilt-extension-api' |
| 26 | + |
| 27 | +const extensions = getExtensions() |
| 28 | +``` |
| 29 | + |
| 30 | +## Watch Extensions |
| 31 | + |
| 32 | +Extensions may take longer to load than the website. |
| 33 | +Therefore, the first call to `getExtensions()` might not return all available extensions. |
| 34 | +To receive updates on additional extensions as they load, you can use `watchExtensions`. |
| 35 | + |
| 36 | +Here's an example of how you can use this function in a React application: |
| 37 | + |
| 38 | +```ts |
| 39 | +import { watchExtensions, Types } from 'kilt-extension-api' |
| 40 | + |
| 41 | +export default function Home(): JSX.Element { |
| 42 | + const [extensions, setExtensions] = useState< |
| 43 | + Types.InjectedWindowProvider<Types.PubSubSessionV1 | Types.PubSubSessionV2>[] |
| 44 | + >([]) |
| 45 | + |
| 46 | + useEffect(() => { |
| 47 | + watchExtensions((extensions) => { |
| 48 | + setExtensions(extensions) |
| 49 | + }) |
| 50 | + }, []) |
| 51 | + |
| 52 | + return ( |
| 53 | + <> |
| 54 | + <h2>Extensions</h2> |
| 55 | + <ul> |
| 56 | + {extensions.map((ext, i) => ( |
| 57 | + <li key={i}>{ext.name}</li> |
| 58 | + ))} |
| 59 | + </ul> |
| 60 | + </> |
| 61 | + ) |
| 62 | +} |
| 63 | +``` |
| 64 | + |
| 65 | +## Well-Known DID Configuration |
| 66 | + |
| 67 | +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). |
| 68 | + |
| 69 | +### Using the CLI Tool |
| 70 | + |
| 71 | +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. |
| 72 | + |
| 73 | +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`). |
| 74 | + |
| 75 | +You can run the CLI tool using Yarn as follows: |
| 76 | + |
| 77 | +```bash |
| 78 | +yarn createDidConfig --did <your DID> --origin <your domain> --assertionMethod <id of your DID's assertionMethod key> --seed <seed or mnemonic of the assertionMethod key> |
| 79 | +``` |
| 80 | +
|
| 81 | +For additional commands and configuration options, refer to the CLI tool's helper: |
| 82 | + |
| 83 | +```bash |
| 84 | +yarn createDidConfig --help |
| 85 | +``` |
| 86 | + |
| 87 | +### Integration into Your App |
| 88 | + |
| 89 | +Similar functionality to the CLI tool is available for import into your Node.js scripts using the subpath `kilt-extension-api/wellKnownDidConfiguration`: |
| 90 | + |
| 91 | +```ts |
| 92 | +import { createCredential, didConfigResourceFromCredential } from './wellKnownDidConfiguration/index.js' |
| 93 | +
|
| 94 | +const credential = await createCredential( |
| 95 | + ({ data }) => { |
| 96 | + //...DID signing logic |
| 97 | + }, |
| 98 | + 'https://example.com', |
| 99 | + 'did:kilt:4pnfkRn5UurBJTW92d9TaVLR2CqJdY4z5HPjrEbpGyBykare' |
| 100 | +) |
| 101 | +
|
| 102 | +const didConfigResource = didConfigResourceFromCredential(credential) |
| 103 | +``` |
| 104 | +
|
| 105 | +This module also assists in verifying a DID configuration resource within an extension context: |
| 106 | +
|
| 107 | +```ts |
| 108 | +import { verifyDidConfigResource } from './wellKnownDidConfiguration/index.js' |
| 109 | +
|
| 110 | +// load didConfigResource from https://example.com/.well-known/did-configuration.json |
| 111 | +
|
| 112 | +const didLinkedToOrigin = await verifyDidConfigResource(didConfigResource, 'https://example.com') |
| 113 | +
|
| 114 | +// or, if a specific DID is expected: |
| 115 | +
|
| 116 | +await verifyDidConfigResource( |
| 117 | + didConfigResource, |
| 118 | + 'https://example.com', |
| 119 | + 'did:kilt:4pnfkRn5UurBJTW92d9TaVLR2CqJdY4z5HPjrEbpGyBykare' |
| 120 | +) |
| 121 | +``` |
0 commit comments