Merge pull request #768 from matiu/feat/cashaddr-in-addrtranslator

Feat/cashaddr in addrtranslator
This commit is contained in:
Matias Alejo Garcia 2018-03-07 06:05:09 -03:00 committed by GitHub
commit 1f46848b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 147 additions and 19 deletions

View File

@ -46,7 +46,6 @@ var config = {
testnet: {
provider: 'insight',
url: 'https://test-insight.bitpay.com:443',
// url: 'http://localhost:3001',
// Multiple servers (in priority order)
// url: ['http://a.b.c', 'https://test-insight.bitpay.com:443'],
},
@ -54,7 +53,9 @@ var config = {
bch: {
livenet: {
provider: 'insight',
url: 'https://cashexplorer.bitcoin.com',
//url: 'https://cashexplorer.bitcoin.com',
url: 'http://localhost:2001',
translateAddresses: false,
},
},
},

View File

@ -32,7 +32,11 @@ AddressTranslator.translate = function(addresses, coin, origCoin) {
origCoin = origCoin || AddressTranslator.getAddressCoin(addresses[0]);
var ret = _.map(addresses, function(x) {
var orig = new Bitcore_[origCoin].Address(x).toObject();
return Bitcore_[coin].Address.fromObject(orig).toString();
if (coin == 'bch') {
return Bitcore_[coin].Address.fromObject(orig).toCashAddress(true);
} else {
return Bitcore_[coin].Address.fromObject(orig).toString();
}
});
if (wasArray)

14
package-lock.json generated
View File

@ -247,9 +247,9 @@
}
},
"bitcore-lib-cash": {
"version": "0.16.1",
"resolved": "https://registry.npmjs.org/bitcore-lib-cash/-/bitcore-lib-cash-0.16.1.tgz",
"integrity": "sha512-RcOZCeyHBCTREHytQ7SwvmkSHJ3a/FZHQ6BIQwstQqpbMkEjGACHUfg8CAwvonLJ4soxcyuuRuensgtLI7MM8g==",
"version": "0.17.0",
"resolved": "https://registry.npmjs.org/bitcore-lib-cash/-/bitcore-lib-cash-0.17.0.tgz",
"integrity": "sha512-uJVW6DPVR12p/uhxOCA0cIA/qrDXa5OYyQyZ9kFvJ/YjoXc8auljdo4cmAC89zF3lvikQebbjGqBYUY5Ej3tIg==",
"requires": {
"bn.js": "4.11.8",
"bs58": "4.0.1",
@ -2444,7 +2444,7 @@
"resolved": "https://registry.npmjs.org/phantomjs-prebuilt/-/phantomjs-prebuilt-2.1.16.tgz",
"integrity": "sha1-79ISpKOWbTZHaE6ouniFSb4q7+8=",
"requires": {
"es6-promise": "4.2.2",
"es6-promise": "4.2.4",
"extract-zip": "1.6.6",
"fs-extra": "1.0.0",
"hasha": "2.2.0",
@ -2456,9 +2456,9 @@
},
"dependencies": {
"es6-promise": {
"version": "4.2.2",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.2.tgz",
"integrity": "sha512-LSas5vsuA6Q4nEdf9wokY5/AJYXry98i0IzXsv49rYsgDGDNDPbqAYR1Pe23iFxygfbGZNR/5VrHXBCh2BhvUQ=="
"version": "4.2.4",
"resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-4.2.4.tgz",
"integrity": "sha512-/NdNZVJg+uZgtm9eS3O6lrOLYmQag2DjdEXuPaHlZ6RuVqgqaVZfgYCepEIKsLqwdQArOPtC3XzRLqGGfT8KQQ=="
}
}
},

View File

@ -22,7 +22,7 @@
"dependencies": {
"async": "^0.9.2",
"bitcore-lib": "^0.15.0",
"bitcore-lib-cash": "^0.16.1",
"bitcore-lib-cash": "^0.17.0",
"body-parser": "^1.11.0",
"compression": "^1.6.2",
"coveralls": "^2.11.2",

View File

@ -1,5 +1,6 @@
a='bd0e9eed-4712-42e5-bb21-193cde4c9e21';
// json support
a='f42c8c47-0f7e-4cb0-9056-6c50bb821d7d';
b= {'walletId':a};
db.addresses.remove(b);

View File

@ -10,7 +10,7 @@ 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');
assert( res == 'qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4gms8s0u59');
});
it('should translate address from bch to btc', function() {
var res = AddressTranslator.translateInput('HBf8isgS8EXG1r3X6GP89FmooUmiJ42wHS');
@ -19,7 +19,7 @@ describe('#AddressTranslator', function() {
it('should keep the address if there is nothing to do (bch)', function() {
var res = AddressTranslator.translate('CcJ4qUfyQ8x5NwhAeCQkrBSWVeXxXghcNz', 'bch');
assert(res=='CcJ4qUfyQ8x5NwhAeCQkrBSWVeXxXghcNz');
assert(res=='qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4gms8s0u59');
});
it('should keep the address if there is nothing to do (btc)', function() {
var res = AddressTranslator.translate('1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA', 'btc');
@ -42,9 +42,9 @@ describe('#AddressTranslator', function() {
it('should work with arrays also', function() {
var res = AddressTranslator.translateOutput(['1LqBGSKuX5yYUonjxT5qGfpUsXKYYWeabA', '37YHiaQnMjy73GS1UpiE8p2Ju6MyrrDw3J', '1DuPdCpGzVX73kBYaAbu5XDNDgE2Lza5Ed']);
assert(res[0] == 'CcJ4qUfyQ8x5NwhAeCQkrBSWVeXxXghcNz');
assert(res[1] == 'HCNQBNqsD4BmfSK3LWNP7CYqvkNznSrXS3');
assert(res[2] == 'CVNHCFALsYVdwt5yFuvpf2qPqoSSGtvY7t');
assert(res[0] == 'qrvcdmgpk73zyfd8pmdl9wnuld36zh9n4gms8s0u59');
assert(res[1] == 'ppqz5v08kssnuupe0ckqtw4ss3qt460fcqugqzq2me');
assert(res[2] == 'qzxc5pnsfs8pmgfprhzc4l4vzf3zxz8p85nc6kfh8l');
});

View File

@ -185,6 +185,7 @@ helpers.createAndJoinWallet = function(m, n, opts, cb) {
pubKey: TestData.keyPair.pub,
singleAddress: !!opts.singleAddress,
coin: opts.coin || 'btc',
network: opts.network || 'livenet',
};
if (_.isBoolean(opts.supportBIP44AndP2PKH))
walletOpts.supportBIP44AndP2PKH = opts.supportBIP44AndP2PKH;
@ -194,11 +195,18 @@ helpers.createAndJoinWallet = function(m, n, opts, cb) {
async.each(_.range(n), function(i, cb) {
var copayerData = TestData.copayers[i + offset];
var pub = (_.isBoolean(opts.supportBIP44AndP2PKH) && !opts.supportBIP44AndP2PKH) ? copayerData.xPubKey_45H : copayerData.xPubKey_44H_0H_0H;
if (opts.network == 'testnet')
pub = copayerData.xPubKey_44H_0H_0Ht;
var copayerOpts = helpers.getSignedCopayerOpts({
walletId: walletId,
coin: opts.coin,
name: 'copayer ' + (i + 1),
xPubKey: (_.isBoolean(opts.supportBIP44AndP2PKH) && !opts.supportBIP44AndP2PKH) ? copayerData.xPubKey_45H : copayerData.xPubKey_44H_0H_0H,
xPubKey: pub,
requestPubKey: copayerData.pubKey_1H_0,
customData: 'custom data ' + (i + 1),
});
@ -206,6 +214,7 @@ helpers.createAndJoinWallet = function(m, n, opts, cb) {
copayerOpts.supportBIP44AndP2PKH = opts.supportBIP44AndP2PKH;
server.joinWallet(copayerOpts, function(err, result) {
if (err) console.log(err);
should.not.exist(err);
copayerIds.push(result.copayerId);
return cb(err);

View File

@ -458,6 +458,7 @@ describe('Wallet service', function() {
});
});
describe('Address derivation strategy', function() {
var server;
beforeEach(function() {
@ -1249,7 +1250,7 @@ describe('Wallet service', function() {
});
});
it('should create address', function(done) {
it('should create address ', function(done) {
server.createAddress({}, function(err, address) {
should.not.exist(err);
should.exist(address);
@ -1377,6 +1378,113 @@ describe('Wallet service', function() {
});
});
describe('shared wallets (BIP44/BCH)', function() {
beforeEach(function(done) {
helpers.createAndJoinWallet(2, 2, {
coin: 'bch'
}, function(s, w) {
server = s;
wallet = w;
done();
});
});
it('should create address', function(done) {
server.createAddress({}, function(err, address) {
should.not.exist(err);
should.exist(address);
address.walletId.should.equal(wallet.id);
address.network.should.equal('livenet');
address.address.should.equal('HBf8isgS8EXG1r3X6GP89FmooUmiJ42wHS');
address.isChange.should.be.false;
address.path.should.equal('m/0/0');
address.type.should.equal('P2SH');
address.coin.should.equal('bch');
server.getNotifications({}, function(err, notifications) {
should.not.exist(err);
var notif = _.find(notifications, {
type: 'NewAddress'
});
should.exist(notif);
notif.data.address.should.equal(address.address);
done();
});
});
});
it('should create many addresses on simultaneous requests', function(done) {
var N = 5;
async.mapSeries(_.range(N), function(i, cb) {
server.createAddress({}, cb);
}, function(err, addresses) {
addresses.length.should.equal(N);
_.each(_.range(N), function(i) {
addresses[i].path.should.equal('m/0/' + i);
});
// No two identical addresses
_.uniq(_.pluck(addresses, 'address')).length.should.equal(N);
done();
});
});
it('should not create address if unable to store it', function(done) {
sinon.stub(server.storage, 'storeAddressAndWallet').yields('dummy error');
server.createAddress({}, function(err, address) {
should.exist(err);
should.not.exist(address);
server.getMainAddresses({}, function(err, addresses) {
addresses.length.should.equal(0);
server.storage.storeAddressAndWallet.restore();
server.createAddress({}, function(err, address) {
should.not.exist(err);
should.exist(address);
done();
});
});
});
});
});
describe('1-1 wallet (BIP44/BCH/Testnet)', function() {
beforeEach(function(done) {
helpers.createAndJoinWallet(1, 1, {
coin: 'bch',
network: 'testnet',
}, function(s, w) {
server = s;
wallet = w;
done();
});
});
it('should create address', function(done) {
server.createAddress({}, function(err, address) {
should.not.exist(err);
should.exist(address);
address.walletId.should.equal(wallet.id);
address.network.should.equal('testnet');
address.address.should.equal('mrM5kMkqZccK5MxZYSsM3SjqdMaNKLJgrJ');
address.isChange.should.be.false;
address.path.should.equal('m/0/0');
address.type.should.equal('P2PKH');
address.coin.should.equal('bch');
server.getNotifications({}, function(err, notifications) {
should.not.exist(err);
var notif = _.find(notifications, {
type: 'NewAddress'
});
should.exist(notif);
notif.data.address.should.equal(address.address);
done();
});
});
});
});
describe('1-of-1 (BIP44 & P2PKH)', function() {
beforeEach(function(done) {
helpers.createAndJoinWallet(1, 1, function(s, w) {

View File

@ -13,6 +13,11 @@ var copayers = [{
xPubKey_45H: 'xpub68pKcb8jHWqWuTgPz2czjFSJJBJTsTNdd87Mgh5bVz4sNFBJBus5KyptGBWgA4V6LGCi12s4Mw4S1JC2GkqX4NJ4kfQ47XqRZLbyM2DY9Jd',
xPrivKey_44H_0H_0H: 'xprv9zWRZ7CXrC4z9xA9RRBFXohmPKbyCajWaCNTHPtwNeJwTnysHG5QK7WMqpNLVtvqGxts7WNcNtqBLfdaFdCGknDPXjLKt2E2BUrPaFDqrLh',
xPubKey_44H_0H_0H: 'xpub6DVmxcjRgZdHNSEcXSiFtweVwMSTc3TMwRJ45nJYvyqvLbK1poPerupqh87rSoz27wvckb1CKnGZoLmLXSZyNGZtVd7neqSvdwJL6fceQpe',
xPrivKey_44H_0H_0Ht: 'tprv8ZgxMBicQKsPcxUEtgtQ2wKpkmuNKS6R2w3UmFTUHHURv4PKGE2aGkkbQEcQs9gGsoW4zPr7VM98xdbjQuWc3cZ6bkEyKy1sywhV9gLUcUi',
xPubKey_44H_0H_0Ht: 'tpubD6NzVbkrYhZ4WRW2nLYzSLywKoRJUmHKcEeG3mVmhZGpkYe5tcrATFNTaQRAWM3dzL2QyXoctpjkaAXruDXyc6xkF4EDGu3eQdwZXFzoFSW',
xPrivKey_1H: 'xprv9upyD5bqT9HBkWym7TvTH3njEzTnjrtkLB2sg3DD2CxxA5hZKGee1sYJUtD8C4QaeATLXQ33TirRzRhuTGDBA6XRoYDMwfXAj1KSmGyNBio',
xPubKey_1H: 'xpub68pKcb8jHWqUy14EDVTTeBjTo2JH9KcbhPxUURcpaYVw2t2hroxtZfrnLBw1bWzBrHbEJA48QmZ8DB9gTvhphKSitC15SiYx9k2ncGh55Hq',
privKey_1H_0: 'a710be25950738a7d13637e2e09affd7f579a3479fd7cc024bd9459f8fba6659',