How to use the @polkadot/util.isU8a 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 / api / packages / types / src / codec / Enum.ts View on Github external
function decodeFromValue (registry: Registry, def: TypesDef, value?: any): Decoded {
  if (isU8a(value)) {
    return createFromValue(registry, def, value[0], value.subarray(1));
  } else if (isNumber(value)) {
    return createFromValue(registry, def, value);
  } else if (isString(value)) {
    return decodeFromString(registry, def, value.toString());
  } else if (isObject(value)) {
    const key = Object.keys(value)[0];

    return decodeFromJSON(registry, def, key, value[key]);
  }

  // Worst-case scenario, return the first with default
  return createFromValue(registry, def, 0);
}
github polkadot-js / api / packages / types / src / primitive / Generic / AccountIndex.ts View on Github external
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));
  }
github polkadot-js / api / packages / types / src / primitive / Data.ts View on Github external
function decodeData (registry: Registry, value?: Record | Uint8Array | Enum | string): [any, number | undefined] {
  if (!value) {
    return [undefined, undefined];
  } else if (isString(value)) {
    return decodeDataU8a(registry, u8aToU8a(value));
  } else if (isU8a(value)) {
    return decodeDataU8a(registry, value);
  }

  // assume we have an Enum or an  object input, handle this via the normal Enum decoding
  return [value, undefined];
}
github polkadot-js / api / packages / types / src / primitive / Extrinsic / v3 / Extrinsic.ts View on Github external
public static decodeExtrinsic (registry: Registry, value?: Call | Uint8Array | ExtrinsicValueV3, isSigned = false): ExtrinsicValueV3 {
    if (!value) {
      return {};
    } else if (value instanceof ExtrinsicV3) {
      return value;
    } else if (value instanceof ClassOf(registry, 'Call')) {
      return { method: value };
    } else if (isU8a(value)) {
      // here we decode manually since we need to pull through the version information
      const signature = new ExtrinsicSignatureV3(registry, value, { isSigned });
      const method = createType(registry, 'Call', value.subarray(signature.encodedLength));

      return {
        method,
        signature
      };
    }

    return value;
  }
github polkadot-js / api / packages / types / src / primitive / Extrinsic / v1 / Extrinsic.ts View on Github external
public static decodeExtrinsic (registry: Registry, value?: Uint8Array | ExtrinsicValueV1, isSigned = false): ExtrinsicValueV1 {
    if (!value) {
      return {};
    } else if (value instanceof ExtrinsicV1) {
      return value;
    } else if (isU8a(value)) {
      // here we decode manually since we need to pull through the version information
      const signature = new ExtrinsicSignatureV1(registry, value, { isSigned });
      const method = createType(registry, 'Call', value.subarray(signature.encodedLength));

      return {
        method,
        signature
      };
    }

    return value;
  }
github polkadot-js / api / packages / types / src / primitive / Method.ts View on Github external
private static decodeMethod (value: DecodedMethod | Uint8Array | string, _meta?: MetaV0 | MetaV4): DecodedMethod {
    if (isHex(value)) {
      return Method.decodeMethod(hexToU8a(value), _meta);
    } else if (isU8a(value)) {
      // The first 2 bytes are the callIndex
      const callIndex = value.subarray(0, 2);

      // Find metadata with callIndex
      const meta = _meta || Method.findFunction(callIndex).meta;

      return {
        args: value.subarray(2),
        argsDef: Method.getArgsDef(meta),
        callIndex,
        meta
      };
    } else if (isObject(value) && value.callIndex && value.args) {
      // destructure value, we only pass args/methodsIndex out
      const { args, callIndex } = value;
github polkadot-js / apps / packages / app-storage / src / Query.tsx View on Github external
function keyToName (isConst: boolean, _key: Uint8Array | StorageEntryPromise | ConstValue): string {
  if (isConst) {
    const key = _key as ConstValue;

    return `const ${key.section}.${key.method}`;
  }

  const key = _key as Uint8Array | StorageEntryPromise;

  if (isU8a(key)) {
    const u8a = Compact.stripLengthPrefix(key);

    // If the string starts with `:`, handle it as a pure string
    return u8a[0] === 0x3a
      ? u8aToString(u8a)
      : u8aToHex(u8a);
  }

  return `${key.creator.section}.${key.creator.method}`;
}
github polkadot-js / api / packages / types / src / codec / Option.ts View on Github external
public static decodeOption (registry: Registry, Type: Constructor, value?: any): Codec {
    if (isNull(value) || isUndefined(value) || value instanceof Null) {
      return new Null(registry);
    } else if (value instanceof Option) {
      return Option.decodeOption(registry, Type, value.value);
    } else if (value instanceof Type) {
      // don't re-create, use as it (which also caters for derived types)
      return value;
    } else if (isU8a(value)) {
      // the isU8a check happens last in the if-tree - since the wrapped value
      // may be an instance of it, so Type and Option checks go in first
      return Option.decodeOptionU8a(registry, Type, value);
    }

    return new Type(registry, value);
  }
github polkadot-js / api / packages / types / src / primitive / Extrinsic / v2 / Extrinsic.ts View on Github external
public static decodeExtrinsic (registry: Registry, value?: Call | Uint8Array | ExtrinsicValueV2, isSigned = false): ExtrinsicValueV2 {
    if (!value) {
      return {};
    } else if (value instanceof ExtrinsicV2) {
      return value;
    } else if (value instanceof ClassOf(registry, 'Call')) {
      return { method: value };
    } else if (isU8a(value)) {
      // here we decode manually since we need to pull through the version information
      const signature = new ExtrinsicSignatureV2(registry, value, { isSigned });
      const method = createType(registry, 'Call', value.subarray(signature.encodedLength));

      return {
        method,
        signature
      };
    }

    return value;
  }
github polkadot-js / api / packages / types / src / primitive / Bool.ts View on Github external
private static decodeBool (value: any): boolean {
    if (value instanceof Boolean) {
      return value.valueOf();
    } else if (isU8a(value)) {
      return value[0] === 1;
    }

    return !!value;
  }