forked from FabricLabs/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactor.js
430 lines (368 loc) · 10.2 KB
/
actor.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
'use strict';
// Dependencies
const crypto = require('crypto');
const { EventEmitter } = require('events');
const monitor = require('fast-json-patch');
const pointer = require('json-pointer');
// Fabric Types
const Hash256 = require('./hash256');
// Fabric Functions
const _sortKeys = require('../functions/_sortKeys');
/**
* Generic Fabric Actor.
* @access protected
* @emits message Fabric {@link Message} objects.
* @property {String} id Unique identifier for this Actor (id === SHA256(preimage)).
* @property {String} preimage Input hash for the `id` property (preimage === SHA256(ActorState)).
*/
class Actor extends EventEmitter {
/**
* Creates an {@link Actor}, which emits messages for other
* Actors to subscribe to. You can supply certain parameters
* for the actor, including key material [!!!] — be mindful of
* what you share with others!
* @param {Object} [actor] Object to use as the actor.
* @param {String} [actor.seed] BIP24 Mnemonic to use as a seed phrase.
* @param {Buffer} [actor.public] Public key.
* @param {Buffer} [actor.private] Private key.
* @returns {Actor} Instance of the Actor. Call {@link Actor#sign} to emit a {@link Signature}.
*/
constructor (actor = {}) {
super(actor);
this.settings = {
type: 'Actor',
status: 'PAUSED'
};
// Internal State
// TODO: encourage use of `state` over `_state`
// TODO: use `const state` here
this._state = {
type: this.settings.type,
status: this.settings.status,
content: this._readObject(actor)
};
// TODO: evaluate disabling by default
this.history = [];
// TODO: evaluate disabling by default
// and/or resolving performance issues at scale
try {
this.observer = monitor.observe(this._state.content, this._handleMonitorChanges.bind(this));
} catch (exception) {
console.error('UNABLE TO WATCH:', exception);
}
// TODO: use elegant method to strip these properties
Object.defineProperty(this, '_events', { enumerable: false });
Object.defineProperty(this, '_eventsCount', { enumerable: false });
Object.defineProperty(this, '_maxListeners', { enumerable: false });
Object.defineProperty(this, '_state', { enumerable: false });
Object.defineProperty(this, 'observer', { enumerable: false });
// Chainable
return this;
}
static chunk (array, size = 32) {
const chunkedArray = [];
for (var i = 0; i < array.length; i += size) {
chunkedArray.push(array.slice(i, i + size));
}
return chunkedArray;
}
/**
* Create an {@link Actor} from a variety of formats.
* @param {Object} input Target {@link Object} to create.
* @returns {Actor} Instance of the {@link Actor}.
*/
static fromAny (input = {}) {
let state = null;
if (typeof input === 'string') {
state = { content: input };
} else if (input instanceof Buffer) {
state = { content: input.toString('hex') };
} else {
state = Object.assign({}, input);
}
return new Actor(state);
}
static fromJSON (input) {
let result = null;
if (typeof input === 'string' && input.length) {
console.log('trying to parse as JSON:', input);
try {
result = JSON.parse(input);
} catch (E) {
console.error('Failure in fromJSON:', E);
}
} else {
console.trace('Invalid input:', typeof input);
}
return result;
}
/**
* Get a number of random bytes from the runtime environment.
* @param {Number} [count=32] Number of random bytes to retrieve.
* @returns {Buffer} The random bytes.
*/
static randomBytes (count = 32) {
return crypto.randomBytes(count);
}
get id () {
const buffer = Buffer.from(this.preimage, 'hex');
return Hash256.digest(buffer);
}
get generic () {
return this.toGenericMessage();
}
get preimage () {
const string = JSON.stringify(this.generic, null, ' ');
const secret = Buffer.from(string, 'utf8');
const preimage = Hash256.digest(secret);
return preimage;
}
get state () {
return JSON.parse(JSON.stringify(this._state.content || {}));
}
get status () {
return this._state.status;
}
get type () {
return this._state['@type'];
}
set state (value) {
this._state.content = value;
}
set status (value) {
this._state.status = value;
}
/**
* Explicitly adopt a set of {@link JSONPatch}-encoded changes.
* @param {Array} changes List of {@link JSONPatch} operations to apply.
* @returns {Actor} Instance of the Actor.
*/
adopt (changes) {
try {
monitor.applyPatch(this._state.content, changes);
this.commit();
} catch (exception) {
this.emit('error', exception);
}
return this;
}
/**
* Resolve the current state to a commitment.
* @returns {String} 32-byte ID
*/
commit () {
const state = new Actor(this.state);
const changes = monitor.generate(this.observer);
const parent = (this.history.length) ? this.history[this.history.length - 1].state : null;
const commit = new Actor({
changes: changes,
parent: parent,
state: state.id // TODO: include whole state?
});
this.history.push(commit);
this.emit('commit', commit);
return commit.id;
}
debug (...params) {
this.emit('debug', params);
}
/**
* Export the Actor's state to a standard {@link Object}.
* @returns {Object} Standard object.
*/
export () {
return {
id: this.id,
type: 'FabricActor',
object: this.state,
version: 1
};
}
/**
* Retrieve a value from the Actor's state by {@link JSONPointer} path.
* @param {String} path Path to retrieve using {@link JSONPointer}.
* @returns {Object} Value of the path in the Actor's state.
*/
get (path) {
return pointer.get(this._state.content, path);
}
log (...params) {
this.emit('log', ...params);
}
mutate (seed) {
if (seed === 0 || !seed) seed = this.randomBytes(32).toString('hex');
const patches = [
{ op: 'replace', path: '/seed', value: seed }
];
monitor.applyPatch(this._state.content, patches);
console.log('new state:', this._state.content);
this.commit();
return this;
}
/**
* Set a value in the Actor's state by {@link JSONPointer} path.
* @param {String} path Path to set using {@link JSONPointer}.
* @param {Object} value Value to set.
* @returns {Object} Value of the path in the Actor's state.
*/
set (path, value) {
pointer.set(this._state.content, path, value);
this.commit();
return this;
}
setStatus (value) {
if (!value) throw new Error('Cannot remove status.');
this.status = value;
}
/**
* Casts the Actor to a normalized Buffer.
* @returns {Buffer}
*/
toBuffer () {
return Buffer.from(this.serialize(), 'utf8');
}
/**
* Casts the Actor to a generic message.
* @returns {Object} Generic message object.
*/
toGenericMessage () {
return {
type: 'FabricActorState',
object: this.toObject()
};
}
toJSON () {
return {
'@id': this.id,
...this.state
};
}
/**
* Returns the Actor's current state as an {@link Object}.
* @returns {Object}
*/
toObject () {
return _sortKeys(this.state);
}
toString (format = 'json') {
switch (format) {
case 'hex':
return Buffer.from(this.serialize(), 'utf8').toString('hex');
case 'json':
default:
return this.serialize();
}
}
/**
* Toggles `status` property to paused.
* @returns {Actor} Instance of the Actor.
*/
pause () {
this.status = 'PAUSING';
this.commit();
this.status = 'PAUSED';
return this;
}
randomBytes (count = 32) {
return crypto.randomBytes(count);
}
/**
* Serialize the Actor's current state into a JSON-formatted string.
* @returns {String}
*/
serialize () {
let json = null;
try {
json = JSON.stringify(this.toObject(), null, ' ');
} catch (exception) {
json = JSON.stringify({
type: 'Error',
content: `Exception serializing: ${exception}`
}, null, ' ');
}
return json;
}
sha256 (value) {
return Hash256.digest(value);
}
/**
* Signs the Actor.
* @returns {Actor}
*/
sign () {
throw new Error('Unimplemented on this branch. Use @fabric/core/types/signer instead.');
/* this.signature = this.key._sign(this.toBuffer());
this.emit('signature', this.signature);
return this; */
}
/**
* Toggles `status` property to unpaused.
* @returns {Actor} Instance of the Actor.
*/
unpause () {
this.status = 'UNPAUSING';
this.commit();
this.status = 'UNPAUSED';
return this;
}
/**
* Get the inner value of the Actor with an optional cast type.
* @param {String} [format] Cast the value to one of: `buffer, hex, json, string`
* @returns {Object} Inner value of the Actor as an {@link Object}, or cast to the requested `format`.
*/
value (format = 'object') {
switch (format) {
default:
return this.state;
case 'buffer':
return Buffer.from(this.value('string'), 'utf8');
case 'hex':
return this.value('buffer').toString('hex');
case 'json':
case 'string':
return JSON.stringify(this.state);
}
}
_getField (name) {
return this._state.content[name];
}
/**
* Incurs 1 SYSCALL
* @access private
* @returns {Object}
*/
_getState () {
return this.state;
}
_handleMonitorChanges (changes) {
// TODO: emit global state event here
// after verify, commit
}
/**
* Parse an Object into a corresponding Fabric state.
* @param {Object} input Object to read as input.
* @returns {Object} Fabric state.
*/
_readObject (input = {}) {
let state = {};
if (typeof input === 'string') {
state = Object.assign(state, {
type: 'String',
size: input.length,
content: input,
encoding: 'utf8'
});
} else if (input instanceof Buffer) {
state = Object.assign(state, {
type: 'Buffer',
size: input.length,
content: input.toString('hex'),
encoding: 'hex'
});
} else {
state = Object.assign(state, input);
}
return state;
}
}
module.exports = Actor;