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

Commit e9289c0

Browse files
committed
Hide encrypted json is no decrypting key is present
1 parent 5dab59b commit e9289c0

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

lib/api.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1493,21 +1493,21 @@ API.prototype._processWallet = function(wallet) {
14931493

14941494
var encryptingKey = self.credentials.sharedEncryptingKey;
14951495

1496-
var name = Utils.decryptMessage(wallet.name, encryptingKey);
1496+
var name = Utils.decryptMessageNoThrow(wallet.name, encryptingKey);
14971497
if (name != wallet.name) {
14981498
wallet.encryptedName = wallet.name;
14991499
}
15001500
wallet.name = name;
15011501
_.each(wallet.copayers, function(copayer) {
1502-
var name = Utils.decryptMessage(copayer.name, encryptingKey);
1502+
var name = Utils.decryptMessageNoThrow(copayer.name, encryptingKey);
15031503
if (name != copayer.name) {
15041504
copayer.encryptedName = copayer.name;
15051505
}
15061506
copayer.name = name;
15071507
_.each(copayer.requestPubKeys, function(access) {
15081508
if (!access.name) return;
15091509

1510-
var name = Utils.decryptMessage(access.name, encryptingKey);
1510+
var name = Utils.decryptMessageNoThrow(access.name, encryptingKey);
15111511
if (name != access.name) {
15121512
access.encryptedName = access.name;
15131513
}
@@ -1747,7 +1747,6 @@ API.prototype.createTxProposal = function(opts, cb) {
17471747
if (err) return cb(err);
17481748

17491749
self._processTxps(txp);
1750-
17511750
if (!Verifier.checkProposalCreation(args, txp, self.credentials.sharedEncryptingKey)) {
17521751
return cb(new Errors.SERVER_COMPROMISED);
17531752
}

lib/common/utils.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,28 @@ Utils.encryptMessage = function(message, encryptingKey) {
3030
}, Utils.SJCL));
3131
};
3232

33+
// Will throw if it can't decrypt
3334
Utils.decryptMessage = function(cyphertextJson, encryptingKey) {
35+
if (!cyphertextJson) return;
36+
37+
if (!encryptingKey)
38+
throw 'No key';
39+
40+
var key = sjcl.codec.base64.toBits(encryptingKey);
41+
return sjcl.decrypt(key, cyphertextJson);
42+
};
43+
44+
45+
Utils.decryptMessageNoThrow = function(cyphertextJson, encryptingKey) {
3446
try {
35-
var key = sjcl.codec.base64.toBits(encryptingKey);
36-
return sjcl.decrypt(key, cyphertextJson);
37-
} catch (ex) {
47+
return Utils.decryptMessage(cyphertextJson, encryptingKey);
48+
} catch (e) {
3849
return cyphertextJson;
3950
}
4051
};
4152

53+
54+
4255
/* TODO: It would be nice to be compatible with bitcoind signmessage. How
4356
* the hash is calculated there? */
4457
Utils.hashMessage = function(text) {

lib/credentials.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ Credentials.prototype.getDerivedXPrivKey = function(password) {
313313
return deriveFn(path);
314314
};
315315

316-
Credentials.prototype.addWalletPrivateKey = function(walletPrivKey) {
316+
Credent.prototype.addWalletPrivateKey = function(walletPrivKey) {
317317
this.walletPrivKey = walletPrivKey;
318318
this.sharedEncryptingKey = Utils.privateKeyToAESKey(walletPrivKey);
319319
};

test/client.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,7 +1454,7 @@ describe('client API', function() {
14541454
});
14551455
});
14561456

1457-
it.only('should store walletPrivKey', function(done) {
1457+
it('should store walletPrivKey', function(done) {
14581458
clients[0].createWallet('mywallet', 'creator', 1, 1, {
14591459
network: 'testnet'
14601460
}, function(err) {
@@ -1468,6 +1468,7 @@ describe('client API', function() {
14681468
status.wallet.publicKeyRing.length.should.equal(1);
14691469
status.wallet.status.should.equal('complete');
14701470
var key2 = status.customData.walletPrivKey;
1471+
14711472
clients[0].credentials.walletPrivKey.should.be.equal(key2);
14721473
done();
14731474
});
@@ -2360,7 +2361,7 @@ describe('client API', function() {
23602361
});
23612362
});
23622363
});
2363-
it('Should fail to create proposal with insufficient funds for fee', function(done) {
2364+
it('Should fail to create proposal with insufficient funds for fee', function(done) {
23642365
var opts = {
23652366
amount: 5e8 - 200e2,
23662367
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
@@ -2450,6 +2451,30 @@ describe('client API', function() {
24502451
});
24512452
});
24522453
});
2454+
it('Should hide message and refusal texts if not key is present', function(done) {
2455+
var opts = {
2456+
amount: 1e8,
2457+
toAddress: 'n2TBMPzPECGUfcT2EByiTJ12TPZkhN2mN5',
2458+
message: 'some message',
2459+
};
2460+
helpers.createAndPublishTxProposal(clients[0], opts, function(err, x) {
2461+
should.not.exist(err);
2462+
clients[1].rejectTxProposal(x, 'rejection comment', function(err, tx1) {
2463+
should.not.exist(err);
2464+
2465+
clients[2].credentials.sharedEncryptingKey=null;
2466+
2467+
clients[2].getTxProposals({}, function(err, txs) {
2468+
should.not.exist(err);
2469+
txs[0].message.should.equal('<ECANNOTDECRYPT>');
2470+
txs[0].actions[0].copayerName.should.equal('<ECANNOTDECRYPT>');
2471+
txs[0].actions[0].comment.should.equal('<ECANNOTDECRYPT>');
2472+
done();
2473+
});
2474+
});
2475+
});
2476+
});
2477+
24532478
it('Should encrypt proposal message', function(done) {
24542479
var opts = {
24552480
outputs: [{

test/utils.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,30 @@ describe('Utils', function() {
189189
});
190190
});
191191

192+
193+
describe('#decryptMessage should throw', function() {
194+
it('should encrypt and decrypt', function() {
195+
var pwd = "ezDRS2NRchMJLf1IWtjL5A==";
196+
var ct = Utils.encryptMessage('hello world', pwd);
197+
(function(){
198+
Utils.decryptMessage(ct, 'test')
199+
}).should.throw('invalid aes key size');
200+
});
201+
});
202+
203+
describe('#decryptMessageNoThrow should not throw', function() {
204+
it('should encrypt and decrypt', function() {
205+
var pwd = "ezDRS2NRchMJLf1IWtjL5A==";
206+
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);
211+
});
212+
});
213+
214+
215+
192216
describe('#getProposalHash', function() {
193217
it('should compute hash for old style proposals', function() {
194218
var hash = Utils.getProposalHash('msj42CCGruhRsFrGATiUuh25dtxYtnpbTx', 1234, 'the message');

0 commit comments

Comments
 (0)