Skip to content
Merged
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
29 changes: 22 additions & 7 deletions packages/bitcore-wallet-service/src/externalservices/moonpay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ export class MoonpayService {
}

moonpayGetSignedPaymentUrl(req): { urlWithSignature: string } {
// moonpayGetKeys deletes env/context from the body, so read context first
const isWebContext = req.body.context === 'web';
const keys = this.moonpayGetKeys(req);
const SECRET_KEY = keys.SECRET_KEY;
const API_KEY = keys.API_KEY;
Expand Down Expand Up @@ -225,15 +227,28 @@ export class MoonpayService {
if (req.body.paymentMethod) qs.push('paymentMethod=' + encodeURIComponent(req.body.paymentMethod));
if (req.body.areFeesIncluded) qs.push('areFeesIncluded=' + encodeURIComponent(req.body.areFeesIncluded));

const deviceIp = Utils.getIpFromReq(req);
if (!deviceIp) {
// Web requests are proxied through the bitpay backend, so the IP on this
// request belongs to that server, not the customer. The proxy captures the
// customer's public IP and forwards it as deviceIp. Web credentials are only
// held by the proxy, so the forwarded value is trusted for that context only.
// If the proxy does not forward an IP, omit allowedIpAddress rather than
// signing an IP the customer will never match.
let deviceIp = isWebContext ? req.body.deviceIp : Utils.getIpFromReq(req);
if (!deviceIp && !isWebContext) {
throw new ClientError('Could not determine device IP address');
}
const allowedIpAddress: string = Bitcore.crypto.Hash.sha256hmac(
Buffer.from(deviceIp),
Buffer.from(SECRET_KEY)
).toString('base64');
qs.push('allowedIpAddress=' + encodeURIComponent(allowedIpAddress));
if (deviceIp) {
// Canonicalize before hashing: HMAC is byte-exact and MoonPay hashes the
// plain IPv4 it observes, while dual-stack sockets report IPv4 clients as
// IPv4-mapped IPv6 (::ffff:1.2.3.4) - strip the prefix so both sides
// hash the same string
deviceIp = String(deviceIp).trim().replace(/^::ffff:/i, '');
const allowedIpAddress: string = Bitcore.crypto.Hash.sha256hmac(
Buffer.from(deviceIp),
Buffer.from(SECRET_KEY)
).toString('base64');
qs.push('allowedIpAddress=' + encodeURIComponent(allowedIpAddress));
}

const URL_SEARCH: string = `?${qs.join('&')}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,36 @@ describe('Moonpay integration', () => {
}
});

it('should hash the forwarded deviceIp instead of the request IP for web context', () => {
req.body.context = 'web';
req.body.deviceIp = '203.0.113.42';
const data = server.externalServices.moonpay.moonpayGetSignedPaymentUrl(req);
should.exist(data.urlWithSignature);
data.urlWithSignature.should.equal('widgetApi4?apiKey=apiKey4&currencyCode=btc&walletAddress=bitcoin%3A123123&baseCurrencyCode=usd&baseCurrencyAmount=500&externalTransactionId=123123&redirectURL=bitpay%3A%2F%2Fmoonpay&allowedIpAddress=HkPyqsZMUzAgsEx27Tlz%2B5XfZHaH0fSfWV%2FMKR7JAPc%3D&signature=B%2Bw0TTQiy8%2Ffq6QeoeSf4dKpdPHZ%2F2EnBB1S4UotNGM%3D');
});

it('should canonicalize IPv4-mapped IPv6 deviceIp before hashing', () => {
req.body.context = 'web';
req.body.deviceIp = '::ffff:203.0.113.42';
const data = server.externalServices.moonpay.moonpayGetSignedPaymentUrl(req);
should.exist(data.urlWithSignature);
data.urlWithSignature.should.equal('widgetApi4?apiKey=apiKey4&currencyCode=btc&walletAddress=bitcoin%3A123123&baseCurrencyCode=usd&baseCurrencyAmount=500&externalTransactionId=123123&redirectURL=bitpay%3A%2F%2Fmoonpay&allowedIpAddress=HkPyqsZMUzAgsEx27Tlz%2B5XfZHaH0fSfWV%2FMKR7JAPc%3D&signature=B%2Bw0TTQiy8%2Ffq6QeoeSf4dKpdPHZ%2F2EnBB1S4UotNGM%3D');
});

it('should omit allowedIpAddress for web context when no deviceIp is forwarded', () => {
req.body.context = 'web';
const data = server.externalServices.moonpay.moonpayGetSignedPaymentUrl(req);
should.exist(data.urlWithSignature);
data.urlWithSignature.should.equal('widgetApi4?apiKey=apiKey4&currencyCode=btc&walletAddress=bitcoin%3A123123&baseCurrencyCode=usd&baseCurrencyAmount=500&externalTransactionId=123123&redirectURL=bitpay%3A%2F%2Fmoonpay&signature=13Q%2BET1UQLnCqCyg3stDAN4%2FTQ8QB009LcuAP1y6B%2FI%3D');
});

it('should ignore a body deviceIp for non-web context and use the request IP', () => {
req.body.deviceIp = '203.0.113.42';
const data = server.externalServices.moonpay.moonpayGetSignedPaymentUrl(req);
should.exist(data.urlWithSignature);
data.urlWithSignature.should.equal('widgetApi2?apiKey=apiKey2&currencyCode=btc&walletAddress=bitcoin%3A123123&baseCurrencyCode=usd&baseCurrencyAmount=500&externalTransactionId=123123&redirectURL=bitpay%3A%2F%2Fmoonpay&allowedIpAddress=CN35SFB5PKS4vkiZ4CglTxRgTAaUHBLGZcenAw6gHEY%3D&signature=3XxjRX3EMj2RNaoAwgOwFBOiVTXsgAS7C50uJf9SsvM%3D');
});

it('should return error if there is some missing arguments', () => {
delete req.body.currencyCode;
try {
Expand Down
Loading