-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-add Resolver API for WASM builds (#1526)
* add 2_resolve_did example * add custom resolver example * add include custom resolution example in main * fix outdated wording in examples * update examples to use a more natural order of calls, remove unused variables * update resolver logic - resolver can be created with read-only and with write capable clients - `IotaDocument` now serialize to full serialized `IotaDocument` intead of just the `CoreDocument` part when calling `.to_json()`/`.toJSON()` - this affects the documents resolved via `Resolver` as well - `Resolver` class now accepts generic type parameter to specify the type of the resolved documents * fix missing imports in examples * update `actions/cache` version see https://github.blog/changelog/2024-12-05-notice-of-upcoming-releases-and-breaking-changes-for-github-actions/#actions-cache-v1-v2-and-actions-toolkit-cache-package-closing-down * Update bindings/wasm/identity_wasm/examples/src/0_basic/2_resolve_did.ts Co-authored-by: Enrico Marconi <[email protected]> * fix typo found in review --------- Co-authored-by: Enrico Marconi <[email protected]>
- Loading branch information
Showing
21 changed files
with
220 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 52 additions & 24 deletions
76
bindings/wasm/identity_wasm/examples/src/0_basic/2_resolve_did.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,84 @@ | ||
// Copyright 2020-2023 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
// TODO: | ||
// - [ ] clarify if we need/want a resolver example | ||
// - [ ] clarify if we need/want the AliasOutput -> ObjectID example | ||
|
||
|
||
import { | ||
CoreDocument, | ||
DIDJwk, | ||
IdentityClientReadOnly, | ||
IotaDID, | ||
IotaDocument, | ||
IotaIdentityClient, | ||
IToCoreDocument, | ||
JwkMemStore, | ||
KeyIdMemStore, | ||
Resolver, | ||
Storage, | ||
} from "@iota/identity-wasm/node"; | ||
import { AliasOutput, } from "@iota/sdk-wasm/node"; | ||
import { API_ENDPOINT, createDid } from "../util"; | ||
import { createDidDocument, getClientAndCreateAccount, getMemstorage } from "../utils_alpha"; | ||
import { IotaClient } from "@iota/iota-sdk/client"; | ||
import { | ||
createDocumentForNetwork, | ||
getClientAndCreateAccount, | ||
getMemstorage, | ||
IDENTITY_IOTA_PACKAGE_ID, | ||
NETWORK_URL, | ||
} from '../utils_alpha'; | ||
|
||
const DID_JWK: string = | ||
"did:jwk:eyJjcnYiOiJQLTI1NiIsImt0eSI6IkVDIiwieCI6ImFjYklRaXVNczNpOF91c3pFakoydHBUdFJNNEVVM3l6OTFQSDZDZEgyVjAiLCJ5IjoiX0tjeUxqOXZXTXB0bm1LdG00NkdxRHo4d2Y3NEk1TEtncmwyR3pIM25TRSJ9"; | ||
|
||
/** Demonstrates how to resolve an existing DID in an Alias Output. */ | ||
export async function resolveIdentity() { | ||
// create new client to interact with chain and get funded account with keys | ||
// create new clients and create new account | ||
const iotaClient = new IotaClient({ url: NETWORK_URL }); | ||
const network = await iotaClient.getChainIdentifier(); | ||
const storage = getMemstorage(); | ||
const identityClient = await getClientAndCreateAccount(storage); | ||
|
||
// create new DID document and publish it | ||
let [document] = await createDidDocument(identityClient, storage); | ||
let did = document.id(); | ||
const [unpublished] = await createDocumentForNetwork(storage, network); | ||
|
||
// create new identity for this account and publish document for it | ||
const { output: identity } = await identityClient | ||
.createIdentity(unpublished) | ||
.finish() | ||
.execute(identityClient); | ||
const did = IotaDID.fromAliasId(identity.id(), identityClient.network()); | ||
|
||
// Resolve the associated Alias Output and extract the DID document from it. | ||
const resolved: IotaDocument = await identityClient.resolveDid(did); | ||
const resolved = await identityClient.resolveDid(did); | ||
console.log("Resolved DID document:", JSON.stringify(resolved, null, 2)); | ||
|
||
// We can also resolve the Object ID reictly | ||
const aliasOutput: AliasOutput = await identityClient.resolveDidOutput(did); | ||
console.log("The Alias Output holds " + aliasOutput.getAmount() + " tokens"); | ||
// We can resolve the Object ID directly | ||
const resolvedIdentity = await identityClient.getIdentity(identity.id()); | ||
console.dir(resolvedIdentity); | ||
console.log(`Resolved identity has object ID ${resolvedIdentity.toFullFledged()?.id()}`); | ||
|
||
// Or we can resolve it via the `Resolver` api: | ||
|
||
// did:jwk can be resolved as well. | ||
// While at it, define a custom resolver for jwk DIDs as well. | ||
const handlers = new Map<string, (did: string) => Promise<CoreDocument | IToCoreDocument>>(); | ||
handlers.set("jwk", didJwkHandler); | ||
const resolver = new Resolver({ handlers }); | ||
|
||
// Create new `Resolver` instance with the client with write capabilities we already have at hand | ||
const resolver = new Resolver({ client: identityClient, handlers }); | ||
|
||
// and resolve identity DID with it. | ||
const resolverResolved = await resolver.resolve(did.toString()); | ||
console.log(`resolverResolved ${did.toString()} resolves to:\n ${JSON.stringify(resolverResolved, null, 2)}`); | ||
|
||
// We can also resolve via the custom resolver defined before: | ||
const did_jwk_resolved_doc = await resolver.resolve(DID_JWK); | ||
console.log(`DID ${DID_JWK} resolves to:\n ${JSON.stringify(did_jwk_resolved_doc, null, 2)}`); | ||
|
||
// We can also create a resolver with a read-only client | ||
const identityClientReadOnly = await IdentityClientReadOnly.createWithPkgId(iotaClient, IDENTITY_IOTA_PACKAGE_ID); | ||
// In this case we will only be resolving `IotaDocument` instances, as we don't pass a `handler` configuration. | ||
// Therefore we can limit the type of the resolved documents to `IotaDocument` when creating the new resolver as well. | ||
const resolverWithReadOnlyClient = new Resolver<IotaDocument>({ client: identityClientReadOnly }); | ||
|
||
// And resolve as before. | ||
const resolvedViaReadOnly = await resolverWithReadOnlyClient.resolve(did.toString()); | ||
console.log(`resolverWithReadOnlyClient ${did.toString()} resolves to:\n ${JSON.stringify(resolvedViaReadOnly, null, 2)}`); | ||
|
||
// As our `Resolver<IotaDocument>` instance will only return `IotaDocument` instances, we can directly work with them, e.g. | ||
console.log(`${did.toString()}'s metadata is ${resolvedViaReadOnly.metadata()}`); | ||
} | ||
|
||
const didJwkHandler = async (did: string) => { | ||
let did_jwk = DIDJwk.parse(did); | ||
return CoreDocument.expandDIDJwk(did_jwk); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 40 additions & 33 deletions
73
bindings/wasm/identity_wasm/examples/src/1_advanced/4_custom_resolution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2021-2025 IOTA Stiftung | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
import { CoreDocument, IToCoreDocument, Resolver as ResolverInner } from "~identity_wasm"; | ||
|
||
// `Resolver` type below acts the same as the "normal" resolver from `~identity_wasm` | ||
// with the difference being that the `Resolver` here allows to pass generic type params to | ||
// the constructor to specify the types expected to be returned by the `resolve` function. | ||
|
||
/** | ||
* Convenience type for resolving DID documents from different DID methods. | ||
* | ||
* DID documents resolved with `resolve` will have the type specified as generic type parameter T. | ||
* With the default being `CoreDocument | IToCoreDocument`. | ||
* | ||
* Also provides methods for resolving DID Documents associated with | ||
* verifiable {@link Credential}s and {@link Presentation}s. | ||
* | ||
* # Configuration | ||
* | ||
* The resolver will only be able to resolve DID documents for methods it has been configured for in the constructor. | ||
*/ | ||
export class Resolver<T extends (CoreDocument | IToCoreDocument)> extends ResolverInner { | ||
/** | ||
* Fetches the DID Document of the given DID. | ||
* | ||
* ### Errors | ||
* | ||
* Errors if the resolver has not been configured to handle the method | ||
* corresponding to the given DID or the resolution process itself fails. | ||
* @param {string} did | ||
* @returns {Promise<T>} | ||
*/ | ||
async resolve(did: string): Promise<T> { | ||
return super.resolve(did) as unknown as T; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.