1
1
import {
2
- BigNumber ,
3
2
BigNumberish ,
4
3
Point ,
5
4
Signature ,
6
5
derivePublicKey ,
7
6
deriveSecretScalar ,
8
- packPublicKey ,
9
7
signMessage ,
10
- unpackPublicKey ,
11
8
verifySignature
12
9
} from "@zk-kit/eddsa-poseidon"
13
10
import { randomBytes } from "crypto"
11
+ import { poseidon2 } from "poseidon-lite/poseidon2"
14
12
15
13
export default class Identity {
16
14
private _privateKey : BigNumberish
17
15
private _secretScalar : string
18
- private _unpackedPublicKey : Point < string >
19
- private _publicKey : string
16
+ private _publicKey : Point < string >
17
+ private _identityCommitment : string
20
18
21
19
/**
22
20
* Initializes the class attributes based on the parameters.
@@ -26,9 +24,9 @@ export default class Identity {
26
24
this . _privateKey = privateKey
27
25
this . _secretScalar = deriveSecretScalar ( privateKey )
28
26
29
- this . _unpackedPublicKey = derivePublicKey ( privateKey )
27
+ this . _publicKey = derivePublicKey ( privateKey )
30
28
31
- this . _publicKey = packPublicKey ( this . _unpackedPublicKey ) as string
29
+ this . _identityCommitment = poseidon2 ( this . _publicKey ) . toString ( )
32
30
}
33
31
34
32
/**
@@ -51,31 +49,27 @@ export default class Identity {
51
49
* Returns the public key.
52
50
* @returns The public key.
53
51
*/
54
- public get publicKey ( ) : string {
52
+ public get publicKey ( ) : Point < string > {
55
53
return this . _publicKey
56
54
}
57
55
58
56
/**
59
- * Returns the unpacked public key .
60
- * @returns The unpacked public key .
57
+ * Returns the identity commitment .
58
+ * @returns The identity commitment .
61
59
*/
62
- public get unpackedPublicKey ( ) : Point < string > {
63
- return this . _unpackedPublicKey
60
+ public get identityCommitment ( ) : string {
61
+ return this . _identityCommitment
64
62
}
65
63
66
64
public signMessage ( message : BigNumberish ) : Signature < string > {
67
65
return signMessage ( this . privateKey , message )
68
66
}
69
67
70
68
public verifySignature ( message : BigNumberish , signature : Signature ) : boolean {
71
- return verifySignature ( message , signature , this . _unpackedPublicKey )
69
+ return verifySignature ( message , signature , this . _publicKey )
72
70
}
73
71
74
- static verifySignature ( message : BigNumberish , signature : Signature , publicKey : BigNumber | Point ) : boolean {
75
- if ( typeof publicKey === "string" || typeof publicKey === "bigint" ) {
76
- publicKey = unpackPublicKey ( publicKey )
77
- }
78
-
72
+ static verifySignature ( message : BigNumberish , signature : Signature , publicKey : Point ) : boolean {
79
73
return verifySignature ( message , signature , publicKey )
80
74
}
81
75
}
0 commit comments