@enclavemoney/enclave-wallet-sdk
Version:
A simple enclave wallet SDK for React applications
132 lines (124 loc) • 4.02 kB
text/typescript
import { JsonRpcProvider } from 'ethers';
const API_BASE_URL = "https://hyperapp.in";
/**
* Checks if a string is a valid ENS name
* @param value The string to check
* @returns boolean indicating if the string is a valid ENS name
*/
export const isENS = (value: string): boolean => {
return value.toLowerCase().endsWith(".eth");
};
/**
* Checks if a string is a valid Ethereum address
* @param value The string to check
* @returns boolean indicating if the string is a valid Ethereum address
*/
export const isEthereumAddress = (value: string): boolean => {
return /^0x[a-fA-F0-9]{40}$/.test(value);
};
/**
* Resolves an ENS name to an Ethereum address
* @param ensName The ENS name to resolve
* @returns The resolved Ethereum address or null if resolution fails
*/
export const resolveENS = async (ensName: string): Promise<string | null> => {
try {
const provider = new JsonRpcProvider(
"https://mainnet.infura.io/v3/e2d74a526a8b4f7abfc930b414329b2e"
);
const address = await provider.resolveName(ensName);
return address;
} catch (error) {
console.error("ENS resolution error:", error);
return null;
}
};
/**
* Gets the username associated with a wallet address
* @param walletAddress The wallet address to look up
* @param walletSdkKey The wallet SDK key for authorization
* @returns Object containing username information
*/
export const getUsernameForWalletAddress = async (walletAddress: string, walletSdkKey: string) => {
try {
const response = await fetch(
`${API_BASE_URL}/api/user/get-username-for-address/?wallet_address=${walletAddress}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `${walletSdkKey}`,
},
}
);
return await response.json();
} catch (error) {
console.error("Error fetching username for wallet address:", error);
return { value: false };
}
};
/**
* Gets the wallet address associated with a username
* @param username The username to look up
* @param walletSdkKey The wallet SDK key for authorization
* @returns Object containing wallet address information
*/
export const getWalletAddressForUsername = async (username: string, walletSdkKey: string) => {
try {
const response = await fetch(
`${API_BASE_URL}/api/user/get-address-for-username/?username=${username}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `${walletSdkKey}`,
},
}
);
return await response.json();
} catch (error) {
console.error("Error fetching wallet address for username:", error);
return { value: false };
}
};
/**
* Resolves a wallet address to an ENS name
* @param address The wallet address to resolve
* @returns The resolved ENS name or null if resolution fails
*/
export const reverseResolveENS = async (address: string): Promise<string | null> => {
try {
const provider = new JsonRpcProvider(
"https://mainnet.infura.io/v3/" + process.env.REACT_APP_INFURA_KEY
);
const ensName = await provider.lookupAddress(address);
return ensName;
} catch (error) {
console.error("Reverse ENS resolution error:", error);
return null;
}
};
/**
* Looks up user information for any input (address, username, ENS, etc.)
* @param input The input string to look up
* @param walletSdkKey The wallet SDK key for authorization
* @returns Object containing user information
*/
export const lookupUser = async (input: string, walletSdkKey: string) => {
try {
const response = await fetch(
`${API_BASE_URL}/api/user/lookup?input=${encodeURIComponent(input)}`,
{
method: "GET",
headers: {
"Content-Type": "application/json",
Authorization: `${walletSdkKey}`,
},
}
);
return await response.json();
} catch (error) {
console.error("Error looking up user:", error);
return { value: false };
}
};