MCPcopy
hub / github.com/tiagozip/cap / createChallenge

Method createChallenge

server/index.js:268–294  ·  view source on GitHub ↗

* Generates a new challenge * @param {ChallengeConfig} [conf] - Challenge configuration * @returns {Promise<{ challenge: {c: number, s: number, d: number}, token?: string, expires: number }>} Challenge data

(conf)

Source from the content-addressed store, hash-verified

266 * @returns {Promise<{ challenge: {c: number, s: number, d: number}, token?: string, expires: number }>} Challenge data
267 */
268 async createChallenge(conf) {
269 await this._lazyCleanup();
270
271 /** @type {{c: number, s: number, d: number}} */
272 const challenge = {
273 c: conf?.challengeCount ?? 50,
274 s: conf?.challengeSize ?? 32,
275 d: conf?.challengeDifficulty ?? 4,
276 };
277
278 const token = await randomHex(25);
279 const expires = Date.now() + (conf?.expiresMs ?? 600000);
280
281 if (conf && conf.store === false) {
282 return { challenge, expires };
283 }
284
285 const challengeData = { expires, challenge };
286
287 if (this.config.storage?.challenges?.store) {
288 await this.config.storage.challenges.store(token, challengeData);
289 } else {
290 this.config.state.challengesList[token] = challengeData;
291 }
292
293 return { challenge, token, expires };
294 }
295
296 /**
297 * Redeems a challenge solution in exchange for a token

Callers 8

capMiddlewareFunction · 0.95
capCheckpointFunction · 0.95
capCheckpointFunction · 0.95
cap.jsFile · 0.80
index.jsFile · 0.80
index.jsFile · 0.80
index.jsFile · 0.80
index.jsFile · 0.80

Calls 2

_lazyCleanupMethod · 0.95
randomHexFunction · 0.85

Tested by

no test coverage detected