@rateloop/contracts 0.1.0

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 (115) hide show
  1. package/README.md +27 -0
  2. package/dist/cjs/abis/AdvisoryVoteRecorderAbi.js +935 -0
  3. package/dist/cjs/abis/CategoryRegistryAbi.js +518 -0
  4. package/dist/cjs/abis/ClusterPayoutOracleAbi.js +2580 -0
  5. package/dist/cjs/abis/ConfidentialityEscrowAbi.js +1619 -0
  6. package/dist/cjs/abis/ContentRegistryAbi.js +2606 -0
  7. package/dist/cjs/abis/FeedbackBonusEscrowAbi.js +1496 -0
  8. package/dist/cjs/abis/FeedbackRegistryAbi.js +642 -0
  9. package/dist/cjs/abis/FrontendRegistryAbi.js +1406 -0
  10. package/dist/cjs/abis/LaunchDistributionPoolAbi.js +2936 -0
  11. package/dist/cjs/abis/LoopReputationAbi.js +1365 -0
  12. package/dist/cjs/abis/ProfileRegistryAbi.js +708 -0
  13. package/dist/cjs/abis/ProtocolConfigAbi.js +2075 -0
  14. package/dist/cjs/abis/QuestionRewardPoolEscrowAbi.js +2910 -0
  15. package/dist/cjs/abis/RateLoopGovernorAbi.js +2018 -0
  16. package/dist/cjs/abis/RaterRegistryAbi.js +3062 -0
  17. package/dist/cjs/abis/RaterRegistryConfidentialityAbi.js +117 -0
  18. package/dist/cjs/abis/RoundRewardDistributorAbi.js +1523 -0
  19. package/dist/cjs/abis/RoundVotingEngineAbi.js +2295 -0
  20. package/dist/cjs/abis/TimelockControllerAbi.js +1001 -0
  21. package/dist/cjs/abis/X402QuestionSubmitterAbi.js +2137 -0
  22. package/dist/cjs/abis/index.js +43 -0
  23. package/dist/cjs/deployedContracts.js +168595 -0
  24. package/dist/cjs/deployments.js +48 -0
  25. package/dist/cjs/index.js +27 -0
  26. package/dist/cjs/package.json +3 -0
  27. package/dist/cjs/protocol.js +99 -0
  28. package/dist/cjs/types.js +2 -0
  29. package/dist/cjs/voting.js +336 -0
  30. package/dist/cjs/votingCore.js +301 -0
  31. package/dist/esm/abis/AdvisoryVoteRecorderAbi.d.ts +727 -0
  32. package/dist/esm/abis/AdvisoryVoteRecorderAbi.d.ts.map +1 -0
  33. package/dist/esm/abis/AdvisoryVoteRecorderAbi.js +932 -0
  34. package/dist/esm/abis/CategoryRegistryAbi.d.ts +396 -0
  35. package/dist/esm/abis/CategoryRegistryAbi.d.ts.map +1 -0
  36. package/dist/esm/abis/CategoryRegistryAbi.js +515 -0
  37. package/dist/esm/abis/ClusterPayoutOracleAbi.d.ts +2005 -0
  38. package/dist/esm/abis/ClusterPayoutOracleAbi.d.ts.map +1 -0
  39. package/dist/esm/abis/ClusterPayoutOracleAbi.js +2577 -0
  40. package/dist/esm/abis/ConfidentialityEscrowAbi.d.ts +1255 -0
  41. package/dist/esm/abis/ConfidentialityEscrowAbi.d.ts.map +1 -0
  42. package/dist/esm/abis/ConfidentialityEscrowAbi.js +1616 -0
  43. package/dist/esm/abis/ContentRegistryAbi.d.ts +2036 -0
  44. package/dist/esm/abis/ContentRegistryAbi.d.ts.map +1 -0
  45. package/dist/esm/abis/ContentRegistryAbi.js +2603 -0
  46. package/dist/esm/abis/FeedbackBonusEscrowAbi.d.ts +1163 -0
  47. package/dist/esm/abis/FeedbackBonusEscrowAbi.d.ts.map +1 -0
  48. package/dist/esm/abis/FeedbackBonusEscrowAbi.js +1493 -0
  49. package/dist/esm/abis/FeedbackRegistryAbi.d.ts +498 -0
  50. package/dist/esm/abis/FeedbackRegistryAbi.d.ts.map +1 -0
  51. package/dist/esm/abis/FeedbackRegistryAbi.js +639 -0
  52. package/dist/esm/abis/FrontendRegistryAbi.d.ts +1084 -0
  53. package/dist/esm/abis/FrontendRegistryAbi.d.ts.map +1 -0
  54. package/dist/esm/abis/FrontendRegistryAbi.js +1403 -0
  55. package/dist/esm/abis/LaunchDistributionPoolAbi.d.ts +2273 -0
  56. package/dist/esm/abis/LaunchDistributionPoolAbi.d.ts.map +1 -0
  57. package/dist/esm/abis/LaunchDistributionPoolAbi.js +2933 -0
  58. package/dist/esm/abis/LoopReputationAbi.d.ts +1042 -0
  59. package/dist/esm/abis/LoopReputationAbi.d.ts.map +1 -0
  60. package/dist/esm/abis/LoopReputationAbi.js +1362 -0
  61. package/dist/esm/abis/ProfileRegistryAbi.d.ts +545 -0
  62. package/dist/esm/abis/ProfileRegistryAbi.d.ts.map +1 -0
  63. package/dist/esm/abis/ProfileRegistryAbi.js +705 -0
  64. package/dist/esm/abis/ProtocolConfigAbi.d.ts +1617 -0
  65. package/dist/esm/abis/ProtocolConfigAbi.d.ts.map +1 -0
  66. package/dist/esm/abis/ProtocolConfigAbi.js +2072 -0
  67. package/dist/esm/abis/QuestionRewardPoolEscrowAbi.d.ts +2287 -0
  68. package/dist/esm/abis/QuestionRewardPoolEscrowAbi.d.ts.map +1 -0
  69. package/dist/esm/abis/QuestionRewardPoolEscrowAbi.js +2907 -0
  70. package/dist/esm/abis/RateLoopGovernorAbi.d.ts +1544 -0
  71. package/dist/esm/abis/RateLoopGovernorAbi.d.ts.map +1 -0
  72. package/dist/esm/abis/RateLoopGovernorAbi.js +2015 -0
  73. package/dist/esm/abis/RaterRegistryAbi.d.ts +2390 -0
  74. package/dist/esm/abis/RaterRegistryAbi.d.ts.map +1 -0
  75. package/dist/esm/abis/RaterRegistryAbi.js +3059 -0
  76. package/dist/esm/abis/RaterRegistryConfidentialityAbi.d.ts +132 -0
  77. package/dist/esm/abis/RaterRegistryConfidentialityAbi.d.ts.map +1 -0
  78. package/dist/esm/abis/RaterRegistryConfidentialityAbi.js +114 -0
  79. package/dist/esm/abis/RoundRewardDistributorAbi.d.ts +1182 -0
  80. package/dist/esm/abis/RoundRewardDistributorAbi.d.ts.map +1 -0
  81. package/dist/esm/abis/RoundRewardDistributorAbi.js +1520 -0
  82. package/dist/esm/abis/RoundVotingEngineAbi.d.ts +1797 -0
  83. package/dist/esm/abis/RoundVotingEngineAbi.d.ts.map +1 -0
  84. package/dist/esm/abis/RoundVotingEngineAbi.js +2292 -0
  85. package/dist/esm/abis/TimelockControllerAbi.d.ts +771 -0
  86. package/dist/esm/abis/TimelockControllerAbi.d.ts.map +1 -0
  87. package/dist/esm/abis/TimelockControllerAbi.js +998 -0
  88. package/dist/esm/abis/X402QuestionSubmitterAbi.d.ts +1663 -0
  89. package/dist/esm/abis/X402QuestionSubmitterAbi.d.ts.map +1 -0
  90. package/dist/esm/abis/X402QuestionSubmitterAbi.js +2134 -0
  91. package/dist/esm/abis/index.d.ts +21 -0
  92. package/dist/esm/abis/index.d.ts.map +1 -0
  93. package/dist/esm/abis/index.js +20 -0
  94. package/dist/esm/deployedContracts.d.ts +8 -0
  95. package/dist/esm/deployedContracts.d.ts.map +1 -0
  96. package/dist/esm/deployedContracts.js +168593 -0
  97. package/dist/esm/deployments.d.ts +6 -0
  98. package/dist/esm/deployments.d.ts.map +1 -0
  99. package/dist/esm/deployments.js +39 -0
  100. package/dist/esm/index.d.ts +7 -0
  101. package/dist/esm/index.d.ts.map +1 -0
  102. package/dist/esm/index.js +6 -0
  103. package/dist/esm/protocol.d.ts +64 -0
  104. package/dist/esm/protocol.d.ts.map +1 -0
  105. package/dist/esm/protocol.js +94 -0
  106. package/dist/esm/types.d.ts +16 -0
  107. package/dist/esm/types.d.ts.map +1 -0
  108. package/dist/esm/types.js +1 -0
  109. package/dist/esm/voting.d.ts +75 -0
  110. package/dist/esm/voting.d.ts.map +1 -0
  111. package/dist/esm/voting.js +279 -0
  112. package/dist/esm/votingCore.d.ts +55 -0
  113. package/dist/esm/votingCore.d.ts.map +1 -0
  114. package/dist/esm/votingCore.js +286 -0
  115. package/package.json +122 -0
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getSharedDeployment = getSharedDeployment;
7
+ exports.getSharedDeploymentAddress = getSharedDeploymentAddress;
8
+ exports.getSharedChainStartBlock = getSharedChainStartBlock;
9
+ exports.getSharedDeploymentStartBlock = getSharedDeploymentStartBlock;
10
+ const viem_1 = require("viem");
11
+ const deployedContracts_1 = __importDefault(require("./deployedContracts"));
12
+ const sharedDeployments = deployedContracts_1.default;
13
+ function isValidStartBlock(value) {
14
+ return typeof value === "number" && Number.isInteger(value) && value >= 0;
15
+ }
16
+ function getSharedDeployment(chainId, contractName) {
17
+ return sharedDeployments[chainId]?.[contractName];
18
+ }
19
+ function getSharedDeploymentAddress(chainId, contractName) {
20
+ const address = getSharedDeployment(chainId, contractName)?.address;
21
+ if (!address || !(0, viem_1.isAddress)(address)) {
22
+ return undefined;
23
+ }
24
+ return address;
25
+ }
26
+ function getSharedChainStartBlock(chainId) {
27
+ const contracts = sharedDeployments[chainId];
28
+ if (!contracts) {
29
+ return undefined;
30
+ }
31
+ const deployedBlocks = Object.values(contracts)
32
+ .map(contract => contract?.deployedOnBlock)
33
+ .filter(isValidStartBlock);
34
+ if (deployedBlocks.length === 0) {
35
+ return undefined;
36
+ }
37
+ return Math.min(...deployedBlocks);
38
+ }
39
+ function getSharedDeploymentStartBlock(chainId, contractName) {
40
+ const contractStartBlock = getSharedDeployment(chainId, contractName)?.deployedOnBlock;
41
+ if (isValidStartBlock(contractStartBlock)) {
42
+ return contractStartBlock;
43
+ }
44
+ if (!getSharedDeployment(chainId, contractName)) {
45
+ return undefined;
46
+ }
47
+ return getSharedChainStartBlock(chainId);
48
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ var __importDefault = (this && this.__importDefault) || function (mod) {
17
+ return (mod && mod.__esModule) ? mod : { "default": mod };
18
+ };
19
+ Object.defineProperty(exports, "__esModule", { value: true });
20
+ exports.deployedContracts = void 0;
21
+ __exportStar(require("./abis/index"), exports);
22
+ __exportStar(require("./deployments"), exports);
23
+ var deployedContracts_1 = require("./deployedContracts");
24
+ Object.defineProperty(exports, "deployedContracts", { enumerable: true, get: function () { return __importDefault(deployedContracts_1).default; } });
25
+ __exportStar(require("./protocol"), exports);
26
+ __exportStar(require("./types"), exports);
27
+ __exportStar(require("./voting"), exports);
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BOUNTY_ELIGIBILITY_RECENT_RECHECK_FLAG = exports.BOUNTY_ELIGIBILITY_CREDENTIAL_MASK = exports.WORLD_CHAIN_USDC_BY_CHAIN_ID = exports.WORLD_ID_V3_ROUTER_BY_CHAIN_ID = exports.USDC_EIP712_DOMAIN_NAME_BY_CHAIN_ID = exports.USDC_BY_CHAIN_ID = exports.MIN_NONZERO_CONFIDENTIALITY_BOND = exports.CONFIDENTIALITY_FLAG_PRIVATE_FOREVER = exports.QUESTION_REWARD_PARTICIPANT_FLOORS = exports.SCORE_SPREAD_POLICY = exports.REWARD_SPLIT_BPS = exports.PLATFORM_REWARD_SPLIT_BPS = exports.EPOCH_WEIGHT_BPS = exports.REVEAL_FAILED_GRACE_MULTIPLIER = exports.DEFAULT_REVEAL_GRACE_PERIOD_SECONDS = exports.DEFAULT_ROUND_CONFIG = exports.ROUND_STATE_LABEL = exports.ROUND_STATE = exports.USER_PREDICTION_PERCENT = exports.USER_PREDICTION_BPS = exports.BPS_SCALE = void 0;
4
+ exports.requiredQuestionRewardParticipants = requiredQuestionRewardParticipants;
5
+ exports.getUsdcEip712DomainName = getUsdcEip712DomainName;
6
+ exports.BPS_SCALE = 10_000;
7
+ exports.USER_PREDICTION_BPS = {
8
+ min: 100,
9
+ max: 9_900,
10
+ };
11
+ exports.USER_PREDICTION_PERCENT = {
12
+ min: exports.USER_PREDICTION_BPS.min / 100,
13
+ max: exports.USER_PREDICTION_BPS.max / 100,
14
+ };
15
+ exports.ROUND_STATE = {
16
+ Open: 0,
17
+ Settled: 1,
18
+ Cancelled: 2,
19
+ Tied: 3,
20
+ RevealFailed: 4,
21
+ };
22
+ exports.ROUND_STATE_LABEL = {
23
+ [exports.ROUND_STATE.Open]: "Open",
24
+ [exports.ROUND_STATE.Settled]: "Settled",
25
+ [exports.ROUND_STATE.Cancelled]: "Cancelled",
26
+ [exports.ROUND_STATE.Tied]: "Tied",
27
+ [exports.ROUND_STATE.RevealFailed]: "RevealFailed",
28
+ };
29
+ exports.DEFAULT_ROUND_CONFIG = {
30
+ epochDurationSeconds: 20 * 60,
31
+ maxDurationSeconds: 20 * 60,
32
+ minVoters: 3,
33
+ maxVoters: 100,
34
+ };
35
+ exports.DEFAULT_REVEAL_GRACE_PERIOD_SECONDS = 60 * 60;
36
+ /** Matches `RoundCleanupLib.REVEAL_FAILED_GRACE_MULTIPLIER` in foundry. */
37
+ exports.REVEAL_FAILED_GRACE_MULTIPLIER = 24;
38
+ exports.EPOCH_WEIGHT_BPS = {
39
+ blind: exports.BPS_SCALE,
40
+ informed: 2_500,
41
+ };
42
+ exports.PLATFORM_REWARD_SPLIT_BPS = {
43
+ frontend: 300,
44
+ };
45
+ exports.REWARD_SPLIT_BPS = {
46
+ voter: 9_600,
47
+ submitter: 0,
48
+ platform: exports.PLATFORM_REWARD_SPLIT_BPS.frontend,
49
+ treasury: 100,
50
+ };
51
+ exports.SCORE_SPREAD_POLICY = {
52
+ intensityBps: 15_000,
53
+ forfeitMinReveals: 8,
54
+ maxForfeitBps: 5_000,
55
+ };
56
+ exports.QUESTION_REWARD_PARTICIPANT_FLOORS = {
57
+ minParticipants: 3,
58
+ highValueAmount: 1_000_000_000,
59
+ highValueMinParticipants: 5,
60
+ veryHighValueAmount: 10_000_000_000,
61
+ veryHighValueMinParticipants: exports.SCORE_SPREAD_POLICY.forfeitMinReveals,
62
+ };
63
+ function requiredQuestionRewardParticipants(amountAtomic) {
64
+ const amount = typeof amountAtomic === "bigint" ? amountAtomic : BigInt(amountAtomic);
65
+ if (amount >= BigInt(exports.QUESTION_REWARD_PARTICIPANT_FLOORS.veryHighValueAmount)) {
66
+ return exports.QUESTION_REWARD_PARTICIPANT_FLOORS.veryHighValueMinParticipants;
67
+ }
68
+ if (amount >= BigInt(exports.QUESTION_REWARD_PARTICIPANT_FLOORS.highValueAmount)) {
69
+ return exports.QUESTION_REWARD_PARTICIPANT_FLOORS.highValueMinParticipants;
70
+ }
71
+ return exports.QUESTION_REWARD_PARTICIPANT_FLOORS.minParticipants;
72
+ }
73
+ exports.CONFIDENTIALITY_FLAG_PRIVATE_FOREVER = 1;
74
+ exports.MIN_NONZERO_CONFIDENTIALITY_BOND = 1000000n;
75
+ exports.USDC_BY_CHAIN_ID = {
76
+ 8453: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
77
+ 84532: "0x036CbD53842c5426634e7929541eC2318f3dCF7e",
78
+ 480: "0x79A02482A880bCE3F13e09Da970dC34db4CD24d1",
79
+ 4801: "0x66145f38cBAC35Ca6F1Dfb4914dF98F1614aeA88",
80
+ };
81
+ exports.USDC_EIP712_DOMAIN_NAME_BY_CHAIN_ID = {
82
+ 8453: "USD Coin",
83
+ 84532: "USDC",
84
+ 480: "USDC",
85
+ 4801: "USDC",
86
+ };
87
+ function getUsdcEip712DomainName(chainId) {
88
+ return exports.USDC_EIP712_DOMAIN_NAME_BY_CHAIN_ID[chainId] ?? "USDC";
89
+ }
90
+ exports.WORLD_ID_V3_ROUTER_BY_CHAIN_ID = {
91
+ 8453: "0xBCC7e5910178AFFEEeBA573ba6903E9869594163",
92
+ 84532: "0x42FF98C4E85212a5D31358ACbFe76a621b50fC02",
93
+ 480: "0x17B354dD2595411ff79041f930e491A4Df39A278",
94
+ 4801: "0x57f928158C3EE7CDad1e4D8642503c4D0201f611",
95
+ };
96
+ /** @deprecated Use `USDC_BY_CHAIN_ID`; retained while downstream packages migrate names. */
97
+ exports.WORLD_CHAIN_USDC_BY_CHAIN_ID = exports.USDC_BY_CHAIN_ID;
98
+ exports.BOUNTY_ELIGIBILITY_CREDENTIAL_MASK = 0x0e;
99
+ exports.BOUNTY_ELIGIBILITY_RECENT_RECHECK_FLAG = 0x80;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,336 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.unpackVoteRoundContext = exports.predictionPercentToBps = exports.parseTlockCiphertextMetadata = exports.packVoteRoundContext = exports.normalizePredictedUpBps = exports.encodeRbtsVotePlaintext = exports.deriveVoteTlockRevealAvailableAtSeconds = exports.decodeRbtsVotePlaintext = exports.buildRbtsCommitHash = exports.buildCommitKey = exports.buildCommitHash = exports.bpsToPredictionPercent = exports.MIN_PREDICTED_UP_PERCENT = exports.MIN_PREDICTED_UP_BPS = exports.MAX_PREDICTED_UP_PERCENT = exports.MAX_PREDICTED_UP_BPS = void 0;
37
+ exports.getVoteTlockChainInfo = getVoteTlockChainInfo;
38
+ exports.tlockEncryptVote = tlockEncryptVote;
39
+ exports.decryptTlockVoteCiphertext = decryptTlockVoteCiphertext;
40
+ exports.createTlockVoteCommit = createTlockVoteCommit;
41
+ exports.createTlockRbtsVoteCommit = createTlockRbtsVoteCommit;
42
+ const viem_1 = require("viem");
43
+ const votingCore_1 = require("./votingCore");
44
+ var votingCore_2 = require("./votingCore");
45
+ Object.defineProperty(exports, "MAX_PREDICTED_UP_BPS", { enumerable: true, get: function () { return votingCore_2.MAX_PREDICTED_UP_BPS; } });
46
+ Object.defineProperty(exports, "MAX_PREDICTED_UP_PERCENT", { enumerable: true, get: function () { return votingCore_2.MAX_PREDICTED_UP_PERCENT; } });
47
+ Object.defineProperty(exports, "MIN_PREDICTED_UP_BPS", { enumerable: true, get: function () { return votingCore_2.MIN_PREDICTED_UP_BPS; } });
48
+ Object.defineProperty(exports, "MIN_PREDICTED_UP_PERCENT", { enumerable: true, get: function () { return votingCore_2.MIN_PREDICTED_UP_PERCENT; } });
49
+ Object.defineProperty(exports, "bpsToPredictionPercent", { enumerable: true, get: function () { return votingCore_2.bpsToPredictionPercent; } });
50
+ Object.defineProperty(exports, "buildCommitHash", { enumerable: true, get: function () { return votingCore_2.buildCommitHash; } });
51
+ Object.defineProperty(exports, "buildCommitKey", { enumerable: true, get: function () { return votingCore_2.buildCommitKey; } });
52
+ Object.defineProperty(exports, "buildRbtsCommitHash", { enumerable: true, get: function () { return votingCore_2.buildRbtsCommitHash; } });
53
+ Object.defineProperty(exports, "decodeRbtsVotePlaintext", { enumerable: true, get: function () { return votingCore_2.decodeRbtsVotePlaintext; } });
54
+ Object.defineProperty(exports, "deriveVoteTlockRevealAvailableAtSeconds", { enumerable: true, get: function () { return votingCore_2.deriveVoteTlockRevealAvailableAtSeconds; } });
55
+ Object.defineProperty(exports, "encodeRbtsVotePlaintext", { enumerable: true, get: function () { return votingCore_2.encodeRbtsVotePlaintext; } });
56
+ Object.defineProperty(exports, "normalizePredictedUpBps", { enumerable: true, get: function () { return votingCore_2.normalizePredictedUpBps; } });
57
+ Object.defineProperty(exports, "packVoteRoundContext", { enumerable: true, get: function () { return votingCore_2.packVoteRoundContext; } });
58
+ Object.defineProperty(exports, "parseTlockCiphertextMetadata", { enumerable: true, get: function () { return votingCore_2.parseTlockCiphertextMetadata; } });
59
+ Object.defineProperty(exports, "predictionPercentToBps", { enumerable: true, get: function () { return votingCore_2.predictionPercentToBps; } });
60
+ Object.defineProperty(exports, "unpackVoteRoundContext", { enumerable: true, get: function () { return votingCore_2.unpackVoteRoundContext; } });
61
+ let tlockModulePromise;
62
+ const AGE_ARMOR_HEADER = "-----BEGIN AGE ENCRYPTED FILE-----";
63
+ const AGE_ARMOR_FOOTER = "-----END AGE ENCRYPTED FILE-----";
64
+ const MIN_ENCRYPTED_BODY_LENGTH = 65;
65
+ const MAINNET_QUICKNET_CHAIN_HASH = "52db9ba70e0cc0f6eaf7803dd07447a1f5477735fd3f661792ba94600c84e971";
66
+ const TLOCK_JS_TESTNET_CHAIN_HASH = "7672797f548f3f4748ac4bf3352fc6c6b6468c9ad40ad456a397545c6e2df5bf";
67
+ const QUICKNET_T_CHAIN = {
68
+ url: "https://testnet-api.drand.cloudflare.com/cc9c398442737cbd141526600919edd69f1d6f9b4adb67e4d912fbc64341a9a5",
69
+ chainHash: "cc9c398442737cbd141526600919edd69f1d6f9b4adb67e4d912fbc64341a9a5",
70
+ publicKey: "b15b65b46fb29104f6a4b5d1e11a8da6344463973d423661bb0804846a0ecd1ef93c25057f1c0baab2ac53e56c662b66072f6d84ee791a3382bfb055afab1e6a375538d8ffc451104ac971d2dc9b168e2d3246b0be2015969cbaac298f6502da",
71
+ };
72
+ const RATELOOP_TLOCK_USER_AGENT = "rateloop-tlock";
73
+ async function loadTlockModule() {
74
+ tlockModulePromise ??= Promise.resolve().then(() => __importStar(require("tlock-js"))).then((module) => ({
75
+ HttpCachingChain: module.HttpCachingChain,
76
+ HttpChainClient: module.HttpChainClient,
77
+ mainnetClient: module.mainnetClient,
78
+ testnetClient: module.testnetClient,
79
+ timelockEncrypt: module.timelockEncrypt,
80
+ timelockDecrypt: module.timelockDecrypt,
81
+ }));
82
+ return tlockModulePromise;
83
+ }
84
+ function normalizeDrandChainHash(hash) {
85
+ if (!hash)
86
+ return null;
87
+ const normalized = hash.toLowerCase();
88
+ if (!/^0x[0-9a-f]{64}$/u.test(normalized)) {
89
+ throw new Error("Invalid drand chain hash");
90
+ }
91
+ return normalized.slice(2);
92
+ }
93
+ function normalizeOptionalPositiveBigInt(value, label) {
94
+ if (value == null)
95
+ return null;
96
+ const normalized = typeof value === "bigint" ? value : BigInt(Math.trunc(value));
97
+ if (normalized <= 0n) {
98
+ throw new Error(`Invalid drand ${label}`);
99
+ }
100
+ return normalized;
101
+ }
102
+ function createHttpTlockClient(tlockModule, chain) {
103
+ const options = {
104
+ disableBeaconVerification: false,
105
+ noCache: false,
106
+ chainVerificationParams: {
107
+ chainHash: chain.chainHash,
108
+ publicKey: chain.publicKey,
109
+ },
110
+ };
111
+ const httpChain = new tlockModule.HttpCachingChain(chain.url, options);
112
+ return new tlockModule.HttpChainClient(httpChain, options, {
113
+ userAgent: RATELOOP_TLOCK_USER_AGENT,
114
+ });
115
+ }
116
+ function resolveTlockClientForRuntime(tlockModule, runtime = {}) {
117
+ if (runtime.client) {
118
+ return runtime.client;
119
+ }
120
+ const expectedHash = normalizeDrandChainHash(runtime.drandChainHash);
121
+ if (!expectedHash || expectedHash === MAINNET_QUICKNET_CHAIN_HASH) {
122
+ return tlockModule.mainnetClient();
123
+ }
124
+ if (expectedHash === QUICKNET_T_CHAIN.chainHash) {
125
+ return createHttpTlockClient(tlockModule, QUICKNET_T_CHAIN);
126
+ }
127
+ if (expectedHash === TLOCK_JS_TESTNET_CHAIN_HASH) {
128
+ return tlockModule.testnetClient();
129
+ }
130
+ throw new Error(`Unsupported drand chain 0x${expectedHash}. Update ProtocolConfig to drand quicknet or quicknet-t before voting.`);
131
+ }
132
+ function assertTlockChainInfoMatchesRuntime(chainInfo, runtime = {}) {
133
+ const expectedHash = normalizeDrandChainHash(runtime.drandChainHash);
134
+ if (!expectedHash)
135
+ return;
136
+ const actualHash = chainInfo.hash.toLowerCase();
137
+ if (actualHash !== expectedHash) {
138
+ throw new Error(`Tlock client chain 0x${actualHash} does not match vote round drand chain 0x${expectedHash}.`);
139
+ }
140
+ const expectedGenesisTime = normalizeOptionalPositiveBigInt(runtime.drandGenesisTimeSeconds, "genesis time");
141
+ if (expectedGenesisTime != null &&
142
+ BigInt(chainInfo.genesis_time) !== expectedGenesisTime) {
143
+ throw new Error(`Tlock client genesis ${chainInfo.genesis_time} does not match vote round drand genesis ${expectedGenesisTime.toString()}.`);
144
+ }
145
+ const expectedPeriod = normalizeOptionalPositiveBigInt(runtime.drandPeriodSeconds, "period");
146
+ if (expectedPeriod != null && BigInt(chainInfo.period) !== expectedPeriod) {
147
+ throw new Error(`Tlock client period ${chainInfo.period} does not match vote round drand period ${expectedPeriod.toString()}.`);
148
+ }
149
+ }
150
+ async function getVoteTlockChainInfo(runtime = {}) {
151
+ const tlockModule = await loadTlockModule();
152
+ const client = resolveTlockClientForRuntime(tlockModule, runtime);
153
+ const chainInfo = await client.chain().info();
154
+ assertTlockChainInfoMatchesRuntime(chainInfo, runtime);
155
+ return {
156
+ periodSeconds: BigInt(chainInfo.period),
157
+ genesisTimeSeconds: BigInt(chainInfo.genesis_time),
158
+ drandChainHash: `0x${chainInfo.hash.toLowerCase()}`,
159
+ };
160
+ }
161
+ async function createTlockVoteArtifacts(isUp, predictedUpBps, salt, epochDurationSeconds, runtime = {}) {
162
+ const tlockModule = await loadTlockModule();
163
+ const { timelockEncrypt } = tlockModule;
164
+ const client = resolveTlockClientForRuntime(tlockModule, runtime);
165
+ const now = runtime.now ?? Date.now;
166
+ const encryptFn = runtime.encryptFn ?? timelockEncrypt;
167
+ const chainInfo = await client.chain().info();
168
+ assertTlockChainInfoMatchesRuntime(chainInfo, runtime);
169
+ const targetRound = runtime.targetRound != null
170
+ ? normalizeTlockTargetRound(runtime.targetRound)
171
+ : deriveAcceptedTlockTargetRound(now(), epochDurationSeconds, chainInfo, runtime.roundStartTimeSeconds, runtime.candidateTimestampOffsetsSeconds);
172
+ const armored = await encryptFn(targetRound, (0, votingCore_1.encodeRbtsVotePlaintext)(isUp, predictedUpBps, salt), client);
173
+ const ciphertext = (0, viem_1.stringToHex)(armored);
174
+ return {
175
+ ciphertext,
176
+ ciphertextHash: (0, viem_1.keccak256)(ciphertext),
177
+ targetRound: BigInt(targetRound),
178
+ drandChainHash: `0x${chainInfo.hash.toLowerCase()}`,
179
+ };
180
+ }
181
+ function roundAtOrAfter(targetTimeMs, chainInfo) {
182
+ if (!Number.isFinite(targetTimeMs)) {
183
+ throw new Error("Cannot use Infinity or NaN as a beacon time");
184
+ }
185
+ const genesisTimeMs = chainInfo.genesis_time * 1000;
186
+ const periodMs = chainInfo.period * 1000;
187
+ if (!Number.isFinite(genesisTimeMs) ||
188
+ !Number.isFinite(periodMs) ||
189
+ periodMs <= 0) {
190
+ throw new Error("Invalid tlock chain timing");
191
+ }
192
+ if (targetTimeMs < genesisTimeMs) {
193
+ throw new Error("Cannot request a round before the genesis time");
194
+ }
195
+ return Math.ceil((targetTimeMs - genesisTimeMs) / periodMs) + 1;
196
+ }
197
+ function roundAt(targetTimeMs, chainInfo) {
198
+ const genesisTimeMs = chainInfo.genesis_time * 1000;
199
+ const periodMs = chainInfo.period * 1000;
200
+ if (!Number.isFinite(genesisTimeMs) ||
201
+ !Number.isFinite(periodMs) ||
202
+ periodMs <= 0) {
203
+ throw new Error("Invalid tlock chain timing");
204
+ }
205
+ if (targetTimeMs < genesisTimeMs) {
206
+ throw new Error("Cannot request a round before the genesis time");
207
+ }
208
+ return Math.floor((targetTimeMs - genesisTimeMs) / periodMs) + 1;
209
+ }
210
+ function normalizeRoundStartTimeMs(roundStartTimeSeconds) {
211
+ if (roundStartTimeSeconds == null)
212
+ return null;
213
+ const normalized = typeof roundStartTimeSeconds === "bigint"
214
+ ? Number(roundStartTimeSeconds)
215
+ : Number(roundStartTimeSeconds);
216
+ if (!Number.isFinite(normalized) || normalized <= 0)
217
+ return null;
218
+ return Math.floor(normalized) * 1000;
219
+ }
220
+ function deriveRevealableAfterMs(commitTimeMs, epochDurationSeconds, roundStartTimeMs) {
221
+ const epochDurationMs = Math.max(1, Math.floor(epochDurationSeconds)) * 1000;
222
+ const anchorTimeMs = roundStartTimeMs ?? commitTimeMs;
223
+ const elapsedMs = Math.max(0, commitTimeMs - anchorTimeMs);
224
+ const epochIndex = Math.floor(elapsedMs / epochDurationMs);
225
+ return anchorTimeMs + (epochIndex + 1) * epochDurationMs;
226
+ }
227
+ function deriveAcceptedTlockTargetRound(nowMs, epochDurationSeconds, chainInfo, roundStartTimeSeconds, candidateTimestampOffsetsSeconds) {
228
+ if (!Number.isFinite(nowMs)) {
229
+ throw new Error("Cannot use Infinity or NaN as a beacon time");
230
+ }
231
+ const roundStartTimeMs = normalizeRoundStartTimeMs(roundStartTimeSeconds);
232
+ const drandPeriodMs = Math.max(1, Math.floor(chainInfo.period)) * 1000;
233
+ const candidateOffsets = candidateTimestampOffsetsSeconds &&
234
+ candidateTimestampOffsetsSeconds.length > 0
235
+ ? candidateTimestampOffsetsSeconds
236
+ : buildDefaultCandidateTimestampOffsetsSeconds(chainInfo.period);
237
+ let minAcceptedTargetRound = 0;
238
+ let maxAcceptedTargetRound = 0;
239
+ for (const offsetSeconds of candidateOffsets) {
240
+ const commitTimeMs = nowMs + Math.floor(offsetSeconds) * 1000;
241
+ const revealableAfterMs = deriveRevealableAfterMs(commitTimeMs, epochDurationSeconds, roundStartTimeMs);
242
+ const minTargetRound = roundAtOrAfter(revealableAfterMs, chainInfo);
243
+ const maxTargetRound = roundAt(revealableAfterMs + 2 * drandPeriodMs, chainInfo);
244
+ if (minTargetRound <= 0 ||
245
+ maxTargetRound <= 0 ||
246
+ minTargetRound > maxTargetRound) {
247
+ throw new Error("No valid drand target round for the commit window");
248
+ }
249
+ minAcceptedTargetRound = Math.max(minAcceptedTargetRound, minTargetRound);
250
+ maxAcceptedTargetRound =
251
+ maxAcceptedTargetRound === 0
252
+ ? maxTargetRound
253
+ : Math.min(maxAcceptedTargetRound, maxTargetRound);
254
+ }
255
+ if (minAcceptedTargetRound === 0 ||
256
+ minAcceptedTargetRound > maxAcceptedTargetRound) {
257
+ throw new Error("No shared drand target round for commit windows");
258
+ }
259
+ return maxAcceptedTargetRound;
260
+ }
261
+ function buildDefaultCandidateTimestampOffsetsSeconds(drandPeriodSeconds) {
262
+ const safePeriodSeconds = Math.max(1, Math.floor(drandPeriodSeconds));
263
+ return Array.from({ length: safePeriodSeconds }, (_, index) => index);
264
+ }
265
+ function normalizeTlockTargetRound(targetRound) {
266
+ const normalized = typeof targetRound === "bigint"
267
+ ? Number(targetRound)
268
+ : Number.isInteger(targetRound)
269
+ ? targetRound
270
+ : Number.NaN;
271
+ if (!Number.isSafeInteger(normalized) || normalized <= 0) {
272
+ throw new Error("targetRound must be a positive safe integer");
273
+ }
274
+ return normalized;
275
+ }
276
+ async function tlockEncryptVote(isUp, predictedUpBps, salt, epochDurationSeconds, runtime = {}) {
277
+ const { ciphertext } = await createTlockVoteArtifacts(isUp, predictedUpBps, salt, epochDurationSeconds, runtime);
278
+ return ciphertext;
279
+ }
280
+ async function decryptTlockVoteCiphertext(ciphertext, runtime = {}) {
281
+ const tlockModule = await loadTlockModule();
282
+ const { timelockDecrypt } = tlockModule;
283
+ const client = resolveTlockClientForRuntime(tlockModule, runtime);
284
+ const decryptFn = runtime.decryptFn ?? timelockDecrypt;
285
+ const armored = (0, viem_1.hexToString)(ciphertext);
286
+ // Cheap structural sanity check before handing the payload to the tlock library.
287
+ // The age armor header alone is ~36 chars and the smallest valid body is bounded by
288
+ // MIN_ENCRYPTED_BODY_LENGTH, so anything shorter than the armor framing + body
289
+ // floor cannot plausibly decrypt to our 36-byte RBTS plaintext.
290
+ if (armored.length <
291
+ AGE_ARMOR_HEADER.length +
292
+ AGE_ARMOR_FOOTER.length +
293
+ MIN_ENCRYPTED_BODY_LENGTH) {
294
+ return null;
295
+ }
296
+ if (!armored.includes(AGE_ARMOR_HEADER) ||
297
+ !armored.includes(AGE_ARMOR_FOOTER)) {
298
+ return null;
299
+ }
300
+ if (runtime.drandChainHash) {
301
+ const chainInfo = await client.chain().info();
302
+ assertTlockChainInfoMatchesRuntime(chainInfo, runtime);
303
+ }
304
+ const plaintext = await decryptFn(armored, client);
305
+ return (0, votingCore_1.decodeRbtsVotePlaintext)(plaintext);
306
+ }
307
+ async function createTlockVoteCommit(params, runtime = {}) {
308
+ const { ciphertext, ciphertextHash, targetRound, drandChainHash } = await createTlockVoteArtifacts(params.isUp, params.predictedUpBps, params.salt, params.epochDurationSeconds, runtime);
309
+ const commitHash = (0, votingCore_1.buildCommitHash)(params.isUp, params.predictedUpBps, params.salt, params.voter, params.contentId, params.roundId, params.roundReferenceRatingBps, targetRound, drandChainHash, ciphertext);
310
+ return {
311
+ ciphertext,
312
+ ciphertextHash,
313
+ commitHash,
314
+ targetRound,
315
+ drandChainHash,
316
+ roundReferenceRatingBps: params.roundReferenceRatingBps,
317
+ commitKey: (0, votingCore_1.buildCommitKey)(params.voter, commitHash),
318
+ };
319
+ }
320
+ async function createTlockRbtsVoteCommit(params, runtime = {}) {
321
+ const predictedUpBps = (0, votingCore_1.normalizePredictedUpBps)(params.predictedUpBps);
322
+ const { ciphertext, ciphertextHash, targetRound, drandChainHash } = await createTlockVoteArtifacts(params.isUp, predictedUpBps, params.salt, params.epochDurationSeconds, runtime);
323
+ const commitHash = (0, votingCore_1.buildRbtsCommitHash)(params.isUp, predictedUpBps, params.salt, params.voter, params.contentId, params.roundId, params.roundReferenceRatingBps, targetRound, drandChainHash, ciphertext);
324
+ return {
325
+ ciphertext,
326
+ ciphertextHash,
327
+ commitHash,
328
+ targetRound,
329
+ drandChainHash,
330
+ roundReferenceRatingBps: params.roundReferenceRatingBps,
331
+ isUp: params.isUp,
332
+ predictedUpBps,
333
+ predictedUpPercent: (0, votingCore_1.bpsToPredictionPercent)(predictedUpBps),
334
+ commitKey: (0, votingCore_1.buildCommitKey)(params.voter, commitHash),
335
+ };
336
+ }