Skip to content

Commit

Permalink
Merge commit 'a198d228326b245a5af4afe59158bbbf646d388d' into chore/up…
Browse files Browse the repository at this point in the history
…date-bwc
  • Loading branch information
Micah Riggan committed Dec 27, 2018
2 parents d5c91f0 + a198d22 commit eda84a1
Show file tree
Hide file tree
Showing 14 changed files with 1,588 additions and 2,638 deletions.
35 changes: 35 additions & 0 deletions packages/bitcore-wallet-client/.circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Javascript Node CircleCI 2.0 configuration file
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
version: 2
jobs:
copay:
docker:
- image: circleci/node:8.12.0
- image: circleci/mongo:4.0.4

working_directory: ~/bws
steps:
- checkout
# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
# fallback to using the latest cache if no exact match is found
- v1-dependencies-
- run: npm ci
- save_cache:
paths:
- node_modules
key: v1-dependencies-{{ checksum "package.json" }}
- run: npm test
- run: npx codecov
- store_artifacts:
path: ./test
- store_test_results:
path: ./test

workflows:
version: 2
build_and_test:
jobs:
- copay
7 changes: 0 additions & 7 deletions packages/bitcore-wallet-client/.travis.yml

This file was deleted.

6 changes: 1 addition & 5 deletions packages/bitcore-wallet-client/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@

BIN_PATH:=node_modules/.bin/

all: bitcore-wallet-client.min.js
all: bitcore-wallet-client.js

clean:
rm bitcore-wallet-client.js
rm bitcore-wallet-client.min.js

bitcore-wallet-client.js: index.js lib/*.js
${BIN_PATH}browserify $< > $@

bitcore-wallet-client.min.js: bitcore-wallet-client.js
uglify -s $< -o $@

cover:
./node_modules/.bin/istanbul cover ./node_modules/.bin/_mocha -- --reporter spec test
42 changes: 34 additions & 8 deletions packages/bitcore-wallet-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ You can start using bitcore-wallet-client in any of these two ways:

Start your own local [Bitcore wallet service](https://github.com/bitpay/bitcore-wallet-service) instance. In this example we assume you have `bitcore-wallet-service` running on your `localhost:3232`.

Then create two files `irene.js` and `tomas.js` with the content below:
Install `bitcore-wallet-client` before start:

```
npm i bitcore-wallet-client
```

### **Create and join a shared wallet**
---
Create two files `irene.js` and `tomas.js` with the content below:

**irene.js**

Expand Down Expand Up @@ -104,12 +112,6 @@ client.joinWallet(secret, "Tomas", {}, function(err, wallet) {
});
```

Install `bitcore-wallet-client` before start:

```
npm i bitcore-wallet-client
```

Create a new wallet with the first script:

```
Expand All @@ -135,10 +137,34 @@ Return: [...]
Note that the scripts created two files named `irene.dat` and `tomas.dat`. With these files you can get status, generate addresses, create proposals, sign transactions, etc.


# Global
### **Open a wallet dat file**
---

``` javascript
var Client = require('bitcore-wallet-client');


var fs = require('fs');
var BWS_INSTANCE_URL = 'https://bws.bitpay.com/bws/api'

var client = new Client({
baseUrl: BWS_INSTANCE_URL,
verbose: false,
});

client.import(fs.readFileSync("filename.dat"));
```
Now you can get the balance for the wallet with:

``` javascript
client.openWallet((err, res) => {
client.getBalance((err, res) => {
console.log(res);
});
});
```

# Global


* * *
Expand Down
56 changes: 0 additions & 56 deletions packages/bitcore-wallet-client/bitcore-wallet-client.min.js

Large diffs are not rendered by default.

56 changes: 22 additions & 34 deletions packages/bitcore-wallet-client/lib/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,23 +165,6 @@ API._encryptMessage = function(message, encryptingKey) {
return Utils.encryptMessage(message, encryptingKey);
};

/**
* Decrypt a message
* @private
* @static
* @memberof Client.API
* @param {String} message
* @param {String} encryptingKey
*/
API._decryptMessage = function(message, encryptingKey) {
if (!message) return '';
try {
return Utils.decryptMessage(message, encryptingKey);
} catch (ex) {
return '<ECANNOTDECRYPT>';
}
};

API.prototype._processTxNotes = function(notes) {
var self = this;

Expand All @@ -190,9 +173,9 @@ API.prototype._processTxNotes = function(notes) {
var encryptingKey = self.credentials.sharedEncryptingKey;
_.each([].concat(notes), function(note) {
note.encryptedBody = note.body;
note.body = API._decryptMessage(note.body, encryptingKey);
note.body = Utils.decryptMessageNoThrow(note.body, encryptingKey);
note.encryptedEditedByName = note.editedByName;
note.editedByName = API._decryptMessage(note.editedByName, encryptingKey);
note.editedByName = Utils.decryptMessageNoThrow(note.editedByName, encryptingKey);
});
};

Expand All @@ -211,18 +194,21 @@ API.prototype._processTxps = function(txps) {
var encryptingKey = self.credentials.sharedEncryptingKey;
_.each([].concat(txps), function(txp) {
txp.encryptedMessage = txp.message;
txp.message = API._decryptMessage(txp.message, encryptingKey) || null;
txp.creatorName = API._decryptMessage(txp.creatorName, encryptingKey);
txp.message = Utils.decryptMessageNoThrow(txp.message, encryptingKey) || null;
txp.creatorName = Utils.decryptMessageNoThrow(txp.creatorName, encryptingKey);

_.each(txp.actions, function(action) {
action.copayerName = API._decryptMessage(action.copayerName, encryptingKey);
action.comment = API._decryptMessage(action.comment, encryptingKey);

// CopayerName encryption is optional (not available in older wallets)
action.copayerName = Utils.decryptMessageNoThrow(action.copayerName, encryptingKey);

action.comment = Utils.decryptMessageNoThrow(action.comment, encryptingKey);
// TODO get copayerName from Credentials -> copayerId to copayerName
// action.copayerName = null;
});
_.each(txp.outputs, function(output) {
output.encryptedMessage = output.message;
output.message = API._decryptMessage(output.message, encryptingKey) || null;
output.message = Utils.decryptMessageNoThrow(output.message, encryptingKey) || null;
});
txp.hasUnconfirmedInputs = _.some(txp.inputs, function(input) {
return input.confirmations == 0;
Expand Down Expand Up @@ -304,7 +290,7 @@ var _deviceValidated;
*
* @param {Object} opts
* @param {String} opts.passphrase
* @param {String} opts.skipDeviceValidation
* @param {Boolean} opts.skipDeviceValidation
*/
API.prototype.validateKeyDerivation = function(opts, cb) {
var self = this;
Expand Down Expand Up @@ -665,7 +651,7 @@ API.prototype.getBalanceFromPrivateKey = function(privateKey, coin, cb) {
var privateKey = new B.PrivateKey(privateKey);
var address = privateKey.publicKey.toAddress();
self.getUtxos({
addresses: address.toString(),
addresses: coin == 'bch' ? address.toLegacyAddress() : address.toString(),
}, function(err, utxos) {
if (err) return cb(err);
return cb(null, _.sumBy(utxos, 'satoshis'));
Expand All @@ -686,7 +672,7 @@ API.prototype.buildTxFromPrivateKey = function(privateKey, destinationAddress, o

function(next) {
self.getUtxos({
addresses: address.toString(),
addresses: coin == 'bch' ? address.toLegacyAddress() : address.toString(),
}, function(err, utxos) {
return next(err, utxos);
});
Expand Down Expand Up @@ -1503,21 +1489,21 @@ API.prototype._processWallet = function(wallet) {

var encryptingKey = self.credentials.sharedEncryptingKey;

var name = Utils.decryptMessage(wallet.name, encryptingKey);
var name = Utils.decryptMessageNoThrow(wallet.name, encryptingKey);
if (name != wallet.name) {
wallet.encryptedName = wallet.name;
}
wallet.name = name;
_.each(wallet.copayers, function(copayer) {
var name = Utils.decryptMessage(copayer.name, encryptingKey);
var name = Utils.decryptMessageNoThrow(copayer.name, encryptingKey);
if (name != copayer.name) {
copayer.encryptedName = copayer.name;
}
copayer.name = name;
_.each(copayer.requestPubKeys, function(access) {
if (!access.name) return;

var name = Utils.decryptMessage(access.name, encryptingKey);
var name = Utils.decryptMessageNoThrow(access.name, encryptingKey);
if (name != access.name) {
access.encryptedName = access.name;
}
Expand Down Expand Up @@ -1765,7 +1751,6 @@ API.prototype.createTxProposal = function(opts, cb) {
if (err) return cb(err);

self._processTxps(txp);

if (!Verifier.checkProposalCreation(args, txp, self.credentials.sharedEncryptingKey)) {
return cb(new Errors.SERVER_COMPROMISED);
}
Expand Down Expand Up @@ -2251,9 +2236,12 @@ API.prototype.broadcastTxProposal = function(txp, cb) {
}),
coin: txp.coin || 'btc',
}, function(err, ack, memo) {
if (err) return cb(err);
self._doBroadcast(txp, function(err, txp) {
return cb(err, txp, memo);
log.warn('Merchant rejected the payment. Broadcasting it any ways.', err);
if (memo) {
log.debug('Merchant memo:', memo);
}
self._doBroadcast(txp, function(err2, txp) {
return cb(err2, txp, memo, err);
});
});
} else {
Expand Down
44 changes: 39 additions & 5 deletions packages/bitcore-wallet-client/lib/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,49 @@ Utils.encryptMessage = function(message, encryptingKey) {
}, Utils.SJCL));
};

// Will throw if it can't decrypt
Utils.decryptMessage = function(cyphertextJson, encryptingKey) {
try {
var key = sjcl.codec.base64.toBits(encryptingKey);
return sjcl.decrypt(key, cyphertextJson);
} catch (ex) {
if (!cyphertextJson) return;

if (!encryptingKey)
throw 'No key';

var key = sjcl.codec.base64.toBits(encryptingKey);
return sjcl.decrypt(key, cyphertextJson);
};


Utils.decryptMessageNoThrow = function(cyphertextJson, encryptingKey) {
function isJsonString(str) {
var r;
try {
r=JSON.parse(str);
} catch (e) {
return false;
}
return r;
}

if (!encryptingKey)
return '<ECANNOTDECRYPT>';

if (!cyphertextJson)
return '';

// no sjcl encrypted json
var r= isJsonString(cyphertextJson);
if (!r|| !r.iv || !r.ct) {
return cyphertextJson;
}

try {
return Utils.decryptMessage(cyphertextJson, encryptingKey);
} catch (e) {
return '<ECANNOTDECRYPT>';
}
};


/* TODO: It would be nice to be compatible with bitcoind signmessage. How
* the hash is calculated there? */
Utils.hashMessage = function(text) {
Expand Down Expand Up @@ -122,7 +156,7 @@ Utils.deriveAddress = function(scriptType, publicKeyRing, path, m, network, coin
}

return {
address: bitcoreAddress.toString(),
address: coin == 'bch' ? bitcoreAddress.toLegacyAddress() : bitcoreAddress.toString(),
path: path,
publicKeys: _.invokeMap(publicKeys, 'toString'),
};
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-wallet-client/lib/paypro.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ PayPro.get = function(opts, cb) {
memo: pd.get('memo'),
time: pd.get('time'),
merchant_data: md,
toAddress: addr.toString(),
toAddress: coin == 'bch' ? addr.toLegacyAddress() : addr.toString(),
amount: amount,
network: network,
domain: opts.host,
Expand Down
2 changes: 1 addition & 1 deletion packages/bitcore-wallet-client/lib/verifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ Verifier.checkProposalCreation = function(args, txp, encryptingKey) {
return false;
}
if (!strEqual(txp.message, decryptedMessage)) return false;
if (args.customData && !_.isEqual(txp.customData, args.customData)) return false;
if ((args.customData || txp.customData) && !_.isEqual(txp.customData, args.customData)) return false;

return true;
};
Expand Down
Loading

0 comments on commit eda84a1

Please sign in to comment.