How to use the @polkadot/util.u8aToU8a 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 / rpc-core / src / index.ts View on Github external
private formatStorageData (key: StorageKey, value: string | null): Codec {
    // single return value (via state.getStorage), decode the value based on the
    // outputType that we have specified. Fallback to Raw on nothing
    const type = key.outputType || 'Raw';
    const meta = key.meta || EMPTY_META;
    const isEmpty = isNull(value);

    // we convert to Uint8Array since it maps to the raw encoding, all
    // data will be correctly encoded (incl. numbers, excl. :code)
    const input = isEmpty
      ? null
      : this.treatAsHex(key)
        ? value
        : u8aToU8a(value);

    if (meta.modifier.isOptional) {
      return new Option(
        this.registry,
        createClass(this.registry, type),
        isEmpty
          ? null
          : createTypeUnsafe(this.registry, type, [input], true)
      );
    }

    return createTypeUnsafe(this.registry, type, [isEmpty ? meta.fallback : input], true);
  }
github polkadot-js / api / packages / types / src / type / Extrinsic.ts View on Github external
static decodeExtrinsic (value: ExtrinsicValue | AnyU8a | Method): ExtrinsicValue | Array | Uint8Array {
    if (Array.isArray(value) || isHex(value)) {
      // Instead of the block below, it should simply be:
      // return Extrinsic.decodeExtrinsic(hexToU8a(value as string));
      const u8a = u8aToU8a(value);

      // HACK 11 Jan 2019 - before https://github.com/paritytech/substrate/pull/1388
      // extrinsics didn't have the length, cater for both approaches
      const [offset, length] = Compact.decodeU8a(u8a);
      const withPrefix = u8a.length === (offset + length.toNumber());

      return Extrinsic.decodeExtrinsic(
        withPrefix
          ? u8a
          : Compact.addLengthPrefix(u8a)
      );
    } else if (isU8a(value)) {
      const [offset, length] = Compact.decodeU8a(value);

      return value.subarray(offset, offset + length.toNumber());
    } else if (value instanceof Method) {
github polkadot-js / api / packages / types / src / primitive / Bytes.ts View on Github external
private static decodeBytes (value?: AnyU8a): Uint8Array | undefined {
    if (Array.isArray(value) || isString(value)) {
      return u8aToU8a(value);
    } else if (!(value instanceof Raw) && isU8a(value)) {
      // We are ensuring we are not a Rawv instance. In the case of a Raw we already have gotten
      // rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected
      return Bytes.decodeBytesU8a(value);
    }

    return value;
  }
github polkadot-js / common / packages / trie-db / src / snapshot.spec.ts View on Github external
describe('snapshots', (): void => {
  const values = [
    { k: toU8a('test'), v: toU8a('one') },
    { k: toU8a('one'), v: toU8a('testing') },
    { k: toU8a('two'), v: toU8a('testing with a much longer value here') },
    { k: toU8a('twzei'), v: toU8a('und Deutch') },
    { k: toU8a('do'), v: toU8a('do it') },
    { k: toU8a('dog'), v: toU8a('doggie') },
    { k: toU8a('dogge'), v: toU8a('bigger dog') },
    { k: toU8a('dodge'), v: toU8a('coin') }
  ];
  let trie: Trie;
  let back: Trie;

  beforeEach((): void => {
    trie = new Trie();
    back = new Trie();
  });

  it('creates a snapshot of the (relevant) trie data', (): void => {
    const root = trieRoot([values[0]]);

    trie.put(values[0].k, values[0].v);
    trie.put(values[0].k, toU8a('two'));
    trie.del(values[0].k);
    trie.put(values[0].k, values[0].v);
github polkadot-js / common / packages / util-crypto / src / keccak / asU8a.ts View on Github external
export default function keccakAsU8a (value: Buffer | Uint8Array | string): Uint8Array {
  return isReady()
    ? keccak256(u8aToU8a(value))
    : new Uint8Array(
      jssha3.keccak256.update(value).arrayBuffer()
    );
}
github polkadot-js / api / packages / types / src / codec / BTreeMap.ts View on Github external
private static decodeBTreeMap (registry: Registry, KeyClass: Constructor, ValClass: Constructor, value: Uint8Array | string | Map): Map {
    if (!value) {
      return new Map();
    } else if (isHex(value)) {
      return BTreeMap.decodeBTreeMap(registry, KeyClass, ValClass, hexToU8a(value));
    } else if (isU8a(value)) {
      return BTreeMap.decodeBTreeMapFromU8a(registry, KeyClass, ValClass, u8aToU8a(value));
    } else if (value instanceof Map) {
      return BTreeMap.decodeBTreeMapFromMap(registry, KeyClass, ValClass, value);
    }

    throw new Error('BTreeMap: cannot decode type');
  }
github polkadot-js / common / packages / util-crypto / src / nacl / verify.ts View on Github external
export default function naclVerify (message: Uint8Array | string, signature: Uint8Array | string, publicKey: Uint8Array | string): boolean {
  const messageU8a = u8aToU8a(message);
  const signatureU8a = u8aToU8a(signature);
  const publicKeyU8a = u8aToU8a(publicKey);

  return isReady()
    ? ed25519Verify(signatureU8a, messageU8a, publicKeyU8a)
    : nacl.sign.detached.verify(messageU8a, signatureU8a, publicKeyU8a);
}
github polkadot-js / common / packages / trie-db / src / snapshot.spec.ts View on Github external
describe('snapshots', (): void => {
  const values = [
    { k: toU8a('test'), v: toU8a('one') },
    { k: toU8a('one'), v: toU8a('testing') },
    { k: toU8a('two'), v: toU8a('testing with a much longer value here') },
    { k: toU8a('twzei'), v: toU8a('und Deutch') },
    { k: toU8a('do'), v: toU8a('do it') },
    { k: toU8a('dog'), v: toU8a('doggie') },
    { k: toU8a('dogge'), v: toU8a('bigger dog') },
    { k: toU8a('dodge'), v: toU8a('coin') }
  ];
  let trie: Trie;
  let back: Trie;

  beforeEach((): void => {
    trie = new Trie();
    back = new Trie();
  });
github polkadot-js / client / packages / client-rpc / src / handlers / state.ts View on Github external
async ([key]: string[]): Promise =>
    u8aToHex(
      db.get(u8aToU8a(key))
    );
github polkadot-js / common / packages / util-crypto / src / blake2 / asU8a.ts View on Github external
export default function blake2AsU8a (data: Uint8Array | string, bitLength = 256, key: Uint8Array | null = null): Uint8Array {
  const byteLength = Math.ceil(bitLength / 8);

  return isReady()
    ? blake2b(u8aToU8a(data), u8aToU8a(key), byteLength)
    : blakejs.blake2b(data, key, byteLength);
}