-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
19 changed files
with
202 additions
and
90 deletions.
There are no files selected for viewing
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,5 @@ | ||
--- | ||
'@coinbase/onchainkit': patch | ||
--- | ||
|
||
- **feat**: exposed the `getName` and `getAvatar` utilities to assist in retrieving name and avatar identity information. These utilities come in handy when working with Next.js or any Node.js backend. By @zizzamia #265 #283 |
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,23 @@ | ||
# `getAvatar` | ||
|
||
The `getAvatar` utility is designed to retrieve an avatar image | ||
URL from an onchain identity provider for a given name. | ||
|
||
Consider the utility instead of the hook when you | ||
use it with Next.js or any Node.js backend. | ||
|
||
Supported providers: | ||
|
||
- ENS | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { getAvatar } from '@coinbase/onchainkit/identity'; | ||
|
||
const avatar = await getAvatar({ ensName: 'vitalik.eth' }); | ||
``` | ||
|
||
## Returns | ||
|
||
[`Promise<GetAvatarReturnType>`](/identity/types#getavatarreturntype) |
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,23 @@ | ||
# `getName` | ||
|
||
The `getName` utility is designed to retrieve a name from an onchain identity | ||
provider for a specific address. | ||
|
||
Consider the utility instead of the hook when you | ||
use it with Next.js or any Node.js backend. | ||
|
||
Supported providers: | ||
|
||
- ENS | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { getName } from '@coinbase/onchainkit/identity'; | ||
|
||
const name = await getName({ address: '0x1234' }); | ||
``` | ||
|
||
## Returns | ||
|
||
[`Promise<GetNameReturnType>`](/identity/types#getnamereturntype) |
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,36 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { getAvatar } from './getAvatar'; | ||
import { publicClient } from '../../network/client'; | ||
|
||
jest.mock('../../network/client'); | ||
|
||
describe('getAvatar', () => { | ||
const mockGetEnsAvatar = publicClient.getEnsAvatar as jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return correct avatar URL from client getAvatar', async () => { | ||
const ensName = 'test.ens'; | ||
const expectedAvatarUrl = 'avatarUrl'; | ||
|
||
mockGetEnsAvatar.mockResolvedValue(expectedAvatarUrl); | ||
|
||
const avatarUrl = await getAvatar(ensName); | ||
|
||
expect(avatarUrl).toBe(expectedAvatarUrl); | ||
expect(mockGetEnsAvatar).toHaveBeenCalledWith({ name: ensName }); | ||
}); | ||
|
||
it('should return null when client getAvatar throws an error', async () => { | ||
const ensName = 'test.ens'; | ||
|
||
mockGetEnsAvatar.mockRejectedValue(new Error('This is an error')); | ||
|
||
await expect(getAvatar(ensName)).rejects.toThrow('This is an error'); | ||
}); | ||
}); |
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,9 @@ | ||
import { normalize } from 'viem/ens'; | ||
import { publicClient } from '../../network/client'; | ||
import { GetAvatarReturnType } from '../types'; | ||
|
||
export const getAvatar = async (ensName: string): Promise<GetAvatarReturnType> => { | ||
return await publicClient.getEnsAvatar({ | ||
name: normalize(ensName), | ||
}); | ||
}; |
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,40 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { getName } from './getName'; | ||
import { publicClient } from '../../network/client'; | ||
|
||
jest.mock('../../network/client'); | ||
|
||
describe('getName', () => { | ||
const mockGetEnsName = publicClient.getEnsName as jest.Mock; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should return correct value from client getName', async () => { | ||
const walletAddress = '0x1234'; | ||
const expectedEnsName = 'avatarUrl'; | ||
mockGetEnsName.mockResolvedValue(expectedEnsName); | ||
const name = await getName(walletAddress); | ||
expect(name).toBe(expectedEnsName); | ||
expect(mockGetEnsName).toHaveBeenCalledWith({ address: walletAddress }); | ||
}); | ||
|
||
it('should return null name when client ', async () => { | ||
const walletAddress = '0x1234'; | ||
const expectedEnsName = 'avatarUrl'; | ||
mockGetEnsName.mockResolvedValue(expectedEnsName); | ||
const name = await getName(walletAddress); | ||
expect(name).toBe(expectedEnsName); | ||
expect(mockGetEnsName).toHaveBeenCalledWith({ address: walletAddress }); | ||
}); | ||
|
||
it('should return null client getName throws an error', async () => { | ||
const walletAddress = '0x1234'; | ||
mockGetEnsName.mockRejectedValue(new Error('This is an error')); | ||
await expect(getName(walletAddress)).rejects.toThrow('This is an error'); | ||
}); | ||
}); |
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,16 @@ | ||
import type { Address } from 'viem'; | ||
import { publicClient } from '../../network/client'; | ||
import { GetNameReturnType } from '../types'; | ||
|
||
/** | ||
* An asynchronous function to fetch the Ethereum Name Service (ENS) name for a given Ethereum address. | ||
* It returns the ENS name if it exists, or null if it doesn't or in case of an error. | ||
* | ||
* @param address - The Ethereum address for which the ENS name is being fetched. | ||
* @returns A promise that resolves to the ENS name (as a string) or null. | ||
*/ | ||
export const getName = async (address: Address): Promise<GetNameReturnType> => { | ||
return await publicClient.getEnsName({ | ||
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
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
Oops, something went wrong.