@waku/rln 0.1.1-5d7f77f → 0.1.1-77ba0a6

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.
Files changed (62) hide show
  1. package/README.md +26 -2
  2. package/bundle/assets/rln_wasm_bg-a503e304.wasm +0 -0
  3. package/bundle/index.js +68738 -22572
  4. package/dist/.tsbuildinfo +1 -1
  5. package/dist/byte_utils.d.ts +13 -0
  6. package/dist/byte_utils.js +22 -0
  7. package/dist/byte_utils.js.map +1 -1
  8. package/dist/codec.d.ts +5 -3
  9. package/dist/codec.js +8 -4
  10. package/dist/codec.js.map +1 -1
  11. package/dist/constants.d.ts +2 -2
  12. package/dist/constants.js +62 -9
  13. package/dist/constants.js.map +1 -1
  14. package/dist/create.d.ts +2 -0
  15. package/dist/create.js +8 -0
  16. package/dist/create.js.map +1 -0
  17. package/dist/index.d.ts +5 -3
  18. package/dist/index.js +5 -10
  19. package/dist/index.js.map +1 -1
  20. package/dist/keystore/cipher.d.ts +4 -0
  21. package/dist/keystore/cipher.js +28 -0
  22. package/dist/keystore/cipher.js.map +1 -0
  23. package/dist/keystore/credential_validation_generated.d.ts +8 -0
  24. package/dist/keystore/credential_validation_generated.js +121 -0
  25. package/dist/keystore/credential_validation_generated.js.map +1 -0
  26. package/dist/keystore/index.d.ts +4 -0
  27. package/dist/keystore/index.js +3 -0
  28. package/dist/keystore/index.js.map +1 -0
  29. package/dist/keystore/keystore.d.ts +50 -0
  30. package/dist/keystore/keystore.js +202 -0
  31. package/dist/keystore/keystore.js.map +1 -0
  32. package/dist/keystore/keystore_validation_generated.d.ts +8 -0
  33. package/dist/keystore/keystore_validation_generated.js +75 -0
  34. package/dist/keystore/keystore_validation_generated.js.map +1 -0
  35. package/dist/keystore/schema_validator.d.ts +2 -0
  36. package/dist/keystore/schema_validator.js +18 -0
  37. package/dist/keystore/schema_validator.js.map +1 -0
  38. package/dist/keystore/types.d.ts +29 -0
  39. package/dist/keystore/types.js +2 -0
  40. package/dist/keystore/types.js.map +1 -0
  41. package/dist/message.d.ts +1 -1
  42. package/dist/message.js +1 -1
  43. package/dist/metamask.d.ts +2 -0
  44. package/dist/metamask.js +11 -0
  45. package/dist/metamask.js.map +1 -0
  46. package/dist/rln.d.ts +47 -0
  47. package/dist/rln.js +110 -12
  48. package/dist/rln.js.map +1 -1
  49. package/dist/rln_contract.d.ts +24 -14
  50. package/dist/rln_contract.js +73 -33
  51. package/dist/rln_contract.js.map +1 -1
  52. package/package.json +23 -8
  53. package/src/byte_utils.ts +24 -0
  54. package/src/codec.ts +10 -5
  55. package/src/constants.ts +63 -9
  56. package/src/create.ts +9 -0
  57. package/src/index.ts +13 -11
  58. package/src/message.ts +1 -1
  59. package/src/metamask.ts +16 -0
  60. package/src/rln.ts +194 -13
  61. package/src/rln_contract.ts +127 -52
  62. package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
package/dist/constants.js CHANGED
@@ -1,14 +1,67 @@
1
- export const RLN_ABI = [
2
- "function MEMBERSHIP_DEPOSIT() public view returns(uint256)",
3
- "function register(uint256 pubkey) external payable",
4
- "function withdraw(uint256 secret, uint256 _pubkeyIndex, address payable receiver) external",
5
- "event MemberRegistered(uint256 pubkey, uint256 index)",
6
- "event MemberWithdrawn(uint256 pubkey, uint256 index)",
1
+ // ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnRegistry_Implementation.json#L3
2
+ export const RLN_REGISTRY_ABI = [
3
+ "error IncompatibleStorage()",
4
+ "error IncompatibleStorageIndex()",
5
+ "error NoStorageContractAvailable()",
6
+ "error StorageAlreadyExists(address storageAddress)",
7
+ "event AdminChanged(address previousAdmin, address newAdmin)",
8
+ "event BeaconUpgraded(address indexed beacon)",
9
+ "event Initialized(uint8 version)",
10
+ "event NewStorageContract(uint16 index, address storageAddress)",
11
+ "event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
12
+ "event Upgraded(address indexed implementation)",
13
+ "function forceProgress()",
14
+ "function initialize(address _poseidonHasher)",
15
+ "function newStorage()",
16
+ "function nextStorageIndex() view returns (uint16)",
17
+ "function owner() view returns (address)",
18
+ "function poseidonHasher() view returns (address)",
19
+ "function proxiableUUID() view returns (bytes32)",
20
+ "function register(uint16 storageIndex, uint256 commitment)",
21
+ "function register(uint256[] commitments)",
22
+ "function register(uint16 storageIndex, uint256[] commitments)",
23
+ "function registerStorage(address storageAddress)",
24
+ "function renounceOwnership()",
25
+ "function storages(uint16) view returns (address)",
26
+ "function transferOwnership(address newOwner)",
27
+ "function upgradeTo(address newImplementation)",
28
+ "function upgradeToAndCall(address newImplementation, bytes data) payable",
29
+ "function usingStorageIndex() view returns (uint16)",
30
+ ];
31
+ // ref https://github.com/waku-org/waku-rln-contract/blob/19fded82bca07e7b535b429dc507cfb83f10dfcf/deployments/sepolia/WakuRlnStorage_0.json#L3
32
+ export const RLN_STORAGE_ABI = [
33
+ "constructor(address _poseidonHasher, uint16 _contractIndex)",
34
+ "error DuplicateIdCommitment()",
35
+ "error FullTree()",
36
+ "error InvalidIdCommitment(uint256 idCommitment)",
37
+ "error NotImplemented()",
38
+ "event MemberRegistered(uint256 idCommitment, uint256 index)",
39
+ "event MemberWithdrawn(uint256 idCommitment, uint256 index)",
40
+ "event OwnershipTransferred(address indexed previousOwner, address indexed newOwner)",
41
+ "function DEPTH() view returns (uint256)",
42
+ "function MEMBERSHIP_DEPOSIT() view returns (uint256)",
43
+ "function SET_SIZE() view returns (uint256)",
44
+ "function contractIndex() view returns (uint16)",
45
+ "function deployedBlockNumber() view returns (uint32)",
46
+ "function idCommitmentIndex() view returns (uint256)",
47
+ "function isValidCommitment(uint256 idCommitment) view returns (bool)",
48
+ "function memberExists(uint256) view returns (bool)",
49
+ "function members(uint256) view returns (uint256)",
50
+ "function owner() view returns (address)",
51
+ "function poseidonHasher() view returns (address)",
52
+ "function register(uint256[] idCommitments)",
53
+ "function register(uint256 idCommitment) payable",
54
+ "function renounceOwnership()",
55
+ "function slash(uint256 idCommitment, address receiver, uint256[8] proof) pure",
56
+ "function stakedAmounts(uint256) view returns (uint256)",
57
+ "function transferOwnership(address newOwner)",
58
+ "function verifier() view returns (address)",
59
+ "function withdraw() pure",
60
+ "function withdrawalBalance(address) view returns (uint256)",
7
61
  ];
8
62
  export const SEPOLIA_CONTRACT = {
9
63
  chainId: 11155111,
10
- startBlock: 3193048,
11
- address: "0x9C09146844C1326c2dBC41c451766C7138F88155",
12
- abi: RLN_ABI,
64
+ address: "0xF471d71E9b1455bBF4b85d475afb9BB0954A29c4",
65
+ abi: RLN_REGISTRY_ABI,
13
66
  };
14
67
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG;IACrB,4DAA4D;IAC5D,oDAAoD;IACpD,4FAA4F;IAC5F,uDAAuD;IACvD,sDAAsD;CACvD,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,OAAO,EAAE,QAAQ;IACjB,UAAU,EAAE,OAAO;IACnB,OAAO,EAAE,4CAA4C;IACrD,GAAG,EAAE,OAAO;CACb,CAAC"}
1
+ {"version":3,"file":"constants.js","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA,6JAA6J;AAC7J,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,6BAA6B;IAC7B,kCAAkC;IAClC,oCAAoC;IACpC,oDAAoD;IACpD,6DAA6D;IAC7D,8CAA8C;IAC9C,kCAAkC;IAClC,gEAAgE;IAChE,qFAAqF;IACrF,gDAAgD;IAChD,0BAA0B;IAC1B,8CAA8C;IAC9C,uBAAuB;IACvB,mDAAmD;IACnD,yCAAyC;IACzC,kDAAkD;IAClD,iDAAiD;IACjD,4DAA4D;IAC5D,0CAA0C;IAC1C,+DAA+D;IAC/D,kDAAkD;IAClD,8BAA8B;IAC9B,kDAAkD;IAClD,8CAA8C;IAC9C,+CAA+C;IAC/C,0EAA0E;IAC1E,oDAAoD;CACrD,CAAC;AAEF,+IAA+I;AAC/I,MAAM,CAAC,MAAM,eAAe,GAAG;IAC7B,6DAA6D;IAC7D,+BAA+B;IAC/B,kBAAkB;IAClB,iDAAiD;IACjD,wBAAwB;IACxB,6DAA6D;IAC7D,4DAA4D;IAC5D,qFAAqF;IACrF,yCAAyC;IACzC,sDAAsD;IACtD,4CAA4C;IAC5C,gDAAgD;IAChD,sDAAsD;IACtD,qDAAqD;IACrD,sEAAsE;IACtE,oDAAoD;IACpD,kDAAkD;IAClD,yCAAyC;IACzC,kDAAkD;IAClD,4CAA4C;IAC5C,iDAAiD;IACjD,8BAA8B;IAC9B,+EAA+E;IAC/E,wDAAwD;IACxD,8CAA8C;IAC9C,4CAA4C;IAC5C,0BAA0B;IAC1B,4DAA4D;CAC7D,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,OAAO,EAAE,QAAQ;IACjB,OAAO,EAAE,4CAA4C;IACrD,GAAG,EAAE,gBAAgB;CACtB,CAAC"}
@@ -0,0 +1,2 @@
1
+ import type { RLNInstance } from "./rln.js";
2
+ export declare function createRLN(): Promise<RLNInstance>;
package/dist/create.js ADDED
@@ -0,0 +1,8 @@
1
+ export async function createRLN() {
2
+ // A dependency graph that contains any wasm must all be imported
3
+ // asynchronously. This file does the single async import, so
4
+ // that no one else needs to worry about it again.
5
+ const rlnModule = await import("./rln.js");
6
+ return rlnModule.create();
7
+ }
8
+ //# sourceMappingURL=create.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"create.js","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,KAAK,UAAU,SAAS;IAC7B,iEAAiE;IACjE,6DAA6D;IAC7D,kDAAkD;IAClD,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;IAC3C,OAAO,SAAS,CAAC,MAAM,EAAE,CAAC;AAC5B,CAAC"}
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import { RLNDecoder, RLNEncoder } from "./codec.js";
2
- import { RLN_ABI, SEPOLIA_CONTRACT } from "./constants.js";
2
+ import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI, SEPOLIA_CONTRACT } from "./constants.js";
3
+ import { createRLN } from "./create.js";
4
+ import { Keystore } from "./keystore/index.js";
5
+ import { extractMetaMaskSigner } from "./metamask.js";
3
6
  import { IdentityCredential, Proof, ProofMetadata, RLNInstance } from "./rln.js";
4
7
  import { RLNContract } from "./rln_contract.js";
5
8
  import { MerkleRootTracker } from "./root_tracker.js";
6
- export declare function create(): Promise<RLNInstance>;
7
- export { RLNInstance, IdentityCredential, Proof, ProofMetadata, RLNEncoder, RLNDecoder, MerkleRootTracker, RLNContract, RLN_ABI, SEPOLIA_CONTRACT, };
9
+ export { createRLN, Keystore, RLNInstance, IdentityCredential, Proof, ProofMetadata, RLNEncoder, RLNDecoder, MerkleRootTracker, RLNContract, RLN_STORAGE_ABI, RLN_REGISTRY_ABI, SEPOLIA_CONTRACT, extractMetaMaskSigner, };
package/dist/index.js CHANGED
@@ -1,15 +1,10 @@
1
1
  import { RLNDecoder, RLNEncoder } from "./codec.js";
2
- import { RLN_ABI, SEPOLIA_CONTRACT } from "./constants.js";
2
+ import { RLN_REGISTRY_ABI, RLN_STORAGE_ABI, SEPOLIA_CONTRACT, } from "./constants.js";
3
+ import { createRLN } from "./create.js";
4
+ import { Keystore } from "./keystore/index.js";
5
+ import { extractMetaMaskSigner } from "./metamask.js";
3
6
  import { IdentityCredential, Proof, ProofMetadata, RLNInstance, } from "./rln.js";
4
7
  import { RLNContract } from "./rln_contract.js";
5
8
  import { MerkleRootTracker } from "./root_tracker.js";
6
- // reexport the create function, dynamically imported from rln.ts
7
- export async function create() {
8
- // A dependency graph that contains any wasm must all be imported
9
- // asynchronously. This file does the single async import, so
10
- // that no one else needs to worry about it again.
11
- const rlnModule = await import("./rln.js");
12
- return await rlnModule.create();
13
- }
14
- export { RLNInstance, IdentityCredential, Proof, ProofMetadata, RLNEncoder, RLNDecoder, MerkleRootTracker, RLNContract, RLN_ABI, SEPOLIA_CONTRACT, };
9
+ export { createRLN, Keystore, RLNInstance, IdentityCredential, Proof, ProofMetadata, RLNEncoder, RLNDecoder, MerkleRootTracker, RLNContract, RLN_STORAGE_ABI, RLN_REGISTRY_ABI, SEPOLIA_CONTRACT, extractMetaMaskSigner, };
15
10
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAC3D,OAAO,EACL,kBAAkB,EAClB,KAAK,EACL,aAAa,EACb,WAAW,GACZ,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,iEAAiE;AACjE,MAAM,CAAC,KAAK,UAAU,MAAM;IAC1B,iEAAiE;IACjE,6DAA6D;IAC7D,kDAAkD;IAClD,MAAM,SAAS,GAAG,MAAM,MAAM,CAAC,UAAU,CAAC,CAAC;IAC3C,OAAO,MAAM,SAAS,CAAC,MAAM,EAAE,CAAC;AAClC,CAAC;AAED,OAAO,EACL,WAAW,EACX,kBAAkB,EAClB,KAAK,EACL,aAAa,EACb,UAAU,EACV,UAAU,EACV,iBAAiB,EACjB,WAAW,EACX,OAAO,EACP,gBAAgB,GACjB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,gBAAgB,GACjB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AACxC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EACL,kBAAkB,EAClB,KAAK,EACL,aAAa,EACb,WAAW,GACZ,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAChD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAEtD,OAAO,EACL,SAAS,EACT,QAAQ,EACR,WAAW,EACX,kBAAkB,EAClB,KAAK,EACL,aAAa,EACb,UAAU,EACV,UAAU,EACV,iBAAiB,EACjB,WAAW,EACX,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,qBAAqB,GACtB,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { IKeystore as IEipKeystore } from "@chainsafe/bls-keystore";
2
+ import type { Keccak256Hash, Password } from "./types.js";
3
+ export declare const decryptEipKeystore: (password: Password, eipKeystore: IEipKeystore) => Promise<Uint8Array>;
4
+ export declare const keccak256Checksum: (password: Password, eipKeystore: IEipKeystore) => Promise<Keccak256Hash>;
@@ -0,0 +1,28 @@
1
+ import { cipherDecrypt } from "@chainsafe/bls-keystore/lib/cipher.js";
2
+ import { kdf } from "@chainsafe/bls-keystore/lib/kdf.js";
3
+ import { normalizePassword } from "@chainsafe/bls-keystore/lib/password.js";
4
+ import { keccak256 } from "ethereum-cryptography/keccak";
5
+ import { bytesToHex, concatBytes, hexToBytes, } from "ethereum-cryptography/utils";
6
+ // eipKeystore supports only sha256 checksum so we just make an assumption it is keccak256
7
+ const validateChecksum = async (password, eipKeystore) => {
8
+ const computedChecksum = await keccak256Checksum(password, eipKeystore);
9
+ return computedChecksum === eipKeystore.crypto.checksum.message;
10
+ };
11
+ // decrypt from @chainsafe/bls-keystore supports only sha256
12
+ // but nwaku uses keccak256
13
+ // https://github.com/waku-org/nwaku/blob/25d6e52e3804d15f9b61bc4cc6dd448540c072a1/waku/waku_keystore/keyfile.nim#L367
14
+ export const decryptEipKeystore = async (password, eipKeystore) => {
15
+ const decryptionKey = await kdf(eipKeystore.crypto.kdf, normalizePassword(password));
16
+ const isChecksumValid = await validateChecksum(password, eipKeystore);
17
+ if (!isChecksumValid) {
18
+ throw Error("Password is invalid.");
19
+ }
20
+ return cipherDecrypt(eipKeystore.crypto.cipher, decryptionKey.slice(0, 16));
21
+ };
22
+ export const keccak256Checksum = async (password, eipKeystore) => {
23
+ const key = await kdf(eipKeystore.crypto.kdf, normalizePassword(password));
24
+ const payload = concatBytes(key.slice(16), hexToBytes(eipKeystore.crypto.cipher.message));
25
+ const ciphertext = keccak256(payload);
26
+ return bytesToHex(ciphertext);
27
+ };
28
+ //# sourceMappingURL=cipher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cipher.js","sourceRoot":"","sources":["../../src/keystore/cipher.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,uCAAuC,CAAC;AACtE,OAAO,EAAE,GAAG,EAAE,MAAM,oCAAoC,CAAC;AACzD,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAC5E,OAAO,EAAE,SAAS,EAAE,MAAM,8BAA8B,CAAC;AACzD,OAAO,EACL,UAAU,EACV,WAAW,EACX,UAAU,GACX,MAAM,6BAA6B,CAAC;AAIrC,0FAA0F;AAC1F,MAAM,gBAAgB,GAAG,KAAK,EAC5B,QAAkB,EAClB,WAAyB,EACP,EAAE;IACpB,MAAM,gBAAgB,GAAG,MAAM,iBAAiB,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;IACxE,OAAO,gBAAgB,KAAK,WAAW,CAAC,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC;AAClE,CAAC,CAAC;AAEF,4DAA4D;AAC5D,2BAA2B;AAC3B,sHAAsH;AACtH,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EACrC,QAAkB,EAClB,WAAyB,EACJ,EAAE;IACvB,MAAM,aAAa,GAAG,MAAM,GAAG,CAC7B,WAAW,CAAC,MAAM,CAAC,GAAG,EACtB,iBAAiB,CAAC,QAAQ,CAAC,CAC5B,CAAC;IACF,MAAM,eAAe,GAAG,MAAM,gBAAgB,CAAC,QAAQ,EAAE,WAAW,CAAC,CAAC;IAEtE,IAAI,CAAC,eAAe,EAAE;QACpB,MAAM,KAAK,CAAC,sBAAsB,CAAC,CAAC;KACrC;IAED,OAAO,aAAa,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,KAAK,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;AAC9E,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,EACpC,QAAkB,EAClB,WAAyB,EACD,EAAE;IAC1B,MAAM,GAAG,GAAG,MAAM,GAAG,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,EAAE,iBAAiB,CAAC,QAAQ,CAAC,CAAC,CAAC;IAC3E,MAAM,OAAO,GAAG,WAAW,CACzB,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,EACb,UAAU,CAAC,WAAW,CAAC,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,CAC9C,CAAC;IACF,MAAM,UAAU,GAAG,SAAS,CAAC,OAAO,CAAC,CAAC;IACtC,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC;AAChC,CAAC,CAAC"}
@@ -0,0 +1,8 @@
1
+ export declare const Credential: typeof validate11;
2
+ declare function validate11(data: any, { instancePath, parentData, parentDataProperty, rootData }?: {
3
+ instancePath?: string | undefined;
4
+ parentData: any;
5
+ parentDataProperty: any;
6
+ rootData?: any;
7
+ }): boolean;
8
+ export {};
@@ -0,0 +1,121 @@
1
+ /* eslint eslint-comments/no-unlimited-disable: "off" */
2
+ // This file was generated by /scripts/schema-validation-codegen.ts
3
+ // Do not modify this file by hand.
4
+ /* eslint-disable */
5
+ // @ts-ignore
6
+ "use strict";
7
+ export const Credential = validate11;
8
+ const schema12 = { "type": "object", "properties": { "crypto": { "type": "object", "properties": { "cipher": { "type": "string" }, "cipherparams": { "type": "object" }, "ciphertext": { "type": "string" }, "kdf": { "type": "string" }, "kdfparams": { "type": "object" }, "mac": { "type": "string" } }, "required": ["cipher", "cipherparams", "ciphertext", "kdf", "kdfparams", "mac"] } }, "required": ["crypto"] };
9
+ function validate11(data, { instancePath = "", parentData, parentDataProperty, rootData = data } = {}) { let vErrors = null; let errors = 0; if (errors === 0) {
10
+ if (data && typeof data == "object" && !Array.isArray(data)) {
11
+ let missing0;
12
+ if ((data.crypto === undefined) && (missing0 = "crypto")) {
13
+ validate11.errors = [{ instancePath, schemaPath: "#/required", keyword: "required", params: { missingProperty: missing0 }, message: "must have required property '" + missing0 + "'" }];
14
+ return false;
15
+ }
16
+ else {
17
+ if (data.crypto !== undefined) {
18
+ let data0 = data.crypto;
19
+ const _errs1 = errors;
20
+ if (errors === _errs1) {
21
+ if (data0 && typeof data0 == "object" && !Array.isArray(data0)) {
22
+ let missing1;
23
+ if (((((((data0.cipher === undefined) && (missing1 = "cipher")) || ((data0.cipherparams === undefined) && (missing1 = "cipherparams"))) || ((data0.ciphertext === undefined) && (missing1 = "ciphertext"))) || ((data0.kdf === undefined) && (missing1 = "kdf"))) || ((data0.kdfparams === undefined) && (missing1 = "kdfparams"))) || ((data0.mac === undefined) && (missing1 = "mac"))) {
24
+ validate11.errors = [{ instancePath: instancePath + "/crypto", schemaPath: "#/properties/crypto/required", keyword: "required", params: { missingProperty: missing1 }, message: "must have required property '" + missing1 + "'" }];
25
+ return false;
26
+ }
27
+ else {
28
+ if (data0.cipher !== undefined) {
29
+ const _errs3 = errors;
30
+ if (typeof data0.cipher !== "string") {
31
+ validate11.errors = [{ instancePath: instancePath + "/crypto/cipher", schemaPath: "#/properties/crypto/properties/cipher/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
32
+ return false;
33
+ }
34
+ var valid1 = _errs3 === errors;
35
+ }
36
+ else {
37
+ var valid1 = true;
38
+ }
39
+ if (valid1) {
40
+ if (data0.cipherparams !== undefined) {
41
+ let data2 = data0.cipherparams;
42
+ const _errs5 = errors;
43
+ if (!(data2 && typeof data2 == "object" && !Array.isArray(data2))) {
44
+ validate11.errors = [{ instancePath: instancePath + "/crypto/cipherparams", schemaPath: "#/properties/crypto/properties/cipherparams/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
45
+ return false;
46
+ }
47
+ var valid1 = _errs5 === errors;
48
+ }
49
+ else {
50
+ var valid1 = true;
51
+ }
52
+ if (valid1) {
53
+ if (data0.ciphertext !== undefined) {
54
+ const _errs7 = errors;
55
+ if (typeof data0.ciphertext !== "string") {
56
+ validate11.errors = [{ instancePath: instancePath + "/crypto/ciphertext", schemaPath: "#/properties/crypto/properties/ciphertext/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
57
+ return false;
58
+ }
59
+ var valid1 = _errs7 === errors;
60
+ }
61
+ else {
62
+ var valid1 = true;
63
+ }
64
+ if (valid1) {
65
+ if (data0.kdf !== undefined) {
66
+ const _errs9 = errors;
67
+ if (typeof data0.kdf !== "string") {
68
+ validate11.errors = [{ instancePath: instancePath + "/crypto/kdf", schemaPath: "#/properties/crypto/properties/kdf/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
69
+ return false;
70
+ }
71
+ var valid1 = _errs9 === errors;
72
+ }
73
+ else {
74
+ var valid1 = true;
75
+ }
76
+ if (valid1) {
77
+ if (data0.kdfparams !== undefined) {
78
+ let data5 = data0.kdfparams;
79
+ const _errs11 = errors;
80
+ if (!(data5 && typeof data5 == "object" && !Array.isArray(data5))) {
81
+ validate11.errors = [{ instancePath: instancePath + "/crypto/kdfparams", schemaPath: "#/properties/crypto/properties/kdfparams/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
82
+ return false;
83
+ }
84
+ var valid1 = _errs11 === errors;
85
+ }
86
+ else {
87
+ var valid1 = true;
88
+ }
89
+ if (valid1) {
90
+ if (data0.mac !== undefined) {
91
+ const _errs13 = errors;
92
+ if (typeof data0.mac !== "string") {
93
+ validate11.errors = [{ instancePath: instancePath + "/crypto/mac", schemaPath: "#/properties/crypto/properties/mac/type", keyword: "type", params: { type: "string" }, message: "must be string" }];
94
+ return false;
95
+ }
96
+ var valid1 = _errs13 === errors;
97
+ }
98
+ else {
99
+ var valid1 = true;
100
+ }
101
+ }
102
+ }
103
+ }
104
+ }
105
+ }
106
+ }
107
+ }
108
+ else {
109
+ validate11.errors = [{ instancePath: instancePath + "/crypto", schemaPath: "#/properties/crypto/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
110
+ return false;
111
+ }
112
+ }
113
+ }
114
+ }
115
+ }
116
+ else {
117
+ validate11.errors = [{ instancePath, schemaPath: "#/type", keyword: "type", params: { type: "object" }, message: "must be object" }];
118
+ return false;
119
+ }
120
+ } validate11.errors = vErrors; return errors === 0; }
121
+ //# sourceMappingURL=credential_validation_generated.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"credential_validation_generated.js","sourceRoot":"","sources":["../../src/keystore/credential_validation_generated.ts"],"names":[],"mappings":"AACA,wDAAwD;AACxD,mEAAmE;AACnE,mCAAmC;AAEnC,oBAAoB;AACpB,aAAa;AACb,YAAY,CAAC;AAAA,MAAM,CAAC,MAAM,UAAU,GAAG,UAAU,CAAC;AAAA,MAAM,QAAQ,GAAG,EAAC,MAAM,EAAC,QAAQ,EAAC,YAAY,EAAC,EAAC,QAAQ,EAAC,EAAC,MAAM,EAAC,QAAQ,EAAC,YAAY,EAAC,EAAC,QAAQ,EAAC,EAAC,MAAM,EAAC,QAAQ,EAAC,EAAC,cAAc,EAAC,EAAC,MAAM,EAAC,QAAQ,EAAC,EAAC,YAAY,EAAC,EAAC,MAAM,EAAC,QAAQ,EAAC,EAAC,KAAK,EAAC,EAAC,MA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K,CAAC;KAAC;CAAC,CAAA,UAAU,CAAC,MAAM,GAAG,OAAO,CAAC,CAAA,OAAO,MAAM,KAAK,CAAC,CAAC,CAAA,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { Keystore } from "./keystore.js";
2
+ import type { DecryptedCredentials, EncryptedCredentials } from "./types.js";
3
+ export { Keystore };
4
+ export type { EncryptedCredentials, DecryptedCredentials };
@@ -0,0 +1,3 @@
1
+ import { Keystore } from "./keystore.js";
2
+ export { Keystore };
3
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/keystore/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAGzC,OAAO,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,50 @@
1
+ import type { ICipherModule, IPbkdf2KdfModule } from "@chainsafe/bls-keystore";
2
+ import type { KeystoreEntity, MembershipHash, Password, Sha256Hash } from "./types.js";
3
+ type NwakuCredential = {
4
+ crypto: {
5
+ cipher: ICipherModule["function"];
6
+ cipherparams: ICipherModule["params"];
7
+ ciphertext: ICipherModule["message"];
8
+ kdf: IPbkdf2KdfModule["function"];
9
+ kdfparams: IPbkdf2KdfModule["params"];
10
+ mac: Sha256Hash;
11
+ };
12
+ };
13
+ interface NwakuKeystore {
14
+ application: string;
15
+ version: string;
16
+ appIdentifier: string;
17
+ credentials: {
18
+ [key: MembershipHash]: NwakuCredential;
19
+ };
20
+ }
21
+ type KeystoreCreateOptions = {
22
+ application?: string;
23
+ version?: string;
24
+ appIdentifier?: string;
25
+ };
26
+ export declare class Keystore {
27
+ private data;
28
+ private constructor();
29
+ static create(options?: KeystoreCreateOptions): Keystore;
30
+ static fromString(str: string): undefined | Keystore;
31
+ static fromObject(obj: NwakuKeystore): Keystore;
32
+ addCredential(options: KeystoreEntity, password: Password): Promise<MembershipHash>;
33
+ readCredential(membershipHash: MembershipHash, password: Password): Promise<undefined | KeystoreEntity>;
34
+ removeCredential(hash: MembershipHash): void;
35
+ toString(): string;
36
+ toObject(): NwakuKeystore;
37
+ /**
38
+ * Read array of hashes of current credentials
39
+ * @returns array of keys of credentials in current Keystore
40
+ */
41
+ keys(): string[];
42
+ private static isValidNwakuStore;
43
+ private static fromCredentialToEip;
44
+ private static fromEipToCredential;
45
+ private static fromBytesToIdentity;
46
+ private static fromArraylikeToBytes;
47
+ private static computeMembershipHash;
48
+ private static fromIdentityToBytes;
49
+ }
50
+ export {};
@@ -0,0 +1,202 @@
1
+ import { create as createEipKeystore } from "@chainsafe/bls-keystore";
2
+ import { sha256 } from "ethereum-cryptography/sha256";
3
+ import { bytesToHex, bytesToUtf8, utf8ToBytes, } from "ethereum-cryptography/utils";
4
+ import _ from "lodash";
5
+ import { v4 as uuidV4 } from "uuid";
6
+ import { buildBigIntFromUint8Array } from "../byte_utils.js";
7
+ import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
8
+ import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
9
+ export class Keystore {
10
+ constructor(options) {
11
+ this.data = Object.assign({
12
+ application: "waku-rln-relay",
13
+ appIdentifier: "01234567890abcdef",
14
+ version: "0.2",
15
+ credentials: {},
16
+ }, options);
17
+ }
18
+ static create(options = {}) {
19
+ return new Keystore(options);
20
+ }
21
+ // should be valid JSON string that contains Keystore file
22
+ // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/keyfile.nim#L376
23
+ static fromString(str) {
24
+ try {
25
+ const obj = JSON.parse(str);
26
+ if (!Keystore.isValidNwakuStore(obj)) {
27
+ throw Error("Invalid string, does not match Nwaku Keystore format.");
28
+ }
29
+ return new Keystore(obj);
30
+ }
31
+ catch (err) {
32
+ console.error("Cannot create Keystore from string:", err);
33
+ return;
34
+ }
35
+ }
36
+ static fromObject(obj) {
37
+ if (!Keystore.isValidNwakuStore(obj)) {
38
+ throw Error("Invalid object, does not match Nwaku Keystore format.");
39
+ }
40
+ return new Keystore(obj);
41
+ }
42
+ async addCredential(options, password) {
43
+ const membershipHash = Keystore.computeMembershipHash(options.membership);
44
+ if (this.data.credentials[membershipHash]) {
45
+ throw Error("Credential already exists in the store.");
46
+ }
47
+ // these are not important
48
+ const stubPath = "/stub/path";
49
+ const stubPubkey = new Uint8Array([0]);
50
+ const secret = Keystore.fromIdentityToBytes(options);
51
+ const eipKeystore = await createEipKeystore(password, secret, stubPubkey, stubPath);
52
+ // need to re-compute checksum since nwaku uses keccak256 instead of sha256
53
+ const checksum = await keccak256Checksum(password, eipKeystore);
54
+ const nwakuCredential = Keystore.fromEipToCredential(eipKeystore, checksum);
55
+ this.data.credentials[membershipHash] = nwakuCredential;
56
+ return membershipHash;
57
+ }
58
+ async readCredential(membershipHash, password) {
59
+ const nwakuCredential = this.data.credentials[membershipHash];
60
+ if (!nwakuCredential) {
61
+ return;
62
+ }
63
+ const eipKeystore = Keystore.fromCredentialToEip(nwakuCredential);
64
+ const bytes = await decryptEipKeystore(password, eipKeystore);
65
+ return Keystore.fromBytesToIdentity(bytes);
66
+ }
67
+ removeCredential(hash) {
68
+ if (!this.data.credentials[hash]) {
69
+ return;
70
+ }
71
+ delete this.data.credentials[hash];
72
+ }
73
+ toString() {
74
+ return JSON.stringify(this.data);
75
+ }
76
+ toObject() {
77
+ return this.data;
78
+ }
79
+ /**
80
+ * Read array of hashes of current credentials
81
+ * @returns array of keys of credentials in current Keystore
82
+ */
83
+ keys() {
84
+ return Object.keys(this.toObject().credentials || {});
85
+ }
86
+ static isValidNwakuStore(obj) {
87
+ if (!isKeystoreValid(obj)) {
88
+ return false;
89
+ }
90
+ const areCredentialsValid = Object.values(_.get(obj, "credentials", {}))
91
+ .map((c) => isCredentialValid(c))
92
+ .every((v) => v);
93
+ return areCredentialsValid;
94
+ }
95
+ static fromCredentialToEip(credential) {
96
+ const nwakuCrypto = credential.crypto;
97
+ const eipCrypto = {
98
+ kdf: {
99
+ function: nwakuCrypto.kdf,
100
+ params: nwakuCrypto.kdfparams,
101
+ message: "",
102
+ },
103
+ cipher: {
104
+ function: nwakuCrypto.cipher,
105
+ params: nwakuCrypto.cipherparams,
106
+ message: nwakuCrypto.ciphertext,
107
+ },
108
+ checksum: {
109
+ // @chainsafe/bls-keystore supports only sha256
110
+ // but nwaku uses keccak256
111
+ // https://github.com/waku-org/nwaku/blob/25d6e52e3804d15f9b61bc4cc6dd448540c072a1/waku/waku_keystore/keyfile.nim#L367
112
+ function: "sha256",
113
+ params: {},
114
+ message: nwakuCrypto.mac,
115
+ },
116
+ };
117
+ return {
118
+ version: 4,
119
+ uuid: uuidV4(),
120
+ description: undefined,
121
+ path: "safe to ignore, not important for decrypt",
122
+ pubkey: "safe to ignore, not important for decrypt",
123
+ crypto: eipCrypto,
124
+ };
125
+ }
126
+ static fromEipToCredential(eipKeystore, checksum) {
127
+ const eipCrypto = eipKeystore.crypto;
128
+ const eipKdf = eipCrypto.kdf;
129
+ return {
130
+ crypto: {
131
+ cipher: eipCrypto.cipher.function,
132
+ cipherparams: eipCrypto.cipher.params,
133
+ ciphertext: eipCrypto.cipher.message,
134
+ kdf: eipKdf.function,
135
+ kdfparams: eipKdf.params,
136
+ // @chainsafe/bls-keystore generates only sha256
137
+ // but nwaku uses keccak256
138
+ // https://github.com/waku-org/nwaku/blob/25d6e52e3804d15f9b61bc4cc6dd448540c072a1/waku/waku_keystore/keyfile.nim#L367
139
+ mac: checksum,
140
+ },
141
+ };
142
+ }
143
+ static fromBytesToIdentity(bytes) {
144
+ try {
145
+ const str = bytesToUtf8(bytes);
146
+ const obj = JSON.parse(str);
147
+ // TODO: add runtime validation of nwaku credentials
148
+ return {
149
+ identity: {
150
+ IDCommitment: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", [])),
151
+ IDTrapdoor: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", [])),
152
+ IDNullifier: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", [])),
153
+ IDCommitmentBigInt: buildBigIntFromUint8Array(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []))),
154
+ IDSecretHash: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", [])),
155
+ },
156
+ membership: {
157
+ treeIndex: _.get(obj, "treeIndex"),
158
+ chainId: _.get(obj, "membershipContract.chainId"),
159
+ address: _.get(obj, "membershipContract.address"),
160
+ },
161
+ };
162
+ }
163
+ catch (err) {
164
+ console.error("Cannot parse bytes to Nwaku Credentials:", err);
165
+ return;
166
+ }
167
+ }
168
+ static fromArraylikeToBytes(obj) {
169
+ const bytes = [];
170
+ let index = 0;
171
+ let lastElement = obj[index];
172
+ while (lastElement !== undefined) {
173
+ bytes.push(lastElement);
174
+ index += 1;
175
+ lastElement = obj[index];
176
+ }
177
+ return new Uint8Array(bytes);
178
+ }
179
+ // follows nwaku implementation
180
+ // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
181
+ static computeMembershipHash(info) {
182
+ return bytesToHex(sha256(utf8ToBytes(`${info.chainId}${info.address}${info.treeIndex}`))).toUpperCase();
183
+ }
184
+ // follows nwaku implementation
185
+ // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
186
+ static fromIdentityToBytes(options) {
187
+ return utf8ToBytes(JSON.stringify({
188
+ treeIndex: options.membership.treeIndex,
189
+ identityCredential: {
190
+ idCommitment: options.identity.IDCommitment,
191
+ idNullifier: options.identity.IDNullifier,
192
+ idSecretHash: options.identity.IDSecretHash,
193
+ idTrapdoor: options.identity.IDTrapdoor,
194
+ },
195
+ membershipContract: {
196
+ chainId: options.membership.chainId,
197
+ address: options.membership.address,
198
+ },
199
+ }));
200
+ }
201
+ }
202
+ //# sourceMappingURL=keystore.js.map
@@ -0,0 +1 @@
1
+ {"version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K,GAAG,CAAC,CAAC;QACd,IAAI,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;QAE7B,OAAO,WAAW,KAAK,SAAS,EAAE;YAChC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC;YACxB,KAAK,IAAI,CAAC,CAAC;YACX,WAAW,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC;SAC1B;QAED,OAAO,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC;IAC/B,CAAC;IAED,+BAA+B;IAC/B,6HAA6H;IACrH,MAAM,CAAC,qBAAqB,CAAC,IAAoB;QACvD,OAAO,UAAU,CACf,MAAM,CAAC,WAAW,CAAC,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,SAAS,EAAE,CAAC,CAAC,CACvE,CAAC,WAAW,EAAE,CAAC;IAClB,CAAC;IAED,+BAA+B;IAC/B,4HAA4H;IACpH,MAAM,CAAC,mBAAmB,CAAC,OAAuB;QACxD,OAAO,WAAW,CAChB,IAAI,CAAC,SAAS,CAAC;YACb,SAAS,EAAE,OAAO,CAAC,UAAU,CAAC,SAAS;YACvC,kBAAkB,EAAE;gBAClB,YAAY,EAAE,OAAO,CAAC,QAAQ,CAAC,YAAY;gBAC3C,WAAW,EAAE,OAAO,CAAC,QAAQ,CAAC,WAAW;gBACzC,YAAY,EAAE,OAAO,CAAC,QAAQ,CAAC,YAAY;gBAC3C,UAAU,EAAE,OAAO,CAAC,QAAQ,CAAC,UAAU;aACxC;YACD,kBAAkB,EAAE;gBAClB,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO;gBACnC,OAAO,EAAE,OAAO,CAAC,UAAU,CAAC,OAAO;aACpC;SACF,CAAC,CACH,CAAC;IACJ,CAAC;CACF"}
@@ -0,0 +1,8 @@
1
+ export declare const Keystore: typeof validate11;
2
+ declare function validate11(data: any, { instancePath, parentData, parentDataProperty, rootData }?: {
3
+ instancePath?: string | undefined;
4
+ parentData: any;
5
+ parentDataProperty: any;
6
+ rootData?: any;
7
+ }): boolean;
8
+ export {};