mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-10 01:23:11 +00:00
move to newer version
This commit is contained in:
parent
fae4bea48d
commit
e146f4512d
5072
package-lock.json
generated
5072
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@waku/rln",
|
||||
"version": "0.0.13",
|
||||
"version": "0.0.14",
|
||||
"description": "Rate Limit Nullifier for js-waku",
|
||||
"types": "./dist/index.d.ts",
|
||||
"module": "./dist/index.js",
|
||||
@ -83,7 +83,7 @@
|
||||
"husky": "^7.0.4",
|
||||
"ignore-loader": "^0.1.2",
|
||||
"isomorphic-fetch": "^3.0.0",
|
||||
"js-waku": "^0.29.0-29436ea",
|
||||
"@waku/interfaces": "^0.0.11",
|
||||
"jsdom": "^19.0.0",
|
||||
"jsdom-global": "^3.0.2",
|
||||
"karma": "^6.3.12",
|
||||
@ -127,6 +127,7 @@
|
||||
]
|
||||
},
|
||||
"dependencies": {
|
||||
"@waku/utils": "^0.0.4",
|
||||
"@waku/zerokit-rln-wasm": "^0.0.5",
|
||||
"ethers": "^5.7.2"
|
||||
}
|
||||
|
||||
52
src/codec.ts
52
src/codec.ts
@ -1,23 +1,25 @@
|
||||
import type {
|
||||
IDecodedMessage,
|
||||
IDecoder,
|
||||
IEncoder,
|
||||
IMessage,
|
||||
IProtoMessage,
|
||||
IRateLimitProof,
|
||||
} from "@waku/interfaces";
|
||||
import debug from "debug";
|
||||
import {
|
||||
Decoder,
|
||||
Encoder,
|
||||
Message,
|
||||
ProtoMessage,
|
||||
RateLimitProof,
|
||||
} from "js-waku/lib/interfaces";
|
||||
|
||||
import { RlnMessage, toRLNSignal } from "./message.js";
|
||||
import { MembershipKey, RLNInstance } from "./rln.js";
|
||||
|
||||
const log = debug("waku:rln:encoder");
|
||||
|
||||
export class RLNEncoder implements Encoder {
|
||||
export class RLNEncoder implements IEncoder {
|
||||
public contentTopic: string;
|
||||
public ephemeral = false;
|
||||
private readonly idKey: Uint8Array;
|
||||
|
||||
constructor(
|
||||
private encoder: Encoder,
|
||||
private encoder: IEncoder,
|
||||
private rlnInstance: RLNInstance,
|
||||
private index: number,
|
||||
membershipKey: MembershipKey
|
||||
@ -27,17 +29,13 @@ export class RLNEncoder implements Encoder {
|
||||
this.contentTopic = encoder.contentTopic;
|
||||
}
|
||||
|
||||
async toWire(message: Partial<Message>): Promise<Uint8Array | undefined> {
|
||||
message.contentTopic = this.contentTopic;
|
||||
async toWire(message: IMessage): Promise<Uint8Array | undefined> {
|
||||
message.rateLimitProof = await this.generateProof(message);
|
||||
log("Proof generated", message.rateLimitProof);
|
||||
return this.encoder.toWire(message);
|
||||
}
|
||||
|
||||
async toProtoObj(
|
||||
message: Partial<Message>
|
||||
): Promise<ProtoMessage | undefined> {
|
||||
message.contentTopic = this.contentTopic;
|
||||
async toProtoObj(message: IMessage): Promise<IProtoMessage | undefined> {
|
||||
const protoMessage = await this.encoder.toProtoObj(message);
|
||||
if (!protoMessage) return;
|
||||
|
||||
@ -46,10 +44,8 @@ export class RLNEncoder implements Encoder {
|
||||
return protoMessage;
|
||||
}
|
||||
|
||||
private async generateProof(
|
||||
message: Partial<Message>
|
||||
): Promise<RateLimitProof> {
|
||||
const signal = toRLNSignal(message);
|
||||
private async generateProof(message: IMessage): Promise<IRateLimitProof> {
|
||||
const signal = toRLNSignal(this.contentTopic, message);
|
||||
|
||||
console.time("proof_gen_timer");
|
||||
const proof = await this.rlnInstance.generateRLNProof(
|
||||
@ -63,21 +59,29 @@ export class RLNEncoder implements Encoder {
|
||||
}
|
||||
}
|
||||
|
||||
export class RLNDecoder<T extends Message> implements Decoder<RlnMessage<T>> {
|
||||
constructor(private rlnInstance: RLNInstance, private decoder: Decoder<T>) {}
|
||||
export class RLNDecoder<T extends IDecodedMessage>
|
||||
implements IDecoder<RlnMessage<T>>
|
||||
{
|
||||
constructor(private rlnInstance: RLNInstance, private decoder: IDecoder<T>) {}
|
||||
|
||||
get contentTopic(): string {
|
||||
return this.decoder.contentTopic;
|
||||
}
|
||||
|
||||
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
|
||||
fromWireToProtoObj(bytes: Uint8Array): Promise<IProtoMessage | undefined> {
|
||||
const protoMessage = this.decoder.fromWireToProtoObj(bytes);
|
||||
log("Message decoded", protoMessage);
|
||||
return Promise.resolve(protoMessage);
|
||||
}
|
||||
|
||||
async fromProtoObj(proto: ProtoMessage): Promise<RlnMessage<T> | undefined> {
|
||||
const msg: T | undefined = await this.decoder.fromProtoObj(proto);
|
||||
async fromProtoObj(
|
||||
pubSubTopic: string,
|
||||
proto: IProtoMessage
|
||||
): Promise<RlnMessage<T> | undefined> {
|
||||
const msg: T | undefined = await this.decoder.fromProtoObj(
|
||||
pubSubTopic,
|
||||
proto
|
||||
);
|
||||
if (!msg) return;
|
||||
return new RlnMessage(this.rlnInstance, msg, proto.rateLimitProof);
|
||||
}
|
||||
|
||||
@ -1,24 +1,34 @@
|
||||
import { utils } from "js-waku";
|
||||
import { Message, RateLimitProof } from "js-waku/lib/interfaces";
|
||||
import type {
|
||||
IDecodedMessage,
|
||||
IMessage,
|
||||
IRateLimitProof,
|
||||
} from "@waku/interfaces";
|
||||
import utils from "@waku/utils/bytes";
|
||||
|
||||
import { epochBytesToInt } from "./epoch.js";
|
||||
import { RLNInstance } from "./rln.js";
|
||||
|
||||
export function toRLNSignal(msg: Partial<Message>): Uint8Array {
|
||||
const contentTopicBytes = utils.utf8ToBytes(msg.contentTopic ?? "");
|
||||
export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
||||
const contentTopicBytes = utils.utf8ToBytes(contentTopic ?? "");
|
||||
return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
|
||||
}
|
||||
|
||||
export class RlnMessage<T extends Message> implements Message {
|
||||
export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
|
||||
public pubSubTopic = "";
|
||||
public ephemeral = false;
|
||||
|
||||
constructor(
|
||||
public rlnInstance: RLNInstance,
|
||||
public msg: T,
|
||||
public rateLimitProof: RateLimitProof | undefined
|
||||
public rateLimitProof: IRateLimitProof | undefined
|
||||
) {}
|
||||
|
||||
public verify(): boolean | undefined {
|
||||
return this.rateLimitProof
|
||||
? this.rlnInstance.verifyWithRoots(this.rateLimitProof, toRLNSignal(this)) // this.rlnInstance.verifyRLNProof once issue status-im/nwaku#1248 is fixed
|
||||
? this.rlnInstance.verifyWithRoots(
|
||||
this.rateLimitProof,
|
||||
toRLNSignal(this.msg.contentTopic, this.msg)
|
||||
) // this.rlnInstance.verifyRLNProof once issue status-im/nwaku#1248 is fixed
|
||||
: undefined;
|
||||
}
|
||||
|
||||
@ -26,16 +36,16 @@ export class RlnMessage<T extends Message> implements Message {
|
||||
return this.rateLimitProof
|
||||
? this.rlnInstance.verifyWithNoRoot(
|
||||
this.rateLimitProof,
|
||||
toRLNSignal(this)
|
||||
toRLNSignal(this.msg.contentTopic, this.msg)
|
||||
) // this.rlnInstance.verifyRLNProof once issue status-im/nwaku#1248 is fixed
|
||||
: undefined;
|
||||
}
|
||||
|
||||
get payload(): Uint8Array | undefined {
|
||||
get payload(): Uint8Array {
|
||||
return this.msg.payload;
|
||||
}
|
||||
|
||||
get contentTopic(): string | undefined {
|
||||
get contentTopic(): string {
|
||||
return this.msg.contentTopic;
|
||||
}
|
||||
|
||||
|
||||
15
src/rln.ts
15
src/rln.ts
@ -1,5 +1,5 @@
|
||||
import type { IRateLimitProof } from "@waku/interfaces";
|
||||
import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
||||
import { RateLimitProof } from "js-waku/lib/interfaces";
|
||||
|
||||
import { writeUIntLE } from "./byte_utils.js";
|
||||
import { dateToEpoch, epochIntToBytes } from "./epoch.js";
|
||||
@ -89,7 +89,7 @@ const shareYOffset = shareXOffset + 32;
|
||||
const nullifierOffset = shareYOffset + 32;
|
||||
const rlnIdentifierOffset = nullifierOffset + 32;
|
||||
|
||||
export class Proof implements RateLimitProof {
|
||||
export class Proof implements IRateLimitProof {
|
||||
readonly proof: Uint8Array;
|
||||
readonly merkleRoot: Uint8Array;
|
||||
readonly epoch: Uint8Array;
|
||||
@ -114,7 +114,7 @@ export class Proof implements RateLimitProof {
|
||||
}
|
||||
}
|
||||
|
||||
export function proofToBytes(p: RateLimitProof): Uint8Array {
|
||||
export function proofToBytes(p: IRateLimitProof): Uint8Array {
|
||||
return concatenate(
|
||||
p.proof,
|
||||
p.merkleRoot,
|
||||
@ -175,7 +175,7 @@ export class RLNInstance {
|
||||
index: number,
|
||||
epoch: Uint8Array | Date | undefined,
|
||||
idKey: Uint8Array
|
||||
): Promise<RateLimitProof> {
|
||||
): Promise<IRateLimitProof> {
|
||||
if (epoch == undefined) {
|
||||
epoch = epochIntToBytes(dateToEpoch(new Date()));
|
||||
} else if (epoch instanceof Date) {
|
||||
@ -206,7 +206,10 @@ export class RLNInstance {
|
||||
return new Proof(proofBytes);
|
||||
}
|
||||
|
||||
verifyRLNProof(proof: RateLimitProof | Uint8Array, msg: Uint8Array): boolean {
|
||||
verifyRLNProof(
|
||||
proof: IRateLimitProof | Uint8Array,
|
||||
msg: Uint8Array
|
||||
): boolean {
|
||||
let pBytes: Uint8Array;
|
||||
if (proof instanceof Uint8Array) {
|
||||
pBytes = proof;
|
||||
@ -248,7 +251,7 @@ export class RLNInstance {
|
||||
}
|
||||
|
||||
verifyWithNoRoot(
|
||||
proof: RateLimitProof | Uint8Array,
|
||||
proof: IRateLimitProof | Uint8Array,
|
||||
msg: Uint8Array
|
||||
): boolean {
|
||||
let pBytes: Uint8Array;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user