-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploy.js
96 lines (87 loc) · 2.31 KB
/
deploy.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const { validate } = require('ara-util')
const {
proxyExists,
deployProxy
} = require('ara-contracts/registry')
/**
* Deploys an AFS proxy to the network
* @param {Object} opts
* @param {String} opts.did
* @param {String} opts.password
* @param {String} opts.afsPassword
* @param {Boolean} opts.estimate
* @param {String} [opts.version]
* @param {Number} [opts.gasPrice]
* @param {Function} [opts.onhash]
* @param {Function} [opts.onreceipt]
* @param {Function} [opts.onconfirmation]
* @param {Function} [opts.onerror]
* @param {Function} [opts.onmined]
* @throws {TypeError|Error}
* @return {String|Object}
*/
async function deploy(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if ('string' !== typeof opts.did || !opts.did) {
throw new TypeError('Expecting non-empty string.')
} else if ('string' !== typeof opts.password || !opts.password) {
throw TypeError('Expecting non-empty password.')
} else if (opts.afsPassword && 'string' !== typeof opts.afsPassword) {
throw TypeError('Expecting non-empty password.')
} else if (opts.estimate && 'boolean' !== typeof opts.estimate) {
throw new TypeError('Expecting boolean.')
} else if (opts.gasPrice && ('number' !== typeof opts.gasPrice || opts.gasPrice < 0)) {
throw new TypeError(`Expected 'opts.gasPrice' to be a positive number. Got ${opts.gasPrice}.`)
}
let { did, estimate, afsPassword } = opts
const {
password,
keyringOpts,
version = '',
gasPrice = 0,
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
} = opts
afsPassword = afsPassword || password
estimate = estimate || false
try {
({ did } = await validate({
did, password: afsPassword, label: 'commit', keyringOpts
}))
} catch (err) {
throw err
}
if (await proxyExists(did)) {
throw new Error('Proxy already exists')
}
let result
try {
result = await deployProxy({
contentDid: did,
keyringOpts,
afsPassword,
password,
estimate,
version,
gasPrice,
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
})
if (estimate) {
return result.toString()
}
} catch (err) {
throw err
}
return result
}
module.exports = {
deploy
}