@semaphore-protocol/identity 0.2.0 → 2.0.0
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/README.md +3 -2
- package/dist/index.mjs +12 -12
- package/dist/index.node.js +12 -16
- package/dist/types/utils.d.ts +1 -1
- package/package.json +5 -5
- package/src/identity.test.ts +12 -0
- package/src/identity.ts +5 -5
- package/src/utils.ts +6 -7
package/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
<h1 align="center">
|
|
3
3
|
Semaphore identity
|
|
4
4
|
</h1>
|
|
5
|
-
<p align="center">A library
|
|
5
|
+
<p align="center">A library to create Semaphore identities.</p>
|
|
6
6
|
</p>
|
|
7
7
|
|
|
8
8
|
<p align="center">
|
|
@@ -38,7 +38,8 @@
|
|
|
38
38
|
</h4>
|
|
39
39
|
</div>
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
| This library provides a class that can be used to create identities compatible with the Semaphore [circuits](https://github.com/semaphore-protocol/semaphore/tree/main/circuits). Each identity contains two private values (_trapdoor_ and _nullifier_), and the Poseidon hash of these values (_commitment_) is used as the public identifier of the Semaphore identity. |
|
|
42
|
+
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
42
43
|
|
|
43
44
|
## 🛠 Install
|
|
44
45
|
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version
|
|
4
|
-
* @file A library
|
|
3
|
+
* @version 2.0.0
|
|
4
|
+
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
7
7
|
* @see [Github]{@link https://github.com/semaphore-protocol/semaphore.js/tree/main/packages/proof}
|
|
@@ -9,7 +9,8 @@
|
|
|
9
9
|
import { BigNumber } from '@ethersproject/bignumber';
|
|
10
10
|
import { poseidon } from 'circomlibjs';
|
|
11
11
|
import { randomBytes } from '@ethersproject/random';
|
|
12
|
-
import
|
|
12
|
+
import { sha256 as sha256$1 } from '@ethersproject/sha2';
|
|
13
|
+
import { toUtf8Bytes } from '@ethersproject/strings';
|
|
13
14
|
|
|
14
15
|
function checkParameter(value, name, type) {
|
|
15
16
|
if (typeof value !== type) {
|
|
@@ -23,8 +24,8 @@ function checkParameter(value, name, type) {
|
|
|
23
24
|
* @returns The hexadecimal hash of the message.
|
|
24
25
|
*/
|
|
25
26
|
function sha256(message) {
|
|
26
|
-
var hash =
|
|
27
|
-
return hash
|
|
27
|
+
var hash = sha256$1(toUtf8Bytes(message));
|
|
28
|
+
return hash;
|
|
28
29
|
}
|
|
29
30
|
/**
|
|
30
31
|
* Generates a random big number.
|
|
@@ -40,14 +41,13 @@ function genRandomNumber(numberOfBytes) {
|
|
|
40
41
|
* @param jsonString The JSON string.
|
|
41
42
|
* @returns True or false.
|
|
42
43
|
*/
|
|
43
|
-
function
|
|
44
|
+
function isJsonArray(jsonString) {
|
|
44
45
|
try {
|
|
45
|
-
JSON.parse(jsonString);
|
|
46
|
+
return Array.isArray(JSON.parse(jsonString));
|
|
46
47
|
}
|
|
47
48
|
catch (error) {
|
|
48
49
|
return false;
|
|
49
50
|
}
|
|
50
|
-
return true;
|
|
51
51
|
}
|
|
52
52
|
|
|
53
53
|
var Identity = /** @class */ (function () {
|
|
@@ -62,10 +62,10 @@ var Identity = /** @class */ (function () {
|
|
|
62
62
|
return;
|
|
63
63
|
}
|
|
64
64
|
checkParameter(identityOrMessage, "identityOrMessage", "string");
|
|
65
|
-
if (!
|
|
66
|
-
var messageHash = sha256(identityOrMessage);
|
|
67
|
-
this._trapdoor = BigNumber.from(
|
|
68
|
-
this._nullifier = BigNumber.from(
|
|
65
|
+
if (!isJsonArray(identityOrMessage)) {
|
|
66
|
+
var messageHash = sha256(identityOrMessage).slice(2);
|
|
67
|
+
this._trapdoor = BigNumber.from(sha256("".concat(messageHash, "identity_trapdoor"))).toBigInt();
|
|
68
|
+
this._nullifier = BigNumber.from(sha256("".concat(messageHash, "identity_nullifier"))).toBigInt();
|
|
69
69
|
return;
|
|
70
70
|
}
|
|
71
71
|
var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
|
package/dist/index.node.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @module @semaphore-protocol/identity
|
|
3
|
-
* @version
|
|
4
|
-
* @file A library
|
|
3
|
+
* @version 2.0.0
|
|
4
|
+
* @file A library to create Semaphore identities.
|
|
5
5
|
* @copyright Ethereum Foundation 2022
|
|
6
6
|
* @license MIT
|
|
7
7
|
* @see [Github]{@link https://github.com/semaphore-protocol/semaphore.js/tree/main/packages/proof}
|
|
@@ -13,11 +13,8 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
13
13
|
var bignumber = require('@ethersproject/bignumber');
|
|
14
14
|
var circomlibjs = require('circomlibjs');
|
|
15
15
|
var random = require('@ethersproject/random');
|
|
16
|
-
var
|
|
17
|
-
|
|
18
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
19
|
-
|
|
20
|
-
var _sha256__default = /*#__PURE__*/_interopDefaultLegacy(_sha256);
|
|
16
|
+
var sha2 = require('@ethersproject/sha2');
|
|
17
|
+
var strings = require('@ethersproject/strings');
|
|
21
18
|
|
|
22
19
|
function checkParameter(value, name, type) {
|
|
23
20
|
if (typeof value !== type) {
|
|
@@ -31,8 +28,8 @@ function checkParameter(value, name, type) {
|
|
|
31
28
|
* @returns The hexadecimal hash of the message.
|
|
32
29
|
*/
|
|
33
30
|
function sha256(message) {
|
|
34
|
-
var hash =
|
|
35
|
-
return hash
|
|
31
|
+
var hash = sha2.sha256(strings.toUtf8Bytes(message));
|
|
32
|
+
return hash;
|
|
36
33
|
}
|
|
37
34
|
/**
|
|
38
35
|
* Generates a random big number.
|
|
@@ -48,14 +45,13 @@ function genRandomNumber(numberOfBytes) {
|
|
|
48
45
|
* @param jsonString The JSON string.
|
|
49
46
|
* @returns True or false.
|
|
50
47
|
*/
|
|
51
|
-
function
|
|
48
|
+
function isJsonArray(jsonString) {
|
|
52
49
|
try {
|
|
53
|
-
JSON.parse(jsonString);
|
|
50
|
+
return Array.isArray(JSON.parse(jsonString));
|
|
54
51
|
}
|
|
55
52
|
catch (error) {
|
|
56
53
|
return false;
|
|
57
54
|
}
|
|
58
|
-
return true;
|
|
59
55
|
}
|
|
60
56
|
|
|
61
57
|
var Identity = /** @class */ (function () {
|
|
@@ -70,10 +66,10 @@ var Identity = /** @class */ (function () {
|
|
|
70
66
|
return;
|
|
71
67
|
}
|
|
72
68
|
checkParameter(identityOrMessage, "identityOrMessage", "string");
|
|
73
|
-
if (!
|
|
74
|
-
var messageHash = sha256(identityOrMessage);
|
|
75
|
-
this._trapdoor = bignumber.BigNumber.from(
|
|
76
|
-
this._nullifier = bignumber.BigNumber.from(
|
|
69
|
+
if (!isJsonArray(identityOrMessage)) {
|
|
70
|
+
var messageHash = sha256(identityOrMessage).slice(2);
|
|
71
|
+
this._trapdoor = bignumber.BigNumber.from(sha256("".concat(messageHash, "identity_trapdoor"))).toBigInt();
|
|
72
|
+
this._nullifier = bignumber.BigNumber.from(sha256("".concat(messageHash, "identity_nullifier"))).toBigInt();
|
|
77
73
|
return;
|
|
78
74
|
}
|
|
79
75
|
var _a = JSON.parse(identityOrMessage), trapdoor = _a[0], nullifier = _a[1];
|
package/dist/types/utils.d.ts
CHANGED
|
@@ -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
|
|
18
|
+
export declare function isJsonArray(jsonString: string): boolean;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@semaphore-protocol/identity",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "A library
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "A library to create Semaphore identities.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "dist/index.node.js",
|
|
7
7
|
"exports": {
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"access": "public"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@types/crypto-js": "^4.1.1",
|
|
31
30
|
"rollup-plugin-cleanup": "^3.2.1",
|
|
32
31
|
"rollup-plugin-typescript2": "^0.31.2",
|
|
33
32
|
"typedoc": "^0.22.11"
|
|
@@ -35,7 +34,8 @@
|
|
|
35
34
|
"dependencies": {
|
|
36
35
|
"@ethersproject/bignumber": "^5.5.0",
|
|
37
36
|
"@ethersproject/random": "^5.5.1",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
37
|
+
"@ethersproject/sha2": "^5.6.1",
|
|
38
|
+
"@ethersproject/strings": "^5.6.1",
|
|
39
|
+
"circomlibjs": "0.0.8"
|
|
40
40
|
}
|
|
41
41
|
}
|
package/src/identity.test.ts
CHANGED
|
@@ -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,
|
|
4
|
+
import { genRandomNumber, isJsonArray, sha256 } from "./utils"
|
|
5
5
|
|
|
6
6
|
export default class Identity {
|
|
7
7
|
private _trapdoor: bigint
|
|
@@ -21,11 +21,11 @@ export default class Identity {
|
|
|
21
21
|
|
|
22
22
|
checkParameter(identityOrMessage, "identityOrMessage", "string")
|
|
23
23
|
|
|
24
|
-
if (!
|
|
25
|
-
const messageHash = sha256(identityOrMessage)
|
|
24
|
+
if (!isJsonArray(identityOrMessage)) {
|
|
25
|
+
const messageHash = sha256(identityOrMessage).slice(2)
|
|
26
26
|
|
|
27
|
-
this._trapdoor = BigNumber.from(
|
|
28
|
-
this._nullifier = BigNumber.from(
|
|
27
|
+
this._trapdoor = BigNumber.from(sha256(`${messageHash}identity_trapdoor`)).toBigInt()
|
|
28
|
+
this._nullifier = BigNumber.from(sha256(`${messageHash}identity_nullifier`)).toBigInt()
|
|
29
29
|
|
|
30
30
|
return
|
|
31
31
|
}
|
package/src/utils.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BigNumber } from "@ethersproject/bignumber"
|
|
2
2
|
import { randomBytes } from "@ethersproject/random"
|
|
3
|
-
import _sha256 from "
|
|
3
|
+
import { sha256 as _sha256 } from "@ethersproject/sha2"
|
|
4
|
+
import { toUtf8Bytes } from "@ethersproject/strings"
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
7
|
* Returns an hexadecimal sha256 hash of the message passed as parameter.
|
|
@@ -8,9 +9,9 @@ import _sha256 from "crypto-js/sha256"
|
|
|
8
9
|
* @returns The hexadecimal hash of the message.
|
|
9
10
|
*/
|
|
10
11
|
export function sha256(message: string): string {
|
|
11
|
-
const hash = _sha256(message)
|
|
12
|
+
const hash = _sha256(toUtf8Bytes(message))
|
|
12
13
|
|
|
13
|
-
return hash
|
|
14
|
+
return hash
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
/**
|
|
@@ -27,12 +28,10 @@ export function genRandomNumber(numberOfBytes = 31): bigint {
|
|
|
27
28
|
* @param jsonString The JSON string.
|
|
28
29
|
* @returns True or false.
|
|
29
30
|
*/
|
|
30
|
-
export function
|
|
31
|
+
export function isJsonArray(jsonString: string) {
|
|
31
32
|
try {
|
|
32
|
-
JSON.parse(jsonString)
|
|
33
|
+
return Array.isArray(JSON.parse(jsonString))
|
|
33
34
|
} catch (error) {
|
|
34
35
|
return false
|
|
35
36
|
}
|
|
36
|
-
|
|
37
|
-
return true
|
|
38
37
|
}
|