Skip to content

Commit

Permalink
Fix check for paid donation for wallet address
Browse files Browse the repository at this point in the history
  • Loading branch information
minibits-cash committed Mar 1, 2024
1 parent 80a4aff commit 8f77439
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 34 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minibits_wallet",
"version": "0.1.6-beta.30",
"version": "0.1.6-beta.31",
"private": true,
"scripts": {
"android:clean": "cd android && ./gradlew clean",
Expand Down
6 changes: 6 additions & 0 deletions src/models/RelaysStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {RelayModel, Relay} from './Relay'
import {log} from '../services/logService'
import { MINIBITS_RELAY_URL } from '@env'
import { NostrClient } from '../services'
import AppError, { Err } from '../utils/AppError'


export const RelaysStoreModel = types
Expand All @@ -37,6 +38,11 @@ export const RelaysStoreModel = types
addRelay(relay: Relay) {

const normalized = NostrClient.getNormalizedRelayUrl(relay.url)

if(!normalized.startsWith('wss://')) {
throw new AppError(Err.VALIDATION_ERROR, 'Relay needs to communicate over secure websocket wss://', {caller: 'addRelay'})
}

relay.url = normalized

if(self.alreadyExists(relay.url)) {
Expand Down
54 changes: 30 additions & 24 deletions src/screens/Contacts/OwnName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const OwnName = observer(function (props: {navigation: any, pubkey: strin
const [isQRcodeVisible, setIsQRCodeVisible] = useState(false)
const [isChecked, setIsChecked] = useState(false)
// const [isNameInputEnabled, setIsNameInputEnabled] = useState(true)
const [isPaidFromWallet, setIsPaidFromWallet] = useState(false)
const [isInvoicePaid, setIsInvoicePaid] = useState<boolean>(false)
const [isResultModalVisible, setIsResultModalVisible] = useState<boolean>(false)
const [resultModalInfo, setResultModalInfo] = useState<
{status: TransactionStatus, message: string} | undefined
Expand Down Expand Up @@ -76,8 +76,7 @@ export const OwnName = observer(function (props: {navigation: any, pubkey: strin
interval: 2 * 1000, // every 2s to make it responsive. Total 4 min
maxPolls: 120,
maxErrors: 10
},
{unpaidInvoice: donationInvoice})
})
.then(() => log.trace('[checkDonationPaid]', 'Polling completed'))
.catch(error =>
log.trace('[checkDonationPaid]', error.message),
Expand All @@ -93,25 +92,43 @@ export const OwnName = observer(function (props: {navigation: any, pubkey: strin
}, [donationInvoice])


useEffect(() => {
const handleIsInvoicePaid = async () => {
if(!isInvoicePaid || !donationInvoice) {
return
}

stopPolling(`checkDonationPaidPoller-${donationInvoice.payment_hash}`)
setResultModalInfo({
status: TransactionStatus.COMPLETED,
message: `Thank you! Donation for ${ownName+MINIBITS_NIP05_DOMAIN} has been successfully paid.`
})
toggleResultModal()
resetState()
}
handleIsInvoicePaid()
return () => {
}
}, [isInvoicePaid])



const togglePaymentModal = () =>
setIsPaymentModalVisible(previousState => !previousState)
const toggleResultModal = () =>
setIsResultModalVisible(previousState => !previousState)


const resetState = function () {
// setIsNameInputEnabled(true)
const resetState = function () {
setIsChecked(false)
setOwnName('')
setInfo('')
setIsLoading(false)
setIsPaymentModalVisible(false)
setDonationInvoice(undefined)
setDonationAmount(DEFAULT_DONATION_AMOUNT)
setIsResultModalVisible(false)
setDonationAmount(DEFAULT_DONATION_AMOUNT)
setIsQRCodeVisible(false)
setIsPaidFromWallet(false)
setIsInvoicePaid(false)
}


Expand Down Expand Up @@ -182,7 +199,6 @@ export const OwnName = observer(function (props: {navigation: any, pubkey: strin

const onPayDonation = async function () {
try {
setIsPaidFromWallet(true)
return navigation.navigate('WalletNavigator', {
screen: 'Transfer',
params: {
Expand All @@ -197,32 +213,22 @@ export const OwnName = observer(function (props: {navigation: any, pubkey: strin
}

// poll handler
const checkDonationPaid = async function (params: {unpaidInvoice: {payment_hash: string, payment_request: string}}): Promise<void> {
const checkDonationPaid = async function (): Promise<void> {
try {
const {unpaidInvoice} = params

if(!unpaidInvoice) {
if(!donationInvoice) {
return
}

const { paid } = await MinibitsClient.checkDonationPaid(
unpaidInvoice.payment_hash as string,
donationInvoice.payment_hash as string,
pubkey as string
)

if(paid) {
setIsLoading(true)

setIsLoading(true)
await walletProfileStore.updateName(ownName)

setIsLoading(false)
setResultModalInfo({
status: TransactionStatus.COMPLETED,
message: `Thank you! Donation for ${ownName+MINIBITS_NIP05_DOMAIN} has been successfully paid.`
})
toggleResultModal()
togglePaymentModal()
stopPolling(`checkDonationPaidPoller-${unpaidInvoice.payment_hash}`)
setIsInvoicePaid(true)
return
}
} catch (e: any) {
Expand Down
1 change: 1 addition & 0 deletions src/screens/Contacts/RandomName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ export const RandomName = observer(function (props: {navigation: any, pubkey: st
</View>
{error && <ErrorModal error={error} />}
{info && <InfoModal message={info} />}
{isLoading && <Loading/>}
</Screen>
)
})
Expand Down
8 changes: 6 additions & 2 deletions src/screens/OwnKeysScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,12 @@ export const OwnKeysScreen: FC<OwnKeysScreenProps> = observer(function OwnKeysSc
const relaysToConnect = relaysStore.allPublicUrls
setOwnProfileRelays(relaysToConnect)

const profile: NostrProfile = await NostrClient.getProfileFromRelays(nip05Pubkey, relaysToConnect)

const profile: NostrProfile | undefined = await NostrClient.getProfileFromRelays(nip05Pubkey, relaysToConnect)

if(!profile) {
throw new AppError(Err.VALIDATION_ERROR, 'Could not retrieve profile from relays', {nip05Pubkey, relaysToConnect})
}

// check that the profile's nip05 matches the one given by user and living on nip05 .well-known server
if(!profile.nip05) {
if(profile.name && profile.name.toLowerCase() === nip05Name) {
Expand Down
2 changes: 1 addition & 1 deletion src/screens/WalletNameScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const WalletNameScreen: FC<WalletNameScreenProps> = observer(function Wal
})

const {walletProfileStore} = useStores()
const {pubkey, name, nip05} = walletProfileStore
const {pubkey, nip05} = walletProfileStore

const renderScene = ({route}: {route: Route}) => {
switch (route.key) {
Expand Down
9 changes: 4 additions & 5 deletions src/services/nostrService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ const getNip05PubkeyAndRelays = async function (nip05: string) {



const getProfileFromRelays = async function (pubkey: string, relays: string[]) {
const getProfileFromRelays = async function (pubkey: string, relays: string[]): Promise<NostrProfile | undefined> {

// get profile from the relays for pubkey linked to nip05
const filters: NostrFilter[] = [{
Expand All @@ -361,7 +361,9 @@ const getProfileFromRelays = async function (pubkey: string, relays: string[]) {


if(!events || events.length === 0) {
throw new AppError(Err.SERVER_ERROR, 'Could not get profile event from the relays.', {relays})
// do not log as error to save capacity
log.warn('Could not get profile event from the relays.', {relays})
return undefined
}

const profile: NostrProfile = JSON.parse(events[events.length - 1].content)
Expand Down Expand Up @@ -462,9 +464,6 @@ const deleteKeyPair = async function (): Promise<void> {

const getNormalizedRelayUrl = function (url: string): string {
try {
if(!url.startsWith('wss://')) {
throw new Error('Relay needs to communicate over wss://')
}
return utils.normalizeURL(url)
} catch (e: any) {
throw new AppError(Err.VALIDATION_ERROR, `Invalid relay URL: ${e.message}`)
Expand Down
2 changes: 1 addition & 1 deletion src/services/walletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ const checkPendingReceived = async function () {
const relays = getTagsByName(zapRequest.tags, 'relays')

if(relays && relays.length > 0) {
const senderProfile = await NostrClient.getProfileFromRelays(sentFromPubkey, relays)
const senderProfile = await NostrClient.getProfileFromRelays(sentFromPubkey, relays) // returns undefined if not found

if(senderProfile) {
sentFrom = senderProfile.nip05 || senderProfile.name
Expand Down

0 comments on commit 8f77439

Please sign in to comment.