Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace RPC call with near-api-js when checking account existence #136

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,7 @@ function SignInPage() {
: null;
const existingDeviceLakKey = existingDevice?.publicKeys?.filter((key) => key !== publicKeyFak)[0];

// @ts-ignore
const oidcToken = user.accessToken;
const oidcToken = await user.getIdToken();
const recoveryPK = await window.fastAuthController.getUserCredential(oidcToken);

// if given lak key is already attached to webAuthN public key, no need to add it again
Expand All @@ -205,7 +204,7 @@ function SignInPage() {
methodNames,
allowance: new BN('250000000000000'),
publicKey: public_key,
}).then((res) => res && res.json()).then((res) => {
}).then((res) => res && res.json()).then(async (res) => {
const failure = res['Receipts Outcome'].find(({ outcome: { status } }) => Object.keys(status).some((k) => k === 'Failure'))?.outcome?.status?.Failure;
if (failure?.ActionError?.kind?.LackBalanceForState) {
navigate(`/devices?${searchParams.toString()}`);
Expand All @@ -215,7 +214,6 @@ function SignInPage() {
// Add device
window.firestoreController.updateUser({
userUid: user.uid,
// User type is missing accessToken but it exist
oidcToken,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import FormContainer from './styles/FormContainer';
import { BadgeProps } from '../../lib/Badge/Badge';
import { Button } from '../../lib/Button';
import Input from '../../lib/Input/Input';
import { openToast } from '../../lib/Toast';
import { inIframe, redirectWithError } from '../../utils';
import { network } from '../../utils/config';
import { userExists } from '../../utils/firebase';
Expand All @@ -37,44 +36,6 @@ const StyledContainer = styled.div`

const emailProviders = ['gmail', 'yahoo', 'outlook'];

const checkIsAccountAvailable = async (desiredUsername: string): Promise<boolean> => {
try {
const response = await fetch(network.nodeUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 'dontcare',
method: 'query',
params: {
request_type: 'view_account',
finality: 'final',
account_id: `${desiredUsername}.${network.fastAuth.accountIdSuffix}`,
},
}),
});
const data = await response.json();
if (data?.error?.cause?.name === 'UNKNOWN_ACCOUNT') {
return true;
}

if (data?.result?.code_hash) {
return false;
}

return false;
} catch (error: any) {
console.log(error);
openToast({
title: error.message,
type: 'ERROR'
});
return false;
}
};

const schema = yup.object().shape({
email: yup
.string()
Expand Down Expand Up @@ -110,14 +71,11 @@ const schema = yup.object().shape({
.test(
'is-account-available',
async (username, context) => {
if (username) {
const isAvailable = await checkIsAccountAvailable(username);
if (!isAvailable) {
return context.createError({
message: `${username}.${network.fastAuth.accountIdSuffix} is taken, try something else.`,
path: context.path
});
}
if (username && await window.fastAuthController.accountExist(username)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this part of logic has been re-written recently

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By what I checked, we only changed the e-mail validation logic. The username validation remains the same.

return context.createError({
message: `${username}.${network.fastAuth.accountIdSuffix} is taken, try something else.`,
path: context.path
});
}

return true;
Expand Down
15 changes: 10 additions & 5 deletions packages/near-fast-auth-signer/src/lib/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,17 @@ class FastAuthController {
return this.accountId;
}

async getAccounts() {
if (this.accountId) {
return [this.accountId];
}
async accountExist(accountId: string): Promise<boolean> {
try {
const accountState = await new Account(this.connection, accountId).state();
return !!accountState;
} catch (error) {
if (error?.type === 'AccountDoesNotExist') {
return false;
}

return [];
return true;
}
}

async signDelegateAction({ receiverId, actions, signerId }) {
Expand Down
3 changes: 3 additions & 0 deletions packages/near-fast-auth-signer/src/lib/firestoreController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ class FirestoreController {
updateUser = async ({
userUid,
oidcToken,
}: {
userUid: string;
oidcToken: string;
}) => {
this.userUid = userUid;
this.oidcToken = oidcToken;
Expand Down
1 change: 1 addition & 0 deletions packages/near-fast-auth-signer/src/lib/useAuthState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const useAuthState = (skipGetKeys = false): AuthState => {

if (accountsList.length === 0) {
setAuthenticated(false);
return;
}

window.fastAuthController = new FastAuthController({
Expand Down