Skip to content

Commit

Permalink
now Wallet inherits EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Nov 20, 2014
1 parent 565a403 commit 2c50a2f
Show file tree
Hide file tree
Showing 13 changed files with 212 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cc-wallet-core",
"version": "0.1.3",
"version": "0.2.0",
"description": "",
"main": "./src/index.js",
"keywords": [],
Expand Down
141 changes: 120 additions & 21 deletions src/Wallet.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var events = require('events')
var inherits = require('util').inherits

var _ = require('lodash')
var Q = require('q')

Expand All @@ -15,13 +18,68 @@ var bitcoin = require('./bitcoin')
var verify = require('./verify')


/**
* @event Wallet#error
* @param {Error} error
*/

/**
* @event Wallet#newHeight
* @param {number} height
*/

/**
* @event Wallet#loadTx
* @param {Transaction} tx
*/

/**
* @event Wallet#addTx
* @param {Transaction} tx
*/

/**
* @event Wallet#updateTx
* @param {string} tx
*/

/**
* @event Wallet#revertTx
* @param {Transaction} tx
*/

/**
* @event Wallet#newAddress
* @param {Address} address
*/

/**
* @event Wallet#touchAddress
* @param {string} address
*/

/**
* @event Wallet#newAsset
* @param {AssetDefinition} assetdef
*/

/**
* @event Wallet#touchAsset
* @param {AssetDefinition} assetdef
*/

/**
* @event Wallet#initialize
*/

/**
* @callback Wallet~errorCallback
* @param {?Error} error
*/

/**
* @class Wallet
* @extends events.EventEmitter
*
* @param {Object} opts
* @param {boolean} [opts.testnet=false]
Expand All @@ -38,47 +96,74 @@ function Wallet(opts) {
storageSaveTimeout: 1000
}, opts)


var self = this
events.EventEmitter.call(self)

verify.boolean(opts.testnet)
verify.string(opts.network)
opts.networkOpts = _.extend({ testnet: opts.testnet }, opts.networkOpts)
verify.boolean(opts.networkOpts.testnet)
verify.string(opts.blockchain)
verify.number(opts.storageSaveTimeout)

this.bitcoinNetwork = opts.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
self.bitcoinNetwork = opts.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin

this.config = new ConfigStorage()
self.config = new ConfigStorage()

this.network = new network[opts.network](this, opts.networkOpts)
this.blockchain = new blockchain[opts.blockchain](this.network, { testnet: opts.testnet })
self.network = new network[opts.network](self, opts.networkOpts)
self.blockchain = new blockchain[opts.blockchain](self.network, { testnet: opts.testnet })

this.cdStorage = new cclib.ColorDefinitionStorage()
this.cdManager = new cclib.ColorDefinitionManager(this.cdStorage)
self.cdStorage = new cclib.ColorDefinitionStorage()
self.cdManager = new cclib.ColorDefinitionManager(self.cdStorage)

this.cDataStorage = new cclib.ColorDataStorage()
this.cData = new cclib.ColorData(this.cDataStorage)
self.cDataStorage = new cclib.ColorDataStorage()
self.cData = new cclib.ColorData(self.cDataStorage)

this.aStorage = new address.AddressStorage()
this.aManager = new address.AddressManager(this.aStorage, this.bitcoinNetwork)
self.aStorage = new address.AddressStorage()
self.aManager = new address.AddressManager(self.aStorage, self.bitcoinNetwork)

this.adStorage = new asset.AssetDefinitionStorage()
this.adManager = new asset.AssetDefinitionManager(this.cdManager, this.adStorage)
self.adStorage = new asset.AssetDefinitionStorage()
self.adManager = new asset.AssetDefinitionManager(self.cdManager, self.adStorage)
if (opts.systemAssetDefinitions)
opts.systemAssetDefinitions.forEach(function(sad) {
this.adManager.resolveAssetDefinition(sad)
}.bind(this))
self.adManager.resolveAssetDefinition(sad)
}.bind(self))

this.txStorage = new tx.TxStorage({saveTimeout: opts.storageSaveTimeout})
this.txDb = new tx.TxDb(this, this.txStorage)
this.txFetcher = new tx.TxFetcher(this.txDb, this.blockchain)
self.txStorage = new tx.TxStorage({saveTimeout: opts.storageSaveTimeout})
self.txDb = new tx.TxDb(self, self.txStorage)
self.txFetcher = new tx.TxFetcher(self.txDb, self.blockchain)

this.coinStorage = new coin.CoinStorage({saveTimeout: opts.storageSaveTimeout})
this.coinManager = new coin.CoinManager(this, this.coinStorage)
self.coinStorage = new coin.CoinStorage({saveTimeout: opts.storageSaveTimeout})
self.coinManager = new coin.CoinManager(self, self.coinStorage)

this.historyManager = new HistoryManager(this)
self.historyManager = new HistoryManager(self)

// events
self.network.on('error', function(error) { self.emit('error', error) })
self.blockchain.on('error', function(error) { self.emit('error', error) })
self.txDb.on('error', function(error) { self.emit('error', error) })
self.txFetcher.on('error', function(error) { self.emit('error', error) })
self.coinManager.on('error', function(error) { self.emit('error', error) })

self.blockchain.on('newHeight', function(height) { self.emit('newHeight', height) })

self.txDb.on('loadTx', function(tx) { self.emit('loadTx', tx) })
self.txDb.on('addTx', function(tx) { self.emit('addTx', tx) })
self.txDb.on('updateTx', function(tx) { self.emit('updateTx', tx) })
self.txDb.on('revertTx', function(tx) { self.emit('revertTx', tx) })

self.aManager.on('newAddress', function(address) { self.emit('newAddress', address) })
self.coinManager.on('touchAddress', function(address) { self.emit('touchAddress', address) })

self.aManager.on('newAsset', function(assetdef) { self.emit('newAsset', assetdef) })
self.coinManager.on('touchAsset', function(assetdef) { self.emit('touchAsset', assetdef) })
}

inherits(Wallet, events.EventEmitter)

Wallet.prototype.getBitcoinNetwork = function() { return this.bitcoinNetwork }
Wallet.prototype.getNetwork = function() { return this.network }
Wallet.prototype.getBlockchain = function() { return this.blockchain }
Wallet.prototype.getColorDefinitionManager = function() { return this.cdManager }
Wallet.prototype.getColorData = function() { return this.cData }
Expand Down Expand Up @@ -120,6 +205,7 @@ Wallet.prototype.initialize = function(seedHex) {
})

this.config.set('initialized', true)
this.emit('initialize')
}

/**
Expand Down Expand Up @@ -526,6 +612,19 @@ Wallet.prototype.sendTx = function(tx, cb) {
.done(function() { cb(null) }, function(error) { cb(error) })
}

/**
*/
Wallet.prototype.removeListeners = function() {
this.removeAllListeners()
this.network.removeAllListeners()
this.blockchain.removeAllListeners()
this.aManager.removeAllListeners()
this.adManager.removeAllListeners()
this.txDb.removeAllListeners()
this.txFetcher.removeAllListeners()
this.coinManager.removeAllListeners()
}

/**
*/
Wallet.prototype.clearStorage = function() {
Expand All @@ -535,8 +634,8 @@ Wallet.prototype.clearStorage = function() {
this.cDataStorage.clear()
this.aStorage.clear()
this.adStorage.clear()
this.coinStorage.clear()
this.txStorage.clear()
this.coinStorage.clear()
}


Expand Down
19 changes: 17 additions & 2 deletions src/address/AddressManager.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
var events = require('events')
var inherits = require('util').inherits

var _ = require('lodash')

var cclib = require('../cclib')
Expand Down Expand Up @@ -72,20 +75,29 @@ function selectChain(definition) {
}


/**
* @event AddressManager#newAddress
* @param {Address} address
*/

/**
* @class AddressManager
*
* @extends events.EventEmitter
* @param {storage.AddressStorage} storage
* @param {Object} network Network description from bitcoinjs-lib.networks
*/
function AddressManager(storage, network) {
verify.AddressStorage(storage)
verify.bitcoinNetwork(network)

events.EventEmitter.call(this)

this.storage = storage
this.network = network
}

inherits(AddressManager, events.EventEmitter)

/**
* @param {string} seedHex
* @return {bitcoinjs-lib.HDNode}
Expand Down Expand Up @@ -151,7 +163,10 @@ AddressManager.prototype.getNewAddress = function(definition, seedHex) {
if (definition instanceof AssetDefinition)
assetDefinition = definition

return new Address(this, record, this.network, assetDefinition)
var address = new Address(this, record, this.network, assetDefinition)
this.emit('newAddress', address)

return address
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/asset/AssetDefinitionManager.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,29 @@
var events = require('events')
var inherits = require('util').inherits

var _ = require('lodash')

var AssetDefinition = require('./AssetDefinition')
var verify = require('../verify')


/**
* @event AssetDefinitionManager#newAsset
* @param {AssetDefinition} assetdef
*/

/**
* @class AssetDefinitionManager
*
* @extends events.EventEmitter
* @param {coloredcoinjs-lib.ColorDefinitionManager} cdManager
* @param {AssetDefinitionStorage} storage
*/
function AssetDefinitionManager(cdManager, storage) {
verify.ColorDefinitionManager(cdManager)
verify.AssetDefinitionStorage(storage)

events.EventEmitter.call(this)

this.cdManager = cdManager
this.storage = storage

Expand All @@ -24,6 +34,8 @@ function AssetDefinitionManager(cdManager, storage) {
})
}

inherits(AssetDefinitionManager, events.EventEmitter)

/**
* @param {Object} data
* @param {string[]} data.monikers
Expand Down Expand Up @@ -60,6 +72,7 @@ AssetDefinitionManager.prototype.resolveAssetDefinition = function(data, autoAdd
colorDescs: assetdef.getColorSet().getColorDescs(),
unit: assetdef.getData().unit
})
this.emit('newAsset', assetdef)

return assetdef
}
Expand Down
5 changes: 3 additions & 2 deletions src/blockchain/Blockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ var errors = require('../errors')

/**
* @event Blockchain#error
* @type {Error} error
* @param {Error} error
*/

/**
* @event Blockchain#newHeight
* @param {number} height
*/

/**
* @event Blockchain#touchAddress
* @type {string} address
* @param {string} address
*/

/**
Expand Down
6 changes: 3 additions & 3 deletions src/blockchain/NaiveBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ function NaiveBlockchain(network, opts) {
self._headerCache = LRU({ max: opts.headerCacheSize })
this._txCache = LRU({ max: opts.txCacheSize })

self._network.on('newHeight', function() {
self._currentHeight = self._network.getCurrentHeight()
self.emit('newHeight')
self._network.on('newHeight', function(newHeight) {
self._currentHeight = newHeight
self.emit('newHeight', newHeight)
})

self._network.on('touchAddress', function(address) {
Expand Down
8 changes: 4 additions & 4 deletions src/blockchain/VerifiedBlockchain.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ VerifiedBlockchain.prototype._waitHeight = function(height) {

var deferred = Q.defer()

function onNewHeight() {
if (height <= self.getCurrentHeight())
function onNewHeight(newHeight) {
if (height <= newHeight)
deferred.resolve()
}
self.on('newHeight', onNewHeight)
Expand Down Expand Up @@ -542,7 +542,7 @@ VerifiedBlockchain.prototype._sync = function() {

self._currentBlockHash = self._storage.getLastHash()
self._currentHeight = _.last(chain).height
self.emit('newHeight')
self.emit('newHeight', self._currentHeight)

deferred.resolve()

Expand Down Expand Up @@ -610,7 +610,7 @@ VerifiedBlockchain.prototype._sync = function() {

self._currentBlockHash = self._storage.getLastHash()
self._currentHeight = index*2016 + chunk.length/80 - 1
self.emit('newHeight')
self.emit('newHeight', self._currentHeight)

index += 1
runChunkLoopOnce()
Expand Down
Loading

0 comments on commit 2c50a2f

Please sign in to comment.