-
Notifications
You must be signed in to change notification settings - Fork 1
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
[BLCKCHN-203] CronosId TS client Integration #9
Open
yeehan-crypto-com
wants to merge
3
commits into
main
Choose a base branch
from
BLCKCHN-203-cronosid-integration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+2,228
−5,213
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { Client } from '../lib/client/Client.js'; | ||
import { LookupAddressResponse, ResolveNameResponse } from '../lib/client/interfaces/cronosid.interfaces.js'; | ||
import { BASE_URL } from '../lib/constants/global.constants.js'; | ||
import { ApiResponse } from './api.interfaces.js'; | ||
|
||
/** | ||
* Resolves a CronosId to its corresponding blockchain address | ||
* | ||
* @async | ||
* @function resolveName | ||
* @param {string} name - The CronosId name to resolve | ||
* @returns {Promise<string>} - A promise that resolves to the blockchain address | ||
* @throws {Error} Will throw an error if the fetch request fails or the server responds with an error message | ||
* | ||
* @example | ||
* const address = await resolveName('example.cro'); | ||
* console.log(address); // '0x...' | ||
*/ | ||
export const resolveName = async (name: string): Promise<ApiResponse<ResolveNameResponse>> => { | ||
const chainId = Client.getChainId(); | ||
const url = `${BASE_URL}/api/v1/cdc-developer-platform/cronosid/resolve/${name}?chainId=${chainId}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorBody = await response.json(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about adding validation whether response is json |
||
const serverErrorMessage = errorBody.error || `HTTP error! status: ${response.status}`; | ||
throw new Error(serverErrorMessage); | ||
} | ||
|
||
return await response.json(); | ||
} catch (e) { | ||
const error = e as Error; | ||
console.error(`[cronosidApi/resolveName] - ${error.message}`); | ||
throw new Error(`Failed to resolve name: ${error.message}`); | ||
} | ||
}; | ||
|
||
/** | ||
* Looks up an address to find its associated CronosId | ||
* | ||
* @async | ||
* @function lookupAddress | ||
* @param {string} address - The blockchain address to lookup | ||
* @returns {Promise<string>} - A promise that resolves to the CronosId name | ||
* @throws {Error} Will throw an error if the fetch request fails or the server responds with an error message | ||
* | ||
* @example | ||
* const name = await lookupAddress('0x...'); | ||
* console.log(name); // 'example.cro' | ||
*/ | ||
export const lookupAddress = async (address: string): Promise<ApiResponse<LookupAddressResponse>> => { | ||
const chainId = Client.getChainId(); | ||
const url = `${BASE_URL}/api/v1/cdc-developer-platform/cronosid/lookup/${address}?chainId=${chainId}`; | ||
|
||
try { | ||
const response = await fetch(url, { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (!response.ok) { | ||
const errorBody = await response.json(); | ||
const serverErrorMessage = errorBody.error || `HTTP error! status: ${response.status}`; | ||
throw new Error(serverErrorMessage); | ||
} | ||
|
||
return await response.json(); | ||
} catch (e) { | ||
const error = e as Error; | ||
console.error(`[cronosidApi/lookupAddress] - ${error.message}`); | ||
throw new Error(`Failed to lookup address: ${error.message}`); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { ApiResponse } from '../../integrations/api.interfaces.js'; | ||
import { lookupAddress, resolveName } from '../../integrations/cronosid.api.js'; | ||
import { ChainName } from '../utils/chain.utils.js'; | ||
import { Client } from './Client.js'; | ||
import { LookupAddressResponse, ResolveNameResponse } from './interfaces/cronosid.interfaces.js'; | ||
|
||
export class CronosId { | ||
/** | ||
* The registry contracts for CronosId on different chains - CronosId is supported on EVM and EVM Testnet only | ||
*/ | ||
private static readonly supportedChains = [ChainName.CRONOS_EVM, ChainName.CRONOS_EVM_TESTNET]; | ||
|
||
/** | ||
* Checks if a given string is a valid CronosId, which is a string ending with '.cro' and not empty. | ||
* example: 'alice.cro' is a valid CronosId, but 'alice' is not. | ||
* | ||
* @param {string} name - The string to check for CronosId validity. | ||
* @returns {boolean} - True if the string is a valid CronosId, false otherwise. | ||
*/ | ||
public static isCronosId(name: string): boolean { | ||
const lowercaseName = name.toLowerCase(); | ||
const parts = lowercaseName.split('.cro'); | ||
return lowercaseName.endsWith('.cro') && parts[0].length > 0; | ||
} | ||
|
||
/** | ||
* Checks if CronosId is supported on the current chain specified in the client. | ||
* | ||
* @returns {boolean} - True if CronosId is supported on the current chain, false otherwise. | ||
*/ | ||
public static isSupported(): boolean { | ||
const chainId = Client.getChainId(); | ||
return this.supportedChains.includes(chainId as ChainName); | ||
} | ||
|
||
/** | ||
* Resolves a CronosId to a wallet address | ||
* | ||
* @param {string} cronosId - The CronosId to resolve. | ||
* @returns {Promise<string>} - A promise that resolves to the resolved address. | ||
* @throws {Error} - Throws an error if the CronosId is not valid or if the resolution fails. | ||
*/ | ||
public static async forwardResolve(cronosId: string): Promise<ApiResponse<ResolveNameResponse>> { | ||
if (!this.isSupported()) { | ||
throw new Error('CronosId is not supported on the current chain'); | ||
} | ||
if (!this.isCronosId(cronosId)) { | ||
throw new Error(`Invalid CronosId format: ${cronosId}`); | ||
} | ||
return await resolveName(cronosId); | ||
} | ||
|
||
/** | ||
* Resolves a wallet address to a CronosId if it has one | ||
* | ||
* @param {string} address - The wallet address to resolve. | ||
* @returns {Promise<string>} - A promise that resolves to the resolved CronosId. | ||
* @throws {Error} - Throws an error if the address is not valid or if the resolution fails. | ||
*/ | ||
public static async reverseResolve(address: string): Promise<ApiResponse<LookupAddressResponse>> { | ||
if (!this.isSupported()) { | ||
throw new Error('CronosId is not supported on the current chain'); | ||
} | ||
|
||
return await lookupAddress(address); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export interface ResolveNameResponse { | ||
resolvedAddress: string; | ||
} | ||
|
||
export interface LookupAddressResponse { | ||
resolvedName: string; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how about using encodeURIComponent for name, chainId?