nara-sdk 1.0.35 → 1.0.37

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 (3) hide show
  1. package/index.ts +2 -0
  2. package/package.json +1 -1
  3. package/src/quest.ts +50 -0
package/index.ts CHANGED
@@ -24,6 +24,8 @@ export {
24
24
  submitAnswer,
25
25
  submitAnswerViaRelay,
26
26
  parseQuestReward,
27
+ computeAnswerHash,
28
+ createQuestion,
27
29
  type QuestInfo,
28
30
  type ZkProof,
29
31
  type ZkProofHex,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nara-sdk",
3
- "version": "1.0.35",
3
+ "version": "1.0.37",
4
4
  "description": "SDK for the Nara chain (Solana-compatible)",
5
5
  "module": "index.ts",
6
6
  "main": "index.ts",
package/src/quest.ts CHANGED
@@ -11,6 +11,7 @@ import {
11
11
  } from "@solana/web3.js";
12
12
  import * as anchor from "@coral-xyz/anchor";
13
13
  import { Program, AnchorProvider, Wallet } from "@coral-xyz/anchor";
14
+ import BN from "bn.js";
14
15
  import type { NaraQuest } from "./idls/nara_quest";
15
16
  import { DEFAULT_QUEST_PROGRAM_ID } from "./constants";
16
17
 
@@ -435,3 +436,52 @@ export async function parseQuestReward(
435
436
  winner,
436
437
  };
437
438
  }
439
+
440
+ /**
441
+ * Compute the Poseidon answer hash for a given answer string.
442
+ * Uses answerToField (UTF-8 encoding) consistent with on-chain question creation.
443
+ */
444
+ export async function computeAnswerHash(answer: string): Promise<number[]> {
445
+ const circomlibjs = await import("circomlibjs");
446
+ const poseidon = await circomlibjs.buildPoseidon();
447
+ const fieldVal = answerToField(answer);
448
+ const hashRaw = poseidon([fieldVal]);
449
+ const hashStr: string = poseidon.F.toString(hashRaw);
450
+ return Array.from(toBigEndian32(BigInt(hashStr)));
451
+ }
452
+
453
+ /**
454
+ * Create a new quest question on-chain (authority only).
455
+ *
456
+ * @param connection - Solana connection
457
+ * @param wallet - Authority keypair (must be the program authority)
458
+ * @param question - The question text
459
+ * @param answer - The answer string (will be hashed with Poseidon + answerToField)
460
+ * @param deadlineSeconds - Duration in seconds from now until the deadline
461
+ * @param rewardSol - Total reward amount in SOL/NARA
462
+ * @param difficulty - Difficulty level (default: 1)
463
+ * @param options - Optional program ID override
464
+ */
465
+ export async function createQuestion(
466
+ connection: Connection,
467
+ wallet: Keypair,
468
+ question: string,
469
+ answer: string,
470
+ deadlineSeconds: number,
471
+ rewardSol: number,
472
+ difficulty: number = 1,
473
+ options?: QuestOptions
474
+ ): Promise<string> {
475
+ const program = createProgram(connection, wallet, options?.programId);
476
+ const answerHash = await computeAnswerHash(answer);
477
+ const deadline = new BN(Math.floor(Date.now() / 1000) + deadlineSeconds);
478
+ const rewardAmount = new BN(Math.round(rewardSol * LAMPORTS_PER_SOL));
479
+
480
+ const signature = await program.methods
481
+ .createQuestion(question, answerHash as any, deadline, rewardAmount, difficulty)
482
+ .accounts({ authority: wallet.publicKey } as any)
483
+ .signers([wallet])
484
+ .rpc();
485
+
486
+ return signature;
487
+ }