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';
|
2020-01-01 04:31:04 +01:00
|
|
|
const bitcoin = require('bitcoinjs-lib');
|
2020-04-28 16:48:36 +02:00
|
|
|
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 {Object} event
|
|
|
|
* @param {void} completionHandler
|
|
|
|
*/
|
|
|
|
static navigationRouteFor(event, completionHandler) {
|
|
|
|
if (event.url === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (typeof event.url !== 'string') {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-03 05:02:41 +01:00
|
|
|
if (DeeplinkSchemaMatch.isPossiblyPSBTFile(event.url)) {
|
|
|
|
RNFS.readFile(event.url)
|
|
|
|
.then(file => {
|
|
|
|
if (file) {
|
|
|
|
completionHandler({
|
|
|
|
routeName: 'PsbtWithHardwareWallet',
|
|
|
|
params: {
|
|
|
|
deepLinkPSBT: file,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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) {
|
|
|
|
completionHandler({
|
|
|
|
routeName: 'HandleOffchainAndOnChain',
|
|
|
|
params: {
|
2020-01-19 19:10:06 +01:00
|
|
|
onWalletSelect: wallet =>
|
|
|
|
completionHandler(DeeplinkSchemaMatch.isBothBitcoinAndLightningOnWalletSelect(wallet, isBothBitcoinAndLightning)),
|
2020-01-01 04:31:04 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (DeeplinkSchemaMatch.isBitcoinAddress(event.url) || BitcoinBIP70TransactionDecode.matchesPaymentURL(event.url)) {
|
|
|
|
completionHandler({
|
|
|
|
routeName: 'SendDetails',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (DeeplinkSchemaMatch.isLightningInvoice(event.url)) {
|
|
|
|
completionHandler({
|
|
|
|
routeName: 'ScanLndInvoice',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (DeeplinkSchemaMatch.isLnUrl(event.url)) {
|
|
|
|
completionHandler({
|
|
|
|
routeName: 'LNDCreateInvoice',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else if (DeeplinkSchemaMatch.isSafelloRedirect(event)) {
|
|
|
|
let urlObject = url.parse(event.url, true) // eslint-disable-line
|
|
|
|
|
|
|
|
const safelloStateToken = urlObject.query['safello-state-token'];
|
|
|
|
|
|
|
|
completionHandler({
|
|
|
|
routeName: 'BuyBitcoin',
|
|
|
|
params: {
|
|
|
|
uri: event.url,
|
|
|
|
safelloStateToken,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
let urlObject = url.parse(event.url, true); // eslint-disable-line
|
|
|
|
console.log('parsed', urlObject);
|
|
|
|
(async () => {
|
|
|
|
if (urlObject.protocol === 'bluewallet:' || urlObject.protocol === 'lapp:' || urlObject.protocol === 'blue:') {
|
|
|
|
switch (urlObject.host) {
|
|
|
|
case 'openlappbrowser':
|
|
|
|
console.log('opening LAPP', urlObject.query.url);
|
|
|
|
// searching for LN wallet:
|
|
|
|
let haveLnWallet = false;
|
|
|
|
for (let w of BlueApp.getWallets()) {
|
|
|
|
if (w.type === LightningCustodianWallet.type) {
|
|
|
|
haveLnWallet = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!haveLnWallet) {
|
|
|
|
// need to create one
|
|
|
|
let w = new LightningCustodianWallet();
|
2020-01-19 15:41:23 +01:00
|
|
|
w.setLabel(w.typeReadable);
|
2020-01-01 04:31:04 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
let lndhub = await AsyncStorage.getItem(AppStorage.LNDHUB);
|
|
|
|
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 (let w of BlueApp.getWallets()) {
|
|
|
|
if (w.type === LightningCustodianWallet.type) {
|
|
|
|
lnWallet = w;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lnWallet) {
|
|
|
|
// something went wrong
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-19 15:41:23 +01:00
|
|
|
completionHandler({
|
|
|
|
routeName: 'LappBrowser',
|
|
|
|
params: {
|
|
|
|
fromSecret: lnWallet.getSecret(),
|
|
|
|
fromWallet: lnWallet,
|
|
|
|
url: urlObject.query.url,
|
|
|
|
},
|
|
|
|
});
|
2020-01-01 04:31:04 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return {
|
|
|
|
routeName: 'SendDetails',
|
|
|
|
params: {
|
|
|
|
uri: uri.bitcoin,
|
|
|
|
fromWallet: wallet,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else if (wallet.chain === Chain.OFFCHAIN) {
|
|
|
|
return {
|
|
|
|
routeName: 'ScanLndInvoice',
|
|
|
|
params: {
|
|
|
|
uri: uri.lndInvoice,
|
|
|
|
fromSecret: wallet.getSecret(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-01 04:31:04 +01:00
|
|
|
static isBitcoinAddress(address) {
|
|
|
|
address = address
|
|
|
|
.replace('bitcoin:', '')
|
2020-04-28 16:48:36 +02:00
|
|
|
.replace('BITCOIN:', '')
|
2020-01-01 04:31:04 +01:00
|
|
|
.replace('bitcoin=', '')
|
|
|
|
.split('?')[0];
|
|
|
|
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) {
|
|
|
|
let urlObject = url.parse(event.url, true) // eslint-disable-line
|
|
|
|
|
|
|
|
return !!urlObject.query['safello-state-token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
static isBothBitcoinAndLightning(url) {
|
2020-04-28 16:48:36 +02:00
|
|
|
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.
|
2020-04-28 16:48:36 +02:00
|
|
|
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;
|
|
|
|
}
|
2020-04-28 16:48:36 +02:00
|
|
|
|
|
|
|
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;
|