api working WIP
This commit is contained in:
parent
5a97c96d6e
commit
868f22cf2b
|
@ -40,11 +40,11 @@ module.exports = function(grunt) {
|
|||
livereload: true
|
||||
}
|
||||
},
|
||||
test: {
|
||||
// we monitor only app/models/* because we have test for models only now
|
||||
files: ['test/**/*.js', 'test/*.js','app/models/*.js'],
|
||||
tasks: ['test'],
|
||||
}
|
||||
// test: {
|
||||
// files: ['test/**/*.js', 'test/*.js','app/models/*.js'],
|
||||
// tasks: ['test'],
|
||||
// }
|
||||
},
|
||||
jshint: {
|
||||
all: {
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
'use strict';
|
||||
|
||||
/**
|
||||
* Module dependencies.
|
||||
*/
|
||||
|
||||
var Address = require('../models/Address');
|
||||
|
||||
|
||||
/**
|
||||
* Find block by hash ...
|
||||
*/
|
||||
exports.address = function(req, res, next, addr) {
|
||||
var a = Address.new(addr);
|
||||
|
||||
a.update(function(err) {
|
||||
if (err && !a.totalReceivedSat) {
|
||||
console.log(err);
|
||||
res.status(404).send('Invalid address');
|
||||
return next();
|
||||
}
|
||||
|
||||
req.address = a;
|
||||
return next();
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* Show block
|
||||
*/
|
||||
exports.show = function(req, res) {
|
||||
if (req.address) {
|
||||
res.jsonp(req.address);
|
||||
}
|
||||
};
|
||||
|
|
@ -14,10 +14,14 @@ var mongoose = require('mongoose'),
|
|||
*/
|
||||
exports.block = function(req, res, next, hash) {
|
||||
Block.fromHashWithInfo(hash, function(err, block) {
|
||||
if (err) return next(err);
|
||||
if (!block) return next(new Error('Failed to load block ' + hash));
|
||||
if (err && !block) {
|
||||
console.log(err);
|
||||
res.status(404).send('Not found');
|
||||
return next();
|
||||
}
|
||||
|
||||
req.block = block.info;
|
||||
next();
|
||||
return next();
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -26,7 +30,9 @@ exports.block = function(req, res, next, hash) {
|
|||
* Show block
|
||||
*/
|
||||
exports.show = function(req, res) {
|
||||
if (req.block) {
|
||||
res.jsonp(req.block);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -4,16 +4,12 @@ require('classtool');
|
|||
|
||||
|
||||
function spec() {
|
||||
var util = require('util');
|
||||
var RpcClient = require('bitcore/RpcClient').class();
|
||||
var networks = require('bitcore/networks');
|
||||
var async = require('async');
|
||||
var Transaction = require('./Transaction');
|
||||
var TransactionItem = require('./TransactionItem');
|
||||
var config = require('../../config/config');
|
||||
var BitcoreAddress = require('bitcore/Address').class();
|
||||
var BitcoreUtil = require('bitcore/util/util');
|
||||
|
||||
function Address(addrStr,cb) {
|
||||
this.addrStr = addrStr;
|
||||
function Address(addrStr) {
|
||||
this.balanceSat = 0;
|
||||
this.totalReceivedSat = 0;
|
||||
this.totalSentSat = 0;
|
||||
|
@ -21,10 +17,30 @@ function spec() {
|
|||
|
||||
// TODO store only txids? +index? +all?
|
||||
this.transactions = [];
|
||||
|
||||
var a = new BitcoreAddress(addrStr);
|
||||
try {
|
||||
a.validate();
|
||||
this.addrStr = addrStr;
|
||||
} catch(e){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Address.prototype.__defineGetter__('balance', function(){
|
||||
|
||||
console.log('#################### '+this.balanceSat);
|
||||
|
||||
|
||||
return this.balanceSat / BitcoreUtil.COIN;
|
||||
});
|
||||
|
||||
Address.prototype.update = function(next) {
|
||||
|
||||
if (! this.addrStr) {
|
||||
return next(new Error('Invalid or undefined address string'));
|
||||
}
|
||||
|
||||
var that = this;
|
||||
async.series([
|
||||
// TODO TXout!
|
||||
|
@ -35,7 +51,7 @@ function spec() {
|
|||
|
||||
txItems.forEach(function(txItem){
|
||||
|
||||
console.log(txItem.txid + ' : ' + txItem.value_sat);
|
||||
// console.log(txItem.txid + ' : ' + txItem.value_sat);
|
||||
that.txApperances +=1;
|
||||
that.balanceSat += txItem.value_sat;
|
||||
|
||||
|
@ -47,12 +63,12 @@ console.log(txItem.txid + ' : ' + txItem.value_sat);
|
|||
that.totalReceivedSat += Math.abs(txItem.value_sat);
|
||||
});
|
||||
return cb();
|
||||
})
|
||||
});
|
||||
}
|
||||
], function (err) {
|
||||
return next(err);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
return Address;
|
||||
}
|
||||
|
|
|
@ -58,32 +58,31 @@ TransactionSchema.statics.fromId = function(txid, cb) {
|
|||
|
||||
|
||||
TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
|
||||
var that = this;
|
||||
var That = this;
|
||||
|
||||
this.fromId(txid, function(err, tx) {
|
||||
if (err) return cb(err);
|
||||
|
||||
if (!tx) {
|
||||
|
||||
return cb(new Error('TX not found'));
|
||||
|
||||
// No in mongo...but maybe in bitcoind... lets query it
|
||||
/* var tx = new that();
|
||||
tx = new That();
|
||||
|
||||
tx.txid = txid;
|
||||
|
||||
tx.queryInfo(function(err, txInfo) {
|
||||
|
||||
if (!txInfo) return cb(new Error('TX not found'));
|
||||
if (!txInfo)
|
||||
return cb(new Error('TX not found1'));
|
||||
|
||||
tx.save(function(err) {
|
||||
console.log('asdadsads');
|
||||
return cb(err,tx);
|
||||
});
|
||||
});
|
||||
*/ }
|
||||
|
||||
tx.queryInfo(function(err) { return cb(err,tx); } );
|
||||
}
|
||||
else {
|
||||
tx.queryInfo(function(err) {
|
||||
return cb(err,tx);
|
||||
});
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -113,8 +112,10 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
|
|||
this.fromIdWithInfo(txid, function(err, t) {
|
||||
if (err || !t) return cb(err);
|
||||
|
||||
var index=0;
|
||||
t.info.vin.forEach(function(i){ i.n = index++});
|
||||
var index = 0;
|
||||
t.info.vin.forEach( function(i){
|
||||
i.n = index++;
|
||||
});
|
||||
|
||||
async.each(t.info.vin, function(i, next_in) {
|
||||
if (i.addr && i.value) {
|
||||
|
@ -130,7 +131,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
|
|||
}
|
||||
else {
|
||||
if ( !i.coinbase ) {
|
||||
console.log ("TX: %s,%d could not parse INPUT", t.txid, i.n);
|
||||
console.log ('TX: %s,%d could not parse INPUT', t.txid, i.n);
|
||||
}
|
||||
return next_in();
|
||||
}
|
||||
|
@ -142,10 +143,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
|
|||
/*
|
||||
* TODO Support multisigs
|
||||
*/
|
||||
if (o.value && o.scriptPubKey
|
||||
&& o.scriptPubKey.addresses
|
||||
&& o.scriptPubKey.addresses[0]
|
||||
) {
|
||||
if (o.value && o.scriptPubKey && o.scriptPubKey.addresses && o.scriptPubKey.addresses[0]) {
|
||||
//console.log("Creating OUT %s %d", o.scriptPubKey.addresses[0], o.valueSat);
|
||||
TransactionItem.create({
|
||||
txid : t.txid,
|
||||
|
@ -156,7 +154,7 @@ TransactionSchema.statics.explodeTransactionItems = function(txid, cb) {
|
|||
}, next_out);
|
||||
}
|
||||
else {
|
||||
console.log ("TX: %s,%d could not parse OUTPUT. Skipping... ", t.txid, o.n);
|
||||
console.log ('TX: %s,%d could not parse OUTPUT', t.txid, o.n);
|
||||
return next_out();
|
||||
}
|
||||
},
|
||||
|
@ -206,7 +204,6 @@ TransactionSchema.methods.fillInputValues = function (tx, next) {
|
|||
// This is used for pay-to-pubkey transaction in which
|
||||
// the pubkey is not provided on the input
|
||||
var scriptPubKey = j.getScript();
|
||||
var txType = scriptPubKey.classify();
|
||||
var hash = scriptPubKey.simpleOutHash();
|
||||
if (hash) {
|
||||
var addr = new Address(network.addressPubkey, hash);
|
||||
|
@ -278,13 +275,13 @@ TransactionSchema.methods.queryInfo = function (next) {
|
|||
}
|
||||
}
|
||||
else {
|
||||
console.log("TX could not be parsed: %s,%d",txInfo.result.txid, c);
|
||||
console.log('TX could not be parsed: %s,%d' ,txInfo.result.txid, c);
|
||||
}
|
||||
c++;
|
||||
});
|
||||
}
|
||||
|
||||
var c = 0;
|
||||
c=0;
|
||||
tx.outs.forEach( function(i) {
|
||||
var n = util.valueToBigInt(i.v).toNumber();
|
||||
valueOut = valueOut.add(n);
|
||||
|
|
|
@ -41,7 +41,7 @@ TransactionItemSchema.statics.fromTxId = function(txid, cb) {
|
|||
var sa= a.value_sat < 0 ? -1 : 1;
|
||||
var sb= b.value_sat < 0 ? -1 : 1;
|
||||
|
||||
if (sa != sb) {
|
||||
if (sa !== sb) {
|
||||
return sa-sb;
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -6,7 +6,7 @@ module.exports = {
|
|||
name: "Mystery - Development"
|
||||
},
|
||||
bitcoind: {
|
||||
protocol: process.env.BITCOIND_USER || 'http',
|
||||
protocol: process.env.BITCOIND_PROTO || 'http',
|
||||
user: process.env.BITCOIND_USER || 'mystery',
|
||||
pass: process.env.BITCOIND_PASS || 'real_mystery',
|
||||
host: process.env.BITCOIND_HOST || '127.0.0.1',
|
||||
|
|
|
@ -7,9 +7,9 @@ module.exports = {
|
|||
},
|
||||
port: '3301',
|
||||
bitcoind: {
|
||||
user: 'mystery',
|
||||
pass: 'real_mystery',
|
||||
protocol: 'http',
|
||||
protocol: process.env.BITCOIND_PROTO || 'http',
|
||||
user: process.env.BITCOIND_USER || 'mystery',
|
||||
pass: process.env.BITCOIND_PASS || 'real_mystery',
|
||||
host: process.env.BITCOIND_HOST || '127.0.0.1',
|
||||
port: process.env.BITCOIND_PORT || '18332',
|
||||
},
|
||||
|
|
|
@ -19,4 +19,8 @@ module.exports = function(app) {
|
|||
|
||||
app.param('txid', transactions.transaction);
|
||||
|
||||
var addresses = require('../app/controllers/addresses');
|
||||
app.get('/api/addr/:addr', addresses.show);
|
||||
app.param('addr', addresses.address);
|
||||
|
||||
};
|
||||
|
|
|
@ -6,7 +6,7 @@ var
|
|||
assert = require('assert'),
|
||||
fs = require('fs'),
|
||||
config = require('../../config/config'),
|
||||
Address = require('../../app/models/Address');
|
||||
Address = require('../../app/models/Address').class();
|
||||
mongoose= require('mongoose'),
|
||||
addrValid = JSON.parse(fs.readFileSync('test/model/addr.json'));
|
||||
|
||||
|
@ -30,8 +30,7 @@ describe('Address update', function(){
|
|||
it('should retrieve the correct info for:' + v.addr, function(done) {
|
||||
this.timeout(5000);
|
||||
|
||||
var a = Address.new(v.addr);
|
||||
|
||||
var a = new Address(v.addr);
|
||||
|
||||
a.update(function(err) {
|
||||
if (err) done(err);
|
||||
|
|
|
@ -1,10 +1,8 @@
|
|||
[
|
||||
{
|
||||
"disabled":1,
|
||||
"addr": "mjRmkmYzvZN3cA3aBKJgYJ65epn3WCG84H"
|
||||
},
|
||||
{
|
||||
"disabled":1,
|
||||
"addr": "mp3Rzxx9s1A21SY3sjJ3CQoa2Xjph7e5eS",
|
||||
"balance": 0,
|
||||
"totalReceived": 50,
|
||||
|
@ -16,6 +14,7 @@
|
|||
"balance": 43.1
|
||||
},
|
||||
{
|
||||
"disabled":1,
|
||||
"addr": "mzW2hdZN2um7WBvTDerdahKqRgj3md9C29",
|
||||
"balance": 910.39522682,
|
||||
"totalReceived": 910.39522682,
|
||||
|
|
Loading…
Reference in New Issue