From fc80106f87b66e935b40a450470262713db2f1d5 Mon Sep 17 00:00:00 2001 From: suyukuoacn Date: Tue, 18 Aug 2020 10:41:55 -0700 Subject: [PATCH] feat(common): KeyConverter class to and from PEM/hex/buffe Signed-off-by: suyukuoacn --- karma.conf.js | 9 +- packages/cactus-common/package-lock.json | 46 +- packages/cactus-common/package.json | 1 + .../src/main/typescript/key-converter.ts | 128 ++++++ .../src/main/typescript/public-api.ts | 1 + .../src/main/typescript/signer-key-pairs.ts | 26 ++ .../src/test/typescript/unit/api-surface.ts | 6 +- .../typescript/unit/js-object-signer.test.ts | 9 + .../typescript/unit/key-converter.test.ts | 395 ++++++++++++++++++ .../unit/objects/get-all-method-names.ts | 3 +- 10 files changed, 611 insertions(+), 13 deletions(-) create mode 100644 packages/cactus-common/src/main/typescript/key-converter.ts create mode 100644 packages/cactus-common/src/main/typescript/signer-key-pairs.ts create mode 100644 packages/cactus-common/src/test/typescript/unit/key-converter.test.ts diff --git a/karma.conf.js b/karma.conf.js index 4124ec7007..1cf05a40f9 100644 --- a/karma.conf.js +++ b/karma.conf.js @@ -12,14 +12,13 @@ module.exports = (config) => { // 'Electron', // "ElectronWithGui", // 'Chrome', - // 'ChromeHeadless', - "ChromeHeadlessDebug", + "ChromeHeadless", + // "ChromeHeadlessDebug", ], files: [ - { - pattern: "packages/cactus-common/src/test/typescript/unit/**/*.ts", - }, + // FIXME: For whatever reason only the first test gets executed not all of them + "./packages/cactus-common/src/test/typescript/unit/**/*", ], plugins: [ diff --git a/packages/cactus-common/package-lock.json b/packages/cactus-common/package-lock.json index ff90cdcf59..ced0620072 100644 --- a/packages/cactus-common/package-lock.json +++ b/packages/cactus-common/package-lock.json @@ -4,6 +4,22 @@ "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/bn.js": { + "version": "4.11.6", + "resolved": "https://registry.npmjs.org/@types/bn.js/-/bn.js-4.11.6.tgz", + "integrity": "sha512-pqr857jrp2kPuO9uRjZ3PwnJTjoQy+fcdxvBTvHm6dkmEL9q+hDD/2j/0ELOBPtPnS8LjCX0gI9nbl8lVkadpg==", + "requires": { + "@types/node": "*" + } + }, + "@types/elliptic": { + "version": "6.4.12", + "resolved": "https://registry.npmjs.org/@types/elliptic/-/elliptic-6.4.12.tgz", + "integrity": "sha512-gP1KsqoouLJGH6IJa28x7PXb3cRqh83X8HCLezd2dF+XcAIMKYv53KV+9Zn6QA561E120uOqZBQ+Jy/cl+fviw==", + "requires": { + "@types/bn.js": "*" + } + }, "@types/joi": { "version": "14.3.4", "resolved": "https://registry.npmjs.org/@types/joi/-/joi-14.3.4.tgz", @@ -19,8 +35,7 @@ "@types/node": { "version": "14.0.24", "resolved": "https://registry.npmjs.org/@types/node/-/node-14.0.24.tgz", - "integrity": "sha512-btt/oNOiDWcSuI721MdL8VQGnjsKjlTMdrKyTcLCKeQp/n4AAMFJ961wMbp+09y8WuGPClDEv07RIItdXKIXAA==", - "dev": true + "integrity": "sha512-btt/oNOiDWcSuI721MdL8VQGnjsKjlTMdrKyTcLCKeQp/n4AAMFJ961wMbp+09y8WuGPClDEv07RIItdXKIXAA==" }, "@types/secp256k1": { "version": "4.0.1", @@ -31,6 +46,17 @@ "@types/node": "*" } }, + "asn1.js": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/asn1.js/-/asn1.js-5.4.1.tgz", + "integrity": "sha512-+I//4cYPccV8LdmBLiX8CYvf9Sp3vQsrqu2QNXRcrbiWvcx/UdlFiqUJJzxRQxgsZmvhXhn4cSKeSmoFjVdupA==", + "requires": { + "bn.js": "^4.0.0", + "inherits": "^2.0.1", + "minimalistic-assert": "^1.0.0", + "safer-buffer": "^2.1.0" + } + }, "base64-js": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.1.tgz", @@ -134,6 +160,17 @@ "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=" }, + "key-encoder": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/key-encoder/-/key-encoder-2.0.3.tgz", + "integrity": "sha512-fgBtpAGIr/Fy5/+ZLQZIPPhsZEcbSlYu/Wu96tNDFNSjSACw5lEIOFeaVdQ/iwrb8oxjlWi6wmWdH76hV6GZjg==", + "requires": { + "@types/elliptic": "^6.4.9", + "asn1.js": "^5.0.1", + "bn.js": "^4.11.8", + "elliptic": "^6.4.1" + } + }, "loglevel": { "version": "1.6.7", "resolved": "https://registry.npmjs.org/loglevel/-/loglevel-1.6.7.tgz", @@ -169,6 +206,11 @@ "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" }, + "safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, "secp256k1": { "version": "4.0.2", "resolved": "https://registry.npmjs.org/secp256k1/-/secp256k1-4.0.2.tgz", diff --git a/packages/cactus-common/package.json b/packages/cactus-common/package.json index 2ef012a107..57b3b21b48 100644 --- a/packages/cactus-common/package.json +++ b/packages/cactus-common/package.json @@ -64,6 +64,7 @@ "dependencies": { "joi": "14.3.1", "json-stable-stringify": "1.0.1", + "key-encoder": "2.0.3", "loglevel": "1.6.7", "loglevel-plugin-prefix": "0.8.4", "secp256k1": "4.0.2", diff --git a/packages/cactus-common/src/main/typescript/key-converter.ts b/packages/cactus-common/src/main/typescript/key-converter.ts new file mode 100644 index 0000000000..0dc816d9dd --- /dev/null +++ b/packages/cactus-common/src/main/typescript/key-converter.ts @@ -0,0 +1,128 @@ +import KeyEncoder from "key-encoder"; + +export enum KeyFormat { + Raw = "raw", + Hex = "hex", + PEM = "pem", +} + +export class KeyConverter { + private keyEncoder: KeyEncoder; + + public readonly supportedKeyFormats: string[]; + + constructor() { + this.supportedKeyFormats = Object.values(KeyFormat); + this.keyEncoder = new KeyEncoder("secp256k1"); + } + + /** + * Convert public key from one format to another format + * @param key + * @param fromFormat + * @param toFormat + */ + public publicKeyAs( + key: string | Uint8Array, + fromFormat: KeyFormat, + toFormat: KeyFormat + ): string | Uint8Array { + this.validateKeyFormatValue(fromFormat); + this.validateKeyFormatValue(toFormat); + + let keyValue: string = ""; + let convertToRaw = false; + + if (key instanceof Uint8Array) { + keyValue = Buffer.from(key).toString("hex"); + } else { + keyValue = key; + } + + if (fromFormat === KeyFormat.Hex) { + fromFormat = KeyFormat.Raw; + } + + if (toFormat === KeyFormat.Raw) { + convertToRaw = true; + } else if (toFormat === KeyFormat.Hex) { + toFormat = KeyFormat.Raw; + } + + let resultKey: string | Uint8Array = this.keyEncoder.encodePublic( + keyValue, + fromFormat, + toFormat + ); + + if (convertToRaw) { + resultKey = Uint8Array.from(Buffer.from(resultKey, "hex")); + } + + return resultKey; + } + + /** + * Convert private key from one format to another format + * @param key + * @param fromFormat + * @param toFormat + */ + public privateKeyAs( + key: string | Buffer, + fromFormat: KeyFormat, + toFormat: KeyFormat + ): string | Buffer { + this.validateKeyFormatValue(fromFormat); + this.validateKeyFormatValue(toFormat); + + let keyValue = key; + let convertToRaw = false; + + if (fromFormat === KeyFormat.Raw) { + if (key instanceof Buffer) { + keyValue = key.toString("hex"); + } + } else if (fromFormat === KeyFormat.Hex) { + fromFormat = KeyFormat.Raw; + } + + if (toFormat === KeyFormat.Raw) { + convertToRaw = true; + } else if (toFormat === KeyFormat.Hex) { + toFormat = KeyFormat.Raw; + } + + let resultKey: string | Buffer = this.keyEncoder.encodePrivate( + keyValue, + fromFormat, + toFormat + ); + + if (convertToRaw) { + resultKey = Buffer.from(resultKey, "hex"); + } + + return resultKey; + } + + /** + * This method will validate if the input key format match to one of the enum value. + * @param keyFormat + */ + private validateKeyFormatValue(keyFormat: KeyFormat): void { + const fnTag = "KeyConverter#publicKeyAs()"; + + if (!this.supportedKeyFormats.some((val) => val === keyFormat)) { + const csv = + this.supportedKeyFormats.join(", ") + + ` => (` + + Object.keys(KeyFormat) + .map((f) => `KeyFormat.${f}`) + .join(", ") + + `)`; + const msg = `${fnTag} Invalid KeyFormat ${keyFormat} Supported: (${csv})`; + throw new Error(msg); + } + } +} diff --git a/packages/cactus-common/src/main/typescript/public-api.ts b/packages/cactus-common/src/main/typescript/public-api.ts index bf15824aff..436bd1439f 100755 --- a/packages/cactus-common/src/main/typescript/public-api.ts +++ b/packages/cactus-common/src/main/typescript/public-api.ts @@ -17,3 +17,4 @@ export { export { ISignerKeyPair } from "./signer-key-pair"; export { Secp256k1Keys } from "./secp256k1-keys"; +export { KeyFormat, KeyConverter } from "./key-converter"; diff --git a/packages/cactus-common/src/main/typescript/signer-key-pairs.ts b/packages/cactus-common/src/main/typescript/signer-key-pairs.ts new file mode 100644 index 0000000000..9d0f67d762 --- /dev/null +++ b/packages/cactus-common/src/main/typescript/signer-key-pairs.ts @@ -0,0 +1,26 @@ +import crypto from "crypto"; +import secp256k1 from "secp256k1"; + +export interface ISignerKeyPairs { + privateKey: any; + publicKey: any; +} + +export class Secp256k1Keys { + /** + * Generate random private and public secp256k1 key in Buffer format + * @return Generated key pair + */ + static generateKeyPairsBuffer(): ISignerKeyPairs { + let privKey: any; + // generate secp256K1 private key + do { + privKey = crypto.randomBytes(32); + } while (!secp256k1.privateKeyVerify(privKey)); + + // generate secp256K1 public key + const pubKey = secp256k1.publicKeyCreate(privKey); + + return { privateKey: privKey, publicKey: pubKey }; + } +} diff --git a/packages/cactus-common/src/test/typescript/unit/api-surface.ts b/packages/cactus-common/src/test/typescript/unit/api-surface.ts index e08a557b7c..ca69c22594 100644 --- a/packages/cactus-common/src/test/typescript/unit/api-surface.ts +++ b/packages/cactus-common/src/test/typescript/unit/api-surface.ts @@ -1,10 +1,8 @@ -// tslint:disable-next-line: no-var-requires -import test from "tape"; +import test, { Test } from "tape"; import { Logger, LoggerProvider } from "../../../main/typescript/public-api"; -test("Library can be loaded", (assert: any) => { - assert.plan(2); +test("Library can be loaded", (assert: Test) => { assert.ok(Logger); assert.ok(LoggerProvider); assert.end(); diff --git a/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts b/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts index d4a9b76734..3360acc154 100644 --- a/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts +++ b/packages/cactus-common/src/test/typescript/unit/js-object-signer.test.ts @@ -52,6 +52,7 @@ test("Simple JSON Test", async (assert: Test) => { const sign2 = jsObjectSigner.sign(payload2); assert.equals(sign1.toString, sign2.toString); + assert.end(); }); test("Simple Nested JSON Test", async (assert: Test) => { @@ -70,6 +71,7 @@ test("Simple Nested JSON Test", async (assert: Test) => { const sign2 = jsObjectSigner.sign(outer2); assert.equals(sign1.toString, sign2.toString); + assert.end(); }); test("Simple Date JSON Test", async (assert: Test) => { @@ -106,6 +108,7 @@ test("Simple Date JSON Test", async (assert: Test) => { const sign2 = jsObjectSigner.sign(outer2); assert.equals(sign1.toString, sign2.toString); + assert.end(); }); test("Circular JSON Test", async (assert: Test) => { @@ -121,6 +124,7 @@ test("Circular JSON Test", async (assert: Test) => { obj.b = obj; assert.throws(() => jsObjectSigner.sign(obj)); + assert.end(); }); test("Very Signature Test", async (assert: Test) => { @@ -136,6 +140,7 @@ test("Very Signature Test", async (assert: Test) => { const verify = jsObjectSigner.verify(payload1, sign1, keyPairs.publicKey); assert.equals(true, verify); + assert.end(); }); test("Test optional sign function", async (assert: Test) => { @@ -156,6 +161,7 @@ test("Test optional sign function", async (assert: Test) => { const sign2 = jsObjectSigner.sign(outer2); assert.equals(sign1.toString, sign2.toString); + assert.end(); }); test("Test optional verify sign function", async (assert: Test) => { @@ -175,6 +181,7 @@ test("Test optional verify sign function", async (assert: Test) => { const verify = jsObjectSigner.verify(outer1, sign1, keyPairs.publicKey); assert.equals(true, verify); + assert.end(); }); test("Test optional hash function", async (assert: Test) => { @@ -195,6 +202,7 @@ test("Test optional hash function", async (assert: Test) => { const sign2 = jsObjectSigner.sign(outer2); assert.equals(sign1.toString, sign2.toString); + assert.end(); }); test("Test missing required constructor field", async (assert: Test) => { @@ -207,4 +215,5 @@ test("Test missing required constructor field", async (assert: Test) => { } catch (e) { assert.equal(e.message, "JsObjectSigner#ctor options.privateKey falsy."); } + assert.end(); }); diff --git a/packages/cactus-common/src/test/typescript/unit/key-converter.test.ts b/packages/cactus-common/src/test/typescript/unit/key-converter.test.ts new file mode 100644 index 0000000000..68cfef972f --- /dev/null +++ b/packages/cactus-common/src/test/typescript/unit/key-converter.test.ts @@ -0,0 +1,395 @@ +import test, { Test } from "tape"; + +import crypto from "crypto"; +import KeyEncoder from "key-encoder"; +import secp256k1 from "secp256k1"; + +import { JsObjectSigner } from "../../../main/typescript/public-api"; + +import { + KeyConverter, + KeyFormat, +} from "../../../main/typescript/key-converter"; + +const keyConverter = new KeyConverter(); + +let privKey: any; +// generate secp256K1 private key +do { + privKey = crypto.randomBytes(32); +} while (!secp256k1.privateKeyVerify(privKey)); + +// generate secp256K1 public key +const pubKey = secp256k1.publicKeyCreate(privKey); + +const keyPairBuffer = { privateKey: privKey, publicKey: pubKey }; + +const keyEncoder: KeyEncoder = new KeyEncoder("secp256k1"); +const hexPublic = Buffer.from(pubKey).toString("hex"); +const pemPublic = keyEncoder.encodePublic( + Buffer.from(pubKey).toString("hex"), + "raw", + "pem" +); +const hexPrivate = privKey.toString("hex"); +const pemPrivate = keyEncoder.encodePrivate( + privKey.toString("hex"), + "raw", + "pem" +); + +test("Test Public Raw key conversion", async (assert: Test) => { + const convertRawPrivate = keyConverter.publicKeyAs( + keyPairBuffer.publicKey, + KeyFormat.Raw, + KeyFormat.Raw + ); + assert.deepEquals( + keyPairBuffer.publicKey, + convertRawPrivate, + "Public Raw => Raw conversion successful" + ); + + const convertHexPublic = keyConverter.publicKeyAs( + keyPairBuffer.publicKey, + KeyFormat.Raw, + KeyFormat.Hex + ); + assert.equals( + hexPublic, + convertHexPublic, + "Public Raw => Hex conversion successful" + ); + + const convertPemPublic = keyConverter.publicKeyAs( + keyPairBuffer.publicKey, + KeyFormat.Raw, + KeyFormat.PEM + ); + assert.equals( + pemPublic, + convertPemPublic, + "Public Raw => PEM conversion successful" + ); + + assert.end(); +}); + +test("Test Public Hex key conversion", async (assert: Test) => { + const convertRawPublic = keyConverter.publicKeyAs( + hexPublic, + KeyFormat.Hex, + KeyFormat.Raw + ); + assert.deepEquals( + keyPairBuffer.publicKey, + convertRawPublic, + "Public Hex => Raw conversion successful" + ); + + const convertHexublic = keyConverter.publicKeyAs( + hexPublic, + KeyFormat.Hex, + KeyFormat.Hex + ); + assert.deepEquals( + hexPublic, + convertHexublic, + "Public Hex => Hex conversion successful" + ); + + const convertPemPublic = keyConverter.publicKeyAs( + hexPublic, + KeyFormat.Hex, + KeyFormat.PEM + ); + assert.deepEquals( + pemPublic, + convertPemPublic, + "Public Hex => PEM conversion successful" + ); + + assert.end(); +}); + +test("Test Public PEM key conversion", async (assert: Test) => { + const convertRawPublic = keyConverter.publicKeyAs( + pemPublic, + KeyFormat.PEM, + KeyFormat.Raw + ); + assert.deepEquals( + keyPairBuffer.publicKey, + convertRawPublic, + "Public PEM => Raw conversion successful" + ); + + const convertHexPublic = keyConverter.publicKeyAs( + pemPublic, + KeyFormat.PEM, + KeyFormat.Hex + ); + assert.deepEquals( + hexPublic, + convertHexPublic, + "Public PEM => Hex conversion successful" + ); + + const convertPemPublic = keyConverter.publicKeyAs( + pemPublic, + KeyFormat.PEM, + KeyFormat.PEM + ); + assert.deepEquals( + pemPublic, + convertPemPublic, + "Public PEM => PEM conversion successful" + ); + + assert.end(); +}); + +test("Test Private Raw key conversion", async (assert: Test) => { + const convertRawPrivate = keyConverter.privateKeyAs( + keyPairBuffer.privateKey, + KeyFormat.Raw, + KeyFormat.Raw + ); + assert.deepEquals( + keyPairBuffer.privateKey, + convertRawPrivate, + "Private Raw => Raw conversion successful" + ); + + const convertHexPrivate = keyConverter.privateKeyAs( + keyPairBuffer.privateKey, + KeyFormat.Raw, + KeyFormat.Hex + ); + assert.equals( + hexPrivate, + convertHexPrivate, + "Private Raw => Hex conversion successful" + ); + + const convertPemPrivate = keyConverter.privateKeyAs( + keyPairBuffer.privateKey, + KeyFormat.Raw, + KeyFormat.PEM + ); + assert.equals( + pemPrivate, + convertPemPrivate, + "Private Raw => PEM conversion successful" + ); + + assert.end(); +}); + +test("Test Private Hex key conversion", async (assert: Test) => { + const convertRawPrivate = keyConverter.privateKeyAs( + hexPrivate, + KeyFormat.Hex, + KeyFormat.Raw + ); + assert.deepEquals( + keyPairBuffer.privateKey, + convertRawPrivate, + "Private Hex => Raw conversion successful" + ); + + const convertHexPrivate = keyConverter.privateKeyAs( + hexPrivate, + KeyFormat.Hex, + KeyFormat.Hex + ); + assert.deepEquals( + hexPrivate, + convertHexPrivate, + "Private Hex => Hex conversion successful" + ); + + const convertPemPrivate = keyConverter.privateKeyAs( + hexPrivate, + KeyFormat.Hex, + KeyFormat.PEM + ); + assert.deepEquals( + pemPrivate, + convertPemPrivate, + "Private Hex => PEM conversion successful" + ); + + assert.end(); +}); + +test("Test Private PEM key conversion", async (assert: Test) => { + const convertRawPrivate = keyConverter.privateKeyAs( + pemPrivate, + KeyFormat.PEM, + KeyFormat.Raw + ); + assert.deepEquals( + keyPairBuffer.privateKey, + convertRawPrivate, + "Private PEM => Raw conversion successful" + ); + + const convertHexPrivate = keyConverter.privateKeyAs( + pemPrivate, + KeyFormat.PEM, + KeyFormat.Hex + ); + assert.deepEquals( + hexPrivate, + convertHexPrivate, + "Private PEM => Hex conversion successful" + ); + + const convertPemPrivate = keyConverter.privateKeyAs( + pemPrivate, + KeyFormat.PEM, + KeyFormat.PEM + ); + assert.deepEquals( + pemPrivate, + convertPemPrivate, + "Private PEM => PEM conversion successful" + ); + + assert.end(); +}); + +test("Test invalide from key format", async (t: Test) => { + t.throws(() => { + keyConverter.publicKeyAs( + keyPairBuffer.publicKey, + "abc" as KeyFormat, + KeyFormat.PEM + ); + }, "KeyConverter#publicKeyAs Invalid KeyFormat"); + + t.throws(() => { + keyConverter.publicKeyAs( + keyPairBuffer.publicKey, + KeyFormat.Raw, + "abc" as any + ); + }, "KeyConverter#publicKeyAs Invalid KeyFormat"); + + t.throws(() => { + keyConverter.privateKeyAs( + keyPairBuffer.privateKey, + "abc" as KeyFormat, + KeyFormat.PEM + ); + }, "KeyConverter#privateKeyAs Invalid KeyFormat"); + + t.throws(() => { + keyConverter.privateKeyAs( + keyPairBuffer.privateKey, + KeyFormat.Raw, + "abc" as any + ); + }, "KeyConverter#privateKeyAs Invalid KeyFormat"); + + t.end(); +}); + +test("correct signatures after conversion whirlwind", async (t: Test) => { + t.comment(`keyPair.privateKey: ${keyPairBuffer.privateKey}`); + + t.comment(`privateKey hex: ${keyPairBuffer.privateKey.toString("hex")}`); + + const privKeyPem = keyConverter.privateKeyAs( + keyPairBuffer.privateKey, + KeyFormat.Raw, + KeyFormat.PEM + ); + t.comment(`privKeyPem: ${privKeyPem}`); + + const privKeyHex = keyConverter.privateKeyAs( + privKeyPem, + KeyFormat.PEM, + KeyFormat.Hex + ); + t.comment(`privKeyHex: ${privKeyHex}`); + + const privKeyRaw = keyConverter.privateKeyAs( + privKeyPem, + KeyFormat.PEM, + KeyFormat.Raw + ); + t.comment(`privKeyBuffer: ${privKeyRaw}`); + t.deepEquals( + keyPairBuffer.privateKey, + privKeyRaw, + "privKey equals privKeyRaw" + ); + + const privKeyPem2 = keyConverter.privateKeyAs( + privKeyHex, + KeyFormat.Hex, + KeyFormat.PEM + ); + t.comment(`privKeyPem2: ${privKeyPem2}`); + + const privKeyPem3 = keyConverter.privateKeyAs( + privKeyRaw, + KeyFormat.Raw, + KeyFormat.PEM + ); + t.comment(`privKeyPem3: ${privKeyPem3}`); + + t.equal(privKeyPem, privKeyPem2, "privKeyPem equals privKeyPem2"); + t.equal(privKeyPem, privKeyPem3, "privKeyPem equals privKeyPem3"); + t.equal(privKeyPem2, privKeyPem3, "privKeyPem2 equals privKeyPem3"); + + const payload = "hello"; + + const signer1 = new JsObjectSigner({ + privateKey: keyPairBuffer.privateKey, + }); + + const signer2 = new JsObjectSigner({ + privateKey: keyConverter.privateKeyAs( + privKeyPem2, + KeyFormat.PEM, + KeyFormat.Raw + ), + }); + const signer3 = new JsObjectSigner({ + privateKey: keyConverter.privateKeyAs( + privKeyPem3, + KeyFormat.PEM, + KeyFormat.Raw + ), + }); + const signer4 = new JsObjectSigner({ + privateKey: keyConverter.privateKeyAs( + privKeyHex, + KeyFormat.Hex, + KeyFormat.Raw + ), + }); + + const signature1 = signer1.sign(payload); + t.comment(`Signature 1: ${signature1}`); + + const signature2 = signer2.sign(payload); + t.comment(`Signature 2: ${signature2}`); + + const signature3 = signer3.sign(payload); + t.comment(`Signature 3: ${signature3}`); + + const signature4 = signer4.sign(payload); + t.comment(`Signature 4: ${signature4}`); + + t.deepEquals(signature1, signature2, "signature1 deep equals signature2"); + + t.deepEquals(signature2, signature3, "signature2 deep equals signature3"); + + t.deepEquals(signature1, signature4, "signature1 deep equals signature4"); + + t.end(); +}); diff --git a/packages/cactus-common/src/test/typescript/unit/objects/get-all-method-names.ts b/packages/cactus-common/src/test/typescript/unit/objects/get-all-method-names.ts index 91ab737a50..70d7508471 100644 --- a/packages/cactus-common/src/test/typescript/unit/objects/get-all-method-names.ts +++ b/packages/cactus-common/src/test/typescript/unit/objects/get-all-method-names.ts @@ -1,10 +1,9 @@ -// tslint:disable-next-line: no-var-requires import test, { Test } from "tape"; import { Objects } from "../../../../main/typescript/public-api"; import { A } from "../../fixtures/dummy-classes"; -test("handles inheritance correctly", (assert: Test) => { +test("handles inheritance correctly", async (assert: Test) => { const a = new A(); const methodNames = Objects.getAllMethodNames(a); assert.ok(Array.isArray(methodNames), "expect an arran of strings returned");