@waku/rln 0.1.1-fa49e29 → 0.1.2-86d4f56
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 +28541 -4914
- package/dist/.tsbuildinfo +1 -1
- package/dist/byte_utils.d.ts +7 -0
- package/dist/byte_utils.js +13 -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/create.d.ts +2 -0
- package/dist/create.js +8 -0
- package/dist/create.js.map +1 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +3 -9
- package/dist/index.js.map +1 -1
- package/dist/keystore/index.d.ts +2 -0
- package/dist/keystore/index.js.map +1 -1
- package/dist/keystore/keystore.d.ts +11 -11
- package/dist/keystore/keystore.js +12 -3
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +25 -5
- 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 +51 -0
- package/dist/rln.js +132 -2
- package/dist/rln.js.map +1 -1
- package/dist/rln_contract.d.ts +12 -11
- package/dist/rln_contract.js +32 -21
- package/dist/rln_contract.js.map +1 -1
- package/package.json +6 -6
- package/src/byte_utils.ts +14 -0
- package/src/codec.ts +10 -5
- package/src/create.ts +9 -0
- package/src/index.ts +4 -9
- package/src/message.ts +1 -1
- package/src/metamask.ts +16 -0
- package/src/rln.ts +237 -2
- package/src/rln_contract.ts +37 -33
- package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
package/dist/rln_contract.js
CHANGED
@@ -1,21 +1,23 @@
|
|
1
|
+
import { hexToBytes } from "@waku/utils/bytes";
|
1
2
|
import { ethers } from "ethers";
|
3
|
+
import { zeroPadLE } from "./byte_utils.js";
|
2
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, { registryAddress, provider }) {
|
6
|
-
this._members = new Map();
|
7
|
-
const initialRoot = rlnInstance.getMerkleRoot();
|
8
|
-
this.registryContract = new ethers.Contract(registryAddress, RLN_REGISTRY_ABI, provider);
|
9
|
-
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
10
|
-
}
|
11
7
|
static async init(rlnInstance, options) {
|
12
8
|
const rlnContract = new RLNContract(rlnInstance, options);
|
13
|
-
await rlnContract.initStorageContract(options.
|
9
|
+
await rlnContract.initStorageContract(options.signer);
|
14
10
|
await rlnContract.fetchMembers(rlnInstance);
|
15
11
|
rlnContract.subscribeToMembers(rlnInstance);
|
16
12
|
return rlnContract;
|
17
13
|
}
|
18
|
-
|
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 = {}) {
|
19
21
|
const storageIndex = options?.storageIndex
|
20
22
|
? options.storageIndex
|
21
23
|
: await this.registryContract.usingStorageIndex();
|
@@ -24,10 +26,16 @@ export class RLNContract {
|
|
24
26
|
throw Error("No RLN Storage initialized on registry contract.");
|
25
27
|
}
|
26
28
|
this.storageIndex = storageIndex;
|
27
|
-
this.storageContract = new ethers.Contract(storageAddress, RLN_STORAGE_ABI,
|
29
|
+
this.storageContract = new ethers.Contract(storageAddress, RLN_STORAGE_ABI, signer);
|
28
30
|
this._membersFilter = this.storageContract.filters.MemberRegistered();
|
29
31
|
this.deployBlock = await this.storageContract.deployedBlockNumber();
|
30
32
|
}
|
33
|
+
get registry() {
|
34
|
+
if (!this.registryContract) {
|
35
|
+
throw Error("Registry contract was not initialized");
|
36
|
+
}
|
37
|
+
return this.registryContract;
|
38
|
+
}
|
31
39
|
get contract() {
|
32
40
|
if (!this.storageContract) {
|
33
41
|
throw Error("Storage contract was not initialized");
|
@@ -78,9 +86,9 @@ export class RLNContract {
|
|
78
86
|
eventsPerBlock.push(evt);
|
79
87
|
toInsertTable.set(evt.blockNumber, eventsPerBlock);
|
80
88
|
}
|
81
|
-
this.removeMembers(rlnInstance, toRemoveTable);
|
82
|
-
this.insertMembers(rlnInstance, toInsertTable);
|
83
89
|
});
|
90
|
+
this.removeMembers(rlnInstance, toRemoveTable);
|
91
|
+
this.insertMembers(rlnInstance, toInsertTable);
|
84
92
|
}
|
85
93
|
insertMembers(rlnInstance, toInsert) {
|
86
94
|
toInsert.forEach((events, blockNumber) => {
|
@@ -90,11 +98,11 @@ export class RLNContract {
|
|
90
98
|
if (!_idCommitment || !index) {
|
91
99
|
return;
|
92
100
|
}
|
93
|
-
const idCommitment =
|
101
|
+
const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32);
|
94
102
|
rlnInstance.insertMember(idCommitment);
|
95
103
|
this._members.set(index.toNumber(), {
|
96
104
|
index,
|
97
|
-
idCommitment: _idCommitment?._hex
|
105
|
+
idCommitment: _idCommitment?._hex,
|
98
106
|
});
|
99
107
|
});
|
100
108
|
const currentRoot = rlnInstance.getMerkleRoot();
|
@@ -116,15 +124,11 @@ export class RLNContract {
|
|
116
124
|
subscribeToMembers(rlnInstance) {
|
117
125
|
this.contract.on(this.membersFilter, (_pubkey, _index, event) => this.processEvents(rlnInstance, [event]));
|
118
126
|
}
|
119
|
-
async
|
120
|
-
const identityCredential = await rlnInstance.generateSeededIdentityCredential(signature);
|
121
|
-
return this.registerWithKey(identityCredential);
|
122
|
-
}
|
123
|
-
async registerWithKey(credential) {
|
127
|
+
async registerWithIdentity(identity) {
|
124
128
|
if (this.storageIndex === undefined) {
|
125
129
|
throw Error("Cannot register credential, no storage contract index found.");
|
126
130
|
}
|
127
|
-
const txRegisterResponse = await this.registryContract["register(uint16,uint256)"](this.storageIndex,
|
131
|
+
const txRegisterResponse = await this.registryContract["register(uint16,uint256)"](this.storageIndex, identity.IDCommitmentBigInt, { gasLimit: 100000 });
|
128
132
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
129
133
|
// assumption: register(uint16,uint256) emits one event
|
130
134
|
const memberRegistered = txRegisterReceipt?.events?.[0];
|
@@ -132,9 +136,16 @@ export class RLNContract {
|
|
132
136
|
return undefined;
|
133
137
|
}
|
134
138
|
const decodedData = this.contract.interface.decodeEventLog("MemberRegistered", memberRegistered.data);
|
139
|
+
const network = await this.registryContract.provider.getNetwork();
|
140
|
+
const address = this.registryContract.address;
|
141
|
+
const membershipId = decodedData.index.toNumber();
|
135
142
|
return {
|
136
|
-
|
137
|
-
|
143
|
+
identity,
|
144
|
+
membership: {
|
145
|
+
address,
|
146
|
+
treeIndex: membershipId,
|
147
|
+
chainId: network.chainId,
|
148
|
+
},
|
138
149
|
};
|
139
150
|
}
|
140
151
|
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,gBAAgB,EAAE,eAAe,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,gBAA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K,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.
|
3
|
+
"version": "0.1.2-86d4f56",
|
4
4
|
"description": "Rate Limit Nullifier for js-waku",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -72,9 +72,9 @@
|
|
72
72
|
"@types/uuid": "^8.3.0",
|
73
73
|
"@typescript-eslint/eslint-plugin": "^5.8.1",
|
74
74
|
"@typescript-eslint/parser": "^5.8.1",
|
75
|
-
"@waku/core": "^0.0.
|
76
|
-
"@waku/interfaces": "^0.0.
|
77
|
-
"@waku/message-encryption": "^0.0.
|
75
|
+
"@waku/core": "^0.0.25",
|
76
|
+
"@waku/interfaces": "^0.0.20",
|
77
|
+
"@waku/message-encryption": "^0.0.23",
|
78
78
|
"@web/rollup-plugin-import-meta-assets": "^1.0.7",
|
79
79
|
"ajv": "^8.12.0",
|
80
80
|
"ajv-formats": "^2.1.1",
|
@@ -140,8 +140,8 @@
|
|
140
140
|
},
|
141
141
|
"dependencies": {
|
142
142
|
"@chainsafe/bls-keystore": "^3.0.0",
|
143
|
-
"@waku/utils": "^0.0.
|
144
|
-
"@waku/zerokit-rln-wasm": "^0.0.
|
143
|
+
"@waku/utils": "^0.0.13",
|
144
|
+
"@waku/zerokit-rln-wasm": "^0.0.13",
|
145
145
|
"ethereum-cryptography": "^2.1.2",
|
146
146
|
"ethers": "^5.7.2",
|
147
147
|
"lodash": "^4.17.21",
|
package/src/byte_utils.ts
CHANGED
@@ -47,3 +47,17 @@ export function buildBigIntFromUint8Array(array: Uint8Array): bigint {
|
|
47
47
|
const dataView = new DataView(array.buffer);
|
48
48
|
return dataView.getBigUint64(0, true);
|
49
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/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
@@ -4,7 +4,9 @@ import {
|
|
4
4
|
RLN_STORAGE_ABI,
|
5
5
|
SEPOLIA_CONTRACT,
|
6
6
|
} from "./constants.js";
|
7
|
+
import { createRLN } from "./create.js";
|
7
8
|
import { Keystore } from "./keystore/index.js";
|
9
|
+
import { extractMetaMaskSigner } from "./metamask.js";
|
8
10
|
import {
|
9
11
|
IdentityCredential,
|
10
12
|
Proof,
|
@@ -14,16 +16,8 @@ import {
|
|
14
16
|
import { RLNContract } from "./rln_contract.js";
|
15
17
|
import { MerkleRootTracker } from "./root_tracker.js";
|
16
18
|
|
17
|
-
// reexport the create function, dynamically imported from rln.ts
|
18
|
-
export async function create(): Promise<RLNInstance> {
|
19
|
-
// A dependency graph that contains any wasm must all be imported
|
20
|
-
// asynchronously. This file does the single async import, so
|
21
|
-
// that no one else needs to worry about it again.
|
22
|
-
const rlnModule = await import("./rln.js");
|
23
|
-
return await rlnModule.create();
|
24
|
-
}
|
25
|
-
|
26
19
|
export {
|
20
|
+
createRLN,
|
27
21
|
Keystore,
|
28
22
|
RLNInstance,
|
29
23
|
IdentityCredential,
|
@@ -36,4 +30,5 @@ export {
|
|
36
30
|
RLN_STORAGE_ABI,
|
37
31
|
RLN_REGISTRY_ABI,
|
38
32
|
SEPOLIA_CONTRACT,
|
33
|
+
extractMetaMaskSigner,
|
39
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
|
+
};
|
package/src/rln.ts
CHANGED
@@ -1,10 +1,28 @@
|
|
1
|
+
import { createDecoder, createEncoder } from "@waku/core";
|
1
2
|
import type { IRateLimitProof } from "@waku/interfaces";
|
2
|
-
import {
|
3
|
+
import type {
|
4
|
+
ContentTopic,
|
5
|
+
IDecodedMessage,
|
6
|
+
EncoderOptions as WakuEncoderOptions,
|
7
|
+
} from "@waku/interfaces";
|
8
|
+
import init from "@waku/zerokit-rln-wasm";
|
3
9
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
10
|
+
import { ethers } from "ethers";
|
4
11
|
|
5
12
|
import { buildBigIntFromUint8Array, writeUIntLE } from "./byte_utils.js";
|
13
|
+
import type { RLNDecoder, RLNEncoder } from "./codec.js";
|
14
|
+
import { createRLNDecoder, createRLNEncoder } from "./codec.js";
|
15
|
+
import { SEPOLIA_CONTRACT } from "./constants.js";
|
6
16
|
import { dateToEpoch, epochIntToBytes } from "./epoch.js";
|
17
|
+
import { Keystore } from "./keystore/index.js";
|
18
|
+
import type {
|
19
|
+
DecryptedCredentials,
|
20
|
+
EncryptedCredentials,
|
21
|
+
} from "./keystore/index.js";
|
22
|
+
import { KeystoreEntity, Password } from "./keystore/types.js";
|
23
|
+
import { extractMetaMaskSigner } from "./metamask.js";
|
7
24
|
import verificationKey from "./resources/verification_key.js";
|
25
|
+
import { RLNContract } from "./rln_contract.js";
|
8
26
|
import * as wc from "./witness_calculator.js";
|
9
27
|
import { WitnessCalculator } from "./witness_calculator.js";
|
10
28
|
|
@@ -48,7 +66,7 @@ async function loadZkey(): Promise<Uint8Array> {
|
|
48
66
|
* @returns RLNInstance
|
49
67
|
*/
|
50
68
|
export async function create(): Promise<RLNInstance> {
|
51
|
-
await init();
|
69
|
+
await (init as any)?.();
|
52
70
|
zerokitRLN.init_panic_hook();
|
53
71
|
const witnessCalculator = await loadWitnessCalculator();
|
54
72
|
const zkey = await loadZkey();
|
@@ -158,12 +176,229 @@ export function sha256(input: Uint8Array): Uint8Array {
|
|
158
176
|
return zerokitRLN.hash(lenPrefixedData);
|
159
177
|
}
|
160
178
|
|
179
|
+
type StartRLNOptions = {
|
180
|
+
/**
|
181
|
+
* If not set - will extract MetaMask account and get signer from it.
|
182
|
+
*/
|
183
|
+
signer?: ethers.Signer;
|
184
|
+
/**
|
185
|
+
* If not set - will use default SEPOLIA_CONTRACT address.
|
186
|
+
*/
|
187
|
+
registryAddress?: string;
|
188
|
+
/**
|
189
|
+
* Credentials to use for generating proofs and connecting to the contract and network.
|
190
|
+
* If provided used for validating the network chainId and connecting to registry contract.
|
191
|
+
*/
|
192
|
+
credentials?: EncryptedCredentials | DecryptedCredentials;
|
193
|
+
};
|
194
|
+
|
195
|
+
type RegisterMembershipOptions =
|
196
|
+
| { signature: string }
|
197
|
+
| { identity: IdentityCredential };
|
198
|
+
|
199
|
+
type WakuRLNEncoderOptions = WakuEncoderOptions & {
|
200
|
+
credentials: EncryptedCredentials | DecryptedCredentials;
|
201
|
+
};
|
202
|
+
|
161
203
|
export class RLNInstance {
|
204
|
+
private started = false;
|
205
|
+
private starting = false;
|
206
|
+
|
207
|
+
private _contract: undefined | RLNContract;
|
208
|
+
private _signer: undefined | ethers.Signer;
|
209
|
+
|
210
|
+
private keystore = Keystore.create();
|
211
|
+
private _credentials: undefined | DecryptedCredentials;
|
212
|
+
|
162
213
|
constructor(
|
163
214
|
private zkRLN: number,
|
164
215
|
private witnessCalculator: WitnessCalculator
|
165
216
|
) {}
|
166
217
|
|
218
|
+
public get contract(): undefined | RLNContract {
|
219
|
+
return this._contract;
|
220
|
+
}
|
221
|
+
|
222
|
+
public get signer(): undefined | ethers.Signer {
|
223
|
+
return this._signer;
|
224
|
+
}
|
225
|
+
|
226
|
+
public async start(options: StartRLNOptions = {}): Promise<void> {
|
227
|
+
if (this.started || this.starting) {
|
228
|
+
return;
|
229
|
+
}
|
230
|
+
|
231
|
+
this.starting = true;
|
232
|
+
|
233
|
+
try {
|
234
|
+
const { credentials, keystore } =
|
235
|
+
await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
236
|
+
const { signer, registryAddress } = await this.determineStartOptions(
|
237
|
+
options,
|
238
|
+
credentials
|
239
|
+
);
|
240
|
+
|
241
|
+
if (keystore) {
|
242
|
+
this.keystore = keystore;
|
243
|
+
}
|
244
|
+
|
245
|
+
this._credentials = credentials;
|
246
|
+
this._signer = signer!;
|
247
|
+
this._contract = await RLNContract.init(this, {
|
248
|
+
registryAddress: registryAddress!,
|
249
|
+
signer: signer!,
|
250
|
+
});
|
251
|
+
this.started = true;
|
252
|
+
} finally {
|
253
|
+
this.starting = false;
|
254
|
+
}
|
255
|
+
}
|
256
|
+
|
257
|
+
private async determineStartOptions(
|
258
|
+
options: StartRLNOptions,
|
259
|
+
credentials: KeystoreEntity | undefined
|
260
|
+
): Promise<StartRLNOptions> {
|
261
|
+
let chainId = credentials?.membership.chainId;
|
262
|
+
const registryAddress =
|
263
|
+
credentials?.membership.address ||
|
264
|
+
options.registryAddress ||
|
265
|
+
SEPOLIA_CONTRACT.address;
|
266
|
+
|
267
|
+
if (registryAddress === SEPOLIA_CONTRACT.address) {
|
268
|
+
chainId = SEPOLIA_CONTRACT.chainId;
|
269
|
+
}
|
270
|
+
|
271
|
+
const signer = options.signer || (await extractMetaMaskSigner());
|
272
|
+
const currentChainId = await signer.getChainId();
|
273
|
+
|
274
|
+
if (chainId && chainId !== currentChainId) {
|
275
|
+
throw Error(
|
276
|
+
`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
|
277
|
+
);
|
278
|
+
}
|
279
|
+
|
280
|
+
return {
|
281
|
+
signer,
|
282
|
+
registryAddress,
|
283
|
+
};
|
284
|
+
}
|
285
|
+
|
286
|
+
private static async decryptCredentialsIfNeeded(
|
287
|
+
credentials?: EncryptedCredentials | DecryptedCredentials
|
288
|
+
): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
|
289
|
+
if (!credentials) {
|
290
|
+
return {};
|
291
|
+
}
|
292
|
+
|
293
|
+
if ("identity" in credentials) {
|
294
|
+
return { credentials };
|
295
|
+
}
|
296
|
+
|
297
|
+
const keystore = Keystore.fromString(credentials.keystore);
|
298
|
+
|
299
|
+
if (!keystore) {
|
300
|
+
return {};
|
301
|
+
}
|
302
|
+
|
303
|
+
const decryptedCredentials = await keystore.readCredential(
|
304
|
+
credentials.id,
|
305
|
+
credentials.password
|
306
|
+
);
|
307
|
+
|
308
|
+
return {
|
309
|
+
keystore,
|
310
|
+
credentials: decryptedCredentials,
|
311
|
+
};
|
312
|
+
}
|
313
|
+
|
314
|
+
public async registerMembership(
|
315
|
+
options: RegisterMembershipOptions
|
316
|
+
): Promise<undefined | DecryptedCredentials> {
|
317
|
+
if (!this.contract) {
|
318
|
+
throw Error("RLN Contract is not initialized.");
|
319
|
+
}
|
320
|
+
|
321
|
+
let identity = "identity" in options && options.identity;
|
322
|
+
|
323
|
+
if ("signature" in options) {
|
324
|
+
identity = await this.generateSeededIdentityCredential(options.signature);
|
325
|
+
}
|
326
|
+
|
327
|
+
if (!identity) {
|
328
|
+
throw Error("Missing signature or identity to register membership.");
|
329
|
+
}
|
330
|
+
|
331
|
+
return this.contract.registerWithIdentity(identity);
|
332
|
+
}
|
333
|
+
|
334
|
+
/**
|
335
|
+
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
336
|
+
* @param id: string, hash of credentials to select from Keystore
|
337
|
+
* @param password: string or bytes to use to decrypt credentials from Keystore
|
338
|
+
*/
|
339
|
+
public async useCredentials(id: string, password: Password): Promise<void> {
|
340
|
+
this._credentials = await this.keystore?.readCredential(id, password);
|
341
|
+
}
|
342
|
+
|
343
|
+
public async createEncoder(
|
344
|
+
options: WakuRLNEncoderOptions
|
345
|
+
): Promise<RLNEncoder> {
|
346
|
+
const { credentials: decryptedCredentials } =
|
347
|
+
await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
348
|
+
const credentials = decryptedCredentials || this._credentials;
|
349
|
+
|
350
|
+
if (!credentials) {
|
351
|
+
throw Error(
|
352
|
+
"Failed to create Encoder: missing RLN credentials. Use createRLNEncoder directly."
|
353
|
+
);
|
354
|
+
}
|
355
|
+
|
356
|
+
await this.verifyCredentialsAgainstContract(credentials);
|
357
|
+
|
358
|
+
return createRLNEncoder({
|
359
|
+
encoder: createEncoder(options),
|
360
|
+
rlnInstance: this,
|
361
|
+
index: credentials.membership.treeIndex,
|
362
|
+
credential: credentials.identity,
|
363
|
+
});
|
364
|
+
}
|
365
|
+
|
366
|
+
private async verifyCredentialsAgainstContract(
|
367
|
+
credentials: KeystoreEntity
|
368
|
+
): Promise<void> {
|
369
|
+
if (!this._contract) {
|
370
|
+
throw Error(
|
371
|
+
"Failed to verify chain coordinates: no contract initialized."
|
372
|
+
);
|
373
|
+
}
|
374
|
+
|
375
|
+
const registryAddress = credentials.membership.address;
|
376
|
+
const currentRegistryAddress = this._contract.registry.address;
|
377
|
+
if (registryAddress !== currentRegistryAddress) {
|
378
|
+
throw Error(
|
379
|
+
`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
|
380
|
+
);
|
381
|
+
}
|
382
|
+
|
383
|
+
const chainId = credentials.membership.chainId;
|
384
|
+
const network = await this._contract.registry.getNetwork();
|
385
|
+
const currentChainId = network.chainId;
|
386
|
+
if (chainId !== currentChainId) {
|
387
|
+
throw Error(
|
388
|
+
`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`
|
389
|
+
);
|
390
|
+
}
|
391
|
+
}
|
392
|
+
|
393
|
+
public createDecoder(
|
394
|
+
contentTopic: ContentTopic
|
395
|
+
): RLNDecoder<IDecodedMessage> {
|
396
|
+
return createRLNDecoder({
|
397
|
+
rlnInstance: this,
|
398
|
+
decoder: createDecoder(contentTopic),
|
399
|
+
});
|
400
|
+
}
|
401
|
+
|
167
402
|
generateIdentityCredentials(): IdentityCredential {
|
168
403
|
const memKeys = zerokitRLN.generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
|
169
404
|
return IdentityCredential.fromBytes(memKeys);
|