-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor address encoding/decoding (#323)
- Loading branch information
Showing
10 changed files
with
110 additions
and
92 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
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 { useApi } from '../contexts/ApiContext' | ||
import { useCallback } from 'react' | ||
import { encodesubstrateAddress } from '../utils/encodeSubstrateAddress' | ||
|
||
export const useGetEncodedAddress = () => { | ||
const { chainInfo } = useApi() | ||
|
||
const getEncodedAddress = useCallback( | ||
(address: string | Uint8Array | undefined) => { | ||
if (!chainInfo || !address) { | ||
return | ||
} | ||
|
||
return encodesubstrateAddress(address, chainInfo.ss58Format) | ||
}, | ||
[chainInfo] | ||
) | ||
|
||
return getEncodedAddress | ||
} |
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,14 @@ | ||
import { encodeAddress } from '@polkadot/util-crypto' | ||
|
||
export const encodesubstrateAddress = (address: string | Uint8Array, ss58Format: number) => { | ||
if (typeof address === 'string' && address.startsWith('0x') && address.length === 42) { | ||
console.log('invalid address format to encode', address) | ||
return address | ||
} | ||
|
||
try { | ||
return encodeAddress(address, ss58Format) | ||
} catch (e) { | ||
console.error(`Error encoding the address ${address}, skipping`, e) | ||
} | ||
} |
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,25 @@ | ||
import { u8aToHex } from '@polkadot/util' | ||
import { decodeAddress } from '@polkadot/util-crypto' | ||
import { HexString } from '../types' | ||
|
||
const decode = (address: string) => { | ||
if (address.startsWith('0x')) { | ||
return null | ||
} | ||
|
||
try { | ||
return u8aToHex(decodeAddress(address)) | ||
} catch (e) { | ||
console.error(`Error decoding the address ${address}, skipping`, e) | ||
return null | ||
} | ||
} | ||
export function getPubKeyFromAddress(address: string[]): HexString[] | ||
export function getPubKeyFromAddress(address: string): HexString | null | ||
export function getPubKeyFromAddress(address: string | string[]) { | ||
if (Array.isArray(address)) { | ||
return address.map(decode).filter((address) => !!address) as HexString[] | ||
} | ||
|
||
return decode(address) | ||
} |
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