|
1 |
| -import { BigNumber } from "@ethersproject/bignumber" |
2 | 1 | import Identity from "./identity"
|
3 | 2 |
|
4 | 3 | describe("Identity", () => {
|
5 |
| - describe("# Identity", () => { |
6 |
| - it("Should not create a identity if the parameter is not valid", () => { |
7 |
| - const fun1 = () => new Identity(13 as any) |
8 |
| - const fun2 = () => new Identity(true as any) |
9 |
| - const fun3 = () => new Identity((() => true) as any) |
10 |
| - |
11 |
| - expect(fun1).toThrow("Parameter 'identityOrMessage' is not a string") |
12 |
| - expect(fun2).toThrow("Parameter 'identityOrMessage' is not a string") |
13 |
| - expect(fun3).toThrow("Parameter 'identityOrMessage' is not a string") |
14 |
| - }) |
15 |
| - |
16 |
| - it("Should create random identities", () => { |
17 |
| - const identity1 = new Identity() |
18 |
| - const identity2 = new Identity() |
19 |
| - |
20 |
| - expect(identity1.trapdoor).not.toBe(identity2.getTrapdoor()) |
21 |
| - expect(identity1.nullifier).not.toBe(identity2.getNullifier()) |
22 |
| - expect(identity1.secret).not.toBe(identity2.getSecret()) |
23 |
| - expect(identity1.commitment).not.toBe(identity2.getCommitment()) |
24 |
| - }) |
25 |
| - |
26 |
| - it("Should create deterministic identities from a message", () => { |
27 |
| - const identity1 = new Identity("message") |
28 |
| - const identity2 = new Identity("message") |
29 |
| - |
30 |
| - expect(identity1.trapdoor).toBe(identity2.getTrapdoor()) |
31 |
| - expect(identity1.nullifier).toBe(identity2.getNullifier()) |
32 |
| - }) |
33 |
| - |
34 |
| - it("Should create deterministic identities from number/boolean messages", () => { |
35 |
| - const identity1 = new Identity("true") |
36 |
| - const identity2 = new Identity("true") |
37 |
| - const identity3 = new Identity("7") |
38 |
| - const identity4 = new Identity("7") |
39 |
| - |
40 |
| - expect(identity1.trapdoor).toBe(identity2.getTrapdoor()) |
41 |
| - expect(identity1.nullifier).toBe(identity2.getNullifier()) |
42 |
| - expect(identity3.trapdoor).toBe(identity4.getTrapdoor()) |
43 |
| - expect(identity3.nullifier).toBe(identity4.getNullifier()) |
44 |
| - }) |
| 4 | + const privateKey = "secret" |
45 | 5 |
|
46 |
| - it("Should not recreate an existing invalid identity", () => { |
47 |
| - const fun = () => new Identity('[true, "01323"]') |
| 6 | + describe("# Identity", () => { |
| 7 | + it("Should create a random identity", () => { |
| 8 | + const identity = new Identity() |
48 | 9 |
|
49 |
| - expect(fun).toThrow("invalid BigNumber value") |
| 10 | + expect(Buffer.isBuffer(identity.privateKey)).toBeTruthy() |
| 11 | + expect(typeof identity.publicKey).toBe("string") |
| 12 | + expect(typeof identity.secretScalar).toBe("string") |
| 13 | + expect(identity.unpackedPublicKey).toHaveLength(2) |
50 | 14 | })
|
51 | 15 |
|
52 |
| - it("Should recreate an existing identity", () => { |
53 |
| - const identity1 = new Identity("message") |
54 |
| - |
55 |
| - const identity2 = new Identity(identity1.toString()) |
| 16 | + it("Should create deterministic identities from a secret (private key)", () => { |
| 17 | + const identity = new Identity(privateKey) |
56 | 18 |
|
57 |
| - expect(identity1.trapdoor).toBe(identity2.getTrapdoor()) |
58 |
| - expect(identity1.nullifier).toBe(identity2.getNullifier()) |
| 19 | + expect(typeof identity.privateKey).toBe("string") |
| 20 | + expect(typeof identity.publicKey).toBe("string") |
| 21 | + expect(typeof identity.secretScalar).toBe("string") |
| 22 | + expect(identity.unpackedPublicKey).toHaveLength(2) |
| 23 | + expect(typeof identity.unpackedPublicKey[0]).toBe("string") |
59 | 24 | })
|
60 | 25 | })
|
61 | 26 |
|
62 |
| - describe("# getTrapdoor", () => { |
63 |
| - it("Should return the identity trapdoor", () => { |
64 |
| - const identity = new Identity("message") |
| 27 | + describe("# signMessage", () => { |
| 28 | + it("Should sign a message", () => { |
| 29 | + const identity = new Identity(privateKey) |
65 | 30 |
|
66 |
| - const trapdoor = identity.getTrapdoor() |
| 31 | + const signature = identity.signMessage("message") |
67 | 32 |
|
68 |
| - expect(trapdoor.toString()).toBe( |
69 |
| - "11566083507498623434013707198824105161167204201250008419741119866456392774309" |
70 |
| - ) |
| 33 | + expect(signature.R8).toHaveLength(2) |
| 34 | + expect(typeof signature.R8[0]).toBe("string") |
| 35 | + expect(typeof signature.S).toBe("string") |
71 | 36 | })
|
72 | 37 | })
|
73 | 38 |
|
74 |
| - describe("# getNullifier", () => { |
75 |
| - it("Should return the identity nullifier", () => { |
76 |
| - const identity = new Identity("message") |
77 |
| - |
78 |
| - const nullifier = identity.getNullifier() |
79 |
| - |
80 |
| - expect(nullifier.toString()).toBe( |
81 |
| - "14070056666392584007908120012103355272369511035580155843212703537125048345255" |
82 |
| - ) |
83 |
| - }) |
84 |
| - }) |
| 39 | + describe("# verifySignature", () => { |
| 40 | + it("Should verify a signature", () => { |
| 41 | + const identity = new Identity(privateKey) |
85 | 42 |
|
86 |
| - describe("# getSecret", () => { |
87 |
| - it("Should return an identity secret", () => { |
88 |
| - const { secret } = new Identity("message") |
| 43 | + const signature = identity.signMessage("message") |
89 | 44 |
|
90 |
| - expect(secret.toString()).toBe( |
91 |
| - "17452394798940441025978193762953691632066258438336130543532009665042636950194" |
92 |
| - ) |
| 45 | + expect(identity.verifySignature("message", signature)).toBeTruthy() |
93 | 46 | })
|
94 |
| - }) |
95 |
| - |
96 |
| - describe("# getCommitment", () => { |
97 |
| - it("Should return an identity commitment", () => { |
98 |
| - const { commitment } = new Identity("message") |
99 |
| - |
100 |
| - expect(commitment.toString()).toBe( |
101 |
| - "19361462367798001240039467285882167157718016385695743307694056771074972404368" |
102 |
| - ) |
103 |
| - }) |
104 |
| - }) |
105 | 47 |
|
106 |
| - describe("# toString", () => { |
107 |
| - it("Should return a string", () => { |
108 |
| - const identity = new Identity("message") |
| 48 | + it("Should verify an external signature", () => { |
| 49 | + const identity = new Identity(privateKey) |
109 | 50 |
|
110 |
| - const identityString = identity.toString() |
| 51 | + const signature = identity.signMessage("message") |
111 | 52 |
|
112 |
| - expect(typeof identityString).toBe("string") |
| 53 | + expect(Identity.verifySignature("message", signature, identity.publicKey)).toBeTruthy() |
| 54 | + expect(Identity.verifySignature("message", signature, BigInt(identity.publicKey))).toBeTruthy() |
113 | 55 | })
|
114 | 56 |
|
115 |
| - it("Should return a valid identity string", () => { |
116 |
| - const identity = new Identity("message") |
| 57 | + it("Should verify an external signature with an unpacked public key", () => { |
| 58 | + const identity = new Identity(privateKey) |
117 | 59 |
|
118 |
| - const [trapdoor, nullifier] = JSON.parse(identity.toString()) |
| 60 | + const signature = identity.signMessage("message") |
119 | 61 |
|
120 |
| - expect(BigNumber.from(trapdoor).toBigInt()).toBe(identity.trapdoor) |
121 |
| - expect(BigNumber.from(nullifier).toBigInt()).toBe(identity.nullifier) |
| 62 | + expect(Identity.verifySignature("message", signature, identity.unpackedPublicKey)).toBeTruthy() |
122 | 63 | })
|
123 | 64 | })
|
124 | 65 | })
|
0 commit comments