How to use the @polkadot/util-crypto.xxhashAsU8a function in @polkadot/util-crypto

To help you get started, we’ve selected a few @polkadot/util-crypto 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 / api / packages / type-storage / src / fromMetadata / v0 / createFunction.ts View on Github external
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)
    );
  };

  if (meta.type.isMap && meta.type.asMap.isLinked) {
    // TODO: there needs some better way to do this
    const keyHash = new U8a(xxhashAsU8a(`head of ${stringKey}`, 128));
    const keyFn: any = () => keyHash;
    keyFn.meta = new StorageFunctionMetadata({
      name: meta.name,
      modifier: new StorageFunctionModifier('Required'),
      type: new StorageFunctionType(new PlainType(meta.type.asMap.key), 0),
      fallback: new Bytes(),
      documentation: meta.documentation
    });
    storageFn.headKey = new StorageKey(keyFn);
  }

  storageFn.meta = meta;
  storageFn.method = stringLowerFirst(method.toString());
  storageFn.section = stringLowerFirst(section.toString());
  storageFn.toJSON = (): any => meta.toJSON();
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / getHasher.spec.ts View on Github external
it('matches the foo test from Rust', (): void => {
      const hasher = getHasher(new StorageHasher(registry, 'Twox64Concat'));
      const hash = hasher('foo');
      const xxhash = xxhashAsU8a('foo', 128);

      expect([
        hash.subarray(0, 8),
        hash.subarray(8)
      ]).toEqual([
        xxhash.subarray(0, 8),
        stringToU8a('foo')
      ]);
    });
  });
github polkadot-js / client / packages / client-runtime / src / crypto / index.ts View on Github external
const twox = (bitLength: number, dataPtr: Pointer, dataLen: number, outPtr: Pointer): void => {
    const data = heap.get(dataPtr, dataLen);
    const hash = xxhashAsU8a(data, bitLength);

    l.debug((): any[] => [`twox_${bitLength}`, [dataPtr, dataLen, outPtr], '<-', u8aDisplay(data), '->', u8aToHex(hash)]);

    heap.set(outPtr, hash);
  };
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 / api / packages / metadata / src / Decorated / storage / fromMetadata / getHasher.ts View on Github external
  isTwox128: (data: HasherInput): Uint8Array => xxhashAsU8a(data, 128),
  isTwox256: (data: HasherInput): Uint8Array => xxhashAsU8a(data, 256),
github polkadot-js / api / packages / metadata / src / Decorated / storage / fromMetadata / getHasher.ts View on Github external
  isTwox256: (data: HasherInput): Uint8Array => xxhashAsU8a(data, 256),
  isTwox64Concat: (data: HasherInput): Uint8Array => u8aConcat(xxhashAsU8a(data, 64), u8aToU8a(data))