Skip to content
Snippets Groups Projects
Commit afec1e6d authored by Cédric Moreau's avatar Cédric Moreau
Browse files

Added /tx/sources to list available coins for a pubkey

parent 1fbc9bde
Branches
Tags
No related merge requests found
......@@ -23,14 +23,11 @@ function TransactionBinding(txServer) {
var conf = txServer.conf;
// Services
var http = txServer.HTTPService;
var ParametersService = txServer.ParametersService;
var PeeringService = txServer.PeeringService;
var BlockchainService = txServer.BlockchainService;
// Models
var Peer = txServer.conn.model('Peer');
var Membership = txServer.conn.model('Membership');
var Source = txServer.conn.model('Source');
this.parseTransaction = function (req, res) {
var onError = http400(res);
......@@ -47,5 +44,35 @@ function TransactionBinding(txServer) {
.pipe(res);
}
this.getSources = function (req, res) {
var pubkey = "";
async.waterfall([
function (next) {
ParametersService.getPubkey(req, next);
},
function (pPubkey, next) {
pubkey = pPubkey;
Source.getAvailableByPubkey(pubkey, next);
},
function (sources, next) {
var result = {
"currency": conf.currency,
"pubkey": pubkey,
"sources": []
};
sources.forEach(function (src) {
result.sources.push(src.json());
});
next(null, result);
}
], function (err, result) {
if (err) {
res.send(500, err);
} else {
res.send(200, JSON.stringify(result, null, " "));
}
});
}
return this;
}
......@@ -20,8 +20,25 @@ SourceSchema.pre('save', function (next) {
});
SourceSchema.methods = {
json: function () {
return {
"pubkey": this.pubkey,
"type": this.type,
"number": this.number,
"fingerprint": this.fingerprint,
"amount": this.amount
};
}
};
SourceSchema.statics.getAvailableByPubkey = function (pubkey, done) {
var Source = this.model('Source');
Source
.find({ "pubkey": pubkey, "consumed": false })
.exec(done);
}
SourceSchema.statics.existsNotConsumed = function (type, pubkey, number, fingerprint, amount, done) {
var Source = this.model('Source');
Source
......
var async = require('async');
var status = require('../models/statusMessage');
var parsers = require('../lib/streams/parsers/doc');
var constants = require('../lib/constants');
module.exports.get = function (conn, currencyName) {
return new ParameterNamespace(conn, currencyName);
......@@ -21,6 +22,19 @@ function ParameterNamespace (conn, currency) {
callback(null, req.params.search);
};
this.getPubkey = function (req, callback){
if(!req.params.pubkey){
callback('Parameter `pubkey` is required');
return;
}
var matches = req.params.pubkey.match(constants.PUBLIC_KEY);
if(!matches){
callback("Pubkey format is incorrect, must be a Base58 string");
return;
}
callback(null, matches[0]);
};
this.getFingerprint = function (req, callback){
if(!req.params.fpr){
callback("Fingerprint is required");
......
......@@ -27,6 +27,7 @@
* [peering/status](#networkpeeringstatus)
* [tx/](#tx)
* [process](#txprocess)
* [sources/[pubkey]](#txsourcespubkey)
## Overview
......@@ -45,7 +46,8 @@ Data is made accessible through an HTTP API mainly inspired from [OpenUDC_exchan
| |-- status
| `-- peers
`-- tx/
`-- process
|-- process
`-- sources
## Merkle URLs
......@@ -563,11 +565,10 @@ POST a transaction.
Name | Value | Method
----------------- | ------------------------------------------------------------- | ------
`transaction` | The raw transaction. | POST
`signature` | The signature of the `transaction`. | POST
**Returns**
The recorded transaction and its signature.
The recorded transaction.
```json
{
"raw": "Version: 1\r\n...\r\n",
......@@ -600,3 +601,46 @@ The recorded transaction and its signature.
}
}
```
#### `tx/sources/[pubkey]`
**Goal**
GET a list of available sources.
**Parameters**
Name | Value | Method
---- | ----- | ------
`pubkey` | Owner of the coins' pubkey. | URL
**Returns**
A list of available sources for the given `pubkey`.
```json
{
"currency": "beta_brousouf",
"pubkey": "HsLShAtzXTVxeUtQd7yi5Z5Zh4zNvbu8sTEZ53nfKcqY",
"sources": [
{
"type: "D",
"number": 5,
"fingerprint": "6C20752F6AD06AEA8D0BB46BB8C4F68641A34C79",
"amount": 100
},
{
"type: "D",
"number": 18,
"fingerprint": "DB7D88E795E42CF8CFBFAAFC77379E97847F9B42",
"amount": 110
},
{
"type: "T",
"number": 55,
"fingerprint": "E614E814179F313B1113475E6319EF4A3D470AD0",
"amount": 30
}
]
}
```
\ No newline at end of file
......@@ -44,6 +44,7 @@ function TxServer (dbConf, overrideConf, interceptors, onInit) {
this.listenTX = function (app) {
var transactions = require('./app/controllers/transactions')(that);
app.post('/tx/process', transactions.parseTransaction);
app.get( '/tx/sources/:pubkey', transactions.getSources);
};
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment