Expand description
Lightning-shaped payment-channel transactions for sidestr and Bitcoin-family testnets.
This is an attributed Rust port of lib/channel.mjs in Melvin Carvalho’s
Hitch, at commit 62f8e39.
Hitch is the Lightning construction without the Lightning network: two
peers lock a coin in a 2-of-2 Taproot leaf, hold asymmetric revocable
commitments, and update them off chain. Optional HTLC outputs add a
preimage success path, an absolute timeout and a revocation path.
A commitment’s revocation key is a two-party key: the counterparty’s
basepoint plus the owner’s per-state point (revocation_pub). The owner
never holds its secret, so it cannot use its own revocation leaf; the
counterparty learns the per-state secret when the state is revoked and
then signs with revocation_key. Every announced point carries a proof
of possession (pop_sign), so neither side can choose a point that
cancels the other’s.
The transaction builders are paired with a pure peer state machine in
protocol and the one-hop invoice decisions in route. Hosts provide
relay transport, wallet funding, atomic snapshot storage, chain watches
and broadcasting. Every builder supports both ordinary BIP 341 sighashes
and Bitcoin Knots’ unified sighash used by txbt4. This is a channel
kernel, not a Lightning node, and it does not speak to Lightning peers.
use bitcoin::hashes::Hash;
use bitcoin::secp256k1::{Keypair, SecretKey};
use bitcoin::{Amount, OutPoint, Txid};
use sidestr_core::block::secp;
use sidestr_hitch::{Balances, Channel, ChannelState, RevocationKeys, Side};
let key_a = SecretKey::from_slice(&[0x11; 32]).unwrap();
let key_b = SecretKey::from_slice(&[0x22; 32]).unwrap();
let pub_a = Keypair::from_secret_key(secp(), &key_a).x_only_public_key().0;
let pub_b = Keypair::from_secret_key(secp(), &key_b).x_only_public_key().0;
let channel = Channel::new(
pub_a,
pub_b,
OutPoint { txid: Txid::all_zeros(), vout: 0 },
Amount::from_sat(100_000),
Amount::from_sat(300),
6,
).unwrap();
let state = ChannelState {
balances: Balances { a: Amount::from_sat(60_000), b: Amount::from_sat(40_000) },
revocation: RevocationKeys { a: pub_a, b: pub_b },
htlcs: vec![],
};
let commitment = channel.commitment(1, Side::A, &state).unwrap();
assert_eq!(commitment.local_value, Amount::from_sat(59_700));
assert_eq!(commitment.remote_value, Amount::from_sat(40_000));Modules§
- protocol
- Hitch’s channel protocol (
lib/peer.mjs) as a pure, sans-IO state machine. - route
- Hitch’s one-hop invoices and HTLC routing (
lib/route.mjs) as a pure decision engine.
Structs§
- Balances
- The two free balances in a channel state. HTLC values are separate.
- Channel
- A funded channel and its invariant parameters.
- Channel
State - Free balances, revocation keys and outstanding HTLCs at one state number.
- Commitment
- One peer’s fully derived commitment transaction for a state.
- Commitment
Htlc - An HTLC after its exact output index and scripts have been derived for a particular commitment owner.
- Funding
Script - Hitch’s 2-of-2 funding output.
- Htlc
- An HTLC carried by a channel state.
- Htlc
Claim - Output and witness choices for an HTLC claim transaction.
- Htlc
Script - A three-path HTLC output in one commitment transaction.
- Htlc
Script Args - Arguments needed to derive one commitment owner’s HTLC scripts.
- Leaf
Signature - A 65-byte tapscript signature: BIP-340 signature followed by its explicit
sighash type (
0x01or Knots’0x21). - Leaf
Spend - One Taproot leaf and the control block that proves it to its output.
- PopSignature
- A 64-byte BIP 340 proof of possession of a revocation secret, carried as 128 lower-case hex characters on Hitch’s wire.
- Revocation
Keys - The revocation public key each owner put into this state.
- ToLocal
Script - A revocable delayed output in one commitment transaction.
Enums§
- Error
- An error while deriving scripts, building a state or signing a spend.
- Htlc
Claim Path - Which leaf claims an HTLC output.
- Output
Kind - An output’s role in a commitment transaction.
- Side
- One of the two channel peers.
Ais the funder and pays channel fees. - Sweep
Path - Which
to_localleaf a sweep uses.
Constants§
- DEFAULT_
DELAY - Hitch’s default relative delay, in blocks.
- DEFAULT_
FEE - Hitch’s default per-transaction channel fee, in satoshis.
- DUST
- Outputs below this value are omitted from commitments, as in Hitch.
- MAX_
DELAY - The longest relative delay Hitch accepts.
- MAX_
EXPIRY - Values below this are block heights rather than timestamps in
nLockTime. - MAX_FEE
- The largest per-transaction channel fee Hitch accepts, in satoshis.
- MIN_
DELAY - The shortest relative delay Hitch accepts.
- MIN_FEE
- The smallest per-transaction channel fee Hitch accepts, in satoshis.
Functions§
- claim_
htlc - Spend one HTLC output through its success, timeout or revocation leaf.
- even_
secret - Negate
secretwhen its public point has an odd y coordinate, so the result’s point is the even lift of the same x-only key. This is Hitch’sevenSecret. - funding_
script - Construct the 2-of-2 funding script. Keys are sorted lexicographically in the leaf, independently of their A/B protocol roles.
- htlc_
script - Construct the success, timeout and revocation leaves for an HTLC. The
tree is
branch(branch(success, timeout), revocation). - internal_
key - Derive Hitch’s fixed unspendable internal key: BIP 341’s
Htweaked bytagged_hash("hitch/nums", "hitch"). - key_
path_ spend - Spend a plain
to_remoteoutput by its literal x-only key path. - pop_
sign - Prove possession of a revocation secret: a BIP 340 signature by
secretovertagged_hash("hitch/pop", context || point). Hitch’scontextis"<channel id>/<owner role>/<base or state number>". - pop_
verify - Check a proof of possession made by
pop_signforpointincontext. Malformed signatures verify asfalse. - preimage_
in - The payment preimage revealed by a spend of an HTLC output, if any
32-byte witness item hashes to
payment_hashunder SHA-256. This is Hitch’spreimageIn, used to carry a preimage the counterparty revealed on the chain back upstream. - revocation_
key - The secret of a two-party revocation key:
even(basepoint_secret) + even(per_state_secret) mod n. This is Hitch’srevocationKey; it signs the revocation leaves of a commitment whose public key isrevocation_pub. - revocation_
pub - The public half of a two-party revocation key:
lift_x(point) + even(scalar)·G, as an x-only key. This is Hitch’srevocationPub. - script_
path_ sighash - Compute a tapscript signature hash and its explicit wire hash type.
- sign_
leaf - Sign one tapscript leaf with an explicit hash type byte.
- sweep_
to_ local - Spend a commitment’s
to_localoutput through its delayed or revocation leaf and install the complete witness. - to_
local_ script - Construct the owner’s delayed leaf and the counterparty’s revocation
leaf, rooted as
branch(delayed, revocation). - to_
remote_ script - A direct key-path output using Hitch’s x-only channel key literally.
- verify_
leaf_ signature - Verify one tapscript signature under the same family rules used to sign.
Type Aliases§
- Result
- Result type returned by this crate.