move to utils, move to resources

This commit is contained in:
Sasha 2024-02-16 00:50:14 +01:00
parent 4b8a38c7f8
commit d763a76515
No known key found for this signature in database
19 changed files with 61 additions and 51 deletions

View File

@ -25,8 +25,8 @@ import {
RLNDecoder,
RLNEncoder,
} from "./codec.js";
import { epochBytesToInt } from "./epoch.js";
import { RlnMessage } from "./message.js";
import { epochBytesToInt } from "./utils/index.js";
import * as rln from "./index.js";

View File

@ -8,8 +8,9 @@ import type {
} from "@waku/interfaces";
import debug from "debug";
import type { IdentityCredential } from "./identity.js";
import { RlnMessage, toRLNSignal } from "./message.js";
import { IdentityCredential, RLNInstance } from "./rln.js";
import { RLNInstance } from "./rln.js";
const log = debug("waku:rln:encoder");

View File

@ -1 +1,2 @@
export { RLNContract } from "./rln_contract.js";
export * from "./constants.js";

View File

@ -2,7 +2,7 @@ import chai from "chai";
import spies from "chai-spies";
import * as ethers from "ethers";
import * as rln from "./index.js";
import * as rln from "../index.js";
chai.use(spies);

View File

@ -1,12 +1,13 @@
import { hexToBytes } from "@waku/utils/bytes";
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 { 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 = {
idCommitment: string;

View File

@ -1,10 +1,10 @@
import { assert, expect } from "chai";
import * as rln from "./index.js";
import { createRLN } from "./create.js";
describe("js-rln", () => {
it("should verify a proof", async function () {
const rlnInstance = await rln.createRLN();
const rlnInstance = await createRLN();
const credential = rlnInstance.generateIdentityCredentials();
@ -59,7 +59,7 @@ describe("js-rln", () => {
}
});
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 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 () {
const rlnInstance = await rln.createRLN();
const rlnInstance = await createRLN();
const seed = "This is a test seed";
const memKeys1 = rlnInstance.generateSeededIdentityCredential(seed);
const memKeys2 = rlnInstance.generateSeededIdentityCredential(seed);

27
src/identity.ts Normal file
View 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
);
}
}

View File

@ -3,12 +3,13 @@ import {
RLN_REGISTRY_ABI,
RLN_STORAGE_ABI,
SEPOLIA_CONTRACT,
} from "./constants.js";
} from "./contract/index.js";
import { RLNContract } from "./contract/index.js";
import { createRLN } from "./create.js";
import { IdentityCredential } from "./identity.js";
import { Keystore } from "./keystore/index.js";
import { Proof } from "./proof.js";
import { IdentityCredential, RLNInstance } from "./rln.js";
import { RLNInstance } from "./rln.js";
import { MerkleRootTracker } from "./root_tracker.js";
import { extractMetaMaskSigner } from "./utils/index.js";

View File

@ -7,8 +7,8 @@ chai.use(chaiSubset);
chai.use(deepEqualInAnyOrder);
chai.use(chaiAsPromised);
import { buildBigIntFromUint8Array } from "../byte_utils.js";
import { IdentityCredential } from "../rln.js";
import { IdentityCredential } from "../identity.js";
import { buildBigIntFromUint8Array } from "../utils/bytes.js";
import { Keystore } from "./keystore.js";
import type { MembershipInfo } from "./types.js";

View File

@ -13,7 +13,7 @@ import {
import _ from "lodash";
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 { isCredentialValid, isKeystoreValid } from "./schema_validator.js";

View File

@ -1,4 +1,4 @@
import type { IdentityCredential } from "../rln.js";
import type { IdentityCredential } from "../identity.js";
export type MembershipHash = string;
export type Sha256Hash = string;

View File

@ -5,8 +5,8 @@ import type {
} from "@waku/interfaces";
import * as utils from "@waku/utils/bytes";
import { epochBytesToInt } from "./epoch.js";
import { RLNInstance } from "./rln.js";
import { epochBytesToInt } from "./utils/index.js";
export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
const contentTopicBytes = utils.utf8ToBytes(contentTopic ?? "");

View File

@ -9,11 +9,14 @@ import init from "@waku/zerokit-rln-wasm";
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
import { ethers } from "ethers";
import type { RLNDecoder, RLNEncoder } from "./codec.js";
import { createRLNDecoder, createRLNEncoder } from "./codec.js";
import { SEPOLIA_CONTRACT } from "./constants.js";
import { RLNContract } from "./contract/index.js";
import { dateToEpoch, epochIntToBytes } from "./epoch.js";
import {
createRLNDecoder,
createRLNEncoder,
type RLNDecoder,
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 type {
DecryptedCredentials,
@ -22,14 +25,15 @@ import type {
import { KeystoreEntity, Password } from "./keystore/types.js";
import { Proof, proofToBytes } from "./proof.js";
import verificationKey from "./resources/verification_key.js";
import * as wc from "./resources/witness_calculator.js";
import { WitnessCalculator } from "./resources/witness_calculator.js";
import {
buildBigIntFromUint8Array,
concatenate,
dateToEpoch,
epochIntToBytes,
extractMetaMaskSigner,
writeUIntLE,
} from "./utils/index.js";
import * as wc from "./witness_calculator.js";
import { WitnessCalculator } from "./witness_calculator.js";
async function loadWitnessCalculator(): Promise<WitnessCalculator> {
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);
}
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 = {
/**
* If not set - will extract MetaMask account and get signer from it.

View File

@ -6,3 +6,4 @@ export {
zeroPadLE,
} from "./bytes.js";
export { sha256, poseidonHash } from "./hash.js";
export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";