Skip to content

Commit

Permalink
protocol handler and camera issue
Browse files Browse the repository at this point in the history
  • Loading branch information
luckyboy1004 committed Oct 19, 2023
1 parent e7f8d4f commit 566fb39
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 48 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,5 @@
<!-- Permissions -->

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
</manifest>
8 changes: 4 additions & 4 deletions dist/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css?family=Montserrat" rel="stylesheet">
<script type="module" crossorigin src="/assets/index-00abf907.js"></script>
<link rel="stylesheet" href="/assets/index-11a54805.css">
<script type="module" crossorigin src="/assets/index-27d73044.js"></script>
<link rel="stylesheet" href="/assets/index-b8df434d.css">
<script type="module">import.meta.url;import("_").catch(()=>1);async function* g(){};if(location.protocol!="file:"){window.__vite_is_modern_browser=true}</script>
<script type="module">!function(){if(window.__vite_is_modern_browser)return;console.warn("vite: loading legacy chunks, syntax error above and the same error below should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))},document.body.appendChild(n)}();</script>
</head>
<body>
<div id="root"></div>

<script nomodule>!function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}();</script>
<script nomodule crossorigin id="vite-legacy-polyfill" src="/assets/polyfills-legacy-39662de5.js"></script>
<script nomodule crossorigin id="vite-legacy-entry" data-src="/assets/index-legacy-5bc6d909.js">System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))</script>
<script nomodule crossorigin id="vite-legacy-polyfill" src="/assets/polyfills-legacy-ad344452.js"></script>
<script nomodule crossorigin id="vite-legacy-entry" data-src="/assets/index-legacy-df3182fe.js">System.import(document.getElementById('vite-legacy-entry').getAttribute('data-src'))</script>
</body>
</html>
58 changes: 53 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@capacitor/preferences": "^5.0.6",
"@capacitor/status-bar": "5.0.6",
"@devmehq/react-qr-code": "^1.0.6",
"@ionic-native/camera": "^5.36.0",
"@ionic/pwa-elements": "^3.2.2",
"@ionic/react": "^7.0.0",
"@ionic/react-router": "^7.0.0",
Expand Down
8 changes: 0 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ import { Prefs } from './Pages/Prefs';
import { Contacts } from './Pages/Contacts';
import { useEffect } from 'react';
import AppUrlListener from './Hooks/appUrlListener';
import { Bitcoin } from './Pages/Bitcoin';
import { Lightning } from './Pages/Lightning';

setupIonicReact();

Expand Down Expand Up @@ -82,12 +80,6 @@ const App: React.FC = () => {
<Route exact path="/">
<NodeUp />
</Route>
<Route exact path="/bitcoin">
<Bitcoin />
</Route>
<Route exact path="/lightning">
<Lightning />
</Route>
<Route exact path="/loader">
<Loader />
</Route>
Expand Down
47 changes: 39 additions & 8 deletions src/Hooks/appUrlListener.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,52 @@
import React, { useEffect, useState } from 'react';
import { useHistory } from 'react-router-dom';
import { useHistory, useLocation } from 'react-router-dom';
import { App, URLOpenListenerEvent } from '@capacitor/app';
import { useIonRouter } from '@ionic/react';
import { bech32 } from 'bech32';
import { Buffer } from 'buffer';

const AppUrlListener: React.FC<any> = () => {
const router = useIonRouter();
let history = useHistory();

const requestTag = {
lnurlPay: "pay",
lnurlWithdraw: "withdraw",
}

useEffect(() => {
App.addListener('appUrlOpen', (event: URLOpenListenerEvent) => {
router.push("/bitcoin")
if (event.url.includes("bitcoin:")) {

}else if (event.url.includes("lightning:")) {

}
recogParam(event.url)
});
}, []);

const recogParam = (param: string) => {
const paramArr = param.split(":");
switch (paramArr[0]) {
case "bitcoin":
router.push("/send?url="+paramArr[1])
break;
case "lightning":
decodeLNURL(paramArr[1])
break;
}
}
//when get lnurl protocol
const decodeLNURL = (lnurl:string) => {
try {
let { words: dataPart } = bech32.decode(lnurl, 2000);
let sourceURL = bech32.fromWords(dataPart);
const lnurlLink = Buffer.from(sourceURL).toString();

if (lnurlLink.includes(requestTag.lnurlPay)) {
router.push("/send?url="+lnurl)
} else if (lnurlLink.includes(requestTag.lnurlWithdraw)) {
router.push("/sources?url="+lnurl)
}
} catch (error) {
console.log(error);
// router.push("/send?url="+lnurl);
}
}

return null;
};
Expand Down
10 changes: 0 additions & 10 deletions src/Pages/Bitcoin/index.tsx

This file was deleted.

10 changes: 0 additions & 10 deletions src/Pages/Lightning/index.tsx

This file was deleted.

20 changes: 18 additions & 2 deletions src/Pages/Scan/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useEffect, useState } from "react";
import QrReader from "reactjs-qr-reader";
import { PageProps, SpendFrom } from "../../globalTypes";
import { notification } from 'antd';

import { Camera, CameraOptions, DestinationType, EncodingType, MediaType } from '@ionic-native/camera';
//It import svg icons library
import * as Icons from "../../Assets/SvgIconLibrary";
import { bech32 } from "bech32";
Expand Down Expand Up @@ -105,8 +105,24 @@ export const Scan = () => {
router.push("/sources")
}

useEffect(() => {
const requestCameraPermission = async () => {
try {
const cameraOptions: CameraOptions = {
quality: 90,
destinationType: DestinationType.DATA_URL,
encodingType: EncodingType.JPEG,
mediaType: MediaType.PICTURE,
};

const imageData = await Camera.getPicture(cameraOptions);
// Process the captured image data as needed
} catch (error) {
// Handle any errors that occur during camera access
}
};

useEffect(() => {
requestCameraPermission();
})

if (error !== '') {
Expand Down
9 changes: 8 additions & 1 deletion src/Pages/Send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import axios from 'axios';
import { useIonRouter } from '@ionic/react';
import { Modal } from '../../Components/Modals/Modal';
import SpendFromDropdown from '../../Components/Dropdowns/SpendFromDropdown';
import { useLocation } from 'react-router-dom';

type PayInvoice = {
type: 'payInvoice'
Expand All @@ -25,6 +26,10 @@ type PayAddress = {
}

export const Send = () => {
//parameter in url when click protocol
const addressSearch = new URLSearchParams(useLocation().search);;
const urlParam = addressSearch.get("url");

const price = useSelector((state: any) => state.usdToBTC);

//reducer
Expand Down Expand Up @@ -67,7 +72,9 @@ export const Send = () => {
}, 1000);
return openNotification("top", "Error", "You don't have any source!");
}
});

setTo(urlParam??"")
}, []);

const ChainAdress = async () => {
if (!nostrSource.length) return;
Expand Down
12 changes: 12 additions & 0 deletions src/Pages/Sources/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ import { NOSTR_PUB_DESTINATION, options } from '../../constants';
import BootstrapSource from "../../Assets/Images/bootstrap_source.jpg";
import { getNostrClient } from '../../Api/nostr';
import { nip19 } from 'nostr-tools';
import { useLocation } from 'react-router-dom';

export const Sources = () => {
//parameter in url when click protocol
const addressSearch = new URLSearchParams(useLocation().search);;
const urlParam = addressSearch.get("url");

//declaration about reducer
const dispatch = useDispatch();
Expand Down Expand Up @@ -468,8 +472,16 @@ export const Sources = () => {
});
}

const detectProtocol = () => {
if (urlParam) {
AddSource_Modal();
setSourcePasteField(urlParam);
}
}

useEffect(() => {
resetSpendFrom();
detectProtocol();
setPayToLists(paySources);
setSpendFromLists(spendSources);
window.addEventListener("touchstart", setPosition);
Expand Down

0 comments on commit 566fb39

Please sign in to comment.