-
Notifications
You must be signed in to change notification settings - Fork 27
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
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,29 @@ 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. | ||
| // Clone so the provider can't mutate originals before submitTx. | ||
| const requests = [ | ||
| { tx: offchainTx.arkTx.clone() }, | ||
| ...offchainTx.checkpoints.map((c) => ({ tx: c.clone() })), | ||
| ]; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| const signed = await this.identity.signMultiple(requests); | ||
| if (signed.length !== requests.length) { | ||
| throw new Error( | ||
| `signMultiple returned ${signed.length} transactions, expected ${requests.length}` | ||
| ); | ||
| } | ||
| const [firstSignedTx, ...signedCheckpoints] = signed; | ||
| signedVirtualTx = firstSignedTx; | ||
| userSignedCheckpoints = signedCheckpoints; | ||
| } 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 +2519,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()); | ||
| }); | ||
|
Comment on lines
+2525
to
+2531
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 On Line 2527, this branch indexes Possible fix if (userSignedCheckpoints) {
+ if (signedCheckpointTxs.length !== userSignedCheckpoints.length) {
+ throw new Error(
+ `submitTx returned ${signedCheckpointTxs.length} checkpoint(s), expected ${userSignedCheckpoints.length}`
+ );
+ }
// 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);
+ combineTapscriptSigs(userSignedCheckpoints[i], serverSigned);
return base64.encode(serverSigned.toPSBT());
});🤖 Prompt for AI Agents |
||
| } 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 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { describe, it, expect, vi } from "vitest"; | ||
| import { | ||
| isBatchSignable, | ||
| BatchSignableIdentity, | ||
| SignRequest, | ||
| Identity, | ||
| } from "../src/identity"; | ||
| import { Transaction } from "../src/utils/transaction"; | ||
| import { SignerSession, TreeSignerSession } from "../src/tree/signingSession"; | ||
|
|
||
| function stubIdentity(): Identity { | ||
| return { | ||
| async xOnlyPublicKey() { | ||
| return new Uint8Array(32); | ||
| }, | ||
| async compressedPublicKey() { | ||
| return new Uint8Array(33); | ||
| }, | ||
| signerSession(): SignerSession { | ||
| return TreeSignerSession.random(); | ||
| }, | ||
| async sign(tx: Transaction) { | ||
| return tx; | ||
| }, | ||
| async signMessage() { | ||
| return new Uint8Array(64); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function stubBatchIdentity( | ||
| signMultipleFn?: (requests: SignRequest[]) => Promise<Transaction[]> | ||
| ): BatchSignableIdentity { | ||
| const base = stubIdentity(); | ||
| return { | ||
| ...base, | ||
| signMultiple: | ||
| signMultipleFn ?? | ||
| (async (requests: SignRequest[]) => | ||
| requests.map((r) => r.tx.clone())), | ||
| }; | ||
| } | ||
|
|
||
| describe("isBatchSignable", () => { | ||
| it("should return true for BatchSignableIdentity", () => { | ||
| const identity = stubBatchIdentity(); | ||
| expect(isBatchSignable(identity)).toBe(true); | ||
| }); | ||
|
|
||
| it("should return false for plain Identity", () => { | ||
| const identity = stubIdentity(); | ||
| expect(isBatchSignable(identity)).toBe(false); | ||
| }); | ||
|
|
||
| it("should return false if signMultiple is not a function", () => { | ||
| const identity = stubIdentity() as any; | ||
| identity.signMultiple = "not a function"; | ||
| expect(isBatchSignable(identity)).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe("BatchSignableIdentity contract", () => { | ||
| it("should return same number of transactions as requests", async () => { | ||
| const identity = stubBatchIdentity(); | ||
| const tx = new Transaction(); | ||
| const requests: SignRequest[] = [ | ||
| { tx: tx.clone() }, | ||
| { tx: tx.clone() }, | ||
| { tx: tx.clone() }, | ||
| ]; | ||
| const results = await identity.signMultiple(requests); | ||
| expect(results).toHaveLength(requests.length); | ||
| }); | ||
|
|
||
| it("should handle empty requests", async () => { | ||
| const identity = stubBatchIdentity(); | ||
| const results = await identity.signMultiple([]); | ||
| expect(results).toEqual([]); | ||
| }); | ||
|
|
||
| it("should pass inputIndexes through to each request", async () => { | ||
| const receivedRequests: SignRequest[] = []; | ||
| const identity = stubBatchIdentity(async (requests) => { | ||
| receivedRequests.push(...requests); | ||
| return requests.map((r) => r.tx.clone()); | ||
| }); | ||
|
|
||
| const tx = new Transaction(); | ||
| await identity.signMultiple([ | ||
| { tx: tx.clone(), inputIndexes: [0, 2] }, | ||
| { tx: tx.clone() }, | ||
| ]); | ||
|
|
||
| expect(receivedRequests[0].inputIndexes).toEqual([0, 2]); | ||
| expect(receivedRequests[1].inputIndexes).toBeUndefined(); | ||
| }); | ||
|
|
||
| it("should preserve request order in results", async () => { | ||
| const markers: string[] = []; | ||
| const identity = stubBatchIdentity(async (requests) => { | ||
| return requests.map((r, i) => { | ||
| markers.push(`signed-${i}`); | ||
| return r.tx.clone(); | ||
| }); | ||
| }); | ||
|
|
||
| const tx = new Transaction(); | ||
| await identity.signMultiple([ | ||
| { tx: tx.clone() }, | ||
| { tx: tx.clone() }, | ||
| { tx: tx.clone() }, | ||
| ]); | ||
|
|
||
| expect(markers).toEqual(["signed-0", "signed-1", "signed-2"]); | ||
| }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.