Skip to main content

Crate outscript

Crate outscript 

Source
Expand description

outscript generates potential output scripts for a given public key.

It supports Bitcoin and Bitcoin-like cryptocurrency output script formats (P2PKH, P2SH, P2WPKH, P2WSH, P2PK, P2TR, etc.), EVM-based networks (Ethereum and compatible chains), and other blockchains such as Litecoin, Dogecoin, Namecoin, Monacoin, Electraproto, Dash, Bitcoin Cash, Massa, Solana and Cardano.

This is a Rust port of the Go library github.com/KarpelesLab/outscript. All cryptography is provided by the purecrypto crate.

§Errors

Everything in the crate reports failures as Error, which each module re-exports (psbt::Error, btctx::Error and outscript::Error are the same type), so ? composes across modules. The self-contained codecs (base58, base64, bech32, bcur, bbqr, cbor, rlp, crypto::secp256k1 and Solana’s compact-u16) keep their own small error types, which convert into Error.

§Secrets

SecpPrivateKey and CardanoExtendedKey wipe their key material when dropped, and implement crypto::Zeroize to scrub it earlier. Signing and derivation wipe the secret-derived buffers they create (nonces, HMAC state, PBKDF2 output), and CardanoExtendedKey::bytes returns a crypto::Zeroizing buffer. Seeds and key bytes you pass in by reference remain yours to wipe: wrap them in crypto::Zeroizing.

§Features

Chains are opt-in. Each chain feature enables its modules, output-script formats and address codecs, and pulls in only the curve arithmetic it needs from purecrypto; all of them are on by default.

  • bitcoin: Bitcoin and Bitcoin-like networks (Litecoin, Dogecoin, Namecoin, Monacoin, Dash, Bitcoin Cash, Electraproto) — scripts, addresses, btcraw, psbt, taproot (script trees and control blocks), btcguess, BtcAmount and (with alloc) BtcTx and block rewards. Implies secp256k1.
  • evm: EIP-55 addresses, evmraw, evmabi and (with alloc) EvmTx. Implies secp256k1.
  • solana: addresses, program-derived addresses and (with alloc) SolanaTx. Implies ed25519.
  • cardano: Shelley addresses, BIP32-Ed25519 derivation and (with alloc) CardanoTx. Implies ed25519.
  • massa: addresses. Implies ed25519.
  • secp256k1 / ed25519: the raw crypto helpers and the matching PubKey variant, without any chain.

So are the transports, which move PSBTs and other payloads between devices as text, usually shown as QR codes (rendering and scanning those is not this crate’s business). They need no chain, and are on by default:

  • bcur: Uniform Resources (ur:crypto-psbt/...) — bytewords and single-part URs, and (with alloc) multi-part fountain encoding and decoding.
  • bbqr: BBQr (B$ZP0500...) — headers, hex/base32 parts and compression, and (with alloc) splitting and joining.

The runtime environment is selected separately:

  • std (default): implies alloc, and adds std::io adapters (BtcVarInt::read_from/write_to, BtcTx::read_from).
  • alloc: the full API on no_std targets with a global allocator — Out/Script, the BtcTx/EvmTx/SolanaTx/CardanoTx builders and parsers, RLP/CBOR and serde JSON.

With neither feature the crate is no_std and never allocates. That core offers (for the enabled chains):

  • hash, crypto (secp256k1 ECDSA/Schnorr/taproot, Ed25519) and cardano_derive (BIP32-Ed25519 keys);
  • output-script generation for every built-in format (generate_script), address rendering (encode_address_to_slice, plus the cardano address builders) and address decoding (decode_bitcoin_based_address and friends);
  • transaction signing: btcraw (legacy, BIP-143 and every BIP-341 taproot sighash, serialization, txid), taproot (script trees, merkle roots and control blocks), psbt (BIP-174 creator, updater, signer, combiner, finalizer and extractor, including taproot script paths) and evmraw (legacy/EIP-2930/EIP-1559 signing, encoding, hashing and sender recovery);
  • solana keys, program-derived addresses and compact-u16; evmabi selectors, words and ERC-20 calldata; BtcAmount parsing/formatting; btcguess script heuristics;
  • caller-buffer codecs: base58, base64, bech32 (segwit, CashAddr and generic bech32), eip55_to_slice, encode_base58_addr_to_slice, pushbytes and BtcVarInt;
  • transports, a part at a time: bcur bytewords, UR parsing and single-part URs; bbqr headers, parts and compression.

Re-exports§

pub use address::decode_bitcoin_based_address;
pub use address::DecodedAddress;
pub use address::encode_address_to_slice;
pub use address::encode_base58_addr_to_slice;
pub use address::decode_evm_address;
pub use address::eip55_to_slice;
pub use crypto::SignerError;
pub use btcguess::ScriptGuess;
pub use btcguess::guess_in_script;
pub use btcguess::guess_out_script;
pub use cardano::decode_cardano_address;
pub use cardano_derive::CARDANO_HARDENED;
pub use cardano_derive::CardanoExtendedKey;
pub use cardano_derive::CardanoExtendedPubKey;
pub use cardano_derive::cardano_harden;
pub use cardano_derive::cardano_icarus_master_key;
pub use inline::InlineBytes;
pub use massa::decode_massa_address;
pub use pubkey::PubKey;
pub use pushbytes::parse_push_bytes;
pub use pushbytes::push_bytes_to_slice;
pub use script::ALL_FORMATS;
pub use script::ScriptBytes;
pub use script::formats_per_network;
pub use script::generate_script;
pub use solana_addr::decode_solana_address;
pub use address::encode_base58_addr;
pub use address::parse_bitcoin_based_address;
pub use address::eip55;
pub use address::parse_evm_address;
pub use btcguess::GuessResult;
pub use btcguess::guess_by_in_script;
pub use btcguess::guess_by_out_script;
pub use btctx::BtcTx;
pub use btctx::BtcTxInput;
pub use btctx::BtcTxOutput;
pub use btctx::BtcTxSign;
pub use btctx::Signer;
pub use btctxparse::BtcInputSig;
pub use btctxparse::extract_btc_input_sig;
pub use cardano::cardano_base_address;
pub use cardano::cardano_enterprise_address;
pub use cardano::cardano_key_hash;
pub use cardano::cardano_reward_address;
pub use cardano::parse_cardano_address;
pub use cardanotx::CardanoAsset;
pub use cardanotx::CardanoInput;
pub use cardanotx::CardanoOutput;
pub use cardanotx::CardanoSigner;
pub use cardanotx::CardanoTx;
pub use cardanotx::CardanoVkeyWitness;
pub use evmabi::AbiBuffer;
pub use evmabi::AbiValue;
pub use evmabi::evm_call;
pub use evmtx::EvmTx;
pub use evmtx::EvmTxType;
pub use insertable::Format;
pub use insertable::Insertable;
pub use massa::parse_massa_address;
pub use out::guess_out;
pub use out::Out;
pub use out::get_outs;
pub use pushbytes::push_bytes;
pub use reward::block_reward;
pub use reward::cumulative_reward;
pub use script::Script;
pub use script::format_def;
pub use solana_addr::parse_solana_address;

Modules§

address
Address parsing and encoding across Bitcoin-family, EVM, Massa and Solana networks (port of address.go, eip55.go).
base58
Base58 encoding using the Bitcoin alphabet.
base64
Standard base64 (RFC 4648, +/ alphabet, = padding) into caller buffers, as used for PSBT text encoding.
bbqr
BBQr, Coinkite’s protocol for moving a file through a series of QR codes. Only the strings are handled here; rendering or scanning QR codes is left to the caller.
bcur
Uniform Resources (BCR-2020-005), the ur: strings hardware wallets exchange as QR codes. Only the strings are handled here; rendering or scanning QR codes is left to the caller.
bech32
Bech32 / Bech32m segwit addresses (BIP-173 / BIP-350) and Bitcoin Cash CashAddr encoding.
btcguess
Heuristics to recover a public-key hash (and possibly the pubkey) from a Bitcoin output or input script (port of btcguess.go).
btcraw
Heap-free Bitcoin transaction signing: a borrowed transaction view (RawTx) with streaming sighash computation (legacy, BIP-143 segwit v0, BIP-341 taproot), serialization into a caller buffer, and txid.
btctx
Bitcoin transactions: building, signing (legacy, segwit BIP-143, taproot BIP-341/340), serialization and parsing. Port of btctx.go and btctx_p2tr.go.
btctxparse
Parsing/extraction of signatures from Bitcoin input scripts and recomputing the sighash they committed to. Port of btctxparse.go.
cardano
Cardano (Shelley-era) addresses: enterprise, base and reward addresses for mainnet and testnet, plus the blake2b-224 key credentials they encode.
cardano_derive
Cardano BIP32-Ed25519 (CIP-1852) hierarchical key derivation and extended-key signing.
cardanotx
Cardano (Shelley/Conway-era) transactions: build, sign and (de)serialize the CBOR wire format expected by the Cardano ledger.
cbor
A minimal, self-contained CBOR codec sufficient for Cardano transaction serialization.
crypto
Cryptographic primitives used by outscript, built on purecrypto.
evmabi
EVM ABI encoding (port of evmabi.go).
evmraw
Heap-free EVM transaction signing: RawEvmTx holds fixed-size fields and streams its RLP encoding straight into keccak-256 or a caller buffer.
evmtx
EVM transactions: legacy, EIP-2930, EIP-1559, EIP-4844. Build, sign, serialize, parse and recover sender. Port of evmtx.go.
hash
Hashing helpers built on purecrypto::hash.
inline
InlineBytes: a short byte string stored inline, for heap-free APIs that return variable-length data (scripts, hashes, encodings) by value.
insertable
Composable Insertable operations that define how an output script is derived from a public key.
massa
Massa address parsing (port of massa.go).
out
The Out type: a generated output script with format name and network flags, plus script-recognition helpers (guess_out, get_outs).
psbt
Partially Signed Bitcoin Transactions (BIP-174, version 0), without allocating.
pubkey
Public-key abstraction used by crate::script.
pushbytes
Bitcoin script PUSHDATA encoding/decoding.
reward
Block reward and cumulative-supply calculations across networks. Port of reward.go.
rlp
RLP (Recursive Length Prefix) encoding/decoding.
script
Output-script generation for a public key.
solana
Solana keys, program-derived addresses and compact-u16 encoding, plus (with alloc) instructions and transactions (legacy + v0). Port of solanatx.go, solana_instructions.go, solana_pda.go.
solana_addr
Solana address parsing (port of solana.go).
taproot
BIP-341 taproot script trees, heap-free: leaf and branch hashes, merkle roots and paths, output keys and control blocks.

Structs§

BtcAmount
A Bitcoin amount in satoshis (1 BTC = 100,000,000 satoshis).
BtcVarInt
A Bitcoin variable-length integer as defined in the Bitcoin protocol. Values 0-0xfc are a single byte; larger values use a prefix byte (0xfd, 0xfe, 0xff) followed by 2, 4, or 8 little-endian bytes.

Enums§

Error
Everything that can go wrong in outscript.

Type Aliases§

Result
A Result with this crate’s Error.