-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvendor-1.js
46 lines (36 loc) · 1.05 KB
/
vendor-1.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
import * as jose from 'jose'
import fs from 'fs'
const keyPair = JSON.parse(fs.readFileSync('./keyPair.json').toString())
const getPrivateKey = () =>{
return jose.importJWK(keyPair.privateKeyJwk);
}
const getPublicKey = () =>{
return jose.importJWK(keyPair.publicKeyJwk);
}
const sign = async (document, privateKey)=>{
const jws = await new jose.CompactSign(
new TextEncoder().encode(JSON.stringify(document)),
)
.setProtectedHeader({ alg: keyPair.publicKeyJwk.alg })
.sign(privateKey)
return jws
}
const verify = async (document, publicKey)=>{
const { protectedHeader } = await jose.compactVerify(document, publicKey)
return protectedHeader.alg === keyPair.publicKeyJwk.alg
}
// (async ()=>{
// const privateKey = await getPrivateKey();
// const publicKey = await getPublicKey();
// const signature = await sign(document, privateKey)
// const verified = await verify(signature, publicKey )
// console.log(verified, signature)
// })()
const api = {
keyPair,
getPrivateKey,
getPublicKey,
sign,
verify
}
export default api