Skip to content

Commit

Permalink
Merge pull request #1230 from edenia/fix/libre-and-ultra-testnet-crea…
Browse files Browse the repository at this point in the history
…te-account-1200

Fix/libre and ultra testnet create account 1200
  • Loading branch information
xavier506 authored Jul 11, 2023
2 parents f88c99b + 4319699 commit aad41ed
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 31 deletions.
35 changes: 23 additions & 12 deletions hapi/src/routes/create-faucet-account.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const {
eosUtil,
axiosUtil,
googleRecaptchaEnterpriseUtil,
getCreateAccountDataUtil
getCreateAccountDataUtil,
sleepFor
} = require('../utils')

module.exports = {
Expand All @@ -21,7 +22,7 @@ module.exports = {
throw Boom.badRequest('Are you a human?')
}

await eosUtil.transact(
const transaction = await eosUtil.transact(
[
{
authorization: [
Expand All @@ -43,25 +44,35 @@ module.exports = {
eosConfig.faucet.password
)

await sleepFor(1)

const {
data: { account_name: account, permissions }
data: { accounts }
} = await axiosUtil.instance.post(
`${eosConfig.apiEndpoint}/v1/chain/get_account`,
`${eosConfig.apiEndpoint}/v1/chain/get_accounts_by_authorizers`,
{
account_name: input.name
keys: [input.public_key]
}
)

const keys = permissions[0]?.required_auth?.keys || []
const key = keys[0]?.key

if (account === input.name && key === input.public_key) {
return { account }
if (!input.name) {
input.name = transaction?.processed?.action_traces[0]?.act?.data?.name
}

return Boom.badData('Wrong key format')
const newAccount = accounts.find(
(account) => account.account_name === input.name || !input.name
)

if (!newAccount) throw new Error('Account creation failed')

return { account: newAccount.account_name }
} catch (err) {
throw Boom.badRequest(err.message)
const errorDetail =
err?.response?.data?.error?.details[0]?.message?.substring(0, 11)

return errorDetail === 'unknown key'
? Boom.badRequest('Account creation failed')
: Boom.badRequest(err.message)
}
},
options: {
Expand Down
10 changes: 7 additions & 3 deletions webapp/src/gql/faucet.gql.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import gql from 'graphql-tag'

export const CREATE_ACCOUNT_MUTATION = gql`
mutation ($token: String!, $public_key: String!, $name: String) {
createAccount(token: $token, public_key: $public_key, name: $name) {
export const CREATE_ACCOUNT_MUTATION = (includeName = true) => gql`
mutation ($token: String!, $public_key: String! ${
includeName ? ', $name: String' : ''
}) {
createAccount(token: $token, public_key: $public_key ${
includeName ? ', name: $name' : ''
}) {
account
}
}
Expand Down
62 changes: 46 additions & 16 deletions webapp/src/routes/Faucet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const Faucet = () => {
const [createAccountValues, setCreateAccountValues] = useState({})
const [transferTokensTransaction, setTransferTokensTransaction] = useState('')
const [createFaucetAccount, { loading: loadingCreateAccount }] = useMutation(
CREATE_ACCOUNT_MUTATION,
CREATE_ACCOUNT_MUTATION(eosConfig.networkName !== 'ultra-testnet'),
)
const [transferFaucetTokens, { loading: loadingTransferFaucetTokens }] =
useMutation(TRANFER_FAUCET_TOKENS_MUTATION)
Expand All @@ -39,14 +39,25 @@ const Faucet = () => {
const createAccount = async () => {
const reCaptchaToken = await executeRecaptcha?.('submit')

if (eosConfig.networkName === 'libre-testnet') {
if (
!reCaptchaToken ||
(!createAccountValues.publicKey && !createAccountValues.accountName)
)
return
} else {
if (!reCaptchaToken || !createAccountValues.publicKey) return
if (
!reCaptchaToken ||
!createAccountValues.publicKey ||
(eosConfig.networkName !== 'ultra-testnet' &&
!createAccountValues.accountName)
) {
showMessage({
type: 'error',
content: 'Fill out the fields to create an account',
})
return
}

if (!isValidAccountName(createAccountValues.accountName)) {
showMessage({
type: 'error',
content: 'Please enter a valid account name',
})
return
}

try {
Expand All @@ -58,7 +69,7 @@ const Faucet = () => {
variables: {
token: reCaptchaToken,
public_key: createAccountValues.publicKey,
name: createAccountValues.accountName || '',
name: createAccountValues.accountName,
},
})

Expand All @@ -71,10 +82,9 @@ const Faucet = () => {
),
})
} catch (err) {
const errorMessage = err.message.replace(
'GraphQL error: assertion failure with message: ',
'',
)
const errorMessage = err.message
.replace('GraphQL error:', '')
.replace('assertion failure with message: ', '')

showMessage({
type: 'error',
Expand All @@ -91,6 +101,14 @@ const Faucet = () => {

if (!reCaptchaToken || !account) return

if (!isValidAccountName(account)) {
showMessage({
type: 'error',
content: 'Please enter a valid account name',
})
return
}

try {
const result = await transferFaucetTokens({
variables: {
Expand Down Expand Up @@ -128,6 +146,12 @@ const Faucet = () => {
setAccount('')
}

const isValidAccountName = name => {
const regex = /^[.12345abcdefghijklmnopqrstuvwxyz]+$/i

return name?.length < 13 && regex.test(name)
}

return (
<div className={classes.test}>
<div className={classes.card}>
Expand All @@ -147,9 +171,11 @@ const Faucet = () => {
...{ publicKey: e.target.value },
})
}
error={!createAccountValues.publicKey}
required
/>
</div>
{eosConfig.networkName === 'libre-testnet' && (
{eosConfig.networkName !== 'ultra-testnet' && (
<div>
<TextField
key="action-field-issue-tokens"
Expand All @@ -162,6 +188,8 @@ const Faucet = () => {
...{ accountName: e.target.value },
})
}
error={!isValidAccountName(createAccountValues.accountName)}
required
/>
</div>
)}
Expand Down Expand Up @@ -195,10 +223,12 @@ const Faucet = () => {
<div>
<TextField
key="action-field-issue-tokens"
label={`Account (500 ${eosConfig.tokenSymbol})`}
label={`${t('account')} (500 ${eosConfig.tokenSymbol})`}
variant="outlined"
value={account}
onChange={(e) => setAccount(e.target.value)}
error={!isValidAccountName(account)}
required
/>
</div>
<div>
Expand Down

0 comments on commit aad41ed

Please sign in to comment.