-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
49 lines (41 loc) · 1.59 KB
/
index.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
import * as jose from 'jose'
import { Custodian } from 'ssikit-sdk';
import express, { Express, Request, Response } from 'express';
// import "./apis/theGraph";
interface signatureRequest {
keyId: string;
message: string;
}
interface verificationRequest {
verifierDid: string;
message: jose.GeneralJWSInput;
}
const app: Express = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.post("/createSignature", async (req: Request, res: Response) => {
const request: signatureRequest = req.body.data as unknown as signatureRequest;
const pvtJWK: jose.JWK = await Custodian.exportKey(request.keyId, "JWK", true);
const privateKey = await jose.importJWK(pvtJWK);
const jws = await new jose.GeneralSign(
new TextEncoder().encode(request.message),
)
.addSignature(privateKey)
.setProtectedHeader({ alg: pvtJWK.alg })
.sign()
res.send(jws)
})
app.post("/verifySignature", async (req: Request, res: Response) => {
const request: verificationRequest = req.body.data as unknown as verificationRequest;
const verifierDid = await Custodian.getDID(request.verifierDid);
console.log(verifierDid)
const pubJWK = verifierDid.verificationMethod[0].publicKeyJwk;
const publicKey = await jose.importJWK(pubJWK);
const { payload } = await jose.generalVerify(request.message, publicKey)
let result = new TextDecoder().decode(payload)
res.send(result)
})
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`⚡️[server]: Server is running at https://localhost:${PORT}`);
});