-
Notifications
You must be signed in to change notification settings - Fork 85
/
4_custom_resolution.ts
86 lines (73 loc) · 2.88 KB
/
4_custom_resolution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import {
CoreDocument,
IotaDocument,
IotaIdentityClient,
JwkMemStore,
KeyIdMemStore,
Resolver,
Storage,
} from "@iota/identity-wasm/node";
import { Client, MnemonicSecretManager, Utils } from "@iota/sdk-wasm/node";
import { API_ENDPOINT, createDid } from "../util";
// Use this external package to avoid implementing the entire did:key method in this example.
import * as ed25519 from "@transmute/did-key-ed25519";
/** Demonstrates how to set up a resolver using custom handlers.
*/
export async function customResolution() {
// Set up a handler for resolving Ed25519 did:key
const keyHandler = async function(didKey: string): Promise<CoreDocument> {
let document = await ed25519.resolve(
didKey,
{ accept: "application/did+ld+json" },
);
return CoreDocument.fromJSON(document.didDocument);
};
// Create a new Client to interact with the IOTA ledger.
const client = new Client({
primaryNode: API_ENDPOINT,
localPow: true,
});
const didClient = new IotaIdentityClient(client);
// Construct a Resolver capable of resolving the did:key and iota methods.
let handlerMap: Map<string, (did: string) => Promise<IotaDocument | CoreDocument>> = new Map();
handlerMap.set("key", keyHandler);
const resolver = new Resolver(
{
client: didClient,
handlers: handlerMap,
},
);
// A valid Ed25519 did:key value taken from https://w3c-ccg.github.io/did-method-key/#example-1-a-simple-ed25519-did-key-value.
const didKey = "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK";
// Generate a random mnemonic for our wallet.
const secretManager: MnemonicSecretManager = {
mnemonic: Utils.generateMnemonic(),
};
// Creates a new wallet and identity for us to resolve (see "0_create_did" example).
const storage: Storage = new Storage(new JwkMemStore(), new KeyIdMemStore());
let { document } = await createDid(
client,
secretManager,
storage,
);
const did = document.id();
// Resolve didKey into a DID document.
const didKeyDoc = await resolver.resolve(didKey);
// Resolve the DID we created on the IOTA ledger.
const didIotaDoc = await resolver.resolve(did.toString());
// Check that the types of the resolved documents match our expectations:
if (didKeyDoc instanceof CoreDocument) {
console.log("Resolved DID Key document:", JSON.stringify(didKeyDoc, null, 2));
} else {
throw new Error(
"the resolved document type should match the output type of keyHandler",
);
}
if (didIotaDoc instanceof IotaDocument) {
console.log("Resolved IOTA DID document:", JSON.stringify(didIotaDoc, null, 2));
} else {
throw new Error(
"the resolved document type should match IotaDocument",
);
}
}