diff --git a/libs/keyStore/lib/KeyStoreInMemory.ts b/libs/keyStore/lib/KeyStoreInMemory.ts index de9abd9..6a6afba 100644 --- a/libs/keyStore/lib/KeyStoreInMemory.ts +++ b/libs/keyStore/lib/KeyStoreInMemory.ts @@ -24,16 +24,15 @@ export default class KeyStoreInMemory implements IKeyStore { * @param [options] Options for retrieving. */ get(keyReference: KeyReference, options?: KeyStoreOptions): Promise { - return new Promise((resolve, reject) => { + if (this.store.has(keyReference.keyReference)) { const key = (this.store.get(keyReference.keyReference)); if (key.kty === KeyType.Oct) { if (options && options.publicKeyOnly) { const error = 'A secret does not has a public key'; - reject(error); - throw new Error(error); + return Promise.reject(new Error(error)); } - return resolve(key); + return Promise.resolve(key); } if (options && options.publicKeyOnly) { @@ -41,20 +40,19 @@ export default class KeyStoreInMemory implements IKeyStore { case 'ec': case 'okp': case 'rsa': - return resolve(this.publicKeysOnly(key)); + return Promise.resolve(this.publicKeysOnly(key)); default: const error = `A secret does not has a public key`; - return reject(error); + return Promise.reject(new Error(error)); } } else { - resolve(key); + return Promise.resolve(key); } } else { const error = `${keyReference.keyReference} not found`; - return reject(error); + return Promise.reject(new Error(error)); } - }); - } + } private publicKeysOnly(container: IKeyContainer) { const publicKeyContainer = clone(container); @@ -88,9 +86,7 @@ export default class KeyStoreInMemory implements IKeyStore { } } } - return new Promise((resolve) => { - resolve(dictionary); - }); + return Promise.resolve(dictionary); } /** @@ -115,8 +111,6 @@ export default class KeyStoreInMemory implements IKeyStore { this.store.set(keyIdentifier.keyReference, container); } - return new Promise((resolve) => { - resolve(); - }); + return Promise.resolve(); } } diff --git a/libs/keyStore/package.json b/libs/keyStore/package.json index 47d3dd7..7edb879 100644 --- a/libs/keyStore/package.json +++ b/libs/keyStore/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-keystore", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "description": "Package for managing keys in a key store.", "repository": { "type": "git", @@ -28,18 +28,18 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "prettier": "^1.18.2", "ts-node": "8.5.4", "tslint": "^5.20.0", "typescript": "3.9.2" }, "dependencies": { - "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.6", + "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.7", "@types/node": "12.12.16", "base64url": "^3.0.1", "clone": "^2.1.2", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/keyStore/tests/KeyStoreInMemory.spec.ts b/libs/keyStore/tests/KeyStoreInMemory.spec.ts index 9fe61a6..11d2282 100644 --- a/libs/keyStore/tests/KeyStoreInMemory.spec.ts +++ b/libs/keyStore/tests/KeyStoreInMemory.spec.ts @@ -108,7 +108,7 @@ describe('KeyStoreInMemory', () => { const error = await keyStore.get(new KeyReference('key'), new KeyStoreOptions({ publicKeyOnly: true })) .catch((err) => { throwCaught = true; - expect(err).toBe('A secret does not has a public key'); + expect(err.message).toBe('A secret does not has a public key'); }); expect(error).toBeUndefined(); expect(throwCaught).toBe(true); @@ -128,7 +128,7 @@ describe('KeyStoreInMemory', () => { const error = await keyStore.get(new KeyReference('key'), new KeyStoreOptions({ publicKeyOnly: true })) .catch((err) => { throwCaught = true; - expect(err).toBe('A secret does not has a public key'); + expect(err.message).toBe('A secret does not has a public key'); }); expect(error).toBeUndefined(); expect(throwCaught).toBe(true); @@ -149,7 +149,7 @@ describe('KeyStoreInMemory', () => { const signature = await keyStore.get(new KeyReference('key1')) .catch((err) => { throwCaught = true; - expect(err).toBe('key1 not found'); + expect(err.message).toBe('key1 not found'); }); expect(signature).toBeUndefined(); expect(throwCaught).toBe(true); diff --git a/libs/keys/package.json b/libs/keys/package.json index 54e6ec0..2b34dca 100644 --- a/libs/keys/package.json +++ b/libs/keys/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-keys", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "description": "Package for managing keys in the DID space.", "repository": { "type": "git", @@ -28,7 +28,7 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "preload-js": "^0.6.3", "prettier": "^1.18.2", "ts-node": "8.5.4", @@ -39,8 +39,8 @@ "@types/node": "12.12.16", "base64url": "^3.0.1", "clone": "2.1.2", - "node-jose": "1.0.0", - "webcrypto-core": "1.1.1" + "node-jose": "2.0.0", + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/plugin-cryptofactory-suites/package.json b/libs/plugin-cryptofactory-suites/package.json index bcd797c..6133305 100644 --- a/libs/plugin-cryptofactory-suites/package.json +++ b/libs/plugin-cryptofactory-suites/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "description": "Package crypto factory suites.", "repository": { "type": "git", @@ -29,20 +29,19 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "prettier": "^1.18.2", "ts-node": "8.5.4", "tslint": "^5.20.0", "typescript": "3.9.2" }, "dependencies": { - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic": "1.1.11-preview.6", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic": "1.1.11-preview.7", "base64url": "^3.0.1", "clone": "2.1.2", - "node-jose": "1.0.0", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/plugin-elliptic/package.json b/libs/plugin-elliptic/package.json index 1d194fb..3b92c62 100644 --- a/libs/plugin-elliptic/package.json +++ b/libs/plugin-elliptic/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "repository": { "type": "git", "url": "https://github.com/microsoft/VerifiableCredentials-Crypto-SDK-Typescript.git" @@ -29,8 +29,8 @@ "elliptic": "6.5.3", "minimalistic-crypto-utils": "1.0.1", "sha.js": "^2.4.11", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", - "webcrypto-core": "1.1.1" + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", + "webcrypto-core": "1.1.8" }, "devDependencies": { "@types/jasmine": "2.8.16", @@ -42,7 +42,7 @@ "jasmine-ts": "0.3.0", "ts-loader": "2.3.7", "node-fetch": "2.4.1", - "nyc": "14.1.1", + "nyc": "^15.1.0", "source-map-support": "0.5.12", "ts-node": "8.5.4", "tslint": "^5.20.0", diff --git a/libs/plugin-elliptic/src/SubtleCryptoElliptic.ts b/libs/plugin-elliptic/src/SubtleCryptoElliptic.ts index c34bf70..1686d93 100644 --- a/libs/plugin-elliptic/src/SubtleCryptoElliptic.ts +++ b/libs/plugin-elliptic/src/SubtleCryptoElliptic.ts @@ -22,7 +22,7 @@ export default class SubtleCryptoElliptic extends SubtleCrypto implements ISubtl this.providers.set(new EllipticEdDsaProvider(crypto)); } - checkRequiredArguments(args: IArguments, size: number, methodName: string) { + checkRequiredArguments(args: any[], size: number, methodName: string) { if (methodName !== 'generateKey' && args.length !== size) { throw new TypeError(`Failed to execute '${methodName}' on 'SubtleCrypto': ${size} arguments required, but only ${args.length} present`); } diff --git a/libs/plugin-factory/package.json b/libs/plugin-factory/package.json index 282c44f..9eca1b5 100644 --- a/libs/plugin-factory/package.json +++ b/libs/plugin-factory/package.json @@ -1,7 +1,7 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-plugin-factory", "description": "Factory Package for crypto plugins.", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "repository": { "type": "git", "url": "https://github.com/microsoft/VerifiableCredentials-Crypto-SDK-Typescript.git" @@ -29,7 +29,7 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "prettier": "^1.18.2", "ts-node": "8.5.4", "tslint": "^5.20.0", @@ -37,12 +37,12 @@ }, "dependencies": { "@azure/identity": "1.0.0", - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-keyvault": "1.1.11-preview.6", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-keyvault": "1.1.11-preview.7", "@types/node": "12.12.16", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/plugin-keyvault/package.json b/libs/plugin-keyvault/package.json index 8c54757..c9929e0 100644 --- a/libs/plugin-keyvault/package.json +++ b/libs/plugin-keyvault/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-plugin-keyvault", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "repository": { "type": "git", "url": "https://github.com/microsoft/VerifiableCredentials-Crypto-SDK-Typescript.git" @@ -35,7 +35,7 @@ "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", "ms-rest-azure": "2.6.0", - "nyc": "14.1.1", + "nyc": "15.1.0", "prettier": "^1.18.2", "source-map-support": "^0.5.9", "ts-node": "8.5.4", @@ -47,13 +47,13 @@ "@azure/identity": "1.0.0", "@azure/keyvault-keys": "4.0.2", "@azure/keyvault-secrets": "4.0.2", - "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic": "1.1.11-preview.6", + "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic": "1.1.11-preview.7", "base64url": "3.0.1", "clone": "2.1.2", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/plugin-keyvault/src/plugin/SubtleCryptoKeyVault.ts b/libs/plugin-keyvault/src/plugin/SubtleCryptoKeyVault.ts index 7e04c37..4e5e34b 100644 --- a/libs/plugin-keyvault/src/plugin/SubtleCryptoKeyVault.ts +++ b/libs/plugin-keyvault/src/plugin/SubtleCryptoKeyVault.ts @@ -28,7 +28,7 @@ export default class SubtleCryptoKeyVault extends SubtleCrypto implements ISubtl * @param options Options used to define optional name */ public async generateKey(algorithm: Algorithm, extractable: boolean, keyUsages: KeyUsage[], options?: IKeyGenerationOptions) { - this.checkRequiredArguments(arguments, options ? 4 : 3, "generateKey"); + //this.checkRequiredArguments(arguments, options ? 4 : 3, "generateKey"); const preparedAlgorithm = this.prepareAlgorithm(algorithm); const provider: any = this.getProvider(preparedAlgorithm.name); const result = await provider.generateKey({ ...preparedAlgorithm, name: provider.name }, extractable, keyUsages, options); diff --git a/libs/plugin-keyvault/tests/KeyStoreKeyVault.spec.ts b/libs/plugin-keyvault/tests/KeyStoreKeyVault.spec.ts index d16b77d..7221522 100644 --- a/libs/plugin-keyvault/tests/KeyStoreKeyVault.spec.ts +++ b/libs/plugin-keyvault/tests/KeyStoreKeyVault.spec.ts @@ -123,7 +123,7 @@ describe('KeyStoreKeyVault', () => { expect(throwed).toBeTruthy(); } catch (err) { throwed = true; - expect(err).toEqual(`${name} not found`) + expect(err.message).toEqual(`${name} not found`) } finally { await (keyStore.getKeyStoreClient('secret')).beginDeleteSecret(name); diff --git a/libs/plugin/package.json b/libs/plugin/package.json index dec6d26..30655e0 100644 --- a/libs/plugin/package.json +++ b/libs/plugin/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-plugin", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "description": "Package for plugeable crypto based on subtle crypto.", "repository": { "type": "git", @@ -35,17 +35,16 @@ "typescript": "3.9.2" }, "dependencies": { - "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "@peculiar/webcrypto": "1.1.1", + "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "@peculiar/webcrypto": "1.1.3", "@types/node": "12.12.16", "base64url": "^3.0.1", "big-integer": "1.6.48", "bn.js": "5.1.2", "clone": "2.1.2", "elliptic": "6.5.3", - "node-jose": "1.0.0", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/protocol-jose/lib/jwt/JwtToken.ts b/libs/protocol-jose/lib/jwt/JwtToken.ts index 2d2d1d5..07b9d4c 100644 --- a/libs/protocol-jose/lib/jwt/JwtToken.ts +++ b/libs/protocol-jose/lib/jwt/JwtToken.ts @@ -41,12 +41,12 @@ export default class JwtToken { ): Promise { if (typeof payload !== 'object') { - return Promise.reject(`JWT payload needs to be an object with a set of claims`); + return Promise.reject(new Error(`JWT payload needs to be an object with a set of claims`)); } options = options || this.options; if (!options) { - return Promise.reject(`JWT need to be defined`); + return Promise.reject(new Error(`JWT need to be defined`)); } // Set the protected header diff --git a/libs/protocol-jose/package.json b/libs/protocol-jose/package.json index 6536bc5..93d2907 100644 --- a/libs/protocol-jose/package.json +++ b/libs/protocol-jose/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-protocol-jose", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "repository": { "type": "git", "url": "https://github.com/microsoft/VerifiableCredentials-Crypto-SDK-Typescript.git" @@ -32,21 +32,21 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "prettier": "^1.18.2", "ts-node": "8.5.4", "tslint": "^5.20.0", "typescript": "3.9.2" }, "dependencies": { - "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-protocols-common": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites": "1.1.11-preview.6", + "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-protocols-common": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites": "1.1.11-preview.7", "base64url": "^3.0.1", "typescript-map": "0.0.7", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/protocols-common/package.json b/libs/protocols-common/package.json index f10eb05..e98748a 100644 --- a/libs/protocols-common/package.json +++ b/libs/protocols-common/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript-protocols-common", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "repository": { "type": "git", "url": "https://github.com/microsoft/VerifiableCredentials-Crypto-SDK-Typescript.git" @@ -32,19 +32,19 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "prettier": "^1.18.2", "ts-node": "8.5.4", "tslint": "^5.20.0", "typescript": "3.9.2" }, "dependencies": { - "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", + "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", "base64url": "3.0.1", "typescript-map": "0.0.7", - "webcrypto-core": "1.1.1" + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/sdk/lib/Crypto.ts b/libs/sdk/lib/Crypto.ts index ebe628e..7d22c16 100644 --- a/libs/sdk/lib/Crypto.ts +++ b/libs/sdk/lib/Crypto.ts @@ -21,6 +21,7 @@ export default class Crypto { .useJwtProtocol() .build(), JSONLDProofs: new JoseBuilder(this) + .useJsonLdProofsProtocol('JcsEd25519Signature2020') .build() }; @@ -35,6 +36,17 @@ export default class Crypto { return this._builder; } + /** + * Get the protocol used for signing + */ + public signingProtocol(type: string): IPayloadProtectionSigning { + return this.signingProtocols[type]; + } + + public get signingProtocols(): { [protocol: string]: IPayloadProtectionSigning } { + return this._signingProtocols; + } + public async generateKey(keyUse: KeyUse, type: string = 'signing'): Promise { let keyReference: KeyReference; let jwaAlgorithm: string; @@ -48,7 +60,7 @@ export default class Crypto { jwaAlgorithm = this.builder.recoveryAlgorithm; break; default: - return Promise.reject(`Key generation type '${type}' not supported`); + return Promise.reject(new Error(`Key generation type '${type}' not supported`)); } if (keyUse === KeyUse.Signature) { @@ -85,19 +97,8 @@ export default class Crypto { return this; } else { - return Promise.reject('not implemented'); + return Promise.reject(new Error('not implemented')); } } - - /** - * Get the protocol used for signing - */ - public signingProtocol(type: string): IPayloadProtectionSigning { - return this.signingProtocols[type]; - } - - public get signingProtocols(): { [protocol: string]: IPayloadProtectionSigning } { - return this._signingProtocols; - } } diff --git a/libs/sdk/lib/Jose.ts b/libs/sdk/lib/Jose.ts index 16efe95..3ebed65 100644 --- a/libs/sdk/lib/Jose.ts +++ b/libs/sdk/lib/Jose.ts @@ -61,14 +61,14 @@ export default class Jose implements IPayloadProtectionSigning { // Support json ld proofs console.log('Support JSON LD proofs'); if (typeof payload === 'string' || payload instanceof Buffer) { - return Promise.reject(`Input to sign JSON LD must be an object`); + return Promise.reject(new Error(`Input to sign JSON LD must be an object`)); } let suite: IJsonLinkedDataProofSuite; try { suite = this.builder.getLinkedDataProofSuite(this); } catch (exception) { - return Promise.reject(exception.message); + return Promise.reject(new Error(exception.message)); } this._jsonLdProofObject = await suite.sign(payload); @@ -89,7 +89,7 @@ export default class Jose implements IPayloadProtectionSigning { if (this.isJwtProtocol()) { if (typeof payload === 'string' || payload instanceof Buffer) { - return Promise.reject(`Input to sign JWT must be an object`); + return Promise.reject(new Error(`Input to sign JWT must be an object`)); } // Add standardized properties @@ -140,7 +140,7 @@ export default class Jose implements IPayloadProtectionSigning { const jwsOptions: IJwsSigningOptions = Jose.optionsFromBuilder(this.builder); if (!this._token) { - return Promise.reject('Import a token by deserialize'); + return Promise.reject(new Error('Import a token by deserialize')); } const result = await this._token.verify(validationKeys!, jwsOptions); @@ -156,12 +156,12 @@ export default class Jose implements IPayloadProtectionSigning { return this._jsonLdProofSuite.serialize(this._jsonLdProofObject); } - return Promise.reject(`No token to serialize`); + return Promise.reject(new Error(`No token to serialize`)); } const protocolFormat: ProtectionFormat = Jose.getProtectionFormat(this.builder.serializationFormat); if (!this._token) { - return Promise.reject(`No token to serialize`); + return Promise.reject(new Error(`No token to serialize`)); } switch (protocolFormat) { @@ -170,7 +170,7 @@ export default class Jose implements IPayloadProtectionSigning { case ProtectionFormat.JwsGeneralJson: return Promise.resolve(this._token.serialize(protocolFormat)); `` default: - return Promise.reject(`The serialization format '${this.builder.serializationFormat}' is not supported`); + return Promise.reject(new Error(`The serialization format '${this.builder.serializationFormat}' is not supported`)); } } @@ -220,7 +220,7 @@ export default class Jose implements IPayloadProtectionSigning { this._signaturePayload = this._token.payload; return Promise.resolve(this); default: - return Promise.reject(`Serialization format '${this.builder.serializationFormat}' is not supported`); + return Promise.reject(new Error(`Serialization format '${this.builder.serializationFormat}' is not supported`)); } } diff --git a/libs/sdk/lib/JoseBuilder.ts b/libs/sdk/lib/JoseBuilder.ts index f7cbe12..eb0182f 100644 --- a/libs/sdk/lib/JoseBuilder.ts +++ b/libs/sdk/lib/JoseBuilder.ts @@ -1,3 +1,4 @@ +import uuid from 'uuid'; import { IPayloadProtection, IPayloadProtectionSigning } from 'verifiablecredentials-crypto-sdk-typescript-protocols-common'; /*--------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All rights reserved. diff --git a/libs/sdk/lib/JsonLinkedDataProofsBase.ts b/libs/sdk/lib/JsonLinkedDataProofsBase.ts index 4ad6b4a..de17552 100644 --- a/libs/sdk/lib/JsonLinkedDataProofsBase.ts +++ b/libs/sdk/lib/JsonLinkedDataProofsBase.ts @@ -38,7 +38,7 @@ export default class JsonLinkedDataProofsBase implements IJsonLinkedDataProofSui * @param payload to embed signature */ public async sign(_payload: object): Promise { - return Promise.reject('sign not implemented'); + return Promise.reject(new Error('sign not implemented')); } /** @@ -48,7 +48,7 @@ export default class JsonLinkedDataProofsBase implements IJsonLinkedDataProofSui * @returns True if signature validated. */ public async verify(_validationKeys?: PublicKey[]): Promise { - return Promise.reject('verify not implemented'); + return Promise.reject(new Error('verify not implemented')); } /** @@ -56,14 +56,12 @@ export default class JsonLinkedDataProofsBase implements IJsonLinkedDataProofSui * @param signedPayload The payload to serialize */ public serialize(signedPayload?: any): Promise { - return new Promise((resolve, reject) => { this._credential = signedPayload ? signedPayload : this._credential; if (!this._credential) { - return reject('No credential to serialize'); + return Promise.reject(new Error('No credential to serialize')); } - resolve(JSON.stringify(this._credential)); - }); + return Promise.resolve(JSON.stringify(this._credential)); } /** @@ -75,7 +73,7 @@ export default class JsonLinkedDataProofsBase implements IJsonLinkedDataProofSui this._credential = JSON.parse(credential); return Promise.resolve(this._credential); } catch (exeption) { - return Promise.reject('Could not parse JSON LD token'); + return Promise.reject(new Error('Could not parse JSON LD token')); } } } \ No newline at end of file diff --git a/libs/sdk/lib/suites/SuiteJcsEd25519Signature2020.ts b/libs/sdk/lib/suites/SuiteJcsEd25519Signature2020.ts index 6ceb4b3..131269c 100644 --- a/libs/sdk/lib/suites/SuiteJcsEd25519Signature2020.ts +++ b/libs/sdk/lib/suites/SuiteJcsEd25519Signature2020.ts @@ -43,11 +43,11 @@ export default class SuiteJcsEd25519Signature2020 extends JsonLinkedDataProofsBa public async sign(payload: any): Promise { if (!payload) { - return Promise.reject('JSON LD proof input is undefined'); + return Promise.reject(new Error('JSON LD proof input is undefined')); } if (typeof payload !== 'object') { - return Promise.reject('JSON LD proof input should be an object'); + return Promise.reject(new Error('JSON LD proof input should be an object')); } const crypto = this._signer.builder.crypto; @@ -75,7 +75,7 @@ export default class SuiteJcsEd25519Signature2020 extends JsonLinkedDataProofsBa public async verify(validationKeys?: PublicKey[], signedPayload?: any): Promise { this._credential = signedPayload ? signedPayload : this._credential; if (!this._credential) { - return Promise.reject('Import a credential by deserialize'); + return Promise.reject(new Error('Import a credential by deserialize')); } @@ -83,7 +83,7 @@ export default class SuiteJcsEd25519Signature2020 extends JsonLinkedDataProofsBa const proof = this._credential.proof; if (!proof) { - return Promise.reject('No proof to validate in signedPayload'); + return Promise.reject(new Error('No proof to validate in signedPayload')); } const payload = clone(this._credential); @@ -98,7 +98,7 @@ export default class SuiteJcsEd25519Signature2020 extends JsonLinkedDataProofsBa const signatureValue = proof.signatureValue; if (!signatureValue) { - return Promise.reject('Proof does not contain the signatureValue'); + return Promise.reject(new Error('Proof does not contain the signatureValue')); } const signature = bs58.decode(signatureValue); diff --git a/libs/sdk/package.json b/libs/sdk/package.json index 853baa5..2854974 100644 --- a/libs/sdk/package.json +++ b/libs/sdk/package.json @@ -1,6 +1,6 @@ { "name": "verifiablecredentials-crypto-sdk-typescript", - "version": "1.1.11-preview.6", + "version": "1.1.11-preview.7", "repository": { "type": "git", "url": "https://github.com/microsoft/VerifiableCredentials-Crypto-SDK-Typescript.git" @@ -33,7 +33,7 @@ "jasmine-reporters": "^2.3.2", "jasmine-spec-reporter": "^6.0.0", "jasmine-ts": "^0.3.0", - "nyc": "14.1.1", + "nyc": "^15.1.0", "prettier": "^1.18.2", "ts-node": "8.5.4", "tslint": "^5.20.0", @@ -52,16 +52,16 @@ "jsonld": "2.0.2", "typescript-map": "0.0.7", "uuid": "^8.3.1", - "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-factory": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-plugin-keyvault": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-protocol-jose": "1.1.11-preview.6", - "verifiablecredentials-crypto-sdk-typescript-protocols-common": "1.1.11-preview.6", - "webcrypto-core": "1.1.1" + "verifiablecredentials-crypto-sdk-typescript-keys": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-keystore": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-cryptofactory-suites": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-elliptic": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-factory": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-plugin-keyvault": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-protocol-jose": "1.1.11-preview.7", + "verifiablecredentials-crypto-sdk-typescript-protocols-common": "1.1.11-preview.7", + "webcrypto-core": "1.1.8" }, "nyc": { "extension": [ diff --git a/libs/sdk/tests/Crypto.spec.ts b/libs/sdk/tests/Crypto.spec.ts index 6f15a75..aef8647 100644 --- a/libs/sdk/tests/Crypto.spec.ts +++ b/libs/sdk/tests/Crypto.spec.ts @@ -34,7 +34,7 @@ describe('Crypto', () => { let throwed = false; await crypto.generateKey(KeyUse.Signature, 'test') .catch((exception) => { - expect(exception).toEqual(`Key generation type 'test' not supported`); + expect(exception.message).toEqual(`Key generation type 'test' not supported`); throwed = true; }) expect(throwed).toBeTruthy(); @@ -42,7 +42,7 @@ describe('Crypto', () => { try { await crypto.generateKey(KeyUse.Encryption); } catch (exception) { - expect(exception).toEqual('not implemented'); + expect(exception.message).toEqual('not implemented'); } }); }); \ No newline at end of file diff --git a/libs/sdk/tests/IJsonLinkedDataProofSuite.spec.ts b/libs/sdk/tests/IJsonLinkedDataProofSuite.spec.ts index af8e33e..4bbf159 100644 --- a/libs/sdk/tests/IJsonLinkedDataProofSuite.spec.ts +++ b/libs/sdk/tests/IJsonLinkedDataProofSuite.spec.ts @@ -34,7 +34,7 @@ describe('SuiteJcsEd25519Signature2020', () => { await suite.verify(); fail('Should throw ' + 'Import a credential by deserialize'); } catch (exception) { - expect(exception).toEqual('Import a credential by deserialize'); + expect(exception.message).toEqual('Import a credential by deserialize'); } let signedPayload = await suite.sign(payload); @@ -65,14 +65,14 @@ describe('SuiteJcsEd25519Signature2020', () => { await suite.sign(undefined); fail('Should throw ' + 'JSON LD proof input is undefined'); } catch (exception) { - expect(exception).toEqual('JSON LD proof input is undefined'); + expect(exception.message).toEqual('JSON LD proof input is undefined'); } try { await suite.sign(suite.sign(' ')); fail('Should throw ' + 'JSON LD proof input should be an object'); } catch (exception) { - expect(exception).toEqual('JSON LD proof input should be an object'); + expect(exception.message).toEqual('JSON LD proof input should be an object'); } try { @@ -81,7 +81,7 @@ describe('SuiteJcsEd25519Signature2020', () => { await suite.verify([key]); fail('Should throw ' + 'No proof to validate in signedPayload'); } catch (exception) { - expect(exception).toEqual('No proof to validate in signedPayload'); + expect(exception.message).toEqual('No proof to validate in signedPayload'); } try { @@ -90,7 +90,7 @@ describe('SuiteJcsEd25519Signature2020', () => { await suite.verify([key]); fail('Should throw ' + 'Proof does not contain the signatureValue'); } catch (exception) { - expect(exception).toEqual('Proof does not contain the signatureValue'); + expect(exception.message).toEqual('Proof does not contain the signatureValue'); } }); diff --git a/libs/sdk/tests/Jose.spec.ts b/libs/sdk/tests/Jose.spec.ts index 1c55c03..afd5f1f 100644 --- a/libs/sdk/tests/Jose.spec.ts +++ b/libs/sdk/tests/Jose.spec.ts @@ -117,7 +117,7 @@ describe('Jose', () => { await jose.verify([jwkPublic]); fail('no token should fail'); } catch (ex) { - expect(ex).toEqual('Import a token by deserialize'); + expect(ex.message).toEqual('Import a token by deserialize'); } // serialize has no token @@ -126,7 +126,7 @@ describe('Jose', () => { await jose.serialize(); fail('no token to serialize should fail'); } catch (ex) { - expect(ex).toEqual('No token to serialize'); + expect(ex.message).toEqual('No token to serialize'); } } }); @@ -167,7 +167,7 @@ describe('Jose', () => { await jose.sign(Buffer.from(JSON.stringify(payload))); fail('Should have throwed exception'); } catch (exception) { - expect(exception).toEqual('Input to sign JWT must be an object'); + expect(exception.message).toEqual('Input to sign JWT must be an object'); } // Negative cases @@ -176,7 +176,7 @@ describe('Jose', () => { await jose.deserialize(serialized); fail(`Serialization format 'JwsCompactJson' is not supported should fail`); } catch (ex) { - expect(ex).toEqual(`Serialization format 'JwsCompactJson' is not supported`); + expect(ex.message).toEqual(`Serialization format 'JwsCompactJson' is not supported`); } }); diff --git a/libs/sdk/tests/JsonLdProof.spec.ts b/libs/sdk/tests/JsonLdProof.spec.ts index 277d999..4bac06c 100644 --- a/libs/sdk/tests/JsonLdProof.spec.ts +++ b/libs/sdk/tests/JsonLdProof.spec.ts @@ -25,9 +25,11 @@ describe('JSONLD proofs', () => { crypto = await crypto.generateKey(KeyUse.Signature, 'recovery'); crypto.builder.useDid(await new LongFormDid(crypto).serialize()); let jsonLdProofBuilder = new JoseBuilder(crypto) - .useJsonLdProofsProtocol('JcsEd25519Signature2020') + .useJsonLdProofsProtocol('JcsEd25519Signature2020') let jsonLdProof: IPayloadProtectionSigning = jsonLdProofBuilder.build(); + expect(jsonLdProofBuilder.isJsonLdProofsProtocol()).toBeTruthy(); + const doc = { '@context': [ 'https://www.w3.org/2018/credentials/v1', @@ -56,23 +58,23 @@ describe('JSONLD proofs', () => { spyOn(Jose, 'payloadIsJsonLdProof').and.returnValue(['xxx', 'JcsEd25519Signature2020']); await jsonLdProof.deserialize(serialized + 'kkk'); } catch (exception) { - expect(exception).toEqual('Could not parse JSON LD token'); + expect(exception.message).toEqual('Could not parse JSON LD token'); } try { await jsonLdProof.serialize(); } catch (exception) { - expect(exception).toEqual('No token to serialize'); + expect(exception.message).toEqual('No token to serialize'); } try { await jsonLdProof.sign(Buffer.from('{}')); } catch (exception) { - expect(exception).toEqual('Input to sign JSON LD must be an object'); + expect(exception.message).toEqual('Input to sign JSON LD must be an object'); } try { spyOn(jsonLdProofBuilder, 'getLinkedDataProofSuite').and.throwError('some error'); await jsonLdProof.sign(doc); } catch (exception) { - expect(exception).toEqual('some error'); + expect(exception.message).toEqual('some error'); } }); diff --git a/libs/sdk/tests/JsonLinkedDataProofsBase.spec.ts b/libs/sdk/tests/JsonLinkedDataProofsBase.spec.ts index 4c3f052..abb0470 100644 --- a/libs/sdk/tests/JsonLinkedDataProofsBase.spec.ts +++ b/libs/sdk/tests/JsonLinkedDataProofsBase.spec.ts @@ -19,28 +19,28 @@ describe('JsonLinkedDataProofsBase', () => { await jsonLdBase.sign({}); fail('should throw ' + 'sign not implemented') } catch (error) { - expect(error).toEqual('sign not implemented') + expect(error.message).toEqual('sign not implemented') } try { await jsonLdBase.verify([]); fail('should throw ' + 'verify not implemented') } catch (error) { - expect(error).toEqual('verify not implemented') + expect(error.message).toEqual('verify not implemented') } try { await jsonLdBase.serialize(); fail('should throw ' + 'No credential to serialize') } catch (error) { - expect(error).toEqual('No credential to serialize') + expect(error.message).toEqual('No credential to serialize') } try { await jsonLdBase.deserialize('xxx'); fail('should throw ' + 'Could not parse JSON LD token') } catch (error) { - expect(error).toEqual('Could not parse JSON LD token') + expect(error.message).toEqual('Could not parse JSON LD token') } }); }); \ No newline at end of file