Skip to content

Commit

Permalink
Update: update calcRoothash
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanxuu committed Aug 29, 2023
1 parent b9980de commit eb9d8f0
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 19 deletions.
4 changes: 2 additions & 2 deletions protocol/vc/src/credential/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ describe('VerifiableCredential', (): void => {
digestHashType: 'Keccak256'
});

const vc = await vcBuilder.build(issuer, false,["did:zk:0xFeDE01Ff4402e35c6f6d20De9821d64bDF4Ba563"]);
const vc = await vcBuilder.build(issuer, true);
expect(isPrivateVC(vc)).toBe(false);

expect(vc).toMatchObject({
Expand All @@ -204,7 +204,7 @@ describe('VerifiableCredential', (): void => {
ctype: ctype.$id,
issuanceDate: now,
credentialSubject: CONTENTS,
issuer: [issuer.id, "did:zk:0xFeDE01Ff4402e35c6f6d20De9821d64bDF4Ba563"],
issuer: [issuer.id],
holder: holder.id,
hasher: ['RescuePrimeOptimized', 'Keccak256'],
proof: [
Expand Down
7 changes: 5 additions & 2 deletions protocol/vc/src/credential/vc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type { HashType, Proof, RawCredential, VerifiableCredential, VerifiableCr
import { assert } from '@polkadot/util';

import { base58Encode } from '@zcloak/crypto';
import { vcVerify } from '@zcloak/verify/vcVerify';
// import { vcVerify } from '@zcloak/verify/vcVerify';

import { DEFAULT_CONTEXT, DEFAULT_VC_VERSION } from '../defaults';
import { calcDigest, DigestPayload } from '../digest';
Expand Down Expand Up @@ -253,8 +253,11 @@ export class VerifiableCredentialBuilder {

if (version === '1') {
message = signedVCMessage(digest, version);
} else {
} else if (version =='0'|| version =='2'){

This comment has been minimized.

Copy link
@jonathanxuu

jonathanxuu Aug 29, 2023

Author Collaborator

Already change the == to === in next commit

message = digest;
} else {
const check: never = version;
return check;
}

const signDidUrl: DidUrl = did.getKeyUrl('assertionMethod');
Expand Down
10 changes: 2 additions & 8 deletions protocol/vc/src/is.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ export function isAttesterProof(issuer: unknown, proof: unknown): boolean {
if (typeof issuer === 'string' && isArray(proof) && proof.length === 1) {
return isAttesterMapping(issuer, proof);
} else if (isArray(issuer) && isArray(proof) && issuer.length === proof.length) {
let check = true;
for (let i = 0; i < issuer.length; i++) {
check = isAttesterMapping(issuer[i], proof[i]) && check;
}
const check = issuer.every((issuer, index) => isAttesterMapping(issuer, proof[index]));
return check;
} else {
return false;
Expand All @@ -98,10 +95,7 @@ export function isAttester(value: unknown, version: unknown): boolean {
if (typeof value === 'string' && (version === '0' || version === '1')) {
return isDidUrl(value)
} else if (isArray(value) && value.length !== 0 && version === '2'){
let check = true;
for (const item of value) {
check = isDidUrl(item) && check;
}
const check = value.every(isDidUrl);
return check;
} else {
return false;
Expand Down
13 changes: 9 additions & 4 deletions protocol/vc/src/rootHash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,17 @@ export function calcRoothash(
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') {
if (version === '2') {
if (hashType == 'Keccak256'){
encoded = values.map((values) => encodeAsSol(values));
} else {
encoded = values.map((value) => rlpEncode(value, hashType)).map((value) => u8aToHex(value));
}
} else if (version === '0' || version === '1') {
encoded = values.map((value) => rlpEncode(value, hashType)).map((value) => u8aToHex(value));
} else {
throw new Error('The Version is not supported for calc roothash');
const check: never = version;
return check;
}

if (nonceMap) {
Expand Down
19 changes: 16 additions & 3 deletions protocol/vc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { rlpEncode as rlpEncodeFn } from '@zcloak/crypto';
import { HASHER } from './hasher';

import Web3 from 'web3';
const web3 = new Web3() as any;

export function rlpEncode(input: NativeType | NativeTypeWithOutNull[], hashType: HashType): Uint8Array {
const result = rlpEncodeFn(input);
Expand All @@ -29,13 +30,12 @@ export function rlpEncode(input: NativeType | NativeTypeWithOutNull[], hashType:
}

export function encodeAsSol(input: NativeType | NativeTypeWithOutNull[]): HexString {
const web3 = new Web3() as any;
switch (typeof input) {
case "string":
return web3.utils.soliditySha3({ type: 'string', value: input })

case "number":
if (input % 1 !== 0) {
if (_isDecimalNumber(input)) {
throw new Error(`Can not encode number with dot`);
}
return web3.utils.soliditySha3({ type: 'int256', value: input });
Expand All @@ -48,8 +48,11 @@ export function encodeAsSol(input: NativeType | NativeTypeWithOutNull[]): HexStr
} else if ((Array.isArray(input) && typeof input[0] == 'number')) {
return web3.utils.soliditySha3({ type: 'int256[]', value: input });
} else throw new Error(`This object can not be encoded ${input}`);
case "undefined":
throw new Error(`Can not encode this undefined type: ${input}`);
default:
throw new Error(`Can not encode this type: ${input}`)
const check: never = input;
return check;
}
}

Expand All @@ -69,3 +72,13 @@ export function signedVPMessage(
challenge || new Uint8Array()
);
}

// helper function, check whether the number is an integer or not
function _isDecimalNumber(num: number): boolean {
const numStr = num.toString();
if (numStr.includes(".")) {
return true;
} else {
return false;
}
}

0 comments on commit eb9d8f0

Please sign in to comment.