-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
@svenvoskamp merging as you are OOO. Please let me know if there's anything that wasn't addressed
- Loading branch information
Showing
48 changed files
with
1,264 additions
and
110 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,22 @@ | ||
import type { Meta } from '@storybook/web-components' | ||
import '@web3modal/ui/src/composites/wui-ens-input' | ||
import type { WuiEnsInput } from '@web3modal/ui' | ||
import { html } from 'lit' | ||
import '../../components/gallery-container' | ||
|
||
type Component = Meta<WuiEnsInput> | ||
|
||
export default { | ||
title: 'Composites/wui-ens-input', | ||
args: { | ||
errorMessage: '', | ||
disabled: false | ||
} | ||
} as Component | ||
|
||
export const Default: Component = { | ||
render: args => | ||
html`<gallery-container width="336" | ||
><wui-ens-input .errorMessage=${args.errorMessage} .disabled=${args.disabled}></wui-ens-input | ||
></gallery-container>` | ||
} |
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,3 @@ | ||
export const ConstantsUtil = { | ||
WC_NAME_SUFFIX: '.wcn.id' | ||
} |
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 @@ | ||
export const NavigationUtil = { | ||
URLS: { | ||
FAQ: 'https://walletconnect.com/faq' | ||
} | ||
} |
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,162 @@ | ||
import { subscribeKey as subKey } from 'valtio/utils' | ||
import { proxy, subscribe as sub } from 'valtio/vanilla' | ||
import { BlockchainApiController } from './BlockchainApiController.js' | ||
import type { BlockchainApiEnsError } from '../utils/TypeUtil.js' | ||
import { AccountController } from './AccountController.js' | ||
import { ConnectorController } from './ConnectorController.js' | ||
import { RouterController } from './RouterController.js' | ||
import { ConnectionController } from './ConnectionController.js' | ||
import { NetworkController } from './NetworkController.js' | ||
import { NetworkUtil } from '@web3modal/common' | ||
import { EnsUtil } from '../utils/EnsUtil.js' | ||
import { ConstantsUtil } from '@web3modal/common' | ||
|
||
// -- Types --------------------------------------------- // | ||
type Suggestion = { | ||
name: string | ||
registered: boolean | ||
} | ||
|
||
export interface EnsControllerState { | ||
suggestions: Suggestion[] | ||
loading: boolean | ||
} | ||
|
||
type StateKey = keyof EnsControllerState | ||
|
||
// -- State --------------------------------------------- // | ||
const state = proxy<EnsControllerState>({ | ||
suggestions: [], | ||
loading: false | ||
}) | ||
|
||
// -- Controller ---------------------------------------- // | ||
export const EnsController = { | ||
state, | ||
|
||
subscribe(callback: (newState: EnsControllerState) => void) { | ||
return sub(state, () => callback(state)) | ||
}, | ||
|
||
subscribeKey<K extends StateKey>(key: K, callback: (value: EnsControllerState[K]) => void) { | ||
return subKey(state, key, callback) | ||
}, | ||
|
||
async resolveName(name: string) { | ||
try { | ||
return await BlockchainApiController.lookupEnsName(name) | ||
} catch (e) { | ||
const error = e as BlockchainApiEnsError | ||
throw new Error(error?.reasons?.[0]?.description || 'Error resolving name') | ||
} | ||
}, | ||
|
||
async isNameRegistered(name: string) { | ||
try { | ||
await BlockchainApiController.lookupEnsName(name) | ||
|
||
return true | ||
} catch { | ||
return false | ||
} | ||
}, | ||
|
||
async getSuggestions(name: string) { | ||
try { | ||
state.loading = true | ||
state.suggestions = [] | ||
const response = await BlockchainApiController.getEnsNameSuggestions(name) | ||
state.suggestions = | ||
response.suggestions.map(suggestion => ({ | ||
...suggestion, | ||
name: suggestion.name.replace(ConstantsUtil.WC_NAME_SUFFIX, '') | ||
})) || [] | ||
|
||
return state.suggestions | ||
} catch (e) { | ||
const errorMessage = this.parseEnsApiError(e, 'Error fetching name suggestions') | ||
throw new Error(errorMessage) | ||
} finally { | ||
state.loading = false | ||
} | ||
}, | ||
|
||
async getNamesForAddress(address: string) { | ||
try { | ||
const network = NetworkController.state.caipNetwork | ||
if (!network) { | ||
return [] | ||
} | ||
|
||
const response = await BlockchainApiController.reverseLookupEnsName({ address }) | ||
|
||
return response | ||
} catch (e) { | ||
const errorMessage = this.parseEnsApiError(e, 'Error fetching names for address') | ||
throw new Error(errorMessage) | ||
} | ||
}, | ||
|
||
async registerName(name: string) { | ||
const network = NetworkController.state.caipNetwork | ||
if (!network) { | ||
throw new Error('Network not found') | ||
} | ||
const address = AccountController.state.address | ||
const emailConnector = ConnectorController.getAuthConnector() | ||
if (!address || !emailConnector) { | ||
throw new Error('Address or auth connector not found') | ||
} | ||
|
||
state.loading = true | ||
|
||
try { | ||
const message = JSON.stringify({ | ||
name: `${name}${ConstantsUtil.WC_NAME_SUFFIX}`, | ||
attributes: {}, | ||
timestamp: Math.floor(Date.now() / 1000) | ||
}) | ||
|
||
RouterController.pushTransactionStack({ | ||
view: 'RegisterAccountNameSuccess', | ||
goBack: false, | ||
replace: true, | ||
onCancel() { | ||
state.loading = false | ||
} | ||
}) | ||
|
||
const signature = await ConnectionController.signMessage(message) | ||
const networkId = NetworkUtil.caipNetworkIdToNumber(network.id) | ||
|
||
if (!networkId) { | ||
throw new Error('Network not found') | ||
} | ||
|
||
const coinType = EnsUtil.convertEVMChainIdToCoinType(networkId) | ||
await BlockchainApiController.registerEnsName({ | ||
coinType, | ||
address: address as `0x${string}`, | ||
signature, | ||
message | ||
}) | ||
|
||
AccountController.setProfileName(`${name}${ConstantsUtil.WC_NAME_SUFFIX}`) | ||
RouterController.replace('RegisterAccountNameSuccess') | ||
} catch (e) { | ||
const errorMessage = this.parseEnsApiError(e, `Error registering name ${name}`) | ||
RouterController.replace('RegisterAccountName') | ||
throw new Error(errorMessage) | ||
} finally { | ||
state.loading = false | ||
} | ||
}, | ||
validateName(name: string) { | ||
return /^[a-zA-Z0-9-]{4,}$/u.test(name) | ||
}, | ||
parseEnsApiError(error: unknown, defaultError: string) { | ||
const ensError = error as BlockchainApiEnsError | ||
|
||
return ensError?.reasons?.[0]?.description || defaultError | ||
} | ||
} |
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,11 @@ | ||
const SLIP44_MSB = 0x80000000 | ||
|
||
export const EnsUtil = { | ||
convertEVMChainIdToCoinType(chainId: number): number { | ||
if (chainId >= SLIP44_MSB) { | ||
throw new Error('Invalid chainId') | ||
} | ||
|
||
return (SLIP44_MSB | chainId) >>> 0 | ||
} | ||
} |
Oops, something went wrong.