-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use default basename avatar when possible (#1002)
- Loading branch information
Showing
11 changed files
with
269 additions
and
4 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": minor | ||
--- | ||
**feat**: Add `isBasename` and `getBaseDefaultProfilePicture` function to resolve to default avatars. By @kirkas #1002 | ||
**feat**: Modify `getAvatar` to resolve default avatars, only for basenames. By @kirkas #1002 |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { getBaseDefaultProfilePicture } from './getBaseDefaultProfilePicture'; | ||
|
||
describe('getBaseDefaultProfilePicture', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('should return correct resolver data for base mainnet', async () => { | ||
const defaultAvatar = getBaseDefaultProfilePicture('shrek.base.eth'); | ||
const validString = defaultAvatar.startsWith('data:image/svg+xml;base64'); | ||
expect(validString).toBe(true); | ||
}); | ||
|
||
it('should return correct resolver data for base sepolia', async () => { | ||
const defaultAvatar = getBaseDefaultProfilePicture('shrek.basetest.eth'); | ||
const validString = defaultAvatar.startsWith('data:image/svg+xml;base64'); | ||
expect(validString).toBe(true); | ||
}); | ||
}); |
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,15 @@ | ||
import { BASE_DEFAULT_PROFILE_PICTURES } from '../constants'; | ||
import type { BaseName } from '../types'; | ||
import { getBaseDefaultProfilePictureIndex } from './getBaseDefaultProfilePictureIndex'; | ||
|
||
export const getBaseDefaultProfilePicture = (username: BaseName) => { | ||
const profilePictureIndex = getBaseDefaultProfilePictureIndex( | ||
username, | ||
BASE_DEFAULT_PROFILE_PICTURES.length, | ||
); | ||
const selectedProfilePicture = | ||
BASE_DEFAULT_PROFILE_PICTURES[profilePictureIndex]; | ||
const base64Svg = btoa(selectedProfilePicture); | ||
const dataUri = `data:image/svg+xml;base64,${base64Svg}`; | ||
return dataUri; | ||
}; |
19 changes: 19 additions & 0 deletions
19
src/identity/utils/getBaseDefaultProfilePictureIndex.test.tsx
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,19 @@ | ||
import { getBaseDefaultProfilePictureIndex } from './getBaseDefaultProfilePictureIndex'; | ||
|
||
describe('getBaseDefaultProfilePictureIndex', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('Should always return the same index given a number of options', async () => { | ||
// Note: This seems silly but this tests "proves" the algorithm is deterministic | ||
expect(getBaseDefaultProfilePictureIndex('shrek.base.eth', 7)).toBe(3); | ||
expect(getBaseDefaultProfilePictureIndex('shrek.basetest.eth', 7)).toBe(4); | ||
expect(getBaseDefaultProfilePictureIndex('leo.base.eth', 7)).toBe(0); | ||
expect(getBaseDefaultProfilePictureIndex('leo.basetest.eth', 7)).toBe(3); | ||
expect(getBaseDefaultProfilePictureIndex('zimmania.base.eth', 7)).toBe(5); | ||
expect(getBaseDefaultProfilePictureIndex('zimmania.basetest.eth', 7)).toBe( | ||
4, | ||
); | ||
}); | ||
}); |
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 { sha256 } from 'viem'; | ||
|
||
// Will return a an index between 0 and optionsLength | ||
export const getBaseDefaultProfilePictureIndex = ( | ||
name: string, | ||
optionsLength: number, | ||
) => { | ||
const nameAsUint8Array = Uint8Array.from( | ||
name.split('').map((letter) => letter.charCodeAt(0)), | ||
); | ||
const hash = sha256(nameAsUint8Array); | ||
const hashValue = Number.parseInt(hash, 16); | ||
const remainder = hashValue % optionsLength; | ||
const index = remainder; | ||
return index; | ||
}; |
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,21 @@ | ||
import { isBasename } from './isBasename'; | ||
|
||
describe('isBasename', () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
}); | ||
|
||
it('Returns true for base mainnet names', async () => { | ||
expect(isBasename('shrek.base.eth')).toBe(true); | ||
}); | ||
|
||
it('Returns true for base mainnet sepolia names', async () => { | ||
expect(isBasename('shrek.basetest.eth')).toBe(true); | ||
}); | ||
|
||
it('Returns false for any other name', async () => { | ||
expect(isBasename('shrek.optimisim.eth')).toBe(false); | ||
expect(isBasename('shrek.eth')).toBe(false); | ||
expect(isBasename('shrek.baaaaaes.eth')).toBe(false); | ||
}); | ||
}); |
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,10 @@ | ||
export const isBasename = (username: string) => { | ||
if (username.endsWith('.base.eth')) { | ||
return true; | ||
} | ||
|
||
if (username.endsWith('.basetest.eth')) { | ||
return true; | ||
} | ||
return false; | ||
}; |