How to use the @polkadot/util.u8aConcat function in @polkadot/util

To help you get started, we’ve selected a few @polkadot/util examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github polkadot-js / apps / packages / app-staking / src / Actions / Account / index.tsx View on Github external
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[],
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / createFunction.ts View on Github external
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));
}
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / getHasher.ts View on Github external
  isTwox64Concat: (data: HasherInput): Uint8Array => u8aConcat(xxhashAsU8a(data, 64), u8aToU8a(data))
};
github polkadot-js / ui / packages / react-qr / src / util.ts View on Github external
return frames.map((frame, index: number): Uint8Array =>
    u8aConcat(
      MULTIPART,
      encodeNumber(frames.length),
      encodeNumber(index),
      frame
    )
  );
github polkadot-js / common / packages / trie-codec / src / encode.ts View on Github external
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
  );
}
github polkadot-js / api / packages / types / src / codec / VecFixed.ts View on Github external
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;
  }
github polkadot-js / api / packages / types / src / codec / Enum.ts View on Github external
public toU8a (isBare?: boolean): Uint8Array {
    const index = this._indexes[this._index];

    return u8aConcat(
      new Uint8Array([index]),
      this.raw.toU8a(isBare)
    );
  }
}
github polkadot-js / common / packages / keyring / src / pair / encode.ts View on Github external
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);
}
github polkadot-js / api / packages / types / src / primitive / Extrinsic / Extrinsic.ts View on Github external
public toU8a (isBare?: boolean): Uint8Array {
    const encoded = u8aConcat(new Uint8Array([this.version]), this.raw.toU8a(isBare));

    return isBare
      ? encoded
      : Compact.addLengthPrefix(encoded);
  }
}