insight-ui-zcash/app/models/Transaction.js

357 lines
9.0 KiB
JavaScript
Raw Normal View History

2014-01-07 11:49:42 -08:00
'use strict';
/**
* Module dependencies.
*/
2014-01-17 11:49:08 -08:00
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
async = require('async'),
RpcClient = require('bitcore/RpcClient').class(),
Transaction = require('bitcore/Transaction').class(),
Address = require('bitcore/Address').class(),
BitcoreBlock = require('bitcore/Block').class(),
networks = require('bitcore/networks'),
util = require('bitcore/util/util'),
bignum = require('bignum'),
config = require('../../config/config'),
sockets = require('../controllers/socket.js'),
TransactionItem = require('./TransactionItem');
2014-01-10 06:38:51 -08:00
2014-01-14 13:09:45 -08:00
var CONCURRENCY = 5;
2014-01-07 11:49:42 -08:00
2014-01-17 11:36:34 -08:00
// TODO: use bitcore networks module
var genesisTXID = '4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b';
2014-01-07 11:49:42 -08:00
/**
*/
var TransactionSchema = new Schema({
// For now we keep this as short as possible
// More fields will be propably added as we move
// forward with the UX
txid: {
2014-01-07 11:49:42 -08:00
type: String,
index: true,
unique: true,
},
2014-01-15 12:36:49 -08:00
/* TODO?
2014-01-10 16:42:39 -08:00
orphaned: {
type: Boolean,
default: false,
},
2014-01-15 12:36:49 -08:00
*/
2014-01-15 05:32:18 -08:00
time: Number,
2014-01-07 11:49:42 -08:00
});
/**
* Statics
*/
TransactionSchema.statics.load = function(id, cb) {
this.findOne({
_id: id
}).exec(cb);
};
2014-01-08 12:04:45 -08:00
TransactionSchema.statics.fromId = function(txid, cb) {
2014-01-07 11:49:42 -08:00
this.findOne({
txid: txid,
2014-01-07 11:49:42 -08:00
}).exec(cb);
};
2014-01-10 11:59:20 -08:00
2014-01-08 12:04:45 -08:00
TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
2014-01-13 12:21:42 -08:00
var That = this;
2014-01-08 12:04:45 -08:00
this.fromId(txid, function(err, tx) {
2014-01-08 11:29:39 -08:00
if (err) return cb(err);
2014-01-13 12:21:42 -08:00
if (!tx) {
// No in mongo...but maybe in bitcoind... lets query it
2014-01-13 12:21:42 -08:00
tx = new That();
tx.txid = txid;
2014-01-15 12:36:49 -08:00
tx.fillInfo(function(err, txInfo) {
2014-01-20 10:51:23 -08:00
if (err) return cb(err);
if (!txInfo) return cb();
2014-01-13 12:21:42 -08:00
tx.save(function(err) {
return cb(err,tx);
});
});
2014-01-13 12:21:42 -08:00
}
else {
2014-01-15 12:36:49 -08:00
tx.fillInfo(function(err) {
2014-01-13 12:21:42 -08:00
return cb(err,tx);
});
}
2014-01-08 11:29:39 -08:00
});
};
2014-01-16 05:54:59 -08:00
TransactionSchema.statics.createFromArray = function(txs, time, next) {
var that = this;
if (!txs) return next();
2014-01-14 11:56:02 -08:00
var mongo_txs = [];
2014-01-15 12:36:49 -08:00
async.forEachLimit(txs, CONCURRENCY, function(txid, cb) {
2014-01-17 11:49:08 -08:00
that.explodeTransactionItems( txid, time, function(err, addrs) {
2014-01-15 13:11:18 -08:00
if (err) return next(err);
2014-01-17 11:49:08 -08:00
if (addrs) {
async.each(addrs, function(addr){
sockets.broadcast_address_tx(addr, {'txid': txid});
});
}
2014-01-15 12:36:49 -08:00
2014-01-16 05:54:59 -08:00
that.create({txid: txid, time: time}, function(err, new_tx) {
2014-01-15 12:36:49 -08:00
if (err && ! err.toString().match(/E11000/)) return cb(err);
if (new_tx) {
mongo_txs.push(new_tx);
}
return cb();
});
2014-01-15 13:11:18 -08:00
});
2014-01-15 12:36:49 -08:00
},
function(err) {
return next(err, mongo_txs);
});
};
2014-01-16 05:54:59 -08:00
TransactionSchema.statics.explodeTransactionItems = function(txid, time, cb) {
2014-01-17 11:49:08 -08:00
var addrs = [];
2014-01-17 11:36:34 -08:00
// Is it from genesis block? (testnet==livenet)
// TODO: parse it from networks.genesisTX
if (txid === genesisTXID) return cb();
2014-01-15 12:36:49 -08:00
this.queryInfo(txid, function(err, info) {
if (err || !info) return cb(err);
2014-01-13 12:21:42 -08:00
var index = 0;
2014-01-15 12:36:49 -08:00
info.vin.forEach( function(i){
2014-01-13 12:21:42 -08:00
i.n = index++;
});
2014-01-15 12:36:49 -08:00
async.forEachLimit(info.vin, CONCURRENCY, function(i, next_in) {
if (i.addr && i.value) {
TransactionItem.create({
2014-01-15 12:36:49 -08:00
txid : txid,
value_sat : -1 * i.valueSat,
addr : i.addr,
index : i.n,
2014-01-16 05:54:59 -08:00
ts : time,
}, next_in);
2014-01-17 11:49:08 -08:00
if (addrs.indexOf(i.addr) === -1) {
addrs.push(i.addr);
}
}
else {
2014-01-11 22:06:14 -08:00
if ( !i.coinbase ) {
2014-01-18 13:28:24 -08:00
console.log ('WARN in TX: %s: could not parse INPUT %d', txid, i.n);
2014-01-11 22:06:14 -08:00
}
2014-01-13 12:21:42 -08:00
return next_in();
}
},
function (err) {
2014-01-16 11:11:55 -08:00
if (err && !err.message.match(/E11000/) ) console.log (err);
2014-01-15 12:36:49 -08:00
async.forEachLimit(info.vout, CONCURRENCY, function(o, next_out) {
/*
* TODO Support multisigs
*/
2014-01-13 12:21:42 -08:00
if (o.value && o.scriptPubKey && o.scriptPubKey.addresses && o.scriptPubKey.addresses[0]) {
TransactionItem.create({
2014-01-15 12:36:49 -08:00
txid : txid,
value_sat : o.valueSat,
addr : o.scriptPubKey.addresses[0], // TODO: only address 0?
index : o.n,
2014-01-16 05:54:59 -08:00
ts : time,
}, next_out);
if (addrs.indexOf(o.scriptPubKey.addresses[0]) === -1) {
addrs.push(o.scriptPubKey.addresses[0]);
}
}
else {
2014-01-18 13:28:24 -08:00
console.log ('WARN in TX: %s could not parse OUTPUT %d', txid, o.n);
2014-01-13 12:21:42 -08:00
return next_out();
}
},
function (err) {
2014-01-15 13:11:18 -08:00
if (err && ! err.toString().match(/E11000/)) return cb(err);
2014-01-17 11:49:08 -08:00
return cb(null, addrs);
});
});
});
};
2014-01-15 12:36:49 -08:00
TransactionSchema.statics.getOutpoints = function (tx, next) {
2014-01-09 13:18:47 -08:00
if (tx.isCoinBase()) return next();
2014-01-15 12:36:49 -08:00
var rpc = new RpcClient(config.bitcoind);
2014-01-11 22:06:14 -08:00
var network = ( config.network === 'testnet') ? networks.testnet : networks.livenet ;
2014-01-08 11:29:39 -08:00
2014-01-14 13:09:45 -08:00
async.forEachLimit(tx.ins, CONCURRENCY, function(i, cb) {
var outHash = i.getOutpointHash();
var outIndex = i.getOutpointIndex();
var outHashBase64 = outHash.reverse().toString('hex');
var c=0;
2014-01-15 12:36:49 -08:00
rpc.getRawTransaction(outHashBase64, function(err, txdata) {
var txin = new Transaction();
2014-01-09 13:18:47 -08:00
if (err || ! txdata.result) return cb( new Error('Input TX '+outHashBase64+' not found'));
var b = new Buffer(txdata.result,'hex');
txin.parse(b);
2014-01-11 22:06:14 -08:00
/*
*We have to parse it anyways. It will have outputs even it is a coinbase tx
if ( txin.isCoinBase() ) {
return cb();
}
*/
2014-01-09 13:18:47 -08:00
txin.outs.forEach( function(j) {
// console.log( c + ': ' + util.formatValue(j.v) );
if (c === outIndex) {
i.value = j.v;
2014-01-11 22:06:14 -08:00
// This is used for pay-to-pubkey transaction in which
// the pubkey is not provided on the input
var scriptPubKey = j.getScript();
var hash = scriptPubKey.simpleOutHash();
if (hash) {
var addr = new Address(network.addressPubkey, hash);
i.addrFromOutput = addr.toString();
}
}
c++;
});
return cb();
});
},
function(err) {
return next(err);
}
);
};
2014-01-08 11:29:39 -08:00
2014-01-15 12:36:49 -08:00
TransactionSchema.statics.queryInfo = function(txid, cb) {
var that = this;
var network = ( config.network === 'testnet') ? networks.testnet : networks.livenet ;
2014-01-15 12:36:49 -08:00
var rpc = new RpcClient(config.bitcoind);
2014-01-15 12:36:49 -08:00
rpc.getRawTransaction(txid, 1, function(err, txInfo) {
2014-01-20 10:51:23 -08:00
// Not found?
if (err && err.code === -5) return cb();
2014-01-15 12:36:49 -08:00
if (err) return cb(err);
2014-01-15 12:36:49 -08:00
var info = txInfo.result;
2014-01-08 11:29:39 -08:00
// Transaction parsing
var b = new Buffer(txInfo.result.hex,'hex');
var tx = new Transaction();
tx.parse(b);
2014-01-15 12:36:49 -08:00
that.getOutpoints(tx, function(err) {
if (err) return cb(err);
// Copy TX relevant values to .info
var c = 0;
var valueIn = bignum(0);
var valueOut = bignum(0);
2014-01-09 13:18:47 -08:00
if ( tx.isCoinBase() ) {
2014-01-15 12:36:49 -08:00
info.isCoinBase = true;
2014-01-09 13:18:47 -08:00
}
else {
tx.ins.forEach(function(i) {
2014-01-10 16:42:39 -08:00
if (i.value) {
2014-01-15 12:36:49 -08:00
info.vin[c].value = util.formatValue(i.value);
2014-01-10 16:42:39 -08:00
var n = util.valueToBigInt(i.value).toNumber();
2014-01-15 12:36:49 -08:00
info.vin[c].valueSat = n;
2014-01-10 16:42:39 -08:00
valueIn = valueIn.add( n );
var scriptSig = i.getScript();
var pubKey = scriptSig.simpleInPubKey();
// We check for pubKey in case a broken / strange TX.
if (pubKey) {
var pubKeyHash = util.sha256ripe160(pubKey);
var addr = new Address(network.addressPubkey, pubKeyHash);
var addrStr = addr.toString();
2014-01-15 12:36:49 -08:00
info.vin[c].addr = addrStr;
2014-01-10 16:42:39 -08:00
}
2014-01-11 22:06:14 -08:00
else {
2014-01-13 12:21:42 -08:00
if (i.addrFromOutput)
2014-01-15 12:36:49 -08:00
info.vin[c].addr = i.addrFromOutput;
2014-01-11 22:06:14 -08:00
}
2014-01-10 16:42:39 -08:00
}
else {
2014-01-13 12:21:42 -08:00
console.log('TX could not be parsed: %s,%d' ,txInfo.result.txid, c);
2014-01-10 10:51:19 -08:00
}
2014-01-09 13:18:47 -08:00
c++;
});
}
2014-01-13 12:21:42 -08:00
c=0;
tx.outs.forEach( function(i) {
var n = util.valueToBigInt(i.v).toNumber();
valueOut = valueOut.add(n);
2014-01-15 12:36:49 -08:00
info.vout[c].valueSat = n;
c++;
});
2014-01-15 12:36:49 -08:00
info.valueOut = valueOut / util.COIN;
2014-01-09 13:35:14 -08:00
if ( !tx.isCoinBase() ) {
2014-01-15 12:36:49 -08:00
info.valueIn = valueIn / util.COIN;
info.feeds = (valueIn - valueOut) / util.COIN;
2014-01-09 13:35:14 -08:00
}
2014-01-14 08:03:29 -08:00
else {
2014-01-15 12:36:49 -08:00
var reward = BitcoreBlock.getBlockValue(info.height) / util.COIN;
info.vin[0].reward = reward;
info.valueIn = reward;
2014-01-14 08:03:29 -08:00
}
2014-01-15 12:36:49 -08:00
info.size = b.length;
2014-01-15 12:36:49 -08:00
return cb(null, info);
});
});
};
2014-01-14 08:03:29 -08:00
2014-01-15 12:36:49 -08:00
TransactionSchema.methods.fillInfo = function(next) {
var that = this;
mongoose.model('Transaction', TransactionSchema).queryInfo(that.txid, function(err, info) {
if (err) return next(err);
that.info = info;
that.info.time = that.time;
2014-01-15 12:36:49 -08:00
return next();
2014-01-08 11:29:39 -08:00
});
};
2014-01-07 11:49:42 -08:00
module.exports = mongoose.model('Transaction', TransactionSchema);