mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
comments in identity lib
This commit is contained in:
@@ -8,6 +8,12 @@ const poseidonHash = (data: Array<bigint>): 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 {
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user