mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-04 06:33:09 +00:00
refactor: RateLimitProof
This commit is contained in:
parent
3ce01eaa10
commit
b9269660c8
14
package-lock.json
generated
14
package-lock.json
generated
@ -41,7 +41,7 @@
|
||||
"husky": "^7.0.4",
|
||||
"ignore-loader": "^0.1.2",
|
||||
"isomorphic-fetch": "^3.0.0",
|
||||
"js-waku": "^0.29.0-5bab85f",
|
||||
"js-waku": "^0.29.0-7714812",
|
||||
"jsdom": "^19.0.0",
|
||||
"jsdom-global": "^3.0.2",
|
||||
"karma": "^6.3.12",
|
||||
@ -6647,9 +6647,9 @@
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/js-waku": {
|
||||
"version": "0.29.0-5bab85f",
|
||||
"resolved": "https://registry.npmjs.org/js-waku/-/js-waku-0.29.0-5bab85f.tgz",
|
||||
"integrity": "sha512-5uJCslPYnz+7QoY33ybVbpDnxduyCKzOmIDCGYitke6AtHMwsBfRIlCWlHdmUWonB3UdLZup80Rk07IPd5gqhA==",
|
||||
"version": "0.29.0-7714812",
|
||||
"resolved": "https://registry.npmjs.org/js-waku/-/js-waku-0.29.0-7714812.tgz",
|
||||
"integrity": "sha512-pHX7cvrLC8Yw0KxTa8KJlFcE9S5WLkBWqd0pWqBWGCG5mU1Q6LWBZv+rOYuhn9wkCCpjF5jWJvmTohzkGt91XA==",
|
||||
"dev": true,
|
||||
"dependencies": {
|
||||
"@chainsafe/libp2p-gossipsub": "^4.1.1",
|
||||
@ -15715,9 +15715,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"js-waku": {
|
||||
"version": "0.29.0-5bab85f",
|
||||
"resolved": "https://registry.npmjs.org/js-waku/-/js-waku-0.29.0-5bab85f.tgz",
|
||||
"integrity": "sha512-5uJCslPYnz+7QoY33ybVbpDnxduyCKzOmIDCGYitke6AtHMwsBfRIlCWlHdmUWonB3UdLZup80Rk07IPd5gqhA==",
|
||||
"version": "0.29.0-7714812",
|
||||
"resolved": "https://registry.npmjs.org/js-waku/-/js-waku-0.29.0-7714812.tgz",
|
||||
"integrity": "sha512-pHX7cvrLC8Yw0KxTa8KJlFcE9S5WLkBWqd0pWqBWGCG5mU1Q6LWBZv+rOYuhn9wkCCpjF5jWJvmTohzkGt91XA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@chainsafe/libp2p-gossipsub": "^4.1.1",
|
||||
|
||||
@ -81,7 +81,7 @@
|
||||
"husky": "^7.0.4",
|
||||
"ignore-loader": "^0.1.2",
|
||||
"isomorphic-fetch": "^3.0.0",
|
||||
"js-waku": "^0.29.0-5bab85f",
|
||||
"js-waku": "^0.29.0-7714812",
|
||||
"jsdom": "^19.0.0",
|
||||
"jsdom-global": "^3.0.2",
|
||||
"karma": "^6.3.12",
|
||||
|
||||
@ -1,9 +1,13 @@
|
||||
import { expect } from "chai";
|
||||
|
||||
import {DecoderV0, EncoderV0, MessageV0} from "js-waku/lib/waku_message/version_0";
|
||||
import {
|
||||
DecoderV0,
|
||||
EncoderV0,
|
||||
MessageV0,
|
||||
} from "js-waku/lib/waku_message/version_0";
|
||||
|
||||
import { RLNDecoder, RLNEncoder } from "./encoder.js";
|
||||
import * as rln from "./index";
|
||||
|
||||
import * as rln from "./index.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-message/utf8";
|
||||
|
||||
@ -27,11 +31,16 @@ describe("js-rln: encoder", () => {
|
||||
const bytes = await rlnEncoder.encode({ payload });
|
||||
const protoResult = await rlnDecoder.decodeProto(bytes!);
|
||||
|
||||
const result = (await rlnDecoder.decode(protoResult!)) as MessageV0;
|
||||
const msg = (await rlnDecoder.decode(protoResult!))!;
|
||||
|
||||
expect(result.contentTopic).to.eq(TestContentTopic);
|
||||
expect(result.version).to.eq(0);
|
||||
expect(result.payload).to.deep.eq(payload);
|
||||
expect(result.timestamp).to.not.be.undefined;
|
||||
// Validate proof
|
||||
const verifResult = rlnInstance.verifyProof(msg.rateLimitProof!);
|
||||
expect(verifResult).to.be.true;
|
||||
|
||||
const msgV0 = msg as MessageV0;
|
||||
expect(msgV0.contentTopic).to.eq(TestContentTopic);
|
||||
expect(msgV0.version).to.eq(0);
|
||||
expect(msgV0.payload).to.deep.eq(payload);
|
||||
expect(msgV0.timestamp).to.not.be.undefined;
|
||||
});
|
||||
});
|
||||
|
||||
@ -76,11 +76,10 @@ export class RLNDecoder implements Decoder<Message> {
|
||||
}
|
||||
|
||||
async decode(proto: ProtoMessage): Promise<Message | undefined> {
|
||||
// https://github.com/status-im/js-waku/issues/921
|
||||
if (proto.version === undefined) {
|
||||
proto.version = 0;
|
||||
const msg = await this.decoder.decode(proto);
|
||||
if (msg) {
|
||||
msg.rateLimitProof = proto.rateLimitProof;
|
||||
}
|
||||
|
||||
return this.decoder.decode(proto);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
import { assert, expect } from "chai";
|
||||
|
||||
import * as rln from "./index";
|
||||
import * as rln from "./index.js";
|
||||
|
||||
describe("js-rln", () => {
|
||||
it("should verify a proof", async function () {
|
||||
@ -50,13 +50,13 @@ describe("js-rln", () => {
|
||||
|
||||
try {
|
||||
// Modifying the proof so it's invalid
|
||||
const proofBytes = proof.toBytes();
|
||||
proofBytes[7] = 1;
|
||||
proofBytes[8] = 2;
|
||||
proofBytes[9] = 3;
|
||||
proof.proof[0] = 0;
|
||||
proof.proof[1] = 1;
|
||||
proof.proof[2] = 2;
|
||||
proof.proof[3] = 3;
|
||||
|
||||
// verify the proof
|
||||
const verifResult = rlnInstance.verifyProof(proofBytes);
|
||||
const verifResult = rlnInstance.verifyProof(proof);
|
||||
expect(verifResult).to.be.false;
|
||||
} catch (err) {
|
||||
//
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import type { MembershipKey, RateLimitProof, RLNInstance } from "./rln.js";
|
||||
import type { MembershipKey, Proof, RLNInstance } from "./rln.js";
|
||||
|
||||
// reexport the create function, dynamically imported from rln.ts
|
||||
export async function create(): Promise<RLNInstance> {
|
||||
@ -9,4 +9,4 @@ export async function create(): Promise<RLNInstance> {
|
||||
return await rlnModule.create();
|
||||
}
|
||||
|
||||
export { RLNInstance, MembershipKey, RateLimitProof };
|
||||
export { RLNInstance, MembershipKey, Proof };
|
||||
|
||||
36
src/rln.ts
36
src/rln.ts
@ -1,4 +1,5 @@
|
||||
import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
||||
import { RateLimitProof } from "js-waku/lib/interfaces";
|
||||
|
||||
import verificationKey from "./resources/verification_key.json";
|
||||
import * as wc from "./witness_calculator.js";
|
||||
@ -120,7 +121,7 @@ const shareYOffset = shareXOffset + 32;
|
||||
const nullifierOffset = shareYOffset + 32;
|
||||
const rlnIdentifierOffset = nullifierOffset + 32;
|
||||
|
||||
export class RateLimitProof {
|
||||
export class Proof implements RateLimitProof {
|
||||
readonly proof: Uint8Array;
|
||||
readonly merkleRoot: Uint8Array;
|
||||
readonly epoch: Uint8Array;
|
||||
@ -143,18 +144,18 @@ export class RateLimitProof {
|
||||
rlnIdentifierOffset
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
toBytes(): Uint8Array {
|
||||
return concatenate(
|
||||
this.proof,
|
||||
this.merkleRoot,
|
||||
this.epoch,
|
||||
this.shareX,
|
||||
this.shareY,
|
||||
this.nullifier,
|
||||
this.rlnIdentifier
|
||||
);
|
||||
}
|
||||
function proofToBytes(p: RateLimitProof): Uint8Array {
|
||||
return concatenate(
|
||||
p.proof,
|
||||
p.merkleRoot,
|
||||
p.epoch,
|
||||
p.shareX,
|
||||
p.shareY,
|
||||
p.nullifier,
|
||||
p.rlnIdentifier
|
||||
);
|
||||
}
|
||||
|
||||
export class RLNInstance {
|
||||
@ -218,13 +219,16 @@ export class RLNInstance {
|
||||
rlnWitness
|
||||
);
|
||||
|
||||
return new RateLimitProof(proofBytes);
|
||||
return new Proof(proofBytes);
|
||||
}
|
||||
|
||||
verifyProof(proof: RateLimitProof | Uint8Array): boolean {
|
||||
if (proof instanceof RateLimitProof) {
|
||||
proof = proof.toBytes();
|
||||
let pBytes: Uint8Array;
|
||||
if (proof instanceof Uint8Array) {
|
||||
pBytes = proof;
|
||||
} else {
|
||||
pBytes = proofToBytes(proof);
|
||||
}
|
||||
return zerokitRLN.verifyProof(this.zkRLN, proof);
|
||||
return zerokitRLN.verifyProof(this.zkRLN, pBytes);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user