mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-07 16:13:07 +00:00
move to utils, move to resources
This commit is contained in:
parent
4b8a38c7f8
commit
d763a76515
@ -25,8 +25,8 @@ import {
|
|||||||
RLNDecoder,
|
RLNDecoder,
|
||||||
RLNEncoder,
|
RLNEncoder,
|
||||||
} from "./codec.js";
|
} from "./codec.js";
|
||||||
import { epochBytesToInt } from "./epoch.js";
|
|
||||||
import { RlnMessage } from "./message.js";
|
import { RlnMessage } from "./message.js";
|
||||||
|
import { epochBytesToInt } from "./utils/index.js";
|
||||||
|
|
||||||
import * as rln from "./index.js";
|
import * as rln from "./index.js";
|
||||||
|
|
||||||
|
|||||||
@ -8,8 +8,9 @@ import type {
|
|||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import debug from "debug";
|
import debug from "debug";
|
||||||
|
|
||||||
|
import type { IdentityCredential } from "./identity.js";
|
||||||
import { RlnMessage, toRLNSignal } from "./message.js";
|
import { RlnMessage, toRLNSignal } from "./message.js";
|
||||||
import { IdentityCredential, RLNInstance } from "./rln.js";
|
import { RLNInstance } from "./rln.js";
|
||||||
|
|
||||||
const log = debug("waku:rln:encoder");
|
const log = debug("waku:rln:encoder");
|
||||||
|
|
||||||
|
|||||||
@ -1 +1,2 @@
|
|||||||
export { RLNContract } from "./rln_contract.js";
|
export { RLNContract } from "./rln_contract.js";
|
||||||
|
export * from "./constants.js";
|
||||||
|
|||||||
@ -2,7 +2,7 @@ import chai from "chai";
|
|||||||
import spies from "chai-spies";
|
import spies from "chai-spies";
|
||||||
import * as ethers from "ethers";
|
import * as ethers from "ethers";
|
||||||
|
|
||||||
import * as rln from "./index.js";
|
import * as rln from "../index.js";
|
||||||
|
|
||||||
chai.use(spies);
|
chai.use(spies);
|
||||||
|
|
||||||
|
|||||||
@ -1,12 +1,13 @@
|
|||||||
import { hexToBytes } from "@waku/utils/bytes";
|
import { hexToBytes } from "@waku/utils/bytes";
|
||||||
import { ethers } from "ethers";
|
import { ethers } from "ethers";
|
||||||
|
|
||||||
|
import type { IdentityCredential } from "../identity.js";
|
||||||
|
import type { DecryptedCredentials } from "../keystore/index.js";
|
||||||
|
import type { RLNInstance } from "../rln.js";
|
||||||
|
import { MerkleRootTracker } from "../root_tracker.js";
|
||||||
import { zeroPadLE } from "../utils/index.js";
|
import { zeroPadLE } from "../utils/index.js";
|
||||||
|
|
||||||
import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
|
import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI } from "./constants.js";
|
||||||
import type { DecryptedCredentials } from "./keystore/index.js";
|
|
||||||
import { type IdentityCredential, RLNInstance } from "./rln.js";
|
|
||||||
import { MerkleRootTracker } from "./root_tracker.js";
|
|
||||||
|
|
||||||
type Member = {
|
type Member = {
|
||||||
idCommitment: string;
|
idCommitment: string;
|
||||||
|
|||||||
@ -1,10 +1,10 @@
|
|||||||
import { assert, expect } from "chai";
|
import { assert, expect } from "chai";
|
||||||
|
|
||||||
import * as rln from "./index.js";
|
import { createRLN } from "./create.js";
|
||||||
|
|
||||||
describe("js-rln", () => {
|
describe("js-rln", () => {
|
||||||
it("should verify a proof", async function () {
|
it("should verify a proof", async function () {
|
||||||
const rlnInstance = await rln.createRLN();
|
const rlnInstance = await createRLN();
|
||||||
|
|
||||||
const credential = rlnInstance.generateIdentityCredentials();
|
const credential = rlnInstance.generateIdentityCredentials();
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ describe("js-rln", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
it("should verify a proof with a seeded membership key generation", async function () {
|
it("should verify a proof with a seeded membership key generation", async function () {
|
||||||
const rlnInstance = await rln.createRLN();
|
const rlnInstance = await createRLN();
|
||||||
const seed = "This is a test seed";
|
const seed = "This is a test seed";
|
||||||
const credential = rlnInstance.generateSeededIdentityCredential(seed);
|
const credential = rlnInstance.generateSeededIdentityCredential(seed);
|
||||||
|
|
||||||
@ -115,7 +115,7 @@ describe("js-rln", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should generate the same membership key if the same seed is provided", async function () {
|
it("should generate the same membership key if the same seed is provided", async function () {
|
||||||
const rlnInstance = await rln.createRLN();
|
const rlnInstance = await createRLN();
|
||||||
const seed = "This is a test seed";
|
const seed = "This is a test seed";
|
||||||
const memKeys1 = rlnInstance.generateSeededIdentityCredential(seed);
|
const memKeys1 = rlnInstance.generateSeededIdentityCredential(seed);
|
||||||
const memKeys2 = rlnInstance.generateSeededIdentityCredential(seed);
|
const memKeys2 = rlnInstance.generateSeededIdentityCredential(seed);
|
||||||
27
src/identity.ts
Normal file
27
src/identity.ts
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
import { buildBigIntFromUint8Array } from "./utils/index.js";
|
||||||
|
|
||||||
|
export class IdentityCredential {
|
||||||
|
constructor(
|
||||||
|
public readonly IDTrapdoor: Uint8Array,
|
||||||
|
public readonly IDNullifier: Uint8Array,
|
||||||
|
public readonly IDSecretHash: Uint8Array,
|
||||||
|
public readonly IDCommitment: Uint8Array,
|
||||||
|
public readonly IDCommitmentBigInt: bigint
|
||||||
|
) {}
|
||||||
|
|
||||||
|
static fromBytes(memKeys: Uint8Array): IdentityCredential {
|
||||||
|
const idTrapdoor = memKeys.subarray(0, 32);
|
||||||
|
const idNullifier = memKeys.subarray(32, 64);
|
||||||
|
const idSecretHash = memKeys.subarray(64, 96);
|
||||||
|
const idCommitment = memKeys.subarray(96);
|
||||||
|
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
|
||||||
|
|
||||||
|
return new IdentityCredential(
|
||||||
|
idTrapdoor,
|
||||||
|
idNullifier,
|
||||||
|
idSecretHash,
|
||||||
|
idCommitment,
|
||||||
|
idCommitmentBigInt
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,12 +3,13 @@ import {
|
|||||||
RLN_REGISTRY_ABI,
|
RLN_REGISTRY_ABI,
|
||||||
RLN_STORAGE_ABI,
|
RLN_STORAGE_ABI,
|
||||||
SEPOLIA_CONTRACT,
|
SEPOLIA_CONTRACT,
|
||||||
} from "./constants.js";
|
} from "./contract/index.js";
|
||||||
import { RLNContract } from "./contract/index.js";
|
import { RLNContract } from "./contract/index.js";
|
||||||
import { createRLN } from "./create.js";
|
import { createRLN } from "./create.js";
|
||||||
|
import { IdentityCredential } from "./identity.js";
|
||||||
import { Keystore } from "./keystore/index.js";
|
import { Keystore } from "./keystore/index.js";
|
||||||
import { Proof } from "./proof.js";
|
import { Proof } from "./proof.js";
|
||||||
import { IdentityCredential, RLNInstance } from "./rln.js";
|
import { RLNInstance } from "./rln.js";
|
||||||
import { MerkleRootTracker } from "./root_tracker.js";
|
import { MerkleRootTracker } from "./root_tracker.js";
|
||||||
import { extractMetaMaskSigner } from "./utils/index.js";
|
import { extractMetaMaskSigner } from "./utils/index.js";
|
||||||
|
|
||||||
|
|||||||
@ -7,8 +7,8 @@ chai.use(chaiSubset);
|
|||||||
chai.use(deepEqualInAnyOrder);
|
chai.use(deepEqualInAnyOrder);
|
||||||
chai.use(chaiAsPromised);
|
chai.use(chaiAsPromised);
|
||||||
|
|
||||||
import { buildBigIntFromUint8Array } from "../byte_utils.js";
|
import { IdentityCredential } from "../identity.js";
|
||||||
import { IdentityCredential } from "../rln.js";
|
import { buildBigIntFromUint8Array } from "../utils/bytes.js";
|
||||||
|
|
||||||
import { Keystore } from "./keystore.js";
|
import { Keystore } from "./keystore.js";
|
||||||
import type { MembershipInfo } from "./types.js";
|
import type { MembershipInfo } from "./types.js";
|
||||||
|
|||||||
@ -13,7 +13,7 @@ import {
|
|||||||
import _ from "lodash";
|
import _ from "lodash";
|
||||||
import { v4 as uuidV4 } from "uuid";
|
import { v4 as uuidV4 } from "uuid";
|
||||||
|
|
||||||
import { buildBigIntFromUint8Array } from "../byte_utils.js";
|
import { buildBigIntFromUint8Array } from "../utils/bytes.js";
|
||||||
|
|
||||||
import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
|
import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
|
||||||
import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
|
import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
import type { IdentityCredential } from "../rln.js";
|
import type { IdentityCredential } from "../identity.js";
|
||||||
|
|
||||||
export type MembershipHash = string;
|
export type MembershipHash = string;
|
||||||
export type Sha256Hash = string;
|
export type Sha256Hash = string;
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import type {
|
|||||||
} from "@waku/interfaces";
|
} from "@waku/interfaces";
|
||||||
import * as utils from "@waku/utils/bytes";
|
import * as utils from "@waku/utils/bytes";
|
||||||
|
|
||||||
import { epochBytesToInt } from "./epoch.js";
|
|
||||||
import { RLNInstance } from "./rln.js";
|
import { RLNInstance } from "./rln.js";
|
||||||
|
import { epochBytesToInt } from "./utils/index.js";
|
||||||
|
|
||||||
export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
||||||
const contentTopicBytes = utils.utf8ToBytes(contentTopic ?? "");
|
const contentTopicBytes = utils.utf8ToBytes(contentTopic ?? "");
|
||||||
|
|||||||
46
src/rln.ts
46
src/rln.ts
@ -9,11 +9,14 @@ import init from "@waku/zerokit-rln-wasm";
|
|||||||
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
||||||
import { ethers } from "ethers";
|
import { ethers } from "ethers";
|
||||||
|
|
||||||
import type { RLNDecoder, RLNEncoder } from "./codec.js";
|
import {
|
||||||
import { createRLNDecoder, createRLNEncoder } from "./codec.js";
|
createRLNDecoder,
|
||||||
import { SEPOLIA_CONTRACT } from "./constants.js";
|
createRLNEncoder,
|
||||||
import { RLNContract } from "./contract/index.js";
|
type RLNDecoder,
|
||||||
import { dateToEpoch, epochIntToBytes } from "./epoch.js";
|
type RLNEncoder,
|
||||||
|
} from "./codec.js";
|
||||||
|
import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
|
||||||
|
import { IdentityCredential } from "./identity.js";
|
||||||
import { Keystore } from "./keystore/index.js";
|
import { Keystore } from "./keystore/index.js";
|
||||||
import type {
|
import type {
|
||||||
DecryptedCredentials,
|
DecryptedCredentials,
|
||||||
@ -22,14 +25,15 @@ import type {
|
|||||||
import { KeystoreEntity, Password } from "./keystore/types.js";
|
import { KeystoreEntity, Password } from "./keystore/types.js";
|
||||||
import { Proof, proofToBytes } from "./proof.js";
|
import { Proof, proofToBytes } from "./proof.js";
|
||||||
import verificationKey from "./resources/verification_key.js";
|
import verificationKey from "./resources/verification_key.js";
|
||||||
|
import * as wc from "./resources/witness_calculator.js";
|
||||||
|
import { WitnessCalculator } from "./resources/witness_calculator.js";
|
||||||
import {
|
import {
|
||||||
buildBigIntFromUint8Array,
|
|
||||||
concatenate,
|
concatenate,
|
||||||
|
dateToEpoch,
|
||||||
|
epochIntToBytes,
|
||||||
extractMetaMaskSigner,
|
extractMetaMaskSigner,
|
||||||
writeUIntLE,
|
writeUIntLE,
|
||||||
} from "./utils/index.js";
|
} from "./utils/index.js";
|
||||||
import * as wc from "./witness_calculator.js";
|
|
||||||
import { WitnessCalculator } from "./witness_calculator.js";
|
|
||||||
|
|
||||||
async function loadWitnessCalculator(): Promise<WitnessCalculator> {
|
async function loadWitnessCalculator(): Promise<WitnessCalculator> {
|
||||||
const url = new URL("./resources/rln.wasm", import.meta.url);
|
const url = new URL("./resources/rln.wasm", import.meta.url);
|
||||||
@ -63,32 +67,6 @@ export async function create(): Promise<RLNInstance> {
|
|||||||
return new RLNInstance(zkRLN, witnessCalculator);
|
return new RLNInstance(zkRLN, witnessCalculator);
|
||||||
}
|
}
|
||||||
|
|
||||||
export class IdentityCredential {
|
|
||||||
constructor(
|
|
||||||
public readonly IDTrapdoor: Uint8Array,
|
|
||||||
public readonly IDNullifier: Uint8Array,
|
|
||||||
public readonly IDSecretHash: Uint8Array,
|
|
||||||
public readonly IDCommitment: Uint8Array,
|
|
||||||
public readonly IDCommitmentBigInt: bigint
|
|
||||||
) {}
|
|
||||||
|
|
||||||
static fromBytes(memKeys: Uint8Array): IdentityCredential {
|
|
||||||
const idTrapdoor = memKeys.subarray(0, 32);
|
|
||||||
const idNullifier = memKeys.subarray(32, 64);
|
|
||||||
const idSecretHash = memKeys.subarray(64, 96);
|
|
||||||
const idCommitment = memKeys.subarray(96);
|
|
||||||
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
|
|
||||||
|
|
||||||
return new IdentityCredential(
|
|
||||||
idTrapdoor,
|
|
||||||
idNullifier,
|
|
||||||
idSecretHash,
|
|
||||||
idCommitment,
|
|
||||||
idCommitmentBigInt
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
type StartRLNOptions = {
|
type StartRLNOptions = {
|
||||||
/**
|
/**
|
||||||
* If not set - will extract MetaMask account and get signer from it.
|
* If not set - will extract MetaMask account and get signer from it.
|
||||||
|
|||||||
@ -6,3 +6,4 @@ export {
|
|||||||
zeroPadLE,
|
zeroPadLE,
|
||||||
} from "./bytes.js";
|
} from "./bytes.js";
|
||||||
export { sha256, poseidonHash } from "./hash.js";
|
export { sha256, poseidonHash } from "./hash.js";
|
||||||
|
export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user