@waku/rln 0.0.8-b5f0cbb → 0.0.9
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/package.json +3 -3
- package/src/codec.ts +26 -22
- package/src/message.ts +8 -2
- package/src/rln.ts +11 -4
- package/bundle/assets/rln-fb4d7b4b.wasm +0 -0
- package/bundle/assets/rln_final-a641c06e.zkey +0 -0
- package/bundle/assets/rln_wasm_bg-0185b546.wasm +0 -0
- package/bundle/index.js +0 -47344
- package/dist/.tsbuildinfo +0 -1
- package/dist/byte_utils.d.ts +0 -1
- package/dist/byte_utils.js +0 -24
- package/dist/byte_utils.js.map +0 -1
- package/dist/codec.d.ts +0 -21
- package/dist/codec.js +0 -57
- package/dist/codec.js.map +0 -1
- package/dist/epoch.d.ts +0 -3
- package/dist/epoch.js +0 -16
- package/dist/epoch.js.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.js +0 -12
- package/dist/index.js.map +0 -1
- package/dist/message.d.ts +0 -13
- package/dist/message.js +0 -29
- package/dist/message.js.map +0 -1
- package/dist/resources/rln.wasm +0 -0
- package/dist/resources/rln_final.zkey +0 -0
- package/dist/resources/verification_key.d.ts +0 -12
- package/dist/resources/verification_key.js +0 -121
- package/dist/resources/verification_key.js.map +0 -1
- package/dist/rln.d.ts +0 -33
- package/dist/rln.js +0 -135
- package/dist/rln.js.map +0 -1
- package/dist/witness_calculator.d.ts +0 -16
- package/dist/witness_calculator.js +0 -291
- package/dist/witness_calculator.js.map +0 -1
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/rln",
|
3
|
-
"version": "0.0.
|
3
|
+
"version": "0.0.9",
|
4
4
|
"description": "Rate Limit Nullifier for js-waku",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -81,7 +81,7 @@
|
|
81
81
|
"husky": "^7.0.4",
|
82
82
|
"ignore-loader": "^0.1.2",
|
83
83
|
"isomorphic-fetch": "^3.0.0",
|
84
|
-
"js-waku": "^0.29.0-
|
84
|
+
"js-waku": "^0.29.0-29436ea",
|
85
85
|
"jsdom": "^19.0.0",
|
86
86
|
"jsdom-global": "^3.0.2",
|
87
87
|
"karma": "^6.3.12",
|
@@ -125,6 +125,6 @@
|
|
125
125
|
]
|
126
126
|
},
|
127
127
|
"dependencies": {
|
128
|
-
"@waku/zerokit-rln-wasm": "^0.0.
|
128
|
+
"@waku/zerokit-rln-wasm": "^0.0.3"
|
129
129
|
}
|
130
130
|
}
|
package/src/codec.ts
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
import debug from "debug";
|
2
|
-
import { proto_message, utils } from "js-waku";
|
3
2
|
import {
|
4
3
|
Decoder,
|
5
4
|
Encoder,
|
6
5
|
Message,
|
7
6
|
ProtoMessage,
|
7
|
+
RateLimitProof,
|
8
8
|
} from "js-waku/lib/interfaces";
|
9
9
|
|
10
|
-
import { RlnMessage } from "./message.js";
|
10
|
+
import { RlnMessage, toRLNSignal } from "./message.js";
|
11
11
|
import { MembershipKey, RLNInstance } from "./rln.js";
|
12
12
|
|
13
13
|
const log = debug("waku:message:rln-encoder");
|
@@ -27,30 +27,39 @@ export class RLNEncoder implements Encoder {
|
|
27
27
|
this.contentTopic = encoder.contentTopic;
|
28
28
|
}
|
29
29
|
|
30
|
-
async
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
async toWire(message: Partial<Message>): Promise<Uint8Array | undefined> {
|
31
|
+
message.contentTopic = this.contentTopic;
|
32
|
+
message.rateLimitProof = await this.generateProof(message);
|
33
|
+
|
34
|
+
return this.encoder.toWire(message);
|
34
35
|
}
|
35
36
|
|
36
|
-
async
|
37
|
-
|
37
|
+
async toProtoObj(
|
38
|
+
message: Partial<Message>
|
39
|
+
): Promise<ProtoMessage | undefined> {
|
40
|
+
message.contentTopic = this.contentTopic;
|
41
|
+
const protoMessage = await this.encoder.toProtoObj(message);
|
38
42
|
if (!protoMessage) return;
|
39
43
|
|
44
|
+
protoMessage.rateLimitProof = await this.generateProof(message);
|
45
|
+
|
46
|
+
return protoMessage;
|
47
|
+
}
|
48
|
+
|
49
|
+
private async generateProof(
|
50
|
+
message: Partial<Message>
|
51
|
+
): Promise<RateLimitProof> {
|
40
52
|
const signal = toRLNSignal(message);
|
41
53
|
|
42
54
|
console.time("proof_gen_timer");
|
43
|
-
const proof = await this.rlnInstance.
|
55
|
+
const proof = await this.rlnInstance.generateRLNProof(
|
44
56
|
signal,
|
45
57
|
this.index,
|
46
58
|
message.timestamp,
|
47
59
|
this.idKey
|
48
60
|
);
|
49
61
|
console.timeEnd("proof_gen_timer");
|
50
|
-
|
51
|
-
protoMessage.rateLimitProof = proof;
|
52
|
-
|
53
|
-
return protoMessage;
|
62
|
+
return proof;
|
54
63
|
}
|
55
64
|
}
|
56
65
|
|
@@ -61,20 +70,15 @@ export class RLNDecoder<T extends Message> implements Decoder<RlnMessage<T>> {
|
|
61
70
|
return this.decoder.contentTopic;
|
62
71
|
}
|
63
72
|
|
64
|
-
|
65
|
-
const protoMessage =
|
73
|
+
fromWireToProtoObj(bytes: Uint8Array): Promise<ProtoMessage | undefined> {
|
74
|
+
const protoMessage = this.decoder.fromWireToProtoObj(bytes);
|
66
75
|
log("Message decoded", protoMessage);
|
67
76
|
return Promise.resolve(protoMessage);
|
68
77
|
}
|
69
78
|
|
70
|
-
async
|
71
|
-
const msg: T | undefined = await this.decoder.
|
79
|
+
async fromProtoObj(proto: ProtoMessage): Promise<RlnMessage<T> | undefined> {
|
80
|
+
const msg: T | undefined = await this.decoder.fromProtoObj(proto);
|
72
81
|
if (!msg) return;
|
73
82
|
return new RlnMessage(this.rlnInstance, msg, proto.rateLimitProof);
|
74
83
|
}
|
75
84
|
}
|
76
|
-
|
77
|
-
function toRLNSignal(msg: Message): Uint8Array {
|
78
|
-
const contentTopicBytes = utils.utf8ToBytes(msg.contentTopic ?? "");
|
79
|
-
return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
|
80
|
-
}
|
package/src/message.ts
CHANGED
@@ -1,18 +1,24 @@
|
|
1
|
+
import { utils } from "js-waku";
|
1
2
|
import { Message, RateLimitProof } from "js-waku/lib/interfaces";
|
2
3
|
|
3
4
|
import { epochBytesToInt } from "./epoch.js";
|
4
5
|
import { RLNInstance } from "./rln.js";
|
5
6
|
|
7
|
+
export function toRLNSignal(msg: Partial<Message>): Uint8Array {
|
8
|
+
const contentTopicBytes = utils.utf8ToBytes(msg.contentTopic ?? "");
|
9
|
+
return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
|
10
|
+
}
|
11
|
+
|
6
12
|
export class RlnMessage<T extends Message> implements Message {
|
7
13
|
constructor(
|
8
14
|
public rlnInstance: RLNInstance,
|
9
15
|
public msg: T,
|
10
|
-
public rateLimitProof
|
16
|
+
public rateLimitProof: RateLimitProof | undefined
|
11
17
|
) {}
|
12
18
|
|
13
19
|
public verify(): boolean | undefined {
|
14
20
|
return this.rateLimitProof
|
15
|
-
? this.rlnInstance.
|
21
|
+
? this.rlnInstance.verifyRLNProof(this.rateLimitProof, toRLNSignal(this))
|
16
22
|
: undefined;
|
17
23
|
}
|
18
24
|
|
package/src/rln.ts
CHANGED
@@ -102,7 +102,7 @@ export class Proof implements RateLimitProof {
|
|
102
102
|
}
|
103
103
|
}
|
104
104
|
|
105
|
-
function proofToBytes(p: RateLimitProof): Uint8Array {
|
105
|
+
export function proofToBytes(p: RateLimitProof): Uint8Array {
|
106
106
|
return concatenate(
|
107
107
|
p.proof,
|
108
108
|
p.merkleRoot,
|
@@ -145,7 +145,7 @@ export class RLNInstance {
|
|
145
145
|
return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg);
|
146
146
|
}
|
147
147
|
|
148
|
-
async
|
148
|
+
async generateRLNProof(
|
149
149
|
msg: Uint8Array,
|
150
150
|
index: number,
|
151
151
|
epoch: Uint8Array | Date | undefined,
|
@@ -181,13 +181,20 @@ export class RLNInstance {
|
|
181
181
|
return new Proof(proofBytes);
|
182
182
|
}
|
183
183
|
|
184
|
-
|
184
|
+
verifyRLNProof(proof: RateLimitProof | Uint8Array, msg: Uint8Array): boolean {
|
185
185
|
let pBytes: Uint8Array;
|
186
186
|
if (proof instanceof Uint8Array) {
|
187
187
|
pBytes = proof;
|
188
188
|
} else {
|
189
189
|
pBytes = proofToBytes(proof);
|
190
190
|
}
|
191
|
-
|
191
|
+
|
192
|
+
// calculate message length
|
193
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
194
|
+
|
195
|
+
return zerokitRLN.verifyRLNProof(
|
196
|
+
this.zkRLN,
|
197
|
+
concatenate(pBytes, msgLen, msg)
|
198
|
+
);
|
192
199
|
}
|
193
200
|
}
|
Binary file
|
Binary file
|
Binary file
|