-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdid.js
41 lines (33 loc) · 1.15 KB
/
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
// import { randomBytes } from 'crypto'
import { DID } from 'dids'
import { Ed25519Provider } from 'key-did-provider-ed25519'
import * as KeyResolver from 'key-did-resolver'
const signature = {
verify: async (jws, key, hash) => {
const expected = { message: `I sign this revision: [${hash}]` }
try {
const resolver = KeyResolver.getResolver()
const result = await (new DID({ resolver })).verifyJWS(jws)
if (expected.message !== result.payload.message) return false
if (key !== result.kid.split("#")[0]) return false
} catch (e) {
console.log(e)
return false
}
return true
},
sign: async (verificationHash, privateKey) => {
const payload = { message: `I sign this revision: [${verificationHash}]` }
// const seed = randomBytes(32)
// console.log(new Buffer.from(seed).toString("hex"))
const provider = new Ed25519Provider(privateKey)
const resolver = KeyResolver.getResolver()
const did = new DID({ provider, resolver })
await did.authenticate()
const jws = await did.createJWS(payload, { did: did.id })
return { jws, key: did.id }
}
}
export {
signature
}