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 anthony/cleanup
  • Loading branch information
anthony23991 committed Jan 10, 2025
2 parents ed4f7f9 + c304ca6 commit 81615d3
Show file tree
Hide file tree
Showing 25 changed files with 918 additions and 135 deletions.
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';
28 changes: 27 additions & 1 deletion packages/@justaname.id/sdk/src/lib/features/subnames/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import {
SubnameRevokeRoute,
SubnameSearchRoute,
SubnameUpdateRoute,
PrimaryNameGetByAddressRoute,
SetPrimaryNameRoute,
} from '../../types';
import { sanitizeAddresses, sanitizeTexts } from '../../utils';
import { PrimaryNameGetByAddressRoute } from '../../types/primary-name';
import { InvalidConfigurationException } from '../../errors';

export interface SubnamesConfig {
Expand Down Expand Up @@ -498,6 +499,31 @@ export class Subnames {
)(['providerUrl']);
}

async setPrimaryName(
params: SetPrimaryNameRoute['params'],
headers: SetPrimaryNameRoute['headers']
): Promise<SetPrimaryNameRoute['response']> {
const { chainId, signature: paramSignature, ...rest } = params;
const _chainId = chainId || this.chainId;

const { signature, xSignature } = this.checkSignature(
paramSignature,
headers.xSignature
);

return assertRestCall(
'SET_PRIMARY_NAME_ROUTE',
'POST',
{
chainId: _chainId,
signature,
...rest,
},
{ ...headers, xSignature },
this.dev
)(['chainId'], ['xMessage', 'xAddress']);
}

async getPrimaryNameByAddress(
params: PrimaryNameGetByAddressRoute['params']
): Promise<PrimaryNameGetByAddressRoute['response']> {
Expand Down
3 changes: 2 additions & 1 deletion packages/@justaname.id/sdk/src/lib/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export * from './common';
export * from './headers';
export * from './justaname';
export * from './offchain-resolver';
export * from './primary-name';
export * from './siwe';
export * from './subnames';
export * from './mApps';
export * from './signin'
export * from './signin';
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './get-primary-name-by-address';
export * from './set-primary-name';
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ChainId, IRequest, IRoute } from '../common';
import { SIWEHeaders } from '../headers';
import { PrimaryNameGetByAddressResponse } from './get-primary-name-by-address';

export interface SetPrimaryNameRequest extends IRequest {
name: string;

address: string;

chainId: ChainId;

signature?: string;
}

export interface SetPrimaryNameRoute
extends IRoute<
SetPrimaryNameRequest,
PrimaryNameGetByAddressResponse,
SIWEHeaders,
'chainId'
> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/* BackBtn.module.css */

.btnCard {
display: flex;
align-items: center;
justify-content: center;
width: 24px;
height: 24px;
border-radius: 50%;
background-color: var(--justweb3-foreground-color-4);
cursor: pointer;
}
25 changes: 25 additions & 0 deletions packages/@justweb3/ui/src/lib/components/BackBtn/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FC } from 'react';
import { ArrowIcon } from '../../icons';
import styles from './BackBtn.module.css';

interface BackBtnProps {
onClick: (e: React.MouseEvent<HTMLDivElement>) => void;
style?: React.CSSProperties;
}

export const BackBtn: FC<BackBtnProps> = ({ onClick, style }) => {
return (
<div className={styles.btnCard} onClick={onClick} style={style}>
<ArrowIcon
width={16}
color={'var(--justweb3-foreground-color-2)'}
style={{

transform: 'rotate(180deg)',
}}
/>
</div>
);
};

export default BackBtn;
3 changes: 2 additions & 1 deletion packages/@justweb3/ui/src/lib/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './OrLine';
export * from './BackBtn';
export * from './ClickableItem';
export * from './ExpandableText';
export * from './OrLine';
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import type { SVGProps } from 'react';
export default function Configuration(props: SVGProps<SVGSVGElement>) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 20 20"
{...props}
>
<mask
id="configuration_svg__a"
width={20}
height={20}
x={0}
y={0}
maskUnits="userSpaceOnUse"
style={{
maskType: 'alpha',
}}
>
<path fill="#D9D9D9" d="M0 0h20v20H0z" />
</mask>
<g mask="url(#configuration_svg__a)">
<path
fill={props.fill || 'var(--justweb3-primary-color)'}
d="M9.167 18.104 3.333 14.75a1.624 1.624 0 0 1-.833-1.437V6.688a1.62 1.62 0 0 1 .833-1.438l5.834-3.354q.395-.23.833-.23t.833.23l5.834 3.354q.395.23.614.604.219.375.219.834v6.625a1.62 1.62 0 0 1-.833 1.437l-5.834 3.354q-.396.23-.833.23-.438 0-.833-.23m0-7.625v5.709l.833.479.833-.48V10.48l5-2.896v-.875l-.896-.52L10 9.042 5.063 6.188l-.896.52v.875z"
/>
</g>
</svg>
);
}
Loading

0 comments on commit 81615d3

Please sign in to comment.