Skip to content

Commit

Permalink
getAttestationTypedData function add version field (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
zzcwoshizz committed Feb 16, 2023
1 parent 0bb816d commit 66c7c43
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changeset/loud-apples-hug.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@zcloak/verify': minor
'@zcloak/vc': minor
---

getAttestationTypedData function add version field
1 change: 0 additions & 1 deletion packages/did/src/did/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
14 changes: 11 additions & 3 deletions packages/vc/src/credential/vc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<SignedData> {
private _signDigest(
did: Did,
digest: HexString,
version: VerifiableCredentialVersion
): Promise<SignedData> {
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);
}
Expand Down
20 changes: 16 additions & 4 deletions packages/vc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -22,22 +27,29 @@ 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: {
name: 'Attestation',
version: '0'
},
message: {
digest
digest,
version
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/verify/src/vcVerify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 66c7c43

Please sign in to comment.