lodash#padStart TypeScript Examples

The following examples show how to use lodash#padStart. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: account.test.ts    From foundry-rpc-js with ISC License 5 votes vote down vote up
function sign(keyPair: EC.KeyPair, message: string) {
    const { r, s, recoveryParam } = keyPair.sign(message, { canonical: true });
    const paddedR = padStart(r.toString(16), 64, "0");
    const paddedS = padStart(s.toString(16), 64, "0");
    const paddedV = padStart(recoveryParam!.toString(16), 2, "0");
    return `0x${paddedR}${paddedS}${paddedV}`;
}
Example #2
Source File: fixLocales.ts    From nextclade with MIT License 5 votes vote down vote up
filepaths.forEach((filepath) => {
  const { json } = readJson(filepath)
  const results = parseLocale(json)

  const keysBefore = results.length
  const resultsFiltered = results.filter(({ reference }) => referenceKeys.includes(reference))
  const keysAfter = resultsFiltered.length
  const keysRemoved = keysBefore - keysAfter

  const resultsFixed = resultsFiltered.map(({ index, missing, extra, reference, localized }) => {
    let missingFixed = missing
    let extraFixed = extra
    let localizedFixed: string | undefined
    if (!isEmpty(missing) && !isEmpty(extra) && missing.length === extra.length) {
      localizedFixed = localized
      const dictionary = safeZip(missing, extra)
      const missingToExtra = Object.fromEntries(dictionary)
      const extraToMissing = Object.fromEntries(dictionary.map(([k, v]) => [v, k]))

      dictionary.forEach(([missing, extra]) => {
        localizedFixed = localizedFixed?.replace(RegExp(extra, 'g'), missing)
      })

      missingFixed = missing.filter((m) => !extra.includes(get(missingToExtra, m)))
      extraFixed = extra.filter((e) => !missing.includes(get(extraToMissing, e)))
    }

    return { index, missing, missingFixed, missingExtra: extraFixed, extra, reference, localized, localizedFixed }
  })

  const contentFixed = resultsFixed.reduce(
    (result, { reference, localized, localizedFixed }) => {
      return {
        result: {
          ...result.result,
          [reference]: localizedFixed ?? localized,
        },
        total: localizedFixed ? result.total + 1 : result.total,
      }
    },
    { result: {}, total: 0 },
  )

  fs.writeJsonSync(filepath, contentFixed.result, { spaces: 2 })

  if (contentFixed.total > 0) {
    console.info(`\nIn file: '${filepath}':\nInfo: corrected ${contentFixed.total} translation issues automatically`)
  }

  if (keysRemoved > 0) {
    console.info(`\nIn file: '${filepath}':\nInfo: removed ${keysRemoved} unused keys`)
  }

  if (resultsFixed.every(({ missingFixed, missingExtra }) => isEmpty(missingFixed) && isEmpty(missingExtra))) {
    return
  }

  const message = resultsFixed
    .filter(({ missingFixed, missingExtra }) => !(isEmpty(missingFixed) && isEmpty(missingExtra)))
    .sort((x, y) => x.index - y.index)
    .map(({ index, missing, extra, reference, localized }) => {
      if (isEmpty(missing) && isEmpty(extra)) {
        return undefined
      }

      const entry = padStart(index.toString(10), 3)
      const missingStr = missing.map(quote).join(', ')
      const extraStr = extra.map(quote).join(', ')
      return `Entry ${entry}:\n    reference: '${reference}'\n    localized: '${localized}'\n    missing  : ${missingStr}\n    extra    : ${extraStr}`
    })
    .filter(notUndefined)
    .join('\n\n')

  if (message !== '') {
    console.warn(
      `\nIn file '${filepath}':\nWarning: translation issues found which cannot be automatically corrected:\n${message}\n`,
    )
  }
})
Example #3
Source File: account.test.ts    From foundry-rpc-js with ISC License 4 votes vote down vote up
describe("account", () => {
    it("create", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-create") });
        const beforeList = await rpc.account.getList();
        const passphrase = `some passphrase ${Math.random()}`;
        const account = await rpc.account.create({ passphrase });
        const afterList = await rpc.account.getList({});
        beforeList.push(account);
        beforeList.sort();
        afterList.sort();
        expect(beforeList).deep.equal(afterList);
    });

    it("importRaw", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-importRaw") });
        const beforeList = await rpc.account.getList({});
        const passphrase = `some passphrase ${Math.random()}`;
        const randomPrivate = secp256k1
            .genKeyPair()
            .getPrivate()
            .toString("hex");
        const secret = `0x${padStart(randomPrivate, 64, "0")}`;
        const account = await rpc.account.importRaw({ secret, passphrase });
        const afterList = await rpc.account.getList();
        beforeList.push(account);
        beforeList.sort();
        afterList.sort();
        expect(beforeList).deep.equal(afterList);
    });

    it("sign", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-sign") });
        const passphrase = `some passphrase ${Math.random()}`;
        const randomKeyPair = secp256k1.genKeyPair();
        const randomPrivate = randomKeyPair.getPrivate().toString("hex");
        const secret = `0x${padStart(randomPrivate, 64, "0")}`;
        const account = await rpc.account.importRaw({ secret, passphrase });
        const message = Buffer.from("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", "hex").toString(
            "hex"
        );
        const signature = await rpc.account.sign({ message: `0x${message}`, account, passphrase });
        const expected = sign(randomKeyPair, message);
        expect(signature).deep.equal(expected);
    });

    it("cannot sign without passphrase", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-sign2") });
        const passphrase = `some passphrase ${Math.random()}`;
        const randomKeyPair = secp256k1.genKeyPair();
        const randomPrivate = randomKeyPair.getPrivate().toString("hex");
        const secret = `0x${padStart(randomPrivate, 64, "0")}`;
        const account = await rpc.account.importRaw({ secret, passphrase });
        const message = Buffer.from("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", "hex").toString(
            "hex"
        );
        await expect(rpc.account.sign({ message: `0x${message}`, account, passphrase: null })).be.rejected;
    });

    it("unlock", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-unlock") });
        const passphrase = `some passphrase ${Math.random()}`;
        const randomKeyPair = secp256k1.genKeyPair();
        const randomPrivate = randomKeyPair.getPrivate().toString("hex");
        const secret = `0x${padStart(randomPrivate, 64, "0")}`;
        const account = await rpc.account.importRaw({ secret, passphrase });
        const message = Buffer.from("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", "hex").toString(
            "hex"
        );
        await rpc.account.unlock({ account, passphrase });
        const signature = await rpc.account.sign({ message: `0x${message}`, account, passphrase: null });
        const expected = sign(randomKeyPair, message);
        expect(signature).deep.equal(expected);
    });

    it("unlock with duration", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-unlock2") });
        const passphrase = `some passphrase ${Math.random()}`;
        const randomKeyPair = secp256k1.genKeyPair();
        const randomPrivate = randomKeyPair.getPrivate().toString("hex");
        const secret = `0x${padStart(randomPrivate, 64, "0")}`;
        const account = await rpc.account.importRaw({ secret, passphrase });
        const message = Buffer.from("1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef", "hex").toString(
            "hex"
        );
        await rpc.account.unlock({ account, passphrase, duration: 3 });
        const signature = await rpc.account.sign({ message: `0x${message}`, account, passphrase: null });
        const expected = sign(randomKeyPair, message);
        expect(signature).deep.equal(expected);

        await wait(4_000);
        await expect(rpc.account.sign({ message: `0x${message}`, account, passphrase: null })).be.rejected;
    });

    it("changePassword", async () => {
        const rpc = new Rpc("http://localhost:8080", { id: generator("account-changePassword") });
        const passphrase1 = `some passphrase ${Math.random()}`;
        const passphrase2 = `another random passphrase ${Math.random()}`;

        const randomKeyPair = secp256k1.genKeyPair();
        const randomPrivate = randomKeyPair.getPrivate().toString("hex");
        const secret = `0x${padStart(randomPrivate, 64, "0")}`;
        const account = await rpc.account.importRaw({ secret, passphrase: passphrase1 });
        const messageStr = Buffer.from(
            "1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
            "hex"
        ).toString("hex");
        const expected = sign(randomKeyPair, messageStr);
        const message = `0x${messageStr}`;

        const signature1 = await rpc.account.sign({ message, account, passphrase: passphrase1 });
        expect(signature1).deep.equal(expected);
        await expect(rpc.account.sign({ message, account, passphrase: passphrase2 })).be.rejected;

        await rpc.account.changePassword({ account, oldPassphrase: passphrase1, newPassphrase: passphrase2 });

        await expect(rpc.account.sign({ message, account, passphrase: passphrase1 })).be.rejected;
        const signature2 = await rpc.account.sign({ message, account, passphrase: passphrase2 });
        expect(signature2).deep.equal(expected);
    });
});