Skip to content

Commit b2fa9cb

Browse files
committed
Resolved comments
Signed-off-by: Rayane Charif <rayane.charif@gonative.cc>
1 parent 597907d commit b2fa9cb

File tree

7 files changed

+64
-42
lines changed

7 files changed

+64
-42
lines changed

bun.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/btcindexer/src/btcindexer.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
type FinalizeRedeemTx,
1717
} from "@gonative-cc/sui-indexer/rpc-interface";
1818
import { logError, logger } from "@gonative-cc/lib/logger";
19-
import { getMnemonic } from "@gonative-cc/lib/secrets";
19+
import { getSecret } from "@gonative-cc/lib/secrets";
2020
import { isValidSuiAddress } from "@mysten/sui/utils";
2121
import { OP_RETURN } from "./opcodes";
2222
import { BitcoinMerkleTree } from "./bitcoin-merkle-tree";
@@ -74,10 +74,7 @@ export async function indexerFromEnv(env: Env): Promise<Indexer> {
7474
throw new Error("Invalid MAX_NBTC_MINT_TX_RETRIES in config. Must be a number >= 0.");
7575
}
7676

77-
const mnemonic = await getMnemonic(env.NBTC_MINTING_SIGNER_MNEMONIC);
78-
if (!mnemonic) {
79-
throw new Error("Failed to retrieve mnemonic");
80-
}
77+
const mnemonic = await getSecret(env.NBTC_MINTING_SIGNER_MNEMONIC);
8178
const suiClients = new Map<SuiNet, SuiClient>();
8279
for (const p of packageConfigs) {
8380
if (!suiClients.has(p.sui_network))

packages/lib/src/secrets.ts

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,15 @@
1-
import { logger } from "./logger";
2-
31
interface SecretStore {
42
get(): Promise<string>;
53
}
64

75
/**
8-
* Retrieves the mnemonic from the secrets store with proper error handling.
9-
* Returns the mnemonic string or null if not found/failed.
6+
* Retrieves a secret from the secrets store.
7+
* Throws if the secret is not found or retrieval fails.
108
*/
11-
export async function getMnemonic(secret: SecretStore): Promise<string | null> {
12-
try {
13-
const mnemonic = await secret.get();
14-
if (!mnemonic) {
15-
logger.error({ msg: "Missing NBTC_MINTING_SIGNER_MNEMONIC" });
16-
return null;
17-
}
18-
return mnemonic;
19-
} catch (error) {
20-
logger.error({ msg: "Failed to retrieve NBTC_MINTING_SIGNER_MNEMONIC", error });
21-
return null;
9+
export async function getSecret(secret: SecretStore): Promise<string> {
10+
const value = await secret.get();
11+
if (!value) {
12+
throw new Error("Secret not found in store");
2213
}
14+
return value;
2315
}

packages/sui-indexer/src/handler.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,17 @@ export class IkaEventHandler {
124124

125125
private async handleCompletedSign(e: SuiEventNode) {
126126
const data = e.json as CompletedSignEventRaw;
127-
const signId = data.sign_id as string;
127+
if (typeof data.sign_id !== "string") {
128+
logger.error({
129+
msg: "Unexpected sign_id type in CompletedSignEvent",
130+
type: typeof data.sign_id,
131+
});
132+
return;
133+
}
134+
const signId = data.sign_id;
128135

129136
// IKA coordinator is shared across protocols, so we only process sign IDs that match our redeems.
130-
// The final signature is recorded via SignatureRecordedEvent from nbtc.move (handled above).
137+
// The final signature is recorded via SignatureRecordedEvent from nbtc.move (handled in NbtcEventHandler).
131138
const redeemInfo = await this.storage.getRedeemInfoBySignId(signId);
132139
if (!redeemInfo) {
133140
return;
@@ -137,7 +144,6 @@ export class IkaEventHandler {
137144
msg: "Ika signature completed",
138145
sign_id: signId,
139146
is_future_sign: data.is_future_sign,
140-
signature_length: data.signature.length,
141147
txDigest: e.txDigest,
142148
});
143149

@@ -169,7 +175,14 @@ export class IkaEventHandler {
169175

170176
private async handleRejectedSign(e: SuiEventNode) {
171177
const data = e.json as RejectedSignEventRaw;
172-
const signId = data.sign_id as string;
178+
if (typeof data.sign_id !== "string") {
179+
logger.error({
180+
msg: "Unexpected sign_id type in RejectedSignEvent",
181+
type: typeof data.sign_id,
182+
});
183+
return;
184+
}
185+
const signId = data.sign_id;
173186
const redeemInfo = await this.storage.getRedeemInfoBySignId(signId);
174187
if (!redeemInfo) {
175188
return;

packages/sui-indexer/src/index.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { NetworkConfig } from "./models";
44
import { Processor } from "./processor";
55
import { D1Storage } from "./storage";
66
import { logError, logger } from "@gonative-cc/lib/logger";
7-
import { getMnemonic } from "@gonative-cc/lib/secrets";
7+
import { getSecret } from "@gonative-cc/lib/secrets";
88
import { RedeemService } from "./redeem-service";
99
import { createSuiClients, type SuiClient } from "./redeem-sui-client";
1010
import type { Service } from "@cloudflare/workers-types";
@@ -27,8 +27,7 @@ export default {
2727
const storage = new D1Storage(env.DB);
2828
const activeNetworks = await storage.getActiveNetworks();
2929

30-
const mnemonic = await getMnemonic(env.NBTC_MINTING_SIGNER_MNEMONIC);
31-
if (!mnemonic) return;
30+
const mnemonic = await getSecret(env.NBTC_MINTING_SIGNER_MNEMONIC);
3231
const suiClients = await createSuiClients(activeNetworks, mnemonic);
3332

3433
// Run both indexer and redeem solver tasks in parallel
@@ -88,8 +87,12 @@ async function poolAndProcessEvents(
8887
storage: D1Storage,
8988
suiClients: Map<SuiNet, SuiClient>,
9089
) {
91-
const client = new SuiGraphQLClient(netCfg.url);
9290
const suiClient = suiClients.get(netCfg.name);
91+
if (!suiClient) {
92+
logger.warn({ msg: "No SuiClient for network, skipping", network: netCfg.name });
93+
return;
94+
}
95+
const client = new SuiGraphQLClient(netCfg.url);
9396
const p = new Processor(netCfg, storage, client, suiClient);
9497

9598
const nbtcPkgs = await storage.getActiveNbtcPkgs(netCfg.name);

packages/sui-indexer/src/processor.ts

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ export class Processor {
99
netCfg: NetworkConfig;
1010
storage: D1Storage;
1111
eventFetcher: EventFetcher;
12-
suiClient?: SuiClient;
12+
suiClient: SuiClient;
1313

1414
constructor(
1515
netCfg: NetworkConfig,
1616
storage: D1Storage,
1717
eventFetcher: EventFetcher,
18-
suiClient?: SuiClient,
18+
suiClient: SuiClient,
1919
) {
2020
this.netCfg = netCfg;
2121
this.storage = storage;
@@ -120,16 +120,9 @@ export class Processor {
120120
endCursor: result.endCursor,
121121
});
122122

123-
if (result.events.length > 0 && this.suiClient) {
123+
if (result.events.length > 0) {
124124
const handler = new IkaEventHandler(this.storage, this.suiClient);
125125
await handler.handleEvents(result.events);
126-
} else if (result.events.length > 0 && !this.suiClient) {
127-
logger.warn({
128-
msg: "Skipping IKA events: suiClient not initialized",
129-
network: this.netCfg.name,
130-
coordinatorPkgId: pkgId,
131-
eventsLength: result.events.length,
132-
});
133126
}
134127

135128
if (result.endCursor && result.endCursor !== cursors[pkgId]) {

packages/sui-indexer/src/rpc.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { WorkerEntrypoint } from "cloudflare:workers";
22
import { logError, logger } from "@gonative-cc/lib/logger";
3+
import { getSecret } from "@gonative-cc/lib/secrets";
34
import { fromBase64 } from "@mysten/sui/utils";
45

56
import { D1Storage } from "./storage";
@@ -29,10 +30,7 @@ export class RPC extends WorkerEntrypoint<Env> implements SuiIndexerRpc {
2930
if (requests.length === 0) return;
3031

3132
const storage = new D1Storage(this.env.DB);
32-
const mnemonic = await this.env.NBTC_MINTING_SIGNER_MNEMONIC.get();
33-
if (!mnemonic) {
34-
throw new Error("NBTC_MINTING_SIGNER_MNEMONIC not set");
35-
}
33+
const mnemonic = await getSecret(this.env.NBTC_MINTING_SIGNER_MNEMONIC);
3634

3735
const { redeemsById, networks } = await this.fetchRedeemDetails(storage, requests);
3836
if (networks.size === 0) return;

0 commit comments

Comments
 (0)