Skip to content

Commit

Permalink
allow scaning of pda addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Oct 19, 2023
1 parent 907e73d commit c3a63f9
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/features/addressBook/AddressQrScanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import QrScanner from '@components/QrScanner'
import { HomeNavigationProp } from '../home/homeTypes'
import { useAppStorage } from '../../storage/AppStorageProvider'
import { parsePaymentLink } from '../../utils/linking'
import { solAddressIsValid } from '../../utils/accountUtils'
import { isValidPublicKey, solAddressIsValid } from '../../utils/accountUtils'

const AddressQrScanner = () => {
const { triggerNotification } = useHaptic()
Expand All @@ -19,7 +19,7 @@ const AddressQrScanner = () => {
const handleBarCodeScanned = useCallback(
async (data: string) => {
// scanned qr is an address string
if (solAddressIsValid(data)) {
if (solAddressIsValid(data) || isValidPublicKey(data)) {
setScannedAddress(data)
triggerNotification('success')
navigation.goBack()
Expand Down
11 changes: 7 additions & 4 deletions src/features/payment/PaymentScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import { useBalance } from '../../utils/Balance'
import {
accountNetType,
formatAccountAlias,
isValidPublicKey,
solAddressIsValid,
} from '../../utils/accountUtils'
import { SendDetails } from '../../utils/linking'
Expand Down Expand Up @@ -230,9 +231,9 @@ const PaymentScreen = () => {
}

if (
paymentsArr.find((p) => {
return !solAddressIsValid(p.payee)
})
paymentsArr.find(
(p) => !(solAddressIsValid(p.payee) || isValidPublicKey(p.payee)),
)
) {
console.error('Invalid address found in deep link')
return
Expand Down Expand Up @@ -418,7 +419,9 @@ const PaymentScreen = () => {
const paymentsValid =
paymentState.payments.length &&
paymentState.payments.every((p) => {
const addressValid = !!(p.address && solAddressIsValid(p.address))
const addressValid =
!!(p.address && solAddressIsValid(p.address)) ||
!!(p.address && isValidPublicKey(p.address))

const paymentValid = p.amount && p.amount?.gt(new BN(0))
return addressValid && paymentValid && !p.hasError
Expand Down
11 changes: 10 additions & 1 deletion src/utils/accountUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Address, { NetTypes as NetType, utils } from '@helium/address'
import { PublicKey } from '@solana/web3.js'
import { PUBLIC_KEY_LENGTH, PublicKey } from '@solana/web3.js'
import Bcrypt from 'bcrypt-react-native'
import BigNumber from 'bignumber.js'
import bs58 from 'bs58'
Expand Down Expand Up @@ -29,6 +29,15 @@ export const heliumAddressToSolAddress = (heliumAddress: string) => {
}
}

export const isValidPublicKey = (address: string) => {
try {
const pubKey = new PublicKey(address)
return pubKey.toBuffer().length === PUBLIC_KEY_LENGTH
} catch {
return false
}
}

export const solAddressIsValid = (address: string) => {
try {
const pubKey = new PublicKey(address)
Expand Down
3 changes: 2 additions & 1 deletion src/utils/linking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import queryString from 'query-string'
import { BurnRouteParam, PaymentRouteParam } from '../features/home/homeTypes'
import { RootStackParamList } from '../navigation/rootTypes'
import { useAccountStorage } from '../storage/AccountStorageProvider'
import { isValidPublicKey } from './accountUtils'

export const APP_LINK_SCHEME = Linking.createURL('')
export const PAYMENT_PATH = 'payment'
Expand Down Expand Up @@ -108,7 +109,7 @@ export const parseDelegate = (qrContent: string) => {
export const parsePaymentLink = (
urlOrAddress: string,
): PaymentRouteParam | undefined => {
if (Address.isValid(urlOrAddress)) {
if (Address.isValid(urlOrAddress) || isValidPublicKey(urlOrAddress)) {
const url = makePayRequestLink({ payee: urlOrAddress })
return queryString.parseUrl(url).query
}
Expand Down

0 comments on commit c3a63f9

Please sign in to comment.