BlueWallet/class/deeplink-schema-match.js

315 lines
9.5 KiB
JavaScript
Raw Normal View History

2020-01-01 04:31:04 +01:00
import { AppStorage, LightningCustodianWallet } from './';
import AsyncStorage from '@react-native-community/async-storage';
import BitcoinBIP70TransactionDecode from '../bip70/bip70';
2020-01-03 05:02:41 +01:00
import RNFS from 'react-native-fs';
import url from 'url';
2020-01-19 19:10:06 +01:00
import { Chain } from '../models/bitcoinUnits';
import Azteco from './azteco';
2020-01-01 04:31:04 +01:00
const bitcoin = require('bitcoinjs-lib');
const bip21 = require('bip21');
2020-01-19 15:41:23 +01:00
const BlueApp: AppStorage = require('../BlueApp');
2020-01-01 04:31:04 +01:00
class DeeplinkSchemaMatch {
static hasSchema(schemaString) {
if (typeof schemaString !== 'string' || schemaString.length <= 0) return false;
const lowercaseString = schemaString.trim().toLowerCase();
return (
lowercaseString.startsWith('bitcoin:') ||
lowercaseString.startsWith('lightning:') ||
lowercaseString.startsWith('blue:') ||
lowercaseString.startsWith('bluewallet:') ||
lowercaseString.startsWith('lapp:')
);
}
/**
* Examines the content of the event parameter.
* If the content is recognizable, create a dictionary with the respective
* navigation dictionary required by react-navigation
*
* @param event {{url: string}} URL deeplink as passed to app, e.g. `bitcoin:bc1qh6tf004ty7z7un2v5ntu4mkf630545gvhs45u7?amount=666&label=Yo`
* @param completionHandler {function} Callback that returns [string, params: object]
2020-01-01 04:31:04 +01:00
*/
static navigationRouteFor(event, completionHandler) {
if (event.url === null) {
return;
}
if (typeof event.url !== 'string') {
return;
}
if (event.url.toLowerCase().startsWith('bluewallet:bitcoin:') || event.url.toLowerCase().startsWith('bluewallet:lightning:')) {
event.url = event.url.substring(11);
}
2020-01-03 05:02:41 +01:00
if (DeeplinkSchemaMatch.isPossiblyPSBTFile(event.url)) {
RNFS.readFile(event.url)
.then(file => {
if (file) {
2020-05-27 13:12:17 +02:00
completionHandler([
'SendDetailsRoot',
{
screen: 'PsbtWithHardwareWallet',
params: {
deepLinkPSBT: file,
},
2020-01-03 05:02:41 +01:00
},
2020-05-27 13:12:17 +02:00
]);
2020-01-03 05:02:41 +01:00
}
})
.catch(e => console.warn(e));
return;
}
2020-01-01 04:31:04 +01:00
let isBothBitcoinAndLightning;
try {
isBothBitcoinAndLightning = DeeplinkSchemaMatch.isBothBitcoinAndLightning(event.url);
} catch (e) {
console.log(e);
}
if (isBothBitcoinAndLightning) {
2020-05-27 13:12:17 +02:00
completionHandler([
'SelectWallet',
2020-05-27 13:12:17 +02:00
{
onWalletSelect: (wallet, { navigation }) => {
navigation.pop(); // close select wallet screen
navigation.navigate(...DeeplinkSchemaMatch.isBothBitcoinAndLightningOnWalletSelect(wallet, isBothBitcoinAndLightning));
2020-05-27 13:12:17 +02:00
},
2020-01-01 04:31:04 +01:00
},
2020-05-27 13:12:17 +02:00
]);
2020-01-01 04:31:04 +01:00
} else if (DeeplinkSchemaMatch.isBitcoinAddress(event.url) || BitcoinBIP70TransactionDecode.matchesPaymentURL(event.url)) {
2020-05-27 13:12:17 +02:00
completionHandler([
'SendDetailsRoot',
{
screen: 'SendDetails',
params: {
uri: event.url,
},
2020-01-01 04:31:04 +01:00
},
2020-05-27 13:12:17 +02:00
]);
2020-01-01 04:31:04 +01:00
} else if (DeeplinkSchemaMatch.isLightningInvoice(event.url)) {
2020-05-27 13:12:17 +02:00
completionHandler([
'ScanLndInvoiceRoot',
{
screen: 'ScanLndInvoice',
params: {
uri: event.url,
},
2020-01-01 04:31:04 +01:00
},
2020-05-27 13:12:17 +02:00
]);
2020-01-01 04:31:04 +01:00
} else if (DeeplinkSchemaMatch.isLnUrl(event.url)) {
2020-05-27 13:12:17 +02:00
completionHandler([
'LNDCreateInvoiceRoot',
{
screen: 'LNDCreateInvoice',
params: {
uri: event.url,
},
2020-01-01 04:31:04 +01:00
},
2020-05-27 13:12:17 +02:00
]);
2020-01-01 04:31:04 +01:00
} else if (DeeplinkSchemaMatch.isSafelloRedirect(event)) {
const urlObject = url.parse(event.url, true); // eslint-disable-line node/no-deprecated-api
2020-01-01 04:31:04 +01:00
const safelloStateToken = urlObject.query['safello-state-token'];
2020-06-15 18:07:46 +02:00
let wallet;
for (const w of BlueApp.getWallets()) {
wallet = w;
break;
}
2020-01-01 04:31:04 +01:00
2020-05-27 13:12:17 +02:00
completionHandler([
'BuyBitcoin',
{
2020-01-01 04:31:04 +01:00
uri: event.url,
safelloStateToken,
2020-06-15 18:07:46 +02:00
wallet,
2020-01-01 04:31:04 +01:00
},
2020-05-27 13:12:17 +02:00
]);
} else if (Azteco.isRedeemUrl(event.url)) {
completionHandler([
'AztecoRedeemRoot',
{
screen: 'AztecoRedeem',
params: Azteco.getParamsFromUrl(event.url),
},
]);
2020-01-01 04:31:04 +01:00
} else {
const urlObject = url.parse(event.url, true); // eslint-disable-line node/no-deprecated-api
console.log('parsed', event.url, 'into', urlObject);
2020-01-01 04:31:04 +01:00
(async () => {
if (urlObject.protocol === 'bluewallet:' || urlObject.protocol === 'lapp:' || urlObject.protocol === 'blue:') {
switch (urlObject.host) {
case 'openlappbrowser': {
2020-01-01 04:31:04 +01:00
console.log('opening LAPP', urlObject.query.url);
// searching for LN wallet:
let haveLnWallet = false;
for (const w of BlueApp.getWallets()) {
2020-01-01 04:31:04 +01:00
if (w.type === LightningCustodianWallet.type) {
haveLnWallet = true;
}
}
if (!haveLnWallet) {
// need to create one
const w = new LightningCustodianWallet();
2020-01-19 15:41:23 +01:00
w.setLabel(w.typeReadable);
2020-01-01 04:31:04 +01:00
try {
const lndhub = await AsyncStorage.getItem(AppStorage.LNDHUB);
2020-01-01 04:31:04 +01:00
if (lndhub) {
w.setBaseURI(lndhub);
w.init();
}
await w.createAccount();
await w.authorize();
} catch (Err) {
// giving up, not doing anything
return;
}
BlueApp.wallets.push(w);
await BlueApp.saveToDisk();
}
// now, opening lapp browser and navigating it to URL.
// looking for a LN wallet:
let lnWallet;
for (const w of BlueApp.getWallets()) {
2020-01-01 04:31:04 +01:00
if (w.type === LightningCustodianWallet.type) {
lnWallet = w;
break;
}
}
if (!lnWallet) {
// something went wrong
return;
}
2020-05-27 13:12:17 +02:00
completionHandler([
'LappBrowser',
{
2020-01-19 15:41:23 +01:00
fromSecret: lnWallet.getSecret(),
fromWallet: lnWallet,
url: urlObject.query.url,
},
2020-05-27 13:12:17 +02:00
]);
2020-01-01 04:31:04 +01:00
break;
}
2020-01-01 04:31:04 +01:00
}
}
})();
}
}
2020-01-04 04:12:29 +01:00
static isTXNFile(filePath) {
return filePath.toLowerCase().startsWith('file:') && filePath.toLowerCase().endsWith('.txn');
}
2020-01-03 05:02:41 +01:00
static isPossiblyPSBTFile(filePath) {
return filePath.toLowerCase().startsWith('file:') && filePath.toLowerCase().endsWith('-signed.psbt');
}
2020-01-19 19:10:06 +01:00
static isBothBitcoinAndLightningOnWalletSelect(wallet, uri) {
if (wallet.chain === Chain.ONCHAIN) {
2020-05-27 13:12:17 +02:00
return [
'SendDetailsRoot',
2020-05-27 13:12:17 +02:00
{
screen: 'SendDetails',
params: {
uri: uri.bitcoin,
fromWallet: wallet,
},
2020-01-19 19:10:06 +01:00
},
2020-05-27 13:12:17 +02:00
];
2020-01-19 19:10:06 +01:00
} else if (wallet.chain === Chain.OFFCHAIN) {
2020-05-27 13:12:17 +02:00
return [
'ScanLndInvoiceRoot',
2020-05-27 13:12:17 +02:00
{
screen: 'ScanLndInvoice',
params: {
uri: uri.lndInvoice,
fromSecret: wallet.getSecret(),
},
2020-01-19 19:10:06 +01:00
},
2020-05-27 13:12:17 +02:00
];
2020-01-19 19:10:06 +01:00
}
}
2020-01-01 04:31:04 +01:00
static isBitcoinAddress(address) {
address = address.replace('bitcoin:', '').replace('BITCOIN:', '').replace('bitcoin=', '').split('?')[0];
2020-01-01 04:31:04 +01:00
let isValidBitcoinAddress = false;
try {
bitcoin.address.toOutputScript(address);
isValidBitcoinAddress = true;
} catch (err) {
isValidBitcoinAddress = false;
}
return isValidBitcoinAddress;
}
static isLightningInvoice(invoice) {
let isValidLightningInvoice = false;
if (invoice.toLowerCase().startsWith('lightning:lnb') || invoice.toLowerCase().startsWith('lnb')) {
isValidLightningInvoice = true;
}
return isValidLightningInvoice;
}
static isLnUrl(text) {
if (text.toLowerCase().startsWith('lightning:lnurl') || text.toLowerCase().startsWith('lnurl')) {
return true;
}
return false;
}
static isSafelloRedirect(event) {
const urlObject = url.parse(event.url, true); // eslint-disable-line node/no-deprecated-api
2020-01-01 04:31:04 +01:00
return !!urlObject.query['safello-state-token'];
}
static isBothBitcoinAndLightning(url) {
if (url.includes('lightning') && (url.includes('bitcoin') || url.includes('BITCOIN'))) {
const txInfo = url.split(/(bitcoin:|BITCOIN:|lightning:|lightning=|bitcoin=)+/);
2020-01-01 04:31:04 +01:00
let bitcoin;
let lndInvoice;
for (const [index, value] of txInfo.entries()) {
try {
// Inside try-catch. We dont wan't to crash in case of an out-of-bounds error.
if (value.startsWith('bitcoin') || value.startsWith('BITCOIN')) {
2020-01-01 04:31:04 +01:00
bitcoin = `bitcoin:${txInfo[index + 1]}`;
if (!DeeplinkSchemaMatch.isBitcoinAddress(bitcoin)) {
bitcoin = false;
break;
}
} else if (value.startsWith('lightning')) {
lndInvoice = `lightning:${txInfo[index + 1]}`;
if (!this.isLightningInvoice(lndInvoice)) {
lndInvoice = false;
break;
}
}
} catch (e) {
console.log(e);
}
if (bitcoin && lndInvoice) break;
}
if (bitcoin && lndInvoice) {
return { bitcoin, lndInvoice };
} else {
return undefined;
}
}
return undefined;
}
static bip21decode(uri) {
return bip21.decode(uri.replace('BITCOIN:', 'bitcoin:'));
}
static bip21encode() {
return bip21.encode.apply(bip21, arguments);
}
2020-01-01 04:31:04 +01:00
}
export default DeeplinkSchemaMatch;