Repository navigation
rpc: scriptPubKey carries Core's inferred desc #1176
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
Merged
reardencode
merged 1 commit into
reardencode:master
from
Naomi-Gift:rpc/scriptpubkey-desc
Oct 24, 2026
Merged
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| Added | ||
|
|
||
| - **`scriptPubKey` carries Core's `desc`.** `getrawtransaction`, | ||
| `decoderawtransaction`, `getblock` verbosity 2, `decodescript`, | ||
| `gettxout`, and REST `getutxos` now return the inferred output | ||
| descriptor with its checksum: `pk`, `multi`, `rawtr`, `addr`, or `raw`, | ||
| as Core infers it with no keystore. |
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,151 @@ | ||
| use bitcoin::key::XOnlyPublicKey; | ||
| use bitcoin::opcodes::all::OP_CHECKMULTISIG; | ||
| use bitcoin::opcodes::{Class, ClassifyContext}; | ||
| use bitcoin::script::Instruction; | ||
| use bitcoin::{Address, Network, Script}; | ||
| use rbitcoin_primitives::hex_encode; | ||
|
|
||
| const MAX_MULTISIG_KEYS: usize = 20; | ||
|
|
||
| pub fn infer_descriptor(script: &Script, network: Network) -> String { | ||
| let body = infer_body(script, network); | ||
| let sum = checksum(&body); | ||
| format!("{body}#{sum}") | ||
| } | ||
|
|
||
| fn infer_body(script: &Script, network: Network) -> String { | ||
| if let Some(key) = p2pk_key(script) { | ||
| return format!("pk({})", hex_encode(key)); | ||
| } | ||
| if let Some((required, keys)) = bare_multisig(script) { | ||
| let keys: Vec<String> = keys.into_iter().map(hex_encode).collect(); | ||
| return format!("multi({required},{})", keys.join(",")); | ||
| } | ||
| if let Some(key) = taproot_output_key(script) { | ||
| return format!("rawtr({})", hex_encode(key.serialize())); | ||
| } | ||
| if let Ok(addr) = Address::from_script(script, network) { | ||
| return format!("addr({addr})"); | ||
| } | ||
| format!("raw({})", hex_encode(script.as_bytes())) | ||
| } | ||
|
|
||
| fn has_key_size(push: &[u8]) -> bool { | ||
| let want = match push.first() { | ||
| Some(2 | 3) => 33, | ||
| Some(4 | 6 | 7) => 65, | ||
| _ => return false, | ||
| }; | ||
| push.len() == want | ||
| } | ||
|
|
||
| fn is_hybrid(key: &[u8]) -> bool { | ||
| matches!(key.first(), Some(6 | 7)) | ||
| } | ||
|
|
||
| fn p2pk_key(script: &Script) -> Option<&[u8]> { | ||
| let b = script.as_bytes(); | ||
| let (&len, rest) = b.split_first()?; | ||
| let (&last, key) = rest.split_last()?; | ||
| let sized = (len == 33 || len == 65) && key.len() == usize::from(len); | ||
| (sized && last == 0xac && has_key_size(key) && !is_hybrid(key)).then_some(key) | ||
| } | ||
|
|
||
| fn bare_multisig(script: &Script) -> Option<(usize, Vec<&[u8]>)> { | ||
| let b = script.as_bytes(); | ||
| let mut ops = script.instruction_indices(); | ||
| let (at, first) = ops.next()?.ok()?; | ||
| let required = multisig_count(b, at, first, 1)?; | ||
| let mut keys = Vec::new(); | ||
| let (at, count) = loop { | ||
| let (at, ins) = ops.next()?.ok()?; | ||
| match ins { | ||
| Instruction::PushBytes(p) if has_key_size(p.as_bytes()) => keys.push(p.as_bytes()), | ||
| other => break (at, other), | ||
| } | ||
| }; | ||
| if multisig_count(b, at, count, required)? != keys.len() { | ||
| return None; | ||
| } | ||
| let (_, last) = ops.next()?.ok()?; | ||
| if last != Instruction::Op(OP_CHECKMULTISIG) || ops.next().is_some() { | ||
| return None; | ||
| } | ||
| if keys.iter().any(|k| is_hybrid(k)) { | ||
| return None; | ||
| } | ||
| Some((required, keys)) | ||
| } | ||
|
|
||
| fn multisig_count(script: &[u8], at: usize, ins: Instruction<'_>, min: usize) -> Option<usize> { | ||
| let n = match ins { | ||
| Instruction::Op(op) => match op.classify(ClassifyContext::Legacy) { | ||
| Class::PushNum(n) => usize::try_from(n).ok()?, | ||
| _ => return None, | ||
| }, | ||
| Instruction::PushBytes(p) => match (script[at], p.as_bytes()) { | ||
| (1, &[n]) if n > 16 => usize::from(n), | ||
| _ => return None, | ||
| }, | ||
| }; | ||
| (min..=MAX_MULTISIG_KEYS).contains(&n).then_some(n) | ||
| } | ||
|
|
||
| fn taproot_output_key(script: &Script) -> Option<XOnlyPublicKey> { | ||
| if !script.is_p2tr() { | ||
| return None; | ||
| } | ||
| XOnlyPublicKey::from_slice(&script.as_bytes()[2..]).ok() | ||
| } | ||
|
|
||
| const INPUT_CHARSET: &str = "0123456789()[],'/*abcdefgh@:$%{}\ | ||
| IJKLMNOPQRSTUVWXYZ&+-.;<=>?!^_|~\ | ||
| ijklmnopqrstuvwxyzABCDEFGH`#\"\\ "; | ||
| const CHECKSUM_CHARSET: &[u8; 32] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l"; | ||
|
|
||
| fn polymod(c: u64, val: u64) -> u64 { | ||
| const GENERATOR: [u64; 5] = [ | ||
| 0xf5dee51989, | ||
| 0xa9fdca3312, | ||
| 0x1bab10e32d, | ||
| 0x3706b1677a, | ||
| 0x644d626ffd, | ||
| ]; | ||
| let c0 = c >> 35; | ||
| let mut c = ((c & 0x7_ffff_ffff) << 5) ^ val; | ||
| for (i, g) in GENERATOR.iter().enumerate() { | ||
| if c0 & (1 << i) != 0 { | ||
| c ^= g; | ||
| } | ||
| } | ||
| c | ||
| } | ||
|
|
||
| fn checksum(body: &str) -> String { | ||
| let mut c = 1u64; | ||
| let mut class = 0u64; | ||
| let mut class_count = 0; | ||
| for ch in body.chars() { | ||
| let pos = INPUT_CHARSET | ||
| .find(ch) | ||
| .expect("inferred descriptor uses only the BIP380 charset") as u64; | ||
| c = polymod(c, pos & 31); | ||
| class = class * 3 + (pos >> 5); | ||
| class_count += 1; | ||
| if class_count == 3 { | ||
| c = polymod(c, class); | ||
| class = 0; | ||
| class_count = 0; | ||
| } | ||
| } | ||
| if class_count > 0 { | ||
| c = polymod(c, class); | ||
| } | ||
| for _ in 0..8 { | ||
| c = polymod(c, 0); | ||
| } | ||
| c ^= 1; | ||
| (0..8) | ||
| .map(|i| char::from(CHECKSUM_CHARSET[((c >> (5 * (7 - i))) & 31) as usize])) | ||
| .collect() | ||
| } | ||
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
Oops, something went wrong.
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.
Nit:
script_pubkey_jsoncallsAddress::from_scriptagain to filladdress. Outputs that are notpk,multi, orrawtrparse the script twice, including every standard output ingetblockverbosity 2. The address string is the same either way.