Skip to content

Commit

Permalink
Centrifuge App: Convert EVM address to Subtrate (#1532)
Browse files Browse the repository at this point in the history
  • Loading branch information
onnovisser authored Aug 15, 2023
1 parent 38c9883 commit 7606648
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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,
},
}

0 comments on commit 7606648

Please sign in to comment.