Skip to content

Commit 505ac3b

Browse files
authored
Merge branch 'master' into rayane/coin-race-condition-docs
2 parents 2d37861 + 9615ee0 commit 505ac3b

File tree

5 files changed

+50
-29
lines changed

5 files changed

+50
-29
lines changed

bun.lock

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"typescript": "^5.9.3",
2929
"typescript-eslint": "^8.49.0",
3030
"wrangler": "^4.54.0",
31-
"@gonative-cc/nbtc": "^0.1.0"
31+
"@gonative-cc/nbtc": "^0.1.1"
3232
},
3333
"lint-staged": {
3434
"*.{js,cjs,mjs,jsx,ts,tsx}": [

packages/sui-indexer/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"@gonative-cc/btcindexer": "workspace:*",
1717
"@gonative-cc/lib": "workspace:*",
18+
"@gonative-cc/nbtc": "0.1.1",
1819
"@ika.xyz/sdk": "^0.2.7",
1920
"@mysten/bcs": "^1.9.2",
2021
"@mysten/sui": "^1.45.2",

packages/sui-indexer/src/redeem-service.ts

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,14 +111,14 @@ export class RedeemService {
111111

112112
private async processSolvedRedeem(req: RedeemRequestWithInputs) {
113113
const client = this.getSuiClient(req.sui_network);
114+
const inputsToVerify: RedeemInput[] = [];
114115

115116
for (const input of req.inputs) {
116117
try {
117118
if (!input.sign_id) {
118119
await this.requestIkaSig(client, req, input);
119120
} else if (input.sign_id && !input.verified) {
120-
// TODO: this should be triggered when getting the event from ika
121-
await this.recordIkaSig(client, req, input);
121+
inputsToVerify.push(input);
122122
}
123123
} catch (e) {
124124
logError(
@@ -133,6 +133,22 @@ export class RedeemService {
133133
);
134134
}
135135
}
136+
137+
if (inputsToVerify.length > 0) {
138+
try {
139+
await this.recordIkaSignatures(client, req, inputsToVerify);
140+
} catch (e) {
141+
logError(
142+
{
143+
msg: "Failed to batch verify signatures",
144+
method: "processSolvedRedeem",
145+
redeemId: req.redeem_id,
146+
count: inputsToVerify.length,
147+
},
148+
e,
149+
);
150+
}
151+
}
136152
}
137153
// TODO: handle front runs
138154
private async requestIkaSig(
@@ -260,36 +276,40 @@ export class RedeemService {
260276
}
261277
}
262278

263-
private async recordIkaSig(
279+
private async recordIkaSignatures(
264280
client: SuiClient,
265281
req: RedeemRequestWithInputs,
266-
input: RedeemInput,
282+
inputs: RedeemInput[],
267283
) {
284+
const inputsWithSignId = inputs.filter(
285+
(input): input is RedeemInput & { sign_id: string } => input.sign_id !== null,
286+
);
287+
288+
if (inputsWithSignId.length === 0) {
289+
return;
290+
}
291+
268292
logger.info({
269-
msg: "Verifying signature for input",
293+
msg: "Batch verifying signatures",
270294
redeemId: req.redeem_id,
271-
utxoId: input.utxo_id,
272-
inputIdx: input.input_index,
273-
signId: input.sign_id,
295+
count: inputsWithSignId.length,
274296
});
275297

276-
if (!input.sign_id) {
277-
throw new Error("Input signature ID is missing");
278-
}
279-
280-
await client.validateSignature(
298+
await client.validateSignatures(
281299
req.redeem_id,
282-
input.input_index,
283-
input.sign_id,
300+
inputsWithSignId,
284301
req.nbtc_pkg,
285302
req.nbtc_contract,
286303
);
287304

288-
await this.storage.markRedeemInputVerified(req.redeem_id, input.utxo_id);
305+
for (const input of inputsWithSignId) {
306+
await this.storage.markRedeemInputVerified(req.redeem_id, input.utxo_id);
307+
}
308+
289309
logger.info({
290-
msg: "Signature verified",
310+
msg: "Signatures verified",
291311
redeemId: req.redeem_id,
292-
utxoId: input.utxo_id,
312+
count: inputsWithSignId.length,
293313
});
294314
}
295315

packages/sui-indexer/src/redeem-sui-client.ts

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ export interface SuiClient {
3131
nbtcPkg: string,
3232
nbtcContract: string,
3333
): Promise<string>;
34-
validateSignature(
34+
validateSignatures(
3535
redeemId: number,
36-
inputIdx: number,
37-
signId: string,
36+
inputs: { input_index: number; sign_id: string }[],
3837
nbtcPkg: string,
3938
nbtcContract: string,
4039
): Promise<void>;
@@ -262,13 +261,13 @@ class SuiClientImp implements SuiClient {
262261
return decoded.sign_id;
263262
}
264263

265-
async validateSignature(
264+
async validateSignatures(
266265
redeemId: number,
267-
inputIdx: number,
268-
signId: string,
266+
inputs: { input_index: number; sign_id: string }[],
269267
nbtcPkg: string,
270268
nbtcContract: string,
271269
): Promise<void> {
270+
if (inputs.length === 0) return;
272271
const tx = new Transaction();
273272
const coordinatorId = this.#ika.getCoordinatorId();
274273

@@ -279,8 +278,8 @@ class SuiClientImp implements SuiClient {
279278
contract: nbtcContract,
280279
dwalletCoordinator: coordinatorId,
281280
redeemId: redeemId,
282-
inputIds: inputIdx,
283-
signIds: [signId],
281+
inputIds: inputs.map((i) => BigInt(i.input_index)),
282+
signIds: inputs.map((i) => i.sign_id),
284283
},
285284
}),
286285
);

0 commit comments

Comments
 (0)