@waku/rln 0.1.1-5d7f77f → 0.1.1-77ba0a6
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 +26 -2
- package/bundle/assets/rln_wasm_bg-a503e304.wasm +0 -0
- package/bundle/index.js +68738 -22572
- package/dist/.tsbuildinfo +1 -1
- package/dist/byte_utils.d.ts +13 -0
- package/dist/byte_utils.js +22 -0
- package/dist/byte_utils.js.map +1 -1
- package/dist/codec.d.ts +5 -3
- package/dist/codec.js +8 -4
- package/dist/codec.js.map +1 -1
- package/dist/constants.d.ts +2 -2
- package/dist/constants.js +62 -9
- package/dist/constants.js.map +1 -1
- package/dist/create.d.ts +2 -0
- package/dist/create.js +8 -0
- package/dist/create.js.map +1 -0
- package/dist/index.d.ts +5 -3
- package/dist/index.js +5 -10
- package/dist/index.js.map +1 -1
- package/dist/keystore/cipher.d.ts +4 -0
- package/dist/keystore/cipher.js +28 -0
- package/dist/keystore/cipher.js.map +1 -0
- package/dist/keystore/credential_validation_generated.d.ts +8 -0
- package/dist/keystore/credential_validation_generated.js +121 -0
- package/dist/keystore/credential_validation_generated.js.map +1 -0
- package/dist/keystore/index.d.ts +4 -0
- package/dist/keystore/index.js +3 -0
- package/dist/keystore/index.js.map +1 -0
- package/dist/keystore/keystore.d.ts +50 -0
- package/dist/keystore/keystore.js +202 -0
- package/dist/keystore/keystore.js.map +1 -0
- package/dist/keystore/keystore_validation_generated.d.ts +8 -0
- package/dist/keystore/keystore_validation_generated.js +75 -0
- package/dist/keystore/keystore_validation_generated.js.map +1 -0
- package/dist/keystore/schema_validator.d.ts +2 -0
- package/dist/keystore/schema_validator.js +18 -0
- package/dist/keystore/schema_validator.js.map +1 -0
- package/dist/keystore/types.d.ts +29 -0
- package/dist/keystore/types.js +2 -0
- package/dist/keystore/types.js.map +1 -0
- package/dist/message.d.ts +1 -1
- package/dist/message.js +1 -1
- package/dist/metamask.d.ts +2 -0
- package/dist/metamask.js +11 -0
- package/dist/metamask.js.map +1 -0
- package/dist/rln.d.ts +47 -0
- package/dist/rln.js +110 -12
- package/dist/rln.js.map +1 -1
- package/dist/rln_contract.d.ts +24 -14
- package/dist/rln_contract.js +73 -33
- package/dist/rln_contract.js.map +1 -1
- package/package.json +23 -8
- package/src/byte_utils.ts +24 -0
- package/src/codec.ts +10 -5
- package/src/constants.ts +63 -9
- package/src/create.ts +9 -0
- package/src/index.ts +13 -11
- package/src/message.ts +1 -1
- package/src/metamask.ts +16 -0
- package/src/rln.ts +194 -13
- package/src/rln_contract.ts +127 -52
- package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
package/dist/rln_contract.js
CHANGED
@@ -1,28 +1,54 @@
|
|
1
|
+
import { hexToBytes } from "@waku/utils/bytes";
|
1
2
|
import { ethers } from "ethers";
|
2
|
-
import {
|
3
|
+
import { zeroPadLE } from "./byte_utils.js";
|
4
|
+
import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
|
3
5
|
import { MerkleRootTracker } from "./root_tracker.js";
|
4
6
|
export class RLNContract {
|
5
|
-
constructor(rlnInstance, { address, provider }) {
|
6
|
-
this._members = [];
|
7
|
-
const initialRoot = rlnInstance.getMerkleRoot();
|
8
|
-
this._contract = new ethers.Contract(address, RLN_ABI, provider);
|
9
|
-
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
10
|
-
this.membersFilter = this.contract.filters.MemberRegistered();
|
11
|
-
}
|
12
7
|
static async init(rlnInstance, options) {
|
13
8
|
const rlnContract = new RLNContract(rlnInstance, options);
|
9
|
+
await rlnContract.initStorageContract(options.signer);
|
14
10
|
await rlnContract.fetchMembers(rlnInstance);
|
15
11
|
rlnContract.subscribeToMembers(rlnInstance);
|
16
12
|
return rlnContract;
|
17
13
|
}
|
14
|
+
constructor(rlnInstance, { registryAddress, signer }) {
|
15
|
+
this._members = new Map();
|
16
|
+
const initialRoot = rlnInstance.getMerkleRoot();
|
17
|
+
this.registryContract = new ethers.Contract(registryAddress, RLN_REGISTRY_ABI, signer);
|
18
|
+
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
19
|
+
}
|
20
|
+
async initStorageContract(signer, options = {}) {
|
21
|
+
const storageIndex = options?.storageIndex
|
22
|
+
? options.storageIndex
|
23
|
+
: await this.registryContract.usingStorageIndex();
|
24
|
+
const storageAddress = await this.registryContract.storages(storageIndex);
|
25
|
+
if (!storageAddress || storageAddress === ethers.constants.AddressZero) {
|
26
|
+
throw Error("No RLN Storage initialized on registry contract.");
|
27
|
+
}
|
28
|
+
this.storageIndex = storageIndex;
|
29
|
+
this.storageContract = new ethers.Contract(storageAddress, RLN_STORAGE_ABI, signer);
|
30
|
+
this._membersFilter = this.storageContract.filters.MemberRegistered();
|
31
|
+
this.deployBlock = await this.storageContract.deployedBlockNumber();
|
32
|
+
}
|
18
33
|
get contract() {
|
19
|
-
|
34
|
+
if (!this.storageContract) {
|
35
|
+
throw Error("Storage contract was not initialized");
|
36
|
+
}
|
37
|
+
return this.storageContract;
|
20
38
|
}
|
21
39
|
get members() {
|
22
|
-
|
40
|
+
const sortedMembers = Array.from(this._members.values()).sort((left, right) => left.index.toNumber() - right.index.toNumber());
|
41
|
+
return sortedMembers;
|
42
|
+
}
|
43
|
+
get membersFilter() {
|
44
|
+
if (!this._membersFilter) {
|
45
|
+
throw Error("Members filter was not initialized.");
|
46
|
+
}
|
47
|
+
return this._membersFilter;
|
23
48
|
}
|
24
49
|
async fetchMembers(rlnInstance, options = {}) {
|
25
50
|
const registeredMemberEvents = await queryFilter(this.contract, {
|
51
|
+
fromBlock: this.deployBlock,
|
26
52
|
...options,
|
27
53
|
membersFilter: this.membersFilter,
|
28
54
|
});
|
@@ -39,11 +65,11 @@ export class RLNContract {
|
|
39
65
|
const index = evt.args.index;
|
40
66
|
const toRemoveVal = toRemoveTable.get(evt.blockNumber);
|
41
67
|
if (toRemoveVal != undefined) {
|
42
|
-
toRemoveVal.push(index);
|
68
|
+
toRemoveVal.push(index.toNumber());
|
43
69
|
toRemoveTable.set(evt.blockNumber, toRemoveVal);
|
44
70
|
}
|
45
71
|
else {
|
46
|
-
toRemoveTable.set(evt.blockNumber, [index]);
|
72
|
+
toRemoveTable.set(evt.blockNumber, [index.toNumber()]);
|
47
73
|
}
|
48
74
|
}
|
49
75
|
else {
|
@@ -54,21 +80,24 @@ export class RLNContract {
|
|
54
80
|
eventsPerBlock.push(evt);
|
55
81
|
toInsertTable.set(evt.blockNumber, eventsPerBlock);
|
56
82
|
}
|
57
|
-
this.removeMembers(rlnInstance, toRemoveTable);
|
58
|
-
this.insertMembers(rlnInstance, toInsertTable);
|
59
83
|
});
|
84
|
+
this.removeMembers(rlnInstance, toRemoveTable);
|
85
|
+
this.insertMembers(rlnInstance, toInsertTable);
|
60
86
|
}
|
61
87
|
insertMembers(rlnInstance, toInsert) {
|
62
88
|
toInsert.forEach((events, blockNumber) => {
|
63
89
|
events.forEach((evt) => {
|
64
|
-
|
90
|
+
const _idCommitment = evt?.args?.idCommitment;
|
91
|
+
const index = evt?.args?.index;
|
92
|
+
if (!_idCommitment || !index) {
|
65
93
|
return;
|
66
94
|
}
|
67
|
-
const
|
68
|
-
const index = evt.args.index;
|
69
|
-
const idCommitment = ethers.utils.zeroPad(ethers.utils.arrayify(pubkey), 32);
|
95
|
+
const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32);
|
70
96
|
rlnInstance.insertMember(idCommitment);
|
71
|
-
this.
|
97
|
+
this._members.set(index.toNumber(), {
|
98
|
+
index,
|
99
|
+
idCommitment: _idCommitment?._hex,
|
100
|
+
});
|
72
101
|
});
|
73
102
|
const currentRoot = rlnInstance.getMerkleRoot();
|
74
103
|
this.merkleRootTracker.pushRoot(blockNumber, currentRoot);
|
@@ -78,9 +107,8 @@ export class RLNContract {
|
|
78
107
|
const removeDescending = new Map([...toRemove].sort().reverse());
|
79
108
|
removeDescending.forEach((indexes, blockNumber) => {
|
80
109
|
indexes.forEach((index) => {
|
81
|
-
|
82
|
-
|
83
|
-
this.members.splice(idx, 1);
|
110
|
+
if (this._members.has(index)) {
|
111
|
+
this._members.delete(index);
|
84
112
|
}
|
85
113
|
rlnInstance.deleteMember(index);
|
86
114
|
});
|
@@ -88,19 +116,31 @@ export class RLNContract {
|
|
88
116
|
});
|
89
117
|
}
|
90
118
|
subscribeToMembers(rlnInstance) {
|
91
|
-
this.contract.on(this.membersFilter, (_pubkey, _index, event) => this.processEvents(rlnInstance, event));
|
119
|
+
this.contract.on(this.membersFilter, (_pubkey, _index, event) => this.processEvents(rlnInstance, [event]));
|
92
120
|
}
|
93
|
-
async
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
const depositValue = await this.contract.MEMBERSHIP_DEPOSIT();
|
99
|
-
const txRegisterResponse = await this.contract.register(credential.IDCommitmentBigInt, {
|
100
|
-
value: depositValue,
|
101
|
-
});
|
121
|
+
async registerWithIdentity(identity) {
|
122
|
+
if (this.storageIndex === undefined) {
|
123
|
+
throw Error("Cannot register credential, no storage contract index found.");
|
124
|
+
}
|
125
|
+
const txRegisterResponse = await this.registryContract["register(uint16,uint256)"](this.storageIndex, identity.IDCommitmentBigInt, { gasLimit: 100000 });
|
102
126
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
103
|
-
|
127
|
+
// assumption: register(uint16,uint256) emits one event
|
128
|
+
const memberRegistered = txRegisterReceipt?.events?.[0];
|
129
|
+
if (!memberRegistered) {
|
130
|
+
return undefined;
|
131
|
+
}
|
132
|
+
const decodedData = this.contract.interface.decodeEventLog("MemberRegistered", memberRegistered.data);
|
133
|
+
const network = await this.registryContract.provider.getNetwork();
|
134
|
+
const address = this.registryContract.address;
|
135
|
+
const membershipId = decodedData.index.toNumber();
|
136
|
+
return {
|
137
|
+
identity,
|
138
|
+
membership: {
|
139
|
+
address,
|
140
|
+
treeIndex: membershipId,
|
141
|
+
chainId: network.chainId,
|
142
|
+
},
|
143
|
+
};
|
104
144
|
}
|
105
145
|
roots() {
|
106
146
|
return this.merkleRootTracker.roots();
|
package/dist/rln_contract.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"rln_contract.js","sourceRoot":"","sources":["../src/rln_contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;
|
1
|
+
{"version":3,"file":"rln_contract.js","sourceRoot":"","sources":["../src/rln_contract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAC5C,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAGnE,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AA0BtD,MAAM,OAAO,WAAW;IAWf,MAAM,CAAC,KAAK,CAAC,IAAI,CACtB,WAAwB,EACxB,OAA+B;QAE/B,MAAM,WAAW,GAAG,IAAI,WAAW,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;QAE1D,MAAM,WAAW,CAAC,mBAAmB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;QACtD,MAAM,WAAW,CAAC,YAAY,CAAC,WAAW,CAAC,CAAC;QAC5C,WAAW,CAAC,kBAAkB,CAAC,WAAW,CAAC,CAAC;QAE5C,OAAO,WAAW,CAAC;IACrB,CAAC;IAED,YACE,WAAwB,EACxB,EAAE,eAAe,EAAE,MAAM,EAAsB;QAjBzC,aAAQ,GAAwB,IAAI,GAAG,EAAE,CAAC;QAmBhD,MAAM,WAAW,GAAG,WAAW,CAAC,aAAa,EAAE,CAAC;QAEhD,IAAI,CAAC,gBAAgB,GAAG,IAAI,MAAM,CAAC,QAAQ,CACzC,eAAe,EACf,gBAAgB,EAChB,MAAM,CACP,CAAC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CAAC;QAE/C,IAAI,GAAG,KAAK,CAAC;KACd;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,QAAQ,CAAC,CAAC,KAAK,CAAI,KAAU,EAAE,IAAY;IACzC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,IAAI,IAAI,GAAG,IAAI,CAAC;IAEhB,OAAO,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE;QAC1B,MAAM,OAAO,GAAG,KAAK,CAAC,KAAK,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAEzC,MAAM,OAAO,CAAC;QAEd,KAAK,GAAG,IAAI,CAAC;QACb,IAAI,IAAI,IAAI,CAAC;KACd;AACH,CAAC;AAED,SAAS,YAAY,CAAI,OAAmB,EAAE,YAAe;IAC3D,OAAO,OAAO,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,EAAE;QAC3B,OAAO,CAAC,KAAK,CAAC,mCAAmC,GAAG,EAAE,OAAO,EAAE,CAAC,CAAC;QACjE,OAAO,YAAY,CAAC;IACtB,CAAC,CAAC,CAAC;AACL,CAAC"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/rln",
|
3
|
-
"version": "0.1.1-
|
3
|
+
"version": "0.1.1-77ba0a6",
|
4
4
|
"description": "Rate Limit Nullifier for js-waku",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -24,6 +24,7 @@
|
|
24
24
|
"scripts": {
|
25
25
|
"prepare": "husky install",
|
26
26
|
"build": "run-s build:**",
|
27
|
+
"build:codegen": "node ./scripts/schema_validator_codegen.js",
|
27
28
|
"build:tsc": "tsc",
|
28
29
|
"build:bundle": "rollup --config rollup.config.js",
|
29
30
|
"size": "npm run build && size-limit",
|
@@ -45,7 +46,7 @@
|
|
45
46
|
"crypto": false
|
46
47
|
},
|
47
48
|
"engines": {
|
48
|
-
"node": ">=
|
49
|
+
"node": ">=18"
|
49
50
|
},
|
50
51
|
"publishConfig": {
|
51
52
|
"access": "public",
|
@@ -59,19 +60,31 @@
|
|
59
60
|
"@size-limit/preset-big-lib": "^8.0.0",
|
60
61
|
"@types/app-root-path": "^1.2.4",
|
61
62
|
"@types/chai": "^4.2.15",
|
63
|
+
"@types/chai-as-promised": "^7.1.6",
|
62
64
|
"@types/chai-spies": "^1.0.3",
|
65
|
+
"@types/chai-subset": "^1.3.3",
|
63
66
|
"@types/debug": "^4.1.7",
|
67
|
+
"@types/deep-equal-in-any-order": "^1.0.1",
|
68
|
+
"@types/lodash": "^4.14.199",
|
64
69
|
"@types/mocha": "^9.1.0",
|
65
70
|
"@types/node": "^17.0.6",
|
66
71
|
"@types/tail": "^2.0.0",
|
67
72
|
"@types/uuid": "^8.3.0",
|
68
73
|
"@typescript-eslint/eslint-plugin": "^5.8.1",
|
69
74
|
"@typescript-eslint/parser": "^5.8.1",
|
75
|
+
"@waku/core": "^0.0.25",
|
76
|
+
"@waku/interfaces": "^0.0.20",
|
77
|
+
"@waku/message-encryption": "^0.0.23",
|
70
78
|
"@web/rollup-plugin-import-meta-assets": "^1.0.7",
|
79
|
+
"ajv": "^8.12.0",
|
80
|
+
"ajv-formats": "^2.1.1",
|
71
81
|
"app-root-path": "^3.0.0",
|
72
82
|
"chai": "^4.3.4",
|
83
|
+
"chai-as-promised": "^7.1.1",
|
73
84
|
"chai-spies": "^1.0.0",
|
85
|
+
"chai-subset": "^1.6.0",
|
74
86
|
"cspell": "^5.14.0",
|
87
|
+
"deep-equal-in-any-order": "^2.0.6",
|
75
88
|
"eslint": "^8.6.0",
|
76
89
|
"eslint-config-prettier": "^8.3.0",
|
77
90
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
@@ -83,9 +96,6 @@
|
|
83
96
|
"husky": "^7.0.4",
|
84
97
|
"ignore-loader": "^0.1.2",
|
85
98
|
"isomorphic-fetch": "^3.0.0",
|
86
|
-
"@waku/interfaces": "^0.0.13",
|
87
|
-
"@waku/message-encryption": "^0.0.16",
|
88
|
-
"@waku/core": "^0.0.18",
|
89
99
|
"jsdom": "^19.0.0",
|
90
100
|
"jsdom-global": "^3.0.2",
|
91
101
|
"karma": "^6.3.12",
|
@@ -129,8 +139,13 @@
|
|
129
139
|
]
|
130
140
|
},
|
131
141
|
"dependencies": {
|
132
|
-
"@
|
133
|
-
"@waku/
|
134
|
-
"
|
142
|
+
"@chainsafe/bls-keystore": "^3.0.0",
|
143
|
+
"@waku/utils": "^0.0.13",
|
144
|
+
"@waku/zerokit-rln-wasm": "^0.0.13",
|
145
|
+
"ethereum-cryptography": "^2.1.2",
|
146
|
+
"ethers": "^5.7.2",
|
147
|
+
"lodash": "^4.17.21",
|
148
|
+
"rlnjs": "^3.2.3",
|
149
|
+
"uuid": "^9.0.1"
|
135
150
|
}
|
136
151
|
}
|
package/src/byte_utils.ts
CHANGED
@@ -37,3 +37,27 @@ export function writeUIntLE(
|
|
37
37
|
|
38
38
|
return buf;
|
39
39
|
}
|
40
|
+
|
41
|
+
/**
|
42
|
+
* Transforms Uint8Array into BigInt
|
43
|
+
* @param array: Uint8Array
|
44
|
+
* @returns BigInt
|
45
|
+
*/
|
46
|
+
export function buildBigIntFromUint8Array(array: Uint8Array): bigint {
|
47
|
+
const dataView = new DataView(array.buffer);
|
48
|
+
return dataView.getBigUint64(0, true);
|
49
|
+
}
|
50
|
+
|
51
|
+
/**
|
52
|
+
* Fills with zeros to set length
|
53
|
+
* @param array little endian Uint8Array
|
54
|
+
* @param length amount to pad
|
55
|
+
* @returns little endian Uint8Array padded with zeros to set length
|
56
|
+
*/
|
57
|
+
export function zeroPadLE(array: Uint8Array, length: number): Uint8Array {
|
58
|
+
const result = new Uint8Array(length);
|
59
|
+
for (let i = 0; i < length; i++) {
|
60
|
+
result[i] = array[i] || 0;
|
61
|
+
}
|
62
|
+
return result;
|
63
|
+
}
|
package/src/codec.ts
CHANGED
@@ -44,18 +44,19 @@ export class RLNEncoder implements IEncoder {
|
|
44
44
|
|
45
45
|
private async generateProof(message: IMessage): Promise<IRateLimitProof> {
|
46
46
|
const signal = toRLNSignal(this.contentTopic, message);
|
47
|
-
|
48
|
-
console.time("proof_gen_timer");
|
49
47
|
const proof = await this.rlnInstance.generateRLNProof(
|
50
48
|
signal,
|
51
49
|
this.index,
|
52
50
|
message.timestamp,
|
53
51
|
this.idSecretHash
|
54
52
|
);
|
55
|
-
console.timeEnd("proof_gen_timer");
|
56
53
|
return proof;
|
57
54
|
}
|
58
55
|
|
56
|
+
get pubsubTopic(): string {
|
57
|
+
return this.encoder.pubsubTopic;
|
58
|
+
}
|
59
|
+
|
59
60
|
get contentTopic(): string {
|
60
61
|
return this.encoder.contentTopic;
|
61
62
|
}
|
@@ -86,6 +87,10 @@ export class RLNDecoder<T extends IDecodedMessage>
|
|
86
87
|
{
|
87
88
|
constructor(private rlnInstance: RLNInstance, private decoder: IDecoder<T>) {}
|
88
89
|
|
90
|
+
get pubsubTopic(): string {
|
91
|
+
return this.decoder.pubsubTopic;
|
92
|
+
}
|
93
|
+
|
89
94
|
get contentTopic(): string {
|
90
95
|
return this.decoder.contentTopic;
|
91
96
|
}
|
@@ -97,11 +102,11 @@ export class RLNDecoder<T extends IDecodedMessage>
|
|
97
102
|
}
|
98
103
|
|
99
104
|
async fromProtoObj(
|
100
|
-
|
105
|
+
pubsubTopic: string,
|
101
106
|
proto: IProtoMessage
|
102
107
|
): Promise<RlnMessage<T> | undefined> {
|
103
108
|
const msg: T | undefined = await this.decoder.fromProtoObj(
|
104
|
-
|
109
|
+
pubsubTopic,
|
105
110
|
proto
|
106
111
|
);
|
107
112
|
if (!msg) return;
|
package/src/constants.ts
CHANGED
@@ -1,14 +1,68 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
"
|
4
|
-
"
|
5
|
-
"
|
6
|
-
"
|
1
|
+
// ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnRegistry_Implementation.json#L3
|
2
|
+
export const RLN_REGISTRY_ABI = [
|
3
|
+
"error IncompatibleStorage()",
|
4
|
+
"error IncompatibleStorageIndex()",
|
5
|
+
"error NoStorageContractAvailable()",
|
6
|
+
"error StorageAlreadyExists(address storageAddress)",
|
7
|
+
"event AdminChanged(address previousAdmin, address newAdmin)",
|
8
|
+
"event BeaconUpgraded(address indexed beacon)",
|
9
|
+
"event Initialized(uint8 version)",
|
10
|
+
"event NewStorageContract(uint16 index, address storageAddress)",
|
11
|
+
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
|
12
|
+
"event Upgraded(address indexed implementation)",
|
13
|
+
"function forceProgress()",
|
14
|
+
"function initialize(address _poseidonHasher)",
|
15
|
+
"function newStorage()",
|
16
|
+
"function nextStorageIndex() view returns (uint16)",
|
17
|
+
"function owner() view returns (address)",
|
18
|
+
"function poseidonHasher() view returns (address)",
|
19
|
+
"function proxiableUUID() view returns (bytes32)",
|
20
|
+
"function register(uint16 storageIndex, uint256 commitment)",
|
21
|
+
"function register(uint256[] commitments)",
|
22
|
+
"function register(uint16 storageIndex, uint256[] commitments)",
|
23
|
+
"function registerStorage(address storageAddress)",
|
24
|
+
"function renounceOwnership()",
|
25
|
+
"function storages(uint16) view returns (address)",
|
26
|
+
"function transferOwnership(address newOwner)",
|
27
|
+
"function upgradeTo(address newImplementation)",
|
28
|
+
"function upgradeToAndCall(address newImplementation, bytes data) payable",
|
29
|
+
"function usingStorageIndex() view returns (uint16)",
|
30
|
+
];
|
31
|
+
|
32
|
+
// ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnStorage_0.json#L3
|
33
|
+
export const RLN_STORAGE_ABI = [
|
34
|
+
"constructor(address _poseidonHasher, uint16 _contractIndex)",
|
35
|
+
"error DuplicateIdCommitment()",
|
36
|
+
"error FullTree()",
|
37
|
+
"error InvalidIdCommitment(uint256 idCommitment)",
|
38
|
+
"error NotImplemented()",
|
39
|
+
"event MemberRegistered(uint256 idCommitment, uint256 index)",
|
40
|
+
"event MemberWithdrawn(uint256 idCommitment, uint256 index)",
|
41
|
+
"event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
|
42
|
+
"function DEPTH() view returns (uint256)",
|
43
|
+
"function MEMBERSHIP_DEPOSIT() view returns (uint256)",
|
44
|
+
"function SET_SIZE() view returns (uint256)",
|
45
|
+
"function contractIndex() view returns (uint16)",
|
46
|
+
"function deployedBlockNumber() view returns (uint32)",
|
47
|
+
"function idCommitmentIndex() view returns (uint256)",
|
48
|
+
"function isValidCommitment(uint256 idCommitment) view returns (bool)",
|
49
|
+
"function memberExists(uint256) view returns (bool)",
|
50
|
+
"function members(uint256) view returns (uint256)",
|
51
|
+
"function owner() view returns (address)",
|
52
|
+
"function poseidonHasher() view returns (address)",
|
53
|
+
"function register(uint256[] idCommitments)",
|
54
|
+
"function register(uint256 idCommitment) payable",
|
55
|
+
"function renounceOwnership()",
|
56
|
+
"function slash(uint256 idCommitment, address receiver, uint256[8] proof) pure",
|
57
|
+
"function stakedAmounts(uint256) view returns (uint256)",
|
58
|
+
"function transferOwnership(address newOwner)",
|
59
|
+
"function verifier() view returns (address)",
|
60
|
+
"function withdraw() pure",
|
61
|
+
"function withdrawalBalance(address) view returns (uint256)",
|
7
62
|
];
|
8
63
|
|
9
64
|
export const SEPOLIA_CONTRACT = {
|
10
65
|
chainId: 11155111,
|
11
|
-
|
12
|
-
|
13
|
-
abi: RLN_ABI,
|
66
|
+
address: "0xF471d71E9b1455bBF4b85d475afb9BB0954A29c4",
|
67
|
+
abi: RLN_REGISTRY_ABI,
|
14
68
|
};
|
package/src/create.ts
ADDED
@@ -0,0 +1,9 @@
|
|
1
|
+
import type { RLNInstance } from "./rln.js";
|
2
|
+
|
3
|
+
export async function createRLN(): Promise<RLNInstance> {
|
4
|
+
// A dependency graph that contains any wasm must all be imported
|
5
|
+
// asynchronously. This file does the single async import, so
|
6
|
+
// that no one else needs to worry about it again.
|
7
|
+
const rlnModule = await import("./rln.js");
|
8
|
+
return rlnModule.create();
|
9
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,5 +1,12 @@
|
|
1
1
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
2
|
-
import {
|
2
|
+
import {
|
3
|
+
RLN_REGISTRY_ABI,
|
4
|
+
RLN_STORAGE_ABI,
|
5
|
+
SEPOLIA_CONTRACT,
|
6
|
+
} from "./constants.js";
|
7
|
+
import { createRLN } from "./create.js";
|
8
|
+
import { Keystore } from "./keystore/index.js";
|
9
|
+
import { extractMetaMaskSigner } from "./metamask.js";
|
3
10
|
import {
|
4
11
|
IdentityCredential,
|
5
12
|
Proof,
|
@@ -9,16 +16,9 @@ import {
|
|
9
16
|
import { RLNContract } from "./rln_contract.js";
|
10
17
|
import { MerkleRootTracker } from "./root_tracker.js";
|
11
18
|
|
12
|
-
// reexport the create function, dynamically imported from rln.ts
|
13
|
-
export async function create(): Promise<RLNInstance> {
|
14
|
-
// A dependency graph that contains any wasm must all be imported
|
15
|
-
// asynchronously. This file does the single async import, so
|
16
|
-
// that no one else needs to worry about it again.
|
17
|
-
const rlnModule = await import("./rln.js");
|
18
|
-
return await rlnModule.create();
|
19
|
-
}
|
20
|
-
|
21
19
|
export {
|
20
|
+
createRLN,
|
21
|
+
Keystore,
|
22
22
|
RLNInstance,
|
23
23
|
IdentityCredential,
|
24
24
|
Proof,
|
@@ -27,6 +27,8 @@ export {
|
|
27
27
|
RLNDecoder,
|
28
28
|
MerkleRootTracker,
|
29
29
|
RLNContract,
|
30
|
-
|
30
|
+
RLN_STORAGE_ABI,
|
31
|
+
RLN_REGISTRY_ABI,
|
31
32
|
SEPOLIA_CONTRACT,
|
33
|
+
extractMetaMaskSigner,
|
32
34
|
};
|
package/src/message.ts
CHANGED
@@ -14,7 +14,7 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
|
14
14
|
}
|
15
15
|
|
16
16
|
export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
|
17
|
-
public
|
17
|
+
public pubsubTopic = "";
|
18
18
|
|
19
19
|
constructor(
|
20
20
|
public rlnInstance: RLNInstance,
|
package/src/metamask.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
import { ethers } from "ethers";
|
2
|
+
|
3
|
+
export const extractMetaMaskSigner = async (): Promise<ethers.Signer> => {
|
4
|
+
const ethereum = (window as any).ethereum;
|
5
|
+
|
6
|
+
if (!ethereum) {
|
7
|
+
throw Error(
|
8
|
+
"Missing or invalid Ethereum provider. Please install a Web3 wallet such as MetaMask."
|
9
|
+
);
|
10
|
+
}
|
11
|
+
|
12
|
+
await ethereum.request({ method: "eth_requestAccounts" });
|
13
|
+
const provider = new ethers.providers.Web3Provider(ethereum, "any");
|
14
|
+
|
15
|
+
return provider.getSigner();
|
16
|
+
};
|