Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function convertInput (value: string): [boolean, Uint8Array] {
// try hex conversion
try {
return [true, hexToU8a(value)];
} catch (error) {
// we continue...
}
// maybe it is an ss58?
try {
return [true, decodeAddress(value)];
} catch (error) {
// we continue
}
return [value === '0x', new Uint8Array([])];
}
public static decodeAccountIndex (value: AnyNumber): BN | Uint8Array | number | string {
if (value instanceof AccountIndex) {
// `value.toBn()` on AccountIndex returns a pure BN (i.e. not an
// AccountIndex), which has the initial `toString()` implementation.
return value.toBn();
} else if (isBn(value) || isNumber(value) || isHex(value) || isU8a(value)) {
return value;
}
return AccountIndex.decodeAccountIndex(decodeAddress(value));
}
private onScan = (data: string | null): void => {
const { onScan } = this.props;
if (!data || !onScan) {
return;
}
try {
const [prefix, address, genesisHash] = data.split(':');
assert(prefix === ADDRESS_PREFIX, `Invalid address received, expected '${ADDRESS_PREFIX}', found '${prefix}'`);
decodeAddress(address);
onScan({ address, genesisHash });
} catch (error) {
console.error('@polkadot/react-qr:QrScanAddress', error.message, data);
}
}
}
export function createSignPayload (address: string, cmd: number, payload: string | Uint8Array): Uint8Array {
return u8aConcat(
SUBSTRATE_ID,
CRYPTO_SR25519,
new Uint8Array([cmd]),
decodeAddress(address),
u8aToU8a(payload)
);
}
public static getDerivedStateFromProps ({ prefix = IdentityIcon.prefix, value }: Props, prevState: State): State | null {
try {
const address = isU8a(value) || isHex(value)
? encodeAddress(value, prefix)
: (value || '');
const publicKey = u8aToHex(decodeAddress(address, true, prefix));
return address === prevState.address
? null
: { address, publicKey };
} catch (error) {
return {
address: '',
publicKey: '0x'
};
}
}
private static decodeString (registry: Registry, value: string): AccountId | AccountIndex {
const decoded = decodeAddress(value);
return decoded.length === 32
? createType(registry, 'AccountId', decoded)
: createType(registry, 'AccountIndex', u8aToBn(decoded, true));
}
private static decodeAccountId (value: AnyU8a | AnyString): Uint8Array {
if (isU8a(value) || Array.isArray(value)) {
return u8aToU8a(value);
} else if (isHex(value)) {
return hexToU8a(value.toString());
} else if (isString(value)) {
return decodeAddress((value as string).toString());
}
return value;
}
public render (): React.ReactNode {
const { api, systemChain = '', t } = this.props;
const { accountId, didCopy, ethereumAddress, signature, step } = this.state;
const payload = accountId
? (
u8aToString(api.consts.claims.prefix.toU8a(true)) +
u8aToHex(decodeAddress(accountId), -1, false)
)
: '';
return (
<main>
<header>
<h1>
claim your <em>{TokenUnit.abbr}</em> tokens
</h1>
<h3>{t('1. Select your {{chain}} account', {
replace: {
chain: systemChain
}</h3></header></main>
function addressToId (address: string): Uint8Array {
return blake2(decodeAddress(address)).map((x, i): number => (x + 256 - ZERO[i]) % 256);
}