Skip to content
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

Centrifuge App: Convert EVM address to Subtrate #1532

Merged
merged 2 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions centrifuge-app/src/components/DebugFlags/DebugFlags.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const Panel: React.FC<{
))}
</select>
)
} else if (obj.type === 'component') {
el = <obj.Component value={value} onChange={(val) => onChange(key as Key, val)} />
} else {
el = (
<input
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { evmToSubstrateAddress } from '@centrifuge/centrifuge-js'
import { useCentrifuge, useWallet } from '@centrifuge/centrifuge-react'
import { isAddress } from '@ethersproject/address'

export function ConvertEvmAddress() {
const ctx = useWallet()
const cent = useCentrifuge()
function convert() {
const addr = window.prompt('EVM address')
if (!addr || !isAddress(addr)) {
window.alert('Invalid EVM address')
return
}
const chainId = window.prompt('Chain ID', String(ctx.substrate.evmChainId))
if (!chainId || !Number.isSafeInteger(Number(chainId))) {
window.alert('Invalid chain ID')
return
}
const converted = evmToSubstrateAddress(addr, Number(chainId))
const formatted = cent.utils.formatAddress(converted)
window.alert(formatted)
console.log('Substrate address', formatted)
}
return <button onClick={convert}>Convert EVM to Substrate</button>
}
16 changes: 16 additions & 0 deletions centrifuge-app/src/components/DebugFlags/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import React from 'react'
import { ConvertEvmAddress } from './components/ConvertEvmAddress'

const params = new URLSearchParams(typeof window !== 'undefined' ? window.location.search : {})
export const debug =
import.meta.env.MODE === 'development' || params.get('debug') != null || !!localStorage.getItem('debugFlags')
Expand All @@ -8,6 +11,12 @@ export type DebugFlagConfig =
default: string
alwaysShow?: boolean
}
| {
type: 'component'
Component: React.FC<{ value: any; onChange: (v: any) => void }>
default: null
alwaysShow?: boolean
}
| {
type: 'checkbox'
default: boolean
Expand Down Expand Up @@ -35,6 +44,7 @@ export type Key =
| 'showAdvancedAccounts'
| 'editAdminConfig'
| 'showPodAccountCreation'
| 'convertEvmAddress'

export const flagsConfig: Record<Key, DebugFlagConfig> = {
address: {
Expand Down Expand Up @@ -97,4 +107,10 @@ export const flagsConfig: Record<Key, DebugFlagConfig> = {
default: false,
alwaysShow: true,
},
convertEvmAddress: {
type: 'component',
Component: ConvertEvmAddress,
default: null,
alwaysShow: true,
},
}
Loading