How to use the @polkadot/types.Compact.addLengthPrefix function in @polkadot/types

To help you get started, we’ve selected a few @polkadot/types 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 / api / packages / type-storage / src / fromMetadata / v0 / createFunction.ts View on Github external
const storageFn = (arg?: any): Uint8Array => {
    let key = rawKey;
    if (meta.type.isMap) {
      assert(!isUndefined(arg) && !isNull(arg), `${meta.name} expects one argument`);

      const type = meta.type.asMap.key.toString();
      const param = createType(type, arg).toU8a(false);

      key = u8aConcat(key, param);
    }

    // StorageKey is a Bytes, so is length-prefixed
    return Compact.addLengthPrefix(
      options.isUnhashed
        ? key
        : xxhashAsU8a(key, 128)
    );
  };
github polkadot-js / apps / packages / react-params / src / Param / KeyValue.tsx View on Github external
export function createParam (hex: string | String, length = -1): StateParam {
  let u8a;

  try {
    u8a = hexToU8a(hex.toString());
  } catch (error) {
    u8a = new Uint8Array([]);
  }

  const isValid = length !== -1
    ? u8a.length === length
    : u8a.length !== 0;

  return {
    isValid,
    u8a: Compact.addLengthPrefix(u8a)
  };
}
github polkadot-js / api / packages / api-contract / src / ContractRegistry.ts View on Github external
const encoder = (...params: CodecArg[]): Uint8Array => {
      assert(params.length === args.length, `Expected ${args.length} arguments to contract ${name}, found ${params.length}`);

      const u8a = new Clazz(
        this.registry,
        args.reduce((mapped, { name }, index): Record => {
          mapped[name] = params[index];

          return mapped;
        }, { ...baseStruct })
      ).toU8a();

      return Compact.addLengthPrefix(u8a);
    };
github polkadot-js / apps / packages / react-params / src / Param / BaseBytes.tsx View on Github external
const _onChange = (hex: string): void => {
    let [isValid, value] = convertInput(hex);

    isValid = isValid && validate(value) && (
      length !== -1
        ? value.length === length
        : value.length !== 0
    );

    if (withLength && isValid) {
      value = Compact.addLengthPrefix(value);
    }

    onChange && onChange({
      isValid,
      value: asHex
        ? u8aToHex(value)
        : value
    });

    setIsValid(isValid);
  };
github polkadot-js / apps / packages / app-storage / src / Selection / Raw.tsx View on Github external
const _onChangeKey = (key: string): void => {
    const u8a = u8aToU8a(key);
    const isValid = u8a.length !== 0;

    setValue({
      isValid,
      key: Compact.addLengthPrefix(u8a)
    });
  };
github polkadot-js / apps / packages / react-params / src / Param / Bytes.tsx View on Github external
const _onChangeFile = (value: Uint8Array): void => {
    onChange && onChange({
      isValid: value.length !== 0,
      value: Compact.addLengthPrefix(value)
    });
  };
github polkadot-js / apps / packages / react-params / src / Param / Bytes.tsx View on Github external
const _onChangeFile = (value: Uint8Array): void => {
    onChange && onChange({
      isValid: value.length !== 0,
      value: Compact.addLengthPrefix(value)
    });
  };