fix(ts-sdk): harden payout PSBT signature extraction#1405
fix(ts-sdk): harden payout PSBT signature extraction#1405jeremy-babylonlabs wants to merge 1 commit intomainfrom
Conversation
🔐 Commit Signature Verification✅ All 1 commit(s) passed verification
Summary
Required key type: Last verified: 2026-04-15 09:23 UTC |
Greptile SummaryThis PR hardens the payout PSBT signature extraction path in two ways: (1) Confidence Score: 5/5Safe to merge — all remaining findings are P2 style suggestions with no correctness impact. The hardening changes are logically sound: the >>> 0 fix on the 4-byte varint path is correct, bounds checking is properly placed, the 0xff rejection is appropriate, trailing-bytes detection is correct, and the strict stack-size enforcement is a deliberate and well-documented security improvement. Tests cover every new code path. No P0/P1 issues found. No files require special attention. Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[extractPayoutSignature] --> B{tapScriptSig present?}
B -- Yes --> C[Find entry matching depositorPubkey]
C --> D{Found?}
D -- No --> E[Throw: no sig for depositor pubkey]
D -- Yes --> F[extractSchnorrSig]
B -- No --> G{finalScriptWitness present?}
G -- No --> H[Throw: no tapScriptSig or finalScriptWitness]
G -- Yes --> I[parseWitnessStack]
I --> I1{requireBytes check at each read}
I1 -- fail --> I2[Throw: Malformed witness data]
I1 -- pass --> I3{varint prefix}
I3 -- 0x00-0xFC --> I4[1-byte count/len]
I3 -- 0xFD --> I5[2-byte LE count/len]
I3 -- 0xFE --> I6[4-byte LE count/len with >>> 0]
I3 -- 0xFF --> I7[Throw: 8-byte varint not supported]
I4 & I5 & I6 --> I8[Read items into Buffer views]
I8 --> I9{Trailing bytes?}
I9 -- Yes --> I10[Throw: trailing bytes]
I9 -- No --> J[witnessStack]
J --> K{stack.length === 3?}
K -- No --> L[Throw: unexpected stack size]
K -- Yes --> F
F --> M{sig.length}
M -- 64 --> N[Return hex]
M -- 65 --> O{sighash === SIGHASH_ALL?}
O -- Yes --> P[Strip byte, return 64-byte hex]
O -- No --> Q[Throw: unexpected sighash type]
M -- other --> R[Throw: unexpected sig length]
Reviews (1): Last reviewed commit: "fix(ts-sdk): harden payout PSBT signatur..." | Re-trigger Greptile |
dc39a8d to
8f3a0e0
Compare
Enforce exact Taproot single-sig witness stack size and fail loudly on malformed witness data instead of silently returning a possibly-wrong signature or zero-valued varint.
8f3a0e0 to
ac78962
Compare
Summary
extractPayoutSignatureso unexpected wallet finalizations fail loudly instead of silently returning a potentially-wrong signature.parseWitnessStackreject malformed witness data: require enough bytes before reads, fix a missing>>> 0on the 4-byte varint path, throw on unsupported 8-byte varints, and reject trailing bytes.Test plan
pnpm --filter @babylonlabs-io/ts-sdk run testpnpm run lint