rust-bitcoin / rust-bitcoin/rust-bitcoin

`script::Builder::push_slice` is not beginner friendly.

Open
#1,746 13 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
2.7k
Forks
1k
Avg merge
4d 39m
Merged PRs (30d)
86

Description

Pushing a script into a Script is a very common step that we need to do in wrapper segwit. sh(wsh), sh(wpkh). Here is how I had to do it in rust-miniscript.

+use bitcoin::script::{PushBytes, PushBytesBuf};
+use core::convert::TryFrom;
 use bitcoin::{script, Address, Network, ScriptBuf};
 
 
-                script::Builder::new()
-                    .push_script(&witness_script.to_v0_p2wsh())
-                    .into_script()
+                let push_bytes = PushBytesBuf::try_from(witness_script.into_bytes())
+                    .expect("Witness script is not too large");
+                script::Builder::new().push_slice(&push_bytes).into_script()
             }
             ShInner::Wpkh(ref wpkh) => {
                 let redeem_script = wpkh.script_pubkey();
-                script::Builder::new()
-                    .push_script(&redeem_script)
-                    .into_script()
+                let push_bytes: &PushBytes =
+                    <&PushBytes>::try_from(redeem_script.as_bytes()).unwrap();
+                script::Builder::new().push_slice(&push_bytes).into_script()

Doing a wintess_script.as_bytes().try_into().unwrap() does not work as rust still requires us to specify the exact that we want to try_into.
multiple impls satisfying _: TryFrom<&[u8]>found in the following crates:bitcoin, core: ...

Importing a new type and reading it's documentation is a nice thing to do, but only to find out that we only fail when the total script size can exceed 2**32 which is exceedingly high. I propose we add the following methods that are more beginner friendly.


// We handle creating the pushbytes internally.
pub fn try_push_slice<S: AsRef<[u8]>>(sl: S) -> Result<Builder, Builder>;

pub fn push_slice_unchecked<S: AsRef<[u8]>>(sl: S) -> Builder;

Does not require the user to understand the inner workings of what pushbytes is and how it actually works. cc @Kixunil

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the implementation and documentation for script::Builder::push_slice, PushBytes, and PushBytesBuf, then read the issue discussion before choosing an API. The proposed behavior is to simplify pushing byte slices while preserving oversized-script handling; completion would require an agreed design and tests covering the accepted methods.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.