-
Notifications
You must be signed in to change notification settings - Fork 6
Update to use PSET and to allow signing #12
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
Open
apoelstra
wants to merge
7
commits into
BlockstreamResearch:master
Choose a base branch
from
apoelstra:2025-10/pset-signer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a183dc1
simplicity: allow parsing both programs and witnesses as hex or b64
apoelstra 5015d4a
simplicity: move parse_elements_utxo up from sighash to mod
apoelstra fa80dbb
simplicity: add `simplicity pset update-input` CLI call
apoelstra b151c95
simplicity: allow providing PSETs in 'simplicity sighash'
apoelstra f60d0a0
simplicity: add 'simplicity pset finalize' command
apoelstra d7e3b04
simplicity: add 'simplicity pset extract' command
apoelstra cacd1f7
simplicity: add 'simplicity pset create' command
apoelstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| // Copyright 2025 Andrew Poelstra | ||
| // SPDX-License-Identifier: CC0-1.0 | ||
|
|
||
| use super::super::{Error, ErrorExt as _}; | ||
| use super::UpdatedPset; | ||
| use crate::cmd; | ||
|
|
||
| use elements::confidential; | ||
| use elements::pset::PartiallySignedTransaction; | ||
| use elements::{Address, AssetId, OutPoint, Transaction, TxIn, TxOut, Txid}; | ||
| use serde::Deserialize; | ||
|
|
||
| use std::collections::HashMap; | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct InputSpec { | ||
| txid: Txid, | ||
| vout: u32, | ||
| #[serde(default)] | ||
| sequence: Option<u32>, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| struct FlattenedOutputSpec { | ||
| address: String, | ||
| asset: AssetId, | ||
| #[serde(with = "elements::bitcoin::amount::serde::as_btc")] | ||
| amount: elements::bitcoin::Amount, | ||
| } | ||
|
|
||
| #[derive(Deserialize)] | ||
| #[serde(untagged)] | ||
| enum OutputSpec { | ||
| Explicit { | ||
| address: String, | ||
| asset: AssetId, | ||
| #[serde(with = "elements::bitcoin::amount::serde::as_btc")] | ||
| amount: elements::bitcoin::Amount, | ||
| }, | ||
| Map(HashMap<String, f64>), | ||
| } | ||
|
|
||
| impl OutputSpec { | ||
| fn flatten(self) -> Box<dyn Iterator<Item = Result<FlattenedOutputSpec, Error>>> { | ||
| match self { | ||
| Self::Map(map) => Box::new(map.into_iter().map(|(address, amount)| { | ||
| // Use liquid bitcoin asset as default for map format | ||
| let default_asset = AssetId::from_slice(&[ | ||
| 0x49, 0x9a, 0x81, 0x85, 0x45, 0xf6, 0xba, 0xe3, 0x9f, 0xc0, 0x3b, 0x63, 0x7f, | ||
| 0x2a, 0x4e, 0x1e, 0x64, 0xe5, 0x90, 0xca, 0xc1, 0xbc, 0x3a, 0x6f, 0x6d, 0x71, | ||
| 0xaa, 0x44, 0x43, 0x65, 0x4c, 0x14, | ||
| ]) | ||
| .expect("valid asset id"); | ||
|
|
||
| Ok(FlattenedOutputSpec { | ||
| address, | ||
| asset: default_asset, | ||
| amount: elements::bitcoin::Amount::from_btc(amount) | ||
| .result_context("parsing amount")?, | ||
| }) | ||
| })), | ||
| Self::Explicit { | ||
| address, | ||
| asset, | ||
| amount, | ||
| } => Box::new( | ||
| Some(Ok(FlattenedOutputSpec { | ||
| address, | ||
| asset, | ||
| amount, | ||
| })) | ||
| .into_iter(), | ||
| ), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn cmd<'a>() -> clap::App<'a, 'a> { | ||
| cmd::subcommand("create", "create an empty PSET").args(&cmd::opts_networks()).args(&[ | ||
| cmd::arg( | ||
| "inputs", | ||
| "input outpoints (JSON array of objects containing txid, vout, sequence)", | ||
| ) | ||
| .takes_value(true) | ||
| .required(true), | ||
| cmd::arg("outputs", "outputs (JSON array of objects containing address, asset, amount)") | ||
| .takes_value(true) | ||
| .required(true), | ||
| ]) | ||
| } | ||
|
|
||
| pub fn exec<'a>(matches: &clap::ArgMatches<'a>) { | ||
| let inputs_json = matches.value_of("inputs").expect("inputs mandatory"); | ||
| let outputs_json = matches.value_of("outputs").expect("inputs mandatory"); | ||
|
|
||
| match exec_inner(inputs_json, outputs_json) { | ||
| Ok(info) => cmd::print_output(matches, &info), | ||
| Err(e) => cmd::print_output(matches, &e), | ||
| } | ||
| } | ||
|
|
||
| fn exec_inner(inputs_json: &str, outputs_json: &str) -> Result<UpdatedPset, Error> { | ||
| // Parse inputs JSON | ||
| let input_specs: Vec<InputSpec> = | ||
| serde_json::from_str(inputs_json).result_context("parsing inputs JSON")?; | ||
|
|
||
| // Parse outputs JSON - support both array and map formats | ||
| let output_specs: Vec<OutputSpec> = | ||
| serde_json::from_str(outputs_json).result_context("parsing outputs JSON")?; | ||
|
|
||
| // Create transaction inputs | ||
| let mut inputs = Vec::new(); | ||
| for input_spec in &input_specs { | ||
| let outpoint = OutPoint::new(input_spec.txid, input_spec.vout); | ||
| let sequence = elements::Sequence(input_spec.sequence.unwrap_or(0xffffffff)); | ||
|
|
||
| inputs.push(TxIn { | ||
| previous_output: outpoint, | ||
| script_sig: elements::Script::new(), | ||
| sequence, | ||
| asset_issuance: Default::default(), | ||
| witness: Default::default(), | ||
| is_pegin: false, | ||
| }); | ||
| } | ||
|
|
||
| // Create transaction outputs | ||
| let mut outputs = Vec::new(); | ||
| for output_spec in output_specs.into_iter().flat_map(OutputSpec::flatten) { | ||
| let output_spec = output_spec?; // serde has crappy error messages so we defer parsing and then have to unwrap errors | ||
|
|
||
| let script_pubkey = match output_spec.address.as_str() { | ||
| "fee" => elements::Script::new(), | ||
| x => { | ||
| let addr = x.parse::<Address>().result_context("parsing address")?; | ||
| if addr.is_blinded() { | ||
| return Err("confidential addresses are not yet supported") | ||
| .result_context("output address"); | ||
| } | ||
| addr.script_pubkey() | ||
| } | ||
| }; | ||
|
|
||
| outputs.push(TxOut { | ||
| asset: confidential::Asset::Explicit(output_spec.asset), | ||
| value: confidential::Value::Explicit(output_spec.amount.to_sat()), | ||
| nonce: elements::confidential::Nonce::Null, | ||
| script_pubkey, | ||
| witness: elements::TxOutWitness::empty(), | ||
| }); | ||
| } | ||
|
|
||
| // Create the transaction | ||
| let tx = Transaction { | ||
| version: 2, | ||
| lock_time: elements::LockTime::ZERO, | ||
| input: inputs, | ||
| output: outputs, | ||
| }; | ||
|
|
||
| // Create PSET from transaction | ||
| let pset = PartiallySignedTransaction::from_tx(tx); | ||
|
|
||
| Ok(UpdatedPset { | ||
| pset: pset.to_string(), | ||
| updated_values: vec![ | ||
| // FIXME we technically update a whole slew of fields; see the implementation | ||
| // of PartiallySignedTransaction::from_tx. Should we attempt to exhaustively | ||
| // list them here? Or list none? Or what? | ||
| ], | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright 2025 Andrew Poelstra | ||
| // SPDX-License-Identifier: CC0-1.0 | ||
|
|
||
| use elements::encode::serialize_hex; | ||
|
|
||
| use super::super::{Error, ErrorExt as _}; | ||
| use crate::cmd; | ||
|
|
||
| pub fn cmd<'a>() -> clap::App<'a, 'a> { | ||
| cmd::subcommand("extract", "extract a raw transaction from a completed PSET") | ||
| .args(&cmd::opts_networks()) | ||
| .args(&[cmd::arg("pset", "PSET to update (base64)").takes_value(true).required(true)]) | ||
| } | ||
|
|
||
| pub fn exec<'a>(matches: &clap::ArgMatches<'a>) { | ||
| let pset_b64 = matches.value_of("pset").expect("tx mandatory"); | ||
| match exec_inner(pset_b64) { | ||
| Ok(info) => cmd::print_output(matches, &info), | ||
| Err(e) => cmd::print_output(matches, &e), | ||
| } | ||
| } | ||
|
|
||
| fn exec_inner(pset_b64: &str) -> Result<String, Error> { | ||
| let pset: elements::pset::PartiallySignedTransaction = | ||
| pset_b64.parse().result_context("decoding PSET")?; | ||
|
|
||
| let tx = pset.extract_tx().result_context("extracting transaction")?; | ||
| Ok(serialize_hex(&tx)) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this need a follow-up to support non-policy assets?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. This is the same as the
elements-clicreatepsbtinterface. If you want non-policy assets then you need to use the verbose form of the command in the other enum variant.