Skip to content

Commit 94240bd

Browse files
matiuBernard Snowden
authored and
Bernard Snowden
committed
Merge pull request bitpay#2996 from bsnowden3/ref/push-notif-httpv1-migr
Ref/push Upgrade notification to httpv1 migr
2 parents 585c980 + 5ac5a5e commit 94240bd

File tree

4 files changed

+99
-72
lines changed

4 files changed

+99
-72
lines changed

packages/bitcore-wallet-service/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"email-validator": "^1.0.1",
3434
"express": "^4.10.0",
3535
"express-rate-limit": "^2.6.0",
36+
"googleapis": "60.0.1",
3637
"inherits": "^2.0.1",
3738
"istanbul": "^0.4.5",
3839
"json-stable-stringify": "^1.0.0",

packages/bitcore-wallet-service/src/config.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ module.exports = {
7272
defaultLanguage: 'en',
7373
defaultUnit: 'btc',
7474
subjectPrefix: '',
75-
pushServerUrl: 'https://fcm.googleapis.com/fcm',
76-
authorizationKey: 'You_have_to_put_something_here'
75+
pushServerUrl: 'https://fcm.googleapis.com/v1/projects/bitpay-wallet/messages:send',
76+
fcmGoogleCredentialsPath: 'you have to put path_to_service_account.json'
7777
},
7878
fiatRateServiceOpts: {
7979
defaultProvider: 'BitPay',
@@ -83,6 +83,9 @@ module.exports = {
8383
maintenanceMode: false
8484
},
8585
staticRoot: '/tmp/static'
86+
// fcm_google_credentials: {
87+
// GOOGLE_APP_CREDENTIALS: 'path_to_service_account_file_here'
88+
// },
8689
// simplex: {
8790
// sandbox: {
8891
// apiKey: 'simplex_sandbox_api_key_here',

packages/bitcore-wallet-service/src/lib/pushnotificationsservice.ts

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ const Utils = require('./common/utils');
1616
const Defaults = require('./common/defaults');
1717
const Constants = require('./common/constants');
1818
const sjcl = require('sjcl');
19+
const { google } = require('googleapis');
20+
const config = require('../config');
1921

2022
const PUSHNOTIFICATIONS_TYPES = {
2123
NewCopayer: {
@@ -49,7 +51,6 @@ export interface IPushNotificationService {
4951
subjectPrefix: string;
5052
pushServerUrl: string;
5153
availableLanguages: string;
52-
authorizationKey: string;
5354
messageBroker: any;
5455
}
5556

@@ -61,7 +62,6 @@ export class PushNotificationsService {
6162
subjectPrefix: string;
6263
pushServerUrl: string;
6364
availableLanguages: string;
64-
authorizationKey: string;
6565
storage: Storage;
6666
messageBroker: any;
6767

@@ -93,9 +93,6 @@ export class PushNotificationsService {
9393
this.defaultUnit = opts.pushNotificationsOpts.defaultUnit || 'btc';
9494
this.subjectPrefix = opts.pushNotificationsOpts.subjectPrefix || '';
9595
this.pushServerUrl = opts.pushNotificationsOpts.pushServerUrl;
96-
this.authorizationKey = opts.pushNotificationsOpts.authorizationKey;
97-
98-
if (!this.authorizationKey) return cb(new Error('Missing authorizationKey attribute in configuration.'));
9996

10097
async.parallel(
10198
[
@@ -168,25 +165,31 @@ export class PushNotificationsService {
168165
notification.data && notification.data.multisigContractAddress
169166
? notification.data.multisigContractAddress
170167
: null;
168+
let tokenAddressBodyMessage = tokenAddress ? tokenAddress : 'null';
169+
let multisigContractAddressBodyMessage = multisigContractAddress
170+
? multisigContractAddress
171+
: 'null';
171172
return {
172-
to: sub.token,
173-
priority: 'high',
174-
restricted_package_name: sub.packageName,
175-
notification: {
176-
title: content.plain.subject,
177-
body: content.plain.body,
178-
sound: 'default',
179-
click_action: 'FCM_PLUGIN_ACTIVITY',
180-
icon: 'fcm_push_icon'
181-
},
182-
data: {
183-
walletId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(notification.walletId)),
184-
tokenAddress,
185-
multisigContractAddress,
186-
copayerId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(recipient.copayerId)),
187-
title: content.plain.subject,
188-
body: content.plain.body,
189-
notification_type: notification.type
173+
message: {
174+
token: sub.token,
175+
priority: 'high',
176+
restricted_package_name: sub.packageName,
177+
notification: {
178+
title: content.plain.subject,
179+
body: content.plain.body,
180+
sound: 'default',
181+
click_action: 'FCM_PLUGIN_ACTIVITY',
182+
icon: 'fcm_push_icon'
183+
},
184+
data: {
185+
walletId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(notification.walletId)),
186+
tokenAddress: tokenAddressBodyMessage,
187+
multisigContractAddress: multisigContractAddressBodyMessage,
188+
copayerId: sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(recipient.copayerId)),
189+
title: content.plain.subject,
190+
body: content.plain.body,
191+
notification_type: notification.type
192+
}
190193
}
191194
};
192195
});
@@ -443,19 +446,38 @@ export class PushNotificationsService {
443446
};
444447
}
445448

449+
_getAccessToken() {
450+
const MESSAGING_SCOPE = 'https://www.googleapis.com/auth/firebase.messaging';
451+
const SCOPES = [MESSAGING_SCOPE];
452+
453+
return new Promise(function(resolve, reject) {
454+
const key = require(config.pushNotificationsOpts.fcmGoogleCredentialsPath);
455+
const jwtClient = new google.auth.JWT(key.client_email, null, key.private_key, SCOPES, null);
456+
jwtClient.authorize(function(err, tokens) {
457+
if (err) {
458+
reject(err);
459+
return;
460+
}
461+
resolve(tokens.access_token);
462+
});
463+
});
464+
}
465+
446466
_makeRequest(opts, cb) {
447-
this.request(
448-
{
449-
url: this.pushServerUrl + '/send',
450-
method: 'POST',
451-
json: true,
452-
headers: {
453-
'Content-Type': 'application/json',
454-
Authorization: 'key=' + this.authorizationKey
467+
this._getAccessToken().then(access_token => {
468+
this.request(
469+
{
470+
url: this.pushServerUrl,
471+
method: 'POST',
472+
json: true,
473+
headers: {
474+
'Content-Type': 'application/json',
475+
Authorization: 'Bearer ' + access_token
476+
},
477+
body: opts
455478
},
456-
body: opts
457-
},
458-
cb
459-
);
479+
cb
480+
);
481+
});
460482
}
461483
}

packages/bitcore-wallet-service/test/integration/pushNotifications.js

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ describe('Push notifications', function() {
8282
defaultUnit: 'btc',
8383
subjectPrefix: '',
8484
pushServerUrl: 'http://localhost:8000',
85-
authorizationKey: 'secret',
85+
fcmGoogleCredentialsPath: 'path_to_service_account.json'
8686
},
8787
}, function(err) {
8888
should.not.exist(err);
@@ -109,15 +109,16 @@ describe('Push notifications', function() {
109109
}, {
110110
isGlobal: true
111111
}, function(err) {
112+
should.not.exist(err);
112113
setTimeout(function() {
113114
var calls = requestStub.getCalls();
114115
var args = _.map(calls, function(c) {
115116
return c.args[0];
116117
});
117118
calls.length.should.equal(1);
118-
args[0].body.notification.title.should.contain('New payment received');
119-
args[0].body.notification.body.should.contain('123,000');
120-
args[0].body.notification.body.should.contain('bits');
119+
args[0].body.message.notification.title.should.contain('New payment received');
120+
args[0].body.message.notification.body.should.contain('123,000');
121+
args[0].body.message.notification.body.should.contain('bits');
121122
done();
122123
}, 100);
123124
});
@@ -237,7 +238,7 @@ describe('Push notifications', function() {
237238
defaultUnit: 'btc',
238239
subjectPrefix: '',
239240
pushServerUrl: 'http://localhost:8000',
240-
authorizationKey: 'secret',
241+
fcmGoogleCredentialsPath: 'path_to_service_account.json'
241242
},
242243
}, function(err) {
243244
should.not.exist(err);
@@ -273,14 +274,14 @@ describe('Push notifications', function() {
273274

274275
calls.length.should.equal(3);
275276

276-
args[0].body.notification.title.should.contain('Nuevo pago recibido');
277-
args[0].body.notification.body.should.contain('0.123');
277+
args[0].body.message.notification.title.should.contain('Nuevo pago recibido');
278+
args[0].body.message.notification.body.should.contain('0.123');
278279

279-
args[1].body.notification.title.should.contain('New payment received');
280-
args[1].body.notification.body.should.contain('123,000');
280+
args[1].body.message.notification.title.should.contain('New payment received');
281+
args[1].body.message.notification.body.should.contain('123,000');
281282

282-
args[2].body.notification.title.should.contain('New payment received');
283-
args[2].body.notification.body.should.contain('123,000');
283+
args[2].body.message.notification.title.should.contain('New payment received');
284+
args[2].body.message.notification.body.should.contain('123,000');
284285
done();
285286
}, 100);
286287
});
@@ -392,7 +393,7 @@ describe('Push notifications', function() {
392393
return c.args[0];
393394
});
394395

395-
args[0].body.notification.title.should.contain('Payment proposal rejected');
396+
args[0].body.message.notification.title.should.contain('Payment proposal rejected');
396397
done();
397398
}, 100);
398399
});
@@ -449,11 +450,11 @@ describe('Push notifications', function() {
449450
return c.args[0];
450451
});
451452

452-
args[0].body.notification.title.should.contain('Payment sent');
453-
args[1].body.notification.title.should.contain('Payment sent');
453+
args[0].body.message.notification.title.should.contain('Payment sent');
454+
args[1].body.message.notification.title.should.contain('Payment sent');
454455

455-
sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(server.copayerId)).should.not.equal(args[0].body.data.copayerId);
456-
sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(server.copayerId)).should.not.equal(args[1].body.data.copayerId);
456+
sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(server.copayerId)).should.not.equal(args[0].body.message.data.copayerId);
457+
sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(server.copayerId)).should.not.equal(args[1].body.message.data.copayerId);
457458
done();
458459
}, 100);
459460
});
@@ -490,7 +491,7 @@ describe('Push notifications', function() {
490491
defaultUnit: 'btc',
491492
subjectPrefix: '',
492493
pushServerUrl: 'http://localhost:8000',
493-
authorizationKey: 'secret',
494+
fcmGoogleCredentialsPath: 'path_to_service_account.json'
494495
},
495496
}, function(err) {
496497
should.not.exist(err);
@@ -528,7 +529,7 @@ describe('Push notifications', function() {
528529
var args = _.filter(_.map(calls, function(call) {
529530
return call.args[0];
530531
}), function(arg) {
531-
return arg.body.notification.title == 'New copayer';
532+
return arg.body.message.notification.title == 'New copayer';
532533
});
533534

534535
server.getWallet(null, function(err, wallet) {
@@ -540,24 +541,24 @@ describe('Push notifications', function() {
540541
var hashedCopayerIds = _.map(wallet.copayers, function(copayer) {
541542
return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(copayer.id));
542543
});
543-
hashedCopayerIds[0].should.equal((args[0].body.data.copayerId));
544-
hashedCopayerIds[1].should.not.equal((args[0].body.data.copayerId));
544+
hashedCopayerIds[0].should.equal((args[0].body.message.data.copayerId));
545+
hashedCopayerIds[1].should.not.equal((args[0].body.message.data.copayerId));
545546

546547
/*
547548
Second call - copayer3 joined
548549
copayer3 should notify to copayer1
549550
*/
550-
hashedCopayerIds[0].should.equal((args[1].body.data.copayerId));
551+
hashedCopayerIds[0].should.equal((args[1].body.message.data.copayerId));
551552

552553
/*
553554
Third call - copayer3 joined
554555
copayer3 should notify to copayer2
555556
*/
556-
hashedCopayerIds[1].should.equal((args[2].body.data.copayerId));
557+
hashedCopayerIds[1].should.equal((args[2].body.message.data.copayerId));
557558

558559
// copayer3 should NOT notify any other copayer
559-
hashedCopayerIds[2].should.not.equal((args[1].body.data.copayerId));
560-
hashedCopayerIds[2].should.not.equal((args[2].body.data.copayerId));
560+
hashedCopayerIds[2].should.not.equal((args[1].body.message.data.copayerId));
561+
hashedCopayerIds[2].should.not.equal((args[2].body.message.data.copayerId));
561562
done();
562563
});
563564
}, 100);
@@ -614,7 +615,7 @@ describe('Push notifications', function() {
614615
defaultUnit: 'eth',
615616
subjectPrefix: '',
616617
pushServerUrl: 'http://localhost:8000',
617-
authorizationKey: 'secret',
618+
fcmGoogleCredentialsPath: 'path_to_service_account.json'
618619
},
619620
}, function(err) {
620621
should.not.exist(err);
@@ -648,10 +649,10 @@ describe('Push notifications', function() {
648649
var args = _.map(_.takeRight(calls, 2), function(c) {
649650
return c.args[0];
650651
});
651-
args[0].body.notification.title.should.contain('New payment received');
652-
args[0].body.notification.title.should.contain('New payment received');
653-
args[0].body.notification.body.should.contain('4.00');
654-
args[0].body.data.tokenAddress.should.equal('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48');
652+
args[0].body.message.notification.title.should.contain('New payment received');
653+
args[0].body.message.notification.title.should.contain('New payment received');
654+
args[0].body.message.notification.body.should.contain('4.00');
655+
args[0].body.message.data.tokenAddress.should.equal('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48');
655656
done();
656657
}, 100);
657658
});
@@ -681,9 +682,9 @@ describe('Push notifications', function() {
681682
var args = _.map(_.takeRight(calls, 2), function(c) {
682683
return c.args[0];
683684
});
684-
args[0].body.notification.title.should.contain('Nuevo pago recibido');
685-
args[0].body.notification.body.should.contain('4.00');
686-
args[0].body.data.tokenAddress.should.equal('0x8E870D67F660D95d5be530380D0eC0bd388289E1');
685+
args[0].body.message.notification.title.should.contain('Nuevo pago recibido');
686+
args[0].body.message.notification.body.should.contain('4.00');
687+
args[0].body.message.data.tokenAddress.should.equal('0x8E870D67F660D95d5be530380D0eC0bd388289E1');
687688
done();
688689
}, 100);
689690
});
@@ -713,9 +714,9 @@ describe('Push notifications', function() {
713714
var args = _.map(_.takeRight(calls, 2), function(c) {
714715
return c.args[0];
715716
});
716-
args[0].body.notification.title.should.contain('New payment received');
717-
args[0].body.notification.body.should.contain('4.00');
718-
args[0].body.data.tokenAddress.should.equal('0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd');
717+
args[0].body.message.notification.title.should.contain('New payment received');
718+
args[0].body.message.notification.body.should.contain('4.00');
719+
args[0].body.message.data.tokenAddress.should.equal('0x056Fd409E1d7A124BD7017459dFEa2F387b6d5Cd');
719720
done();
720721
}, 100);
721722
});

0 commit comments

Comments
 (0)