Skip to content
This repository has been archived by the owner on Apr 3, 2019. It is now read-only.

Chore/cashaddr #826

Open
wants to merge 6 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
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ var config = {
},
testnet: {
provider: 'v8',
url: 'http://localhost:3000',
url: 'http://localhost:3000',
//url: 'https://api.bitcore.io',
},

Expand Down
57 changes: 7 additions & 50 deletions lib/blockchainexplorers/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ var io = require('socket.io-client');
const request = require('request-promise-native');
var Common = require('../common');
var Client;
var BCHAddressTranslator = require('../bchaddresstranslator');
var Bitcore = require('bitcore-lib');
var Bitcore_ = {
btc: Bitcore,
Expand Down Expand Up @@ -62,41 +61,6 @@ var _parseErr = function(err, res) {
};


// Translate Request Address query
V8.prototype.translateQueryAddresses = function(addresses) {
if (!this.addressFormat) return addresses;

return BCHAddressTranslator.translate(addresses, this.addressFormat, 'copay');
};


// Translate Result Address
V8.prototype.translateResultAddresses = function(addresses) {
if (!this.addressFormat) return addresses;

return BCHAddressTranslator.translate(addresses, 'copay', this.addressFormat);
};


V8.prototype.translateTx = function(tx) {
var self = this;
if (!this.addressFormat) return tx;

_.each(tx.vin, function(x){
if (x.addr) {
x.addr = self.translateResultAddresses(x.addr);
}
});


_.each(tx.vout, function(x){
if (x.scriptPubKey && x.scriptPubKey.addresses) {
x.scriptPubKey.addresses = self.translateResultAddresses(x.scriptPubKey.addresses);
}
});

};

V8.prototype.supportsGrouping = function () {
return true;
};
Expand All @@ -123,10 +87,6 @@ V8.prototype.addAddresses = function (wallet, addresses, cb) {
var client = this._getAuthClient(wallet);

const payload = _.map(addresses, a => {
if (self.addressFormat) {
a = self.translateQueryAddresses(a);
}

return {
address: a,
}
Expand Down Expand Up @@ -188,6 +148,9 @@ V8.prototype.getConnectionInfo = function() {

/**
* Retrieve a list of unspent outputs associated with an address or set of addresses
*
*
* This is for internal usage, address should be on internal representaion
*/
V8.prototype.getUtxos = function(wallet, cb) {
var self = this;
Expand All @@ -196,9 +159,6 @@ V8.prototype.getUtxos = function(wallet, cb) {
client.getCoins({pubKey: wallet.beAuthPublicKey2, payload: {} })
.then( (unspent) => {
_.each(unspent, function(x) {
if (self.addressFormat) {
x.address = self.translateResultAddresses(x.address);
}
// v8 field name differences
x.satoshis = x.value;
x.amount = x.value / 1e8;
Expand Down Expand Up @@ -239,6 +199,8 @@ console.log('[v8.js.221:err:]',err); //TODO
});
};


// This is for internal usage, addresses should be returned on internal representation
V8.prototype.getTransaction = function(txid, cb) {
var self = this;
console.log('[v8.js.207] GET TX', txid); //TODO
Expand All @@ -248,7 +210,6 @@ console.log('[v8.js.207] GET TX', txid); //TODO
if (!tx || _.isEmpty(tx)) {
return cb();
}
self.translateTx(tx);
return cb(null, tx);
})
.catch((err) =>{
Expand Down Expand Up @@ -305,9 +266,6 @@ console.time('V8 getTxs');
log.error('v8 error at JSON.parse:' + e + ' Parsing:' + rawTx + ":");
return cb(e);
}
if (tx.address && self.addressFormat) {
tx.address = self.translateResultAddresses(tx.address);
}
// v8 field name differences
if (tx.value)
tx.amount = tx.satoshis / 1e8;
Expand All @@ -333,7 +291,7 @@ console.time('V8 getTxs');
V8.prototype.getAddressActivity = function(address, cb) {
var self = this;

var url = this.baseUrl + '/address/' + this.translateQueryAddresses(address) + '/txs?limit=1';
var url = this.baseUrl + '/address/' + address + '/txs?limit=1';
console.log('[v8.js.328:url:] CHECKING ADDRESS ACTIVITY',url); //TODO
request.get(url, {})
.then( (ret) => {
Expand Down Expand Up @@ -420,10 +378,9 @@ V8.prototype.initSocket = function(callbacks) {
// script output, or similar.
if (!data.address) return;
var out;
let addr = self.coin == 'bch' ? BCHAddressTranslator.translate(data.address, 'copay', 'cashaddr') : data.address;
try {
out = {
address: addr,
address: data.address,
amount: data.value / 1e8,
};
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion lib/blockchainexplorers/v8/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Client.prototype.importAddresses = async function(params) {
const { payload, pubKey } = params;
const url = `${this.baseUrl}/wallet/${pubKey}`;

console.log('add addresses:',url); //TODO
console.log('addAddresses:',url, payload); //TODO
const signature = this.sign({ method: 'POST', url, payload});
let h = { 'x-signature': signature};
return request.post(url, {
Expand Down
1 change: 1 addition & 0 deletions lib/errors/errordefinitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var errors = {
COPAYER_VOTED: 'Copayer already voted on this transaction proposal',
DUST_AMOUNT: 'Amount below dust threshold',
INCORRECT_ADDRESS_NETWORK: 'Incorrect address network',
ONLY_CASHADDR: 'Only cashaddr wo prefix is allowed for outputs',
INSUFFICIENT_FUNDS: 'Insufficient funds',
INSUFFICIENT_FUNDS_FOR_FEE: 'Insufficient funds for fee',
INVALID_ADDRESS: 'Invalid address',
Expand Down
56 changes: 56 additions & 0 deletions lib/expressapp.js
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,19 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});



// DEPRECATED (do not use cashaddr)
router.get('/v1/txproposals/', function(req, res) {
getServerWithAuth(req, res, function(server) {
server.getPendingTxs({noCashAddr: true}, function(err, pendings) {
if (err) return returnError(err, res, req);
res.json(pendings);
});
});
});

router.get('/v2/txproposals/', function(req, res) {
getServerWithAuth(req, res, function(server) {
server.getPendingTxs({}, function(err, pendings) {
if (err) return returnError(err, res, req);
Expand All @@ -361,7 +373,19 @@ ExpressApp.prototype.start = function(opts, cb) {
return returnError(err, res, req);
});


// DEPRECATED, no cash addr
router.post('/v2/txproposals/', function(req, res) {
getServerWithAuth(req, res, function(server) {
req.body.noCashAddr = true;
server.createTx(req.body, function(err, txp) {
if (err) return returnError(err, res, req);
res.json(txp);
});
});
});

router.post('/v3/txproposals/', function(req, res) {
getServerWithAuth(req, res, function(server) {
server.createTx(req.body, function(err, txp) {
if (err) return returnError(err, res, req);
Expand All @@ -370,6 +394,8 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});



// DEPRECATED
router.post('/v1/addresses/', function(req, res) {
logDeprecated(req);
Expand All @@ -396,7 +422,21 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});

// DEPRECATED (no cashaddr by default)
router.post('/v3/addresses/', function(req, res) {
getServerWithAuth(req, res, function(server) {
var opts = req.body;
opts = opts || {};
opts.noCashAddr = true;
server.createAddress(opts, function(err, address) {
if (err) return returnError(err, res, req);
res.json(address);
});
});
});


router.post('/v4/addresses/', function(req, res) {
getServerWithAuth(req, res, function(server) {
server.createAddress(req.body, function(err, address) {
if (err) return returnError(err, res, req);
Expand All @@ -405,6 +445,7 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});


router.get('/v1/addresses/', function(req, res) {
getServerWithAuth(req, res, function(server) {
var opts = {};
Expand Down Expand Up @@ -529,7 +570,20 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});

//
router.post('/v1/txproposals/:id/publish/', function(req, res) {
getServerWithAuth(req, res, function(server) {
req.body.txProposalId = req.params['id'];
req.body.noCashAddr = true;
server.publishTx(req.body, function(err, txp) {
if (err) return returnError(err, res, req);
res.json(txp);
res.end();
});
});
});

router.post('/v2/txproposals/:id/publish/', function(req, res) {
getServerWithAuth(req, res, function(server) {
req.body.txProposalId = req.params['id'];
server.publishTx(req.body, function(err, txp) {
Expand All @@ -540,6 +594,8 @@ ExpressApp.prototype.start = function(opts, cb) {
});
});



// TODO Check HTTP verb and URL name
router.post('/v1/txproposals/:id/broadcast/', function(req, res) {
getServerWithAuth(req, res, function(server) {
Expand Down
17 changes: 13 additions & 4 deletions lib/model/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ Address.fromObj = function(obj) {
return x;
};

Address._deriveAddress = function(scriptType, publicKeyRing, path, m, coin, network) {
Address._deriveAddress = function(scriptType, publicKeyRing, path, m, coin, network, noNativeCashAddr) {
$.checkArgument(Utils.checkValueInCollection(scriptType, Constants.SCRIPT_TYPES));

var publicKeys = _.map(publicKeyRing, function(item) {
Expand All @@ -73,16 +73,25 @@ Address._deriveAddress = function(scriptType, publicKeyRing, path, m, coin, netw
break;
}



let addrStr = bitcoreAddress.toString(true);
if (noNativeCashAddr && coin == 'bch') {
addrStr = bitcoreAddress.toLegacyAddress();
}

return {
// bws still use legacy addresses for BCH
address: coin == 'bch' ? bitcoreAddress.toLegacyAddress() : bitcoreAddress.toString(),
address: addrStr,
path: path,
publicKeys: _.invokeMap(publicKeys, 'toString'),
};
};

Address.derive = function(walletId, scriptType, publicKeyRing, path, m, coin, network, isChange) {
var raw = Address._deriveAddress(scriptType, publicKeyRing, path, m, coin, network);

// noNativeCashAddr only for testing
Address.derive = function(walletId, scriptType, publicKeyRing, path, m, coin, network, isChange, noNativeCashAddr) {
var raw = Address._deriveAddress(scriptType, publicKeyRing, path, m, coin, network, noNativeCashAddr);
return Address.create(_.extend(raw, {
coin: coin,
walletId: walletId,
Expand Down
8 changes: 6 additions & 2 deletions lib/model/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ Wallet.create = function(opts) {
x.beAuthPrivateKey2 = null;
x.beAuthPublicKey2 = null;

// x.nativeCashAddr opts is only for testing
x.nativeCashAddr = _.isUndefined(opts.nativeCashAddr) ? (x.coin == 'bch' ? true : null) : opts.nativeCashAddr;

return x;
};

Expand Down Expand Up @@ -95,6 +98,8 @@ Wallet.fromObj = function(obj) {
x.beAuthPrivateKey2 = obj.beAuthPrivateKey2;
x.beAuthPublicKey2 = obj.beAuthPublicKey2;

x.nativeCashAddr = obj.nativeCashAddr;

return x;
};

Expand Down Expand Up @@ -184,12 +189,11 @@ Wallet.prototype.isScanning = function() {

Wallet.prototype.createAddress = function(isChange, step) {
$.checkState(this.isComplete());

var self = this;

var path = this.addressManager.getNewAddressPath(isChange, step);
log.verbose('Deriving addr:' + path);
var address = Address.derive(self.id, this.addressType, this.publicKeyRing, path, this.m, this.coin, this.network, isChange);
var address = Address.derive(self.id, this.addressType, this.publicKeyRing, path, this.m, this.coin, this.network, isChange, !self.nativeCashAddr);
return address;
};

Expand Down
Loading