@semaphore-protocol/identity 4.0.0-beta.1 → 4.0.0-beta.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2169 -0
- package/dist/index.js +2167 -0
- package/dist/types/index.d.ts +86 -2
- package/package.json +13 -19
- package/src/index.ts +140 -3
- package/dist/index.browser.js +0 -1075
- package/dist/index.node.cjs +0 -233
- package/dist/index.node.js +0 -231
- package/dist/types/bytes-to-bigint.d.ts +0 -1
- package/dist/types/identity.d.ts +0 -83
- package/dist/types/random-number.browser.d.ts +0 -1
- package/dist/types/random-number.node.d.ts +0 -1
- package/src/bytes-to-bigint.ts +0 -9
- package/src/identity.ts +0 -115
- package/src/random-number.browser.ts +0 -8
- package/src/random-number.node.ts +0 -9
package/src/identity.ts
DELETED
|
@@ -1,115 +0,0 @@
|
|
|
1
|
-
import type { Point } from "@zk-kit/baby-jubjub"
|
|
2
|
-
import { Signature, derivePublicKey, deriveSecretScalar, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"
|
|
3
|
-
import type { BigNumberish } from "@zk-kit/utils"
|
|
4
|
-
import { poseidon2 } from "poseidon-lite/poseidon2"
|
|
5
|
-
import { randomNumber } from "./random-number.node"
|
|
6
|
-
|
|
7
|
-
/**
|
|
8
|
-
* The Semaphore identity is essentially an {@link https://www.rfc-editor.org/rfc/rfc8032 | EdDSA}
|
|
9
|
-
* public/private key pair. The {@link https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon | EdDSA implementation}
|
|
10
|
-
* in this library uses {@link https://eips.ethereum.org/EIPS/eip-2494 | Baby Jubjub} for public key generation
|
|
11
|
-
* and {@link https://www.poseidon-hash.info | Poseidon} for signatures.
|
|
12
|
-
* In addition, the commitment, i.e. the hash of the public key, is used to represent
|
|
13
|
-
* Semaphore identities in groups, adding an additional layer of privacy and security.
|
|
14
|
-
*/
|
|
15
|
-
export default class Identity {
|
|
16
|
-
// The EdDSA private key, passed as a parameter or generated randomly.
|
|
17
|
-
private _privateKey: BigNumberish
|
|
18
|
-
// The secret scalar derived from the private key.
|
|
19
|
-
// It is used in circuits to derive the public key.
|
|
20
|
-
private _secretScalar: string
|
|
21
|
-
// The EdDSA public key, derived from the private key.
|
|
22
|
-
private _publicKey: Point<string>
|
|
23
|
-
// The identity commitment used as a public value in Semaphore groups.
|
|
24
|
-
private _commitment: string
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Initializes the class attributes based on a given private key.
|
|
28
|
-
* If the private key is not passed as a parameter, a random key is generated.
|
|
29
|
-
* The constructor calculates the secret scalar and public key from the private key,
|
|
30
|
-
* and computes a commitment of the public key using a hash function (Poseidon).
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* // Generates an identity.
|
|
34
|
-
* const { privateKey, publicKey, commitment } = new Identity("private-key")
|
|
35
|
-
* @example
|
|
36
|
-
* // Generates an identity with a random private key.
|
|
37
|
-
* const { privateKey, publicKey, commitment } = new Identity()
|
|
38
|
-
*
|
|
39
|
-
* @param privateKey The private key used to derive the public key.
|
|
40
|
-
*/
|
|
41
|
-
constructor(privateKey: BigNumberish = randomNumber().toString()) {
|
|
42
|
-
this._privateKey = privateKey
|
|
43
|
-
this._secretScalar = deriveSecretScalar(privateKey)
|
|
44
|
-
this._publicKey = derivePublicKey(privateKey)
|
|
45
|
-
this._commitment = poseidon2(this._publicKey).toString()
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Returns the private key.
|
|
50
|
-
* @returns The private key as a {@link https://zkkit.pse.dev/types/_zk_kit_utils.BigNumberish.html | BigNumberish}.
|
|
51
|
-
*/
|
|
52
|
-
public get privateKey(): BigNumberish {
|
|
53
|
-
return this._privateKey
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Returns the secret scalar.
|
|
58
|
-
* @returns The secret scalar as a string.
|
|
59
|
-
*/
|
|
60
|
-
public get secretScalar(): string {
|
|
61
|
-
return this._secretScalar
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Returns the public key as a Baby Jubjub {@link https://zkkit.pse.dev/types/_zk_kit_baby_jubjub.Point.html | Point}.
|
|
66
|
-
* @returns The public key as a point.
|
|
67
|
-
*/
|
|
68
|
-
public get publicKey(): Point<string> {
|
|
69
|
-
return this._publicKey
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
/**
|
|
73
|
-
* Returns the commitment hash of the public key.
|
|
74
|
-
* @returns The commitment as a string.
|
|
75
|
-
*/
|
|
76
|
-
public get commitment(): string {
|
|
77
|
-
return this._commitment
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Generates a signature for a given message using the private key.
|
|
82
|
-
* This method demonstrates how to sign a message and could be used
|
|
83
|
-
* for authentication or data integrity.
|
|
84
|
-
*
|
|
85
|
-
* @example
|
|
86
|
-
* const identity = new Identity()
|
|
87
|
-
* const signature = identity.signMessage("message")
|
|
88
|
-
*
|
|
89
|
-
* @param message The message to be signed.
|
|
90
|
-
* @returns A {@link https://zkkit.pse.dev/types/_zk_kit_eddsa_poseidon.Signature.html | Signature} object containing the signature components.
|
|
91
|
-
*/
|
|
92
|
-
public signMessage(message: BigNumberish): Signature<string> {
|
|
93
|
-
return signMessage(this.privateKey, message)
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/**
|
|
97
|
-
* Verifies a signature against a given message and public key.
|
|
98
|
-
* This static method allows for the verification of signatures without needing
|
|
99
|
-
* an instance of the Identity class. It's useful for cases where you only have
|
|
100
|
-
* the public key, the message and a signature, and need to verify if they match.
|
|
101
|
-
*
|
|
102
|
-
* @example
|
|
103
|
-
* const identity = new Identity()
|
|
104
|
-
* const signature = identity.signMessage("message")
|
|
105
|
-
* Identity.verifySignature("message", signature, identity.publicKey)
|
|
106
|
-
*
|
|
107
|
-
* @param message The message that was signed.
|
|
108
|
-
* @param signature The signature to verify.
|
|
109
|
-
* @param publicKey The public key to use for verification.
|
|
110
|
-
* @returns A boolean indicating whether the signature is valid.
|
|
111
|
-
*/
|
|
112
|
-
static verifySignature(message: BigNumberish, signature: Signature, publicKey: Point): boolean {
|
|
113
|
-
return verifySignature(message, signature, publicKey)
|
|
114
|
-
}
|
|
115
|
-
}
|