-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecover.js
49 lines (40 loc) · 1.24 KB
/
recover.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
const bip39 = require('bip39')
const { create } = require('./create')
/**
* Recover an Identity using a bip39 mnemonic
* @public
* @param {object} opts
* @param {object} opts.context
* @param {string} opts.password
* @param {string} opts.mnemonic
* @param {array} opts.ddo (optional)
* @throws TypeError
* @return {object}
*/
async function recover(opts) {
if (null == opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts to be an object.')
}
if (null == opts.password) {
throw new TypeError('Expecting password for recovery.')
}
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 recovery.')
} 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 recovery.')
}
const identity = await create(opts)
return identity
}
module.exports = {
recover
}