Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Digest hash #56

Merged
merged 2 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/angry-coins-shake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@zcloak/vc': minor
---

encode did when calc digest
5 changes: 5 additions & 0 deletions .changeset/wild-foxes-serve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@zcloak/did': minor
---

encodeDidUrl: the identifier as HexString, and prefix is `did:zk`.
6 changes: 6 additions & 0 deletions packages/did/src/did/details.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { assert } from '@polkadot/util';

import { parseDid } from '@zcloak/did-resolver/parseDid';

import { encodeDidUrl } from '../utils';

export abstract class DidDetails implements IDidDetails {
public id: DidUrl;
public identifier: string;
Expand Down Expand Up @@ -64,4 +66,8 @@ export abstract class DidDetails implements IDidDetails {

return method;
}

public encodeDidUrl(): Uint8Array {
return encodeDidUrl(this.id);
}
}
1 change: 1 addition & 0 deletions packages/did/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * as helpers from './did/helpers';
export * as keys from './keys';

export * from './hasher';
export * from './utils';
36 changes: 36 additions & 0 deletions packages/did/src/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2021-2023 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { alice } from 'test-support';

import { initCrypto } from '@zcloak/crypto';

import { encodeDidUrl } from './utils';

describe('Did utils', (): void => {
beforeAll(async () => {
await initCrypto();
});

it('encode did url', () => {
const encoded = encodeDidUrl(alice.id);

expect(encoded).toEqual(
new Uint8Array([
100, 105, 100, 58, 122, 107, 58, 17, 248, 183, 127, 52, 252, 241, 75, 112, 149, 191, 82, 40,
172, 6, 6, 50, 78, 130, 209
])
);
});

it('encode did has fragment', () => {
const encoded = encodeDidUrl(alice.getKeyUrl('authentication'));

expect(encoded).toEqual(
new Uint8Array([
100, 105, 100, 58, 122, 107, 58, 17, 248, 183, 127, 52, 252, 241, 75, 112, 149, 191, 82, 40,
172, 6, 6, 50, 78, 130, 209
])
);
});
});
23 changes: 23 additions & 0 deletions packages/did/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import type { DidUrl, VerificationMethodType } from '@zcloak/did-resolver/types';
import type { KeypairType } from '@zcloak/keyring/types';

import { stringToU8a, u8aConcat } from '@polkadot/util';

import { parseDid } from '@zcloak/did-resolver/parseDid';

/**
Expand Down Expand Up @@ -48,3 +50,24 @@ export function typeTransform(type: KeypairType): VerificationMethodType {
throw new Error(`Can not transform type: ${type}`);
}
}

/**
* @name encodeDidUrl
* @summary
* encode `didUrl` to a bytes.
* @description
* encode `didUrl` to bytes with the identifier as HexString, and the prefix is `did:zk:`. Returns `Uint8Array`.
* @example
* ```typescript
* import { encodeDidUrl } from '@zcloak/did'
*
* encodeDidUrl('did:zk:0x11f8b77F34FCF14B7095BF5228Ac0606324E82D1'); // [...]
* ```
*/
export function encodeDidUrl(didUrl: DidUrl): Uint8Array {
const { identifier, method } = parseDid(didUrl);

const prefix = stringToU8a(`did:${method}:`);

return u8aConcat(prefix, identifier);
}
4 changes: 3 additions & 1 deletion packages/vc/src/digest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { HashType, VerifiableCredentialVersion } from './types';

import { numberToU8a, stringToU8a, u8aConcat, u8aToHex } from '@polkadot/util';

import { encodeDidUrl } from '@zcloak/did';

import { DEFAULT_DIGEST_HASH_TYPE } from './defaults';
import { HASHER } from './hasher';

Expand Down Expand Up @@ -73,7 +75,7 @@ export function calcDigest<Version extends VerifiableCredentialVersion>(
} else {
encoded = u8aConcat(
payload.rootHash,
stringToU8a(payload.holder),
encodeDidUrl(payload.holder),
numberToU8a((payload as DigestPayload<'1'>).issuanceDate),
numberToU8a(payload.expirationDate || 0),
payload.ctype
Expand Down