Skip to content

Read Merkle paths and ASM the way @bsv/sdk 4.1.1 does - #16

Merged
rohenaz merged 5 commits into
mainfrom
merklepath-asm-ts-compat
Nov 16, 2026
Merged

rohenaz merged 5 commits into
mainfrom
merklepath-asm-ts-compat

Conversation

@rohenaz

@rohenaz rohenaz commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Before: Swift rejected some BUMPs that @bsv/sdk 4.1.1 writes, accepted coinbase proofs under 100 blocks deep, and read and wrote ASM in a dialect that differs from TS (OP_FALSE/OP_TRUE, numbers as opcodes).

After: Merkle paths, coinbase maturity and ASM match @bsv/sdk 4.1.1 by default, with the remaining deliberate differences listed in COMP-022.

How: flags are read as a bitmask, upper-level offsets are checked like TS, full SPV verification checks coinbase maturity against ChainTracker.currentHeight(), and the default ASM style and dialect are now .typeScript.

Behaviour changes

  • asm() defaults to .typeScript: prints OP_0/OP_1, 0xb6 as OP_LSHIFTNUM, 0xb8 as OP_NOP9, zero-length pushes as OP_0; a malformed push after a top-level OP_RETURN prints lossily instead of throwing (use style: .canonical for the strict check).
  • Script(asm:) defaults to .typeScript: 1 is data, odd hex gets a leading zero, raw hex must be lowercase, 0 and -1 are opcodes, OP_PUSHDATA1/2/4 <len> <hex> keeps its opcode with a plain decimal length, a trailing OP_PUSHDATA* is an empty push, empty text is an empty script.
  • New enum cases (break exhaustive switches): MerklePathError.illegalOffset, ScriptError.invalidASMPushDataLength, .typeScript on ScriptASMStyle and ScriptASMDialect.
  • MerklePathError.invalidFlags is deprecated and never thrown; a duplicate's txid bit is not kept when written back out.
  • BUMP and BEEF parsing throw illegalOffset for upper-level nodes no level-zero hash needs.
  • Single-level paths with an odd node count now prove their root.
  • MerklePath.verify and full SPVProofVerifier.verify return false for a coinbase proof under 100 blocks deep, so ChainTracker.currentHeight() must return the real chain tip. BEEF.verify is unchanged, as in TS.
  • applyFee gives the equal-split change remainder to the last change output instead of the fee.

Testing

swift test in swift:6.1 (Linux): 862 tests passed, XCTest 132 passed, 0 failures; only the live-network and opt-in tests skipped. Merged with main (PR 15 error text) and added the illegalOffset description.

- A single-level path pairs the right-most node of each height with
  itself when it has no right neighbour, so full-block paths with an odd
  node count prove their root (MerklePath.ts:34, :380, :430, :671).
- BUMP flags are read as a bitmask: bit 0 is a duplicate, otherwise bit 1
  marks a txid. @bsv/sdk writes flags 3 for a marked duplicate.
- Nodes above level zero must be siblings of a level-zero hash's
  ancestors (legalOffsetsOnly); new MerklePathError.illegalOffset.
- MerklePath.verify returns false for a coinbase (offset 0) proof until
  the tracker's current height is 100 blocks above it.
- Equal change distribution gives the division remainder to the last
  change output instead of paying it as fee.
- Tests use the @bsv/sdk valid and invalid BUMP vectors and block 125632.
- New ScriptASMDialect.typeScript and ScriptASMStyle.typeScript, now the
  defaults. They use the OP.ts names (OP_0, OP_1, OP_SUBSTR..OP_RSHIFTNUM,
  OP_NOP9..OP_NOP77, OP_SMALLDATA), so ASM crossing between the SDKs
  keeps its bytes. BRC-106 and Go output stay available.
- The TypeScript dialect reads -1, odd-length hex and
  OP_PUSHDATA1/2/4 <length> <hex>, which keeps its push opcode.
- The TypeScript style prints zero-length pushes as OP_0 and names a
  top-level OP_RETURN, reading the bytes after it leniently.
- New Opcode.typeScriptName and ScriptError.invalidASMPushDataLength.
- COMP-022 records the decision.
…4.1.1

SPVProofVerifier.verify(_:rootTransactionID:...) now returns false when the
walk reaches a proven coinbase (offset 0) less than 100 blocks below the
tracker's currentHeight(), as @bsv/sdk Transaction.verify does through
MerklePath.verify. The tip is fetched once, only when such a proof is
reached. BEEF.verify still checks roots only, like Beef.verify.
ChainTracker.currentHeight() now says it must be the real chain tip.

ASM in the default TypeScript dialect:
- raw data tokens must be lowercase, as @bsv/sdk rejects uppercase; the
  data token of OP_PUSHDATA* still accepts either case, as @bsv/sdk does
- a trailing OP_PUSHDATA1/2/4 (or "OP_PUSHDATA1 0") is an empty push
- the OP_PUSHDATA* length must be plain decimal, without leading zeros
- docs and COMP-022 no longer say Swift writes exactly what 4.1.1 writes,
  list where it follows the unmerged @bsv/sdk fixes, and state that the
  lenient OP_RETURN tail is lossy for malformed pushes

Also: the 32-bit push-length guard runs after the truncated-length check,
invalidFlags is marked deprecated, legal offsets are only built for levels
that hold nodes, and COMP-025 notes that a duplicate's txid bit is not
kept. The previous Merkle commit also made applyFee give the change
remainder to the last change output.
@rohenaz
rohenaz merged commit 7fdf8b0 into main Nov 16, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant