Skip to content

Commit

Permalink
style(1338): removed all "unexpected any" types from js-object-signer…
Browse files Browse the repository at this point in the history
… and test

Signed-off-by: jsladerman <jsladerman@gmail.com>

style(1338): added back updated version of removed test case

Signed-off-by: jsladerman <jsladerman@gmail.com>

style(1338): moved key type check to constructor
allows for private keys to be passed as strings
type check is needed to satisfy compiler

Signed-off-by: jsladerman <jsladerman@gmail.com>
  • Loading branch information
jsladerman authored and petermetz committed Sep 18, 2021
1 parent 32a06a3 commit 63e639e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
45 changes: 29 additions & 16 deletions packages/cactus-common/src/main/typescript/js-object-signer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,27 @@ import secp256k1 from "secp256k1";
import sha3 from "sha3";
import stringify from "json-stable-stringify";

export type SignatureFunction = (msg: any, pkey: any) => any;
export type SignatureFunction = (msg: unknown, pkey: Uint8Array) => Uint8Array;
export type VerifySignatureFunction = (
msg: any,
signature: any,
msg: unknown,
signature: Uint8Array,
pubKey: Uint8Array,
) => boolean;
export type HashFunction = (data: any) => string;
export type HashFunction = (data: unknown) => string;

export interface IJsObjectSignerOptions {
privateKey: any;
privateKey: Uint8Array | string;
signatureFunc?: SignatureFunction;
verifySignatureFunc?: VerifySignatureFunction;
hashFunc?: HashFunction;
logLevel?: LogLevelDesc;
}

export class JsObjectSigner {
private privateKey: any;
private signatureFunc: any;
private verifySignatureFunc: any;
private hashFunc: any;
private privateKey: Uint8Array;
private signatureFunc?: SignatureFunction;
private verifySignatureFunc?: VerifySignatureFunction;
private hashFunc?: HashFunction;
private readonly logger: Logger;

constructor(public readonly options: IJsObjectSignerOptions) {
Expand All @@ -37,7 +37,14 @@ export class JsObjectSigner {
throw new Error(`JsObjectSigner#ctor options.privateKey falsy.`);
}

this.privateKey = options.privateKey;
// allows for private keys to passed as strings, but will automatically convert them to byte arrays for consistency
// type check is needed to satisfy compiler
if (typeof options.privateKey === "string") {
this.privateKey = Buffer.from(options.privateKey, `hex`);
} else {
this.privateKey = options.privateKey;
}

this.signatureFunc = options.signatureFunc;
this.verifySignatureFunc = options.verifySignatureFunc;
this.hashFunc = options.hashFunc;
Expand All @@ -53,21 +60,23 @@ export class JsObjectSigner {
* @param msg
* @returns Generated signature
*/
public sign(msg: any): any {
public sign(msg: unknown): Uint8Array {
this.logger.debug("Message to sign: " + stringify(msg));

if (this.signatureFunc) {
return this.signatureFunc(msg, this.privateKey);
} else {
let hashMsg: any;
let hashMsg: string;
if (this.hashFunc) {
hashMsg = this.hashFunc(msg);
} else {
hashMsg = this.dataHash(msg);
}

const pkey = Buffer.from(this.privateKey, `hex`);
const signObj = secp256k1.ecdsaSign(Buffer.from(hashMsg, `hex`), pkey);
const signObj = secp256k1.ecdsaSign(
Buffer.from(hashMsg, `hex`),
this.privateKey,
);
return signObj.signature;
}
}
Expand All @@ -79,7 +88,11 @@ export class JsObjectSigner {
* @param signature
* @returns {boolean}
*/
public verify(msg: any, signature: Uint8Array, pubKey: any): boolean {
public verify(
msg: unknown,
signature: Uint8Array,
pubKey: Uint8Array,
): boolean {
if (this.verifySignatureFunc) {
return this.verifySignatureFunc(msg, signature, pubKey);
}
Expand All @@ -95,7 +108,7 @@ export class JsObjectSigner {
* @param data
* @returns {string}
*/
private dataHash(data: any): string {
private dataHash(data: unknown): string {
const hashObj = new sha3.SHA3Hash(256);
hashObj.update(stringify(data));
const hashMsg = hashObj.digest(`hex`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import stringify from "json-stable-stringify";

const keyPairs = Secp256k1Keys.generateKeyPairsBuffer();

const hashFunction = (data: any): string => {
const hashFunction = (data: unknown): string => {
return crypto.createHash("sha256").update(stringify(data)).digest("hex");
};

const signFunction = (msg: any, pkey: any): any => {
const signFunction = (msg: unknown, pkey: Uint8Array): Uint8Array => {
const signObj = secp256k1.ecdsaSign(
Buffer.from(hashFunction(msg), `hex`),
Buffer.from(pkey, `hex`),
pkey,
);
return signObj.signature;
};

const verifySignFunction = (
msg: any,
signature: any,
msg: unknown,
signature: Uint8Array,
pubKey: Uint8Array,
): boolean => {
return secp256k1.ecdsaVerify(
Expand Down Expand Up @@ -117,6 +117,8 @@ test("Circular JSON Test", async (assert: Test) => {
};
const jsObjectSigner = new JsObjectSigner(jsObjectSignerOptions);

// "any" type actually makes sense here in order to create a circular json
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const obj: any = { a: "foo" };
obj.b = obj;

Expand Down Expand Up @@ -204,12 +206,15 @@ test("Test optional hash function", async (assert: Test) => {

test("Test missing required constructor field", async (assert: Test) => {
try {
const pkey: unknown = undefined;
const jsObjectSignerOptions: IJsObjectSignerOptions = {
privateKey: undefined,
privateKey: pkey as Uint8Array,
};
new JsObjectSigner(jsObjectSignerOptions);
} catch (e) {
assert.equal(e.message, "JsObjectSigner#ctor options.privateKey falsy.");
if (e instanceof Error) {
assert.equal(e.message, "JsObjectSigner#ctor options.privateKey falsy.");
}
}
assert.end();
});

0 comments on commit 63e639e

Please sign in to comment.