Skip to content

Commit

Permalink
Add account validity check
Browse files Browse the repository at this point in the history
  • Loading branch information
jribbink committed Oct 17, 2023
1 parent 81259c9 commit 1cbfa3a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ export const CREATE_ACCOUNT_ERROR = "Account creation has failed"
export const FUND_ACCOUNT_ERROR = "Account funding has failed"
export const MISSING_FUSD_VAULT_ERROR =
"This account does not have an FUSD vault"
export const INVALID_NETWORK_ADDRESS_ERROR = (network: Networks) =>
`This address is invalid for ${network}, please verify that it is correct.`

export const paths = {
root: "/",
fundAccount: "/fund-account",
}

export const ADDRESS_REGEXP = /^(0x)?[0-9a-fA-F]{16}$/
3 changes: 2 additions & 1 deletion lib/validate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ACCOUNTS_KEYS_DOCS_URL,
ADDRESS_FORMAT_ERROR,
ADDRESS_MISSING_ERROR,
ADDRESS_REGEXP,
GENERATE_KEYS_DOCS_URL,
PUBLIC_KEY_FORMAT_ERROR,
PUBLIC_KEY_MISSING_ERROR,
Expand Down Expand Up @@ -53,7 +54,7 @@ export const createAccountSchemaServer = yup
const fundAccountSchemaClientShape = {
address: yup
.string()
.matches(/^(0x)?([0-9a-f]{16})$/i, () => (
.matches(ADDRESS_REGEXP, () => (
<>
{ADDRESS_FORMAT_ERROR}{" "}
<Link href={ACCOUNTS_KEYS_DOCS_URL} target="_blank" variant="underline">
Expand Down
25 changes: 24 additions & 1 deletion pages/api/fund.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import * as fcl from "@onflow/fcl"
import * as t from "@onflow/types"
import {verify} from "hcaptcha"
import {FUSD_TYPE, MISSING_FUSD_VAULT_ERROR} from "lib/constants"
import {
FUSD_TYPE,
INVALID_NETWORK_ADDRESS_ERROR,
MISSING_FUSD_VAULT_ERROR,
} from "lib/constants"
import publicConfig from "lib/publicConfig"
import {NextApiRequest, NextApiResponse} from "next"
import config from "../../lib/config"
Expand All @@ -11,6 +15,12 @@ import {fundAccountSchemaServer} from "../../lib/validate"
import {verifyAPIKey} from "../../lib/common"
import {ValidationError} from "yup"

const scriptCheckAccount = `
pub fun main(address: Address): Bool {
return getAccount(address) != nil
}
`

const scriptCheckFUSDVault = `
import FUSD from ${publicConfig.contractFUSD}
import FungibleToken from ${publicConfig.contractFungibleToken}
Expand Down Expand Up @@ -44,6 +54,19 @@ export default async function fund(req: NextApiRequest, res: NextApiResponse) {
const address = fcl.withPrefix(req.body.address) || ""
const token = req.body.token

// Validate address
const isValidAddress = await fcl.send([
fcl.script(scriptCheckAccount),
fcl.args([fcl.arg(address, t.Address)]),
])

if (!isValidAddress) {
res
.status(400)
.json({errors: [INVALID_NETWORK_ADDRESS_ERROR(publicConfig.network)]})
return
}

if (token === FUSD_TYPE) {
try {
const hasFUSDVault = await fcl
Expand Down

0 comments on commit 1cbfa3a

Please sign in to comment.