-
Notifications
You must be signed in to change notification settings - Fork 3
/
attest.ts
65 lines (53 loc) · 2.09 KB
/
attest.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
import path from "path";
import dotenv from "dotenv";
import { keys } from "@zcloak/did";
import { Keyring } from "@zcloak/keyring";
import { initCrypto } from "@zcloak/crypto";
import { VerifiableCredentialBuilder } from "@zcloak/vc";
import { decryptMessage, encryptMessage } from "@zcloak/message";
import type { RawCredential } from "@zcloak/vc/types";
import type { VerifiableCredential } from "@zcloak/vc/types";
import { getMessage } from "../utils/messageHelper";
import { getCtypeFromHash } from "../utils/ctypeHelper";
import { sendMessage2Server } from "../utils/messageHelper";
dotenv.config({ path: path.resolve(__dirname, "../../.env") });
(async () => {
// initCrypto for wasm
await initCrypto();
console.log("initCrypto for wasm...");
// Get attester DID from mnemonic
// We need attester's private key to sign signature when approving send credential
const mnemonic = process.env.ATTESTER_MNEMONIC as string;
const keyring = new Keyring();
const attester = keys.fromMnemonic(keyring, mnemonic, "ecdsa");
// Step 0: get latest message
const serverMsg = await getMessage(attester, "Request_Attestation");
// Step 1: decrypt claim message
// serverMsg array is just for demo use, the details should be based on the actual situation
const decrypted = await decryptMessage(serverMsg[0], attester);
// Step 2: get rawCredential from decrypt return value
const rawCredential: RawCredential = decrypted.data;
// Step 3: issuer sign a signature to build vcBuilder
const ctype = await getCtypeFromHash(rawCredential.ctype);
const vcBuilder = VerifiableCredentialBuilder.fromRawCredential(
rawCredential,
ctype
)
.setExpirationDate(null)
.setIssuanceDate(Date.now());
// Step 4: build vc from vcBuilder
const vc: VerifiableCredential<false> = await vcBuilder.build(
attester,
false
);
// Step 5: encrypt vc to message
const message = await encryptMessage(
"Response_Approve_Attestation",
vc,
attester,
decrypted.sender,
decrypted.id
);
// Step 6: send encrypted message to server
await sendMessage2Server(message);
})();