From 67b3fa14e121cd53eb638eef20fbc0037ab014a8 Mon Sep 17 00:00:00 2001 From: zzcwoshizz Date: Tue, 14 Feb 2023 23:08:37 +0800 Subject: [PATCH] add test-support package for unit test (#46) --- .changeset/config.json | 2 +- jest.config.cjs | 3 +- package.json | 2 +- packages/crypto/src/blake2/asHex.spec.ts | 2 +- packages/crypto/src/blake2/asU8a.spec.ts | 2 +- packages/crypto/src/blake3-2to1/asU8a.spec.ts | 2 +- packages/crypto/src/blake3/asU8a.spec.ts | 2 +- packages/crypto/src/ed25519/fromSeed.spec.ts | 2 +- packages/crypto/src/ed25519/sign.spec.ts | 2 +- packages/crypto/src/hmac/shaAsU8a.spec.ts | 2 +- packages/crypto/src/mnemonic/generate.spec.ts | 7 + packages/crypto/src/pbkdf2/encode.spec.ts | 2 +- packages/crypto/src/rescue/asU8a.spec.ts | 2 +- packages/crypto/src/rlp/encode.spec.ts | 2 +- packages/crypto/src/scrypt/encode.spec.ts | 2 +- .../crypto/src/secp256k1/compress.spec.ts | 2 +- packages/crypto/src/sha/asU8a512.spec.ts | 2 +- packages/ctype/src/publish.spec.ts | 15 +- .../src/ArweaveDidResolver.spec.ts | 2 +- packages/did/src/did/index.spec.ts | 192 ++++++++++++----- packages/test-support/.skip-build | 0 packages/test-support/LICENSE | 201 ++++++++++++++++++ packages/test-support/README.md | 1 + packages/test-support/package.json | 29 +++ packages/test-support/src/dids.ts | 61 ++++++ packages/test-support/src/index.ts | 6 + packages/test-support/src/keyring.ts | 6 + packages/test-support/src/resolver.ts | 15 ++ packages/test-support/tsconfig.build.json | 19 ++ packages/vc/src/credential/index.spec.ts | 3 - packages/vc/src/digest.spec.ts | 2 +- packages/verify/src/digestVerify.spec.ts | 2 +- tsconfig.base.json | 2 + yarn.lock | 11 + 34 files changed, 521 insertions(+), 86 deletions(-) create mode 100644 packages/test-support/.skip-build create mode 100644 packages/test-support/LICENSE create mode 100644 packages/test-support/README.md create mode 100644 packages/test-support/package.json create mode 100644 packages/test-support/src/dids.ts create mode 100644 packages/test-support/src/index.ts create mode 100644 packages/test-support/src/keyring.ts create mode 100644 packages/test-support/src/resolver.ts create mode 100644 packages/test-support/tsconfig.build.json diff --git a/.changeset/config.json b/.changeset/config.json index b0c65ec..b38a901 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -10,5 +10,5 @@ "access": "public", "baseBranch": "master", "updateInternalDependencies": "patch", - "ignore": [] + "ignore": ["test-support"] } diff --git a/jest.config.cjs b/jest.config.cjs index ce1897e..6645e46 100644 --- a/jest.config.cjs +++ b/jest.config.cjs @@ -15,7 +15,8 @@ module.exports = Object.assign({}, config, { '@zcloak/verify(.*)$': '/packages/verify/src/$1', '@zcloak/wasm-asm(.*)$': '/packages/wasm-asm/src/$1', '@zcloak/wasm-bridge(.*)$': '/packages/wasm-bridge/src/$1', - '@zcloak/wasm(.*)$': '/packages/wasm/src/$1' + '@zcloak/wasm(.*)$': '/packages/wasm/src/$1', + 'test-support(.*)$': '/packages/test-support/src/$1' }, testTimeout: 30000 }); diff --git a/package.json b/package.json index 3ed4a32..5506002 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,7 @@ "build:wasm": "install-build-deps && build-wasm", "changeset": "zcloak-exec-changeset", "clean": "zcloak-dev-clean-build", - "lint": "zcloak-dev-run-lint && zcloak-dev-lint-dependencies", + "lint": "zcloak-dev-run-lint", "postinstall": "zcloak-dev-yarn-only", "test": "zcloak-dev-run-test --coverage --forceExit --runInBand --testPathIgnorePatterns e2e", "test:one": "zcloak-dev-run-test --runInBand", diff --git a/packages/crypto/src/blake2/asHex.spec.ts b/packages/crypto/src/blake2/asHex.spec.ts index d47a507..c660b31 100644 --- a/packages/crypto/src/blake2/asHex.spec.ts +++ b/packages/crypto/src/blake2/asHex.spec.ts @@ -7,7 +7,7 @@ import { initCrypto } from '../initCrypto'; import { blake2AsHex } from '.'; describe('blake2AsHex', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/blake2/asU8a.spec.ts b/packages/crypto/src/blake2/asU8a.spec.ts index e372ae0..ed284a6 100644 --- a/packages/crypto/src/blake2/asU8a.spec.ts +++ b/packages/crypto/src/blake2/asU8a.spec.ts @@ -7,7 +7,7 @@ import { initCrypto } from '../initCrypto'; import { blake2AsU8a } from '.'; describe('blake2AsU8a', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/blake3-2to1/asU8a.spec.ts b/packages/crypto/src/blake3-2to1/asU8a.spec.ts index 3f99d91..1d28d42 100644 --- a/packages/crypto/src/blake3-2to1/asU8a.spec.ts +++ b/packages/crypto/src/blake3-2to1/asU8a.spec.ts @@ -7,7 +7,7 @@ import { initCrypto } from '../initCrypto'; import { blake32to1AsU8a } from './asU8a'; describe('blake32to1AsU8a', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/blake3/asU8a.spec.ts b/packages/crypto/src/blake3/asU8a.spec.ts index e29dadf..cb2664e 100644 --- a/packages/crypto/src/blake3/asU8a.spec.ts +++ b/packages/crypto/src/blake3/asU8a.spec.ts @@ -5,7 +5,7 @@ import { initCrypto } from '../initCrypto'; import { blake3AsU8a } from './asU8a'; describe('blake3AsU8a', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/ed25519/fromSeed.spec.ts b/packages/crypto/src/ed25519/fromSeed.spec.ts index 2fecdf1..724f9a1 100644 --- a/packages/crypto/src/ed25519/fromSeed.spec.ts +++ b/packages/crypto/src/ed25519/fromSeed.spec.ts @@ -25,7 +25,7 @@ describe('ed25519PairFromSeed', (): void => { ]) }; - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/ed25519/sign.spec.ts b/packages/crypto/src/ed25519/sign.spec.ts index 367caab..2bee76a 100644 --- a/packages/crypto/src/ed25519/sign.spec.ts +++ b/packages/crypto/src/ed25519/sign.spec.ts @@ -11,7 +11,7 @@ import { ed25519PairFromSeed, ed25519Sign } from '.'; describe('ed25519Sign', (): void => { let pair: Keypair; - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); pair = ed25519PairFromSeed(stringToU8a('12345678901234567890123456789012')); }); diff --git a/packages/crypto/src/hmac/shaAsU8a.spec.ts b/packages/crypto/src/hmac/shaAsU8a.spec.ts index 79d8234..d303bf3 100644 --- a/packages/crypto/src/hmac/shaAsU8a.spec.ts +++ b/packages/crypto/src/hmac/shaAsU8a.spec.ts @@ -7,7 +7,7 @@ import { initCrypto } from '../initCrypto'; import { hmacShaAsU8a } from '.'; describe('hmacShaAsU8a', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/mnemonic/generate.spec.ts b/packages/crypto/src/mnemonic/generate.spec.ts index c4cdcc6..4827f85 100644 --- a/packages/crypto/src/mnemonic/generate.spec.ts +++ b/packages/crypto/src/mnemonic/generate.spec.ts @@ -16,6 +16,7 @@ describe('mnemonicGenerate', (): void => { it.each([12, 15, 18, 21, 24] as 12[])('generates a valid mnemonic (%p words)', (num): void => { const mnemonic = mnemonicGenerate(num); + const isValid = mnemonicValidate(mnemonic); expect(mnemonic.split(' ')).toHaveLength(num); @@ -26,6 +27,12 @@ describe('mnemonicGenerate', (): void => { const m1 = mnemonicGenerate(24); const m2 = mnemonicGenerate(24); + console.log(mnemonicGenerate()); + console.log(mnemonicGenerate()); + console.log(mnemonicGenerate()); + console.log(mnemonicGenerate()); + console.log(mnemonicGenerate()); + expect(m1 === m2).toEqual(false); expect(mnemonicValidate(m1)).toEqual(true); expect(mnemonicValidate(m2)).toEqual(true); diff --git a/packages/crypto/src/pbkdf2/encode.spec.ts b/packages/crypto/src/pbkdf2/encode.spec.ts index c17f9a4..60bfd45 100644 --- a/packages/crypto/src/pbkdf2/encode.spec.ts +++ b/packages/crypto/src/pbkdf2/encode.spec.ts @@ -13,7 +13,7 @@ const KNOWN_SALT = new Uint8Array([ const TEST_PASSWORD = 'test password'; describe('pbkdf2Encode', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/rescue/asU8a.spec.ts b/packages/crypto/src/rescue/asU8a.spec.ts index dd5c738..cc38852 100644 --- a/packages/crypto/src/rescue/asU8a.spec.ts +++ b/packages/crypto/src/rescue/asU8a.spec.ts @@ -5,7 +5,7 @@ import { initCrypto } from '../initCrypto'; import { rescuePrimeAsU8a } from '.'; describe('rescuePrimeAsU8a', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/rlp/encode.spec.ts b/packages/crypto/src/rlp/encode.spec.ts index 9be1e31..d185667 100644 --- a/packages/crypto/src/rlp/encode.spec.ts +++ b/packages/crypto/src/rlp/encode.spec.ts @@ -8,7 +8,7 @@ import { rlpEncode } from './encode'; import * as official from './rlptest.json'; describe('encode rlp', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/scrypt/encode.spec.ts b/packages/crypto/src/scrypt/encode.spec.ts index 14f4f5c..9957f62 100644 --- a/packages/crypto/src/scrypt/encode.spec.ts +++ b/packages/crypto/src/scrypt/encode.spec.ts @@ -19,7 +19,7 @@ export const KNOWN_SALT = new Uint8Array([ ]); describe('scryptEncode', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/secp256k1/compress.spec.ts b/packages/crypto/src/secp256k1/compress.spec.ts index 87ba9c0..7582865 100644 --- a/packages/crypto/src/secp256k1/compress.spec.ts +++ b/packages/crypto/src/secp256k1/compress.spec.ts @@ -7,7 +7,7 @@ import { initCrypto } from '../initCrypto'; import { secp256k1Compress } from '.'; describe('secp256k1Compress', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/crypto/src/sha/asU8a512.spec.ts b/packages/crypto/src/sha/asU8a512.spec.ts index 37345ef..ecd4687 100644 --- a/packages/crypto/src/sha/asU8a512.spec.ts +++ b/packages/crypto/src/sha/asU8a512.spec.ts @@ -5,7 +5,7 @@ import { initCrypto } from '../initCrypto'; import { sha512AsU8a } from '.'; describe('sha512AsU8a', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/ctype/src/publish.spec.ts b/packages/ctype/src/publish.spec.ts index f9860e6..50d5d1a 100644 --- a/packages/ctype/src/publish.spec.ts +++ b/packages/ctype/src/publish.spec.ts @@ -3,19 +3,16 @@ import type { BaseCType } from './types'; -import { initCrypto, mnemonicGenerate } from '@zcloak/crypto'; -import { Did, helpers } from '@zcloak/did'; +import { charlie } from 'test-support'; + +import { initCrypto } from '@zcloak/crypto'; import { DEFAULT_CTYPE_SCHEMA } from './defaults'; import { getCTypeHash, getPublish } from './publish'; describe('publish ctype', (): void => { - let publisher: Did; - beforeAll(async (): Promise => { await initCrypto(); - - publisher = helpers.createEcdsaFromMnemonic(mnemonicGenerate(12)); }); it('get ctype hash', (): void => { @@ -37,8 +34,8 @@ describe('publish ctype', (): void => { required: ['name', 'age'] }; - expect(getCTypeHash(base, publisher.id)).toEqual( - getCTypeHash(base, publisher.getKeyUrl('authentication')) + expect(getCTypeHash(base, charlie.id)).toEqual( + getCTypeHash(base, charlie.getKeyUrl('authentication')) ); }); @@ -61,7 +58,7 @@ describe('publish ctype', (): void => { required: ['name', 'age'] }; - expect(await getPublish(base, publisher)).toMatchObject({ + expect(await getPublish(base, charlie)).toMatchObject({ ...base, $schema: DEFAULT_CTYPE_SCHEMA }); diff --git a/packages/did-resolver/src/ArweaveDidResolver.spec.ts b/packages/did-resolver/src/ArweaveDidResolver.spec.ts index a52803c..cf24c2b 100644 --- a/packages/did-resolver/src/ArweaveDidResolver.spec.ts +++ b/packages/did-resolver/src/ArweaveDidResolver.spec.ts @@ -44,7 +44,7 @@ const DOCUMENT = { describe.skip('ArweaveDidResolver', (): void => { let resolver: ArweaveDidResolver; - beforeEach(() => { + beforeAll(() => { resolver = new ArweaveDidResolver(); }); diff --git a/packages/did/src/did/index.spec.ts b/packages/did/src/did/index.spec.ts index 8b575c8..a15ac09 100644 --- a/packages/did/src/did/index.spec.ts +++ b/packages/did/src/did/index.spec.ts @@ -2,46 +2,25 @@ // SPDX-License-Identifier: Apache-2.0 import { stringToU8a } from '@polkadot/util'; - -import { ethereumEncode, initCrypto, mnemonicGenerate } from '@zcloak/crypto'; -import { MockDidResolver } from '@zcloak/did-resolver'; -import { DidDocument } from '@zcloak/did-resolver/types'; +import { alice, bob, DOCUMENTS, testResolver } from 'test-support'; + +import { + decodeMultibase, + eip712, + ethereumEncode, + initCrypto, + keccak256AsU8a, + secp256k1Verify +} from '@zcloak/crypto'; +import { TypedData } from '@zcloak/crypto/eip712/types'; import { Keyring } from '@zcloak/keyring'; +import { getPublishDocumentTypedData } from '../utils'; import { createEcdsaFromMnemonic } from './helpers'; -const DOCUMENT: DidDocument = { - '@context': ['https://www.w3.org/ns/did/v1'], - id: 'did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1', - controller: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'], - verificationMethod: [ - { - id: 'did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0', - controller: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'], - type: 'EcdsaSecp256k1VerificationKey2019', - publicKeyMultibase: 'zdpxuL2ps42J5jVMJU9DpsMpwnJGsDkTS1N64JC3CbChq' - }, - { - id: 'did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-1', - controller: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'], - type: 'X25519KeyAgreementKey2019', - publicKeyMultibase: 'z9kQXjRwuVoQXKKmotXrkwxMQRhXEQX39A8XfWmZTgb4' - } - ], - authentication: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], - assertionMethod: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], - keyAgreement: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-1'], - capabilityInvocation: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], - capabilityDelegation: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], - service: [] -}; - -const resolver = new MockDidResolver(); - describe('Did', (): void => { beforeAll(async () => { await initCrypto(); - resolver.addDocument(DOCUMENT); }); describe('create', (): void => { @@ -69,8 +48,6 @@ describe('Did', (): void => { 'health correct setup usage father decorate curious copper sorry recycle skin equal'; const did = createEcdsaFromMnemonic(mnemonic, keyring); - resolver.addDocument(did.getDocument()); - expect(did.get([...(did.authentication ?? [])][0]).publicKey).toEqual(key0); expect(did.get([...(did.keyAgreement ?? [])][0]).publicKey).toEqual(key1); expect([...did.controller][0]).toEqual(`did:zk:${ethereumEncode(controllerKey)}`); @@ -85,42 +62,147 @@ describe('Did', (): void => { const document = did.getDocument(); - expect(document.id).toEqual(DOCUMENT.id); - expect(document.controller).toEqual(DOCUMENT.controller); - expect(document.verificationMethod).toEqual(DOCUMENT.verificationMethod); - expect(document.authentication).toEqual(DOCUMENT.authentication); - expect(document.assertionMethod).toEqual(DOCUMENT.assertionMethod); - expect(document.keyAgreement).toEqual(DOCUMENT.keyAgreement); - expect(document.capabilityInvocation).toEqual(DOCUMENT.capabilityInvocation); - expect(document.capabilityDelegation).toEqual(DOCUMENT.capabilityDelegation); - expect(document.service).toEqual(DOCUMENT.service); + expect(document.id).toEqual(DOCUMENTS.alice.id); + expect(document.controller).toEqual(DOCUMENTS.alice.controller); + expect(document.verificationMethod).toEqual(DOCUMENTS.alice.verificationMethod); + expect(document.authentication).toEqual(DOCUMENTS.alice.authentication); + expect(document.assertionMethod).toEqual(DOCUMENTS.alice.assertionMethod); + expect(document.keyAgreement).toEqual(DOCUMENTS.alice.keyAgreement); + expect(document.capabilityInvocation).toEqual(DOCUMENTS.alice.capabilityInvocation); + expect(document.capabilityDelegation).toEqual(DOCUMENTS.alice.capabilityDelegation); + expect(document.service).toEqual(DOCUMENTS.alice.service); }); }); - describe('encrypt and decrypt', (): void => { - it('encrypt and decrypt', async (): Promise => { - const sender = createEcdsaFromMnemonic(mnemonicGenerate(12)); - const receiver = createEcdsaFromMnemonic(mnemonicGenerate(12)); + describe('did.sign', (): void => { + it('sign a uint8array', async (): Promise => { + const message = stringToU8a('abcd'); - resolver.addDocument(sender.getDocument()); - resolver.addDocument(receiver.getDocument()); + const signature1 = await bob.signWithKey(message, 'authentication'); + const signature2 = await bob.signWithKey(message, 'assertionMethod'); + const signature3 = await bob.signWithKey(message, 'capabilityDelegation'); + const signature4 = await bob.signWithKey(message, 'capabilityInvocation'); + + expect( + secp256k1Verify( + keccak256AsU8a(message), + signature1.signature, + bob.get(bob.getKeyUrl('authentication')).publicKey + ) + ).toBe(true); + expect( + secp256k1Verify( + keccak256AsU8a(message), + signature2.signature, + bob.get(bob.getKeyUrl('assertionMethod')).publicKey + ) + ).toBe(true); + expect( + secp256k1Verify( + keccak256AsU8a(message), + signature3.signature, + bob.get(bob.getKeyUrl('capabilityDelegation')).publicKey + ) + ).toBe(true); + expect( + secp256k1Verify( + keccak256AsU8a(message), + signature4.signature, + bob.get(bob.getKeyUrl('capabilityInvocation')).publicKey + ) + ).toBe(true); + }); + it('sign a TypedData', async (): Promise => { + const typedData: TypedData = { + types: { + EIP712Domain: [ + { name: 'name', type: 'string' }, + { name: 'version', type: 'string' } + ], + Test: [{ name: 'test', type: 'bytes' }] + }, + primaryType: 'Test', + domain: { + name: 'Test', + version: '0' + }, + message: { + test: '0x1234' + } + }; + + const signature1 = await alice.signWithKey(typedData, 'authentication'); + const signature2 = await alice.signWithKey(typedData, 'assertionMethod'); + const signature3 = await alice.signWithKey(typedData, 'capabilityDelegation'); + const signature4 = await alice.signWithKey(typedData, 'capabilityInvocation'); + + expect( + secp256k1Verify( + eip712.getMessage(typedData, true), + signature1.signature, + alice.get(alice.getKeyUrl('authentication')).publicKey + ) + ).toBe(true); + expect( + secp256k1Verify( + eip712.getMessage(typedData, true), + signature2.signature, + alice.get(alice.getKeyUrl('assertionMethod')).publicKey + ) + ).toBe(true); + expect( + secp256k1Verify( + eip712.getMessage(typedData, true), + signature3.signature, + alice.get(alice.getKeyUrl('capabilityDelegation')).publicKey + ) + ).toBe(true); + expect( + secp256k1Verify( + eip712.getMessage(typedData, true), + signature4.signature, + alice.get(alice.getKeyUrl('capabilityInvocation')).publicKey + ) + ).toBe(true); + }); + }); + + describe('encrypt and decrypt', (): void => { + it('encrypt and decrypt', async (): Promise => { const message = stringToU8a('abcd'); const { data: encrypted, receiverUrl, senderUrl - } = await sender.encrypt( + } = await alice.encrypt( message, - receiver.getKeyUrl('keyAgreement'), - sender.getKeyUrl('keyAgreement'), - resolver + bob.getKeyUrl('keyAgreement'), + alice.getKeyUrl('keyAgreement'), + testResolver ); - const decrypted = await receiver.decrypt(encrypted, senderUrl, receiverUrl, resolver); + const decrypted = await bob.decrypt(encrypted, senderUrl, receiverUrl, testResolver); expect(decrypted).toEqual(message); }); }); + + describe('did.getPublish', (): void => { + it('getPublish verify', async (): Promise => { + const document = await alice.getPublish(); + + expect(document.proof[0].signatureType).toBe('EcdsaSecp256k1SignatureEip712'); + + console.log(decodeMultibase(document.proof[0].signature)); + expect( + secp256k1Verify( + eip712.getMessage(getPublishDocumentTypedData(document), true), + decodeMultibase(document.proof[0].signature), + alice.get(alice.getKeyUrl('capabilityInvocation')).publicKey + ) + ).toBe(true); + }); + }); }); diff --git a/packages/test-support/.skip-build b/packages/test-support/.skip-build new file mode 100644 index 0000000..e69de29 diff --git a/packages/test-support/LICENSE b/packages/test-support/LICENSE new file mode 100644 index 0000000..0d381b2 --- /dev/null +++ b/packages/test-support/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/packages/test-support/README.md b/packages/test-support/README.md new file mode 100644 index 0000000..db65e2e --- /dev/null +++ b/packages/test-support/README.md @@ -0,0 +1 @@ +# test-support diff --git a/packages/test-support/package.json b/packages/test-support/package.json new file mode 100644 index 0000000..e2d2af3 --- /dev/null +++ b/packages/test-support/package.json @@ -0,0 +1,29 @@ +{ + "author": "zCloak", + "bugs": "https://github.com/zCloak-Network/zkid-sdk/issues", + "contributors": [], + "description": "test-support", + "homepage": "https://github.com/zCloak-Network/zkid-sdk#readme", + "license": "Apache-2.0", + "maintainers": [], + "name": "test-support", + "private": true, + "publishConfig": { + "directory": "build" + }, + "repository": { + "directory": "packages/test-support", + "type": "git", + "url": "https://github.com/zCloak-Network/zkid-sdk.git" + }, + "sideEffects": false, + "type": "module", + "version": "0.0.0", + "main": "index.js", + "dependencies": { + "@zcloak/crypto": "workspace:^", + "@zcloak/ctype": "workspace:^", + "@zcloak/did": "workspace:^", + "@zcloak/did-resolver": "workspace:^" + } +} diff --git a/packages/test-support/src/dids.ts b/packages/test-support/src/dids.ts new file mode 100644 index 0000000..5e91cba --- /dev/null +++ b/packages/test-support/src/dids.ts @@ -0,0 +1,61 @@ +// Copyright 2021-2023 zcloak authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import type { DidDocument } from '@zcloak/did-resolver/types'; + +import { helpers } from '@zcloak/did'; + +import { testKeyring } from './keyring'; + +export const alice = helpers.createEcdsaFromMnemonic( + 'health correct setup usage father decorate curious copper sorry recycle skin equal', + testKeyring +); +export const bob = helpers.createEcdsaFromMnemonic( + 'increase help fortune noise jelly bronze hand among like powder crowd swamp', + testKeyring +); +export const charlie = helpers.createEcdsaFromMnemonic( + 'volume daring fancy soccer verify chronic category hurdle jungle ranch night zoo', + testKeyring +); +export const dave = helpers.createEcdsaFromMnemonic( + 'risk split police harsh domain radio this country moon screen tragic faith', + testKeyring +); +export const eve = helpers.createEcdsaFromMnemonic( + 'group drift eagle symptom pride hour cat seven various exercise sphere raccoon', + testKeyring +); +export const ferdie = helpers.createEcdsaFromMnemonic( + 'lyrics window whale trial web dizzy axis fall use tiny drift anger', + testKeyring +); + +export const DOCUMENTS: Record = { + alice: { + '@context': ['https://www.w3.org/ns/did/v1'], + id: 'did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1', + controller: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'], + verificationMethod: [ + { + id: 'did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0', + controller: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'], + type: 'EcdsaSecp256k1VerificationKey2019', + publicKeyMultibase: 'zdpxuL2ps42J5jVMJU9DpsMpwnJGsDkTS1N64JC3CbChq' + }, + { + id: 'did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-1', + controller: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'], + type: 'X25519KeyAgreementKey2019', + publicKeyMultibase: 'z9kQXjRwuVoQXKKmotXrkwxMQRhXEQX39A8XfWmZTgb4' + } + ], + authentication: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], + assertionMethod: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], + keyAgreement: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-1'], + capabilityInvocation: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], + capabilityDelegation: ['did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1#key-0'], + service: [] + } +}; diff --git a/packages/test-support/src/index.ts b/packages/test-support/src/index.ts new file mode 100644 index 0000000..255ea17 --- /dev/null +++ b/packages/test-support/src/index.ts @@ -0,0 +1,6 @@ +// Copyright 2021-2023 zcloak authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +export * from './dids'; +export * from './keyring'; +export * from './resolver'; diff --git a/packages/test-support/src/keyring.ts b/packages/test-support/src/keyring.ts new file mode 100644 index 0000000..f9e9dc4 --- /dev/null +++ b/packages/test-support/src/keyring.ts @@ -0,0 +1,6 @@ +// Copyright 2021-2023 zcloak authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { Keyring } from '@zcloak/keyring'; + +export const testKeyring = new Keyring(); diff --git a/packages/test-support/src/resolver.ts b/packages/test-support/src/resolver.ts new file mode 100644 index 0000000..db327f9 --- /dev/null +++ b/packages/test-support/src/resolver.ts @@ -0,0 +1,15 @@ +// Copyright 2021-2023 zcloak authors & contributors +// SPDX-License-Identifier: Apache-2.0 + +import { MockDidResolver } from '@zcloak/did-resolver'; + +import { alice, bob, charlie, dave, eve, ferdie } from './dids'; + +export const testResolver = new MockDidResolver(); + +testResolver.addDocument(alice.getDocument()); +testResolver.addDocument(bob.getDocument()); +testResolver.addDocument(charlie.getDocument()); +testResolver.addDocument(dave.getDocument()); +testResolver.addDocument(eve.getDocument()); +testResolver.addDocument(ferdie.getDocument()); diff --git a/packages/test-support/tsconfig.build.json b/packages/test-support/tsconfig.build.json new file mode 100644 index 0000000..0762499 --- /dev/null +++ b/packages/test-support/tsconfig.build.json @@ -0,0 +1,19 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "baseUrl": "..", + "outDir": "./build", + "rootDir": "./src" + }, + "exclude": [ + "**/*.spec.ts", + "**/test/**/*" + ], + "references": [ + { "path": "../crypto/tsconfig.build.json" }, + { "path": "../ctype/tsconfig.build.json" }, + { "path": "../did/tsconfig.build.json" }, + { "path": "../did-resolver/tsconfig.build.json" }, + { "path": "../keyring/tsconfig.build.json" } + ] +} diff --git a/packages/vc/src/credential/index.spec.ts b/packages/vc/src/credential/index.spec.ts index 6bc0808..057e03d 100644 --- a/packages/vc/src/credential/index.spec.ts +++ b/packages/vc/src/credential/index.spec.ts @@ -206,9 +206,6 @@ describe('VerifiableCredential', (): void => { const vc = await vcBuilder.build(issuer, true); - console.log(vc); - console.log(issuer.getDocument()); - expect(isPublicVC(vc)).toBe(true); expect(vc).toMatchObject({ diff --git a/packages/vc/src/digest.spec.ts b/packages/vc/src/digest.spec.ts index b272366..4b57f91 100644 --- a/packages/vc/src/digest.spec.ts +++ b/packages/vc/src/digest.spec.ts @@ -7,7 +7,7 @@ import { calcDigest, DigestPayloadV0 } from './digest'; import { calcRoothash } from './rootHash'; describe('digest', (): void => { - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/packages/verify/src/digestVerify.spec.ts b/packages/verify/src/digestVerify.spec.ts index 09144b7..791d1df 100644 --- a/packages/verify/src/digestVerify.spec.ts +++ b/packages/verify/src/digestVerify.spec.ts @@ -10,7 +10,7 @@ describe('verify digest', (): void => { const holder = 'did:zk:0x082d674c00e27fBaAAE123a85f5024A1DD702e51'; const ctype = '0xd50f5298fda74ff0b46be740e602fa5ce0bc2a48fc5ddfbbae3c0678f59b5b97'; - beforeEach(async (): Promise => { + beforeAll(async (): Promise => { await initCrypto(); }); diff --git a/tsconfig.base.json b/tsconfig.base.json index 105a46e..51b351a 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -25,6 +25,8 @@ "@zcloak/wasm-asm/*": ["wasm-asm/src/*"], "@zcloak/wasm-bridge": ["wasm-bridge/src"], "@zcloak/wasm-bridge/*": ["wasm-bridge/src/*"], + "test-support": ["test-support/src"], + "test-support/*": ["test-support/src/*"], }, "skipLibCheck": true, "resolveJsonModule": true diff --git a/yarn.lock b/yarn.lock index f016590..ed1541f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -13524,6 +13524,17 @@ __metadata: languageName: node linkType: hard +"test-support@workspace:packages/test-support": + version: 0.0.0-use.local + resolution: "test-support@workspace:packages/test-support" + dependencies: + "@zcloak/crypto": "workspace:^" + "@zcloak/ctype": "workspace:^" + "@zcloak/did": "workspace:^" + "@zcloak/did-resolver": "workspace:^" + languageName: unknown + linkType: soft + "text-extensions@npm:^1.0.0": version: 1.9.0 resolution: "text-extensions@npm:1.9.0"