Skip to content

Commit

Permalink
fix(resolver): wrong blockchain error (#3037)
Browse files Browse the repository at this point in the history
Signed-off-by: banklesss <105349292+banklesss@users.noreply.github.com>
  • Loading branch information
banklesss authored Jan 24, 2024
1 parent f5e0306 commit 910b52b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions packages/resolver/src/adapters/unstoppable/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ describe('getCryptoAddress', () => {
})

await expect(() => getCryptoAddress(domain)).rejects.toThrow(
Resolver.Errors.NotFound,
Resolver.Errors.WrongBlockchain,
)
expect(mockFetchData).toHaveBeenCalledWith(
{
Expand All @@ -225,7 +225,7 @@ describe('getCryptoAddress', () => {
)
})

it('should throw invalid blockchain if the response contain the address for other blockchain', async () => {
it('should throw wrong blockchain if the response contain the address for other blockchain', async () => {
const domain = mockApiResponse.meta.domain
const expectedUrl = `${unstoppableApiConfig.mainnet.getCryptoAddress}${domain}`
const invalidApiResponse = {
Expand Down
18 changes: 9 additions & 9 deletions packages/resolver/src/adapters/unstoppable/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,28 +40,23 @@ export const unstoppableApiGetCryptoAddress = (

handleApiError(error)
} else {
// parsing
const safeParsedAdaResponse = UnstoppableApiAdaResponseSchema.safeParse(
response.value.data,
)
const safeParsedGeneralResponse =
UnstoppableApiGeneralResponseSchema.safeParse(response.value.data)

// checking
const hasCardanoAddress = safeParsedAdaResponse.success
const hasOtherBlockchainAddress =
!hasCardanoAddress &&
safeParsedGeneralResponse.success &&
Object.keys(response.value.data.records).length > 0
const hasNotAnyAddress =
!hasOtherBlockchainAddress && safeParsedGeneralResponse.success
const hasOtherBlockchainAddress = safeParsedGeneralResponse.success

if (hasCardanoAddress)
return response.value.data.records['crypto.ADA.address']

if (hasOtherBlockchainAddress)
throw new Resolver.Errors.WrongBlockchain()

if (hasNotAnyAddress) throw new Resolver.Errors.NotFound()

throw new Resolver.Errors.InvalidResponse()
}
} catch (error: unknown) {
Expand Down Expand Up @@ -104,10 +99,13 @@ const UnstoppableApiAdaResponseSchema = z.object({
})

const UnstoppableApiGeneralResponseSchema = z.object({
meta: z.object({
blockchain: z.string(),
}),
records: z.record(z.string(), z.string()),
})

// https://docs.unstoppabledomains.com/openapi/resolution/
// curl https://api.unstoppabledomains.com/resolve/supported_tlds
export const unstoppableSupportedTlds = [
'.x',
'.polygon',
Expand All @@ -125,6 +123,8 @@ export const unstoppableSupportedTlds = [
'.anime',
'.manga',
'.go',
'.altimist',
'.unstoppable',
'.zil',
'.eth',
] as const
Expand Down
2 changes: 1 addition & 1 deletion packages/resolver/src/utils/isResolvableDomain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ describe('isResolvableDomain', () => {
${'ud.eth'} | ${true}
${'$adahandle'} | ${true}
${'cns.ada'} | ${true}
${'ud.unstoppable'} | ${true}
${'ud.com'} | ${false}
${'ud.unstoppable'} | ${false}
${'other.uk'} | ${false}
${'$'} | ${false}
`('should return $expected for $resolve', ({resolve, expected}) => {
Expand Down

0 comments on commit 910b52b

Please sign in to comment.