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

Commit a0da3d4

Browse files
committed
fix copayerName decrypt in certain cases
1 parent 5e37c83 commit a0da3d4

File tree

6 files changed

+75
-88
lines changed

6 files changed

+75
-88
lines changed

bitcore-wallet-client.min.js

Lines changed: 0 additions & 56 deletions
Large diffs are not rendered by default.

lib/api.js

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -164,23 +164,6 @@ API._encryptMessage = function(message, encryptingKey) {
164164
return Utils.encryptMessage(message, encryptingKey);
165165
};
166166

167-
/**
168-
* Decrypt a message
169-
* @private
170-
* @static
171-
* @memberof Client.API
172-
* @param {String} message
173-
* @param {String} encryptingKey
174-
*/
175-
API._decryptMessage = function(message, encryptingKey) {
176-
if (!message) return '';
177-
try {
178-
return Utils.decryptMessage(message, encryptingKey);
179-
} catch (ex) {
180-
return '<ECANNOTDECRYPT>';
181-
}
182-
};
183-
184167
API.prototype._processTxNotes = function(notes) {
185168
var self = this;
186169

@@ -189,9 +172,9 @@ API.prototype._processTxNotes = function(notes) {
189172
var encryptingKey = self.credentials.sharedEncryptingKey;
190173
_.each([].concat(notes), function(note) {
191174
note.encryptedBody = note.body;
192-
note.body = API._decryptMessage(note.body, encryptingKey);
175+
note.body = Utils.decryptMessageNoThrow(note.body, encryptingKey);
193176
note.encryptedEditedByName = note.editedByName;
194-
note.editedByName = API._decryptMessage(note.editedByName, encryptingKey);
177+
note.editedByName = Utils.decryptMessageNoThrow(note.editedByName, encryptingKey);
195178
});
196179
};
197180

@@ -210,18 +193,21 @@ API.prototype._processTxps = function(txps) {
210193
var encryptingKey = self.credentials.sharedEncryptingKey;
211194
_.each([].concat(txps), function(txp) {
212195
txp.encryptedMessage = txp.message;
213-
txp.message = API._decryptMessage(txp.message, encryptingKey) || null;
214-
txp.creatorName = API._decryptMessage(txp.creatorName, encryptingKey);
196+
txp.message = Utils.decryptMessageNoThrow(txp.message, encryptingKey) || null;
197+
txp.creatorName = Utils.decryptMessageNoThrow(txp.creatorName, encryptingKey);
215198

216199
_.each(txp.actions, function(action) {
217-
action.copayerName = API._decryptMessage(action.copayerName, encryptingKey);
218-
action.comment = API._decryptMessage(action.comment, encryptingKey);
200+
201+
// CopayerName encryption is optional (not available in older wallets)
202+
action.copayerName = Utils.decryptMessageNoThrow(action.copayerName, encryptingKey);
203+
204+
action.comment = Utils.decryptMessageNoThrow(action.comment, encryptingKey);
219205
// TODO get copayerName from Credentials -> copayerId to copayerName
220206
// action.copayerName = null;
221207
});
222208
_.each(txp.outputs, function(output) {
223209
output.encryptedMessage = output.message;
224-
output.message = API._decryptMessage(output.message, encryptingKey) || null;
210+
output.message = Utils.decryptMessageNoThrow(output.message, encryptingKey) || null;
225211
});
226212
txp.hasUnconfirmedInputs = _.some(txp.inputs, function(input) {
227213
return input.confirmations == 0;

lib/common/utils.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,36 @@ Utils.decryptMessage = function(cyphertextJson, encryptingKey) {
4343

4444

4545
Utils.decryptMessageNoThrow = function(cyphertextJson, encryptingKey) {
46+
function isJsonString(str) {
47+
var r;
48+
try {
49+
r=JSON.parse(str);
50+
} catch (e) {
51+
return false;
52+
}
53+
return r;
54+
}
55+
56+
if (!encryptingKey)
57+
return '<ECANNOTDECRYPT>';
58+
59+
if (!cyphertextJson)
60+
return '';
61+
62+
// no sjcl encrypted json
63+
var r= isJsonString(cyphertextJson);
64+
if (!r|| !r.iv || !r.ct) {
65+
return cyphertextJson;
66+
}
67+
4668
try {
4769
return Utils.decryptMessage(cyphertextJson, encryptingKey);
4870
} catch (e) {
49-
return cyphertextJson;
71+
return '<ECANNOTDECRYPT>';
5072
}
5173
};
5274

5375

54-
5576
/* TODO: It would be nice to be compatible with bitcoind signmessage. How
5677
* the hash is calculated there? */
5778
Utils.hashMessage = function(text) {

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "bitcore-wallet-client",
33
"description": "Client for bitcore-wallet-service",
44
"author": "BitPay Inc",
5-
"version": "6.7.4",
5+
"version": "6.7.5",
66
"license": "MIT",
77
"keywords": [
88
"bitcoin",

test/utils.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,47 @@ describe('Utils', function() {
204204
it('should encrypt and decrypt', function() {
205205
var pwd = "ezDRS2NRchMJLf1IWtjL5A==";
206206
var ct = Utils.encryptMessage('hello world', pwd);
207-
var msg = Utils.decryptMessageNoThrow(ct, 'test');
208-
// returns encrypted json
209-
should.exist(JSON.parse(msg).iv);
210-
should.exist(JSON.parse(msg).ct);
207+
var msg = Utils.decryptMessageNoThrow(ct, pwd);
208+
209+
msg.should.equal('hello world');
210+
});
211+
212+
it('should encrypt and fail to decrypt', function() {
213+
var pwd = "ezDRS2NRchMJLf1IWtjL5A==";
214+
var ct = Utils.encryptMessage('hello world', pwd);
215+
var msg = Utils.decryptMessageNoThrow(ct, 'hola');
216+
217+
msg.should.equal('<ECANNOTDECRYPT>');
218+
});
219+
220+
221+
it('should failover to decrypt a non-encrypted msg' , function() {
222+
var pwd = "ezDRS2NRchMJLf1IWtjL5A==";
223+
var msg = Utils.decryptMessageNoThrow('hola mundo', 'hola');
224+
225+
msg.should.equal('hola mundo');
226+
});
227+
228+
it('should failover to decrypt a non-encrypted msg (case 2)' , function() {
229+
var pwd = "ezDRS2NRchMJLf1IWtjL5A==";
230+
var msg = Utils.decryptMessageNoThrow('{"pepe":1}', 'hola');
231+
232+
msg.should.equal('{"pepe":1}');
211233
});
234+
235+
236+
it('should no try to decrypt empty', function() {
237+
var msg = Utils.decryptMessageNoThrow('', 'hola');
238+
msg.should.equal('');
239+
});
240+
241+
242+
it('should no try to decrypt null', function() {
243+
var msg = Utils.decryptMessageNoThrow(null, 'hola');
244+
msg.should.equal('');
245+
});
246+
247+
212248
});
213249

214250

0 commit comments

Comments
 (0)