From d362e471da110db574761ebfd70afc4190953fde Mon Sep 17 00:00:00 2001 From: Andrija Novakovic Date: Tue, 19 Oct 2021 16:19:41 +0200 Subject: [PATCH] comments in identity lib --- packages/identity/src/identity.ts | 31 +++++++++++++++++++++++++++++ packages/identity/src/strategies.ts | 25 +++++++++++++---------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/packages/identity/src/identity.ts b/packages/identity/src/identity.ts index fd5b6aa..1a1bb5f 100644 --- a/packages/identity/src/identity.ts +++ b/packages/identity/src/identity.ts @@ -8,6 +8,12 @@ const poseidonHash = (data: Array): bigint => { }; class ZkIdentity { + /** + * Generates new ZkIdentity + * @param strategy strategy for identity generation + * @param metadata additional data needed to create identity for given strategy + * @returns Identity + */ genIdentity(strategy = "random", metadata: any = {}): Identity { if (strategy === "random") return genRandomIdentity(); else if (strategy === "signedMessage") return genIdentityFromSignedMessage(metadata); @@ -15,10 +21,20 @@ class ZkIdentity { throw new Error("provided strategy is not supported"); } + /** + * Creates secret from ZkIdentity + * @param identity identity to generate secret for + * @returns secret + */ genSecretFromIdentity(identity: Identity): bigint[] { return [identity.identityNullifier, identity.identityTrapdoor]; } + /** + * Creates random secret + * @param parts number of parts in secret + * @returns secret + */ genRandomSecret(parts = 2): bigint[] { const secret: bigint[] = []; for (let i = 0; i < parts; i++) { @@ -27,16 +43,31 @@ class ZkIdentity { return secret; } + /** + * Generate commitment from identity secret + * @param secret identity secret + * @returns identity commitment + */ genIdentityCommitment(secret: bigint[]): bigint { const secretHash = poseidonHash(secret); return poseidonHash([secretHash]); } + /** + * Serializes identity + * @param identity to serialize + * @returns serialized identity + */ serializeIdentity(identity: Identity): string { const data = [identity.identityNullifier.toString(16), identity.identityTrapdoor.toString(16)]; return JSON.stringify(data); } + /** + * Unserializes identity + * @param serialisedIdentity + * @returns ZkIdentity + */ unSerializeIdentity(serialisedIdentity: string): Identity { const data = JSON.parse(serialisedIdentity); return { diff --git a/packages/identity/src/strategies.ts b/packages/identity/src/strategies.ts index 1fd2605..8b7d3f1 100644 --- a/packages/identity/src/strategies.ts +++ b/packages/identity/src/strategies.ts @@ -1,24 +1,29 @@ import * as crypto from 'crypto'; import * as bigintConversion from 'bigint-conversion'; import { sha256 as _sha256 } from "js-sha256"; +import { Identity } from '@libsem/types'; -interface identity { - identityNullifier: bigint, - identityTrapdoor: bigint, -} - -const genRandomNumber = (numBytes = 32): bigint => { +const genRandomNumber = (numBytes = 31): bigint => { return bigintConversion.bufToBigint(crypto.randomBytes(numBytes)) } -const genRandomIdentity = (): identity => { +/** + * + * @returns Identity + */ +const genRandomIdentity = (): Identity => { return { identityNullifier: genRandomNumber(31), identityTrapdoor: genRandomNumber(31) } } -const genIdentityFromSignedMessage = (metadata: any): identity => { +/** + * + * @param metadata { signedMessage } from which to create identity + * @returns Identity + */ +const genIdentityFromSignedMessage = (metadata: any): Identity => { const sha256 = (message: string): string => { const hash = _sha256.create() hash.update(message) @@ -38,7 +43,7 @@ const genIdentityFromSignedMessage = (metadata: any): identity => { } export { - genRandomIdentity, + genRandomIdentity, genIdentityFromSignedMessage, genRandomNumber -} \ No newline at end of file +}