1+ import { derivePublicKey , deriveSecretScalar } from "@zk-kit/eddsa-poseidon"
2+ import { poseidon2 } from "poseidon-lite"
13import { Identity } from "../src"
24
35describe ( "Identity" , ( ) => {
46 const privateKeyText = "secret"
5- const privateKeyHexadecimal = "dd998334940df8931b76d899fdb189415f7ff4280599f03a7574725a166aad7d"
7+ const privateKeyBuffer = Buffer . from ( "another secret" )
68
79 describe ( "# Identity" , ( ) => {
810 it ( "Should create an identity with a random secret (private key)" , ( ) => {
911 const identity = new Identity ( )
12+ const privateKey = Buffer . from ( identity . privateKey )
1013
11- expect ( typeof identity . privateKey ) . toBe ( "string" )
12- expect ( identity . privateKey ) . toHaveLength ( 64 )
13- expect ( typeof identity . secretScalar ) . toBe ( "bigint" )
14- expect ( identity . publicKey ) . toHaveLength ( 2 )
15- expect ( typeof identity . commitment ) . toBe ( "bigint" )
14+ expect ( identity . privateKey ) . toHaveLength ( 32 )
15+ expect ( identity . secretScalar ) . toBe ( deriveSecretScalar ( privateKey ) )
16+ expect ( identity . publicKey ) . toStrictEqual ( derivePublicKey ( privateKey ) )
17+ expect ( identity . commitment ) . toBe ( poseidon2 ( identity . publicKey ) )
1618 } )
1719
18- it ( "Should create deterministic identities from a secret text (private key)" , ( ) => {
20+ it ( "Should create a deterministic identity from a secret text (private key)" , ( ) => {
1921 const identity = new Identity ( privateKeyText )
2022
21- expect ( typeof identity . privateKey ) . toBe ( "string" )
22- expect ( typeof identity . secretScalar ) . toBe ( "bigint" )
23- expect ( identity . publicKey ) . toHaveLength ( 2 )
24- expect ( typeof identity . commitment ) . toBe ( "bigint" )
23+ expect ( identity . secretScalar ) . toBe ( deriveSecretScalar ( privateKeyText ) )
24+ expect ( identity . publicKey ) . toStrictEqual ( derivePublicKey ( privateKeyText ) )
25+ expect ( identity . commitment ) . toBe ( poseidon2 ( identity . publicKey ) )
2526 } )
2627
27- it ( "Should create deterministic identities from a secret hexadecimal (private key)" , ( ) => {
28- const identity = new Identity ( privateKeyHexadecimal )
28+ it ( "Should create a deterministic identity from a secret buffer (private key)" , ( ) => {
29+ const identity = new Identity ( privateKeyBuffer )
2930
30- expect ( typeof identity . privateKey ) . toBe ( "string" )
31- expect ( identity . privateKey ) . toHaveLength ( 64 )
32- expect ( typeof identity . secretScalar ) . toBe ( "bigint" )
33- expect ( identity . publicKey ) . toHaveLength ( 2 )
34- expect ( typeof identity . commitment ) . toBe ( "bigint" )
31+ expect ( identity . secretScalar ) . toBe ( deriveSecretScalar ( privateKeyBuffer ) )
32+ expect ( identity . publicKey ) . toStrictEqual ( derivePublicKey ( privateKeyBuffer ) )
33+ expect ( identity . commitment ) . toBe ( poseidon2 ( identity . publicKey ) )
34+ } )
35+
36+ it ( "Should create the same identity if the private key is the same" , ( ) => {
37+ const identity = new Identity ( )
38+
39+ const identity2 = new Identity ( identity . privateKey )
40+
41+ expect ( identity . privateKey ) . toStrictEqual ( identity2 . privateKey )
42+ expect ( identity . secretScalar ) . toBe ( identity2 . secretScalar )
43+ expect ( identity . publicKey ) . toStrictEqual ( identity2 . publicKey )
44+ expect ( identity . commitment ) . toBe ( identity2 . commitment )
3545 } )
3646
3747 it ( "Should throw an error if the private key is not a string" , ( ) => {
3848 const fun = ( ) => new Identity ( 32 as any )
3949
40- expect ( fun ) . toThrow ( "Parameter 'privateKey' is not a string, received type: number" )
50+ expect ( fun ) . toThrow ( "Parameter 'privateKey' is none of the following types: Buffer, Uint8Array, string" )
51+ } )
52+ } )
53+
54+ describe ( "# export" , ( ) => {
55+ it ( "Should export an identity where the private key is a buffer" , ( ) => {
56+ const identity = new Identity ( privateKeyBuffer )
57+
58+ const privateKey = identity . export ( )
59+
60+ expect ( typeof privateKey ) . toBe ( "string" )
61+ expect ( Buffer . from ( privateKey , "base64" ) ) . toStrictEqual ( privateKeyBuffer )
62+ } )
63+
64+ it ( "Should export an identity where the private key is text" , ( ) => {
65+ const identity = new Identity ( privateKeyBuffer )
66+
67+ const privateKey = identity . export ( )
68+
69+ expect ( typeof privateKey ) . toBe ( "string" )
70+ expect ( Buffer . from ( privateKey , "base64" ) ) . toStrictEqual ( privateKeyBuffer )
71+ } )
72+ } )
73+
74+ describe ( "# import" , ( ) => {
75+ it ( "Should import an identity with a private key of buffer type" , ( ) => {
76+ const identity = new Identity ( privateKeyBuffer )
77+ const privateKey = identity . export ( )
78+
79+ const identity2 = Identity . import ( privateKey )
80+
81+ expect ( identity2 . privateKey ) . toStrictEqual ( identity . privateKey )
82+ expect ( identity2 . secretScalar ) . toBe ( identity . secretScalar )
83+ expect ( identity2 . publicKey ) . toStrictEqual ( identity . publicKey )
84+ expect ( identity2 . commitment ) . toBe ( identity . commitment )
85+ } )
86+
87+ it ( "Should import an identity with a private key of text type" , ( ) => {
88+ const identity = new Identity ( privateKeyText )
89+ const privateKey = identity . export ( )
90+
91+ const identity2 = Identity . import ( privateKey )
92+
93+ expect ( identity2 . privateKey ) . toStrictEqual ( Buffer . from ( identity . privateKey ) )
94+ expect ( identity2 . secretScalar ) . toBe ( identity . secretScalar )
95+ expect ( identity2 . publicKey ) . toStrictEqual ( identity . publicKey )
96+ expect ( identity2 . commitment ) . toBe ( identity . commitment )
97+ } )
98+
99+ it ( "Should import an identity generated from a random private key" , ( ) => {
100+ const identity = new Identity ( )
101+ const privateKey = identity . export ( )
102+
103+ const identity2 = Identity . import ( privateKey )
104+
105+ expect ( identity2 . privateKey ) . toStrictEqual ( Buffer . from ( identity . privateKey ) )
106+ expect ( identity2 . secretScalar ) . toBe ( identity . secretScalar )
107+ expect ( identity2 . publicKey ) . toStrictEqual ( identity . publicKey )
108+ expect ( identity2 . commitment ) . toBe ( identity . commitment )
41109 } )
42110 } )
43111
@@ -63,7 +131,7 @@ describe("Identity", () => {
63131 } )
64132
65133 it ( "Should verify a signature with hexadecimal private key" , ( ) => {
66- const identity = new Identity ( privateKeyHexadecimal )
134+ const identity = new Identity ( privateKeyBuffer )
67135
68136 const signature = identity . signMessage ( "message" )
69137
0 commit comments