-
Notifications
You must be signed in to change notification settings - Fork 3
/
generate-did.js
56 lines (46 loc) · 1.64 KB
/
generate-did.js
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
import didProto from "peaq-did-proto-js";
import { u8aToHex } from "@polkadot/util";
import { v4 as uuidv4 } from "uuid";
import { getPeaqKeyPair } from "./utils.js";
const {
Document,
VerificationMethod,
VerificationType,
Signature,
Service,
Metadata,
ServiceType,
Status,
} = didProto;
const getDidString = (address) => `did:peaq:${address}`;
const createVerificationMethod = (address) => {
const id = uuidv4();
const verificationMethod = new VerificationMethod();
verificationMethod.setId(id);
verificationMethod.setType(VerificationType.ED25519VERIFICATIONKEY2020);
verificationMethod.setController(getDidString(address));
verificationMethod.setPublickeymultibase(`z${address}`);
return { verificationMethod, verificationId: id };
};
const createSignature = (address) => {
const peaqKeyPair = getPeaqKeyPair();
const signed = peaqKeyPair.sign(address);
const signature = new Signature();
signature.setType(VerificationType.Ed25519VerificationKey2020);
signature.setIssuer(peaqKeyPair.address);
signature.setHash(u8aToHex(signed));
return signature;
};
export const generateDidDocument = (controllerAddress, userAddress) => {
const document = new Document();
document.setId(getDidString(userAddress));
document.setController(getDidString(controllerAddress));
const { verificationId, verificationMethod } =
createVerificationMethod(userAddress);
document.addVerificationmethods(verificationMethod);
const signature = createSignature(userAddress);
document.setSignature(signature);
document.addAuthentications(verificationId);
const bytes = document.serializeBinary();
return u8aToHex(bytes);
};