Skip to content

feat(core): add zero-copy Deref bridge to bitcoin::Script#186

Open
alexanderwiederin wants to merge 6 commits into
sedited:masterfrom
alexanderwiederin:bitcoin-interop
Open

feat(core): add zero-copy Deref bridge to bitcoin::Script#186
alexanderwiederin wants to merge 6 commits into
sedited:masterfrom
alexanderwiederin:bitcoin-interop

Conversation

@alexanderwiederin
Copy link
Copy Markdown
Collaborator

@alexanderwiederin alexanderwiederin commented May 21, 2026

closes #20

Summary

Implements Deref<Target = bitcoin::Script> for ScriptPubkey and ScriptPubkeyRef, behind a new bitcoin feature flag. The Deref implementation uses a new function, script_as_bytes, which captures the raw pointer passed to the btck_script_pubkey_to_bytes callback and casts it to a &[u8] slice, avoiding any allocation.

Motivation

Silent payments block scanning requires script introspection such as is_p2tr (reference). Delegating this functionality to rust-bitcoin keeps the kernel C API focused on consensus, avoiding scope creep. Further, integrating ScriptPubkey with rust-bitcoin's Script type, provides a suite of new functionality with Bitcoin protocol awareness.

Usage

use bitcoinkernel::ScriptPubkey;

let bytes = hex::decode("5120deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef").unwrap();
let script = ScriptPubkey::new(&bytes).unwrap();

// the following functionality is exposed through `bitcoin::Script`
assert!(script.is_p2tr());
assert!(script.witness_version().is_some());
assert!(!script.is_op_return());
println!("{}", script.to_asm_string());

What you should know

Given that Deref is intended to be used for wrappers and their underlying type, the use here may be considered misplaced - bitcoin::Script is not an underlying type, but a peer type from a separate crate that shares the same underlying representation. The alternative would be to add a ScriptPubkey::as_bitcoin_script() method to make the relationship explicit, at the cost of call-site ergonomics. I recognise this is a design decision that requires careful consideration. I'm happy to go either way.

See the Rust documentation on when to implement Deref.

@alexanderwiederin alexanderwiederin force-pushed the bitcoin-interop branch 4 times, most recently from 21b7ae6 to b4ad130 Compare May 22, 2026 12:45
Implements `Deref<Target = bitcoin::Script>` for `ScriptPubkey` and
`ScriptPubkeyRef` behind the `bitcoin` feature flag.

`btck_script_pubkey_to_bytes` calls its writer callback synchronously
with a direct pointer into the `CScript` buffer, allowing a zero-copy
`&[u8]` slice. `Script::from_bytes` is then a zero-cost cast, giving
kernel script types the full `bitcoin::Script` API transparently.
Separates the CI MSRV run with and without the bitcoin feature to
improve localization of issues.
@alexanderwiederin alexanderwiederin marked this pull request as ready for review May 22, 2026 14:18
@alexanderwiederin alexanderwiederin changed the title [DRAFT] feat(core): add zero-copy Deref bridge to bitcoin::Script feat(core): add zero-copy Deref bridge to bitcoin::Script May 22, 2026
@alexanderwiederin alexanderwiederin changed the title feat(core): add zero-copy Deref bridge to bitcoin::Script feat(core): add zero-copy Deref bridge to bitcoin::Script May 22, 2026
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.

Feat: Script Type Detection

1 participant