postchain-client
Version:
Client library for accessing a Postchain node through REST.
66 lines • 3.28 kB
JavaScript
import { checkSignature, createPublicKey, makeKeyPair, makeTuid, randomBytes, sign, verifyKeyPair, } from "../../../src/encryption/encryption";
import { toBuffer } from "../../../src/formatter";
describe("Encryption test", () => {
describe("Random bytes test", () => {
it("randomBytes returns random bytes, different every time", () => {
const bytes1 = randomBytes(40);
expect(bytes1.length).toBe(40);
const bytes2 = randomBytes(40);
expect(bytes2.length).toBe(40);
expect(bytes1.equals(bytes2)).toBe(false);
});
it("randomBytes returns empty buffer of size 0", () => {
expect(randomBytes(0).length).toBe(0);
});
it("randomBytes throws on negative size", () => {
expect(() => randomBytes(-1)).toThrow(new RangeError('The value of "size" is out of range. It must be >= 0 && <= 2147483647. Received -1'));
});
it("randomBytes rounds non-integer size down to the nearest integer", () => {
expect(randomBytes(3.14).length).toBe(3);
});
});
describe("Public key tests", () => {
it("createPublicKey creates a valid secp256k1 pubKey", () => {
const privKey = toBuffer("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa");
const content = toBuffer("hello");
const pubKey = createPublicKey(privKey);
const signature = sign(content, privKey);
expect(checkSignature(content, pubKey, signature)).toBe(true);
});
it("createPublicKey throws on invalid privKey", () => {
expect(() => createPublicKey(randomBytes(0))).toThrow();
expect(() => createPublicKey(randomBytes(31))).toThrow();
expect(() => createPublicKey(randomBytes(33))).toThrow();
});
});
describe("Tuid tests", () => {
it("makes a tuid", () => {
const tuid = makeTuid();
expect(toBuffer(tuid).length).toBe(16);
expect(tuid.length).toBe(32);
});
});
describe("Key pair tests", () => {
it("generates key pairs", () => {
const user = makeKeyPair();
expect(user.privKey).toBeTruthy();
expect(user.pubKey).toBeTruthy();
});
it("generates key pairs from private key", () => {
const privKey = toBuffer("0101010101010101010101010101010101010101010101010101010101010101");
const user = makeKeyPair(privKey);
const userVerify = verifyKeyPair(privKey);
expect(user.pubKey).toEqual(userVerify.pubKey);
expect(user.privKey).toEqual(privKey);
});
it("verifies the authenticity of a private key", () => {
const user = {
pubKey: toBuffer("031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f"),
privKey: toBuffer("0101010101010101010101010101010101010101010101010101010101010101"),
};
const verifiedUser = verifyKeyPair(toBuffer("0101010101010101010101010101010101010101010101010101010101010101"));
expect(JSON.stringify(user)).toEqual(JSON.stringify(verifiedUser));
});
});
});
//# sourceMappingURL=encryption.test.js.map