-
Notifications
You must be signed in to change notification settings - Fork 22
feat: add BatchSignableIdentity for one-shot batch PSBT signing #395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,7 +28,7 @@ import { | |
| validateVtxoTxGraph, | ||
| } from "../tree/validation"; | ||
| import { validateBatchRecipients } from "./validation"; | ||
| import { Identity, ReadonlyIdentity } from "../identity"; | ||
| import { Identity, ReadonlyIdentity, isBatchSignable } from "../identity"; | ||
| import { | ||
| ArkTransaction, | ||
| Asset, | ||
|
|
@@ -62,6 +62,7 @@ import { getSequence, VtxoScript } from "../script/base"; | |
| import { CSVMultisigTapscript, RelativeTimelock } from "../script/tapscript"; | ||
| import { | ||
| buildOffchainTx, | ||
| combineTapscriptSigs, | ||
| hasBoardingTxExpired, | ||
| isValidArkAddress, | ||
| } from "../utils/arkTransaction"; | ||
|
|
@@ -2485,7 +2486,22 @@ export class Wallet extends ReadonlyWallet implements IWallet { | |
| outputs, | ||
| this.serverUnrollScript | ||
| ); | ||
| const signedVirtualTx = await this.identity.sign(offchainTx.arkTx); | ||
|
|
||
| let signedVirtualTx: Transaction; | ||
| let userSignedCheckpoints: Transaction[] | undefined; | ||
|
|
||
| if (isBatchSignable(this.identity)) { | ||
| // Batch-sign arkTx + all checkpoints in one wallet popup | ||
| const requests = [ | ||
| { tx: offchainTx.arkTx }, | ||
| ...offchainTx.checkpoints.map((c) => ({ tx: c })), | ||
| ]; | ||
|
Comment on lines
+2495
to
+2498
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Send clones to
Possible fix const requests = [
- { tx: offchainTx.arkTx },
- ...offchainTx.checkpoints.map((c) => ({ tx: c })),
+ { tx: offchainTx.arkTx.clone() },
+ ...offchainTx.checkpoints.map((c) => ({ tx: c.clone() })),
];🤖 Prompt for AI Agents |
||
| const signed = await this.identity.signMultiple(requests); | ||
| signedVirtualTx = signed[0]; | ||
| userSignedCheckpoints = signed.slice(1); | ||
|
Comment on lines
+2499
to
+2501
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Validate the batch signer returned one result per request. A short or extra response array will only fail later when Possible fix const signed = await this.identity.signMultiple(requests);
- signedVirtualTx = signed[0];
- userSignedCheckpoints = signed.slice(1);
+ if (signed.length !== requests.length) {
+ throw new Error(
+ `Batch signer returned ${signed.length} result(s) for ${requests.length} request(s)`
+ );
+ }
+ const [firstSignedTx, ...signedCheckpoints] = signed;
+ signedVirtualTx = firstSignedTx;
+ userSignedCheckpoints = signedCheckpoints;🤖 Prompt for AI Agents |
||
| } else { | ||
| signedVirtualTx = await this.identity.sign(offchainTx.arkTx); | ||
| } | ||
|
|
||
| // Mark pending before submitting — if we crash between submit and | ||
| // finalize, the next init will recover via finalizePendingTxs. | ||
|
|
@@ -2496,13 +2512,27 @@ export class Wallet extends ReadonlyWallet implements IWallet { | |
| base64.encode(signedVirtualTx.toPSBT()), | ||
| offchainTx.checkpoints.map((c) => base64.encode(c.toPSBT())) | ||
| ); | ||
| const finalCheckpoints = await Promise.all( | ||
| signedCheckpointTxs.map(async (c) => { | ||
| const tx = Transaction.fromPSBT(base64.decode(c)); | ||
| const signedCheckpoint = await this.identity.sign(tx); | ||
| return base64.encode(signedCheckpoint.toPSBT()); | ||
| }) | ||
| ); | ||
|
|
||
| let finalCheckpoints: string[]; | ||
|
|
||
| if (userSignedCheckpoints) { | ||
| // Merge pre-signed user signatures onto server-signed checkpoints | ||
| finalCheckpoints = signedCheckpointTxs.map((c, i) => { | ||
| const serverSigned = Transaction.fromPSBT(base64.decode(c)); | ||
| combineTapscriptSigs(userSignedCheckpoints![i], serverSigned); | ||
| return base64.encode(serverSigned.toPSBT()); | ||
| }); | ||
| } else { | ||
| // Legacy: sign each checkpoint individually (N popups) | ||
| finalCheckpoints = await Promise.all( | ||
| signedCheckpointTxs.map(async (c) => { | ||
| const tx = Transaction.fromPSBT(base64.decode(c)); | ||
| const signedCheckpoint = await this.identity.sign(tx); | ||
| return base64.encode(signedCheckpoint.toPSBT()); | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| await this.arkProvider.finalizeTx(arkTxid, finalCheckpoints); | ||
|
|
||
| try { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Encode the batch request/result mapping explicitly.
Wallet.buildAndSubmitOffchainTx()consumessignMultiple()positionally, but this API only promises a bareTransaction[]. A provider can legally reorder or drop results and still satisfy the type, which would misapply signatures during checkpoint finalization. Either make the response keyed/id-based, or at least document a strict same-order/same-length contract here so implementers don’t guess.🤖 Prompt for AI Agents