Rust / LWK reference
LWK provides Rust primitives for wallet management, PSET construction, and signing. The page explains AMP2 client methods, LWK operations, serialization helpers, and dependencies.
LWK (Liquid Wallet Kit) provides the Rust primitives for wallet management, PSET construction, and signing. The Amp2 client crate adds AMP2 server communication.
AMP2 client
Amp2::new
Amp2::new(keyorigin_xpub: String, url: String) -> Result<Amp2>Creates a new AMP2 client connected to the specified server. The keyorigin_xpub identifies the venue's key material.
amp2.descriptor_from_str
amp2.descriptor_from_str(keyorigin_xpub: &str) -> Result<Descriptor>Builds a 2-of-2 multisig descriptor combining the given xpub with AMP2's server xpub.
amp2.register
amp2.register(descriptor: ConfidentialDescriptor) -> Result<RegisterResponse>
// RegisterResponse { wid: String }Registers a wallet descriptor with AMP2. Returns the deterministic wallet ID.
amp2.cosign
amp2.cosign(pset: PartiallySignedTransaction) -> Result<CosignResponse>
// CosignResponse { pset: PartiallySignedTransaction }Submits a PSET for AMP2 cosigning. The HSM validates restrictions and adds its signature.
amp2.elements_network
amp2.elements_network() -> ElementsNetworkReturns the network configuration (testnet/mainnet) for use with TxBuilder.
LWK wallet operations
ConfidentialDescriptor::from_str
use elements_miniscript::ConfidentialDescriptor;
ConfidentialDescriptor::from_str(descriptor_str: &str) -> Result<ConfidentialDescriptor>Parses a confidential descriptor string into a structured descriptor object.
TxBuilder::new
use lwk_wollet::TxBuilder;
TxBuilder::new(network: ElementsNetwork) -> TxBuilderCreates a new transaction builder for the given network.
builder.add_recipient
builder.add_recipient(
address: &elements::Address,
satoshi: u64,
asset_id: &elements::AssetId,
) -> Result<()>Adds an output to the transaction. Can be called multiple times for multi recipient transactions.
builder.finish
builder.finish(wallet: &Wollet) -> PartiallySignedTransactionSelects UTXOs, computes fees, and produces an unsigned PSET.
LWK signing
SwSigner::new
use lwk_signer::SwSigner;
SwSigner::new(mnemonic: &str, is_mainnet: bool) -> Result<SwSigner>Creates a software signer from a BIP 39 mnemonic. Set is_mainnet to false for testnet.
signer.sign
signer.sign(pset: &mut PartiallySignedTransaction) -> Result<()>Adds the signer's signatures to the PSET in place.
signer.keyorigin_xpub
use lwk_common::Bip;
signer.keyorigin_xpub(bip: &Bip) -> Result<String>Returns the keyorigin xpub string (e.g., [fingerprint/path]tpub...) for the given derivation scheme.
PSET serialization helpers
use base64::engine::general_purpose;
use base64::Engine;
use elements::encode as el_encode;
use elements::pset::PartiallySignedTransaction;
pub fn pset_to_b64(pset: &PartiallySignedTransaction) -> String {
let bytes = el_encode::serialize(pset);
general_purpose::STANDARD.encode(&bytes)
}
pub fn pset_from_b64(s: &str) -> eyre::Result<PartiallySignedTransaction> {
let bytes = general_purpose::STANDARD.decode(s)?;
Ok(el_encode::deserialize::<PartiallySignedTransaction>(&bytes)?)
}Typical integration flow
Amp2::new(xpub, url): connect to AMP2amp2.descriptor_from_str(xpub): build descriptoramp2.register(descriptor): register wallet, get WIDTxBuilder::new(network)+add_recipient+finish: build PSETamp2.cosign(pset): AMP2 cosignssigner.sign(&mut pset): sign locally- Broadcast via Esplora or Elements RPC
Dependencies
[dependencies]
lwk_wollet = "0.7"
lwk_signer = "0.7"
lwk_common = "0.7"
elements = "0.24"
elements-miniscript = "0.3"
base64 = "0.21"Check crates.io for the latest versions.