bitcore-node-zcash/app/models/Address.js

200 lines
4.9 KiB
JavaScript
Raw Normal View History

2014-01-09 15:24:14 -08:00
'use strict';
2014-03-10 11:06:10 -07:00
var imports = require('soop').imports();
2014-03-05 18:03:56 -08:00
var async = require('async');
2014-05-07 05:45:44 -07:00
var bitcore = require('bitcore');
var BitcoreAddress = bitcore.Address;
var BitcoreTransaction = bitcore.Transaction;
var BitcoreUtil = bitcore.util;
2014-05-07 06:42:45 -07:00
var Parser = bitcore.BinaryParser;
2014-05-07 05:45:44 -07:00
var Buffer = bitcore.Buffer;
2014-03-10 11:06:10 -07:00
var TransactionDb = imports.TransactionDb || require('../../lib/TransactionDb').default();
2014-05-25 13:34:49 -07:00
var BlockDb = imports.BlockDb || require('../../lib/BlockDb').default();
var config = require('../../config/config');
2014-03-05 18:03:56 -08:00
var CONCURRENCY = 5;
function Address(addrStr) {
this.balanceSat = 0;
this.totalReceivedSat = 0;
this.totalSentSat = 0;
this.unconfirmedBalanceSat = 0;
this.txApperances = 0;
this.unconfirmedTxApperances= 0;
2014-05-25 13:34:49 -07:00
this.seen = {};
2014-03-05 18:03:56 -08:00
// TODO store only txids? +index? +all?
this.transactions = [];
var a = new BitcoreAddress(addrStr);
a.validate();
this.addrStr = addrStr;
Object.defineProperty(this, 'totalSent', {
get: function() {
return parseFloat(this.totalSentSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
this.totalSentSat = i * BitcoreUtil.COIN;
},
enumerable: 1,
});
Object.defineProperty(this, 'balance', {
get: function() {
return parseFloat(this.balanceSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
this.balance = i * BitcoreUtil.COIN;
},
enumerable: 1,
});
Object.defineProperty(this, 'totalReceived', {
get: function() {
return parseFloat(this.totalReceivedSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
this.totalReceived = i * BitcoreUtil.COIN;
},
enumerable: 1,
});
Object.defineProperty(this, 'unconfirmedBalance', {
get: function() {
return parseFloat(this.unconfirmedBalanceSat) / parseFloat(BitcoreUtil.COIN);
},
set: function(i) {
this.unconfirmedBalanceSat = i * BitcoreUtil.COIN;
},
enumerable: 1,
});
2014-03-05 18:03:56 -08:00
}
2014-03-05 18:03:56 -08:00
Address.prototype.getUtxo = function(next) {
var self = this;
2014-05-25 13:34:49 -07:00
var tDb = TransactionDb;
var bDb = BlockDb;
var ret;
if (!self.addrStr) return next(new Error('no error'));
2014-03-05 18:03:56 -08:00
2014-05-25 13:34:49 -07:00
tDb.fromAddr(self.addrStr, function(err,txOut){
2014-03-05 18:03:56 -08:00
if (err) return next(err);
2014-05-25 13:34:49 -07:00
var unspent = txOut.filter(function(x){
return !x.spentTxId;
});
2014-03-05 18:03:56 -08:00
2014-05-25 13:34:49 -07:00
bDb.fillConfirmations(unspent, function() {
tDb.fillScriptPubKey(unspent, function() {
ret = unspent.map(function(x){
return {
2014-03-05 18:03:56 -08:00
address: self.addrStr,
2014-05-25 13:34:49 -07:00
txid: x.txid,
vout: x.index,
ts: x.ts,
scriptPubKey: x.scriptPubKey,
amount: x.value_sat / BitcoreUtil.COIN,
confirmations: x.isConfirmedCached ? (config.safeConfirmations+'+') : x.confirmations,
};
});
return next(null, ret);
2014-03-05 18:03:56 -08:00
});
});
2014-03-05 18:03:56 -08:00
});
};
2014-05-25 13:34:49 -07:00
Address.prototype._addTxItem = function(txItem, notxlist) {
var add=0, addSpend=0;
var v = txItem.value_sat;
var seen = this.seen;
var txs = [];
if ( !seen[txItem.txid] ) {
if (!notxlist) {
txs.push({txid: txItem.txid, ts: txItem.ts});
}
seen[txItem.txid]=1;
add=1;
}
if (txItem.spentTxId && !seen[txItem.spentTxId] ) {
if (!notxlist) {
txs.push({txid: txItem.spentTxId, ts: txItem.spentTs});
}
seen[txItem.spentTxId]=1;
addSpend=1;
}
if (txItem.isConfirmed) {
this.txApperances += add;
this.totalReceivedSat += v;
if (! txItem.spentTxId ) {
//unspent
this.balanceSat += v;
}
else if(!txItem.spentIsConfirmed) {
// unspent
this.balanceSat += v;
this.unconfirmedBalanceSat -= v;
this.unconfirmedTxApperances += addSpend;
}
else {
// spent
this.totalSentSat += v;
this.txApperances += addSpend;
}
}
else {
this.unconfirmedBalanceSat += v;
this.unconfirmedTxApperances += add;
}
2014-05-26 12:58:44 -07:00
2014-05-25 13:34:49 -07:00
return txs;
};
Address.prototype._setTxs = function(txs) {
// sort input and outputs togheter
txs.sort(
function compare(a,b) {
if (a.ts < b.ts) return 1;
if (a.ts > b.ts) return -1;
return 0;
});
this.transactions = txs.map(function(i) { return i.txid; } );
};
2014-03-21 06:24:44 -07:00
Address.prototype.update = function(next, notxlist) {
2014-03-05 18:03:56 -08:00
var self = this;
if (!self.addrStr) return next();
var txs = [];
2014-05-25 13:34:49 -07:00
var tDb = TransactionDb;
var bDb = BlockDb;
tDb.fromAddr(self.addrStr, function(err,txOut){
if (err) return next(err);
2014-03-05 18:03:56 -08:00
2014-05-25 13:34:49 -07:00
bDb.fillConfirmations(txOut, function(err) {
if (err) return next(err);
tDb.cacheConfirmations(txOut, function(err) {
if (err) return next(err);
txOut.forEach(function(txItem){
txs=txs.concat(self._addTxItem(txItem, notxlist));
});
2014-05-25 13:34:49 -07:00
if (!notxlist)
self._setTxs(txs);
return next();
});
});
2014-03-05 18:03:56 -08:00
});
};
2014-03-05 18:03:56 -08:00
module.exports = require('soop')(Address);