-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathazimuth.js
642 lines (587 loc) · 19.7 KB
/
azimuth.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
/**
* Azimuth API
* @module azimuth
*/
const internal = require('./internal/azimuth');
const utils = require('./utils');
// Generic API for azimuth
//
// Typically:
//
// * if 'point' is an object, will compute locally
// * if 'point' is a uint, will hit the network
//
// Note that the type check for 'object' is weak, but intentionally so: the
// type branch is intended to provide a generic API, and to work seamlessly
// with Promises, rather than to prevent invalid inputs *per se*.
/**
* The Azimuth contract's address on the Ethereum mainnet.
*
* Unlike that of the Ecliptic, Polls, etc. contracts, the Azimuth contract's
* address never changes. It is also registered as 'azimuth.eth' on ENS.
*/
const mainnet = '0x223c067f8cf28ae173ee5cafea60ca44c335fecb'
/**
* Check if an address is the owner of a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @param {Number} address - Owner's address.
* @return {Promise<Bool>} True if owner of the point, false otherwise.
*/
function isOwner(contracts, point, address) {
if (typeof point === 'object') {
return utils.addressEquals(point.owner, address);
}
return internal.isOwner(contracts, point, address);
}
/**
* Get the owner of a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Address>} The point's owner.
*/
function getOwner(contracts, point) {
if (typeof point === 'object') {
return point.owner;
}
return internal.getOwner(contracts, point);
}
/**
* Check if a point is active.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Bool>} True if the point is active, false otherwise.
*/
function isActive(contracts, point) {
if (typeof point === 'object') {
return point.active;
}
return internal.isActive(contracts, point);
}
/**
* Get the key configuration for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Object>} The point's key configuration.
*/
function getKeys(contracts, point) {
if (typeof point === 'object') {
return {
encryptionKey: point.encryptionKey,
authenticationKey: point.authenticationKey,
cryptoSuiteVersion: point.cryptoSuiteVersion,
keyRevisionNumber: point.keyRevisionNumber
};
}
return internal.getKeys(contracts, point);
}
/**
* Get the key revision number of a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Number>} The point's key revision number.
*/
function getKeyRevisionNumber(contracts, point) {
if (typeof point === 'object') {
return point.keyRevisionNumber;
}
return internal.getKeyRevisionNumber(contracts, point);
}
/**
* Check if a point has been booted.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Bool>} True if it has been booted, false otherwise.
*/
function hasBeenLinked(contracts, point) {
if (typeof point === 'object') {
return point.keyRevisionNumber > 0;
}
return internal.hasBeenLinked(contracts, point);
}
/**
* Check if a point is live.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Bool>} True if the point is live, false otherwise.
*/
function isLive(contracts, point) {
if (typeof point === 'object') {
let ekey = point.encryptionKey;
let akey = point.authenticationKey;
let crsv = point.cryptoSuiteVersion;
return ekey !== 0 && akey !== 0 && crsv !== 0;
}
return internal.isLive(contracts, point);
}
/**
* Get a point's continuity number.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Number>} The point's continuity number.
*/
function getContinuityNumber(contracts, point) {
if (typeof point === 'object') {
return point.continuityNumber;
}
return internal.getContinuityNumber(contracts, point);
}
/**
* Get a point's spawn count.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Number>} The point's spawn count.
*/
function getSpawnCount(contracts, point) {
return internal.getSpawnCount(contracts, point);
}
/**
* Get an array of all child points the target point has spawned.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Array<Number>>} The spawned points.
*/
async function getSpawned(contracts, point) {
//There is an inconsistency in the web3 library: in some versions
// getSpawned returns Array<Number>, in others Array<String>,
// so map to Number to be on the safe side.
return (await internal.getSpawned(contracts, point)).map(Number);
}
/**
* Get a point's sponsor.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Number>} The point's sponsor.
*/
function getSponsor(contracts, point) {
if (typeof point === 'object') {
return point.sponsor;
}
return internal.getSponsor(contracts, point);
}
/**
* Get an array of all points the target point is sponsoring.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Array<String>>} The points being sponsored.
*/
const getSponsoring = internal.getSponsoring;
/**
* Get the number of points the target point is sponsoring.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Number>} The amount of points being sponsored.
*/
const getSponsoringCount = internal.getSponsoringCount;
/**
* Check if a point has a sponsor.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Bool>} True if the point has a sponsor, false otherwise.
*/
function hasSponsor(contracts, point) {
if (typeof point === 'object') {
return point.hasSponsor;
}
return internal.hasSponsor(contracts, point);
}
/**
* Check if a point is the sponsor of another.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @param {Number} sponsor - The sponsor's point number.
* @return {Promise<Bool>} True if a sponsor, false otherwise.
*/
function isSponsor(contracts, point, sponsor) {
if (typeof point === 'object') {
return point.hasSponsor && point.sponsor === sponsor;
}
return internal.isSponsor(contracts, point, sponsor);
}
/**
* Check if a point is requesting escape.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Bool>} True if requesting escape, false otherwise.
*/
function isEscaping(contracts, point) {
if (typeof point === 'object') {
return point.escapeRequested;
}
return internal.isEscaping(contracts, point);
}
/**
* Get the sponsor that another point is requesting escape to.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<Number>} The sponsor point number.
*/
function getEscapeRequest(contracts, point) {
if (typeof point === 'object') {
return point.escapeRequestedTo;
}
return internal.getEscapeRequest(contracts, point);
}
/**
* Get a list of points that are requesting escape to a sponsor.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Array<Number>>} An array of points requesting escape
*/
const getEscapeRequests = internal.getEscapeRequests;
/**
* Get the number of points that are requesting escape to a sponsor.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Number>} The number of points requesting escape
*/
const getEscapeRequestsCount = internal.getEscapeRequestsCount;
/**
* Check if a point is requesting escape to another point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @param {Number} sponsor - Sponsor's point number.
* @return {Promise<Bool>} True if escape to sponsor requested, false
* otherwise.
*/
function isRequestingEscapeTo(contracts, point, sponsor) {
if (typeof point === 'object') {
return point.escapeRequested && point.escapeRequestedTo === sponsor;
}
return internal.isRequestingEscapeTo(contracts, point, sponsor);
}
/**
* Check if an address is a spawn proxy for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @param {String} address - Target address.
* @return {Promise<Bool>} True if address is spawn proxy, false otherwise.
*/
function isSpawnProxy(contracts, point, address) {
if (typeof point === 'object') {
return utils.addressEquals(point.spawnProxy, address);
}
return internal.isSpawnProxy(contracts, point, address);
}
/**
* Get the spawn proxy for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<String>} The spawn proxy's address.
*/
function getSpawnProxy(contracts, point) {
if (typeof point === 'object') {
return point.spawnProxy;
}
return internal.getSpawnProxy(contracts, point);
}
/**
* Check if an address is a transfer proxy for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @param {String} address - Target address.
* @return {Promise<Bool>} True if the address is a transfer proxy, false
* otherwise.
*/
function isTransferProxy(contracts, point, address) {
if (typeof point === 'object') {
return utils.addressEquals(point.transferProxy, address);
}
return internal.isTransferProxy(contracts, point, address);
}
/**
* Get the transfer proxy for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<String>} The transfer proxy's address.
*/
function getTransferProxy(contracts, point) {
if (typeof point === 'object') {
return point.transferProxy;
}
return internal.getTransferProxy(contracts, point);
}
// NB (jtobin):
//
// The following do not work with cached point types, and AFAICT can not be
// made to.
/**
* Calculate the prefix of a point.
* @param {Number} point - Point number.
* @return {Number} The point's prefix.
*/
function getPrefix(point) {
if (point < 65536) { return point % 256; }
return point % 65536;
}
let PointSize = {
Galaxy: 0,
Star: 1,
Planet: 2
}
/**
* Calculate the size of a point.
* @param {Number} point - Point number.
* @return {Number} The point's size.
*/
function getPointSize(point) {
if (point < 256) { return PointSize.Galaxy; }
if (point < 65536) { return PointSize.Star; }
return PointSize.Planet;
}
/**
* Get the azimuth contract owner.
* @param {Object} contracts - An Urbit contracts object.
* @return {Promise<String>} The contract owner's address.
*/
const owner = internal.owner;
/**
* Get a point object, given its point id.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @param {string} what - 'state', 'rights', defaults to 'both'
* @return {Promise<Object>} A point object with the requested data.
*/
async function getPoint(contracts, point, what) {
what = what || 'both';
let data = {};
if (what === 'both' || what === 'state') {
data = await internal.getPoint(contracts, point);
}
if (what === 'both' || what === 'rights') {
Object.assign(data, await internal.getRights(contracts, point));
}
return data;
}
/**
* Get a list of unspawned/spawnable points
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @return {Promise<Array<Number>>} - Unspawned children of point
*/
async function getUnspawnedChildren(contracts, point) {
let size = getPointSize(point);
if (size >= PointSize.Planet) {
return [];
}
let spawned = await getSpawned(contracts, point);
let unspawned = [];
let childSpace = (size === PointSize.Galaxy) ? 0x100 : 0x10000;
for (let i = 1; i < childSpace; i++) {
let child = point + (i*childSpace);
if (spawned.indexOf(child) < 0) {
unspawned.push(child);
}
}
return unspawned;
}
/**
* Get the block at which the point was activated. Returns zero if it hasn't
* been activated yet.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @param {Number} minBlock - (optional) Block to start search at. (Default 0.)
* @param {Number} maxBlock - (optional) Block to end search at. (Default latest.)
* @return {Promise<Number>} - Block of activation.
*/
async function getActivationBlock(contracts, point, minBlock, maxBlock) {
minBlock = minBlock || 0;
maxBlock = maxBlock || 'latest';
const logs = await contracts.azimuth.getPastEvents('Activated', {
fromBlock: minBlock,
toBlock: maxBlock,
filter: { point: [point] },
});
if (logs.length === 0) {
return 0;
} else {
return logs[0].blockNumber;
}
}
/**
* Get the points that an address owns.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Array<Number>>} An array of owned azimuth.
*/
const getOwnedPoints = internal.getOwnedPoints;
/**
* Get a count of points owned by an address.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Number>} Owned point count for the address.
*/
const getOwnedPointCount = internal.getOwnedPointCount;
/**
* Get the point at the given index of the array containing an owner's azimuth.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @param {Number} index - The index of the array.
* @return {Promise<Number>} The point at the provided index.
*/
const getOwnedPointAtIndex = internal.getOwnedPointAtIndex;
/**
* Check if an address is a management proxy for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @param {String} address - Target address.
* @return {Promise<Bool>} True if address is management proxy, false otherwise.
*/
function isManagementProxy(contracts, point, address) {
if (typeof point === 'object') {
return utils.addressEquals(point.managementProxy, address);
}
return internal.isManagementProxy(contracts, point, address);
}
/**
* Get the management proxy for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number | Object} point - Point number or point object.
* @return {Promise<String>} The management proxy's address.
*/
function getManagementProxy(contracts, point) {
if (typeof point === 'object') {
return point.managementProxy;
}
return internal.getManagementProxy(contracts, point);
}
/**
* Check if an address can manage a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @param {String} address - The manager's address.
* @return {Promise<Bool>} True if the address can manage the point, false
* otherwise.
*/
const canManage = internal.canManage;
/**
* Get a count of the points an address is managing.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Number>} The count of points being managed.
*/
const getManagerForCount = internal.getManagerForCount;
/**
* Get the points an account is managing.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Array<Number>>} The points being managed.
*/
const getManagerFor = internal.getManagerFor;
/**
* Check if an address is a voting proxy for an owner.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The owner's address.
* @param {String} address - The voting proxy's address.
* @return {Promise<Bool>} True is voting proxy, false otherwise.
*/
const isVotingProxy = internal.isVotingProxy;
/**
* Check if an address can vote for a point.
* @param {Object} contracts - An Urbit contracts object.
* @param {Number} point - Point number.
* @param {String} address - The target address.
* @return {Promise<Bool>} True is the address can vote for the point, false
* otherwise.
*/
const canVoteAs = internal.canVoteAs;
/**
* Get a count of the points an address can vote for.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Number>} The count of points that can be voted for.
*/
const getVotingForCount = internal.getVotingForCount;
/**
* Get the points an account is voting for.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Array<Number>>} The points being voted for.
*/
const getVotingFor = internal.getVotingFor;
/**
* Get a count of the points an address is a spawn proxy for.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Number>} The count of azimuth.
*/
const getSpawningForCount = internal.getSpawningForCount;
/**
* Get the points an account is a spawn proxy for.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Array<Number>>} The azimuth.
*/
const getSpawningFor = internal.getSpawningFor;
/**
* Get a count of the points an address is a transfer proxy for.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Number>} The count of azimuth.
*/
const getTransferringForCount = internal.getTransferringForCount;
/**
* Get the points an account is a transfer proxy for.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The target address.
* @return {Promise<Array<Number>>} The azimuth.
*/
const getTransferringFor = internal.getTransferringFor;
/**
* Check if an address is an operator for another.
* @param {Object} contracts - An Urbit contracts object.
* @param {String} address - The owner's address.
* @param {String} address - The operator's address.
* @return {Promise<Bool>} True is operator, false otherwise.
*/
const isOperator = internal.isOperator;
module.exports = {
mainnet,
owner,
getPoint,
getOwnedPoints,
getOwnedPointCount,
getOwnedPointAtIndex,
isManagementProxy,
canManage,
getManagerForCount,
getManagerFor,
isVotingProxy,
canVoteAs,
getVotingForCount,
getVotingFor,
isOwner,
getOwner,
isActive,
getKeys,
getKeyRevisionNumber,
hasBeenLinked,
isLive,
getContinuityNumber,
getSpawnCount,
getSpawned,
getUnspawnedChildren,
getActivationBlock,
getSponsor,
getSponsoring,
getSponsoringCount,
hasSponsor,
isSponsor,
isEscaping,
getEscapeRequest,
getEscapeRequests,
getEscapeRequestsCount,
isRequestingEscapeTo,
isSpawnProxy,
getSpawnProxy,
getSpawningForCount,
getSpawningFor,
isTransferProxy,
getTransferProxy,
getTransferringForCount,
getTransferringFor,
getPrefix,
PointSize,
getPointSize,
isOperator
}