Skip to content

Commit

Permalink
Add: add Param: version for function calcRoothash
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanxuu committed Aug 26, 2023
1 parent bf39112 commit d61c738
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 22 deletions.
1 change: 1 addition & 0 deletions packages/verify/src/vcVerify.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ describe('vc verify', (): void => {
credentialSubject: calcRoothash(
fullVC.credentialSubject as AnyJson,
fullVC.hasher[0],
fullVC.version,
fullVC.credentialSubjectNonceMap || {}
).rootHash,
credentialSubjectNonceMap: {},
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 @@ -81,7 +81,7 @@ export async function vcVerify(
let rootHash: HexString;

if (isPublicVC(vc)) {
rootHash = calcRoothash(vc.credentialSubject, vc.hasher[0]).rootHash;
rootHash = calcRoothash(vc.credentialSubject, vc.hasher[0], vc.version).rootHash;
} else {
const { credentialSubject, credentialSubjectHashes, credentialSubjectNonceMap, hasher } = vc;

Expand Down
4 changes: 2 additions & 2 deletions protocol/vc/src/credential/vc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ export class VerifiableCredentialBuilder {
let rootHashResult: RootHashResult;

if (isPublic) {
rootHashResult = calcRoothash(this.raw.contents, this.raw.hashType);
rootHashResult = calcRoothash(this.raw.contents, this.raw.hashType, this.version);
} else {
rootHashResult = calcRoothash(this.raw.contents, this.raw.hashType, {});
rootHashResult = calcRoothash(this.raw.contents, this.raw.hashType, this.version,{});
}

const digestPayload: DigestPayload<VerifiableCredentialVersion> = {
Expand Down
2 changes: 1 addition & 1 deletion protocol/vc/src/digest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('digest', (): void => {
isUser: true
};

const { rootHash } = calcRoothash(input, 'Blake32to1', {
const { rootHash } = calcRoothash(input, 'Blake32to1', '1', {
'0x6b90277e3f4ab97b83b3fc61ecffa4ec063f70d7255233ef5afdf418dcec3b75':
'0x25807968a4c5f3ce2f116c5914991c97e33020408146406a36e4fa4826dd6c7d',
'0x1300000000000000000000000000000000000000000000000000000000000000':
Expand Down
10 changes: 5 additions & 5 deletions protocol/vc/src/rootHash.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('calcRoothash', (): void => {
isUser: true
};

const { rootHash } = calcRoothash(input, 'RescuePrime', {
const { rootHash } = calcRoothash(input, 'RescuePrime', '1', {
'0x88af5a7ba28c1de54ebd589dea81d30caa3f467646f6d714c0d2604599d63e1e':
'0x357d50aac640931f9976477de30b3b476be4a14ae367b045496670d7a23c457d',
'0x9ad57aefa90d9473f855c14221f330fe959a554b3d86c9d701db11c7559ce107':
Expand All @@ -44,7 +44,7 @@ describe('calcRoothash', (): void => {
types: ['1', '2', '5']
};

const { rootHash } = calcRoothash(input, 'RescuePrime', {
const { rootHash } = calcRoothash(input, 'RescuePrime', '1', {
'0x88af5a7ba28c1de54ebd589dea81d30caa3f467646f6d714c0d2604599d63e1e':
'0x42ecebd0acae94843f906a9fe69e5c672c60d8d688b71aa85948d8e60becf082',
'0x9ad57aefa90d9473f855c14221f330fe959a554b3d86c9d701db11c7559ce107':
Expand All @@ -69,7 +69,7 @@ describe('calcRoothash', (): void => {
types: ['1', '2', '5']
};

const { rootHash } = calcRoothash(input, 'RescuePrime');
const { rootHash } = calcRoothash(input, 'RescuePrime', '1');

expect(rootHash).toEqual('0x9579c93741b6979500bca88dcb104da424c2c3bb0e2fa85f13d76cd00637e46f');
});
Expand All @@ -84,7 +84,7 @@ describe('calcRoothash', (): void => {
isUser: true
};

const { rootHash } = calcRoothash(input, 'Blake32to1', {
const { rootHash } = calcRoothash(input, 'Blake32to1', '1', {
'0x6b90277e3f4ab97b83b3fc61ecffa4ec063f70d7255233ef5afdf418dcec3b75':
'0x25807968a4c5f3ce2f116c5914991c97e33020408146406a36e4fa4826dd6c7d',
'0x1300000000000000000000000000000000000000000000000000000000000000':
Expand All @@ -108,7 +108,7 @@ describe('calcRoothash', (): void => {
isUser: true
};

const { rootHash } = calcRoothash(input, 'RescuePrimeOptimized', {
const { rootHash } = calcRoothash(input, 'RescuePrimeOptimized', '1', {
'0x011eed5f2e2321069b83e2bdd38c790948231cd1d49ed6c4dd09bb0f16b0661c':
'0xfd766e5717ae2ae09abee1268d6a653dc50c2067d9efd665e7b52d8f0f597d2b',
'0x7f8a33bf3f50ca9b84d1dc5561c8f71d48d6256763fd8d0f5e5e902dce5dfb88':
Expand Down
23 changes: 17 additions & 6 deletions protocol/vc/src/rootHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
// SPDX-License-Identifier: Apache-2.0

import type { HexString } from '@zcloak/crypto/types';
import type { AnyJson, HashType } from './types';
import type { AnyJson, HashType, VerifiableCredentialVersion } from './types';

import { assert, bufferToU8a, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { MerkleTree } from 'merkletreejs';

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

import { HASHER } from './hasher';
import { rlpEncode } from './utils';
import { encodeAsSol, rlpEncode } from './utils';

export type RootHashResult = {
rootHash: HexString;
Expand Down Expand Up @@ -71,7 +71,7 @@ export function rootHashFromMerkle(
* @description
* calc rootHash with supplied `input` and `hashType`. Returns [[RootHashResult]].
*/
export function calcRoothash(input: AnyJson, hashType: HashType): RootHashResult;
export function calcRoothash(input: AnyJson, hashType: HashType, version: VerifiableCredentialVersion): RootHashResult;
/**
* @name calcRoothash
* @summary calc rootHash from any json.
Expand All @@ -83,16 +83,27 @@ export function calcRoothash(input: AnyJson, hashType: HashType): RootHashResult
export function calcRoothash(
input: AnyJson,
hashType: HashType,
nonceMap: Record<HexString, HexString>
version: VerifiableCredentialVersion,
nonceMap: Record<HexString, HexString>,
): RootHashResult;

export function calcRoothash(
input: AnyJson,
hashType: HashType,
nonceMap?: Record<HexString, HexString>
version: VerifiableCredentialVersion,
nonceMap?: Record<HexString, HexString>,
): RootHashResult {
const values = Object.values(input);
const encoded: HexString[] = values.map((value) => rlpEncode(value, hashType)).map((value) => u8aToHex(value));
let encoded: HexString[] = [];

// if the version is `2` and the hash in merkletree is Keccak256, we assume this vc aims to be used on chain.
if (version === '2' && hashType === 'Keccak256') {
encoded = values.map((values) => encodeAsSol(values));
} else if (version === '0' || version === '1' || version === '2') {
encoded = values.map((value) => rlpEncode(value, hashType)).map((value) => u8aToHex(value));
} else {
throw new Error('The Version is not supported for calc roothash');
}

if (nonceMap) {
for (const encode of encoded) {
Expand Down
4 changes: 2 additions & 2 deletions protocol/vc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export function rlpEncode(input: NativeType | NativeTypeWithOutNull[], hashType:

export function encodeAsSol(input: NativeType | NativeTypeWithOutNull[]): HexString {
const web3 = new Web3() as any;
console.log("this type is: ", typeof input)
switch (typeof input) {
case "string":
return web3.utils.soliditySha3({ type: 'string', value: input })
Expand All @@ -50,7 +49,8 @@ export function encodeAsSol(input: NativeType | NativeTypeWithOutNull[]): HexStr
return web3.utils.soliditySha3({ type: 'int256[]', value: input });
} else throw new Error(`This object can not be encoded ${input}`);
default:
throw new Error(`This input can not be encoded ${input}`);
const check: never = input;
return check;
}
}

Expand Down
8 changes: 4 additions & 4 deletions protocol/vc/src/vp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ describe('VerifiablePresentation', (): void => {
verifiableCredential: [
{
...vc,
credentialSubject: calcRoothash(CONTENTS1, vc.hasher[0], vc.credentialSubjectNonceMap).rootHash,
credentialSubject: calcRoothash(CONTENTS1, vc.hasher[0], vc.version, vc.credentialSubjectNonceMap).rootHash,
credentialSubjectHashes: [],
credentialSubjectNonceMap: {}
}
Expand Down Expand Up @@ -260,13 +260,13 @@ describe('VerifiablePresentation', (): void => {
verifiableCredential: [
{
...vc1,
credentialSubject: calcRoothash(CONTENTS2, vc1.hasher[0], vc1.credentialSubjectNonceMap).rootHash,
credentialSubject: calcRoothash(CONTENTS2, vc1.hasher[0], vc1.version, vc1.credentialSubjectNonceMap).rootHash,
credentialSubjectHashes: [],
credentialSubjectNonceMap: {}
},
{
...vc2,
credentialSubject: calcRoothash(CONTENTS2, vc2.hasher[0], vc2.credentialSubjectNonceMap).rootHash,
credentialSubject: calcRoothash(CONTENTS2, vc2.hasher[0], vc2.version, vc2.credentialSubjectNonceMap).rootHash,
credentialSubjectHashes: [],
credentialSubjectNonceMap: {}
}
Expand Down Expand Up @@ -362,7 +362,7 @@ describe('VerifiablePresentation', (): void => {
verifiableCredential: [
{
...vc1,
credentialSubject: calcRoothash(CONTENTS1, vc1.hasher[0], vc1.credentialSubjectNonceMap).rootHash,
credentialSubject: calcRoothash(CONTENTS1, vc1.hasher[0], vc1.version, vc1.credentialSubjectNonceMap).rootHash,
credentialSubjectHashes: [],
credentialSubjectNonceMap: {}
},
Expand Down
2 changes: 1 addition & 1 deletion protocol/vc/src/vp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function transformVC(
vc.credentialSubjectNonceMap[encode] = nonceMap[encode];
}
} else {
const { rootHash } = calcRoothash(vc.credentialSubject, vc.hasher[0], vc.credentialSubjectNonceMap);
const { rootHash } = calcRoothash(vc.credentialSubject, vc.hasher[0], vc.version, vc.credentialSubjectNonceMap);

vc.credentialSubject = rootHash;
vc.credentialSubjectHashes = [];
Expand Down

0 comments on commit d61c738

Please sign in to comment.