Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make serialized objects Uint8Array instead of Buffer #134

Merged
merged 4 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class SecretKey implements Serializable {
/**
* Serialize a secret key into a Buffer.
*/
serialize(): Buffer;
serialize(): Uint8Array;
toHex(): string;
toPublicKey(): PublicKey;
sign(msg: BlstBuffer): Signature;
Expand All @@ -108,7 +108,7 @@ export class PublicKey implements Serializable {
* Convert a serialized public key into a PublicKey object.
*/
static deserialize(pkBytes: BlstBuffer, coordType?: CoordType): PublicKey;
serialize(compress?: boolean): Buffer;
serialize(compress?: boolean): Uint8Array;
toHex(compress?: boolean): string;
keyValidate(): void;
isInfinity(): boolean;
Expand All @@ -121,7 +121,7 @@ export class Signature implements Serializable {
* Convert a serialized signature into a Signature object.
*/
static deserialize(sigBytes: BlstBuffer, coordType?: CoordType): Signature;
serialize(compress?: boolean): Buffer;
serialize(compress?: boolean): Uint8Array;
toHex(compress?: boolean): string;
sigValidate(): void;
isInfinity(): boolean;
Expand Down
6 changes: 3 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ const bindingsPath = getBindingsPath(rootDir);

function prepareBindings(bindings) {
bindings.SecretKey.prototype.toHex = function toHex() {
return `0x${this.serialize().toString("hex")}`;
return `0x${Buffer.from(this.serialize().buffer).toString("hex")}`;
matthewkeil marked this conversation as resolved.
Show resolved Hide resolved
};

bindings.PublicKey.prototype.toHex = function toHex(compress) {
return `0x${this.serialize(compress).toString("hex")}`;
return `0x${Buffer.from(this.serialize(compress).buffer).toString("hex")}`;
};

bindings.Signature.prototype.toHex = function toHex(compress) {
return `0x${this.serialize(compress).toString("hex")}`;
return `0x${Buffer.from(this.serialize(compress).buffer).toString("hex")}`;
};

return {
Expand Down
2 changes: 1 addition & 1 deletion src/addon.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace blst_ts {
if (!info[0].IsUndefined()) { \
compressed = info[0].ToBoolean().Value(); \
} \
Napi::Buffer<uint8_t> serialized = Napi::Buffer<uint8_t>::New( \
Napi::Uint8Array serialized = Napi::Uint8Array::New( \
env, \
compressed ? snake_case_name##_length_compressed \
: snake_case_name##_length_uncompressed); \
Expand Down
4 changes: 2 additions & 2 deletions src/secret_key.cc
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ Napi::Value SecretKey::Serialize(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
Napi::EscapableHandleScope scope(env);

Napi::Buffer<uint8_t> serialized =
Napi::Buffer<uint8_t>::New(env, secret_key_length);
Napi::Uint8Array serialized =
Napi::Uint8Array::New(env, secret_key_length);
key->to_bendian(serialized.Data());

return scope.Escape(serialized);
Expand Down
11 changes: 10 additions & 1 deletion test/unit/PublicKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {expect} from "chai";
import {BLST_CONSTANTS, CoordType, PublicKey, SecretKey} from "../../lib";
import {expectEqualHex, expectNotEqualHex, sullyUint8Array} from "../utils";
import {validPublicKey, SECRET_KEY_BYTES, invalidInputs, badPublicKey, G1_POINT_AT_INFINITY} from "../__fixtures__";
import {validPublicKey, SECRET_KEY_BYTES, invalidInputs, G1_POINT_AT_INFINITY} from "../__fixtures__";

describe("PublicKey", () => {
it("should exist", () => {
Expand Down Expand Up @@ -71,6 +71,9 @@ describe("PublicKey", () => {
describe("serialize", () => {
const sk = SecretKey.deserialize(SECRET_KEY_BYTES);
const pk = sk.toPublicKey();
it("should serialize the key to Uint8Array", () => {
expect(pk.serialize()).to.be.instanceof(Uint8Array);
});
it("should default to compressed serialization", () => {
expectEqualHex(pk.serialize(), pk.serialize(true));
expectNotEqualHex(pk.serialize(), pk.serialize(false));
Expand All @@ -88,6 +91,12 @@ describe("PublicKey", () => {
expectEqualHex(jacobian.serialize(false), affine.serialize(false));
});
});
describe("toHex", () => {
it("should toHex string correctly", () => {
const key = PublicKey.deserialize(validPublicKey.compressed);
expectEqualHex(key.toHex(true), validPublicKey.compressed);
});
});
describe("keyValidate()", () => {
it("should not throw on valid public key", () => {
const pk = PublicKey.deserialize(validPublicKey.uncompressed);
Expand Down
8 changes: 7 additions & 1 deletion test/unit/SecretKey.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("SecretKey", () => {
});
describe("serialize", () => {
it("should serialize the key to Uint8Array", () => {
expect(key.serialize()).to.be.instanceof(Buffer);
expect(key.serialize()).to.be.instanceof(Uint8Array);
});
it("should be the correct length", () => {
expect(key.serialize().length).to.equal(BLST_CONSTANTS.SECRET_KEY_LENGTH);
Expand All @@ -80,6 +80,12 @@ describe("SecretKey", () => {
expectEqualHex(SecretKey.deserialize(serialized).serialize(), serialized);
});
});
describe("toHex", () => {
it("should toHex string correctly", () => {
const key = SecretKey.deserialize(SECRET_KEY_BYTES);
expectEqualHex(key.toHex(), SECRET_KEY_BYTES);
});
});
describe("toPublicKey", () => {
it("should create a valid PublicKey", () => {
const pk = key.toPublicKey();
Expand Down
9 changes: 9 additions & 0 deletions test/unit/Signature.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ describe("Signature", () => {
describe("methods", () => {
describe("serialize", () => {
const sig = SecretKey.fromKeygen(KEY_MATERIAL).sign(Buffer.from("some fancy message"));
it("should serialize the signature to Uint8Array", () => {
expect(sig.serialize()).to.be.instanceof(Uint8Array);
});
it("should default to compressed serialization", () => {
expectEqualHex(sig.serialize(), sig.serialize(true));
expectNotEqualHex(sig.serialize(), sig.serialize(false));
Expand All @@ -69,6 +72,12 @@ describe("Signature", () => {
expectEqualHex(jacobian.serialize(false), affine.serialize(false));
});
});
describe("toHex", () => {
it("should toHex string correctly", () => {
const key = Signature.deserialize(validSignature.compressed);
expectEqualHex(key.toHex(true), validSignature.compressed);
});
});
describe("sigValidate()", () => {
it("should return undefined for valid", () => {
const sig = Signature.deserialize(validSignature.compressed);
Expand Down