-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrevoke.js
71 lines (58 loc) · 1.91 KB
/
revoke.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const crypto = require('ara-crypto')
const bip39 = require('bip39')
const { normalize } = require('./did')
const { resolve } = require('./resolve')
const { create } = require('./create')
/**
* Revoke an identity using a bip39 mnemonic
* @public
* @param {object} opts
* @param {object} opts.context
* @param {string} opts.mnemonic
* @param {string} opts.password
* @throws TypeError
* @return {object}
*/
async function revoke(opts) {
if (null == opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts to be an object.')
}
if (opts.context && 'object' !== typeof opts.context) {
throw new TypeError('Expecting context object.')
}
if (opts.context && 'object' !== typeof opts.context.web3) {
throw new TypeError('Expecting web3 to be in context.')
}
if (null == opts.mnemonic) {
throw new TypeError('Expecting mnemonic for revoking.')
} else if (opts.mnemonic && 'string' !== typeof opts.mnemonic) {
throw new TypeError('Expecting mnemonic to be a string.')
}
if (!bip39.validateMnemonic(opts.mnemonic)) {
throw new TypeError('Expecting a valid bip39 mnemonic for revoking.')
}
if (null == opts.password) {
throw new TypeError('Expecting password.')
} else if (opts.password && 'string' !== typeof opts.password) {
throw new TypeError('Expecting password to be a string.')
}
const seed = crypto.blake2b(await bip39.mnemonicToSeed(opts.mnemonic))
const { publicKey } = crypto.keyPair(seed)
let ddo
try {
ddo = await resolve(normalize(publicKey.toString('hex')))
} catch (err) {
throw new Error('Could not resolve DID for the provided mnemonic')
}
if (ddo.revoked && 'string' === typeof ddo.revoked) {
throw new Error('DID for the provided mnemonic has already been revoked')
}
opts.created = ddo.created
opts.revoked = true
opts.ddo = ddo
const identity = await create(opts)
return identity
}
module.exports = {
revoke
}