@dorafactory/maci-sdk 0.0.7 → 0.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/README.md +27 -7
  2. package/dist/index.js +751 -187
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +748 -187
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/libs/circom/tree.d.ts +20 -20
  7. package/dist/libs/contract/ts/AMaci.client.d.ts +1 -1
  8. package/dist/libs/contract/ts/Maci.client.d.ts +21 -47
  9. package/dist/libs/contract/ts/Maci.types.d.ts +40 -31
  10. package/dist/libs/contract/ts/OracleMaci.client.d.ts +52 -52
  11. package/dist/libs/contract/ts/OracleMaci.types.d.ts +17 -7
  12. package/dist/libs/contract/ts/Registry.client.d.ts +2 -2
  13. package/dist/libs/contract/types.d.ts +2 -2
  14. package/dist/libs/errors/types.d.ts +1 -0
  15. package/dist/libs/http/http.d.ts +1 -1
  16. package/dist/libs/index.d.ts +3 -0
  17. package/dist/libs/indexer/indexer.d.ts +11 -2
  18. package/dist/libs/maci/index.d.ts +1 -0
  19. package/dist/libs/maci/maci.d.ts +64 -5
  20. package/dist/libs/oracle-certificate/oracle-certificate.d.ts +1 -21
  21. package/dist/libs/oracle-certificate/types.d.ts +12 -0
  22. package/dist/libs/query/event.d.ts +7 -0
  23. package/dist/libs/query/index.d.ts +1 -0
  24. package/dist/maci.d.ts +7 -5
  25. package/dist/types/index.d.ts +22 -0
  26. package/package.json +1 -1
  27. package/src/libs/const.ts +3 -2
  28. package/src/libs/contract/contract.ts +2 -6
  29. package/src/libs/contract/ts/AMaci.client.ts +868 -874
  30. package/src/libs/contract/ts/AMaci.types.ts +216 -216
  31. package/src/libs/contract/ts/Maci.client.ts +748 -888
  32. package/src/libs/contract/ts/Maci.types.ts +229 -224
  33. package/src/libs/contract/ts/OracleMaci.client.ts +623 -348
  34. package/src/libs/contract/ts/OracleMaci.types.ts +191 -138
  35. package/src/libs/contract/ts/Registry.client.ts +438 -446
  36. package/src/libs/contract/ts/Registry.types.ts +97 -97
  37. package/src/libs/contract/types.ts +6 -2
  38. package/src/libs/contract/utils.ts +9 -0
  39. package/src/libs/errors/types.ts +1 -0
  40. package/src/libs/http/http.ts +3 -7
  41. package/src/libs/index.ts +3 -0
  42. package/src/libs/indexer/indexer.ts +18 -0
  43. package/src/libs/maci/index.ts +1 -0
  44. package/src/libs/maci/maci.ts +302 -10
  45. package/src/libs/oracle-certificate/oracle-certificate.ts +19 -73
  46. package/src/libs/oracle-certificate/types.ts +15 -1
  47. package/src/libs/query/event.ts +78 -0
  48. package/src/libs/query/index.ts +1 -0
  49. package/src/maci.ts +27 -5
  50. package/src/types/index.ts +28 -0
package/dist/index.mjs CHANGED
@@ -113,11 +113,12 @@ var Http = class {
113
113
  getFetch() {
114
114
  return this.customFetch || fetch;
115
115
  }
116
- async fetch(url) {
116
+ async fetch(url, options) {
117
117
  try {
118
118
  const fetchFn = this.getFetch();
119
119
  const response = await fetchFn(url, {
120
- ...this.defaultOptions
120
+ ...this.defaultOptions,
121
+ ...options
121
122
  });
122
123
  if (!response.ok) {
123
124
  throw new HttpError(
@@ -228,7 +229,8 @@ var ERROR = {
228
229
  ERROR_ROUND_NOT_FOUND: "ERROR_ROUND_NOT_FOUND",
229
230
  ERROR_ROUNDS_NOT_FOUND: "ERROR_ROUNDS_NOT_FOUND",
230
231
  ERROR_TRANSACTION_NOT_FOUND: "ERROR_TRANSACTION_NOT_FOUND",
231
- ERROR_TRANSACTIONS_NOT_FOUND: "ERROR_TRANSACTIONS_NOT_FOUND"
232
+ ERROR_TRANSACTIONS_NOT_FOUND: "ERROR_TRANSACTIONS_NOT_FOUND",
233
+ ERROR_SIGN_UP_EVENTS_NOT_FOUND: "ERROR_SIGN_UP_EVENTS_NOT_FOUND"
232
234
  };
233
235
 
234
236
  // src/libs/query/account.ts
@@ -404,7 +406,7 @@ function getDefaultParams(network = "mainnet") {
404
406
  rpcEndpoint: "https://vota-rpc.dorafactory.org",
405
407
  restEndpoint: "https://vota-rest.dorafactory.org",
406
408
  apiEndpoint: "https://vota-api.dorafactory.org",
407
- certificateApiEndpoint: "https://vota-certificate-api.dorafactory.org",
409
+ certificateApiEndpoint: "https://vota-certificate-api.dorafactory.org/api/v1",
408
410
  registryAddress: "dora1smg5qp5trjdkcekdjssqpjehdjf6n4cjss0clyvqcud3t3u3948s8rmgg4",
409
411
  maciCodeId: 106,
410
412
  oracleCodeId: 111,
@@ -418,7 +420,7 @@ function getDefaultParams(network = "mainnet") {
418
420
  rpcEndpoint: "https://vota-testnet-rpc.dorafactory.org",
419
421
  restEndpoint: "https://vota-testnet-rest.dorafactory.org",
420
422
  apiEndpoint: "https://vota-testnet-api.dorafactory.org",
421
- certificateApiEndpoint: "https://vota-testnet-certificate-api.dorafactory.org",
423
+ certificateApiEndpoint: "https://vota-testnet-certificate-api.dorafactory.org/api/v1",
422
424
  registryAddress: "dora13c8aecstyxrhax9znvvh5zey89edrmd2k5va57pxvpe3fxtfsfeqlhsjnd",
423
425
  maciCodeId: 107,
424
426
  oracleCodeId: 110,
@@ -1421,6 +1423,63 @@ var Proof = class {
1421
1423
  }
1422
1424
  };
1423
1425
 
1426
+ // src/libs/query/event.ts
1427
+ var Event = class {
1428
+ constructor(http) {
1429
+ this.http = http;
1430
+ }
1431
+ async getSignUpEventByPubKey(contractAddress, pubKey) {
1432
+ try {
1433
+ const STATE_IDX_BY_PUB_KEY_QUERY = `
1434
+ query signUpEvents($limit: Int, $after: Cursor) {
1435
+ signUpEvents(
1436
+ first: $limit, after: $after,
1437
+ filter: {
1438
+ contractAddress: {
1439
+ equalTo: "${contractAddress}"
1440
+ }
1441
+ pubKey: {
1442
+ equalTo: "${pubKey.map((n) => `\\"${n}\\"`).join(",")}"
1443
+ }
1444
+ }) {
1445
+ nodes {
1446
+ id
1447
+ blockHeight
1448
+ txHash
1449
+ contractAddress
1450
+ timestamp
1451
+ pubKey
1452
+ stateIdx
1453
+ balance
1454
+ }
1455
+ }
1456
+ }
1457
+ `;
1458
+ const response = await this.http.fetchGraphql(
1459
+ STATE_IDX_BY_PUB_KEY_QUERY,
1460
+ ""
1461
+ );
1462
+ if (!response || !response.data || !response.data.signUpEvents || !response.data.signUpEvents.nodes || response.data.signUpEvents.nodes.length === 0) {
1463
+ return {
1464
+ code: 404,
1465
+ error: {
1466
+ message: `No signUpEvents found for pubKey ${pubKey.map((n) => `"${n}"`).join(",")}`,
1467
+ type: ERROR.ERROR_SIGN_UP_EVENTS_NOT_FOUND
1468
+ }
1469
+ };
1470
+ }
1471
+ return {
1472
+ code: 200,
1473
+ data: {
1474
+ signUpEvents: response.data.signUpEvents.nodes
1475
+ }
1476
+ };
1477
+ } catch (error) {
1478
+ return handleError(error);
1479
+ }
1480
+ }
1481
+ };
1482
+
1424
1483
  // src/libs/indexer/indexer.ts
1425
1484
  var Indexer = class {
1426
1485
  /**
@@ -1443,6 +1502,7 @@ var Indexer = class {
1443
1502
  this.operator = new Operator(this.http, this.registryAddress);
1444
1503
  this.proof = new Proof(this.http);
1445
1504
  this.transaction = new Transaction(this.http);
1505
+ this.event = new Event(this.http);
1446
1506
  }
1447
1507
  /**
1448
1508
  * @method balanceOf
@@ -1584,6 +1644,16 @@ var Indexer = class {
1584
1644
  async getProofByContractAddress(address) {
1585
1645
  return await this.proof.getProofByContractAddress(address);
1586
1646
  }
1647
+ /**
1648
+ * @method getStateIdxByPubKey
1649
+ * @description Get the state index of a specific public key.
1650
+ * @param {string} contractAddress - The contract address.
1651
+ * @param {bigint[]} pubKey - The public key.
1652
+ * @returns {Promise<SignUpEventsResponse>} The sign up events response.
1653
+ */
1654
+ async getSignUpEventByPubKey(contractAddress, pubKey) {
1655
+ return await this.event.getSignUpEventByPubKey(contractAddress, pubKey);
1656
+ }
1587
1657
  };
1588
1658
 
1589
1659
  // src/libs/contract/config.ts
@@ -1623,16 +1693,6 @@ var MaciQueryClient = class {
1623
1693
  get_msg_chain_length: {}
1624
1694
  });
1625
1695
  };
1626
- this.getDMsgChainLength = async () => {
1627
- return this.client.queryContractSmart(this.contractAddress, {
1628
- get_d_msg_chain_length: {}
1629
- });
1630
- };
1631
- this.getProcessedDMsgCount = async () => {
1632
- return this.client.queryContractSmart(this.contractAddress, {
1633
- get_processed_d_msg_count: {}
1634
- });
1635
- };
1636
1696
  this.getProcessedMsgCount = async () => {
1637
1697
  return this.client.queryContractSmart(this.contractAddress, {
1638
1698
  get_processed_msg_count: {}
@@ -1655,9 +1715,7 @@ var MaciQueryClient = class {
1655
1715
  get_all_result: {}
1656
1716
  });
1657
1717
  };
1658
- this.getStateIdxInc = async ({
1659
- address
1660
- }) => {
1718
+ this.getStateIdxInc = async ({ address }) => {
1661
1719
  return this.client.queryContractSmart(this.contractAddress, {
1662
1720
  get_state_idx_inc: {
1663
1721
  address
@@ -1685,6 +1743,13 @@ var MaciQueryClient = class {
1685
1743
  }
1686
1744
  });
1687
1745
  };
1746
+ this.whiteBalanceOf = async ({ sender }) => {
1747
+ return this.client.queryContractSmart(this.contractAddress, {
1748
+ white_balance_of: {
1749
+ sender
1750
+ }
1751
+ });
1752
+ };
1688
1753
  this.voteOptionMap = async () => {
1689
1754
  return this.client.queryContractSmart(this.contractAddress, {
1690
1755
  vote_option_map: {}
@@ -1717,8 +1782,6 @@ var MaciQueryClient = class {
1717
1782
  this.getPeriod = this.getPeriod.bind(this);
1718
1783
  this.getNumSignUp = this.getNumSignUp.bind(this);
1719
1784
  this.getMsgChainLength = this.getMsgChainLength.bind(this);
1720
- this.getDMsgChainLength = this.getDMsgChainLength.bind(this);
1721
- this.getProcessedDMsgCount = this.getProcessedDMsgCount.bind(this);
1722
1785
  this.getProcessedMsgCount = this.getProcessedMsgCount.bind(this);
1723
1786
  this.getProcessedUserCount = this.getProcessedUserCount.bind(this);
1724
1787
  this.getResult = this.getResult.bind(this);
@@ -1727,6 +1790,7 @@ var MaciQueryClient = class {
1727
1790
  this.getVoiceCreditBalance = this.getVoiceCreditBalance.bind(this);
1728
1791
  this.whiteList = this.whiteList.bind(this);
1729
1792
  this.isWhiteList = this.isWhiteList.bind(this);
1793
+ this.whiteBalanceOf = this.whiteBalanceOf.bind(this);
1730
1794
  this.voteOptionMap = this.voteOptionMap.bind(this);
1731
1795
  this.maxVoteOptions = this.maxVoteOptions.bind(this);
1732
1796
  this.queryTotalFeeGrant = this.queryTotalFeeGrant.bind(this);
@@ -1859,68 +1923,6 @@ var MaciClient = class extends MaciQueryClient {
1859
1923
  _funds
1860
1924
  );
1861
1925
  };
1862
- this.publishDeactivateMessage = async ({
1863
- encPubKey,
1864
- message
1865
- }, fee = "auto", memo, _funds) => {
1866
- return await this.client.execute(
1867
- this.sender,
1868
- this.contractAddress,
1869
- {
1870
- publish_deactivate_message: {
1871
- enc_pub_key: encPubKey,
1872
- message
1873
- }
1874
- },
1875
- fee,
1876
- memo,
1877
- _funds
1878
- );
1879
- };
1880
- this.processDeactivateMessage = async ({
1881
- groth16Proof,
1882
- newDeactivateCommitment,
1883
- newDeactivateRoot,
1884
- size
1885
- }, fee = "auto", memo, _funds) => {
1886
- return await this.client.execute(
1887
- this.sender,
1888
- this.contractAddress,
1889
- {
1890
- process_deactivate_message: {
1891
- groth16_proof: groth16Proof,
1892
- new_deactivate_commitment: newDeactivateCommitment,
1893
- new_deactivate_root: newDeactivateRoot,
1894
- size
1895
- }
1896
- },
1897
- fee,
1898
- memo,
1899
- _funds
1900
- );
1901
- };
1902
- this.addNewKey = async ({
1903
- d,
1904
- groth16Proof,
1905
- nullifier,
1906
- pubkey
1907
- }, fee = "auto", memo, _funds) => {
1908
- return await this.client.execute(
1909
- this.sender,
1910
- this.contractAddress,
1911
- {
1912
- add_new_key: {
1913
- d,
1914
- groth16_proof: groth16Proof,
1915
- nullifier,
1916
- pubkey
1917
- }
1918
- },
1919
- fee,
1920
- memo,
1921
- _funds
1922
- );
1923
- };
1924
1926
  this.publishMessage = async ({
1925
1927
  encPubKey,
1926
1928
  message
@@ -1941,7 +1943,8 @@ var MaciClient = class extends MaciQueryClient {
1941
1943
  };
1942
1944
  this.processMessage = async ({
1943
1945
  groth16Proof,
1944
- newStateCommitment
1946
+ newStateCommitment,
1947
+ plonkProof
1945
1948
  }, fee = "auto", memo, _funds) => {
1946
1949
  return await this.client.execute(
1947
1950
  this.sender,
@@ -1949,7 +1952,8 @@ var MaciClient = class extends MaciQueryClient {
1949
1952
  {
1950
1953
  process_message: {
1951
1954
  groth16_proof: groth16Proof,
1952
- new_state_commitment: newStateCommitment
1955
+ new_state_commitment: newStateCommitment,
1956
+ plonk_proof: plonkProof
1953
1957
  }
1954
1958
  },
1955
1959
  fee,
@@ -1971,7 +1975,8 @@ var MaciClient = class extends MaciQueryClient {
1971
1975
  };
1972
1976
  this.processTally = async ({
1973
1977
  groth16Proof,
1974
- newTallyCommitment
1978
+ newTallyCommitment,
1979
+ plonkProof
1975
1980
  }, fee = "auto", memo, _funds) => {
1976
1981
  return await this.client.execute(
1977
1982
  this.sender,
@@ -1979,7 +1984,8 @@ var MaciClient = class extends MaciQueryClient {
1979
1984
  {
1980
1985
  process_tally: {
1981
1986
  groth16_proof: groth16Proof,
1982
- new_tally_commitment: newTallyCommitment
1987
+ new_tally_commitment: newTallyCommitment,
1988
+ plonk_proof: plonkProof
1983
1989
  }
1984
1990
  },
1985
1991
  fee,
@@ -2072,9 +2078,6 @@ var MaciClient = class extends MaciQueryClient {
2072
2078
  this.signUp = this.signUp.bind(this);
2073
2079
  this.startProcessPeriod = this.startProcessPeriod.bind(this);
2074
2080
  this.stopVotingPeriod = this.stopVotingPeriod.bind(this);
2075
- this.publishDeactivateMessage = this.publishDeactivateMessage.bind(this);
2076
- this.processDeactivateMessage = this.processDeactivateMessage.bind(this);
2077
- this.addNewKey = this.addNewKey.bind(this);
2078
2081
  this.publishMessage = this.publishMessage.bind(this);
2079
2082
  this.processMessage = this.processMessage.bind(this);
2080
2083
  this.stopProcessingPeriod = this.stopProcessingPeriod.bind(this);
@@ -2157,9 +2160,7 @@ var AMaciQueryClient = class {
2157
2160
  get_all_result: {}
2158
2161
  });
2159
2162
  };
2160
- this.getStateIdxInc = async ({
2161
- address
2162
- }) => {
2163
+ this.getStateIdxInc = async ({ address }) => {
2163
2164
  return this.client.queryContractSmart(this.contractAddress, {
2164
2165
  get_state_idx_inc: {
2165
2166
  address
@@ -2598,9 +2599,7 @@ var RegistryQueryClient = class {
2598
2599
  operator: {}
2599
2600
  });
2600
2601
  };
2601
- this.isMaciOperator = async ({
2602
- address
2603
- }) => {
2602
+ this.isMaciOperator = async ({ address }) => {
2604
2603
  return this.client.queryContractSmart(this.contractAddress, {
2605
2604
  is_maci_operator: {
2606
2605
  address
@@ -2859,9 +2858,7 @@ var OracleMaciQueryClient = class {
2859
2858
  get_processed_user_count: {}
2860
2859
  });
2861
2860
  };
2862
- this.getResult = async ({
2863
- index
2864
- }) => {
2861
+ this.getResult = async ({ index }) => {
2865
2862
  return this.client.queryContractSmart(this.contractAddress, {
2866
2863
  get_result: {
2867
2864
  index
@@ -2873,9 +2870,7 @@ var OracleMaciQueryClient = class {
2873
2870
  get_all_result: {}
2874
2871
  });
2875
2872
  };
2876
- this.getStateIdxInc = async ({
2877
- address
2878
- }) => {
2873
+ this.getStateIdxInc = async ({ address }) => {
2879
2874
  return this.client.queryContractSmart(this.contractAddress, {
2880
2875
  get_state_idx_inc: {
2881
2876
  address
@@ -3005,147 +3000,259 @@ var OracleMaciClient = class extends OracleMaciQueryClient {
3005
3000
  stateTreeDepth,
3006
3001
  voteOptionTreeDepth
3007
3002
  }, fee = "auto", memo, _funds) => {
3008
- return await this.client.execute(this.sender, this.contractAddress, {
3009
- set_params: {
3010
- int_state_tree_depth: intStateTreeDepth,
3011
- message_batch_size: messageBatchSize,
3012
- state_tree_depth: stateTreeDepth,
3013
- vote_option_tree_depth: voteOptionTreeDepth
3014
- }
3015
- }, fee, memo, _funds);
3003
+ return await this.client.execute(
3004
+ this.sender,
3005
+ this.contractAddress,
3006
+ {
3007
+ set_params: {
3008
+ int_state_tree_depth: intStateTreeDepth,
3009
+ message_batch_size: messageBatchSize,
3010
+ state_tree_depth: stateTreeDepth,
3011
+ vote_option_tree_depth: voteOptionTreeDepth
3012
+ }
3013
+ },
3014
+ fee,
3015
+ memo,
3016
+ _funds
3017
+ );
3016
3018
  };
3017
3019
  this.setRoundInfo = async ({
3018
3020
  roundInfo
3019
3021
  }, fee = "auto", memo, _funds) => {
3020
- return await this.client.execute(this.sender, this.contractAddress, {
3021
- set_round_info: {
3022
- round_info: roundInfo
3023
- }
3024
- }, fee, memo, _funds);
3022
+ return await this.client.execute(
3023
+ this.sender,
3024
+ this.contractAddress,
3025
+ {
3026
+ set_round_info: {
3027
+ round_info: roundInfo
3028
+ }
3029
+ },
3030
+ fee,
3031
+ memo,
3032
+ _funds
3033
+ );
3025
3034
  };
3026
3035
  this.setVoteOptionsMap = async ({
3027
3036
  voteOptionMap
3028
3037
  }, fee = "auto", memo, _funds) => {
3029
- return await this.client.execute(this.sender, this.contractAddress, {
3030
- set_vote_options_map: {
3031
- vote_option_map: voteOptionMap
3032
- }
3033
- }, fee, memo, _funds);
3038
+ return await this.client.execute(
3039
+ this.sender,
3040
+ this.contractAddress,
3041
+ {
3042
+ set_vote_options_map: {
3043
+ vote_option_map: voteOptionMap
3044
+ }
3045
+ },
3046
+ fee,
3047
+ memo,
3048
+ _funds
3049
+ );
3034
3050
  };
3035
3051
  this.startVotingPeriod = async (fee = "auto", memo, _funds) => {
3036
- return await this.client.execute(this.sender, this.contractAddress, {
3037
- start_voting_period: {}
3038
- }, fee, memo, _funds);
3052
+ return await this.client.execute(
3053
+ this.sender,
3054
+ this.contractAddress,
3055
+ {
3056
+ start_voting_period: {}
3057
+ },
3058
+ fee,
3059
+ memo,
3060
+ _funds
3061
+ );
3039
3062
  };
3040
3063
  this.signUp = async ({
3041
3064
  amount,
3042
3065
  certificate,
3043
3066
  pubkey
3044
3067
  }, fee = "auto", memo, _funds) => {
3045
- return await this.client.execute(this.sender, this.contractAddress, {
3046
- sign_up: {
3047
- amount,
3048
- certificate,
3049
- pubkey
3050
- }
3051
- }, fee, memo, _funds);
3068
+ return await this.client.execute(
3069
+ this.sender,
3070
+ this.contractAddress,
3071
+ {
3072
+ sign_up: {
3073
+ amount,
3074
+ certificate,
3075
+ pubkey
3076
+ }
3077
+ },
3078
+ fee,
3079
+ memo,
3080
+ _funds
3081
+ );
3052
3082
  };
3053
3083
  this.startProcessPeriod = async (fee = "auto", memo, _funds) => {
3054
- return await this.client.execute(this.sender, this.contractAddress, {
3055
- start_process_period: {}
3056
- }, fee, memo, _funds);
3084
+ return await this.client.execute(
3085
+ this.sender,
3086
+ this.contractAddress,
3087
+ {
3088
+ start_process_period: {}
3089
+ },
3090
+ fee,
3091
+ memo,
3092
+ _funds
3093
+ );
3057
3094
  };
3058
3095
  this.stopVotingPeriod = async (fee = "auto", memo, _funds) => {
3059
- return await this.client.execute(this.sender, this.contractAddress, {
3060
- stop_voting_period: {}
3061
- }, fee, memo, _funds);
3096
+ return await this.client.execute(
3097
+ this.sender,
3098
+ this.contractAddress,
3099
+ {
3100
+ stop_voting_period: {}
3101
+ },
3102
+ fee,
3103
+ memo,
3104
+ _funds
3105
+ );
3062
3106
  };
3063
3107
  this.publishMessage = async ({
3064
3108
  encPubKey,
3065
3109
  message
3066
3110
  }, fee = "auto", memo, _funds) => {
3067
- return await this.client.execute(this.sender, this.contractAddress, {
3068
- publish_message: {
3069
- enc_pub_key: encPubKey,
3070
- message
3071
- }
3072
- }, fee, memo, _funds);
3111
+ return await this.client.execute(
3112
+ this.sender,
3113
+ this.contractAddress,
3114
+ {
3115
+ publish_message: {
3116
+ enc_pub_key: encPubKey,
3117
+ message
3118
+ }
3119
+ },
3120
+ fee,
3121
+ memo,
3122
+ _funds
3123
+ );
3073
3124
  };
3074
3125
  this.processMessage = async ({
3075
3126
  groth16Proof,
3076
3127
  newStateCommitment,
3077
3128
  plonkProof
3078
3129
  }, fee = "auto", memo, _funds) => {
3079
- return await this.client.execute(this.sender, this.contractAddress, {
3080
- process_message: {
3081
- groth16_proof: groth16Proof,
3082
- new_state_commitment: newStateCommitment,
3083
- plonk_proof: plonkProof
3084
- }
3085
- }, fee, memo, _funds);
3130
+ return await this.client.execute(
3131
+ this.sender,
3132
+ this.contractAddress,
3133
+ {
3134
+ process_message: {
3135
+ groth16_proof: groth16Proof,
3136
+ new_state_commitment: newStateCommitment,
3137
+ plonk_proof: plonkProof
3138
+ }
3139
+ },
3140
+ fee,
3141
+ memo,
3142
+ _funds
3143
+ );
3086
3144
  };
3087
3145
  this.stopProcessingPeriod = async (fee = "auto", memo, _funds) => {
3088
- return await this.client.execute(this.sender, this.contractAddress, {
3089
- stop_processing_period: {}
3090
- }, fee, memo, _funds);
3146
+ return await this.client.execute(
3147
+ this.sender,
3148
+ this.contractAddress,
3149
+ {
3150
+ stop_processing_period: {}
3151
+ },
3152
+ fee,
3153
+ memo,
3154
+ _funds
3155
+ );
3091
3156
  };
3092
3157
  this.processTally = async ({
3093
3158
  groth16Proof,
3094
3159
  newTallyCommitment,
3095
3160
  plonkProof
3096
3161
  }, fee = "auto", memo, _funds) => {
3097
- return await this.client.execute(this.sender, this.contractAddress, {
3098
- process_tally: {
3099
- groth16_proof: groth16Proof,
3100
- new_tally_commitment: newTallyCommitment,
3101
- plonk_proof: plonkProof
3102
- }
3103
- }, fee, memo, _funds);
3162
+ return await this.client.execute(
3163
+ this.sender,
3164
+ this.contractAddress,
3165
+ {
3166
+ process_tally: {
3167
+ groth16_proof: groth16Proof,
3168
+ new_tally_commitment: newTallyCommitment,
3169
+ plonk_proof: plonkProof
3170
+ }
3171
+ },
3172
+ fee,
3173
+ memo,
3174
+ _funds
3175
+ );
3104
3176
  };
3105
3177
  this.stopTallyingPeriod = async ({
3106
3178
  results,
3107
3179
  salt
3108
3180
  }, fee = "auto", memo, _funds) => {
3109
- return await this.client.execute(this.sender, this.contractAddress, {
3110
- stop_tallying_period: {
3111
- results,
3112
- salt
3113
- }
3114
- }, fee, memo, _funds);
3181
+ return await this.client.execute(
3182
+ this.sender,
3183
+ this.contractAddress,
3184
+ {
3185
+ stop_tallying_period: {
3186
+ results,
3187
+ salt
3188
+ }
3189
+ },
3190
+ fee,
3191
+ memo,
3192
+ _funds
3193
+ );
3115
3194
  };
3116
3195
  this.grant = async ({
3117
3196
  baseAmount,
3118
3197
  grantee
3119
3198
  }, fee = "auto", memo, _funds) => {
3120
- return await this.client.execute(this.sender, this.contractAddress, {
3121
- grant: {
3122
- base_amount: baseAmount,
3123
- grantee
3124
- }
3125
- }, fee, memo, _funds);
3199
+ return await this.client.execute(
3200
+ this.sender,
3201
+ this.contractAddress,
3202
+ {
3203
+ grant: {
3204
+ base_amount: baseAmount,
3205
+ grantee
3206
+ }
3207
+ },
3208
+ fee,
3209
+ memo,
3210
+ _funds
3211
+ );
3126
3212
  };
3127
3213
  this.revoke = async ({
3128
3214
  grantee
3129
3215
  }, fee = "auto", memo, _funds) => {
3130
- return await this.client.execute(this.sender, this.contractAddress, {
3131
- revoke: {
3132
- grantee
3133
- }
3134
- }, fee, memo, _funds);
3216
+ return await this.client.execute(
3217
+ this.sender,
3218
+ this.contractAddress,
3219
+ {
3220
+ revoke: {
3221
+ grantee
3222
+ }
3223
+ },
3224
+ fee,
3225
+ memo,
3226
+ _funds
3227
+ );
3135
3228
  };
3136
3229
  this.bond = async (fee = "auto", memo, _funds) => {
3137
- return await this.client.execute(this.sender, this.contractAddress, {
3138
- bond: {}
3139
- }, fee, memo, _funds);
3230
+ return await this.client.execute(
3231
+ this.sender,
3232
+ this.contractAddress,
3233
+ {
3234
+ bond: {}
3235
+ },
3236
+ fee,
3237
+ memo,
3238
+ _funds
3239
+ );
3140
3240
  };
3141
3241
  this.withdraw = async ({
3142
3242
  amount
3143
3243
  }, fee = "auto", memo, _funds) => {
3144
- return await this.client.execute(this.sender, this.contractAddress, {
3145
- withdraw: {
3146
- amount
3147
- }
3148
- }, fee, memo, _funds);
3244
+ return await this.client.execute(
3245
+ this.sender,
3246
+ this.contractAddress,
3247
+ {
3248
+ withdraw: {
3249
+ amount
3250
+ }
3251
+ },
3252
+ fee,
3253
+ memo,
3254
+ _funds
3255
+ );
3149
3256
  };
3150
3257
  this.client = client;
3151
3258
  this.sender = sender;
@@ -3671,6 +3778,13 @@ function getContractParams(type, circuitType, proofSystem, maxVoter, maxOption)
3671
3778
  // plonkTallyVkey,
3672
3779
  };
3673
3780
  case "2" /* ORACLE_MACI */:
3781
+ if (circuitType === "0" /* IP1V */) {
3782
+ groth16ProcessVkey = CIRCUIT_INFO["9-4-3-625"]["groth16"].process_1p1v_vkey;
3783
+ groth16TallyVkey = CIRCUIT_INFO["9-4-3-625"]["groth16"].tally_1p1v_vkey;
3784
+ } else if (circuitType === "1" /* QV */) {
3785
+ groth16ProcessVkey = CIRCUIT_INFO["9-4-3-625"]["groth16"].process_qv_vkey;
3786
+ groth16TallyVkey = CIRCUIT_INFO["9-4-3-625"]["groth16"].tally_qv_vkey;
3787
+ }
3674
3788
  return {
3675
3789
  parameters: CIRCUIT_INFO["9-4-3-625"].parameter,
3676
3790
  groth16ProcessVkey,
@@ -3753,7 +3867,6 @@ var Contract = class {
3753
3867
  }
3754
3868
  }
3755
3869
  });
3756
- console.log(`Deploy tx: ${res.transactionHash} - ${contractAddress}`);
3757
3870
  return contractAddress;
3758
3871
  }
3759
3872
  async createMaciRound({
@@ -3814,7 +3927,7 @@ var Contract = class {
3814
3927
  certification_system: maciCertSystem,
3815
3928
  qtr_lib: QTR_LIB
3816
3929
  },
3817
- "MACI",
3930
+ `[MACI] ${title}`,
3818
3931
  "auto"
3819
3932
  );
3820
3933
  return instantiateResponse;
@@ -3882,7 +3995,7 @@ var Contract = class {
3882
3995
  qtr_lib: QTR_LIB,
3883
3996
  feegrant_operator: this.feegrantOperator
3884
3997
  },
3885
- "[Oracle MACI]" + title,
3998
+ `[Oracle MACI] ${title}`,
3886
3999
  "auto"
3887
4000
  );
3888
4001
  return instantiateResponse;
@@ -3945,6 +4058,32 @@ var Contract = class {
3945
4058
  }
3946
4059
  };
3947
4060
 
4061
+ // src/libs/oracle-certificate/oracle-certificate.ts
4062
+ var OracleCertificate = class {
4063
+ constructor({ certificateApiEndpoint, http }) {
4064
+ this.certificateApiEndpoint = certificateApiEndpoint;
4065
+ this.http = http;
4066
+ }
4067
+ async sign(data) {
4068
+ const response = await this.http.fetch(
4069
+ `${this.certificateApiEndpoint}/${data.ecosystem}/sign`,
4070
+ {
4071
+ method: "POST",
4072
+ headers: {
4073
+ "Content-Type": "application/json"
4074
+ },
4075
+ body: JSON.stringify({
4076
+ address: data.address,
4077
+ height: data.height,
4078
+ contractAddress: data.contractAddress
4079
+ })
4080
+ }
4081
+ );
4082
+ const signatureData = await response.json();
4083
+ return signatureData;
4084
+ }
4085
+ };
4086
+
3948
4087
  // src/libs/circom/index.ts
3949
4088
  import {
3950
4089
  isOfflineDirectSigner
@@ -4208,6 +4347,413 @@ var Circom = class {
4208
4347
  }
4209
4348
  };
4210
4349
 
4350
+ // src/libs/maci/maci.ts
4351
+ import { GasPrice as GasPrice2, calculateFee } from "@cosmjs/stargate";
4352
+ import { MsgExecuteContract } from "cosmjs-types/cosmwasm/wasm/v1/tx";
4353
+ function isErrorResponse(response) {
4354
+ return typeof response === "object" && response !== null && "error" in response && typeof response.error === "object" && "message" in response.error;
4355
+ }
4356
+ var MACI = class {
4357
+ constructor({
4358
+ circom,
4359
+ contract,
4360
+ indexer,
4361
+ oracleCertificate
4362
+ }) {
4363
+ this.circom = circom;
4364
+ this.contract = contract;
4365
+ this.indexer = indexer;
4366
+ this.oracleCertificate = oracleCertificate;
4367
+ }
4368
+ async getStateIdxInc({
4369
+ signer,
4370
+ address,
4371
+ contractAddress
4372
+ }) {
4373
+ const client = await this.contract.maciClient({
4374
+ signer,
4375
+ contractAddress
4376
+ });
4377
+ const stateIdx = await client.getStateIdxInc({ address });
4378
+ return stateIdx;
4379
+ }
4380
+ async getVoiceCreditBalance({
4381
+ signer,
4382
+ stateIdx,
4383
+ contractAddress
4384
+ }) {
4385
+ const client = await this.contract.maciClient({
4386
+ signer,
4387
+ contractAddress
4388
+ });
4389
+ const voiceCredit = await client.getVoiceCreditBalance({
4390
+ index: stateIdx.toString()
4391
+ });
4392
+ return voiceCredit;
4393
+ }
4394
+ async getStateIdxByPubKey({
4395
+ contractAddress,
4396
+ pubKey
4397
+ }) {
4398
+ const response = await this.indexer.getSignUpEventByPubKey(
4399
+ contractAddress,
4400
+ pubKey
4401
+ );
4402
+ if (isErrorResponse(response)) {
4403
+ return -1;
4404
+ }
4405
+ return response.data.signUpEvents[0].stateIdx;
4406
+ }
4407
+ // only for maci and oracle maci, amaci will set the voice credit when deploy the contract
4408
+ async queryWhitelistBalanceOf({
4409
+ signer,
4410
+ address,
4411
+ contractAddress,
4412
+ certificate,
4413
+ mode = "maci"
4414
+ }) {
4415
+ if (mode === "amaci") {
4416
+ const isWhiteListed = await this.isWhitelisted({
4417
+ signer,
4418
+ address,
4419
+ contractAddress
4420
+ });
4421
+ if (isWhiteListed) {
4422
+ const round = await this.indexer.getRoundById(contractAddress);
4423
+ if (!isErrorResponse(round)) {
4424
+ return round.data.round.voiceCreditAmount;
4425
+ } else {
4426
+ throw new Error(
4427
+ `Failed to query amaci voice credit: ${round.error.type}`
4428
+ );
4429
+ }
4430
+ } else {
4431
+ return "0";
4432
+ }
4433
+ }
4434
+ if (certificate) {
4435
+ const client = await this.contract.oracleMaciClient({
4436
+ signer,
4437
+ contractAddress
4438
+ });
4439
+ const balance = await client.whiteBalanceOf({
4440
+ amount: address,
4441
+ certificate,
4442
+ sender: address
4443
+ });
4444
+ return balance;
4445
+ } else {
4446
+ const client = await this.contract.maciClient({
4447
+ signer,
4448
+ contractAddress
4449
+ });
4450
+ const balance = await client.whiteBalanceOf({
4451
+ sender: address
4452
+ });
4453
+ return balance;
4454
+ }
4455
+ }
4456
+ async isWhitelisted({
4457
+ signer,
4458
+ address,
4459
+ contractAddress
4460
+ }) {
4461
+ const client = await this.contract.amaciClient({
4462
+ signer,
4463
+ contractAddress
4464
+ });
4465
+ const isWhitelisted = await client.isWhiteList({
4466
+ sender: address
4467
+ });
4468
+ return isWhitelisted;
4469
+ }
4470
+ async getOracleWhitelistConfig({
4471
+ signer,
4472
+ contractAddress
4473
+ }) {
4474
+ const client = await this.contract.oracleMaciClient({
4475
+ signer,
4476
+ contractAddress
4477
+ });
4478
+ const snapshotHeight = await client.queryOracleWhitelistConfig();
4479
+ return snapshotHeight;
4480
+ }
4481
+ async getRoundInfo({ contractAddress }) {
4482
+ const roundInfo = await this.indexer.getRoundById(contractAddress);
4483
+ if (isErrorResponse(roundInfo)) {
4484
+ throw new Error(`Failed to get round info: ${roundInfo.error.type}`);
4485
+ }
4486
+ return roundInfo.data.round;
4487
+ }
4488
+ async getRoundCircuitType({ contractAddress }) {
4489
+ const roundInfo = await this.getRoundInfo({ contractAddress });
4490
+ return roundInfo.circuitType;
4491
+ }
4492
+ async queryRoundIsQv({ contractAddress }) {
4493
+ const circuitType = await this.getRoundCircuitType({ contractAddress });
4494
+ return circuitType === "1";
4495
+ }
4496
+ async queryRoundGasStation({ contractAddress }) {
4497
+ const roundInfo = await this.getRoundInfo({ contractAddress });
4498
+ return roundInfo.gasStationEnable;
4499
+ }
4500
+ async requestOracleCertificate({
4501
+ signer,
4502
+ ecosystem,
4503
+ address,
4504
+ contractAddress
4505
+ }) {
4506
+ const oracleWhitelistConfig = await this.getOracleWhitelistConfig({
4507
+ signer,
4508
+ contractAddress
4509
+ });
4510
+ const signResponse = await this.oracleCertificate.sign({
4511
+ ecosystem,
4512
+ address,
4513
+ contractAddress,
4514
+ height: oracleWhitelistConfig.snapshot_height
4515
+ });
4516
+ return signResponse;
4517
+ }
4518
+ async signup({
4519
+ signer,
4520
+ address,
4521
+ contractAddress,
4522
+ oracleCertificate,
4523
+ gasStation = false
4524
+ }) {
4525
+ try {
4526
+ const maciAccount = await this.circom.genKeypairFromSign(signer, address);
4527
+ const client = await this.contract.contractClient({
4528
+ signer
4529
+ });
4530
+ if (oracleCertificate) {
4531
+ return await this.signupOracle({
4532
+ client,
4533
+ address,
4534
+ pubKey: maciAccount.pubKey,
4535
+ contractAddress,
4536
+ oracleCertificate,
4537
+ gasStation
4538
+ });
4539
+ } else {
4540
+ return await this.signupSimple({
4541
+ client,
4542
+ address,
4543
+ pubKey: maciAccount.pubKey,
4544
+ contractAddress,
4545
+ gasStation
4546
+ });
4547
+ }
4548
+ } catch (error) {
4549
+ throw Error(`Signup failed! ${error}`);
4550
+ }
4551
+ }
4552
+ async processVoteOptions({
4553
+ selectedOptions,
4554
+ contractAddress,
4555
+ voiceCreditBalance
4556
+ }) {
4557
+ const idxSet = /* @__PURE__ */ new Set();
4558
+ for (const option of selectedOptions) {
4559
+ if (idxSet.has(option.idx)) {
4560
+ throw new Error(
4561
+ `Duplicate option index (${option.idx}) is not allowed`
4562
+ );
4563
+ }
4564
+ idxSet.add(option.idx);
4565
+ }
4566
+ const options = selectedOptions.filter((o) => !!o.vc).sort((a, b) => a.idx - b.idx);
4567
+ const isQv = await this.queryRoundIsQv({ contractAddress });
4568
+ const usedVc = options.reduce((s, o) => s + (isQv ? o.vc * o.vc : o.vc), 0);
4569
+ if (Number(voiceCreditBalance) < usedVc) {
4570
+ throw new Error("Insufficient voice credit balance");
4571
+ }
4572
+ return options;
4573
+ }
4574
+ async vote({
4575
+ signer,
4576
+ address,
4577
+ stateIdx,
4578
+ contractAddress,
4579
+ selectedOptions,
4580
+ operatorCoordPubKey,
4581
+ gasStation = false
4582
+ }) {
4583
+ if (stateIdx === -1) {
4584
+ throw new Error("State index is not set, Please signup first");
4585
+ }
4586
+ try {
4587
+ const voiceCreditBalance = await this.getVoiceCreditBalance({
4588
+ signer,
4589
+ stateIdx,
4590
+ contractAddress
4591
+ });
4592
+ const options = await this.processVoteOptions({
4593
+ selectedOptions,
4594
+ contractAddress,
4595
+ voiceCreditBalance
4596
+ });
4597
+ const maciAccount = await this.circom.genKeypairFromSign(signer, address);
4598
+ const plan = options.map((o) => {
4599
+ return [o.idx, o.vc];
4600
+ });
4601
+ const payload = batchGenMessage(
4602
+ stateIdx,
4603
+ maciAccount,
4604
+ operatorCoordPubKey,
4605
+ plan
4606
+ );
4607
+ const client = await this.contract.contractClient({
4608
+ signer
4609
+ });
4610
+ return await this.publishMessage({
4611
+ client,
4612
+ address,
4613
+ payload,
4614
+ contractAddress,
4615
+ gasStation
4616
+ });
4617
+ } catch (error) {
4618
+ throw Error(`Vote failed! ${error}`);
4619
+ }
4620
+ }
4621
+ async publishMessage({
4622
+ client,
4623
+ address,
4624
+ payload,
4625
+ contractAddress,
4626
+ gasStation
4627
+ }) {
4628
+ const msgs = payload.map(
4629
+ ({ msg, encPubkeys }) => ({
4630
+ typeUrl: "/cosmwasm.wasm.v1.MsgExecuteContract",
4631
+ value: MsgExecuteContract.fromPartial({
4632
+ sender: address,
4633
+ contract: contractAddress,
4634
+ msg: new TextEncoder().encode(
4635
+ JSON.stringify(
4636
+ stringizing({
4637
+ publish_message: {
4638
+ enc_pub_key: {
4639
+ x: encPubkeys[0],
4640
+ y: encPubkeys[1]
4641
+ },
4642
+ message: {
4643
+ data: msg
4644
+ }
4645
+ }
4646
+ })
4647
+ )
4648
+ )
4649
+ })
4650
+ })
4651
+ );
4652
+ const gasPrice = GasPrice2.fromString("100000000000peaka");
4653
+ const fee = calculateFee(2e7 * msgs.length, gasPrice);
4654
+ if (gasStation) {
4655
+ const grantFee = {
4656
+ amount: fee.amount,
4657
+ gas: fee.gas,
4658
+ granter: contractAddress
4659
+ };
4660
+ return client.signAndBroadcast(address, msgs, grantFee);
4661
+ }
4662
+ return client.signAndBroadcast(address, msgs, fee);
4663
+ }
4664
+ async signupSimple({
4665
+ client,
4666
+ address,
4667
+ pubKey,
4668
+ contractAddress,
4669
+ gasStation
4670
+ }) {
4671
+ const gasPrice = GasPrice2.fromString("100000000000peaka");
4672
+ const fee = calculateFee(6e7, gasPrice);
4673
+ if (gasStation === true) {
4674
+ const grantFee = {
4675
+ amount: fee.amount,
4676
+ gas: fee.gas,
4677
+ granter: contractAddress
4678
+ };
4679
+ return client.execute(
4680
+ address,
4681
+ contractAddress,
4682
+ {
4683
+ sign_up: {
4684
+ pubkey: {
4685
+ x: pubKey[0].toString(),
4686
+ y: pubKey[1].toString()
4687
+ }
4688
+ }
4689
+ },
4690
+ grantFee
4691
+ );
4692
+ }
4693
+ return client.execute(
4694
+ address,
4695
+ contractAddress,
4696
+ {
4697
+ sign_up: {
4698
+ pubkey: {
4699
+ x: pubKey[0].toString(),
4700
+ y: pubKey[1].toString()
4701
+ }
4702
+ }
4703
+ },
4704
+ fee
4705
+ );
4706
+ }
4707
+ async signupOracle({
4708
+ client,
4709
+ address,
4710
+ pubKey,
4711
+ contractAddress,
4712
+ oracleCertificate,
4713
+ gasStation
4714
+ }) {
4715
+ const gasPrice = GasPrice2.fromString("100000000000peaka");
4716
+ const fee = calculateFee(6e7, gasPrice);
4717
+ if (gasStation === true) {
4718
+ const grantFee = {
4719
+ amount: fee.amount,
4720
+ gas: fee.gas,
4721
+ granter: contractAddress
4722
+ };
4723
+ return client.execute(
4724
+ address,
4725
+ contractAddress,
4726
+ {
4727
+ sign_up: {
4728
+ pubkey: {
4729
+ x: pubKey[0].toString(),
4730
+ y: pubKey[1].toString()
4731
+ },
4732
+ amount: oracleCertificate.amount,
4733
+ certificate: oracleCertificate.signature
4734
+ }
4735
+ },
4736
+ grantFee
4737
+ );
4738
+ }
4739
+ return client.execute(
4740
+ address,
4741
+ contractAddress,
4742
+ {
4743
+ sign_up: {
4744
+ pubkey: {
4745
+ x: pubKey[0].toString(),
4746
+ y: pubKey[1].toString()
4747
+ },
4748
+ amount: oracleCertificate.amount,
4749
+ certificate: oracleCertificate.signature
4750
+ }
4751
+ },
4752
+ fee
4753
+ );
4754
+ }
4755
+ };
4756
+
4211
4757
  // src/maci.ts
4212
4758
  var MaciClient2 = class {
4213
4759
  /**
@@ -4225,12 +4771,14 @@ var MaciClient2 = class {
4225
4771
  customFetch,
4226
4772
  defaultOptions,
4227
4773
  feegrantOperator,
4228
- whitelistBackendPubkey
4774
+ whitelistBackendPubkey,
4775
+ certificateApiEndpoint
4229
4776
  }) {
4230
4777
  const defaultParams = getDefaultParams(network);
4231
4778
  this.rpcEndpoint = rpcEndpoint || defaultParams.rpcEndpoint;
4232
4779
  this.restEndpoint = restEndpoint || defaultParams.restEndpoint;
4233
4780
  this.apiEndpoint = apiEndpoint || defaultParams.apiEndpoint;
4781
+ this.certificateApiEndpoint = certificateApiEndpoint || defaultParams.certificateApiEndpoint;
4234
4782
  this.registryAddress = registryAddress || defaultParams.registryAddress;
4235
4783
  this.maciCodeId = maciCodeId || defaultParams.maciCodeId;
4236
4784
  this.oracleCodeId = oracleCodeId || defaultParams.oracleCodeId;
@@ -4257,6 +4805,16 @@ var MaciClient2 = class {
4257
4805
  whitelistBackendPubkey: this.whitelistBackendPubkey
4258
4806
  });
4259
4807
  this.circom = new Circom({ network });
4808
+ this.oracleCertificate = new OracleCertificate({
4809
+ certificateApiEndpoint: this.certificateApiEndpoint,
4810
+ http: this.http
4811
+ });
4812
+ this.maci = new MACI({
4813
+ circom: this.circom,
4814
+ contract: this.contract,
4815
+ indexer: this.indexer,
4816
+ oracleCertificate: this.oracleCertificate
4817
+ });
4260
4818
  }
4261
4819
  async oracleMaciClient({
4262
4820
  signer,
@@ -4443,13 +5001,16 @@ export {
4443
5001
  Circom,
4444
5002
  Circuit,
4445
5003
  Contract,
5004
+ Event,
4446
5005
  Http,
4447
5006
  Indexer,
5007
+ MACI,
4448
5008
  MaciCertSystemType,
4449
5009
  MaciCircuitType,
4450
5010
  MaciClient2 as MaciClient,
4451
5011
  MaciRoundType,
4452
5012
  Operator,
5013
+ OracleCertificate,
4453
5014
  Proof,
4454
5015
  Round,
4455
5016
  Scalar2 as Scalar,