Skip to content

Commit

Permalink
Fix consignment bindle format parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
cryptoquick committed Oct 14, 2023
1 parent 3dae2e0 commit a63b879
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
6 changes: 5 additions & 1 deletion src/rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ pub async fn accept_transfer(
Ok(resp)
}

#[derive(Debug, Clone, Eq, PartialEq, Display, From, Error)]
#[derive(Debug, Clone, Eq, PartialEq, Display, Error)]
#[display(doc_comments)]
pub enum SaveTransferError {
/// Some request data is missing. {0:?}
Expand All @@ -1458,6 +1458,10 @@ pub enum SaveTransferError {
WrongSwap(RgbOfferErrors),
/// Write I/O or connectivity error. {1} in {0}
Write(String, String),
/// Strict Serialize error
StrictSerializeError(#[from] strict_encoding::SerializeError),
// /// Bindle Parse Error
// BindleParseError(BindleParseError<rgb::containers::transfer::TransferId>),
}

pub async fn save_transfer(
Expand Down
93 changes: 67 additions & 26 deletions src/rgb/prebuild.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::{collections::BTreeMap, ops::Mul, str::FromStr};

use amplify::{confinement::Confined, hex::FromHex};
use amplify::{
confinement::{Confined, U32},
hex::FromHex,
};
use baid58::ToBaid58;
use bech32::{decode, FromBase32};
use bitcoin::Network;
Expand All @@ -9,12 +12,13 @@ use garde::Validate;
use rand::{rngs::StdRng, Rng, SeedableRng};
use rgb::{RgbWallet, TerminalPath};
use rgbstd::{
containers::{Bindle, Consignment},
contract::ContractId,
interface::TypedState,
persistence::{Inventory, Stash, Stock},
};
use rgbwallet::RgbInvoice;
use strict_encoding::tn;
use strict_encoding::{tn, StrictSerialize};

use crate::{
bitcoin::get_swap_new_address,
Expand Down Expand Up @@ -45,6 +49,8 @@ use crate::rgb::{
RgbSwapError, SaveTransferError, TransferError,
};

use super::transfer::extract_bindle;

pub const DUST_LIMIT_SATOSHI: u64 = 546;

pub async fn prebuild_transfer_asset(
Expand Down Expand Up @@ -863,38 +869,73 @@ pub async fn prebuild_buyer_swap(
pub fn prebuild_extract_transfer(
consignment: &str,
) -> Result<RgbExtractTransfer, SaveTransferError> {
let mut is_armored = false;
let serialized = if consignment.starts_with("rgb1") {
let (_, serialized, _) =
decode(consignment).expect("invalid serialized contract/genesis (bech32m format)");
Vec::<u8>::from_base32(&serialized)
.expect("invalid hexadecimal contract/genesis (bech32m format)")
} else if consignment.starts_with("-----BEGIN RGB CONTRACT-----") {
is_armored = true;
Vec::new()
} else {
Vec::<u8>::from_hex(consignment).expect("invalid hexadecimal contract/genesis")
};

let confined = Confined::try_from_iter(serialized.iter().copied())
.expect("invalid confined serialization");
let (tx_id, transfer, offer_id, bid_id) = match extract_transfer(consignment.to_owned()) {
Ok((txid, tranfer)) => (txid, tranfer, None, None),
_ => match extract_swap_transfer(consignment.to_owned()) {
Ok((txid, tranfer, offer_id, bid_id)) => (
txid,
tranfer,
Some(offer_id.to_baid58_string()),
Some(bid_id.to_baid58_string()),
),
Err(err) => return Err(SaveTransferError::WrongConsigSwap(err)),
},
};
if is_armored {
let transfer =
Bindle::<Consignment<true>>::from_str(consignment).expect("bindle parse error"); // ?;

let confined = transfer.to_strict_serialized::<U32>()?;

let (tx_id, transfer, offer_id, bid_id) = match extract_bindle(transfer) {
Ok((txid, transfer)) => (txid, transfer, None, None),
_ => match extract_swap_transfer(consignment.to_owned()) {
Ok((txid, transfer, offer_id, bid_id)) => (
txid,
transfer,
Some(offer_id.to_baid58_string()),
Some(bid_id.to_baid58_string()),
),
Err(err) => return Err(SaveTransferError::WrongConsigSwap(err)),
},
};

let contract_id = transfer.contract_id().to_string();
Ok(RgbExtractTransfer {
consig_id: transfer.id().to_string(),
contract_id,
tx_id,
transfer,
offer_id,
bid_id,
strict: confined,
})
let contract_id = transfer.contract_id().to_string();
Ok(RgbExtractTransfer {
consig_id: transfer.id().to_string(),
contract_id,
tx_id,
transfer,
offer_id,
bid_id,
strict: confined,
})
} else {
let confined = Confined::try_from_iter(serialized.iter().copied())
.expect("invalid confined serialization");
let (tx_id, transfer, offer_id, bid_id) = match extract_transfer(consignment.to_owned()) {
Ok((txid, transfer)) => (txid, transfer, None, None),
_ => match extract_swap_transfer(consignment.to_owned()) {
Ok((txid, transfer, offer_id, bid_id)) => (
txid,
transfer,
Some(offer_id.to_baid58_string()),
Some(bid_id.to_baid58_string()),
),
Err(err) => return Err(SaveTransferError::WrongConsigSwap(err)),
},
};

let contract_id = transfer.contract_id().to_string();
Ok(RgbExtractTransfer {
consig_id: transfer.id().to_string(),
contract_id,
tx_id,
transfer,
offer_id,
bid_id,
strict: confined,
})
}
}
10 changes: 8 additions & 2 deletions src/rgb/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bp::{seals::txout::CloseMethod, Chain, Txid};
use indexmap::IndexMap;
use psbt::{serialize::Serialize, Psbt};
use rgbstd::{
containers::{Bindle, Transfer},
containers::{Bindle, Consignment, Transfer},
contract::{ContractId, GraphSeal},
interface::TypedState,
persistence::{Inventory, Stash, Stock},
Expand Down Expand Up @@ -223,12 +223,18 @@ pub fn extract_transfer(transfer: String) -> Result<(Txid, Bindle<Transfer>), Ac
let transfer = Transfer::from_strict_serialized::<{ U32 }>(confined)
.map_err(|err| AcceptTransferError::WrongConsig(err.to_string()))?;

extract_bindle(Bindle::new(transfer))
}

pub fn extract_bindle(
transfer: Bindle<Consignment<true>>,
) -> Result<(Txid, Bindle<Transfer>), AcceptTransferError> {
for (bundle_id, _) in transfer.terminals() {
if transfer.known_transitions_by_bundle_id(bundle_id).is_none() {
return Err(AcceptTransferError::Inconclusive);
};
if let Some(AnchoredBundle { anchor, bundle: _ }) = transfer.anchored_bundle(bundle_id) {
return Ok((anchor.txid, Bindle::new(transfer)));
return Ok((anchor.txid, transfer));
}
}

Expand Down

0 comments on commit a63b879

Please sign in to comment.