@waku/rln 0.1.4 → 0.1.5-5e19700.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 +5 -0
- package/bundle/_virtual/index2.js +1 -1
- package/bundle/index.js +3 -1
- package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +1 -1
- package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +1 -1
- package/bundle/node_modules/{js-sha3 → @ethersproject/keccak256/node_modules/js-sha3}/src/sha3.js +2 -2
- package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +1 -19
- package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +10 -58
- package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +1 -1
- package/bundle/packages/rln/dist/contract/abi.js +502 -248
- package/bundle/packages/rln/dist/contract/constants.js +4 -4
- package/bundle/packages/rln/dist/contract/rln_contract.js +120 -36
- package/bundle/packages/rln/dist/contract/rln_light_contract.js +473 -0
- package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
- package/bundle/packages/rln/dist/keystore/keystore.js +3 -2
- package/bundle/packages/rln/dist/rln.js +6 -6
- package/bundle/packages/rln/dist/rln_light.js +149 -0
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
- package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_assert.js +1 -1
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_sha2.js +3 -3
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/_u64.js +1 -1
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +79 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +126 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/hmac.js +3 -3
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/pbkdf2.js +4 -4
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/scrypt.js +5 -5
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha256.js +3 -3
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/sha512.js +4 -4
- package/bundle/{node_modules/@chainsafe/bls-keystore → packages/rln}/node_modules/@noble/hashes/utils.js +2 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/codec.test-utils.d.ts +36 -0
- package/dist/codec.test-utils.js +56 -0
- package/dist/codec.test-utils.js.map +1 -0
- package/dist/contract/abi.d.ts +21 -17
- package/dist/contract/abi.js +502 -248
- package/dist/contract/abi.js.map +1 -1
- package/dist/contract/constants.d.ts +22 -18
- package/dist/contract/constants.js +3 -3
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/rln_contract.d.ts +10 -3
- package/dist/contract/rln_contract.js +120 -36
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/contract/rln_light_contract.d.ts +124 -0
- package/dist/contract/rln_light_contract.js +456 -0
- package/dist/contract/rln_light_contract.js.map +1 -0
- package/dist/contract/test-setup.d.ts +26 -0
- package/dist/contract/test-setup.js +56 -0
- package/dist/contract/test-setup.js.map +1 -0
- package/dist/contract/test-utils.d.ts +39 -0
- package/dist/contract/test-utils.js +118 -0
- package/dist/contract/test-utils.js.map +1 -0
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js +2 -1
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +1 -0
- package/dist/rln.d.ts +1 -1
- package/dist/rln.js +6 -6
- package/dist/rln.js.map +1 -1
- package/dist/rln_light.d.ts +64 -0
- package/dist/rln_light.js +144 -0
- package/dist/rln_light.js.map +1 -0
- package/package.json +1 -91
- package/src/codec.test-utils.ts +80 -0
- package/src/contract/abi.ts +502 -248
- package/src/contract/constants.ts +3 -3
- package/src/contract/rln_contract.ts +158 -46
- package/src/contract/rln_light_contract.ts +718 -0
- package/src/contract/test-setup.ts +86 -0
- package/src/contract/test-utils.ts +179 -0
- package/src/index.ts +17 -2
- package/src/keystore/keystore.ts +2 -1
- package/src/keystore/types.ts +1 -0
- package/src/rln.ts +7 -7
- package/src/rln_light.ts +235 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
@@ -0,0 +1,86 @@
|
|
1
|
+
import { hexToBytes } from "@waku/utils/bytes";
|
2
|
+
import { ethers } from "ethers";
|
3
|
+
import sinon from "sinon";
|
4
|
+
|
5
|
+
import { createRLN } from "../create.js";
|
6
|
+
import type { IdentityCredential } from "../identity.js";
|
7
|
+
|
8
|
+
import { DEFAULT_RATE_LIMIT, LINEA_CONTRACT } from "./constants.js";
|
9
|
+
import { RLNContract } from "./rln_contract.js";
|
10
|
+
|
11
|
+
export interface TestRLNInstance {
|
12
|
+
rlnInstance: any;
|
13
|
+
identity: IdentityCredential;
|
14
|
+
insertMemberSpy: sinon.SinonStub;
|
15
|
+
}
|
16
|
+
|
17
|
+
/**
|
18
|
+
* Creates a test RLN instance with basic setup
|
19
|
+
*/
|
20
|
+
export async function createTestRLNInstance(): Promise<TestRLNInstance> {
|
21
|
+
const rlnInstance = await createRLN();
|
22
|
+
const insertMemberSpy = sinon.stub();
|
23
|
+
rlnInstance.zerokit.insertMember = insertMemberSpy;
|
24
|
+
|
25
|
+
const mockSignature =
|
26
|
+
"0xdeb8a6b00a8e404deb1f52d3aa72ed7f60a2ff4484c737eedaef18a0aacb2dfb4d5d74ac39bb71fa358cf2eb390565a35b026cc6272f2010d4351e17670311c21c";
|
27
|
+
const identity =
|
28
|
+
rlnInstance.zerokit.generateSeededIdentityCredential(mockSignature);
|
29
|
+
|
30
|
+
return {
|
31
|
+
rlnInstance,
|
32
|
+
identity,
|
33
|
+
insertMemberSpy
|
34
|
+
};
|
35
|
+
}
|
36
|
+
|
37
|
+
/**
|
38
|
+
* Initializes an RLN contract with the given registry contract
|
39
|
+
*/
|
40
|
+
export async function initializeRLNContract(
|
41
|
+
rlnInstance: any,
|
42
|
+
mockedRegistryContract: ethers.Contract
|
43
|
+
): Promise<RLNContract> {
|
44
|
+
const provider = new ethers.providers.JsonRpcProvider();
|
45
|
+
const voidSigner = new ethers.VoidSigner(LINEA_CONTRACT.address, provider);
|
46
|
+
|
47
|
+
const originalRegister = mockedRegistryContract.register;
|
48
|
+
(mockedRegistryContract as any).register = function (...args: any[]) {
|
49
|
+
const result = originalRegister.apply(this, args);
|
50
|
+
|
51
|
+
if (args[0] && rlnInstance.zerokit) {
|
52
|
+
const idCommitmentBigInt = args[0];
|
53
|
+
const idCommitmentHex =
|
54
|
+
"0x" + idCommitmentBigInt.toString(16).padStart(64, "0");
|
55
|
+
const idCommitment = ethers.utils.zeroPad(
|
56
|
+
hexToBytes(idCommitmentHex),
|
57
|
+
32
|
58
|
+
);
|
59
|
+
rlnInstance.zerokit.insertMember(idCommitment);
|
60
|
+
}
|
61
|
+
|
62
|
+
return result;
|
63
|
+
};
|
64
|
+
|
65
|
+
const contract = await RLNContract.init(rlnInstance, {
|
66
|
+
address: LINEA_CONTRACT.address,
|
67
|
+
signer: voidSigner,
|
68
|
+
rateLimit: DEFAULT_RATE_LIMIT,
|
69
|
+
contract: mockedRegistryContract
|
70
|
+
});
|
71
|
+
|
72
|
+
return contract;
|
73
|
+
}
|
74
|
+
|
75
|
+
/**
|
76
|
+
* Common test message data
|
77
|
+
*/
|
78
|
+
export const TEST_DATA = {
|
79
|
+
contentTopic: "/test/1/waku-message/utf8",
|
80
|
+
emptyPubsubTopic: "",
|
81
|
+
testMessage: Uint8Array.from(
|
82
|
+
"Hello World".split("").map((x) => x.charCodeAt(0))
|
83
|
+
),
|
84
|
+
mockSignature:
|
85
|
+
"0xdeb8a6b00a8e404deb1f52d3aa72ed7f60a2ff4484c737eedaef18a0aacb2dfb4d5d74ac39bb71fa358cf2eb390565a35b026cc6272f2010d4351e17670311c21c"
|
86
|
+
} as const;
|
@@ -0,0 +1,179 @@
|
|
1
|
+
import { hexToBytes } from "@waku/utils/bytes";
|
2
|
+
import { expect } from "chai";
|
3
|
+
import * as ethers from "ethers";
|
4
|
+
import sinon from "sinon";
|
5
|
+
|
6
|
+
import type { IdentityCredential } from "../identity.js";
|
7
|
+
|
8
|
+
import { DEFAULT_RATE_LIMIT, LINEA_CONTRACT } from "./constants.js";
|
9
|
+
|
10
|
+
export const mockRateLimits = {
|
11
|
+
minRate: 20,
|
12
|
+
maxRate: 600,
|
13
|
+
maxTotalRate: 1200,
|
14
|
+
currentTotalRate: 500
|
15
|
+
};
|
16
|
+
|
17
|
+
type MockProvider = {
|
18
|
+
getLogs: () => never[];
|
19
|
+
getBlockNumber: () => Promise<number>;
|
20
|
+
getNetwork: () => Promise<{ chainId: number }>;
|
21
|
+
};
|
22
|
+
|
23
|
+
type MockFilters = {
|
24
|
+
MembershipRegistered: () => { address: string };
|
25
|
+
MembershipErased: () => { address: string };
|
26
|
+
MembershipExpired: () => { address: string };
|
27
|
+
};
|
28
|
+
|
29
|
+
export function createMockProvider(): MockProvider {
|
30
|
+
return {
|
31
|
+
getLogs: () => [],
|
32
|
+
getBlockNumber: () => Promise.resolve(1000),
|
33
|
+
getNetwork: () => Promise.resolve({ chainId: 11155111 })
|
34
|
+
};
|
35
|
+
}
|
36
|
+
|
37
|
+
export function createMockFilters(): MockFilters {
|
38
|
+
return {
|
39
|
+
MembershipRegistered: () => ({ address: LINEA_CONTRACT.address }),
|
40
|
+
MembershipErased: () => ({ address: LINEA_CONTRACT.address }),
|
41
|
+
MembershipExpired: () => ({ address: LINEA_CONTRACT.address })
|
42
|
+
};
|
43
|
+
}
|
44
|
+
|
45
|
+
type ContractOverrides = Partial<{
|
46
|
+
filters: Record<string, unknown>;
|
47
|
+
[key: string]: unknown;
|
48
|
+
}>;
|
49
|
+
|
50
|
+
export function createMockRegistryContract(
|
51
|
+
overrides: ContractOverrides = {}
|
52
|
+
): ethers.Contract {
|
53
|
+
const filters = {
|
54
|
+
MembershipRegistered: () => ({ address: LINEA_CONTRACT.address }),
|
55
|
+
MembershipErased: () => ({ address: LINEA_CONTRACT.address }),
|
56
|
+
MembershipExpired: () => ({ address: LINEA_CONTRACT.address })
|
57
|
+
};
|
58
|
+
|
59
|
+
const baseContract = {
|
60
|
+
minMembershipRateLimit: () =>
|
61
|
+
Promise.resolve(ethers.BigNumber.from(mockRateLimits.minRate)),
|
62
|
+
maxMembershipRateLimit: () =>
|
63
|
+
Promise.resolve(ethers.BigNumber.from(mockRateLimits.maxRate)),
|
64
|
+
maxTotalRateLimit: () =>
|
65
|
+
Promise.resolve(ethers.BigNumber.from(mockRateLimits.maxTotalRate)),
|
66
|
+
currentTotalRateLimit: () =>
|
67
|
+
Promise.resolve(ethers.BigNumber.from(mockRateLimits.currentTotalRate)),
|
68
|
+
queryFilter: () => [],
|
69
|
+
provider: createMockProvider(),
|
70
|
+
filters,
|
71
|
+
on: () => ({}),
|
72
|
+
removeAllListeners: () => ({}),
|
73
|
+
register: () => ({
|
74
|
+
wait: () =>
|
75
|
+
Promise.resolve({
|
76
|
+
events: [mockRLNRegisteredEvent()]
|
77
|
+
})
|
78
|
+
}),
|
79
|
+
estimateGas: {
|
80
|
+
register: () => Promise.resolve(ethers.BigNumber.from(100000))
|
81
|
+
},
|
82
|
+
functions: {
|
83
|
+
register: () => Promise.resolve()
|
84
|
+
},
|
85
|
+
getMemberIndex: () => Promise.resolve(null),
|
86
|
+
interface: {
|
87
|
+
getEvent: (eventName: string) => ({
|
88
|
+
name: eventName,
|
89
|
+
format: () => {}
|
90
|
+
})
|
91
|
+
},
|
92
|
+
address: LINEA_CONTRACT.address
|
93
|
+
};
|
94
|
+
|
95
|
+
// Merge overrides while preserving filters
|
96
|
+
const merged = {
|
97
|
+
...baseContract,
|
98
|
+
...overrides,
|
99
|
+
filters: { ...filters, ...(overrides.filters || {}) }
|
100
|
+
};
|
101
|
+
|
102
|
+
return merged as unknown as ethers.Contract;
|
103
|
+
}
|
104
|
+
|
105
|
+
export function mockRLNRegisteredEvent(idCommitment?: string): ethers.Event {
|
106
|
+
return {
|
107
|
+
args: {
|
108
|
+
idCommitment:
|
109
|
+
idCommitment ||
|
110
|
+
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
|
111
|
+
membershipRateLimit: ethers.BigNumber.from(DEFAULT_RATE_LIMIT),
|
112
|
+
index: ethers.BigNumber.from(1)
|
113
|
+
},
|
114
|
+
event: "MembershipRegistered"
|
115
|
+
} as unknown as ethers.Event;
|
116
|
+
}
|
117
|
+
|
118
|
+
export function formatIdCommitment(idCommitmentBigInt: bigint): string {
|
119
|
+
return "0x" + idCommitmentBigInt.toString(16).padStart(64, "0");
|
120
|
+
}
|
121
|
+
|
122
|
+
export function createRegisterStub(
|
123
|
+
identity: IdentityCredential
|
124
|
+
): sinon.SinonStub {
|
125
|
+
return sinon.stub().callsFake(() => ({
|
126
|
+
wait: () =>
|
127
|
+
Promise.resolve({
|
128
|
+
events: [
|
129
|
+
{
|
130
|
+
event: "MembershipRegistered",
|
131
|
+
args: {
|
132
|
+
idCommitment: formatIdCommitment(identity.IDCommitmentBigInt),
|
133
|
+
rateLimit: DEFAULT_RATE_LIMIT,
|
134
|
+
index: ethers.BigNumber.from(1)
|
135
|
+
}
|
136
|
+
}
|
137
|
+
]
|
138
|
+
})
|
139
|
+
}));
|
140
|
+
}
|
141
|
+
|
142
|
+
export function verifyRegistration(
|
143
|
+
decryptedCredentials: any,
|
144
|
+
identity: IdentityCredential,
|
145
|
+
registerStub: sinon.SinonStub,
|
146
|
+
insertMemberSpy: sinon.SinonStub
|
147
|
+
): void {
|
148
|
+
if (!decryptedCredentials) {
|
149
|
+
throw new Error("Decrypted credentials should not be undefined");
|
150
|
+
}
|
151
|
+
|
152
|
+
// Verify registration call
|
153
|
+
expect(
|
154
|
+
registerStub.calledWith(
|
155
|
+
sinon.match.same(identity.IDCommitmentBigInt),
|
156
|
+
sinon.match.same(DEFAULT_RATE_LIMIT),
|
157
|
+
sinon.match.array,
|
158
|
+
sinon.match.object
|
159
|
+
)
|
160
|
+
).to.be.true;
|
161
|
+
|
162
|
+
// Verify credential properties
|
163
|
+
expect(decryptedCredentials).to.have.property("identity");
|
164
|
+
expect(decryptedCredentials).to.have.property("membership");
|
165
|
+
expect(decryptedCredentials.membership).to.include({
|
166
|
+
address: LINEA_CONTRACT.address,
|
167
|
+
treeIndex: 1
|
168
|
+
});
|
169
|
+
|
170
|
+
// Verify member insertion
|
171
|
+
const expectedIdCommitment = ethers.utils.zeroPad(
|
172
|
+
hexToBytes(formatIdCommitment(identity.IDCommitmentBigInt)),
|
173
|
+
32
|
174
|
+
);
|
175
|
+
expect(insertMemberSpy.callCount).to.equal(1);
|
176
|
+
expect(insertMemberSpy.getCall(0).args[0]).to.deep.equal(
|
177
|
+
expectedIdCommitment
|
178
|
+
);
|
179
|
+
}
|
package/src/index.ts
CHANGED
@@ -1,15 +1,19 @@
|
|
1
1
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
2
2
|
import { RLN_ABI } from "./contract/abi.js";
|
3
|
-
import {
|
3
|
+
import { LINEA_CONTRACT, RLNContract } from "./contract/index.js";
|
4
|
+
import { RLNLightContract } from "./contract/rln_light_contract.js";
|
4
5
|
import { createRLN } from "./create.js";
|
5
6
|
import { IdentityCredential } from "./identity.js";
|
6
7
|
import { Keystore } from "./keystore/index.js";
|
7
8
|
import { Proof } from "./proof.js";
|
8
9
|
import { RLNInstance } from "./rln.js";
|
10
|
+
import { RLNLightInstance } from "./rln_light.js";
|
9
11
|
import { MerkleRootTracker } from "./root_tracker.js";
|
10
12
|
import { extractMetaMaskSigner } from "./utils/index.js";
|
11
13
|
|
12
14
|
export {
|
15
|
+
RLNLightInstance,
|
16
|
+
RLNLightContract,
|
13
17
|
createRLN,
|
14
18
|
Keystore,
|
15
19
|
RLNInstance,
|
@@ -19,7 +23,18 @@ export {
|
|
19
23
|
RLNDecoder,
|
20
24
|
MerkleRootTracker,
|
21
25
|
RLNContract,
|
22
|
-
|
26
|
+
LINEA_CONTRACT,
|
23
27
|
extractMetaMaskSigner,
|
24
28
|
RLN_ABI
|
25
29
|
};
|
30
|
+
|
31
|
+
export type {
|
32
|
+
DecryptedCredentials,
|
33
|
+
EncryptedCredentials,
|
34
|
+
Keccak256Hash,
|
35
|
+
KeystoreEntity,
|
36
|
+
MembershipHash,
|
37
|
+
MembershipInfo,
|
38
|
+
Password,
|
39
|
+
Sha256Hash
|
40
|
+
} from "./keystore/types.js";
|
package/src/keystore/keystore.ts
CHANGED
@@ -274,7 +274,8 @@ export class Keystore {
|
|
274
274
|
membership: {
|
275
275
|
treeIndex: _.get(obj, "treeIndex"),
|
276
276
|
chainId: _.get(obj, "membershipContract.chainId"),
|
277
|
-
address: _.get(obj, "membershipContract.address")
|
277
|
+
address: _.get(obj, "membershipContract.address"),
|
278
|
+
rateLimit: _.get(obj, "membershipContract.rateLimit")
|
278
279
|
}
|
279
280
|
};
|
280
281
|
} catch (err) {
|
package/src/keystore/types.ts
CHANGED
package/src/rln.ts
CHANGED
@@ -16,7 +16,7 @@ import {
|
|
16
16
|
type RLNEncoder
|
17
17
|
} from "./codec.js";
|
18
18
|
import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
|
19
|
-
import {
|
19
|
+
import { LINEA_CONTRACT, RLNContract } from "./contract/index.js";
|
20
20
|
import { IdentityCredential } from "./identity.js";
|
21
21
|
import { Keystore } from "./keystore/index.js";
|
22
22
|
import type {
|
@@ -108,7 +108,7 @@ type StartRLNOptions = {
|
|
108
108
|
*/
|
109
109
|
signer?: ethers.Signer;
|
110
110
|
/**
|
111
|
-
* If not set - will use default
|
111
|
+
* If not set - will use default LINEA_CONTRACT address.
|
112
112
|
*/
|
113
113
|
address?: string;
|
114
114
|
/**
|
@@ -160,7 +160,7 @@ export class RLNInstance {
|
|
160
160
|
try {
|
161
161
|
const { credentials, keystore } =
|
162
162
|
await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
163
|
-
const { signer, address } = await this.determineStartOptions(
|
163
|
+
const { signer, address, rateLimit } = await this.determineStartOptions(
|
164
164
|
options,
|
165
165
|
credentials
|
166
166
|
);
|
@@ -174,7 +174,7 @@ export class RLNInstance {
|
|
174
174
|
this._contract = await RLNContract.init(this, {
|
175
175
|
address: address!,
|
176
176
|
signer: signer!,
|
177
|
-
rateLimit:
|
177
|
+
rateLimit: rateLimit ?? this.zerokit.getRateLimit
|
178
178
|
});
|
179
179
|
this.started = true;
|
180
180
|
} finally {
|
@@ -190,10 +190,10 @@ export class RLNInstance {
|
|
190
190
|
const address =
|
191
191
|
credentials?.membership.address ||
|
192
192
|
options.address ||
|
193
|
-
|
193
|
+
LINEA_CONTRACT.address;
|
194
194
|
|
195
|
-
if (address ===
|
196
|
-
chainId =
|
195
|
+
if (address === LINEA_CONTRACT.address) {
|
196
|
+
chainId = LINEA_CONTRACT.chainId;
|
197
197
|
}
|
198
198
|
|
199
199
|
const signer = options.signer || (await extractMetaMaskSigner());
|
package/src/rln_light.ts
ADDED
@@ -0,0 +1,235 @@
|
|
1
|
+
import { hmac } from "@noble/hashes/hmac";
|
2
|
+
import { sha256 } from "@noble/hashes/sha256";
|
3
|
+
import { Logger } from "@waku/utils";
|
4
|
+
import { ethers } from "ethers";
|
5
|
+
|
6
|
+
import { LINEA_CONTRACT } from "./contract/constants.js";
|
7
|
+
import { RLNLightContract } from "./contract/rln_light_contract.js";
|
8
|
+
import { IdentityCredential } from "./identity.js";
|
9
|
+
import { Keystore } from "./keystore/index.js";
|
10
|
+
import type {
|
11
|
+
DecryptedCredentials,
|
12
|
+
EncryptedCredentials
|
13
|
+
} from "./keystore/index.js";
|
14
|
+
import { KeystoreEntity, Password } from "./keystore/types.js";
|
15
|
+
import {
|
16
|
+
buildBigIntFromUint8Array,
|
17
|
+
extractMetaMaskSigner
|
18
|
+
} from "./utils/index.js";
|
19
|
+
|
20
|
+
const log = new Logger("waku:rln");
|
21
|
+
|
22
|
+
/**
|
23
|
+
* Create an instance of RLN
|
24
|
+
* @returns RLNInstance
|
25
|
+
*/
|
26
|
+
export async function create(): Promise<RLNLightInstance> {
|
27
|
+
try {
|
28
|
+
return new RLNLightInstance();
|
29
|
+
} catch (error) {
|
30
|
+
log.error("Failed to initialize RLN:", error);
|
31
|
+
throw error;
|
32
|
+
}
|
33
|
+
}
|
34
|
+
|
35
|
+
type StartRLNOptions = {
|
36
|
+
/**
|
37
|
+
* If not set - will extract MetaMask account and get signer from it.
|
38
|
+
*/
|
39
|
+
signer?: ethers.Signer;
|
40
|
+
/**
|
41
|
+
* If not set - will use default SEPOLIA_CONTRACT address.
|
42
|
+
*/
|
43
|
+
address?: string;
|
44
|
+
/**
|
45
|
+
* Credentials to use for generating proofs and connecting to the contract and network.
|
46
|
+
* If provided used for validating the network chainId and connecting to registry contract.
|
47
|
+
*/
|
48
|
+
credentials?: EncryptedCredentials | DecryptedCredentials;
|
49
|
+
/**
|
50
|
+
* Rate limit for the member.
|
51
|
+
*/
|
52
|
+
rateLimit?: number;
|
53
|
+
};
|
54
|
+
|
55
|
+
type RegisterMembershipOptions =
|
56
|
+
| { signature: string }
|
57
|
+
| { identity: IdentityCredential };
|
58
|
+
|
59
|
+
export class RLNLightInstance {
|
60
|
+
private started = false;
|
61
|
+
private starting = false;
|
62
|
+
|
63
|
+
private _contract: undefined | RLNLightContract;
|
64
|
+
private _signer: undefined | ethers.Signer;
|
65
|
+
|
66
|
+
private keystore = Keystore.create();
|
67
|
+
private _credentials: undefined | DecryptedCredentials;
|
68
|
+
|
69
|
+
public constructor() {}
|
70
|
+
|
71
|
+
public get contract(): undefined | RLNLightContract {
|
72
|
+
return this._contract;
|
73
|
+
}
|
74
|
+
|
75
|
+
public get signer(): undefined | ethers.Signer {
|
76
|
+
return this._signer;
|
77
|
+
}
|
78
|
+
|
79
|
+
public async start(options: StartRLNOptions = {}): Promise<void> {
|
80
|
+
if (this.started || this.starting) {
|
81
|
+
return;
|
82
|
+
}
|
83
|
+
|
84
|
+
this.starting = true;
|
85
|
+
|
86
|
+
try {
|
87
|
+
const { credentials, keystore } =
|
88
|
+
await RLNLightInstance.decryptCredentialsIfNeeded(options.credentials);
|
89
|
+
const { signer, address, rateLimit } = await this.determineStartOptions(
|
90
|
+
options,
|
91
|
+
credentials
|
92
|
+
);
|
93
|
+
|
94
|
+
if (keystore) {
|
95
|
+
this.keystore = keystore;
|
96
|
+
}
|
97
|
+
|
98
|
+
this._credentials = credentials;
|
99
|
+
this._signer = signer!;
|
100
|
+
this._contract = await RLNLightContract.init({
|
101
|
+
address: address!,
|
102
|
+
signer: signer!,
|
103
|
+
rateLimit: rateLimit
|
104
|
+
});
|
105
|
+
this.started = true;
|
106
|
+
} finally {
|
107
|
+
this.starting = false;
|
108
|
+
}
|
109
|
+
}
|
110
|
+
|
111
|
+
public get credentials(): DecryptedCredentials | undefined {
|
112
|
+
return this._credentials;
|
113
|
+
}
|
114
|
+
|
115
|
+
private async determineStartOptions(
|
116
|
+
options: StartRLNOptions,
|
117
|
+
credentials: KeystoreEntity | undefined
|
118
|
+
): Promise<StartRLNOptions> {
|
119
|
+
let chainId = credentials?.membership.chainId;
|
120
|
+
const address =
|
121
|
+
credentials?.membership.address ||
|
122
|
+
options.address ||
|
123
|
+
LINEA_CONTRACT.address;
|
124
|
+
|
125
|
+
if (address === LINEA_CONTRACT.address) {
|
126
|
+
chainId = LINEA_CONTRACT.chainId;
|
127
|
+
}
|
128
|
+
|
129
|
+
const signer = options.signer || (await extractMetaMaskSigner());
|
130
|
+
const currentChainId = await signer.getChainId();
|
131
|
+
|
132
|
+
if (chainId && chainId !== currentChainId) {
|
133
|
+
throw Error(
|
134
|
+
`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
|
135
|
+
);
|
136
|
+
}
|
137
|
+
|
138
|
+
return {
|
139
|
+
signer,
|
140
|
+
address
|
141
|
+
};
|
142
|
+
}
|
143
|
+
|
144
|
+
private static async decryptCredentialsIfNeeded(
|
145
|
+
credentials?: EncryptedCredentials | DecryptedCredentials
|
146
|
+
): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
|
147
|
+
if (!credentials) {
|
148
|
+
return {};
|
149
|
+
}
|
150
|
+
|
151
|
+
if ("identity" in credentials) {
|
152
|
+
return { credentials };
|
153
|
+
}
|
154
|
+
|
155
|
+
const keystore = Keystore.fromString(credentials.keystore);
|
156
|
+
|
157
|
+
if (!keystore) {
|
158
|
+
return {};
|
159
|
+
}
|
160
|
+
|
161
|
+
const decryptedCredentials = await keystore.readCredential(
|
162
|
+
credentials.id,
|
163
|
+
credentials.password
|
164
|
+
);
|
165
|
+
|
166
|
+
return {
|
167
|
+
keystore,
|
168
|
+
credentials: decryptedCredentials
|
169
|
+
};
|
170
|
+
}
|
171
|
+
|
172
|
+
/**
|
173
|
+
* Generates an identity credential from a seed string
|
174
|
+
* This is a pure implementation that doesn't rely on Zerokit
|
175
|
+
* @param seed A string seed to generate the identity from
|
176
|
+
* @returns IdentityCredential
|
177
|
+
*/
|
178
|
+
private generateSeededIdentityCredential(seed: string): IdentityCredential {
|
179
|
+
// Convert the seed to bytes
|
180
|
+
const encoder = new TextEncoder();
|
181
|
+
const seedBytes = encoder.encode(seed);
|
182
|
+
|
183
|
+
// Generate deterministic values using HMAC-SHA256
|
184
|
+
// We use different context strings for each component to ensure they're different
|
185
|
+
const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
|
186
|
+
const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
|
187
|
+
|
188
|
+
// Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
|
189
|
+
const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
|
190
|
+
const idSecretHash = sha256(combinedBytes);
|
191
|
+
|
192
|
+
// Generate IDCommitment as a hash of IDSecretHash
|
193
|
+
const idCommitment = sha256(idSecretHash);
|
194
|
+
|
195
|
+
// Convert IDCommitment to BigInt
|
196
|
+
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
|
197
|
+
|
198
|
+
return new IdentityCredential(
|
199
|
+
idTrapdoor,
|
200
|
+
idNullifier,
|
201
|
+
idSecretHash,
|
202
|
+
idCommitment,
|
203
|
+
idCommitmentBigInt
|
204
|
+
);
|
205
|
+
}
|
206
|
+
|
207
|
+
public async registerMembership(
|
208
|
+
options: RegisterMembershipOptions
|
209
|
+
): Promise<undefined | DecryptedCredentials> {
|
210
|
+
if (!this.contract) {
|
211
|
+
throw Error("RLN Contract is not initialized.");
|
212
|
+
}
|
213
|
+
|
214
|
+
let identity = "identity" in options && options.identity;
|
215
|
+
|
216
|
+
if ("signature" in options) {
|
217
|
+
identity = this.generateSeededIdentityCredential(options.signature);
|
218
|
+
}
|
219
|
+
|
220
|
+
if (!identity) {
|
221
|
+
throw Error("Missing signature or identity to register membership.");
|
222
|
+
}
|
223
|
+
|
224
|
+
return this.contract.registerWithIdentity(identity);
|
225
|
+
}
|
226
|
+
|
227
|
+
/**
|
228
|
+
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
229
|
+
* @param id: string, hash of credentials to select from Keystore
|
230
|
+
* @param password: string or bytes to use to decrypt credentials from Keystore
|
231
|
+
*/
|
232
|
+
public async useCredentials(id: string, password: Password): Promise<void> {
|
233
|
+
this._credentials = await this.keystore?.readCredential(id, password);
|
234
|
+
}
|
235
|
+
}
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|