-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistry.js
601 lines (548 loc) · 17.1 KB
/
registry.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/* eslint-disable no-shadow */
const { abi } = require('./build/contracts/Registry.json')
const debug = require('debug')('ara-contracts:registry')
const rc = require('ara-runtime-configuration')()
const { parse, resolve } = require('path')
const pify = require('pify')
const path = require('path')
const solc = require('solc')
const fs = require('fs')
let constants = require('./constants')
const {
validate,
getIdentifier,
getDocumentOwner,
web3: {
tx,
call,
account,
contract,
abi: web3Abi,
},
transform: {
toHexString
}
} = require('ara-util')
async function proxyExists(contentDid = '') {
try {
const address = await getProxyAddress(contentDid)
return !/^0x0+$/.test(address)
} catch (err) {
return false
}
}
/**
* Gets the proxy contract address for contentDid
* @param {String} contentDid //unhashed
* @return {string}
* @throws {Error,TypeError}
*/
async function getProxyAddress(contentDid = '') {
if (null == contentDid || 'string' !== typeof contentDid || !contentDid) {
throw TypeError('Expecting non-empty content DID')
}
contentDid = getIdentifier(contentDid)
try {
return call({
abi,
address: constants.REGISTRY_ADDRESS,
functionName: 'getProxyAddress',
arguments: [
toHexString(contentDid, { encoding: 'hex', ethify: true })
]
})
} catch (err) {
throw err
}
}
async function getProxyVersion(contentDid = '') {
if (null == contentDid || 'string' !== typeof contentDid || !contentDid) {
throw TypeError('Expecting non-empty content DID')
}
contentDid = getIdentifier(contentDid)
try {
return call({
abi,
address: constants.REGISTRY_ADDRESS,
functionName: 'getProxyVersion',
arguments: [
toHexString(contentDid, { encoding: 'hex', ethify: true })
]
})
} catch (err) {
throw err
}
}
/**
* Upgrades a proxy to a new version // 33834 gas
* @param {String} opts.contentDid // unhashed
* @param {String} opts.password
* @param {String} opts.afsPassword
* @param {String|number} opts.version
* @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]
* @return {Bool}
* @throws {Error,TypeError}
*/
async function upgradeProxy(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if ('string' !== typeof opts.contentDid || !opts.contentDid) {
throw new TypeError('Expecting non-empty content DID')
} else if (null == opts.password || 'string' !== typeof opts.password || !opts.password) {
throw new TypeError('Expecting non-empty password')
} else if (opts.afsPassword && 'string' !== typeof opts.afsPassword) {
throw TypeError('Expecting non-empty password.')
} else if (('string' !== typeof opts.version && 'number' !== typeof opts.version) || !opts.version) {
throw new TypeError('Expecting non-empty version string or number')
} else if (opts.estimate && 'boolean' !== typeof opts.estimate) {
throw new TypeError('Expecting estimate to be of type 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 { version, afsPassword } = opts
const {
contentDid,
password,
keyringOpts,
gasPrice = 0,
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
} = opts
const estimate = opts.estimate || false
afsPassword = afsPassword || password
if ('number' === typeof version) {
version = version.toString()
}
let did
let ddo
try {
({ did, ddo } = await validate({
did: contentDid, password: afsPassword, label: 'registry', keyringOpts
}))
} catch (err) {
throw err
}
if (constants.ZERO_ADDRESS === await getStandard(version)) {
throw new Error(`AFS Standard version ${version} does not exist. Please try again with an existing version.`)
}
let owner = getDocumentOwner(ddo, true)
owner = `${constants.AID_PREFIX}${owner}`
const acct = await account.load({ did: owner, password })
let upgraded = false
try {
const { tx: transaction, ctx: ctx1 } = await tx.create({
account: acct,
to: constants.REGISTRY_ADDRESS,
gasLimit: 1000000,
gasPrice,
data: {
abi,
functionName: 'upgradeProxy',
values: [
toHexString(contentDid, { encoding: 'hex', ethify: true }),
version
]
}
})
if (estimate) {
const cost = tx.estimateCost(transaction)
ctx1.close()
return cost
}
const { contract: registry, ctx: ctx2 } = await contract.get(abi, constants.REGISTRY_ADDRESS)
upgraded = await new Promise((resolve, reject) => {
tx.sendSignedTransaction(transaction, {
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
})
// listen to ProxyUpgraded event for proxy address
registry.events.ProxyUpgraded({ fromBlock: 'latest' })
.on('data', (log) => {
const { returnValues: { _contentId } } = log
if (_contentId === toHexString(did, { encoding: 'hex', ethify: true })) {
resolve(true)
}
})
.on('error', log => reject(log))
})
ctx2.close()
ctx1.close()
} catch (err) {
throw err
}
return upgraded
}
/**
* Deploys a proxy contract for opts.contentDid // 349574 gas
* @param {String} opts.contentDid // unhashed
* @param {String} opts.password
* @param {String} opts.afsPassword
* @param {String|number} opts.version
* @param {Boolean} [opts.estimate]
* @param {Object} [opts.keyringOpts]
* @param {String} [opts.ownerDid] // only used for estimate
* @param {Number} [opts.gasPrice]
* @param {Function} [opts.onhash]
* @param {Function} [opts.onreceipt]
* @param {Function} [opts.onconfirmation]
* @param {Function} [opts.onerror]
* @param {Function} [opts.onmined]
* @return {string}
* @throws {Error,TypeError}
*/
async function deployProxy(opts) {
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if (!opts.contentDid || 'string' !== typeof opts.contentDid) {
throw new TypeError('Expecting non-empty string for content DID')
} else if (!opts.password || 'string' !== typeof opts.password) {
throw new 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 estimate to be of type boolean')
} else if (opts.ownerDid && 'string' !== typeof opts.ownerDid) {
throw new TypeError('Expecting non-empty string for owner DID')
} else if (opts.gasPrice && ('number' !== typeof opts.gasPrice || opts.gasPrice < 0)) {
throw new TypeError(`Expected 'opts.gasPrice' to be a number. Got ${opts.gasPrice}.`)
}
let { ownerDid, afsPassword } = opts
const {
contentDid,
password,
keyringOpts,
gasPrice = 0,
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
} = opts
const estimate = opts.estimate || ownerDid || false
afsPassword = afsPassword || password
const network = rc.web3.network_id
const defaultVersion = 'mainnet' === network ? constants.MAIN_STANDARD_VERSION : constants.TEST_STANDARD_VERSION
let version = opts.version || defaultVersion
if ('number' === typeof version) {
version = version.toString()
}
// content DID identifier
let did
// owner account
let acct
if (!ownerDid) {
// content DDO
let ddo
try {
({ did, ddo } = await validate({
did: contentDid, password: afsPassword, label: 'registry', keyringOpts
}))
} catch (err) {
throw err
}
try {
const address = await getProxyAddress(did)
if (!/^0x0+$/.test(address)) {
throw new Error(`Proxy for ${did} already exists. No need to deploy proxy.`)
}
} catch (err) {
throw err
}
debug('creating tx to deploy proxy for', did)
let owner = getDocumentOwner(ddo, true)
owner = `${constants.AID_PREFIX}${owner}`
acct = await account.load({ did: owner, password })
} else {
try {
await validate({
did: ownerDid, password, label: 'registry', keyringOpts
})
} catch (err) {
throw err
}
did = getIdentifier(contentDid)
debug('estimating cost to deploy for fake did', did)
ownerDid = `${constants.AID_PREFIX}${getIdentifier(ownerDid)}`
acct = await account.load({ did: ownerDid, password })
}
let proxyAddress = null
try {
const encodedData = web3Abi.encodeParameters(
[ 'address', 'address', 'address', 'bytes32' ],
[ acct.address, constants.ARA_TOKEN_ADDRESS, constants.LIBRARY_ADDRESS, toHexString(did, { encoding: 'hex', ethify: true }) ]
)
const { tx: transaction, ctx: ctx1 } = await tx.create({
account: acct,
to: constants.REGISTRY_ADDRESS,
gasLimit: 3000000,
gasPrice,
data: {
abi,
functionName: 'createAFS',
values: [
toHexString(did, { encoding: 'hex', ethify: true }),
version,
encodedData
]
}
})
if (estimate) {
const cost = tx.estimateCost(transaction)
ctx1.close()
return cost
}
const { contract: registry, ctx: ctx2 } = await contract.get(abi, constants.REGISTRY_ADDRESS)
proxyAddress = await new Promise((resolve, reject) => {
tx.sendSignedTransaction(transaction, {
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
})
// listen to ProxyDeployed event for proxy address
registry.events.ProxyDeployed({ fromBlock: 'latest' })
.on('data', (log) => {
const { returnValues: { _contentId, _address } } = log
if (_contentId === toHexString(did, { encoding: 'hex', ethify: true })) {
resolve(_address)
}
})
.on('error', log => reject(log))
})
ctx2.close()
ctx1.close()
debug('proxy deployed at', proxyAddress)
} catch (err) {
throw err
}
return proxyAddress
}
/**
* Gets the latest AFS contract stndard
* @return {String}
* @throws {Error}
*/
async function getLatestStandard() {
try {
const version = await call({
abi,
address: constants.REGISTRY_ADDRESS,
functionName: 'latestVersion_'
})
return getStandard(version)
} catch (err) {
throw err
}
}
/**
* Gets an AFS contract stndard
* @param {String} version
* @return {String}
* @throws {Error}
*/
async function getStandard(version) {
if (null == version || 'string' !== typeof version || !version) {
if ('number' === typeof version) {
version = version.toString()
} else {
throw TypeError('Expecting non-empty standard version')
}
}
try {
const address = await call({
abi,
address: constants.REGISTRY_ADDRESS,
functionName: 'getImplementation',
arguments: [
version
]
})
return address
} catch (err) {
throw err
}
}
async function _compileStandard(bytespath, paths) {
try {
// compile AFS sources and dependencies
const sources = {
'ERC20.sol': await pify(fs.readFile)(resolve(__dirname, './contracts/ignored_contracts/ERC20.sol'), 'utf8'),
'StandardToken.sol': await pify(fs.readFile)(resolve(__dirname, './contracts/ignored_contracts/StandardToken.sol'), 'utf8'),
'openzeppelin-solidity/contracts/math/SafeMath.sol': await pify(fs.readFile)(resolve(__dirname, './node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol'), 'utf8'),
'Ownable.sol': await pify(fs.readFile)(resolve(__dirname, './contracts/ignored_contracts/Ownable.sol'), 'utf8'),
'bytes/BytesLib.sol': await pify(fs.readFile)(resolve(__dirname, './installed_contracts/bytes/contracts/BytesLib.sol'), 'utf8'),
'SafeMath32.sol': await pify(fs.readFile)(path.resolve(__dirname, './contracts/SafeMath32.sol'), 'utf8')
}
paths.forEach((path) => {
const src = fs.readFileSync(path, 'utf8')
path = parse(path).base
sources[path] = src
})
const compiledFile = solc.compile({ sources }, 1)
const label = Object.keys(compiledFile.contracts)[0]
debug(`writing bytecode for ${label}`)
const compiledContract = compiledFile.contracts[label]
const afsAbi = JSON.parse(compiledContract.interface)
const { bytecode } = compiledContract
const bytes = toHexString(bytecode, { encoding: 'hex', ethify: true })
await pify(fs.writeFile)(bytespath, bytes)
return { bytes, afsAbi }
} catch (err) {
throw err
}
}
/**
* Deploys a new AFS Standard // 2322093 gas (contract deploy) + 58053 gas (add standard)
* @param {Object} opts
* @param {String} opts.requesterDid
* @param {String} opts.password
* @param {String} opts.version
* @param {String} opts.paths
* @param {Object} [opts.keyringOpts]
* @param {String} [opts.compiledPath]
* @param {Number} [opts.gasPrice]
* @param {Function} [opts.onhash]
* @param {Function} [opts.onreceipt]
* @param {Function} [opts.onconfirmation]
* @param {Function} [opts.onerror]
* @param {Function} [opts.onmined]
* @return {String}
* @throws {Error,TypeError}
*/
async function deployNewStandard(opts) {
// ensures compatability with truffle migrate step
delete require.cache[require.resolve('./constants')]
constants = require('./constants')
if (!opts || 'object' !== typeof opts) {
throw new TypeError('Expecting opts object.')
} else if ('string' !== typeof opts.requesterDid || !opts.requesterDid) {
throw TypeError('Expecting non-empty requester DID')
} else if ('string' !== typeof opts.password || !opts.password) {
throw TypeError('Expecting non-empty password')
} else if (!opts.paths || !opts.paths.length) {
throw TypeError('Expecting one or more paths')
} else if (opts.compiledPath && 'string' !== typeof opts.compiledPath) {
throw new TypeError('Expecting path to be a string.')
} 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}.`)
}
if (null == opts.version || 'string' !== typeof opts.version || !opts.version) {
if ('number' === typeof opts.version) {
opts.version = opts.version.toString()
} else {
throw TypeError('Expecting non-empty standard version')
}
}
let { compiledPath } = opts
compiledPath = compiledPath || './build/contracts/AFS.json'
const {
requesterDid,
gasPrice = 0,
keyringOpts,
password,
version,
paths,
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
} = opts
let did
try {
({ did } = await validate({
did: requesterDid, password, label: 'registry', keyringOpts
}))
} catch (err) {
throw err
}
try {
const address = await getStandard(version)
if (!/^0x0+$/.test(address)) {
throw new Error(`AFS Standard version ${version} already exists. Please try again with a different version name.`)
}
} catch (err) {
throw err
}
const prefixedDid = `${constants.AID_PREFIX}${did}`
const acct = await account.load({ did: prefixedDid, password })
const registryOwner = await call({
abi,
address: constants.REGISTRY_ADDRESS,
functionName: 'owner_'
})
if (acct.address != registryOwner) {
throw new Error('Only the owner of the Registry contract may deploy a new standard.')
}
const bytespath = path.resolve(__dirname, `${constants.BYTESDIR}/Standard_${version}`)
let bytes
let afsAbi
try {
bytes = await pify(fs.readFile)(bytespath, 'utf8')
/* eslint-disable import/no-dynamic-require */
const compiledAfs = require(compiledPath)
afsAbi = compiledAfs.abi
} catch (err) {
debug(`Could not read ${bytespath}; compiling instead...`)
const { bytes: b, afsAbi: a } = await _compileStandard(bytespath, paths)
bytes = b
afsAbi = a
}
let address = null
try {
({ contractAddress: address } = await contract.deploy({
account: acct,
abi: afsAbi,
bytecode: bytes
}))
const { tx: transaction, ctx: ctx1 } = await tx.create({
account: acct,
to: constants.REGISTRY_ADDRESS,
gasLimit: 7000000,
gasPrice,
data: {
abi,
functionName: 'addStandardVersion',
values: [
version,
address
]
}
})
// listen to ProxyDeployed event for proxy address
await tx.sendSignedTransaction(transaction, {
onhash,
onreceipt,
onconfirmation,
onerror,
onmined
})
ctx1.close()
} catch (err) {
throw err
}
return address
}
module.exports = {
proxyExists,
deployProxy,
getStandard,
upgradeProxy,
getProxyAddress,
getProxyVersion,
getLatestStandard,
deployNewStandard
}