Skip to content
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

Blinding API cleanups #143

Merged
merged 9 commits into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 5 additions & 9 deletions examples/pset_blind_coinjoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,20 +266,16 @@ fn main() {
// ----------------------------------------------------------
// B Adds it's own outputs. Step 2 completed
// ----- Step 3: B to blind it's own outputs
let inp_txout_sec = [
None,
Some(&asset_txout_secrets.sec),
];
let mut inp_txout_sec = HashMap::new();
inp_txout_sec.insert(1, asset_txout_secrets.sec);

pset.blind_non_last(&mut rng, &secp, &inp_txout_sec).unwrap();
assert_eq!(pset, deser_pset(&tests["pset_coinjoined_B_blinded"]));

// Step 4: A blinds it's own inputs
let inp_txout_sec = [
Some(&btc_txout_secrets.sec),
None,
];
pset.blind_last(&mut rng, &secp, &inp_txout_sec).unwrap();
let mut inp_txout_sec_a = HashMap::new();
inp_txout_sec_a.insert(0, btc_txout_secrets.sec);
pset.blind_last(&mut rng, &secp, &inp_txout_sec_a).unwrap();
assert_eq!(pset, deser_pset(&tests["pset_coinjoined_blinded"]));

// check whether the blinding was correct
Expand Down
23 changes: 10 additions & 13 deletions examples/raw_blind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use elements::{
bitcoin::PublicKey, pset::PartiallySignedTransaction as Pset, Address, AddressParams, OutPoint,
Script, TxOutSecrets, TxOutWitness, Txid, WScriptHash,
};
use elements::{pset, secp256k1_zkp};
use elements::{pset, secp256k1_zkp, SurjectionInput};

use elements::encode::{deserialize, serialize_hex};
use elements::hashes::hex::FromHex;
Expand Down Expand Up @@ -168,8 +168,8 @@ fn main() {
// Add outputs
// Send 5_000 worth of asset units to new address
let inputs = [
(btc_txout.asset, Some(&btc_txout_secrets.sec)),
(asset_txout.asset, Some(&asset_txout_secrets.sec)),
(SurjectionInput::from_txout_secrets(btc_txout_secrets.sec)),
(SurjectionInput::from_txout_secrets(asset_txout_secrets.sec)),
];

let dest_wsh =
Expand All @@ -179,7 +179,7 @@ fn main() {
let dest_blind_pk =
PublicKey::from_str("0212bf0ea45b733dfde8ecb5e896306c4165c666c99fc5d1ab887f71393a975cea")
.unwrap();
let (dest_asset_txout, dest_abf, dest_vbf) = TxOut::new_not_last_confidential(
let (dest_asset_txout, dest_abf, dest_vbf, _) = TxOut::new_not_last_confidential(
&mut rng,
&secp,
dest_amt,
Expand All @@ -200,7 +200,7 @@ fn main() {
let change_wsh =
WScriptHash::from_str("f6b43d56e004e9d0b1ec2fc3c95511d81af08420992be8dec7f86cdf8970b3e2")
.unwrap();
let (change_asset_txout, asset_change_abf, asset_change_vbf) =
let (change_asset_txout, asset_change_abf, asset_change_vbf, _) =
TxOut::new_not_last_confidential(
&mut rng,
&secp,
Expand Down Expand Up @@ -254,19 +254,16 @@ fn main() {

// For the last output we require all secrets.
let inputs = [
(btc_txout.asset, &btc_txout_secrets.sec),
(asset_txout.asset, &asset_txout_secrets.sec),
btc_txout_secrets.sec,
asset_txout_secrets.sec,
];
let (btc_change_txout, _abf, _vbf) = TxOut::new_last_confidential(
let (btc_change_txout, _abf, _vbf, _) = TxOut::new_last_confidential(
&mut rng,
&secp,
change_amt,
Address::p2wsh(
&Script::new_v0_wsh(&change_wsh),
Some(change_blind_pk.inner),
&PARAMS,
),
btc_txout_secrets.sec.asset,
Script::new_v0_wsh(&change_wsh),
change_blind_pk.inner,
&inputs,
&output_secrets,
)
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_one_inp_signed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_signed.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/blinded_unsigned.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/extracted_tx.hex

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/test_vector/raw_blind/finalized.hex

Large diffs are not rendered by default.

43 changes: 25 additions & 18 deletions src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ impl AddressParams {
bech_hrp: "ert",
blech_hrp: "el",
};

/// The default liquid testnet network address parameters.
pub const LIQUID_TESTNET: AddressParams = AddressParams {
p2pkh_prefix: 36,
p2sh_prefix: 19,
blinded_prefix: 23,
bech_hrp: "tex",
blech_hrp: "tlq",
};
}

/// The method used to produce an address
Expand Down Expand Up @@ -439,7 +448,7 @@ impl Address {
if data.len() < 2 || data.len() > 40 + if blinded { 33 } else { 0 } {
return Err(AddressError::InvalidWitnessProgramLength(data.len() - if blinded { 33 } else { 0 }));
}

// Specific segwit v0 check.
if !blinded && version.to_u8() == 0 && data.len() != 20 && data.len() != 32 {
return Err(AddressError::InvalidSegwitV0ProgramLength(data.len()));
Expand Down Expand Up @@ -649,20 +658,19 @@ impl FromStr for Address {
// shorthands
let liq = &AddressParams::LIQUID;
let ele = &AddressParams::ELEMENTS;
let liq_test = &AddressParams::LIQUID_TESTNET;

let net_arr = [liq, ele, liq_test];

// Bech32.
let prefix = find_prefix(s);
if match_prefix(prefix, liq.bech_hrp) {
return Address::from_bech32(s, false, liq);
}
if match_prefix(prefix, liq.blech_hrp) {
return Address::from_bech32(s, true, liq);
}
if match_prefix(prefix, ele.bech_hrp) {
return Address::from_bech32(s, false, ele);
}
if match_prefix(prefix, ele.blech_hrp) {
return Address::from_bech32(s, true, ele);
for net in net_arr.iter() {
// Bech32.
if match_prefix(prefix, net.bech_hrp) {
return Address::from_bech32(s, false, net);
}
if match_prefix(prefix, net.blech_hrp) {
return Address::from_bech32(s, true, net);
}
}

// Base58.
Expand All @@ -675,11 +683,10 @@ impl FromStr for Address {
}

let p = data[0];
if p == liq.p2pkh_prefix || p == liq.p2sh_prefix || p == liq.blinded_prefix {
return Address::from_base58(&data, liq);
}
if p == ele.p2pkh_prefix || p == ele.p2sh_prefix || p == ele.blinded_prefix {
return Address::from_base58(&data, ele);
for net in net_arr.iter() {
if p == net.p2pkh_prefix || p == net.p2sh_prefix || p == net.blinded_prefix {
return Address::from_base58(&data, net);
}
}

Err(AddressError::InvalidAddress(s.to_owned()))
Expand Down
Loading