Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Cash addr #529

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions app/controllers/addresses.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

var _ = require('lodash');
var Address = require('../models/Address');
var AddressTranslator = require('../../lib/AddressTranslator');
var common = require('./common');
var async = require('async');

Expand All @@ -30,6 +31,7 @@ var getAddr = function(req, res, next) {
var a;
try {
var addr = req.param('addr');
addr = AddressTranslator.translate(addr, 'btc', 'bch');
a = new Address(addr);
} catch (e) {
common.handleErrors({
Expand All @@ -47,10 +49,9 @@ var getAddrs = function(req, res, next) {
var addrStrs = req.param('addrs');
var s = addrStrs.split(',');
if (s.length === 0) return as;
for (var i = 0; i < s.length; i++) {
var a = new Address(s[i]);
as.push(a);
}
as = _.map(s,function(x) {
return new Address(AddressTranslator.translate(s[i], 'btc', 'bch'));
});
} catch (e) {
common.handleErrors({
message: 'Invalid addrs param:' + e.message,
Expand Down
13 changes: 12 additions & 1 deletion app/controllers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var bitcore = require('bitcore');
var RpcClient = bitcore.RpcClient;
var config = require('../../config/config');
var bitcoreRpc = imports.bitcoreRpc || new RpcClient(config.bitcoind);
var AddressTranslator = require('../../lib/AddressTranslator');

var tDb = require('../../lib/TransactionDb').default();
var bdb = require('../../lib/BlockDb').default();
Expand Down Expand Up @@ -156,8 +157,18 @@ exports.list = function(req, res, next) {
});
}
else if (addrStr) {
var a = new Address(addrStr);

try {
addrStr = AddressTranslator.translate(addrStr, 'btc', 'bch');
} catch (e) {
common.handleErrors({
message: 'Invalid address:' + e.message,
code: 1
}, res, next);
return null;
};

var a = new Address(addrStr);
a.update(function(err) {
if (err && !a.totalReceivedSat) {
console.log(err);
Expand Down
37 changes: 37 additions & 0 deletions lib/AddressTranslator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
var Bitcore_ = {
btc: require('bitcore-lib'),
bch: require('bitcore-lib-cash')
};

var _ = require('lodash');

function AddressTranslator() {
};


AddressTranslator.getAddressCoin = function(address) {
try {
new Bitcore_['btc'].Address(address);
return 'btc';
} catch (e) {
try {
new Bitcore_['bch'].Address(address);
return 'bch';
} catch (e) {
return;
}
}
};


AddressTranslator.translate = function(address, coin, origCoin) {
origCoin = origCoin || AddressTranslator.getAddressCoin(address);
var origAddress = new Bitcore_[origCoin].Address(address);
var origObj = origAddress.toObject();

var result = Bitcore_[coin].Address.fromObject(origObj)
return result.toString();
};


module.exports = AddressTranslator;
2 changes: 1 addition & 1 deletion lib/PeerSync.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ PeerSync.prototype.handleBlock = function(info) {
if (self.shouldBroadcast) {
sockets.broadcastBlock(blockHash);
// broadcasting address here is a bad idea. listening to new block
// should be enoght
// should be enough
}
}
});
Expand Down
55 changes: 32 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,32 @@
"description": "An open-source bitcoin blockchain API. The Insight API provides you with a convenient, powerful and simple way to query and broadcast data on the bitcoin network and build your own services with it.",
"version": "0.2.16",
"repository": "git://github.com/bitpay/insight-api.git",
"contributors": [{
"name": "Matias Alejo Garcia",
"email": "[email protected]"
}, {
"name": "Manuel Araoz",
"email": "[email protected]"
}, {
"name": "Mario Colque",
"email": "[email protected]"
}, {
"name": "Gustavo Cortez",
"email": "[email protected]"
}, {
"name": "Juan Ignacio Sosa Lopez",
"email": "[email protected]"
}, {
"name": "Ivan Socolsky",
"email": "[email protected]"
}],
"contributors": [
{
"name": "Matias Alejo Garcia",
"email": "[email protected]"
},
{
"name": "Manuel Araoz",
"email": "[email protected]"
},
{
"name": "Mario Colque",
"email": "[email protected]"
},
{
"name": "Gustavo Cortez",
"email": "[email protected]"
},
{
"name": "Juan Ignacio Sosa Lopez",
"email": "[email protected]"
},
{
"name": "Ivan Socolsky",
"email": "[email protected]"
}
],
"bugs": {
"url": "https://github.com/bitpay/insight-api/issues"
},
Expand All @@ -47,8 +54,9 @@
"async": "*",
"base58-native": "0.1.2",
"bignum": "*",
"morgan": "*",
"bitcore": "git://github.com/bitpay/bitcore.git#aa41c70cff2583d810664c073a324376c39c8b36",
"bitcore-lib": "^0.15.0",
"bitcore-lib-cash": "^0.15.1",
"bufferput": "git://github.com/bitpay/node-bufferput.git",
"buffertools": "*",
"commander": "^2.3.0",
Expand All @@ -58,19 +66,20 @@
"glob": "*",
"leveldown": "~0.10.0",
"levelup": "~0.19.0",
"lodash": "^2.4.1",
"lodash": "^2.4.2",
"microtime": "^0.6.0",
"mkdirp": "^0.5.0",
"moment": "~2.5.0",
"morgan": "*",
"nodemailer": "^1.3.0",
"nodemailer-smtp-transport": "*",
"preconditions": "^1.0.7",
"request": "^2.48.0",
"socket.io": "1.0.6",
"socket.io-client": "1.0.6",
"soop": "=0.1.5",
"winston": "*",
"xmlhttprequest": "~1.6.0",
"nodemailer-smtp-transport": "*"
"xmlhttprequest": "~1.6.0"
},
"devDependencies": {
"chai": "*",
Expand Down
40 changes: 40 additions & 0 deletions test/AddressTranslator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

var _ = require('lodash');
var chai = require('chai');
var sinon = require('sinon');
var assert = require('assert');
var should = chai.should;

var AddressTranslator = require('../lib/AddressTranslator');

describe('#AddressTranslator', function() {
it('should translate address from btc to bch', function() {
var res = AddressTranslator.translate('1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA', 'bch');
assert( res == 'CcJ4qUfyQ8x5NwhAeCQkrBSWVeXxXghcNz');
});
it('should translate address from bch to btc', function() {
var res = AddressTranslator.translate('HBf8isgS8EXG1r3X6GP89FmooUmiJ42wHS', 'btc');
assert(res=='36q2G5FMGvJbPgAVEaiyAsFGmpkhPKwk2r');
});

it('should keep the address if there is nothing to do (bch)', function() {
var res = AddressTranslator.translate('CcJ4qUfyQ8x5NwhAeCQkrBSWVeXxXghcNz', 'bch');
assert(res=='CcJ4qUfyQ8x5NwhAeCQkrBSWVeXxXghcNz');
});
it('should keep the address if there is nothing to do (btc)', function() {
var res = AddressTranslator.translate('1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA', 'btc');
assert(res=='1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA');
});
it('should support 3 params NOK', function() {
(function() {
var res = AddressTranslator.translate('1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA', 'btc', 'bch');
}).should.throw('Address has mismatched network type.');
});
it('should support 3 params OK', function() {
var res = AddressTranslator.translate('HBf8isgS8EXG1r3X6GP89FmooUmiJ42wHS', 'btc', 'bch');
assert(res=='36q2G5FMGvJbPgAVEaiyAsFGmpkhPKwk2r');
});

});