@semaphore-protocol/identity 0.2.0 → 0.2.1

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.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 0.2.0
3
+ * @version 0.2.1
4
4
  * @file A library for managing Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -40,14 +40,13 @@ function genRandomNumber(numberOfBytes) {
40
40
  * @param jsonString The JSON string.
41
41
  * @returns True or false.
42
42
  */
43
- function isJson(jsonString) {
43
+ function isJsonArray(jsonString) {
44
44
  try {
45
- JSON.parse(jsonString);
45
+ return Array.isArray(JSON.parse(jsonString));
46
46
  }
47
47
  catch (error) {
48
48
  return false;
49
49
  }
50
- return true;
51
50
  }
52
51
 
53
52
  var Identity = /** @class */ (function () {
@@ -62,7 +61,7 @@ var Identity = /** @class */ (function () {
62
61
  return;
63
62
  }
64
63
  checkParameter(identityOrMessage, "identityOrMessage", "string");
65
- if (!isJson(identityOrMessage)) {
64
+ if (!isJsonArray(identityOrMessage)) {
66
65
  var messageHash = sha256(identityOrMessage);
67
66
  this._trapdoor = BigNumber.from("0x".concat(sha256("".concat(messageHash, "identity_trapdoor")))).toBigInt();
68
67
  this._nullifier = BigNumber.from("0x".concat(sha256("".concat(messageHash, "identity_nullifier")))).toBigInt();
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @module @semaphore-protocol/identity
3
- * @version 0.2.0
3
+ * @version 0.2.1
4
4
  * @file A library for managing Semaphore identities.
5
5
  * @copyright Ethereum Foundation 2022
6
6
  * @license MIT
@@ -48,14 +48,13 @@ function genRandomNumber(numberOfBytes) {
48
48
  * @param jsonString The JSON string.
49
49
  * @returns True or false.
50
50
  */
51
- function isJson(jsonString) {
51
+ function isJsonArray(jsonString) {
52
52
  try {
53
- JSON.parse(jsonString);
53
+ return Array.isArray(JSON.parse(jsonString));
54
54
  }
55
55
  catch (error) {
56
56
  return false;
57
57
  }
58
- return true;
59
58
  }
60
59
 
61
60
  var Identity = /** @class */ (function () {
@@ -70,7 +69,7 @@ var Identity = /** @class */ (function () {
70
69
  return;
71
70
  }
72
71
  checkParameter(identityOrMessage, "identityOrMessage", "string");
73
- if (!isJson(identityOrMessage)) {
72
+ if (!isJsonArray(identityOrMessage)) {
74
73
  var messageHash = sha256(identityOrMessage);
75
74
  this._trapdoor = bignumber.BigNumber.from("0x".concat(sha256("".concat(messageHash, "identity_trapdoor")))).toBigInt();
76
75
  this._nullifier = bignumber.BigNumber.from("0x".concat(sha256("".concat(messageHash, "identity_nullifier")))).toBigInt();
@@ -15,4 +15,4 @@ export declare function genRandomNumber(numberOfBytes?: number): bigint;
15
15
  * @param jsonString The JSON string.
16
16
  * @returns True or false.
17
17
  */
18
- export declare function isJson(jsonString: string): boolean;
18
+ export declare function isJsonArray(jsonString: string): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@semaphore-protocol/identity",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "A library for managing Semaphore identities.",
5
5
  "license": "MIT",
6
6
  "main": "dist/index.node.js",
@@ -29,6 +29,18 @@ describe("Identity", () => {
29
29
  expect(identity1.getNullifier()).toBe(identity2.getNullifier())
30
30
  })
31
31
 
32
+ it("Should create deterministic identities from number/boolean messages", () => {
33
+ const identity1 = new Identity("true")
34
+ const identity2 = new Identity("true")
35
+ const identity3 = new Identity("7")
36
+ const identity4 = new Identity("7")
37
+
38
+ expect(identity1.getTrapdoor()).toBe(identity2.getTrapdoor())
39
+ expect(identity1.getNullifier()).toBe(identity2.getNullifier())
40
+ expect(identity3.getTrapdoor()).toBe(identity4.getTrapdoor())
41
+ expect(identity3.getNullifier()).toBe(identity4.getNullifier())
42
+ })
43
+
32
44
  it("Should not recreate an existing invalid identity", () => {
33
45
  const fun = () => new Identity('[true, "01323"]')
34
46
 
package/src/identity.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { BigNumber } from "@ethersproject/bignumber"
2
2
  import { poseidon } from "circomlibjs"
3
3
  import checkParameter from "./checkParameter"
4
- import { genRandomNumber, isJson, sha256 } from "./utils"
4
+ import { genRandomNumber, isJsonArray, sha256 } from "./utils"
5
5
 
6
6
  export default class Identity {
7
7
  private _trapdoor: bigint
@@ -21,7 +21,7 @@ export default class Identity {
21
21
 
22
22
  checkParameter(identityOrMessage, "identityOrMessage", "string")
23
23
 
24
- if (!isJson(identityOrMessage)) {
24
+ if (!isJsonArray(identityOrMessage)) {
25
25
  const messageHash = sha256(identityOrMessage)
26
26
 
27
27
  this._trapdoor = BigNumber.from(`0x${sha256(`${messageHash}identity_trapdoor`)}`).toBigInt()
package/src/utils.ts CHANGED
@@ -27,12 +27,10 @@ export function genRandomNumber(numberOfBytes = 31): bigint {
27
27
  * @param jsonString The JSON string.
28
28
  * @returns True or false.
29
29
  */
30
- export function isJson(jsonString: string) {
30
+ export function isJsonArray(jsonString: string) {
31
31
  try {
32
- JSON.parse(jsonString)
32
+ return Array.isArray(JSON.parse(jsonString))
33
33
  } catch (error) {
34
34
  return false
35
35
  }
36
-
37
- return true
38
36
  }