-
Notifications
You must be signed in to change notification settings - Fork 205
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
[FEAT]: Use default basename avatar when possible #1002
Merged
Merged
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
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; | ||
}; |
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.
I like the comments