-
Notifications
You must be signed in to change notification settings - Fork 0
/
ownership.js
242 lines (220 loc) · 7.05 KB
/
ownership.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
const { writeIdentity } = require('ara-identity/util')
const passGenerator = require('generate-password')
const { METADATA_SUFFIX } = require('./constants')
const { createIdentity } = require('./create')
const { archive } = require('ara-identity')
const {
getDocumentOwner,
validate
} = require('ara-util')
const {
approveOwnershipTransfer,
revokeOwnershipRequest,
requestOwnership,
hasRequested,
getOwner
} = require('ara-contracts/ownership')
const PASSWORD_LENGTH = 12
/**
* Estimates the gas cost for requesting ownership.
* @param {Object}
* @return {String}
*/
async function estimateRequestGasCost(opts) {
return request(Object.assign(opts, { estimate: true }))
}
/**
* Estimates the gas cost for revoking an ownership request.
* @param {Object} opts
* @return {String}
*/
async function estimateRevokeGasCost(opts) {
return revokeRequest(Object.assign(opts, { estimate: true }))
}
/**
* Estimates the gas cost for approving an ownership transfer.
* @param {Object} opts
* @return {String}
*/
async function estimateApproveGasCost(opts) {
return approveTransfer(Object.assign(opts, { estimate: true }))
}
/**
* Requests ownership of an AFS.
* @param {Object} opts
* @param {String} opts.requesterDid
* @param {String} opts.contentDid
* @param {String} opts.password
* @param {String} [opts.estimate]
* @param {Object} [opts.keyringOpts]
* @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 {Error|TypeError}
* @return {Object}
*/
async function request(opts) {
return requestOwnership(opts)
}
/**
* Revokes a previous ownership request of an AFS.
* @param {Object} opts
* @param {String} opts.requesterDid
* @param {String} opts.contentDid
* @param {String} opts.password
* @param {String} [opts.estimate]
* @param {Object} [opts.keyringOpts]
* @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 {Error|TypeError}
* @return {Object}
*/
async function revokeRequest(opts) {
return revokeOwnershipRequest(opts)
}
/**
* Approves and transfers an ownership request.
* @param {Object} opts
* @param {String} opts.newOwnerDid
* @param {String} opts.mnemonic
* @param {String} opts.did
* @param {String} opts.password
* @param {String} opts.afsPassword
* @param {Boolean} [opts.estimate]
* @param {Object} [opts.keyringOpts]
* @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 {Error|TypeError}
* @return {String|Object}
*/
async function approveTransfer(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object')
} else if (!opts.mnemonic || 'string' !== typeof opts.mnemonic) {
throw new TypeError('Mnemonic must be non-empty string')
} else if (!opts.newOwnerDid || 'string' !== typeof opts.newOwnerDid) {
throw new TypeError('New owner DID must be non-empty string')
} else if (!opts.contentDid || 'string' !== typeof opts.contentDid) {
throw new TypeError('AFS DID must be non-empty string')
} else if (!opts.password || 'string' !== typeof opts.password) {
throw new TypeError('Password must be non-empty string')
} 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('Estimate must be of type boolean')
}
const {
keyringOpts = {},
newOwnerDid,
contentDid,
mnemonic,
password,
estimate
} = opts
let { afsPassword } = opts
afsPassword = afsPassword || password
let result
let randomPassword
try {
const identity = await validate({ did: contentDid, password: afsPassword, keyringOpts })
const { ddo } = identity
const metadataPublicKey = _getMetadataPublicKey(ddo)
randomPassword = passGenerator.generate({ length: PASSWORD_LENGTH, numbers: true })
const newIdentity = await createIdentity({
mnemonic, owner: newOwnerDid, password: randomPassword, metadataPublicKey
})
const { publicKey } = newIdentity
if (identity.did !== publicKey.toString('hex')) {
throw new Error('Mnemonic is incorrect, please confirm it is the AFS owner\'s mnemonic.')
}
result = await approveOwnershipTransfer(opts)
if (!estimate && result.status) {
await _archiveNewIdentity(newIdentity)
} else {
return result
}
} catch (err) {
throw err
}
return {
receipt: result,
password: randomPassword
}
}
/**
* Claims a transferred ownership, updating
* the password used to encrypt the keystore.
* @param {Object} opts
* @param {String} opts.currentPassword
* @param {String} opts.newPassword
* @param {String} opts.contentDid
* @param {String} opts.Mnemonic
* @throws {Error|TypeError}
*/
async function claim(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object')
} else if (!opts.currentPassword || 'string' !== typeof opts.currentPassword) {
throw new TypeError('Expecting non-empty string for current password')
} else if (!opts.newPassword || 'string' !== typeof opts.newPassword) {
throw new TypeError('Expecting non-empty string for new password')
} else if (!opts.contentDid || 'string' !== typeof opts.contentDid) {
throw new TypeError('Expecting DID of content to claim')
} else if (!opts.mnemonic || 'string' !== typeof opts.mnemonic) {
throw new TypeError('Expecting AFS mnemonic as string')
}
const {
keyringOpts = {},
currentPassword,
newPassword,
contentDid,
mnemonic
} = opts
try {
const { ddo } = await validate({ did: contentDid, password: currentPassword, keyringOpts })
const owner = getDocumentOwner(ddo, true)
const metadataPublicKey = _getMetadataPublicKey(ddo)
const claimedIdentity = await createIdentity({
mnemonic, owner, password: newPassword, metadataPublicKey
})
await _archiveNewIdentity(claimedIdentity)
} catch (err) {
throw err
}
}
async function _archiveNewIdentity(identity) {
await archive(identity, {})
await writeIdentity(identity)
}
// expose in ara-util if this functionality is needed elsewhere
// cckelly
function _getMetadataPublicKey(ddo) {
const result = ddo.publicKey.find(({ id }) => id.includes(METADATA_SUFFIX))
if (!result) {
throw new Error('Could not parse DDO, please make sure the provided DIDs are correct.')
}
const { publicKeyHex } = result
return publicKeyHex
}
module.exports = {
estimateRequestGasCost,
estimateApproveGasCost,
estimateRevokeGasCost,
approveTransfer,
revokeRequest,
hasRequested,
getOwner,
request,
claim
}