@waku/rln 0.1.1-b429b05 → 0.1.1-bafbe01
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 +24632 -2013
- 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 +4 -3
- package/dist/index.js +4 -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 +2 -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 +193 -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 +9 -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 +10 -0
- package/dist/metamask.js.map +1 -0
- package/dist/rln.d.ts +16 -0
- package/dist/rln.js +19 -12
- package/dist/rln.js.map +1 -1
- package/dist/rln_contract.d.ts +23 -13
- package/dist/rln_contract.js +65 -28
- 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 +11 -11
- package/src/message.ts +1 -1
- package/src/metamask.ts +15 -0
- package/src/rln.ts +35 -13
- package/src/rln_contract.ts +117 -41
- package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
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,11 @@
|
|
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";
|
3
9
|
import {
|
4
10
|
IdentityCredential,
|
5
11
|
Proof,
|
@@ -9,16 +15,9 @@ import {
|
|
9
15
|
import { RLNContract } from "./rln_contract.js";
|
10
16
|
import { MerkleRootTracker } from "./root_tracker.js";
|
11
17
|
|
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
18
|
export {
|
19
|
+
createRLN,
|
20
|
+
Keystore,
|
22
21
|
RLNInstance,
|
23
22
|
IdentityCredential,
|
24
23
|
Proof,
|
@@ -27,6 +26,7 @@ export {
|
|
27
26
|
RLNDecoder,
|
28
27
|
MerkleRootTracker,
|
29
28
|
RLNContract,
|
30
|
-
|
29
|
+
RLN_STORAGE_ABI,
|
30
|
+
RLN_REGISTRY_ABI,
|
31
31
|
SEPOLIA_CONTRACT,
|
32
32
|
};
|
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,15 @@
|
|
1
|
+
import { ethers } from "ethers";
|
2
|
+
|
3
|
+
export const extractMetaMaskAccount =
|
4
|
+
async (): Promise<ethers.providers.Web3Provider> => {
|
5
|
+
const ethereum = (window as any).ethereum;
|
6
|
+
|
7
|
+
if (!ethereum) {
|
8
|
+
throw Error(
|
9
|
+
"Missing or invalid Ethereum provider. Please install a Web3 wallet such as MetaMask."
|
10
|
+
);
|
11
|
+
}
|
12
|
+
|
13
|
+
await ethereum.request({ method: "eth_requestAccounts" });
|
14
|
+
return new ethers.providers.Web3Provider(ethereum, "any");
|
15
|
+
};
|
package/src/rln.ts
CHANGED
@@ -1,9 +1,14 @@
|
|
1
1
|
import type { IRateLimitProof } from "@waku/interfaces";
|
2
|
-
import init
|
2
|
+
import init from "@waku/zerokit-rln-wasm";
|
3
|
+
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
4
|
+
import { ethers } from "ethers";
|
3
5
|
|
4
|
-
import { writeUIntLE } from "./byte_utils.js";
|
6
|
+
import { buildBigIntFromUint8Array, writeUIntLE } from "./byte_utils.js";
|
7
|
+
import { SEPOLIA_CONTRACT } from "./constants.js";
|
5
8
|
import { dateToEpoch, epochIntToBytes } from "./epoch.js";
|
9
|
+
import { extractMetaMaskAccount } from "./metamask.js";
|
6
10
|
import verificationKey from "./resources/verification_key.js";
|
11
|
+
import { RLNContract } from "./rln_contract.js";
|
7
12
|
import * as wc from "./witness_calculator.js";
|
8
13
|
import { WitnessCalculator } from "./witness_calculator.js";
|
9
14
|
|
@@ -26,16 +31,6 @@ function concatenate(...input: Uint8Array[]): Uint8Array {
|
|
26
31
|
return result;
|
27
32
|
}
|
28
33
|
|
29
|
-
/**
|
30
|
-
* Transforms Uint8Array into BigInt
|
31
|
-
* @param array: Uint8Array
|
32
|
-
* @returns BigInt
|
33
|
-
*/
|
34
|
-
function buildBigIntFromUint8Array(array: Uint8Array): bigint {
|
35
|
-
const dataView = new DataView(array.buffer);
|
36
|
-
return dataView.getBigUint64(0, true);
|
37
|
-
}
|
38
|
-
|
39
34
|
const stringEncoder = new TextEncoder();
|
40
35
|
|
41
36
|
const DEPTH = 20;
|
@@ -57,7 +52,7 @@ async function loadZkey(): Promise<Uint8Array> {
|
|
57
52
|
* @returns RLNInstance
|
58
53
|
*/
|
59
54
|
export async function create(): Promise<RLNInstance> {
|
60
|
-
await init();
|
55
|
+
await (init as any)?.();
|
61
56
|
zerokitRLN.init_panic_hook();
|
62
57
|
const witnessCalculator = await loadWitnessCalculator();
|
63
58
|
const zkey = await loadZkey();
|
@@ -167,12 +162,39 @@ export function sha256(input: Uint8Array): Uint8Array {
|
|
167
162
|
return zerokitRLN.hash(lenPrefixedData);
|
168
163
|
}
|
169
164
|
|
165
|
+
type StartRLNOptions = {
|
166
|
+
/**
|
167
|
+
* If not set - will extract MetaMask account and get provider from it.
|
168
|
+
*/
|
169
|
+
provider?: ethers.providers.Provider;
|
170
|
+
/**
|
171
|
+
* If not set - will use default SEPOLIA_CONTRACT address.
|
172
|
+
*/
|
173
|
+
registryAddress?: string;
|
174
|
+
};
|
175
|
+
|
170
176
|
export class RLNInstance {
|
177
|
+
private _contract: null | RLNContract = null;
|
178
|
+
|
171
179
|
constructor(
|
172
180
|
private zkRLN: number,
|
173
181
|
private witnessCalculator: WitnessCalculator
|
174
182
|
) {}
|
175
183
|
|
184
|
+
public get contract(): null | RLNContract {
|
185
|
+
return this._contract;
|
186
|
+
}
|
187
|
+
|
188
|
+
public async start(options: StartRLNOptions = {}): Promise<void> {
|
189
|
+
const provider = options.provider || (await extractMetaMaskAccount());
|
190
|
+
const registryAddress = options.registryAddress || SEPOLIA_CONTRACT.address;
|
191
|
+
|
192
|
+
this._contract = await RLNContract.init(this, {
|
193
|
+
registryAddress,
|
194
|
+
provider,
|
195
|
+
});
|
196
|
+
}
|
197
|
+
|
176
198
|
generateIdentityCredentials(): IdentityCredential {
|
177
199
|
const memKeys = zerokitRLN.generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
|
178
200
|
return IdentityCredential.fromBytes(memKeys);
|
package/src/rln_contract.ts
CHANGED
@@ -1,19 +1,29 @@
|
|
1
|
+
import { hexToBytes } from "@waku/utils/bytes";
|
1
2
|
import { ethers } from "ethers";
|
2
3
|
|
3
|
-
import {
|
4
|
+
import { zeroPadLE } from "./byte_utils.js";
|
5
|
+
import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
|
4
6
|
import { IdentityCredential, RLNInstance } from "./rln.js";
|
5
7
|
import { MerkleRootTracker } from "./root_tracker.js";
|
6
8
|
|
7
9
|
type Member = {
|
8
|
-
|
9
|
-
index:
|
10
|
+
idCommitment: string;
|
11
|
+
index: ethers.BigNumber;
|
10
12
|
};
|
11
13
|
|
12
|
-
type
|
13
|
-
|
14
|
-
|
14
|
+
type Provider = ethers.Signer | ethers.providers.Provider;
|
15
|
+
|
16
|
+
type RLNContractOptions = {
|
17
|
+
provider: Provider;
|
18
|
+
registryAddress: string;
|
19
|
+
};
|
20
|
+
|
21
|
+
type RLNStorageOptions = {
|
22
|
+
storageIndex?: number;
|
15
23
|
};
|
16
24
|
|
25
|
+
type RLNContractInitOptions = RLNContractOptions & RLNStorageOptions;
|
26
|
+
|
17
27
|
type FetchMembersOptions = {
|
18
28
|
fromBlock?: number;
|
19
29
|
fetchRange?: number;
|
@@ -21,18 +31,23 @@ type FetchMembersOptions = {
|
|
21
31
|
};
|
22
32
|
|
23
33
|
export class RLNContract {
|
24
|
-
private
|
25
|
-
private membersFilter: ethers.EventFilter;
|
34
|
+
private registryContract: ethers.Contract;
|
26
35
|
private merkleRootTracker: MerkleRootTracker;
|
27
36
|
|
28
|
-
private
|
37
|
+
private deployBlock: undefined | number;
|
38
|
+
private storageIndex: undefined | number;
|
39
|
+
private storageContract: undefined | ethers.Contract;
|
40
|
+
private _membersFilter: undefined | ethers.EventFilter;
|
41
|
+
|
42
|
+
private _members: Map<number, Member> = new Map();
|
29
43
|
|
30
44
|
public static async init(
|
31
45
|
rlnInstance: RLNInstance,
|
32
|
-
options:
|
46
|
+
options: RLNContractInitOptions
|
33
47
|
): Promise<RLNContract> {
|
34
48
|
const rlnContract = new RLNContract(rlnInstance, options);
|
35
49
|
|
50
|
+
await rlnContract.initStorageContract(options.provider);
|
36
51
|
await rlnContract.fetchMembers(rlnInstance);
|
37
52
|
rlnContract.subscribeToMembers(rlnInstance);
|
38
53
|
|
@@ -41,21 +56,61 @@ export class RLNContract {
|
|
41
56
|
|
42
57
|
constructor(
|
43
58
|
rlnInstance: RLNInstance,
|
44
|
-
{
|
59
|
+
{ registryAddress, provider }: RLNContractOptions
|
45
60
|
) {
|
46
61
|
const initialRoot = rlnInstance.getMerkleRoot();
|
47
62
|
|
48
|
-
this.
|
63
|
+
this.registryContract = new ethers.Contract(
|
64
|
+
registryAddress,
|
65
|
+
RLN_REGISTRY_ABI,
|
66
|
+
provider
|
67
|
+
);
|
49
68
|
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
50
|
-
|
69
|
+
}
|
70
|
+
|
71
|
+
private async initStorageContract(
|
72
|
+
provider: Provider,
|
73
|
+
options: RLNStorageOptions = {}
|
74
|
+
): Promise<void> {
|
75
|
+
const storageIndex = options?.storageIndex
|
76
|
+
? options.storageIndex
|
77
|
+
: await this.registryContract.usingStorageIndex();
|
78
|
+
const storageAddress = await this.registryContract.storages(storageIndex);
|
79
|
+
|
80
|
+
if (!storageAddress || storageAddress === ethers.constants.AddressZero) {
|
81
|
+
throw Error("No RLN Storage initialized on registry contract.");
|
82
|
+
}
|
83
|
+
|
84
|
+
this.storageIndex = storageIndex;
|
85
|
+
this.storageContract = new ethers.Contract(
|
86
|
+
storageAddress,
|
87
|
+
RLN_STORAGE_ABI,
|
88
|
+
provider
|
89
|
+
);
|
90
|
+
this._membersFilter = this.storageContract.filters.MemberRegistered();
|
91
|
+
|
92
|
+
this.deployBlock = await this.storageContract.deployedBlockNumber();
|
51
93
|
}
|
52
94
|
|
53
95
|
public get contract(): ethers.Contract {
|
54
|
-
|
96
|
+
if (!this.storageContract) {
|
97
|
+
throw Error("Storage contract was not initialized");
|
98
|
+
}
|
99
|
+
return this.storageContract as ethers.Contract;
|
55
100
|
}
|
56
101
|
|
57
102
|
public get members(): Member[] {
|
58
|
-
|
103
|
+
const sortedMembers = Array.from(this._members.values()).sort(
|
104
|
+
(left, right) => left.index.toNumber() - right.index.toNumber()
|
105
|
+
);
|
106
|
+
return sortedMembers;
|
107
|
+
}
|
108
|
+
|
109
|
+
private get membersFilter(): ethers.EventFilter {
|
110
|
+
if (!this._membersFilter) {
|
111
|
+
throw Error("Members filter was not initialized.");
|
112
|
+
}
|
113
|
+
return this._membersFilter as ethers.EventFilter;
|
59
114
|
}
|
60
115
|
|
61
116
|
public async fetchMembers(
|
@@ -63,6 +118,7 @@ export class RLNContract {
|
|
63
118
|
options: FetchMembersOptions = {}
|
64
119
|
): Promise<void> {
|
65
120
|
const registeredMemberEvents = await queryFilter(this.contract, {
|
121
|
+
fromBlock: this.deployBlock,
|
66
122
|
...options,
|
67
123
|
membersFilter: this.membersFilter,
|
68
124
|
});
|
@@ -79,13 +135,13 @@ export class RLNContract {
|
|
79
135
|
}
|
80
136
|
|
81
137
|
if (evt.removed) {
|
82
|
-
const index:
|
138
|
+
const index: ethers.BigNumber = evt.args.index;
|
83
139
|
const toRemoveVal = toRemoveTable.get(evt.blockNumber);
|
84
140
|
if (toRemoveVal != undefined) {
|
85
|
-
toRemoveVal.push(index);
|
141
|
+
toRemoveVal.push(index.toNumber());
|
86
142
|
toRemoveTable.set(evt.blockNumber, toRemoveVal);
|
87
143
|
} else {
|
88
|
-
toRemoveTable.set(evt.blockNumber, [index]);
|
144
|
+
toRemoveTable.set(evt.blockNumber, [index.toNumber()]);
|
89
145
|
}
|
90
146
|
} else {
|
91
147
|
let eventsPerBlock = toInsertTable.get(evt.blockNumber);
|
@@ -96,10 +152,10 @@ export class RLNContract {
|
|
96
152
|
eventsPerBlock.push(evt);
|
97
153
|
toInsertTable.set(evt.blockNumber, eventsPerBlock);
|
98
154
|
}
|
99
|
-
|
100
|
-
this.removeMembers(rlnInstance, toRemoveTable);
|
101
|
-
this.insertMembers(rlnInstance, toInsertTable);
|
102
155
|
});
|
156
|
+
|
157
|
+
this.removeMembers(rlnInstance, toRemoveTable);
|
158
|
+
this.insertMembers(rlnInstance, toInsertTable);
|
103
159
|
}
|
104
160
|
|
105
161
|
private insertMembers(
|
@@ -108,18 +164,19 @@ export class RLNContract {
|
|
108
164
|
): void {
|
109
165
|
toInsert.forEach((events: ethers.Event[], blockNumber: number) => {
|
110
166
|
events.forEach((evt) => {
|
111
|
-
|
167
|
+
const _idCommitment = evt?.args?.idCommitment;
|
168
|
+
const index: ethers.BigNumber = evt?.args?.index;
|
169
|
+
|
170
|
+
if (!_idCommitment || !index) {
|
112
171
|
return;
|
113
172
|
}
|
114
173
|
|
115
|
-
const
|
116
|
-
const index = evt.args.index;
|
117
|
-
const idCommitment = ethers.utils.zeroPad(
|
118
|
-
ethers.utils.arrayify(pubkey),
|
119
|
-
32
|
120
|
-
);
|
174
|
+
const idCommitment = zeroPadLE(hexToBytes(_idCommitment?._hex), 32);
|
121
175
|
rlnInstance.insertMember(idCommitment);
|
122
|
-
this.
|
176
|
+
this._members.set(index.toNumber(), {
|
177
|
+
index,
|
178
|
+
idCommitment: _idCommitment?._hex,
|
179
|
+
});
|
123
180
|
});
|
124
181
|
|
125
182
|
const currentRoot = rlnInstance.getMerkleRoot();
|
@@ -134,9 +191,8 @@ export class RLNContract {
|
|
134
191
|
const removeDescending = new Map([...toRemove].sort().reverse());
|
135
192
|
removeDescending.forEach((indexes: number[], blockNumber: number) => {
|
136
193
|
indexes.forEach((index) => {
|
137
|
-
|
138
|
-
|
139
|
-
this.members.splice(idx, 1);
|
194
|
+
if (this._members.has(index)) {
|
195
|
+
this._members.delete(index);
|
140
196
|
}
|
141
197
|
rlnInstance.deleteMember(index);
|
142
198
|
});
|
@@ -147,14 +203,14 @@ export class RLNContract {
|
|
147
203
|
|
148
204
|
public subscribeToMembers(rlnInstance: RLNInstance): void {
|
149
205
|
this.contract.on(this.membersFilter, (_pubkey, _index, event) =>
|
150
|
-
this.processEvents(rlnInstance, event)
|
206
|
+
this.processEvents(rlnInstance, [event])
|
151
207
|
);
|
152
208
|
}
|
153
209
|
|
154
210
|
public async registerWithSignature(
|
155
211
|
rlnInstance: RLNInstance,
|
156
212
|
signature: string
|
157
|
-
): Promise<
|
213
|
+
): Promise<Member | undefined> {
|
158
214
|
const identityCredential =
|
159
215
|
await rlnInstance.generateSeededIdentityCredential(signature);
|
160
216
|
|
@@ -163,16 +219,36 @@ export class RLNContract {
|
|
163
219
|
|
164
220
|
public async registerWithKey(
|
165
221
|
credential: IdentityCredential
|
166
|
-
): Promise<
|
167
|
-
|
168
|
-
|
222
|
+
): Promise<Member | undefined> {
|
223
|
+
if (this.storageIndex === undefined) {
|
224
|
+
throw Error(
|
225
|
+
"Cannot register credential, no storage contract index found."
|
226
|
+
);
|
227
|
+
}
|
169
228
|
const txRegisterResponse: ethers.ContractTransaction =
|
170
|
-
await this.
|
171
|
-
|
172
|
-
|
229
|
+
await this.registryContract["register(uint16,uint256)"](
|
230
|
+
this.storageIndex,
|
231
|
+
credential.IDCommitmentBigInt,
|
232
|
+
{ gasLimit: 100000 }
|
233
|
+
);
|
173
234
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
174
235
|
|
175
|
-
|
236
|
+
// assumption: register(uint16,uint256) emits one event
|
237
|
+
const memberRegistered = txRegisterReceipt?.events?.[0];
|
238
|
+
|
239
|
+
if (!memberRegistered) {
|
240
|
+
return undefined;
|
241
|
+
}
|
242
|
+
|
243
|
+
const decodedData = this.contract.interface.decodeEventLog(
|
244
|
+
"MemberRegistered",
|
245
|
+
memberRegistered.data
|
246
|
+
);
|
247
|
+
|
248
|
+
return {
|
249
|
+
idCommitment: decodedData.idCommitment,
|
250
|
+
index: decodedData.index,
|
251
|
+
};
|
176
252
|
}
|
177
253
|
|
178
254
|
public roots(): Uint8Array[] {
|
Binary file
|