diff --git a/components/PrescriptionCreator/sendPrescription.tsx b/components/PrescriptionCreator/ImportNsec.tsx similarity index 51% rename from components/PrescriptionCreator/sendPrescription.tsx rename to components/PrescriptionCreator/ImportNsec.tsx index e89e2a2..1b37eb0 100644 --- a/components/PrescriptionCreator/sendPrescription.tsx +++ b/components/PrescriptionCreator/ImportNsec.tsx @@ -1,6 +1,13 @@ -import { Button, Modal, Text, TextInput, View } from "react-native" +import { useState } from "react"; +import { Button, Modal, NativeSyntheticEvent, Text, TextInput, TextInputChangeEventData, View } from "react-native" -export const SendPrescription = ({ isVisible, onClose }: { isVisible: boolean, onClose: () => void }) => { +export const ImportNsec = ({ isVisible, onClose, onPress }: { isVisible: boolean, onClose: () => void, onPress: (nsec: `nsec1${string}`) => void }) => { + + const [nsec, setNsec] = useState(""); + + const handleNsec = (value: string) => { + setNsec(value) + } return ( - Search an npub to send prescription - + + Import Your Nsec + + - + diff --git a/components/PrescriptionCreator/index.tsx b/components/PrescriptionCreator/index.tsx index 88615a3..128b074 100644 --- a/components/PrescriptionCreator/index.tsx +++ b/components/PrescriptionCreator/index.tsx @@ -1,12 +1,14 @@ -import {Alert, Appearance, Dimensions, Image, StyleSheet, Text, View} from 'react-native'; -import {Colors} from 'react-native/Libraries/NewAppScreen'; -import {PropsWithChildren, useState} from 'react'; -import {Button, Card, Modal} from '@ant-design/react-native'; +import { Alert, Appearance, Dimensions, Image, StyleSheet, Text, View } from 'react-native'; +import { Colors } from 'react-native/Libraries/NewAppScreen'; +import { PropsWithChildren, useEffect, useState } from 'react'; +import { Button, Card, Modal } from '@ant-design/react-native'; import { V1Field } from '@formstr/sdk/dist/interfaces'; import { InputFiller } from '../Inputs/Inputs'; // import { SendPrescription } from './sendPrescription'; import { Dropdown } from 'react-native-element-dropdown'; import { SimplePool, UnsignedEvent, finalizeEvent, generateSecretKey, getPublicKey, nip04, nip19 } from 'nostr-tools'; +import EncryptedStorage from 'react-native-encrypted-storage'; +import { ImportNsec } from './ImportNsec'; type SectionProps = PropsWithChildren<{ title: string; @@ -42,7 +44,7 @@ const styles = StyleSheet.create({ const width = Dimensions.get('window').width; //full width const height = Dimensions.get('window').height -function Section({children, title}: SectionProps): React.JSX.Element { +function Section({ children, title }: SectionProps): React.JSX.Element { return ( { - if(form === null) return Loading... - const [showSendScreen, setShowSendScreen] = useState(false); +export const PrescriptionCreator = ({ form }: { form: any }) => { + if (form === null) return Loading... + const [showImportNsec, setShowImportNsec] = useState(false); + const [loggedInNpub, setLoggedInNpub] = useState("") const [selectedPharmacyId, setSelectedPharmacyId] = useState(""); const [selectedPharmacyRelays, setSelectedPharmacyRelays] = useState([]); + useEffect(() => { + async function initialize() { + let doctorCredentials = null; + try { + doctorCredentials = await EncryptedStorage.getItem("user_credentials"); + if(!doctorCredentials) { + setShowImportNsec(true) + } + else { + setLoggedInNpub(nip19.npubEncode(getPublicKey(nip19.decode(doctorCredentials).data as Uint8Array))) + } + } + catch(e) { + console.log("Error getting credentials", e) + } + } + initialize() + }, []) const renderItem = (item: any) => { - return - {item.label} - - Npub: {item.npub} - Relays: {item.relays.join(', ')} - + return + {item.label} + + Npub: {item.npub} + Relays: {item.relays.join(', ')} + } const handleLocationChange = (item: any) => { @@ -98,16 +119,25 @@ export const PrescriptionCreator = ({form} : {form: any}) => { setSelectedPharmacyRelays(item.relays) } + const handleImportNsec = (nsec: string) => { + EncryptedStorage.setItem("user_credentials", nsec) + if(nsec.startsWith('nsec1') && nsec.length !== 63) { + Alert.alert("not a valid nsec!") + return; + } + setShowImportNsec(false) + } + const sendPrescription = async () => { console.log("Will generate IDs") - const sk = generateSecretKey() + const sk = nip19.decode(await EncryptedStorage.getItem("user_credentials") as `nsec1${string}`).data as Uint8Array const pk = getPublicKey(sk) const pharmacyId = nip19.decode(selectedPharmacyId).data as string - console.log("Ids generated", sk, pk) + console.log("Got ids") const baseKind4Event: UnsignedEvent = { kind: 4, - tags: [["p", pharmacyId ]], - content: await nip04.encrypt(sk, pharmacyId, "This is a test message from PeerScribe" ), + tags: [["p", pharmacyId]], + content: await nip04.encrypt(sk, pharmacyId, "This is a test message from PeerScribe"), created_at: Math.floor(Date.now() / 1000), pubkey: pk } @@ -117,7 +147,7 @@ export const PrescriptionCreator = ({form} : {form: any}) => { await Promise.any(pool.publish(selectedPharmacyRelays, finalEvent)) console.log("Event Published") Alert.alert("Prescription Sent to the pharmacy!") - } + } return ( { }} />
- From the practice of {form.name} + From the practice of {loggedInNpub} +
- - - + + +
- + {form.fields.map((field: V1Field) => { return ( { }}> {field.answerType} */} - {}} /> + { }} /> ); })}
- {/* { setShowSendScreen(false)}}/> */} + { setShowImportNsec(false)}} onPress={handleImportNsec}/>
); diff --git a/package.json b/package.json index ea60145..7ca9367 100644 --- a/package.json +++ b/package.json @@ -21,6 +21,7 @@ "react-native": "0.73.4", "react-native-crypto": "^2.2.0", "react-native-element-dropdown": "^2.10.4", + "react-native-encrypted-storage": "^4.0.3", "react-native-get-random-values": "^1.11.0", "react-native-picker-select": "^9.0.1", "react-native-randombytes": "^3.6.1", diff --git a/yarn.lock b/yarn.lock index 8a96c88..2278b45 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6586,6 +6586,11 @@ react-native-element-dropdown@^2.10.4: dependencies: lodash "^4.17.21" +react-native-encrypted-storage@^4.0.3: + version "4.0.3" + resolved "https://registry.yarnpkg.com/react-native-encrypted-storage/-/react-native-encrypted-storage-4.0.3.tgz#2a4d65459870511e8f4ccd22f02433dab7fa5e91" + integrity sha512-0pJA4Aj2S1PIJEbU7pN/Qvf7JIJx3hFywx+i+bLHtgK0/6Zryf1V2xVsWcrD50dfiu3jY1eN2gesQ5osGxE7jA== + react-native-gesture-handler@~2.2.1: version "2.2.1" resolved "https://registry.yarnpkg.com/react-native-gesture-handler/-/react-native-gesture-handler-2.2.1.tgz#672258aa5a40c92a97c736bdde0a8bd6c8c6c692"