Skip to content

Commit

Permalink
add Wallet.checkAddress
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Aug 20, 2014
1 parent 3639343 commit 073924f
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 38 deletions.
96 changes: 59 additions & 37 deletions src/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ function Wallet(data) {

this.aStorage = new storage.AddressStorage()
this.aManager = new AddressManager(this.aStorage)
var network = data.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
this.aManager.setMasterKeyFromSeed(data.masterKey, network)
this.network = data.testnet ? bitcoin.networks.testnet : bitcoin.networks.bitcoin
this.aManager.setMasterKeyFromSeed(data.masterKey, this.network)

this.blockchain = new cclib.blockchain.BlockrIOAPI({ testnet: data.testnet })

Expand All @@ -41,10 +41,7 @@ function Wallet(data) {

this.adStorage = new storage.AssetDefinitionStorage()
this.adManager = new asset.AssetDefinitionManager(this.cdManager, this.adStorage)

this.adManager.getAllAssets().forEach(function(assetdef) {
this.getSomeAddress(assetdef)
}.bind(this))
this.adManager.getAllAssets().forEach(this.getSomeAddress.bind(this))

this.txTransformer = new tx.TxTransformer()
}
Expand Down Expand Up @@ -112,6 +109,32 @@ Wallet.prototype.getAllAddresses = function(assetdef) {
return addresses.map(function(address) { return address.getAddress() })
}

/**
* @param {string} address
* @return {boolean}
*/
Wallet.prototype.checkAddress = function(assetdef, address) {
var isValid = true

/** Check colorId, except bitcoin */
var colordefs = assetdef.getColorSet().getColorDefinitions()
var isBitcoinAsset = colordefs.length === 1 && colordefs[0].getColorType() === 'uncolored'
if (!isBitcoinAsset) {
isValid = isValid && assetdef.getId() === address.split('@')[0]
address = address.split('@')[1]
}

/** Check bitcoin address */
try {
address = bitcoin.Address.fromBase58Check(address)
isValid = isValid && address.version === this.network.pubKeyHash
} catch (e) {
isValid = false
}

return isValid
}

/**
* Return new CoinQuery for request confirmed/unconfirmed coins, balance ...
*
Expand Down Expand Up @@ -149,37 +172,36 @@ Wallet.prototype._getBalance = function(assetdef, opts, cb) {
assert(!opts.onlyConfirmed || !opts.onlyUnconfirmed, 'opts.onlyConfirmed and opts.onlyUnconfirmed both is true')
assert(_.isFunction(cb), 'Expected function cb, got ' + cb)

var coinQuery = this.getCoinQuery()
coinQuery = coinQuery.onlyColoredAs(assetdef.getColorSet().getColorDefinitions())
coinQuery = coinQuery.onlyAddresses(this.getAllAddresses(assetdef))

if (opts.onlyConfirmed)
coinQuery = coinQuery.getConfirmed()
if (opts.onlyUnconfirmed)
coinQuery = coinQuery.getUnconfirmed()

coinQuery.getCoins(function(error, coinList) {
if (error !== null) {
cb(error)
return
}

coinList.getTotalValue(function(error, colorValues) {
if (error !== null) {
cb(error)
return
}

var balance = 0
if (colorValues.length === 1)
balance = colorValues[0].getValue()
// When supported more than one colorDefinition in one AssetDefinition
//if (colorValues.length > 1)
// balance = colorValues.reduce(function(cv1, cv2) { return cv1.getValue() + cv2.getValue() })

cb(null, balance)
})
})
var self = this

Q.fcall(function() {
var coinQuery = self.getCoinQuery()
coinQuery = coinQuery.onlyColoredAs(assetdef.getColorSet().getColorDefinitions())
coinQuery = coinQuery.onlyAddresses(self.getAllAddresses(assetdef))

if (opts.onlyConfirmed)
coinQuery = coinQuery.getConfirmed()
if (opts.onlyUnconfirmed)
coinQuery = coinQuery.getUnconfirmed()

return Q.ninvoke(coinQuery, 'getCoins')

}).then(function(coinList) {
return Q.ninvoke(coinList, 'getTotalValue')

}).then(function(colorValues) {
if (colorValues.length === 0)
return 0

return cclib.color.ColorValue.sum(colorValues).getValue()

}).then(function(balance) {
cb(null, balance)

}).fail(function(error) {
cb(error)

}).done()
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/asset/AssetDefinition.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var assert = require('assert')
var _ = require('lodash')
var cclib = require('coloredcoinjs-lib')


/**
* AssetDefinition description
* @typedef {Object} AssetDefinitionDesc
Expand Down
2 changes: 1 addition & 1 deletion src/tx/OperationalTx.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ OperationalTx.prototype.getDustThreshold = function() {

/*
* @param {ColorValue}
* @param {Object|null} [feeEstimator=null]
* @param {?Object} [feeEstimator=null]
* @param {OperationalTx~selectCoins} cb
*/
OperationalTx.prototype.selectCoins = function(colorValue, feeEstimator, cb) {
Expand Down
10 changes: 10 additions & 0 deletions test/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ describe('Wallet', function() {
var addresses = wallet.getAllAddresses(bitcoin)
expect(addresses).to.deep.equal(['mmHBqwp1fDwWXaXqo5ZrEE4qAoXH5xkUvd'])
})

it('checkAddress bitcoin', function() {
var isValid = wallet.checkAddress(bitcoin, 'mgFmR51KZRKb2jcmJb276KQK9enC9cmG9v')
expect(isValid).to.be.true
})

it('checkAddress color', function() {
var isValid = wallet.checkAddress(epobc, 'ES5wsZmWHs5xzP@mgFmR51KZRKb2jcmJb276KQK9enC9cmG9v')
expect(isValid).to.be.true
})
})

describe('balance methods', function() {
Expand Down

0 comments on commit 073924f

Please sign in to comment.