How to use the @polkadot/util.stringToU8a 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 / common / packages / keyring / src / keyring.ts View on Github external
if (isHex(phrase, 256)) {
      seed = hexToU8a(phrase);
    } else {
      const str = phrase as string;
      const parts = str.split(' ');

      if ([12, 15, 18, 21, 24].includes(parts.length)) {
        // FIXME This keeps compat with older versions, but breaks compat with subkey
        // seed = type === 'sr25519'
        //   ? mnemonicToMiniSecret(phrase, password)
        //   : mnemonicToSeed(phrase, password);
        seed = mnemonicToMiniSecret(phrase, password);
      } else {
        assert(str.length <= 32, 'specified phrase is not a valid mnemonic and is invalid as a raw seed at > 32 bytes');

        seed = stringToU8a(str.padEnd(32));
      }
    }

    const keypair = type === 'sr25519'
      ? schnorrkelFromSeed(seed)
      : naclFromSeed(seed);
    const derived = keyFromPath(keypair, path, type);

    return createPair(type, derived, meta, null);
  }
github polkadot-js / client / packages / client-runtime / src / storage / root.spec.js View on Github external
it('creates a basic storage root', () => {
    stateDb.db.put(stringToU8a('doe'), stringToU8a('reindeer'));
    stateDb.db.put(stringToU8a('dog'), stringToU8a('puppy'));
    stateDb.db.put(stringToU8a('dogglesworth'), stringToU8a('cat'));

    storage_root(5);

    expect(
      heap.set
    ).toHaveBeenCalledWith(
      5,
      new Uint8Array([
        11, 65, 228, 136, 204, 203, 214, 125, 31, 16, 137, 89, 44, 44, 35, 95, 92, 83, 153, 176, 83, 247, 254, 145, 82, 221, 75, 95, 39, 153, 20, 205
      ])
    );
  });
});
github polkadot-js / common / packages / trie-hash / src / trieRootOrdered.spec.ts View on Github external
it('encodes values', (): void => {
    expect(
      u8aToHex(
        trieRootOrdered([
          stringToU8a('doe'),
          stringToU8a('reindeer')
        ])
      )
    ).toEqual('0xb9b1bb07e481f0393e15f32f34abd665f7a698786a7ec9feb31b2e8927ad5f86');
  });
});
github polkadot-js / common / packages / util-crypto / src / nacl / sign.spec.ts View on Github external
it('returns a valid signature for the message', (): void => {
    expect(
      naclSign(
        new Uint8Array([0x61, 0x62, 0x63, 0x64]),
        naclKeypairFromSeed(
          stringToU8a('12345678901234567890123456789012')
        )
      )
    ).toEqual(
      new Uint8Array([28, 58, 206, 239, 249, 70, 59, 191, 166, 40, 219, 218, 235, 170, 25, 79, 10, 94, 9, 197, 34, 126, 1, 150, 246, 68, 28, 238, 36, 26, 172, 163, 168, 90, 202, 211, 126, 246, 57, 212, 43, 24, 88, 197, 240, 113, 118, 76, 37, 81, 91, 110, 236, 50, 144, 134, 100, 223, 220, 238, 34, 185, 211, 7])
    );
  });
});
github polkadot-js / common / packages / util-crypto / src / schnorrkel / keypair / fromSeed.spec.ts View on Github external
describe('schnorrkelKeypairFromSeed', (): void => {
  const TEST = stringToU8a('12345678901234567890123456789012');
  const RESULT = {
    publicKey: new Uint8Array([116, 28, 8, 160, 111, 65, 197, 150, 96, 143, 103, 116, 37, 155, 217, 4, 51, 4, 173, 250, 93, 62, 234, 98, 118, 11, 217, 190, 151, 99, 77, 99]),
    secretKey: new Uint8Array([240, 16, 102, 96, 195, 221, 162, 63, 22, 218, 169, 172, 91, 129, 27, 150, 48, 119, 245, 188, 10, 248, 159, 133, 128, 79, 13, 232, 228, 36, 240, 80, 249, 141, 102, 243, 148, 66, 80, 111, 249, 71, 253, 145, 31, 24, 199, 167, 165, 218, 99, 154, 99, 232, 211, 180, 226, 51, 247, 65, 67, 217, 81, 193])
  };

  beforeEach(async (): Promise => {
    await waitReady();
  });

  it('generates a valid publicKey/secretKey pair (u8a)', (): void => {
    expect(
      schnorrkelKeypairFromSeed(TEST)
    ).toEqual(RESULT);
  });

  tests.forEach(([mnemonic, , , secret], index): void => {
github polkadot-js / client / packages / client-runtime / src / storage / enumerated.spec.js View on Github external
it('calculates a basic ennumerated root', () => {
    expect(
      enumerated([
        stringToU8a('doe'),
        stringToU8a('reindeer')
      ])
    ).toEqual(
      new Uint8Array([
        185, 177, 187, 7, 228, 129, 240, 57, 62, 21, 243, 47, 52, 171, 214, 101, 247, 166, 152, 120, 106, 126, 201, 254, 179, 27, 46, 137, 39, 173, 95, 134
      ])
    );
  });
});
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 / common / packages / util-crypto / src / key / DeriveJunction.ts View on Github external
public soft (value: number | BN | string | Uint8Array): DeriveJunction {
    if (isNumber(value) || isBn(value)) {
      return this.soft(bnToHex(value, BN_OPTIONS));
    } else if (isString(value)) {
      return isHex(value)
        ? this.soft(hexToU8a(value))
        : this.soft(compactAddLength(stringToU8a(value)));
    }

    if (value.length > JUNCTION_ID_LEN) {
      return this.soft(blake2AsU8a(value));
    }

    this._chainCode.fill(0);
    this._chainCode.set(value, 0);

    return this;
  }
github dappforce / dappforce-subsocial-ui / src / components / main / Status.tsx View on Github external
componentDidUpdate ({ optionsAll = { account: [] as any } as KeyringOptions, queueAction, system_events, t }: Props) {
    const eventHash = xxhashAsHex(stringToU8a(JSON.stringify(system_events || [])));

    if (eventHash === prevEventHash) {
      return;
    }

    prevEventHash = eventHash;

    const addresses = optionsAll.account.map((account) => account.value);

    (system_events || []).forEach(({ event: { data, method, section } }) => {
      if (section === 'balances' && method === 'Transfer') {
        const account = data[1].toString();

        if (addresses.includes(account)) {
          queueAction({
            account,
github polkadot-js / apps / packages / app-explorer / src / EventsRecent.tsx View on Github external
static getDerivedStateFromProps ({ system_events = [] }: Props, prevState: State): State | null {
    const prevEventHash = xxhashAsHex(stringToU8a(JSON.stringify(system_events)));

    if (prevEventHash === prevState.prevEventHash) {
      return null;
    }

    const recentEvents = system_events
      .filter(({ event }) => event.section !== 'system')
      .concat(prevState.recentEvents)
      .filter((_, index) => index < MAX_ITEMS);

    return {
      prevEventHash,
      recentEvents
    };
  }