mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
fix: set correct metadata message parameter
Former-commit-id: 45d2a29941f53dc1762c01d32fbc5ecb72f959e5 [formerly 194e925a21]
Former-commit-id: 229e602beca805eb6d799bf87b8218c7f5257ffa
This commit is contained in:
4405
packages/identity/package-lock.json
generated
4405
packages/identity/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
import { genRandomIdentity, genIdentityFromSignedMessage, genRandomNumber } from "./strategies"
|
||||
import { genRandomIdentity, genIdentityFromMessage, genRandomNumber } from "./strategies"
|
||||
import * as bigintConversion from "bigint-conversion"
|
||||
import * as ciromlibjs from "circomlibjs"
|
||||
import { Identity, SerializedIdentity } from "@libsem/types"
|
||||
@@ -9,7 +9,7 @@ const poseidonHash = (data: Array<bigint>): bigint => {
|
||||
|
||||
export enum Strategy {
|
||||
RANDOM,
|
||||
SIGNED_MESSAGE,
|
||||
MESSAGE,
|
||||
SERIALIZED
|
||||
}
|
||||
|
||||
@@ -28,8 +28,8 @@ class ZkIdentity {
|
||||
const { identityTrapdoor, identityNullifier } = genRandomIdentity()
|
||||
this.identityTrapdoor = identityTrapdoor
|
||||
this.identityNullifier = identityNullifier
|
||||
} else if (strategy === Strategy.SIGNED_MESSAGE) {
|
||||
const { identityTrapdoor, identityNullifier } = genIdentityFromSignedMessage(metadata)
|
||||
} else if (strategy === Strategy.MESSAGE) {
|
||||
const { identityTrapdoor, identityNullifier } = genIdentityFromMessage(metadata as string)
|
||||
this.identityTrapdoor = identityTrapdoor
|
||||
this.identityNullifier = identityNullifier
|
||||
} else if (strategy === Strategy.SERIALIZED) {
|
||||
@@ -49,7 +49,7 @@ class ZkIdentity {
|
||||
if (data.length !== 2) throw new Error("Format is wrong")
|
||||
return new ZkIdentity(Strategy.SERIALIZED, {
|
||||
identityNullifier: data[0],
|
||||
identityTrapdoor: data[1],
|
||||
identityTrapdoor: data[1]
|
||||
})
|
||||
}
|
||||
/**
|
||||
|
||||
@@ -23,16 +23,14 @@ const genRandomIdentity = (): Identity => {
|
||||
* @param metadata { signedMessage } from which to create identity
|
||||
* @returns Identity
|
||||
*/
|
||||
const genIdentityFromSignedMessage = (metadata: any): Identity => {
|
||||
const genIdentityFromMessage = (message: string): Identity => {
|
||||
const sha256 = (message: string): string => {
|
||||
const hash = _sha256.create()
|
||||
hash.update(message)
|
||||
return hash.hex()
|
||||
}
|
||||
|
||||
const { signedMessage } = metadata
|
||||
|
||||
const messageHash = sha256(signedMessage)
|
||||
const messageHash = sha256(message)
|
||||
const identityNullifier = bigintConversion.hexToBigint(sha256(`${messageHash}identity_nullifier`))
|
||||
const identityTrapdoor = bigintConversion.hexToBigint(sha256(`${messageHash}identity_trapdoor`))
|
||||
|
||||
@@ -42,4 +40,4 @@ const genIdentityFromSignedMessage = (metadata: any): Identity => {
|
||||
}
|
||||
}
|
||||
|
||||
export { genRandomIdentity, genIdentityFromSignedMessage, genRandomNumber }
|
||||
export { genRandomIdentity, genIdentityFromMessage, genRandomNumber }
|
||||
|
||||
@@ -1,51 +1,66 @@
|
||||
import { ZkIdentity } from "../src";
|
||||
import { Strategy, ZkIdentity } from "../src"
|
||||
|
||||
describe("Semaphore identity", () => {
|
||||
describe("Create identity", () => {
|
||||
it("Should create a Semaphore identity", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
expect(typeof identity).toEqual("object");
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
|
||||
expect(typeof identity).toEqual("object")
|
||||
})
|
||||
|
||||
it("Should create a Semaphore identity with a message strategy", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity(Strategy.MESSAGE, "message")
|
||||
|
||||
expect(typeof identity).toEqual("object")
|
||||
})
|
||||
|
||||
it("Should generate secret from identity", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
identity.genSecretFromIdentity();
|
||||
const identitySecret = identity.getSecret();
|
||||
expect(identitySecret.length).toEqual(2);
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
identity.genSecretFromIdentity()
|
||||
const identitySecret = identity.getSecret()
|
||||
|
||||
expect(identitySecret.length).toEqual(2)
|
||||
expect(typeof identitySecret).toEqual("object")
|
||||
})
|
||||
|
||||
it("Should generate random secret", async () => {
|
||||
const secretParts = 5;
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
identity.genRandomSecret(secretParts);
|
||||
const identitySecret = identity.getSecret();
|
||||
const secretParts = 5
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
identity.genRandomSecret(secretParts)
|
||||
const identitySecret = identity.getSecret()
|
||||
|
||||
expect(identitySecret.length).toEqual(5)
|
||||
expect(typeof identitySecret).toEqual("object")
|
||||
})
|
||||
|
||||
it("Should generate identity commitment from identity", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
const identityCommitment: bigint = identity.genIdentityCommitment();
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
const identityCommitment: bigint = identity.genIdentityCommitment()
|
||||
|
||||
expect(typeof identityCommitment).toEqual("bigint")
|
||||
})
|
||||
|
||||
it("Should generate identity commitment from random secret", async () => {
|
||||
const secretParts = 5
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
identity.genRandomSecret(secretParts);
|
||||
const identityCommitment: bigint = identity.genIdentityCommitmentFromSecret();
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
identity.genRandomSecret(secretParts)
|
||||
const identityCommitment: bigint = identity.genIdentityCommitmentFromSecret()
|
||||
|
||||
expect(typeof identityCommitment).toEqual("bigint")
|
||||
})
|
||||
|
||||
it("Should serialize identity", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
const serialized: string = identity.serializeIdentity();
|
||||
expect(typeof serialized).toEqual("string");
|
||||
})
|
||||
it("Should unserialize identity", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity();
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
const serialized: string = identity.serializeIdentity()
|
||||
const unserialized: ZkIdentity = ZkIdentity.genFromSerialized(serialized);
|
||||
|
||||
expect(typeof serialized).toEqual("string")
|
||||
})
|
||||
|
||||
it("Should unserialize identity", async () => {
|
||||
const identity: ZkIdentity = new ZkIdentity()
|
||||
const serialized: string = identity.serializeIdentity()
|
||||
const unserialized: ZkIdentity = ZkIdentity.genFromSerialized(serialized)
|
||||
|
||||
expect(unserialized).toStrictEqual(identity)
|
||||
})
|
||||
})
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user