-
Notifications
You must be signed in to change notification settings - Fork 485
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support
bech32
address standard (#2351)
* create settings context with simple address format toggler * transform base16 hash to bech32 hash * add ENV variables * add snippet to address page * add redirect for bech32 addresses * change address format in search * add provider to tests * update demo values * migrate from Buffer to Uint8Array and add tests * bug fixes and screenshots updates * review fixes * roll back changes in env values * update screenshots
- Loading branch information
Showing
66 changed files
with
509 additions
and
131 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
NEXT_PUBLIC_GRAPHIQL_TRANSACTION=none | ||
NEXT_PUBLIC_API_SPEC_URL=none | ||
NEXT_PUBLIC_VIEWS_CONTRACT_EXTRA_VERIFICATION_METHODS=none | ||
NEXT_PUBLIC_HOMEPAGE_STATS=[] | ||
NEXT_PUBLIC_HOMEPAGE_STATS=[] | ||
NEXT_PUBLIC_VIEWS_ADDRESS_FORMAT=['base16','bech32'] | ||
NEXT_PUBLIC_VIEWS_ADDRESS_BECH_32_PREFIX=foo |
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,49 @@ | ||
import { bech32 } from '@scure/base'; | ||
|
||
import config from 'configs/app'; | ||
import bytesToHex from 'lib/bytesToHex'; | ||
import hexToBytes from 'lib/hexToBytes'; | ||
|
||
export const DATA_PART_REGEXP = /^[\da-z]{38}$/; | ||
export const BECH_32_SEPARATOR = '1'; // https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki#bech32 | ||
|
||
export function toBech32Address(hash: string) { | ||
if (config.UI.views.address.hashFormat.bech32Prefix) { | ||
try { | ||
const words = bech32.toWords(hexToBytes(hash)); | ||
return bech32.encode(config.UI.views.address.hashFormat.bech32Prefix, words); | ||
} catch (error) {} | ||
} | ||
|
||
return hash; | ||
} | ||
|
||
export function isBech32Address(hash: string) { | ||
if (!config.UI.views.address.hashFormat.bech32Prefix) { | ||
return false; | ||
} | ||
|
||
if (!hash.startsWith(`${ config.UI.views.address.hashFormat.bech32Prefix }${ BECH_32_SEPARATOR }`)) { | ||
return false; | ||
} | ||
|
||
const strippedHash = hash.replace(`${ config.UI.views.address.hashFormat.bech32Prefix }${ BECH_32_SEPARATOR }`, ''); | ||
return DATA_PART_REGEXP.test(strippedHash); | ||
} | ||
|
||
export function fromBech32Address(hash: string) { | ||
if (config.UI.views.address.hashFormat.bech32Prefix) { | ||
try { | ||
const { words, prefix } = bech32.decode(hash as `${ string }${ typeof BECH_32_SEPARATOR }${ string }`); | ||
|
||
if (prefix !== config.UI.views.address.hashFormat.bech32Prefix) { | ||
return hash; | ||
} | ||
|
||
const bytes = bech32.fromWords(words); | ||
return bytesToHex(bytes); | ||
} catch (error) {} | ||
} | ||
|
||
return hash; | ||
} |
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,8 @@ | ||
export default function bytesToBase64(bytes: Uint8Array) { | ||
let result = ''; | ||
for (const byte of bytes) { | ||
result += Number(byte).toString(16).padStart(2, '0'); | ||
} | ||
|
||
return `0x${ result }`; | ||
} |
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,56 @@ | ||
import React from 'react'; | ||
|
||
import { ADDRESS_FORMATS, type AddressFormat } from 'types/views/address'; | ||
|
||
import * as cookies from 'lib/cookies'; | ||
|
||
import { useAppContext } from './app'; | ||
|
||
interface SettingsProviderProps { | ||
children: React.ReactNode; | ||
} | ||
|
||
interface TSettingsContext { | ||
addressFormat: AddressFormat; | ||
toggleAddressFormat: () => void; | ||
} | ||
|
||
export const SettingsContext = React.createContext<TSettingsContext | null>(null); | ||
|
||
export function SettingsContextProvider({ children }: SettingsProviderProps) { | ||
const { cookies: appCookies } = useAppContext(); | ||
const initialAddressFormat = cookies.get(cookies.NAMES.ADDRESS_FORMAT, appCookies); | ||
|
||
const [ addressFormat, setAddressFormat ] = React.useState<AddressFormat>( | ||
initialAddressFormat && ADDRESS_FORMATS.includes(initialAddressFormat) ? initialAddressFormat as AddressFormat : 'base16', | ||
); | ||
|
||
const toggleAddressFormat = React.useCallback(() => { | ||
setAddressFormat(prev => { | ||
const nextValue = prev === 'base16' ? 'bech32' : 'base16'; | ||
cookies.set(cookies.NAMES.ADDRESS_FORMAT, nextValue); | ||
return nextValue; | ||
}); | ||
}, []); | ||
|
||
const value = React.useMemo(() => { | ||
return { | ||
addressFormat, | ||
toggleAddressFormat, | ||
}; | ||
}, [ addressFormat, toggleAddressFormat ]); | ||
|
||
return ( | ||
<SettingsContext.Provider value={ value }> | ||
{ children } | ||
</SettingsContext.Provider> | ||
); | ||
} | ||
|
||
export function useSettingsContext(disabled?: boolean) { | ||
const context = React.useContext(SettingsContext); | ||
if (context === undefined || disabled) { | ||
return null; | ||
} | ||
return context; | ||
} |
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,20 @@ | ||
import type { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import type { AddressFormat } from 'types/views/address'; | ||
|
||
import config from 'configs/app'; | ||
import * as cookiesLib from 'lib/cookies'; | ||
|
||
export default function addressFormatMiddleware(req: NextRequest, res: NextResponse) { | ||
const addressFormatCookie = req.cookies.get(cookiesLib.NAMES.ADDRESS_FORMAT); | ||
const defaultFormat = config.UI.views.address.hashFormat.availableFormats[0]; | ||
|
||
if (addressFormatCookie) { | ||
const isValidCookie = config.UI.views.address.hashFormat.availableFormats.includes(addressFormatCookie.value as AddressFormat); | ||
if (!isValidCookie) { | ||
res.cookies.set(cookiesLib.NAMES.ADDRESS_FORMAT, defaultFormat, { path: '/' }); | ||
} | ||
} else { | ||
res.cookies.set(cookiesLib.NAMES.ADDRESS_FORMAT, defaultFormat, { path: '/' }); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export { account } from './account'; | ||
export { default as colorTheme } from './colorTheme'; | ||
export { default as addressFormat } from './addressFormat'; |
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
Binary file modified
BIN
+2.87 KB
(110%)
ui/address/__screenshots__/AddressDetails.pw.tsx_default_contract-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.01 KB
(100%)
ui/address/__screenshots__/AddressDetails.pw.tsx_default_filecoin-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.38 KB
(110%)
ui/address/__screenshots__/AddressDetails.pw.tsx_default_mobile-contract-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+2.66 KB
(110%)
ui/address/__screenshots__/AddressDetails.pw.tsx_default_mobile-filecoin-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+4.03 KB
(120%)
ui/address/__screenshots__/AddressDetails.pw.tsx_default_mobile-validator-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+3.58 KB
(110%)
ui/address/__screenshots__/AddressDetails.pw.tsx_default_validator-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.