Skip to content

Commit

Permalink
Update API with new Wallet methods
Browse files Browse the repository at this point in the history
  • Loading branch information
martindale committed Jan 2, 2020
1 parent b584fd2 commit 8278fed
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 13 deletions.
96 changes: 96 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ inspection of <a href="#Machine">Machine</a> mechanics.</p>
<dt><a href="#Machine">Machine</a></dt>
<dd><p>General-purpose state machine with <a href="#Vector">Vector</a>-based instructions.</p>
</dd>
<dt><a href="#Mempool">Mempool</a></dt>
<dd><p>Stores a list of <a href="Transaction">Transaction</a> elements.</p>
</dd>
<dt><a href="#Message">Message</a> : <code>Object</code></dt>
<dd><p>The <a href="#Message">Message</a> type defines the Application Messaging Protocol, or AMP.
Each <a href="Actor">Actor</a> in the network receives and broadcasts messages,
Expand Down Expand Up @@ -118,6 +121,9 @@ execute either the full set or none.</p>
<dd><p>The <a href="#Transition">Transition</a> type reflects a change from one finite
<a href="#State">State</a> to another.</p>
</dd>
<dt><a href="#Value">Value</a></dt>
<dd><p><a href="Number">Number</a>-like type.</p>
</dd>
<dt><a href="#Vector">Vector</a></dt>
<dd></dd>
<dt><a href="#Walker">Walker</a></dt>
Expand Down Expand Up @@ -332,6 +338,7 @@ Fabric-based networking and storage.
| Name | Type | Description |
| --- | --- | --- |
| components | [<code>Collection</code>](#Collection) | Interface elements. |
| stash | [<code>Store</code>](#Store) | Routable [Datastore](Datastore). |


* [App](#App)[<code>Scribe</code>](#Scribe)
Expand Down Expand Up @@ -538,6 +545,11 @@ Creates a channel between to peers. Useful for aggregation
of many transactions over time, to be settled on-chain later.

**Kind**: global class

* [Channel](#Channel)
* [new Channel([settings])](#new_Channel_new)
* [.add(amount)](#Channel+add)

<a name="new_Channel_new"></a>

### new Channel([settings])
Expand All @@ -546,6 +558,17 @@ of many transactions over time, to be settled on-chain later.
| --- | --- | --- |
| [settings] | <code>Object</code> | Configuration for the channel. |

<a name="Channel+add"></a>

### channel.add(amount)
Add an amount to the channel's balance.

**Kind**: instance method of [<code>Channel</code>](#Channel)

| Param | Type | Description |
| --- | --- | --- |
| amount | <code>Number</code> | Amount value to add to current outgoing balance. |

<a name="Circuit"></a>

## Circuit
Expand Down Expand Up @@ -1053,6 +1076,23 @@ so counts should always begin with 0.
| --- | --- | --- |
| input | [<code>Vector</code>](#Vector) | Input state, undefined if desired. |

<a name="Mempool"></a>

## Mempool
Stores a list of [Transaction](Transaction) elements.

**Kind**: global class
**Emits**: <code>event:{Message} confirmed Emitted when the Mempool has dropped a transaction.</code>
<a name="new_Mempool_new"></a>

### new Mempool(settings)
Creates an instance of a [Mempool](#Mempool) [Service](#Service).


| Param | Type | Description |
| --- | --- | --- |
| settings | <code>Object</code> | Map of settings to utilize. |

<a name="Message"></a>

## Message : <code>Object</code>
Expand Down Expand Up @@ -1100,6 +1140,7 @@ transitive state.
* [.get(key)](#Store+get) ⇒ <code>Promise</code>
* [.set(key, value)](#Store+set)
* [.trust(source)](#Store+trust)[<code>Store</code>](#Store)
* [.del(key)](#Store+del)
* [.start()](#Store+start) ⇒ <code>Promise</code>

<a name="new_Oracle_new"></a>
Expand Down Expand Up @@ -1185,6 +1226,17 @@ Implicitly trust an [Event](Event) source.
| --- | --- | --- |
| source | <code>EventEmitter</code> | Event-emitting source. |

<a name="Store+del"></a>

### oracle.del(key)
Remove a [Value](#Value) by [Path](#Path).

**Kind**: instance method of [<code>Oracle</code>](#Oracle)

| Param | Type | Description |
| --- | --- | --- |
| key | [<code>Path</code>](#Path) | Key to remove. |

<a name="Store+start"></a>

### oracle.start() ⇒ <code>Promise</code>
Expand Down Expand Up @@ -2105,6 +2157,7 @@ Long-term storage.
* [.get(key)](#Store+get) ⇒ <code>Promise</code>
* [.set(key, value)](#Store+set)
* [.trust(source)](#Store+trust)[<code>Store</code>](#Store)
* [.del(key)](#Store+del)
* [.start()](#Store+start) ⇒ <code>Promise</code>

<a name="new_Store_new"></a>
Expand Down Expand Up @@ -2179,6 +2232,17 @@ Implicitly trust an [Event](Event) source.
| --- | --- | --- |
| source | <code>EventEmitter</code> | Event-emitting source. |

<a name="Store+del"></a>

### store.del(key)
Remove a [Value](#Value) by [Path](#Path).

**Kind**: instance method of [<code>Store</code>](#Store)

| Param | Type | Description |
| --- | --- | --- |
| key | [<code>Path</code>](#Path) | Key to remove. |

<a name="Store+start"></a>

### store.start() ⇒ <code>Promise</code>
Expand Down Expand Up @@ -2266,6 +2330,38 @@ The [Transition](#Transition) type reflects a change from one finite
| --- | --- | --- |
| settings | <code>Object</code> | Configuration for the transition object. |

<a name="Value"></a>

## Value
[Number](Number)-like type.

**Kind**: global class

* [Value](#Value)
* [new Value(data)](#new_Value_new)
* [.value(input)](#Value+value)

<a name="new_Value_new"></a>

### new Value(data)
Use the [Value](#Value) type to interact with [Number](Number)-like objects.


| Param | Type | Description |
| --- | --- | --- |
| data | <code>Mixed</code> | Input value. |

<a name="Value+value"></a>

### value.value(input)
Compute the numeric representation of this input.

**Kind**: instance method of [<code>Value</code>](#Value)

| Param | Type | Description |
| --- | --- | --- |
| input | <code>String</code> | Input string to seek for value. |

<a name="Vector"></a>

## Vector
Expand Down
59 changes: 59 additions & 0 deletions types/mempool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
'use strict';

const Entity = require('./entity');
const Service = require('./service');
const Collection = require('./collection');
const Transaction = require('./transaction');

/**
* Stores a list of {@link Transaction} elements.
* @emits {Message} confirmed Emitted when the Mempool has dropped a transaction.
*/
class Mempool extends Service {
/**
* Creates an instance of a {@link Mempool} {@link Service}.
* @param {Object} settings Map of settings to utilize.
*/
constructor (settings = {}) {
super(settings);

this.settings = Object.assign({
name: '@fabric/mempool'
}, settings);

this.transactions = new Collection({
type: Transaction
});

this._state = {
transactions: []
}

return this;
}

async _remove (txid) {
return false;
}

async add (transaction) {
const entity = new Entity(transaction);
const target = await this.transactions.create(transaction);
// TODO: compare target.id and entity.id
this._state.transactions.push(entity.id);
}

async confirm (txid) {
const found = await this._findByTXID(txid);
if (!found) throw new Error(`Could not find transaction: ${txid} in ${JSON.stringify(this.transactions.map())}`);
this.emit('confirmed', txid);
}

async _findByTXID (txid) {
return this._state.transactions.filter((x) => {
return x.id == query;
});
}
}

module.exports = Mempool;
6 changes: 3 additions & 3 deletions types/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class Message extends Vector {
return message;
}

get [Symbol.toStringTag] () {
return 'Message';
}
/* get [Symbol.toStringTag] () {
return `<Message | ${JSON.stringify(this.raw)}>`;
} */

get id () {
return crypto.createHash('sha256').update(this.asRaw()).digest('hex');
Expand Down
1 change: 1 addition & 0 deletions types/scribe.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const State = require('./state');

/**
* Simple tag-based recordkeeper.
* @extends State
* @property {Object} config Current configuration.
*/
class Scribe extends State {
Expand Down
36 changes: 30 additions & 6 deletions types/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,19 @@ class Store extends Scribe {
this['@entity']['@data'].collections = {};
this['@entity']['@data'].tips = {};

this.commits = new Collection({
type: 'State'
});

Object.assign(this['@data'], this['@entity']['@data']);

return this;
}

async _errorHandler (err) {
console.error('[FABRIC:STORE]', 'Error condition:', err);
}

async _setEncrypted (path, value, passphrase = '') {
let secret = value; // TODO: encrypt value
let name = crypto.createHash('sha256').createHash(path).digest('hex');
Expand Down Expand Up @@ -171,7 +179,7 @@ class Store extends Scribe {
let tip = null;

if (!self.db) {
await self.open();
await self.open().catch(self._errorHandler.bind(self));
}

let family = null;
Expand Down Expand Up @@ -446,15 +454,31 @@ class Store extends Scribe {
return this;
}

del (key) {
/**
* Remove a {@link Value} by {@link Path}.
* @param {Path} key Key to remove.
*/
async del (key) {
if (!this.db) {
await this.open();
}

return this.db.del(key);
}

batch (ops, done) {
async batch (ops, done) {
if (!this.db) {
await this.open();
}

return this.db.batch(ops).then(done);
}

async commit () {
const entity = new Entity(this.state);

if (this.db) {
return this.db.batch(ops).then(done);
} else {
return done;
await this.db.close();
}
}

Expand Down
30 changes: 30 additions & 0 deletions types/value.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

const MAX = Math.pow(2, 64);
const BN = require('bn.js');

/**
* {@link Number}-like type.
*/
class Value {
/**
* Use the {@link Value} type to interact with {@link Number}-like objects.
* @param {Mixed} data Input value.
*/
constructor (data = {}) {
this._state = {
input: data,
output: null
};
}

/**
* Compute the numeric representation of this input.
* @param {String} input Input string to seek for value.
*/
value (input) {
return new BN(input);
}
}

module.exports = Value;
Loading

0 comments on commit 8278fed

Please sign in to comment.