Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// This allows us to instantiate an address with a raw publicKey. Do this first before
// we checking the first byte, otherwise we may split an already-existent valid address
if (value.length === 32) {
return new AccountId(value);
} else if (value[0] === 0xff) {
return new AccountId(value.subarray(1));
}
const [offset, length] = AccountIndex.readLength(value);
return new AccountIndex(u8aToBn(value.subarray(offset, offset + length), true));
} else if (isHex(value)) {
return Address.decodeAddress(hexToU8a(value));
}
const decoded = decodeAddress(value);
return decoded.length === 32
? new AccountId(decoded)
: new AccountIndex(u8aToBn(decoded, true));
}
function toHex(address: string): string {
return u8aToHex(
// When saving pre-checksum changes, ensure that we can decode
decodeAddress(address, true)
);
}
function toHex (address: string): string {
return u8aToHex(
// When saving pre-checksum changes, ensure that we can decode
decodeAddress(address, true)
);
}
return async (address: AccountId | AccountIndex | string | null | undefined, cb: (idAndIndex: IdAndIndex) => any): PromiseSubscription => {
try {
// yes, this can fail, don't care too much, catch will catch it
const decoded = decodeAddress((address as any).toString());
if (decoded.length === 32) {
const accountId = new AccountId(decoded);
return accountIdToIndex(api)(accountId, (accountIndex?: AccountIndex) =>
cb([accountId, accountIndex])
);
}
const accountIndex = new AccountIndex(address as string);
return accountIndexToId(api)(accountIndex, (accountId?: AccountId) =>
cb([accountId, accountIndex])
);
} catch (error) {
// swallow
accountIdAndIndex = (address?: AccountId | AccountIndex | string | null): Observable<[AccountId | undefined, AccountIndex | undefined]> => {
try {
// yes, this can fail, don't care too much, catch will catch it
const length = decodeAddress((address as any).toString()).length;
assert([1, 2, 4, 8, 32].indexOf(length) !== -1, `Invalid length for decoded address, found ${length}`);
if (length === 32) {
const accountId = new AccountId(address as string);
return this
.accountIndexFromId(accountId)
.pipe(
map((accountIndex?: AccountIndex): [AccountId, AccountIndex | undefined] =>
[accountId, accountIndex]
)
);
}
const accountIndex = new AccountIndex(address as string);
static decodeAccountIndex (value: AnyNumber): BN | Uint8Array | number | string {
if (value instanceof UInt) {
return value.raw;
} else if (value instanceof U8a) {
return AccountIndex.decodeAccountIndex(value.raw);
} else if (isBn(value) || isNumber(value) || isU8a(value)) {
return value;
} else if (isHex(value)) {
// Here we convert via hexToU8a since we expect the LE encoded value representation. This
// is different than UInt where we expect a BE (human-readable representation)
return hexToU8a(value);
}
return AccountIndex.decodeAccountIndex(decodeAddress(value));
}
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 AnyString).toString());
}
return value;
}