Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/JustaName-id/JustaName-sdk
Browse files Browse the repository at this point in the history
… into angelo/engr-730-add-openpassport-in-widget
  • Loading branch information
anthony23991 committed Jan 15, 2025
2 parents 089411a + c00615e commit a4d0a7e
Show file tree
Hide file tree
Showing 29 changed files with 1,111 additions and 6,092 deletions.
49 changes: 0 additions & 49 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@
"@docusaurus/eslint-plugin": "^3.5.2",
"@ensdomains/address-encoder": "^1.1.2",
"@ensdomains/ensjs": "4.0.2",
"@hookform/error-message": "^2.0.1",
"@hookform/resolvers": "^3.9.0",
"@inquirer/prompts": "^4.3.0",
"@justaname.id/address-resolution": "^1.1.0",
"@million/lint": "^1.0.13",
"@privy-io/react-auth": "^1.82.0",
"@privy-io/wagmi": "^0.2.12",
"@radix-ui/react-accordion": "^1.2.1",
Expand All @@ -35,25 +31,17 @@
"@radix-ui/react-tooltip": "^1.1.4",
"@rainbow-me/rainbowkit": "^2.0.2",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-json": "^6.1.0",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@storybook/addons": "7.6.17",
"@storybook/api": "7.6.17",
"@storybook/blocks": "^8.3.3",
"@storybook/icons": "^1.2.10",
"@tanstack/react-query": "^5.28.4",
"@tanstack/react-query-devtools": "^5.59.15",
"@uidotdev/usehooks": "^2.4.1",
"@uiw/color-convert": "^2.3.0",
"@uiw/react-color-chrome": "^2.3.0",
"@uiw/react-color-github": "^2.3.0",
"@uiw/react-split": "^5.9.3",
"@usecapsule/react-sdk": "^4.6.1",
"@usecapsule/wagmi-v2-integration": "^3.6.1",
"@vitejs/plugin-react-swc": "^3.5.0",
"@walletconnect/react-native-compat": "^2.11.2",
"@web3modal/wagmi-react-native": "^1.2.0",
"@xmtp/content-type-reaction": "^1.1.11",
"@xmtp/content-type-read-receipt": "^1.1.12",
"@xmtp/content-type-remote-attachment": "^1.1.12",
Expand All @@ -63,55 +51,32 @@
"axios": "^1.6.0",
"class-variance-authority": "^0.7.0",
"cors": "^2.8.5",
"create-package-json": "^1.1.0",
"cropperjs": "^1.6.2",
"crypto-js": "^4.2.0",
"dotenv": "^16.4.5",
"embla-carousel-react": "^8.3.0",
"ethers": "^6.11.1",
"expo": "~50.0.3",
"expo-linking": "^6.2.2",
"expo-splash-screen": "~0.26.1",
"expo-status-bar": "~1.11.1",
"express": "^4.18.1",
"express-session": "^1.18.0",
"handlebars": "^4.7.8",
"input-otp": "^1.2.4",
"inquirer": "^8.0.0",
"iron-session": "^8.0.3",
"lodash": "4.17.21",
"lucide-react": "^0.436.0",
"moment": "^2.30.1",
"next": "14.2.3",
"openapi-typescript": "next",
"ora": "^8.0.1",
"pino-pretty": "^11.0.0",
"postcss-import": "^16.1.0",
"posthog-js": "^1.187.2",
"prism-react-renderer": "^2.4.0",
"punycode": "^2.3.1",
"qs": "^6.12.0",
"react": "^18.3.1",
"react-code-blocks": "^0.1.6",
"react-dom": "18.3.1",
"react-hook-form": "^7.53.0",
"react-media-recorder-2": "^1.6.23",
"react-native": "0.73.2",
"react-native-svg": "14.1.0",
"react-native-svg-transformer": "1.2.0",
"react-native-url-polyfill": "^2.0.0",
"react-native-web": "~0.19.9",
"react-router-dom": "6.11.2",
"react-scan": "^0.0.35",
"react-split": "^2.0.14",
"react-timer-hook": "^3.0.8",
"react-tiny-popover": "^8.0.4",
"siwe": "^2.3.2",
"styled-components": "^6.1.12",
"tailwind-merge": "^2.5.2",
"tailwindcss-animate": "^1.0.7",
"tar-fs": "^3.0.6",
"tar-stream": "^3.1.7",
"tslib": "^2.3.0",
"vaul": "^1.1.1",
"viem": "2.x",
Expand All @@ -121,7 +86,6 @@
"devDependencies": {
"@babel/core": "^7.14.5",
"@babel/preset-react": "^7.14.5",
"@chromatic-com/storybook": "^1",
"@nx/cypress": "19.7.3",
"@nx/eslint": "19.7.3",
"@nx/eslint-plugin": "19.7.3",
Expand All @@ -137,8 +101,6 @@
"@nx/vite": "19.7.3",
"@nx/web": "19.7.3",
"@nx/webpack": "19.7.3",
"@pmmmwh/react-refresh-webpack-plugin": "^0.5.7",
"@rollup/plugin-url": "8.0.2",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^12.0.0",
"@semantic-release/exec": "^6.0.3",
Expand All @@ -162,24 +124,15 @@
"@swc/core": "1.5.29",
"@swc/helpers": "0.5.13",
"@swc/jest": "0.2.36",
"@testing-library/jest-native": "~5.4.3",
"@testing-library/react": "15.0.6",
"@testing-library/react-native": "~12.4.2",
"@types/cors": "^2.8.17",
"@types/express-session": "^1.18.0",
"@types/inquirer": "^8.0.0",
"@types/jest": "29.5.13",
"@types/node": "18.16.9",
"@types/node-fetch": "^2.6.11",
"@types/react": "18.3.1",
"@types/react-dom": "18.3.0",
"@types/tar": "^6.1.11",
"@types/tar-fs": "^2.0.4",
"@typescript-eslint/eslint-plugin": "7.16.0",
"@typescript-eslint/parser": "7.16.0",
"@vitejs/plugin-react": "^4.2.0",
"@vitest/coverage-v8": "1.3.1",
"@vitest/ui": "1.3.1",
"autoprefixer": "^10.4.20",
"babel-jest": "29.7.0",
"babel-preset-expo": "~10.0.0",
Expand All @@ -195,14 +148,12 @@
"jest": "29.7.0",
"jest-environment-jsdom": "29.7.0",
"jest-environment-node": "^29.7.0",
"jest-expo": "~50.0.1",
"jsdom": "~22.1.0",
"nx": "19.7.3",
"nx-release": "^3.3.0",
"postcss": "8.4.38",
"prettier": "^2.6.2",
"react-refresh": "^0.10.0",
"react-test-renderer": "18.2.0",
"rollup": "^4.24.0",
"rollup-plugin-postcss": "^4.0.2",
"rollup-plugin-scss": "^4.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
export * from './usePrimaryName';
export * from './usePrimaryNameBatch';
export * from './useSetPrimaryName';

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Address } from 'viem';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { getName } from '@ensdomains/ensjs/public';
import { ChainId } from '@justaname.id/sdk';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { Address } from 'viem';
import { useJustaName } from '../../providers';
import { useEnsPublicClient } from '../client/useEnsPublicClient';
import { defaultOptions } from '../../query';
import { getName } from '@ensdomains/ensjs/public';
import { useEnsPublicClient } from '../client/useEnsPublicClient';
import { PrimaryNameTaskQueue } from './primary-name-task-queue';
import { buildPrimaryNameBatchKey } from './usePrimaryNameBatch';

Expand All @@ -17,6 +17,7 @@ export interface UsePrimaryNameParams {
address?: string;
chainId?: ChainId;
enabled?: boolean;
priority?: "onChain" | "offChain";
}

export interface UsePrimaryNameResult {
Expand All @@ -40,26 +41,44 @@ export const usePrimaryName = (
params?: UsePrimaryNameParams
): UsePrimaryNameResult => {
const { chainId, justaname } = useJustaName();
const _priority = params?.priority || 'offChain';
const _enabled = params?.enabled !== undefined ? params.enabled : true;
const _chainId = params?.chainId || chainId;
const { ensClient } = useEnsPublicClient({
chainId: _chainId,
});
const queryClient = useQueryClient();

const getPrimaryName = async (
const getOnChainPrimaryName = async (
_params: getPrimaryNameParams
): Promise<string> => {

if (!ensClient) {
throw new Error('Public client not found');
}

if (!params?.address) {
throw new Error('Address is required');
}
const taskFn = () => {
return getName(ensClient, {
address: params?.address as Address,
});
};
const reverseResolution = await PrimaryNameTaskQueue.enqueue(taskFn);
if (reverseResolution && reverseResolution?.name) {
return reverseResolution.name;
}
return '';
}

let name = '';


const getOffChainPrimaryName = async (
_params: getPrimaryNameParams
): Promise<string> => {
if (!params?.address) {
throw new Error('Address is required');
}
const primaryNames = queryClient.getQueryData(
buildPrimaryNameBatchKey(_chainId)
) as Record<string, string>;
Expand All @@ -76,25 +95,35 @@ export const usePrimaryName = (
chainId: _chainId,
});
if (primaryNameGetByAddressResponse.name) {
name = primaryNameGetByAddressResponse.name;
} else {
const taskFn = () => {
if (!params?.address) {
throw new Error('Address is required');
}
return getName(ensClient, {
address: params?.address as Address,
});
};

const reverseResolution = await PrimaryNameTaskQueue.enqueue(taskFn);

if (reverseResolution && reverseResolution?.name) {
name = reverseResolution.name;
return primaryNameGetByAddressResponse.name;
}else{
return '';
}
}


const getPrimaryName = async (
_params: getPrimaryNameParams
): Promise<string> => {
let name = '';
if(_priority === 'offChain'){
name = await getOffChainPrimaryName(_params);
if(name.length > 0){
return name;
}else{
name = await getOnChainPrimaryName(_params);
return name;
}
}else{
name = await getOnChainPrimaryName(_params);
if(name.length > 0){
return name;
}else{
name = await getOffChainPrimaryName(_params);
return name;
}
return name;
};
}
};

const query = useQuery({
...defaultOptions,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
'use client';

import { SetPrimaryNameRoute } from '@justaname.id/sdk';
import { UseMutateAsyncFunction, useMutation } from '@tanstack/react-query';
import { useMemo } from 'react';
import { useJustaName, useSubnameSignature } from '../../providers';
import { useMountedAccount } from '../account/useMountedAccount';
import { usePrimaryName } from './usePrimaryName';

export interface UseSetPrimaryNameFunctionParams {
name: string;
}

export type UseSetPrimaryNameParams = Omit<
SetPrimaryNameRoute['params'],
'name'
>;

export interface UseSetPrimaryNameResult {
setPrimaryName: UseMutateAsyncFunction<
SetPrimaryNameRoute['response'],
Error,
UseSetPrimaryNameFunctionParams
>;
isSetPrimaryNamePending: boolean;
}

export const useSetPrimaryName = (
params?: UseSetPrimaryNameParams
): UseSetPrimaryNameResult => {
const { justaname, chainId } = useJustaName();
const { address } = useMountedAccount();
const { getSignature } = useSubnameSignature();
const {refetchPrimaryName} = usePrimaryName({
address: address || '',
enabled: !!address,
});
const _chainId = useMemo(
() => params?.chainId || chainId,
[params?.chainId, chainId]
);

const mutate = useMutation({
mutationFn: async (_params: UseSetPrimaryNameFunctionParams) => {
if (!address) {
throw new Error('No address found');
}

const _name = _params.name;
const signature = await getSignature();
const primaryNameSet = await justaname.subnames.setPrimaryName(
{
address,
name: _name,
chainId: _chainId,
},
{
xAddress: address,
xSignature: signature.signature,
xMessage: signature.message,
}
);
refetchPrimaryName();
return primaryNameSet;
},
});

return {
setPrimaryName: mutate.mutateAsync,
isSetPrimaryNamePending: mutate.isPending,
};
};
12 changes: 10 additions & 2 deletions packages/@justaname.id/sdk/src/lib/api/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ import {
MAPP_REVOKE_PERMISSION_ROUTE,
} from './mapp';
import { GET_ALL_OFFCHAIN_RESOLVERS_ROUTE } from './offchain-resolver';
import { GET_PRIMARY_NAME_BY_ADDRESS_ROUTE } from './primary-name';
import { PrimaryNameGetByAddressRoute } from '../../types/primary-name';
import {
GET_PRIMARY_NAME_BY_ADDRESS_ROUTE,
SET_PRIMARY_NAME_ROUTE,
} from './primary-name';
import {
PrimaryNameGetByAddressRoute,
SetPrimaryNameRoute,
} from '../../types/primary-name';

export interface ROUTES {
SIWE_VERIFY_MESSAGE_ROUTE: VerifyMessageRoute;
Expand All @@ -79,6 +85,7 @@ export interface ROUTES {
GET_ALL_ENS_WITH_COUNT_ROUTE: SubnameGetAllByEnsDomainWithCountRoute;
GET_ALL_OFFCHAIN_RESOLVERS_ROUTE: OffchainResolversGetAllRoute;
GET_PRIMARY_NAME_BY_ADDRESS_ROUTE: PrimaryNameGetByAddressRoute;
SET_PRIMARY_NAME_ROUTE: SetPrimaryNameRoute;
}

export const Routes: Record<keyof ROUTES, string> = {
Expand Down Expand Up @@ -106,4 +113,5 @@ export const Routes: Record<keyof ROUTES, string> = {
GET_ALL_ENS_WITH_COUNT_ROUTE,
GET_ALL_OFFCHAIN_RESOLVERS_ROUTE,
GET_PRIMARY_NAME_BY_ADDRESS_ROUTE,
SET_PRIMARY_NAME_ROUTE,
};
3 changes: 3 additions & 0 deletions packages/@justaname.id/sdk/src/lib/api/routes/primary-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ export const PRIMARY_NAME_BASE_ROUTE = GLOBAL_PREFIX + '/ens/v1/primary-name';

export const GET_PRIMARY_NAME_BY_ADDRESS_ROUTE =
PRIMARY_NAME_BASE_ROUTE + '/address';

export const SET_PRIMARY_NAME_ROUTE =
PRIMARY_NAME_BASE_ROUTE + '/set-primary-name';
Loading

0 comments on commit a4d0a7e

Please sign in to comment.