Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getStakeState (allStashes: string[] | undefined, { controllerId, nextSessionIds, nominators, rewardDestination, sessionIds, stakers, stakingLedger, validatorPrefs }: DerivedStakingAccount, stashId: string, validateInfo: ValidatorInfo): StakeState {
const isStashNominating = !!(nominators?.length);
const isStashValidating = !validateInfo[1].isEmpty || !!allStashes?.includes(stashId);
const nextConcat = u8aConcat(...nextSessionIds.map((id): Uint8Array => id.toU8a()));
const currConcat = u8aConcat(...sessionIds.map((id): Uint8Array => id.toU8a()));
return {
controllerId: toIdString(controllerId),
destination: rewardDestination?.toNumber() || 0,
hexSessionIdNext: u8aToHex(nextConcat, 48),
hexSessionIdQueue: u8aToHex(currConcat.length ? currConcat : nextConcat, 48),
isLoading: false,
isStashNominating,
isStashValidating,
// we assume that all ids are non-null
nominees: nominators?.map(toIdString) as string[],
sessionIds: (
nextSessionIds.length
? nextSessionIds
: sessionIds
).map(toIdString) as string[],
function createKeyDoubleMap (registry: Registry, { meta: { name, type } }: CreateItemFn, rawKey: Uint8Array, args: [CreateArgType, CreateArgType], [hasher, key2Hasher]: [HasherFunction, HasherFunction?]): Uint8Array {
// since we are passing an almost-unknown through, trust, but verify
assert(
Array.isArray(args) && !isUndefined(args[0]) && !isNull(args[0]) && !isUndefined(args[1]) && !isNull(args[1]),
`${name} is a DoubleMap and requires two arguments`
);
const [key1, key2] = args;
const type1 = type.asDoubleMap.key1.toString();
const type2 = type.asDoubleMap.key2.toString();
const param1Encoded = u8aConcat(rawKey, createTypeUnsafe(registry, type1, [key1]).toU8a(true));
const param1Hashed = hasher(param1Encoded);
// If this fails it means the getHashers function failed - and we have much bigger issues
const param2Hashed = (key2Hasher as HasherFunction)(createTypeUnsafe(registry, type2, [key2]).toU8a(true));
// as per createKey, always add the length prefix (underlying it is Bytes)
return Compact.addLengthPrefix(u8aConcat(param1Hashed, param2Hashed));
}
isTwox64Concat: (data: HasherInput): Uint8Array => u8aConcat(xxhashAsU8a(data, 64), u8aToU8a(data))
};
return frames.map((frame, index: number): Uint8Array =>
u8aConcat(
MULTIPART,
encodeNumber(frames.length),
encodeNumber(index),
frame
)
);
function _encodeBranch (header: NodeHeader, input: (null | Uint8Array)[]): Uint8Array {
const values: Uint8Array[] = [];
let bitmap = 0;
for (let index = 0; index < BRANCH_VALUE_INDEX; index++) {
const value = input[index];
if (value) {
bitmap |= BITMAP[index];
values.push(encodeValue(value));
}
}
return u8aConcat(
header.toU8a(),
new Uint8Array([(bitmap & 0xff), (bitmap >> 8)]),
encodeValue(input[BRANCH_VALUE_INDEX]),
...values
);
}
public static decodeVecFixed (registry: Registry, Type: Constructor, allocLength: number, value: VecFixed | Uint8Array | string | any[]): T[] {
const values = Vec.decodeVec(
registry,
Type,
isU8a(value)
? u8aConcat(compactToU8a(allocLength), value)
: value
);
while (values.length < allocLength) {
values.push(new Type(registry));
}
assert(values.length === allocLength, `Expected a length of exactly ${allocLength} entries`);
return values;
}
public toU8a (isBare?: boolean): Uint8Array {
const index = this._indexes[this._index];
return u8aConcat(
new Uint8Array([index]),
this.raw.toU8a(isBare)
);
}
}
assert(secretKey, 'Expected a valid secretKey to be passed to encode');
const encoded = u8aConcat(
PKCS8_HEADER,
secretKey,
PKCS8_DIVIDER,
publicKey
);
if (!passphrase) {
return encoded;
}
const { encrypted, nonce } = naclEncrypt(encoded, u8aFixLength(stringToU8a(passphrase), 256, true));
return u8aConcat(nonce, encrypted);
}
public toU8a (isBare?: boolean): Uint8Array {
const encoded = u8aConcat(new Uint8Array([this.version]), this.raw.toU8a(isBare));
return isBare
? encoded
: Compact.addLengthPrefix(encoded);
}
}