From 66c7c4388adc6f34cace0c00902e70512b090a3e Mon Sep 17 00:00:00 2001 From: zzcwoshizz Date: Thu, 16 Feb 2023 11:33:23 +0800 Subject: [PATCH] getAttestationTypedData function add version field (#47) --- .changeset/loud-apples-hug.md | 6 ++++++ packages/did/src/did/index.spec.ts | 1 - packages/vc/src/credential/vc.ts | 14 +++++++++++--- packages/vc/src/utils.ts | 20 ++++++++++++++++---- packages/verify/src/vcVerify.ts | 2 +- 5 files changed, 34 insertions(+), 9 deletions(-) create mode 100644 .changeset/loud-apples-hug.md diff --git a/.changeset/loud-apples-hug.md b/.changeset/loud-apples-hug.md new file mode 100644 index 0000000..ffcad05 --- /dev/null +++ b/.changeset/loud-apples-hug.md @@ -0,0 +1,6 @@ +--- +'@zcloak/verify': minor +'@zcloak/vc': minor +--- + +getAttestationTypedData function add version field diff --git a/packages/did/src/did/index.spec.ts b/packages/did/src/did/index.spec.ts index a15ac09..0d7aef7 100644 --- a/packages/did/src/did/index.spec.ts +++ b/packages/did/src/did/index.spec.ts @@ -195,7 +195,6 @@ describe('Did', (): void => { expect(document.proof[0].signatureType).toBe('EcdsaSecp256k1SignatureEip712'); - console.log(decodeMultibase(document.proof[0].signature)); expect( secp256k1Verify( eip712.getMessage(getPublishDocumentTypedData(document), true), diff --git a/packages/vc/src/credential/vc.ts b/packages/vc/src/credential/vc.ts index 89f2c33..b89f2a3 100644 --- a/packages/vc/src/credential/vc.ts +++ b/packages/vc/src/credential/vc.ts @@ -121,7 +121,11 @@ export class VerifiableCredentialBuilder { digestPayload, this.digestHashType ); - const { id, signature, type: signType } = await this._signDigest(issuer, digest); + const { + id, + signature, + type: signType + } = await this._signDigest(issuer, digest, this.version); const proof: Proof = { type: signType, @@ -220,11 +224,15 @@ export class VerifiableCredentialBuilder { // sign digest by did, if the key type is `Ed25519VerificationKey2020`, it will sign `digest`, // if the key type is `EcdsaSecp256k1VerificationKey2019`, it will sign `getAttestationTypedData`. // otherwise, it will throw Error - private _signDigest(did: Did, digest: HexString): Promise { + private _signDigest( + did: Did, + digest: HexString, + version: VerifiableCredentialVersion + ): Promise { const { id, type } = did.get(did.getKeyUrl('assertionMethod')); if (type === 'EcdsaSecp256k1VerificationKey2019') { - return did.signWithKey(getAttestationTypedData(digest), id); + return did.signWithKey(getAttestationTypedData(digest, version), id); } else if (type === 'Ed25519VerificationKey2020') { return did.signWithKey(digest, id); } diff --git a/packages/vc/src/utils.ts b/packages/vc/src/utils.ts index 8d9cdd7..b8b7a4f 100644 --- a/packages/vc/src/utils.ts +++ b/packages/vc/src/utils.ts @@ -3,7 +3,12 @@ import type { HexString } from '@polkadot/util/types'; import type { TypedData } from '@zcloak/crypto/eip712/types'; -import type { HashType, NativeType, NativeTypeWithOutNull } from './types'; +import type { + HashType, + NativeType, + NativeTypeWithOutNull, + VerifiableCredentialVersion +} from './types'; import { rlpEncode as rlpEncodeFn } from '@zcloak/crypto'; @@ -22,14 +27,20 @@ export function rlpEncode( } } -export function getAttestationTypedData(digest: HexString): TypedData { +export function getAttestationTypedData( + digest: HexString, + version: VerifiableCredentialVersion +): TypedData { return { types: { EIP712Domain: [ { name: 'name', type: 'string' }, { name: 'version', type: 'string' } ], - Attestation: [{ name: 'digest', type: 'bytes' }] + Attestation: [ + { name: 'digest', type: 'bytes' }, + { name: 'version', type: 'uint256' } + ] }, primaryType: 'Attestation', domain: { @@ -37,7 +48,8 @@ export function getAttestationTypedData(digest: HexString): TypedData { version: '0' }, message: { - digest + digest, + version } }; } diff --git a/packages/verify/src/vcVerify.ts b/packages/verify/src/vcVerify.ts index 82c5174..ece8498 100644 --- a/packages/verify/src/vcVerify.ts +++ b/packages/verify/src/vcVerify.ts @@ -46,7 +46,7 @@ async function verifyShared( const message = proof[0].type === 'EcdsaSecp256k1SignatureEip712' - ? eip712.getMessage(getAttestationTypedData(digest), true) + ? eip712.getMessage(getAttestationTypedData(digest, version), true) : digest; const proofValid = await (resolverOrDidDocument