Skip to content

Commit

Permalink
Add: add encodeAsSol function and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanxuu committed Aug 25, 2023
1 parent d592a45 commit bf39112
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .yarn/releases/yarn-4.0.0-rc.40.cjs

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions protocol/vc/src/rootHash.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright 2021-2023 zcloak authors & contributors
// SPDX-License-Identifier: Apache-2.0

import { HexString } from '@polkadot/util/types';
import { initCrypto } from '@zcloak/crypto';

import { calcRoothash } from './rootHash';
import { encodeAsSol } from './utils';

describe('calcRoothash', (): void => {
beforeAll(async (): Promise<void> => {
Expand Down Expand Up @@ -120,4 +122,20 @@ describe('calcRoothash', (): void => {
expect(rootHash).toEqual('0x2cbd72a75cf5797fb6893b222a45de60eaa2ddf2e5d435d67e5ed8067efb76e3');
});
});
describe('calcContentAsSolidity', () => {
it('calcRoothash', (): void => {
const input = {
name: 'zCloak',
age: 111,
number_array: [11, 12, 13],
isUser: true,
string_array: ["zCloak", "database"]
};
const values = Object.values(input);
const encodedClaimHashes: HexString[] = values.map((values) => encodeAsSol(values));

expect(encodedClaimHashes).toEqual(["0x28cb5b00333a3266fa3d92f3426ad4ef1d20018b44dd64913578d43438b4051a", "0x39f2babe526038520877fc7c33d81accf578af4a06c5fa6b0d038cae36e12711", "0x8d20824cd86ad1c5673537a4b5d1ee68bd9265b7d357e82bc76483ae879322c2", "0x5fe7f977e71dba2ea1a68e21057beebb9be2ac30c6410aa38d4f3fbe41dcffd2", "0x69ab0c29dcf9256886435d72272bceee1f0a0b2cee41865d3b56f9726e7fe0a3",
]);
});
});
});
28 changes: 28 additions & 0 deletions protocol/vc/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import { rlpEncode as rlpEncodeFn } from '@zcloak/crypto';

import { HASHER } from './hasher';

import Web3 from 'web3';

export function rlpEncode(input: NativeType | NativeTypeWithOutNull[], hashType: HashType): Uint8Array {
const result = rlpEncodeFn(input);

Expand All @@ -26,6 +28,32 @@ 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)

This comment has been minimized.

Copy link
@jonathanxuu

jonathanxuu Aug 26, 2023

Author Collaborator

Already removed the console.log("this type is: ", typeof input) in the next commit

switch (typeof input) {
case "string":
return web3.utils.soliditySha3({ type: 'string', value: input })

case "number":
if (input % 1 !== 0) {
throw new Error(`Can not encode number with dot`);
}
return web3.utils.soliditySha3({ type: 'int256', value: input });
case "boolean":
return web3.utils.soliditySha3({ type: 'bool', value: input });
case "object":
if (Array.isArray(input) && typeof input[0] == 'string') {
const encodedParams = web3.eth.abi.encodeParameters(['string[]'], [input]);
return web3.utils.keccak256(encodedParams);
} 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}`);
default:
throw new Error(`This input can not be encoded ${input}`);
}
}

export function signedVCMessage(digest: HexString, version: VerifiableCredentialVersion): Uint8Array {
return u8aConcat(stringToU8a('CredentialVersionedDigest'), numberToU8a(Number(version), 16), digest);
}
Expand Down

0 comments on commit bf39112

Please sign in to comment.