@dorafactory/maci-sdk 0.0.34 → 0.0.36

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 (83) hide show
  1. package/dist/index.d.ts +14 -2244
  2. package/dist/index.js +748 -363
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +722 -365
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/libs/const.d.ts +118 -0
  7. package/dist/libs/contract/config.d.ts +29 -0
  8. package/dist/libs/contract/contract.d.ts +57 -0
  9. package/dist/libs/contract/index.d.ts +1 -0
  10. package/dist/libs/contract/ts/AMaci.client.d.ts +216 -0
  11. package/dist/libs/contract/ts/AMaci.types.d.ts +221 -0
  12. package/dist/libs/contract/ts/Maci.client.d.ts +180 -0
  13. package/dist/libs/contract/ts/Maci.types.d.ts +226 -0
  14. package/dist/libs/contract/ts/OracleMaci.client.d.ts +206 -0
  15. package/dist/libs/contract/ts/OracleMaci.types.d.ts +263 -0
  16. package/dist/libs/contract/ts/Registry.client.d.ts +128 -0
  17. package/dist/libs/contract/ts/Registry.types.d.ts +110 -0
  18. package/dist/libs/contract/types.d.ts +39 -0
  19. package/dist/libs/contract/utils.d.ts +63 -0
  20. package/dist/libs/contract/vars.d.ts +63 -0
  21. package/dist/libs/crypto/babyjub.d.ts +73 -0
  22. package/dist/libs/crypto/bigintUtils.d.ts +6 -0
  23. package/dist/libs/crypto/constants.d.ts +3 -0
  24. package/dist/libs/crypto/hashing.d.ts +69 -0
  25. package/dist/libs/crypto/index.d.ts +9 -0
  26. package/dist/libs/crypto/keys.d.ts +74 -0
  27. package/dist/libs/crypto/sign.d.ts +9 -0
  28. package/dist/libs/crypto/tree.d.ts +23 -0
  29. package/dist/libs/crypto/types.d.ts +55 -0
  30. package/dist/libs/errors/index.d.ts +28 -0
  31. package/dist/libs/errors/types.d.ts +19 -0
  32. package/dist/libs/http/http.d.ts +16 -0
  33. package/dist/libs/http/index.d.ts +1 -0
  34. package/dist/libs/index.d.ts +7 -0
  35. package/dist/libs/indexer/index.d.ts +1 -0
  36. package/dist/libs/indexer/indexer.d.ts +152 -0
  37. package/dist/libs/indexer/types.d.ts +7 -0
  38. package/dist/libs/maci/index.d.ts +1 -0
  39. package/dist/libs/maci/maci.d.ts +182 -0
  40. package/dist/libs/maci/types.d.ts +6 -0
  41. package/dist/libs/oracle-certificate/index.d.ts +1 -0
  42. package/dist/libs/oracle-certificate/oracle-certificate.d.ts +9 -0
  43. package/dist/libs/oracle-certificate/types.d.ts +40 -0
  44. package/dist/libs/query/account.d.ts +7 -0
  45. package/dist/libs/query/circuit.d.ts +8 -0
  46. package/dist/libs/query/event.d.ts +7 -0
  47. package/dist/libs/query/index.d.ts +7 -0
  48. package/dist/libs/query/operator.d.ts +11 -0
  49. package/dist/libs/query/proof.d.ts +7 -0
  50. package/dist/libs/query/round.d.ts +18 -0
  51. package/dist/libs/query/transaction.d.ts +9 -0
  52. package/dist/maci.d.ts +190 -0
  53. package/dist/types/index.d.ts +383 -0
  54. package/dist/utils/index.d.ts +7 -0
  55. package/package.json +18 -24
  56. package/src/index.ts +1 -3
  57. package/src/libs/contract/contract.ts +14 -13
  58. package/src/libs/contract/types.ts +3 -3
  59. package/src/libs/crypto/babyjub.ts +132 -0
  60. package/src/libs/crypto/bigintUtils.ts +31 -0
  61. package/src/libs/crypto/constants.ts +22 -0
  62. package/src/libs/crypto/hashing.ts +167 -0
  63. package/src/libs/crypto/index.ts +9 -0
  64. package/src/libs/{circom/circomlib.ts → crypto/keys.ts} +104 -114
  65. package/src/libs/crypto/sign.ts +91 -0
  66. package/src/libs/crypto/tree.ts +176 -0
  67. package/src/libs/crypto/types.ts +101 -0
  68. package/src/libs/index.ts +1 -1
  69. package/src/libs/maci/maci.ts +28 -29
  70. package/src/maci.ts +140 -100
  71. package/src/types/index.ts +7 -0
  72. package/src/utils/index.ts +22 -22
  73. package/dist/browser.d.mts +0 -2243
  74. package/dist/browser.d.ts +0 -2243
  75. package/dist/browser.js +0 -31635
  76. package/dist/browser.js.map +0 -1
  77. package/dist/browser.mjs +0 -31646
  78. package/dist/browser.mjs.map +0 -1
  79. package/dist/index.d.mts +0 -2244
  80. package/src/browser.ts +0 -17
  81. package/src/libs/circom/index.ts +0 -98
  82. package/src/libs/circom/types.ts +0 -8
  83. package/src/polyfills/browser-polyfills.ts +0 -9
package/dist/index.mjs CHANGED
@@ -533,30 +533,6 @@ function hexToDecimalString(hexString) {
533
533
  const decimalString = BigInt("0x" + hexString).toString(10);
534
534
  return decimalString;
535
535
  }
536
- function padWithZerosIfNeeded(inputString) {
537
- if (inputString.length === 64) {
538
- return inputString;
539
- } else if (inputString.length < 64) {
540
- const zerosToAdd = 64 - inputString.length;
541
- const zeroPadding = "0".repeat(zerosToAdd);
542
- return zeroPadding + inputString;
543
- }
544
- throw new Error("Invalid input string length");
545
- }
546
- function decompressPublicKey(compressedPubkey) {
547
- const x = compressedPubkey.slice(0, 64);
548
- const y = compressedPubkey.slice(64);
549
- return {
550
- x: hexToDecimalString(x),
551
- y: hexToDecimalString(y)
552
- };
553
- }
554
- function compressPublicKey(decompressedPubkey) {
555
- const x = decompressedPubkey[0];
556
- const y = decompressedPubkey[1];
557
- const compressedPubkey = padWithZerosIfNeeded(x.toString(16)) + padWithZerosIfNeeded(y.toString(16));
558
- return compressedPubkey;
559
- }
560
536
 
561
537
  // src/libs/query/operator.ts
562
538
  var Operator = class {
@@ -1122,9 +1098,9 @@ var Round = class {
1122
1098
  }
1123
1099
  }
1124
1100
  const results = JSON.parse(response.data.round.results);
1125
- const votes = results.map((r) => ({
1126
- v: Number(r.slice(0, -24)),
1127
- v2: Number(r.slice(-24))
1101
+ const votes = results.map((r2) => ({
1102
+ v: Number(r2.slice(0, -24)),
1103
+ v2: Number(r2.slice(-24))
1128
1104
  }));
1129
1105
  const totalVotes = votes.reduce(
1130
1106
  (s, c) => ({
@@ -4193,6 +4169,585 @@ function getAMaciRoundCircuitFee(maxVoter, maxOption) {
4193
4169
  return requiredFee;
4194
4170
  }
4195
4171
 
4172
+ // src/libs/crypto/keys.ts
4173
+ import { randomBytes as randomBytes2 } from "crypto";
4174
+
4175
+ // src/libs/crypto/bigintUtils.ts
4176
+ var stringizing = (o, path = []) => {
4177
+ if (path.includes(o)) {
4178
+ throw new Error("loop nesting!");
4179
+ }
4180
+ const newPath = [...path, o];
4181
+ if (Array.isArray(o)) {
4182
+ return o.map((item) => stringizing(item, newPath));
4183
+ } else if (typeof o === "object") {
4184
+ const output = {};
4185
+ for (const key in o) {
4186
+ output[key] = stringizing(o[key], newPath);
4187
+ }
4188
+ return output;
4189
+ } else {
4190
+ return o.toString();
4191
+ }
4192
+ };
4193
+ var bigInt2Buffer = (i) => {
4194
+ let hex = i.toString(16);
4195
+ if (hex.length % 2 === 1) {
4196
+ hex = "0" + hex;
4197
+ }
4198
+ return Buffer.from(hex, "hex");
4199
+ };
4200
+
4201
+ // src/libs/crypto/keys.ts
4202
+ import { poseidonEncrypt } from "@zk-kit/poseidon-cipher";
4203
+ import * as BabyJub from "@zk-kit/baby-jubjub";
4204
+ import {
4205
+ derivePublicKey,
4206
+ signMessage,
4207
+ deriveSecretScalar
4208
+ } from "@zk-kit/eddsa-poseidon";
4209
+ import { solidityPackedSha256 as solidityPackedSha2562 } from "ethers";
4210
+ import { mulPointEscalar as mulPointEscalar2 } from "@zk-kit/baby-jubjub";
4211
+ import { packPublicKey, unpackPublicKey } from "@zk-kit/eddsa-poseidon";
4212
+
4213
+ // src/libs/crypto/babyjub.ts
4214
+ import assert2 from "assert";
4215
+ import { randomBytes } from "crypto";
4216
+
4217
+ // src/libs/crypto/constants.ts
4218
+ import { r } from "@zk-kit/baby-jubjub";
4219
+ import { keccak256, toUtf8Bytes } from "ethers";
4220
+ import assert from "assert";
4221
+ var SNARK_FIELD_SIZE = r;
4222
+ var NOTHING_UP_MY_SLEEVE = BigInt(keccak256(toUtf8Bytes("Maci"))) % SNARK_FIELD_SIZE;
4223
+ assert(
4224
+ NOTHING_UP_MY_SLEEVE === BigInt(
4225
+ "8370432830353022751713833565135785980866757267633941821328460903436894336785"
4226
+ )
4227
+ );
4228
+ var PAD_KEY_HASH = BigInt(
4229
+ "1309255631273308531193241901289907343161346846555918942743921933037802809814"
4230
+ );
4231
+
4232
+ // src/libs/crypto/babyjub.ts
4233
+ var G1Point = class {
4234
+ /**
4235
+ * Create a new instance of G1Point
4236
+ * @param x the x coordinate
4237
+ * @param y the y coordinate
4238
+ */
4239
+ constructor(x, y) {
4240
+ assert2(x < SNARK_FIELD_SIZE && x >= 0, "G1Point x out of range");
4241
+ assert2(y < SNARK_FIELD_SIZE && y >= 0, "G1Point y out of range");
4242
+ this.x = x;
4243
+ this.y = y;
4244
+ }
4245
+ /**
4246
+ * Check whether two points are equal
4247
+ * @param pt the point to compare with
4248
+ * @returns whether they are equal or not
4249
+ */
4250
+ equals(pt) {
4251
+ return this.x === pt.x && this.y === pt.y;
4252
+ }
4253
+ /**
4254
+ * Return the point as a contract param in the form of an object
4255
+ * @returns the point as a contract param
4256
+ */
4257
+ asContractParam() {
4258
+ return {
4259
+ x: this.x.toString(),
4260
+ y: this.y.toString()
4261
+ };
4262
+ }
4263
+ };
4264
+ var G2Point = class {
4265
+ /**
4266
+ * Create a new instance of G2Point
4267
+ * @param x the x coordinate
4268
+ * @param y the y coordinate
4269
+ */
4270
+ constructor(x, y) {
4271
+ this.checkPointsRange(x, "x");
4272
+ this.checkPointsRange(y, "y");
4273
+ this.x = x;
4274
+ this.y = y;
4275
+ }
4276
+ /**
4277
+ * Check whether two points are equal
4278
+ * @param pt the point to compare with
4279
+ * @returns whether they are equal or not
4280
+ */
4281
+ equals(pt) {
4282
+ return this.x[0] === pt.x[0] && this.x[1] === pt.x[1] && this.y[0] === pt.y[0] && this.y[1] === pt.y[1];
4283
+ }
4284
+ /**
4285
+ * Return the point as a contract param in the form of an object
4286
+ * @returns the point as a contract param
4287
+ */
4288
+ asContractParam() {
4289
+ return {
4290
+ x: this.x.map((n) => n.toString()),
4291
+ y: this.y.map((n) => n.toString())
4292
+ };
4293
+ }
4294
+ /**
4295
+ * Check whether the points are in range
4296
+ * @param x the x coordinate
4297
+ * @param type the type of the coordinate
4298
+ */
4299
+ checkPointsRange(x, type) {
4300
+ assert2(
4301
+ x.every((n) => n < SNARK_FIELD_SIZE && n >= 0),
4302
+ `G2Point ${type} out of range`
4303
+ );
4304
+ }
4305
+ };
4306
+ var genRandomBabyJubValue = () => {
4307
+ const min = BigInt("6350874878119819312338956282401532410528162663560392320966563075034087161851");
4308
+ let privKey = SNARK_FIELD_SIZE;
4309
+ do {
4310
+ const rand = BigInt(`0x${randomBytes(32).toString("hex")}`);
4311
+ if (rand >= min) {
4312
+ privKey = rand % SNARK_FIELD_SIZE;
4313
+ }
4314
+ } while (privKey >= SNARK_FIELD_SIZE);
4315
+ return privKey;
4316
+ };
4317
+
4318
+ // src/libs/crypto/hashing.ts
4319
+ import { poseidonPerm } from "@zk-kit/poseidon-cipher";
4320
+ import { solidityPackedSha256 } from "ethers";
4321
+ import assert3 from "assert";
4322
+ var sha256Hash = (input) => {
4323
+ const types = [];
4324
+ input.forEach(() => {
4325
+ types.push("uint256");
4326
+ });
4327
+ return BigInt(
4328
+ solidityPackedSha256(
4329
+ types,
4330
+ input.map((x) => x.toString())
4331
+ )
4332
+ ) % SNARK_FIELD_SIZE;
4333
+ };
4334
+ var poseidon = (inputs) => poseidonPerm([BigInt(0), ...inputs.map((x) => BigInt(x))])[0];
4335
+ var poseidonT3 = (inputs) => {
4336
+ assert3(inputs.length === 2);
4337
+ return poseidon(inputs);
4338
+ };
4339
+ var poseidonT4 = (inputs) => {
4340
+ assert3(inputs.length === 3);
4341
+ return poseidon(inputs);
4342
+ };
4343
+ var poseidonT5 = (inputs) => {
4344
+ assert3(inputs.length === 4);
4345
+ return poseidon(inputs);
4346
+ };
4347
+ var poseidonT6 = (inputs) => {
4348
+ assert3(inputs.length === 5);
4349
+ return poseidon(inputs);
4350
+ };
4351
+ var hashLeftRight = (left, right) => poseidonT3([left, right]);
4352
+ var funcs = {
4353
+ 2: poseidonT3,
4354
+ 3: poseidonT4,
4355
+ 4: poseidonT5,
4356
+ 5: poseidonT6
4357
+ };
4358
+ var hashN = (numElements, elements) => {
4359
+ const elementLength = elements.length;
4360
+ if (elements.length > numElements) {
4361
+ throw new TypeError(
4362
+ `the length of the elements array should be at most ${numElements}; got ${elements.length}`
4363
+ );
4364
+ }
4365
+ const elementsPadded = elements.slice();
4366
+ if (elementLength < numElements) {
4367
+ for (let i = elementLength; i < numElements; i += 1) {
4368
+ elementsPadded.push(BigInt(0));
4369
+ }
4370
+ }
4371
+ return funcs[numElements](elementsPadded);
4372
+ };
4373
+ var hashLeanIMT = (a, b) => hashN(2, [a, b]);
4374
+ var hash2 = (elements) => hashN(2, elements);
4375
+ var hash3 = (elements) => hashN(3, elements);
4376
+ var hash4 = (elements) => hashN(4, elements);
4377
+ var hash5 = (elements) => hashN(5, elements);
4378
+ var hash12 = (elements) => {
4379
+ const max = 12;
4380
+ const elementLength = elements.length;
4381
+ if (elementLength > max) {
4382
+ throw new TypeError(
4383
+ `the length of the elements array should be at most ${max}; got ${elements.length}`
4384
+ );
4385
+ }
4386
+ const elementsPadded = elements.slice();
4387
+ if (elementLength < max) {
4388
+ for (let i = elementLength; i < max; i += 1) {
4389
+ elementsPadded.push(BigInt(0));
4390
+ }
4391
+ }
4392
+ return poseidonT5([
4393
+ poseidonT6(elementsPadded.slice(0, 5)),
4394
+ poseidonT6(elementsPadded.slice(5, 10)),
4395
+ elementsPadded[10],
4396
+ elementsPadded[11]
4397
+ ]);
4398
+ };
4399
+ var hashOne = (preImage) => poseidonT3([preImage, BigInt(0)]);
4400
+
4401
+ // src/libs/crypto/tree.ts
4402
+ var Tree = class _Tree {
4403
+ constructor(degree, depth, zero) {
4404
+ this.DEPTH = depth;
4405
+ this.HEIGHT = depth + 1;
4406
+ this.DEGREE = degree;
4407
+ this.LEAVES_COUNT = degree ** depth;
4408
+ this.LEAVES_IDX_0 = (degree ** depth - 1) / (degree - 1);
4409
+ this.NODES_COUNT = (degree ** (depth + 1) - 1) / (degree - 1);
4410
+ this.initZero(zero);
4411
+ this.initNodes();
4412
+ }
4413
+ get root() {
4414
+ return this.nodes[0];
4415
+ }
4416
+ async initZero(zero) {
4417
+ this.zeros = new Array(this.HEIGHT);
4418
+ this.zeros[0] = zero;
4419
+ for (let i = 1; i < this.zeros.length; i++) {
4420
+ this.zeros[i] = await poseidon([this.zeros[i - 1], this.zeros[i - 1]]);
4421
+ }
4422
+ }
4423
+ async initNodes() {
4424
+ const DEGREE = this.DEGREE;
4425
+ this.nodes = new Array(this.NODES_COUNT);
4426
+ for (let d = this.DEPTH; d >= 0; d--) {
4427
+ const size = DEGREE ** d;
4428
+ const idx0 = (DEGREE ** d - 1) / (DEGREE - 1);
4429
+ const zero = this.zeros[this.DEPTH - d];
4430
+ for (let i = 0; i < size; i++) {
4431
+ this.nodes[idx0 + i] = zero;
4432
+ }
4433
+ }
4434
+ }
4435
+ initLeaves(leaves) {
4436
+ const DEGREE = this.DEGREE;
4437
+ for (let i = 0; i < leaves.length; i++) {
4438
+ if (i >= this.LEAVES_COUNT) {
4439
+ console.error("OVERFLOW");
4440
+ break;
4441
+ }
4442
+ this.nodes[this.LEAVES_IDX_0 + i] = BigInt(leaves[i]);
4443
+ }
4444
+ for (let d = this.DEPTH - 1; d >= 0; d--) {
4445
+ const size = DEGREE ** d;
4446
+ const idx0 = (DEGREE ** d - 1) / (DEGREE - 1);
4447
+ for (let i = 0; i < size / DEGREE; i++) {
4448
+ const start = (idx0 + i) * DEGREE + 1;
4449
+ const children = this.nodes.slice(start, start + DEGREE);
4450
+ this.nodes[idx0 + i] = poseidon(children);
4451
+ }
4452
+ }
4453
+ }
4454
+ leaf(leafIdx) {
4455
+ if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
4456
+ throw new Error("wrong leaf index");
4457
+ }
4458
+ const nodeIdx = this.LEAVES_IDX_0 + leafIdx;
4459
+ return this.nodes[nodeIdx];
4460
+ }
4461
+ leaves() {
4462
+ return this.nodes.slice(this.LEAVES_IDX_0);
4463
+ }
4464
+ updateLeaf(leafIdx, leaf) {
4465
+ if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
4466
+ throw new Error("wrong leaf index");
4467
+ }
4468
+ const nodeIdx = this.LEAVES_IDX_0 + leafIdx;
4469
+ this.nodes[nodeIdx] = leaf;
4470
+ this._update(nodeIdx);
4471
+ }
4472
+ pathIdxOf(leafIdx) {
4473
+ if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
4474
+ throw new Error("wrong leaf index");
4475
+ }
4476
+ let idx = this.LEAVES_IDX_0 + leafIdx;
4477
+ const pathIdx = [];
4478
+ for (let i = 0; i < this.DEPTH; i++) {
4479
+ const parentIdx = Math.floor((idx - 1) / this.DEGREE);
4480
+ const childrenIdx0 = parentIdx * this.DEGREE + 1;
4481
+ pathIdx.push(idx - childrenIdx0);
4482
+ idx = parentIdx;
4483
+ }
4484
+ return pathIdx;
4485
+ }
4486
+ pathElementOf(leafIdx) {
4487
+ if (leafIdx > this.LEAVES_COUNT || leafIdx < 0) {
4488
+ throw new Error("wrong leaf index");
4489
+ }
4490
+ let idx = this.LEAVES_IDX_0 + leafIdx;
4491
+ const pathElement = [];
4492
+ for (let h = 0; h < this.DEPTH; h++) {
4493
+ const parentIdx = Math.floor((idx - 1) / this.DEGREE);
4494
+ const childrenIdx0 = parentIdx * this.DEGREE + 1;
4495
+ const el = [];
4496
+ for (let i = childrenIdx0; i < childrenIdx0 + this.DEGREE; i++) {
4497
+ if (i === idx) continue;
4498
+ el.push(this.nodes[i]);
4499
+ }
4500
+ pathElement.push(el);
4501
+ idx = parentIdx;
4502
+ }
4503
+ return pathElement;
4504
+ }
4505
+ subTree(length) {
4506
+ const subTree = new _Tree(this.DEGREE, this.DEPTH, this.zeros[0]);
4507
+ const nodes = [...this.nodes];
4508
+ const DEGREE = this.DEGREE;
4509
+ let tail = length;
4510
+ for (let d = this.DEPTH; d >= 0; d--) {
4511
+ const size = DEGREE ** d;
4512
+ const idx0 = (DEGREE ** d - 1) / (DEGREE - 1);
4513
+ const zero = this.zeros[this.DEPTH - d];
4514
+ for (let i = tail; i < size; i++) {
4515
+ nodes[idx0 + i] = zero;
4516
+ }
4517
+ tail = Math.ceil(tail / DEGREE);
4518
+ }
4519
+ subTree.nodes = nodes;
4520
+ subTree._update(this.LEAVES_IDX_0 + length - 1);
4521
+ return subTree;
4522
+ }
4523
+ async _update(nodeIdx) {
4524
+ let idx = nodeIdx;
4525
+ while (idx > 0) {
4526
+ const parentIdx = Math.floor((idx - 1) / this.DEGREE);
4527
+ const childrenIdx0 = parentIdx * this.DEGREE + 1;
4528
+ this.nodes[parentIdx] = await poseidon(
4529
+ this.nodes.slice(childrenIdx0, childrenIdx0 + this.DEGREE)
4530
+ );
4531
+ idx = parentIdx;
4532
+ }
4533
+ }
4534
+ };
4535
+ var tree_default = Tree;
4536
+
4537
+ // src/libs/crypto/keys.ts
4538
+ var SNARK_FIELD_SIZE2 = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
4539
+ var genPrivKey = () => BigInt(`0x${randomBytes2(32).toString("hex")}`);
4540
+ var genRandomSalt = () => genRandomBabyJubValue();
4541
+ var formatPrivKeyForBabyJub = (privKey) => BigInt(deriveSecretScalar(bigInt2Buffer(privKey)));
4542
+ var packPubKey = (pubKey) => BigInt(packPublicKey(pubKey));
4543
+ var unpackPubKey = (packed) => {
4544
+ const pubKey = unpackPublicKey(packed);
4545
+ return pubKey.map((x) => BigInt(x));
4546
+ };
4547
+ var genPubKey = (privKey) => {
4548
+ const key = derivePublicKey(bigInt2Buffer(privKey));
4549
+ return [BigInt(key[0]), BigInt(key[1])];
4550
+ };
4551
+ var genKeypair = (pkey) => {
4552
+ const privKey = pkey ? pkey % SNARK_FIELD_SIZE2 : genPrivKey();
4553
+ const pubKey = genPubKey(privKey);
4554
+ const formatedPrivKey = formatPrivKeyForBabyJub(privKey);
4555
+ const keypair = { privKey, pubKey, formatedPrivKey };
4556
+ return keypair;
4557
+ };
4558
+ var genEcdhSharedKey = (privKey, pubKey) => mulPointEscalar2(pubKey, formatPrivKeyForBabyJub(privKey));
4559
+ var genMessageFactory = (stateIdx, signPriKey, signPubKey, coordPubKey) => (encPriKey, nonce, voIdx, newVotes, isLastCmd, salt) => {
4560
+ if (!salt) {
4561
+ salt = BigInt("0x" + randomBytes2(7).toString("hex"));
4562
+ }
4563
+ const packaged = BigInt(nonce) + (BigInt(stateIdx) << 32n) + (BigInt(voIdx) << 64n) + (BigInt(newVotes) << 96n) + (BigInt(salt) << 192n);
4564
+ let newPubKey = [...signPubKey];
4565
+ if (isLastCmd) {
4566
+ newPubKey = [0n, 0n];
4567
+ }
4568
+ const hash = poseidon([packaged, ...newPubKey]);
4569
+ const signature = signMessage(bigInt2Buffer(signPriKey), hash);
4570
+ const command = [packaged, ...newPubKey, ...signature.R8, signature.S];
4571
+ const message = poseidonEncrypt(
4572
+ command,
4573
+ genEcdhSharedKey(encPriKey, coordPubKey),
4574
+ 0n
4575
+ );
4576
+ return message;
4577
+ };
4578
+ var batchGenMessage = (stateIdx, keypair, coordPubKey, plan) => {
4579
+ const genMessage = genMessageFactory(
4580
+ stateIdx,
4581
+ BigInt(keypair.privKey),
4582
+ keypair.pubKey,
4583
+ coordPubKey
4584
+ );
4585
+ const payload = [];
4586
+ for (let i = plan.length - 1; i >= 0; i--) {
4587
+ const p = plan[i];
4588
+ const encAccount = genKeypair();
4589
+ const msg = genMessage(
4590
+ BigInt(encAccount.privKey),
4591
+ i + 1,
4592
+ p[0],
4593
+ p[1],
4594
+ i === plan.length - 1
4595
+ );
4596
+ payload.push({
4597
+ msg,
4598
+ encPubkeys: encAccount.pubKey
4599
+ });
4600
+ }
4601
+ return payload;
4602
+ };
4603
+ var privateKeyFromTxt = (txt) => {
4604
+ if (typeof txt !== "string") {
4605
+ return;
4606
+ }
4607
+ const key = txt.split("\n")[1] || "";
4608
+ if (key.length !== 512) {
4609
+ return;
4610
+ }
4611
+ const keys = key.match(/[0-9a-f]{128}/g);
4612
+ if (!keys || keys.length !== 4) {
4613
+ return;
4614
+ }
4615
+ const priKey = poseidon(keys.map((k) => BigInt("0x" + k)));
4616
+ return genKeypair(priKey % SNARK_FIELD_SIZE2);
4617
+ };
4618
+ var rerandomize = (pubKey, ciphertext, randomVal = genRandomSalt()) => {
4619
+ const d1 = BabyJub.addPoint(
4620
+ BabyJub.mulPointEscalar(BabyJub.Base8, randomVal),
4621
+ ciphertext.c1
4622
+ );
4623
+ const d2 = BabyJub.addPoint(
4624
+ BabyJub.mulPointEscalar(pubKey, randomVal),
4625
+ ciphertext.c2
4626
+ );
4627
+ return {
4628
+ d1,
4629
+ d2
4630
+ };
4631
+ };
4632
+ var genAddKeyProof = async (depth, {
4633
+ coordPubKey,
4634
+ oldKey,
4635
+ deactivates
4636
+ }) => {
4637
+ const sharedKeyHash = poseidon(genEcdhSharedKey(oldKey.privKey, coordPubKey));
4638
+ const randomVal = genRandomSalt();
4639
+ const deactivateIdx = deactivates.findIndex((d) => d[4] === sharedKeyHash);
4640
+ if (deactivateIdx < 0) {
4641
+ return null;
4642
+ }
4643
+ const deactivateLeaf = deactivates[deactivateIdx];
4644
+ const c1 = [deactivateLeaf[0], deactivateLeaf[1]];
4645
+ const c2 = [deactivateLeaf[2], deactivateLeaf[3]];
4646
+ const { d1, d2 } = rerandomize(coordPubKey, { c1, c2 }, randomVal);
4647
+ const nullifier = poseidon([
4648
+ BigInt(oldKey.formatedPrivKey),
4649
+ 1444992409218394441042n
4650
+ ]);
4651
+ const tree = new tree_default(5, depth, 0n);
4652
+ const leaves = deactivates.map((d) => poseidon(d));
4653
+ tree.initLeaves(leaves);
4654
+ const deactivateRoot = tree.root;
4655
+ const deactivateLeafPathElements = tree.pathElementOf(deactivateIdx);
4656
+ const inputHash = BigInt(
4657
+ solidityPackedSha2562(
4658
+ new Array(7).fill("uint256"),
4659
+ stringizing([
4660
+ deactivateRoot,
4661
+ poseidon(coordPubKey),
4662
+ nullifier,
4663
+ d1[0],
4664
+ d1[1],
4665
+ d2[0],
4666
+ d2[1]
4667
+ ])
4668
+ )
4669
+ ) % SNARK_FIELD_SIZE2;
4670
+ const input = {
4671
+ inputHash,
4672
+ coordPubKey,
4673
+ deactivateRoot,
4674
+ deactivateIndex: deactivateIdx,
4675
+ deactivateLeaf: poseidon(deactivateLeaf),
4676
+ c1,
4677
+ c2,
4678
+ randomVal,
4679
+ d1,
4680
+ d2,
4681
+ deactivateLeafPathElements,
4682
+ nullifier,
4683
+ oldPrivateKey: oldKey.formatedPrivKey
4684
+ };
4685
+ return input;
4686
+ };
4687
+
4688
+ // src/libs/crypto/sign.ts
4689
+ import {
4690
+ isOfflineDirectSigner
4691
+ } from "@cosmjs/proto-signing";
4692
+ async function signMessage2(signer, address, message, network) {
4693
+ const accounts = await signer.getAccounts();
4694
+ const account = accounts.find((acc) => acc.address === address);
4695
+ const chainId = getDefaultParams(network).chainId;
4696
+ if (!account) {
4697
+ throw new Error(`Address ${address} not found in wallet`);
4698
+ }
4699
+ if (isOfflineDirectSigner(signer)) {
4700
+ const signDoc = {
4701
+ bodyBytes: new TextEncoder().encode(message),
4702
+ authInfoBytes: new Uint8Array(),
4703
+ chainId,
4704
+ accountNumber: BigInt(0)
4705
+ };
4706
+ const { signature } = await signer.signDirect(address, signDoc);
4707
+ return {
4708
+ signature: signature.signature,
4709
+ pubkey: account.pubkey
4710
+ };
4711
+ } else {
4712
+ const signDoc = {
4713
+ chain_id: chainId,
4714
+ account_number: "0",
4715
+ sequence: "0",
4716
+ fee: {
4717
+ gas: "0",
4718
+ amount: []
4719
+ },
4720
+ msgs: [],
4721
+ memo: message
4722
+ };
4723
+ const { signature } = await signer.signAmino(address, signDoc);
4724
+ return {
4725
+ signature: signature.signature,
4726
+ pubkey: account.pubkey
4727
+ };
4728
+ }
4729
+ }
4730
+ async function genKeypairFromSignature(signature) {
4731
+ const sign = BigInt("0x" + Buffer.from(signature, "base64").toString("hex"));
4732
+ return genKeypair(sign);
4733
+ }
4734
+ async function genKeypairFromSign({
4735
+ signer,
4736
+ address,
4737
+ network
4738
+ }) {
4739
+ if (!address) {
4740
+ [{ address }] = await signer.getAccounts();
4741
+ }
4742
+ const sig = await signMessage2(
4743
+ signer,
4744
+ address,
4745
+ "Generate_MACI_Private_Key",
4746
+ network
4747
+ );
4748
+ return genKeypairFromSignature(sig.signature);
4749
+ }
4750
+
4196
4751
  // src/libs/contract/contract.ts
4197
4752
  var Contract = class {
4198
4753
  constructor({
@@ -4292,7 +4847,9 @@ var Contract = class {
4292
4847
  const end_time = (endVoting.getTime() * 10 ** 6).toString();
4293
4848
  const [{ address }] = await signer.getAccounts();
4294
4849
  const client = await createContractClientByWallet(this.rpcEndpoint, signer);
4295
- const { x: operatorPubkeyX, y: operatorPubkeyY } = decompressPublicKey(operatorPubkey);
4850
+ const [operatorPubkeyX, operatorPubkeyY] = unpackPubKey(
4851
+ BigInt(operatorPubkey)
4852
+ );
4296
4853
  const {
4297
4854
  parameters,
4298
4855
  groth16ProcessVkey,
@@ -4319,8 +4876,8 @@ var Contract = class {
4319
4876
  },
4320
4877
  parameters,
4321
4878
  coordinator: {
4322
- x: operatorPubkeyX,
4323
- y: operatorPubkeyY
4879
+ x: operatorPubkeyX.toString(),
4880
+ y: operatorPubkeyY.toString()
4324
4881
  },
4325
4882
  groth16_process_vkey: groth16ProcessVkey,
4326
4883
  groth16_tally_vkey: groth16TallyVkey,
@@ -4355,7 +4912,9 @@ var Contract = class {
4355
4912
  const end_time = (endVoting.getTime() * 1e6).toString();
4356
4913
  const [{ address }] = await signer.getAccounts();
4357
4914
  const client = await createContractClientByWallet(this.rpcEndpoint, signer);
4358
- const { x: operatorPubkeyX, y: operatorPubkeyY } = decompressPublicKey(operatorPubkey);
4915
+ const [operatorPubkeyX, operatorPubkeyY] = unpackPubKey(
4916
+ BigInt(operatorPubkey)
4917
+ );
4359
4918
  const { maciVoteType, maciCertSystem } = getContractParams(
4360
4919
  "2" /* ORACLE_MACI */,
4361
4920
  circuitType,
@@ -4373,8 +4932,8 @@ var Contract = class {
4373
4932
  end_time
4374
4933
  },
4375
4934
  coordinator: {
4376
- x: operatorPubkeyX,
4377
- y: operatorPubkeyY
4935
+ x: operatorPubkeyX.toString(),
4936
+ y: operatorPubkeyY.toString()
4378
4937
  },
4379
4938
  vote_option_map: voteOptionMap,
4380
4939
  whitelist_backend_pubkey: this.whitelistBackendPubkey,
@@ -4497,280 +5056,6 @@ var OracleCertificate = class {
4497
5056
  }
4498
5057
  };
4499
5058
 
4500
- // src/libs/circom/index.ts
4501
- import {
4502
- isOfflineDirectSigner
4503
- } from "@cosmjs/proto-signing";
4504
-
4505
- // src/libs/circom/circomlib.ts
4506
- import { randomBytes } from "crypto";
4507
- import {
4508
- babyJub,
4509
- eddsa,
4510
- poseidon,
4511
- poseidonEncrypt,
4512
- Tree
4513
- } from "@dorafactory/circomlib";
4514
- import { Scalar, utils } from "ffjavascript";
4515
- import createBlakeHash from "blake-hash";
4516
- import { solidityPackedSha256 } from "ethers";
4517
- var SNARK_FIELD_SIZE = 21888242871839275222246405745257275088548364400416034343698204186575808495617n;
4518
- var bigInt2Buffer = (i) => {
4519
- let hex = i.toString(16);
4520
- if (hex.length % 2 === 1) {
4521
- hex = "0" + hex;
4522
- }
4523
- return Buffer.from(hex, "hex");
4524
- };
4525
- var genRandomKey = () => {
4526
- const min = 6350874878119819312338956282401532410528162663560392320966563075034087161851n;
4527
- let rand;
4528
- while (true) {
4529
- rand = BigInt("0x" + randomBytes(32).toString("hex"));
4530
- if (rand >= min) {
4531
- break;
4532
- }
4533
- }
4534
- const privKey = rand % SNARK_FIELD_SIZE;
4535
- return privKey;
4536
- };
4537
- var genPubKey = (privKey) => {
4538
- return eddsa.prv2pub(bigInt2Buffer(privKey));
4539
- };
4540
- var stringizing = (o, path = []) => {
4541
- if (path.includes(o)) {
4542
- throw new Error("loop nesting!");
4543
- }
4544
- const newPath = [...path, o];
4545
- if (Array.isArray(o)) {
4546
- return o.map((item) => stringizing(item, newPath));
4547
- } else if (typeof o === "object") {
4548
- const output = {};
4549
- for (const key in o) {
4550
- output[key] = stringizing(o[key], newPath);
4551
- }
4552
- return output;
4553
- } else {
4554
- return o.toString();
4555
- }
4556
- };
4557
- var genKeypair = (pkey) => {
4558
- const privKey = pkey ? pkey % SNARK_FIELD_SIZE : genRandomKey();
4559
- const pubKey = genPubKey(privKey);
4560
- const formatedPrivKey = formatPrivKeyForBabyJub(privKey);
4561
- return { privKey, pubKey, formatedPrivKey };
4562
- };
4563
- var formatPrivKeyForBabyJub = (privKey) => {
4564
- const sBuff = eddsa.pruneBuffer(
4565
- createBlakeHash("blake512").update(bigInt2Buffer(privKey)).digest().slice(0, 32)
4566
- );
4567
- const s = utils.leBuff2int(sBuff);
4568
- return Scalar.shr(s, 3);
4569
- };
4570
- var genEcdhSharedKey = (privKey, pubKey) => {
4571
- const sharedKey = babyJub.mulPointEscalar(
4572
- pubKey,
4573
- formatPrivKeyForBabyJub(privKey)
4574
- );
4575
- if (sharedKey[0] === 0n) {
4576
- return [0n, 1n];
4577
- } else {
4578
- return sharedKey;
4579
- }
4580
- };
4581
- var genMessageFactory = (stateIdx, signPriKey, signPubKey, coordPubKey) => (encPriKey, nonce, voIdx, newVotes, isLastCmd, salt) => {
4582
- if (!salt) {
4583
- salt = BigInt("0x" + randomBytes(7).toString("hex"));
4584
- }
4585
- const packaged = BigInt(nonce) + (BigInt(stateIdx) << 32n) + (BigInt(voIdx) << 64n) + (BigInt(newVotes) << 96n) + (BigInt(salt) << 192n);
4586
- let newPubKey = [...signPubKey];
4587
- if (isLastCmd) {
4588
- newPubKey = [0n, 0n];
4589
- }
4590
- const hash = poseidon([packaged, ...newPubKey]);
4591
- const signature = eddsa.signPoseidon(bigInt2Buffer(signPriKey), hash);
4592
- const command = [packaged, ...newPubKey, ...signature.R8, signature.S];
4593
- const message = poseidonEncrypt(
4594
- command,
4595
- genEcdhSharedKey(encPriKey, coordPubKey),
4596
- 0n
4597
- );
4598
- return message;
4599
- };
4600
- var batchGenMessage = (stateIdx, account, coordPubKey, plan) => {
4601
- const genMessage = genMessageFactory(
4602
- stateIdx,
4603
- account.privKey,
4604
- account.pubKey,
4605
- coordPubKey
4606
- );
4607
- const payload = [];
4608
- for (let i = plan.length - 1; i >= 0; i--) {
4609
- const p = plan[i];
4610
- const encAccount = genKeypair();
4611
- const msg = genMessage(
4612
- encAccount.privKey,
4613
- i + 1,
4614
- p[0],
4615
- p[1],
4616
- i === plan.length - 1
4617
- );
4618
- payload.push({
4619
- msg,
4620
- encPubkeys: encAccount.pubKey
4621
- });
4622
- }
4623
- return payload;
4624
- };
4625
- var privateKeyFromTxt = (txt) => {
4626
- if (typeof txt !== "string") {
4627
- return;
4628
- }
4629
- const key = txt.split("\n")[1] || "";
4630
- if (key.length !== 512) {
4631
- return;
4632
- }
4633
- const keys = key.match(/[0-9a-f]{128}/g);
4634
- if (!keys || keys.length !== 4) {
4635
- return;
4636
- }
4637
- const priKey = poseidon(keys.map((k) => BigInt("0x" + k)));
4638
- return genKeypair(priKey % SNARK_FIELD_SIZE);
4639
- };
4640
- var rerandomize = (pubKey, ciphertext, randomVal = genRandomKey()) => {
4641
- const d1 = babyJub.addPoint(
4642
- babyJub.mulPointEscalar(babyJub.Base8, randomVal),
4643
- ciphertext.c1
4644
- );
4645
- const d2 = babyJub.addPoint(
4646
- babyJub.mulPointEscalar(pubKey, randomVal),
4647
- ciphertext.c2
4648
- );
4649
- return {
4650
- d1,
4651
- d2
4652
- };
4653
- };
4654
- var genAddKeyProof = async (depth, {
4655
- coordPubKey,
4656
- oldKey,
4657
- deactivates
4658
- }) => {
4659
- const sharedKeyHash = poseidon(genEcdhSharedKey(oldKey.privKey, coordPubKey));
4660
- const randomVal = genRandomKey();
4661
- const deactivateIdx = deactivates.findIndex((d) => d[4] === sharedKeyHash);
4662
- if (deactivateIdx < 0) {
4663
- return null;
4664
- }
4665
- const deactivateLeaf = deactivates[deactivateIdx];
4666
- const c1 = [deactivateLeaf[0], deactivateLeaf[1]];
4667
- const c2 = [deactivateLeaf[2], deactivateLeaf[3]];
4668
- const { d1, d2 } = rerandomize(coordPubKey, { c1, c2 }, randomVal);
4669
- const nullifier = poseidon([oldKey.formatedPrivKey, 1444992409218394441042n]);
4670
- const tree = new Tree(5, depth, 0n);
4671
- const leaves = deactivates.map((d) => poseidon(d));
4672
- tree.initLeaves(leaves);
4673
- const deactivateRoot = tree.root;
4674
- const deactivateLeafPathElements = tree.pathElementOf(deactivateIdx);
4675
- const inputHash = BigInt(
4676
- solidityPackedSha256(
4677
- new Array(7).fill("uint256"),
4678
- stringizing([
4679
- deactivateRoot,
4680
- poseidon(coordPubKey),
4681
- nullifier,
4682
- d1[0],
4683
- d1[1],
4684
- d2[0],
4685
- d2[1]
4686
- ])
4687
- )
4688
- ) % SNARK_FIELD_SIZE;
4689
- const input = {
4690
- inputHash,
4691
- coordPubKey,
4692
- deactivateRoot,
4693
- deactivateIndex: deactivateIdx,
4694
- deactivateLeaf: poseidon(deactivateLeaf),
4695
- c1,
4696
- c2,
4697
- randomVal,
4698
- d1,
4699
- d2,
4700
- deactivateLeafPathElements,
4701
- nullifier,
4702
- oldPrivateKey: oldKey.formatedPrivKey
4703
- };
4704
- return input;
4705
- };
4706
-
4707
- // src/libs/circom/index.ts
4708
- var Circom = class {
4709
- constructor({ network }) {
4710
- this.network = network;
4711
- this.chainId = getDefaultParams(network).chainId;
4712
- }
4713
- async signMessage(signer, address, message) {
4714
- if (typeof window !== "undefined" && window.keplr) {
4715
- const sig = await window.keplr.signArbitrary(
4716
- this.chainId,
4717
- address,
4718
- message
4719
- );
4720
- return {
4721
- signature: sig.signature,
4722
- pubkey: sig.pub_key.value
4723
- };
4724
- }
4725
- const accounts = await signer.getAccounts();
4726
- const account = accounts.find((acc) => acc.address === address);
4727
- if (!account) {
4728
- throw new Error(`Address ${address} not found in wallet`);
4729
- }
4730
- if (isOfflineDirectSigner(signer)) {
4731
- const signDoc = {
4732
- bodyBytes: new TextEncoder().encode(message),
4733
- authInfoBytes: new Uint8Array(),
4734
- chainId: this.chainId,
4735
- accountNumber: BigInt(0)
4736
- };
4737
- const { signature } = await signer.signDirect(address, signDoc);
4738
- return {
4739
- signature: signature.signature,
4740
- pubkey: account.pubkey
4741
- };
4742
- } else {
4743
- const signDoc = {
4744
- chain_id: this.chainId,
4745
- account_number: "0",
4746
- sequence: "0",
4747
- fee: {
4748
- gas: "0",
4749
- amount: []
4750
- },
4751
- msgs: [],
4752
- memo: message
4753
- };
4754
- const { signature } = await signer.signAmino(address, signDoc);
4755
- return {
4756
- signature: signature.signature,
4757
- pubkey: account.pubkey
4758
- };
4759
- }
4760
- }
4761
- async genKeypairFromSign(signer, address) {
4762
- const sig = await this.signMessage(
4763
- signer,
4764
- address,
4765
- "Generate_MACI_Private_Key"
4766
- );
4767
- const sign = BigInt(
4768
- "0x" + Buffer.from(sig.signature, "base64").toString("hex")
4769
- );
4770
- return genKeypair(sign);
4771
- }
4772
- };
4773
-
4774
5059
  // src/libs/maci/maci.ts
4775
5060
  import { GasPrice as GasPrice2, calculateFee } from "@cosmjs/stargate";
4776
5061
  import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx.js";
@@ -4779,15 +5064,15 @@ function isErrorResponse(response) {
4779
5064
  }
4780
5065
  var MACI = class {
4781
5066
  constructor({
4782
- circom,
4783
5067
  contract,
4784
5068
  indexer,
4785
- oracleCertificate
5069
+ oracleCertificate,
5070
+ maciKeypair
4786
5071
  }) {
4787
- this.circom = circom;
4788
5072
  this.contract = contract;
4789
5073
  this.indexer = indexer;
4790
5074
  this.oracleCertificate = oracleCertificate;
5075
+ this.maciKeypair = maciKeypair;
4791
5076
  }
4792
5077
  async getStateIdxInc({
4793
5078
  signer,
@@ -4905,8 +5190,8 @@ var MACI = class {
4905
5190
  contractAddress
4906
5191
  });
4907
5192
  const balance = await client.whiteBalanceOf({
4908
- amount: address,
4909
- certificate,
5193
+ amount: certificate.amount,
5194
+ certificate: certificate.signature,
4910
5195
  sender: address
4911
5196
  });
4912
5197
  return balance;
@@ -5077,7 +5362,7 @@ var MACI = class {
5077
5362
  signer,
5078
5363
  address,
5079
5364
  contractAddress,
5080
- maciAccount,
5365
+ maciKeypair,
5081
5366
  oracleCertificate,
5082
5367
  gasStation = false
5083
5368
  }) {
@@ -5085,8 +5370,8 @@ var MACI = class {
5085
5370
  if (!address) {
5086
5371
  address = (await signer.getAccounts())[0].address;
5087
5372
  }
5088
- if (maciAccount === void 0) {
5089
- maciAccount = await this.circom.genKeypairFromSign(signer, address);
5373
+ if (maciKeypair === void 0) {
5374
+ maciKeypair = this.maciKeypair;
5090
5375
  }
5091
5376
  const client = await this.contract.contractClient({
5092
5377
  signer
@@ -5095,7 +5380,7 @@ var MACI = class {
5095
5380
  return await this.signupOracle({
5096
5381
  client,
5097
5382
  address,
5098
- pubKey: maciAccount.pubKey,
5383
+ pubKey: maciKeypair.pubKey,
5099
5384
  contractAddress,
5100
5385
  oracleCertificate,
5101
5386
  gasStation
@@ -5104,7 +5389,7 @@ var MACI = class {
5104
5389
  return await this.signupSimple({
5105
5390
  client,
5106
5391
  address,
5107
- pubKey: maciAccount.pubKey,
5392
+ pubKey: maciKeypair.pubKey,
5108
5393
  contractAddress,
5109
5394
  gasStation
5110
5395
  });
@@ -5142,7 +5427,7 @@ var MACI = class {
5142
5427
  contractAddress,
5143
5428
  selectedOptions,
5144
5429
  operatorCoordPubKey,
5145
- maciAccount,
5430
+ maciKeypair,
5146
5431
  gasStation = false
5147
5432
  }) {
5148
5433
  if (stateIdx === -1) {
@@ -5162,15 +5447,15 @@ var MACI = class {
5162
5447
  if (!address) {
5163
5448
  address = (await signer.getAccounts())[0].address;
5164
5449
  }
5165
- if (maciAccount === void 0) {
5166
- maciAccount = await this.circom.genKeypairFromSign(signer, address);
5450
+ if (maciKeypair === void 0) {
5451
+ maciKeypair = this.maciKeypair;
5167
5452
  }
5168
5453
  const plan = options.map((o) => {
5169
5454
  return [o.idx, o.vc];
5170
5455
  });
5171
5456
  const payload = batchGenMessage(
5172
5457
  stateIdx,
5173
- maciAccount,
5458
+ maciKeypair,
5174
5459
  operatorCoordPubKey,
5175
5460
  plan
5176
5461
  );
@@ -5465,6 +5750,7 @@ var MaciClient2 = class {
5465
5750
  * @param {ClientParams} params - The parameters for the Maci Client instance.
5466
5751
  */
5467
5752
  constructor({
5753
+ signer,
5468
5754
  network,
5469
5755
  rpcEndpoint,
5470
5756
  restEndpoint,
@@ -5476,8 +5762,11 @@ var MaciClient2 = class {
5476
5762
  defaultOptions,
5477
5763
  feegrantOperator,
5478
5764
  whitelistBackendPubkey,
5479
- certificateApiEndpoint
5765
+ certificateApiEndpoint,
5766
+ maciKeypair
5480
5767
  }) {
5768
+ this.signer = signer;
5769
+ this.network = network;
5481
5770
  const defaultParams = getDefaultParams(network);
5482
5771
  this.rpcEndpoint = rpcEndpoint || defaultParams.rpcEndpoint;
5483
5772
  this.restEndpoint = restEndpoint || defaultParams.restEndpoint;
@@ -5488,6 +5777,7 @@ var MaciClient2 = class {
5488
5777
  this.oracleCodeId = oracleCodeId || defaultParams.oracleCodeId;
5489
5778
  this.feegrantOperator = feegrantOperator || defaultParams.oracleFeegrantOperator;
5490
5779
  this.whitelistBackendPubkey = whitelistBackendPubkey || defaultParams.oracleWhitelistBackendPubkey;
5780
+ this.maciKeypair = maciKeypair ?? genKeypair();
5491
5781
  this.http = new Http(
5492
5782
  this.apiEndpoint,
5493
5783
  this.restEndpoint,
@@ -5508,22 +5798,41 @@ var MaciClient2 = class {
5508
5798
  feegrantOperator: this.feegrantOperator,
5509
5799
  whitelistBackendPubkey: this.whitelistBackendPubkey
5510
5800
  });
5511
- this.circom = new Circom({ network });
5512
5801
  this.oracleCertificate = new OracleCertificate({
5513
5802
  certificateApiEndpoint: this.certificateApiEndpoint,
5514
5803
  http: this.http
5515
5804
  });
5516
5805
  this.maci = new MACI({
5517
- circom: this.circom,
5518
5806
  contract: this.contract,
5519
5807
  indexer: this.indexer,
5520
- oracleCertificate: this.oracleCertificate
5808
+ oracleCertificate: this.oracleCertificate,
5809
+ maciKeypair: this.maciKeypair
5521
5810
  });
5522
5811
  }
5812
+ getSigner(signer) {
5813
+ return signer || this.signer;
5814
+ }
5815
+ getMaciKeypair() {
5816
+ return this.maciKeypair;
5817
+ }
5818
+ getMaciPubkey() {
5819
+ return this.packMaciPubkey(this.maciKeypair.pubKey);
5820
+ }
5821
+ packMaciPubkey(pubkey) {
5822
+ return packPubKey(pubkey || this.maciKeypair.pubKey);
5823
+ }
5824
+ unpackMaciPubkey(pubkey) {
5825
+ return unpackPubKey(BigInt(pubkey));
5826
+ }
5827
+ async getAddress(signer) {
5828
+ const [{ address }] = await this.getSigner(signer).getAccounts();
5829
+ return address;
5830
+ }
5523
5831
  async oracleMaciClient({
5524
5832
  signer,
5525
5833
  contractAddress
5526
5834
  }) {
5835
+ signer = this.getSigner(signer);
5527
5836
  return await this.contract.oracleMaciClient({
5528
5837
  signer,
5529
5838
  contractAddress
@@ -5533,28 +5842,50 @@ var MaciClient2 = class {
5533
5842
  signer,
5534
5843
  contractAddress
5535
5844
  }) {
5845
+ signer = this.getSigner(signer);
5536
5846
  return await this.contract.registryClient({ signer, contractAddress });
5537
5847
  }
5538
5848
  async maciClient({
5539
5849
  signer,
5540
5850
  contractAddress
5541
5851
  }) {
5852
+ signer = this.getSigner(signer);
5542
5853
  return await this.contract.maciClient({ signer, contractAddress });
5543
5854
  }
5544
5855
  async amaciClient({
5545
5856
  signer,
5546
5857
  contractAddress
5547
5858
  }) {
5859
+ signer = this.getSigner(signer);
5548
5860
  return await this.contract.amaciClient({ signer, contractAddress });
5549
5861
  }
5550
5862
  async createAMaciRound(params) {
5551
- return await this.contract.createAMaciRound(params);
5863
+ return await this.contract.createAMaciRound({
5864
+ signer: this.getSigner(),
5865
+ ...params
5866
+ });
5552
5867
  }
5553
5868
  async createMaciRound(params) {
5554
- return await this.contract.createMaciRound(params);
5869
+ return await this.contract.createMaciRound({
5870
+ signer: this.getSigner(),
5871
+ ...params
5872
+ });
5555
5873
  }
5556
5874
  async createOracleMaciRound(params) {
5557
- return await this.contract.createOracleMaciRound(params);
5875
+ return await this.contract.createOracleMaciRound({
5876
+ signer: this.getSigner(),
5877
+ ...params
5878
+ });
5879
+ }
5880
+ async genKeypairFromSign({
5881
+ signer,
5882
+ address
5883
+ } = {}) {
5884
+ return await genKeypairFromSign({
5885
+ signer: this.getSigner(signer),
5886
+ address,
5887
+ network: this.network
5888
+ });
5558
5889
  }
5559
5890
  async getStateIdxInc({
5560
5891
  signer,
@@ -5562,7 +5893,7 @@ var MaciClient2 = class {
5562
5893
  contractAddress
5563
5894
  }) {
5564
5895
  return await this.maci.getStateIdxInc({
5565
- signer,
5896
+ signer: this.getSigner(signer),
5566
5897
  address,
5567
5898
  contractAddress
5568
5899
  });
@@ -5573,7 +5904,7 @@ var MaciClient2 = class {
5573
5904
  contractAddress
5574
5905
  }) {
5575
5906
  return await this.maci.getVoiceCreditBalance({
5576
- signer,
5907
+ signer: this.getSigner(signer),
5577
5908
  stateIdx,
5578
5909
  contractAddress
5579
5910
  });
@@ -5588,18 +5919,22 @@ var MaciClient2 = class {
5588
5919
  });
5589
5920
  }
5590
5921
  async feegrantAllowance({
5922
+ signer,
5591
5923
  address,
5592
5924
  contractAddress
5593
5925
  }) {
5926
+ address = await this.getAddress(signer);
5594
5927
  return await this.maci.feegrantAllowance({
5595
5928
  address,
5596
5929
  contractAddress
5597
5930
  });
5598
5931
  }
5599
5932
  async hasFeegrant({
5933
+ signer,
5600
5934
  address,
5601
5935
  contractAddress
5602
5936
  }) {
5937
+ address = await this.getAddress(signer);
5603
5938
  return await this.maci.hasFeegrant({
5604
5939
  address,
5605
5940
  contractAddress
@@ -5612,6 +5947,10 @@ var MaciClient2 = class {
5612
5947
  certificate,
5613
5948
  mode = "maci"
5614
5949
  }) {
5950
+ signer = this.getSigner(signer);
5951
+ if (!address) {
5952
+ address = await this.getAddress(signer);
5953
+ }
5615
5954
  return await this.maci.queryWhitelistBalanceOf({
5616
5955
  signer,
5617
5956
  address,
@@ -5625,6 +5964,10 @@ var MaciClient2 = class {
5625
5964
  address,
5626
5965
  contractAddress
5627
5966
  }) {
5967
+ signer = this.getSigner(signer);
5968
+ if (!address) {
5969
+ address = await this.getAddress(signer);
5970
+ }
5628
5971
  return await this.maci.isWhitelisted({
5629
5972
  signer,
5630
5973
  address,
@@ -5636,7 +5979,7 @@ var MaciClient2 = class {
5636
5979
  contractAddress
5637
5980
  }) {
5638
5981
  return await this.maci.getOracleWhitelistConfig({
5639
- signer,
5982
+ signer: this.getSigner(signer),
5640
5983
  contractAddress
5641
5984
  });
5642
5985
  }
@@ -5684,7 +6027,7 @@ var MaciClient2 = class {
5684
6027
  contractAddress
5685
6028
  }) {
5686
6029
  return await this.maci.requestOracleCertificate({
5687
- signer,
6030
+ signer: this.getSigner(signer),
5688
6031
  ecosystem,
5689
6032
  address,
5690
6033
  contractAddress
@@ -5694,15 +6037,15 @@ var MaciClient2 = class {
5694
6037
  signer,
5695
6038
  address,
5696
6039
  contractAddress,
5697
- maciAccount,
6040
+ maciKeypair,
5698
6041
  oracleCertificate,
5699
6042
  gasStation = false
5700
6043
  }) {
5701
6044
  return await this.maci.signup({
5702
- signer,
6045
+ signer: this.getSigner(signer),
5703
6046
  address,
5704
6047
  contractAddress,
5705
- maciAccount,
6048
+ maciKeypair,
5706
6049
  oracleCertificate,
5707
6050
  gasStation
5708
6051
  });
@@ -5714,32 +6057,17 @@ var MaciClient2 = class {
5714
6057
  contractAddress,
5715
6058
  selectedOptions,
5716
6059
  operatorCoordPubKey,
5717
- maciAccount,
6060
+ maciKeypair,
5718
6061
  gasStation = false
5719
6062
  }) {
5720
6063
  return await this.maci.vote({
5721
- signer,
6064
+ signer: this.getSigner(signer),
5722
6065
  address,
5723
6066
  stateIdx,
5724
6067
  contractAddress,
5725
6068
  selectedOptions,
5726
6069
  operatorCoordPubKey,
5727
- maciAccount,
5728
- gasStation
5729
- });
5730
- }
5731
- async publishMessage({
5732
- client,
5733
- address,
5734
- payload,
5735
- contractAddress,
5736
- gasStation
5737
- }) {
5738
- return await this.maci.publishMessage({
5739
- client,
5740
- address,
5741
- payload,
5742
- contractAddress,
6070
+ maciKeypair,
5743
6071
  gasStation
5744
6072
  });
5745
6073
  }
@@ -5747,6 +6075,7 @@ var MaciClient2 = class {
5747
6075
  signer,
5748
6076
  contractAddress
5749
6077
  }) {
6078
+ signer = this.getSigner(signer);
5750
6079
  return await this.maci.claimAMaciRound({
5751
6080
  signer,
5752
6081
  contractAddress
@@ -5761,8 +6090,11 @@ var MaciClient2 = class {
5761
6090
  address,
5762
6091
  amount
5763
6092
  }) {
6093
+ if (!address) {
6094
+ address = await this.getAddress(signer);
6095
+ }
5764
6096
  return await this.maci.batchGrantWithBond({
5765
- signer,
6097
+ signer: this.getSigner(signer),
5766
6098
  contractAddress,
5767
6099
  address,
5768
6100
  amount
@@ -5773,22 +6105,22 @@ var MaciClient2 = class {
5773
6105
  contractAddress,
5774
6106
  address
5775
6107
  }) {
6108
+ if (!address) {
6109
+ address = await this.getAddress(signer);
6110
+ }
5776
6111
  return await this.maci.batchRevokeWithdraw({
5777
- signer,
6112
+ signer: this.getSigner(signer),
5778
6113
  contractAddress,
5779
6114
  address
5780
6115
  });
5781
6116
  }
5782
6117
  };
5783
-
5784
- // src/index.ts
5785
- import { Scalar as Scalar2, utils as utils2 } from "ffjavascript";
5786
- import { default as default2 } from "blake-hash";
5787
6118
  export {
5788
- Circom,
5789
6119
  Circuit,
5790
6120
  Contract,
5791
6121
  Event,
6122
+ G1Point,
6123
+ G2Point,
5792
6124
  Http,
5793
6125
  Indexer,
5794
6126
  MACI,
@@ -5796,28 +6128,53 @@ export {
5796
6128
  MaciCircuitType,
5797
6129
  MaciClient2 as MaciClient,
5798
6130
  MaciRoundType,
6131
+ NOTHING_UP_MY_SLEEVE,
5799
6132
  Operator,
5800
6133
  OracleCertificate,
6134
+ PAD_KEY_HASH,
5801
6135
  Proof,
5802
6136
  Round,
5803
- Scalar2 as Scalar,
6137
+ SNARK_FIELD_SIZE,
5804
6138
  Transaction,
6139
+ Tree,
5805
6140
  UserAccount,
5806
6141
  batchGenMessage,
6142
+ bigInt2Buffer,
5807
6143
  circuits,
5808
- compressPublicKey,
5809
- default2 as createBlakeHash,
5810
- decompressPublicKey,
6144
+ formatPrivKeyForBabyJub,
5811
6145
  genAddKeyProof,
5812
6146
  genEcdhSharedKey,
5813
6147
  genKeypair,
6148
+ genKeypairFromSign,
6149
+ genKeypairFromSignature,
5814
6150
  genMessageFactory,
6151
+ genPrivKey,
6152
+ genPubKey,
6153
+ genRandomBabyJubValue,
6154
+ genRandomSalt,
5815
6155
  getDefaultParams,
6156
+ hash12,
6157
+ hash2,
6158
+ hash3,
6159
+ hash4,
6160
+ hash5,
6161
+ hashLeanIMT,
6162
+ hashLeftRight,
6163
+ hashN,
6164
+ hashOne,
5816
6165
  hexToDecimalString,
5817
6166
  isValidAddress,
6167
+ packPubKey,
6168
+ poseidon,
6169
+ poseidonT3,
6170
+ poseidonT4,
6171
+ poseidonT5,
6172
+ poseidonT6,
5818
6173
  privateKeyFromTxt,
6174
+ sha256Hash,
6175
+ signMessage2 as signMessage,
5819
6176
  stringizing,
5820
- utils2 as utils,
6177
+ unpackPubKey,
5821
6178
  validator_operator_set
5822
6179
  };
5823
6180
  //# sourceMappingURL=index.mjs.map