Use browser friendly hexToBytes, bytesToHex methods

This commit is contained in:
dapplion 2020-11-28 19:52:32 +00:00
parent 2c34db8b8e
commit 8055f73afb
18 changed files with 81 additions and 29 deletions

View File

@ -1,5 +1,5 @@
import * as blst from "@chainsafe/blst";
import {bytesToHex, hexToBytes, randomBytes} from "../helpers/utils";
import {bytesToHex, hexToBytes, randomBytes} from "../helpers";
import {SECRET_KEY_LENGTH} from "../constants";
import {IPrivateKey} from "../interface";
import {PublicKey} from "./publicKey";

View File

@ -1,5 +1,5 @@
import * as blst from "@chainsafe/blst";
import {bytesToHex, hexToBytes} from "../helpers/utils";
import {bytesToHex, hexToBytes} from "../helpers";
import {IPublicKey} from "../interface";
import {Signature} from "./signature";

View File

@ -1,5 +1,5 @@
import * as blst from "@chainsafe/blst";
import {bytesToHex, hexToBytes} from "../helpers/utils";
import {bytesToHex, hexToBytes} from "../helpers";
import {ISignature} from "../interface";
import {PublicKey} from "./publicKey";

38
src/helpers/hex.ts Normal file
View File

@ -0,0 +1,38 @@
/**
* Browser compatible fromHex method
* From https://github.com/herumi/bls-eth-wasm/blob/04eedb77aa96e66b4f65a0ab477228adf8090c36/src/bls.js#L62
*/
export function hexToBytes(hex: string): Uint8Array {
if (hex.startsWith("0x")) {
hex = hex.slice(2);
}
if (hex.length & 1) {
throw Error("hexToBytes:length must be even " + hex.length);
}
const n = hex.length / 2;
const a = new Uint8Array(n);
for (let i = 0; i < n; i++) {
a[i] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
}
return a;
}
/**
* Browser compatible toHex method
* From https://github.com/herumi/bls-eth-wasm/blob/04eedb77aa96e66b4f65a0ab477228adf8090c36/src/bls.js#L50
*/
export function bytesToHex(bytes: Uint8Array): string {
// return "0x" + Buffer.from(bytes).toString("hex");
let s = "";
const n = bytes.length;
for (let i = 0; i < n; i++) {
s += ("0" + bytes[i].toString(16)).slice(-2);
}
return "0x" + s;
}

View File

@ -1 +1,2 @@
export * from "./hex";
export * from "./utils";

View File

@ -9,14 +9,6 @@ export function assert(condition: unknown, message = "Assertion failed"): assert
}
}
export function hexToBytes(hex: string): Uint8Array {
return Buffer.from(hex.replace("0x", ""), "hex");
}
export function bytesToHex(bytes: Uint8Array): string {
return "0x" + Buffer.from(bytes).toString("hex");
}
export function isEqualBytes(a: Buffer | Uint8Array, b: Buffer | Uint8Array): boolean {
return toBuffer(a).equals(toBuffer(b));
}

View File

@ -5,7 +5,7 @@ import {SECRET_KEY_LENGTH} from "../constants";
import {getContext} from "./context";
import {PublicKey} from "./publicKey";
import {Signature} from "./signature";
import {bytesToHex, hexToBytes} from "../helpers/utils";
import {bytesToHex, hexToBytes} from "../helpers";
import {IPrivateKey} from "../interface";
export class PrivateKey implements IPrivateKey {

View File

@ -2,7 +2,7 @@ import {PublicKeyType} from "bls-eth-wasm";
import {getContext} from "./context";
import {EMPTY_PUBLIC_KEY} from "../constants";
import {Signature} from "./signature";
import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers/utils";
import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers";
import {IPublicKey} from "../interface";
export class PublicKey implements IPublicKey {

View File

@ -3,7 +3,7 @@ import {SIGNATURE_LENGTH, EMPTY_SIGNATURE} from "../constants";
import {SignatureType} from "bls-eth-wasm";
import {getContext} from "./context";
import {PublicKey} from "./publicKey";
import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers/utils";
import {bytesToHex, hexToBytes, isEqualBytes} from "../helpers";
import {ISignature} from "../interface";
export class Signature implements ISignature {

View File

@ -1,6 +1,6 @@
import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {bytesToHex, hexToBytes} from "../../src/helpers/utils";
import {bytesToHex, hexToBytes} from "../../src/helpers";
import {SPEC_TESTS_DIR} from "../params";
import {describeForAllImplementations} from "../switch";

View File

@ -1,6 +1,6 @@
import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils";
import {hexToBytes} from "../../src/helpers";
import {SPEC_TESTS_DIR} from "../params";
import {describeForAllImplementations} from "../switch";

View File

@ -1,6 +1,6 @@
import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils";
import {hexToBytes} from "../../src/helpers";
import {SPEC_TESTS_DIR} from "../params";
import {describeForAllImplementations} from "../switch";

View File

@ -1,6 +1,6 @@
import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {bytesToHex, hexToBytes} from "../../src/helpers/utils";
import {bytesToHex, hexToBytes} from "../../src/helpers";
import {SPEC_TESTS_DIR} from "../params";
import {describeForAllImplementations} from "../switch";

View File

@ -1,6 +1,6 @@
import path from "path";
import {describeDirectorySpecTest, InputType} from "@chainsafe/lodestar-spec-test-util";
import {hexToBytes} from "../../src/helpers/utils";
import {hexToBytes} from "../../src/helpers";
import {SPEC_TESTS_DIR} from "../params";
import {describeForAllImplementations} from "../switch";

View File

@ -0,0 +1,29 @@
import {expect} from "chai";
import {hexToBytes, bytesToHex} from "../../../src/helpers/hex";
describe("helpers / hex", () => {
const testCases: {id: string; hex: string}[] = [
{
id: "pubkey",
hex: "0xb6f21199594b56d77670564bf422cb331d5281ca2c1f9a45588a56881d8287ef8619efa6456d6cd2ef61306aa5b21311",
},
];
for (const {id, hex} of testCases) {
it(`${id} hexToBytes`, () => {
const expectedBytes = hexToBytesNode(hex);
const bytes = hexToBytes(hex);
expect(expectedBytes.equals(bytes)).to.be.true;
});
it(`${id} bytesToHex`, () => {
const bytes = hexToBytesNode(hex);
const _hex = bytesToHex(bytes);
expect(_hex).to.equal(hex);
});
}
});
function hexToBytesNode(hex: string): Buffer {
return Buffer.from(hex.replace("0x", ""), "hex");
}

View File

@ -16,7 +16,7 @@ export function runPrivateKeyTests(bls: IBls): void {
});
it("should export private key to hex string from non-prefixed hex", () => {
expect(bls.PrivateKey.fromHex(privateKey.replace("0x", "")).toHex()).to.be.equal(privateKey);
expect(bls.PrivateKey.fromHex(privateKey).toHex()).to.be.equal(privateKey);
});
it("should not accept too short private key", () => {

View File

@ -11,7 +11,7 @@ export function runPublicKeyTests(bls: IBls): void {
});
it("should export public key to hex string from non-prefixed hex", () => {
expect(bls.PublicKey.fromHex(publicKey.replace("0x", "")).toHex()).to.be.equal(publicKey);
expect(bls.PublicKey.fromHex(publicKey).toHex()).to.be.equal(publicKey);
});
it("from private key", () => {

View File

@ -1,13 +1,5 @@
import {randomBytes} from "../src/helpers";
export function fromHexString(hex: string): Uint8Array {
return Buffer.from(hex.replace("0x", ""), "hex");
}
export function toHexString(bytes: Buffer | Uint8Array): string {
return `0x${Buffer.from(bytes).toString("hex")}`;
}
export function randomMessage(): Uint8Array {
return randomBytes(32);
}