Skip to content

Fixes for RGB transfer return encodings #391

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

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
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
34 changes: 17 additions & 17 deletions src/rgb.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use ::psbt::{serialize::Serialize, Psbt};
use amplify::{
confinement::U32,
hex::{FromHex, ToHex},
};
use amplify::hex::{FromHex, ToHex};
use anyhow::Result;
use autosurgeon::reconcile;
use bitcoin::{psbt::PartiallySignedTransaction, Network, Txid};
Expand All @@ -13,7 +10,7 @@ use garde::Validate;
use miniscript_crate::DescriptorPublicKey;
use rgb::{RgbDescr, RgbWallet};
use rgbstd::{
containers::BindleContent,
containers::{Bindle, BindleContent},
contract::ContractId,
interface::TypedState,
persistence::{Inventory, Stash, Stock},
Expand All @@ -25,7 +22,7 @@ use std::{
ops::Sub,
str::FromStr,
};
use strict_encoding::{tn, StrictSerialize};
use strict_encoding::tn;
use thiserror::Error;

pub mod accept;
Expand Down Expand Up @@ -705,7 +702,7 @@ pub async fn full_transfer_asset(
psbt,
commit,
outpoint,
..
txid,
} = internal_transfer_asset(
transfer_req,
params,
Expand All @@ -730,6 +727,7 @@ pub async fn full_transfer_asset(
consig,
psbt,
commit,
txid,
};

rgb_account.clone().update(&mut rgb_account_changes);
Expand Down Expand Up @@ -762,7 +760,7 @@ pub async fn transfer_asset(
psbt,
commit,
outpoint,
..
txid,
} = internal_transfer_asset(
request.clone(),
params,
Expand All @@ -787,6 +785,7 @@ pub async fn transfer_asset(
consig,
psbt,
commit,
txid,
};

store_stock_account_transfers(sk, stock, rgb_account, rgb_transfers)
Expand Down Expand Up @@ -839,12 +838,9 @@ async fn internal_transfer_asset(
let consig_id = transfer.bindle_id().to_string();
let consig = if let (Some(offer_id), Some(bid_id)) = (params.offer_id, params.bid_id) {
let swap = TransferSwap::with(&offer_id, &bid_id, transfer.unbindle());
swap.to_strict_serialized::<{ U32 }>()
.map_err(|err| TransferError::WrongConsig(err.to_string()))?
Bindle::new(swap.consig).to_string()
} else {
transfer
.to_strict_serialized::<{ U32 }>()
.map_err(|err| TransferError::WrongConsig(err.to_string()))?
transfer.to_string()
};

let bp_txid = bp::Txid::from_hex(&psbt.to_txid().to_hex())
Expand All @@ -857,7 +853,7 @@ async fn internal_transfer_asset(
let rgb_transfer = RgbTransfer {
iface: rgb_invoice.iface.unwrap().to_string(),
consig_id: consig_id.clone(),
consig: consig.to_hex(),
consig: consig.clone(),
tx: bp_txid,
is_send: true,
};
Expand All @@ -874,15 +870,15 @@ async fn internal_transfer_asset(
.insert(contract_id, vec![rgb_transfer]);
}

let consig_hex = consig.to_hex();
let commit = commit.to_hex();
let psbt = psbt.to_string();

let resp = RgbInternalTransferResponse {
consig_id,
consig: consig_hex,
consig,
psbt,
commit,
txid: bp_txid.to_string(),
outpoint: outpoint.to_string(),
};

Expand Down Expand Up @@ -1447,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 @@ -1462,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("-----") {
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
4 changes: 4 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,8 @@ pub struct RgbTransferResponse {
pub psbt: String,
/// Tapret Commitment (used to spend output)
pub commit: String,
/// Transfer Bitcoin L1 transaction id
pub txid: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -729,6 +731,8 @@ pub struct RgbInternalTransferResponse {
pub outpoint: String,
/// Tapret Commitment (used to spend output)
pub commit: String,
/// Transfer Bitcoin L1 transaction id
pub txid: String,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions tests/rgb/integration/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1906,6 +1906,7 @@ async fn allow_consecutive_full_transfer_bidirectional() -> anyhow::Result<()> {
consig,
psbt,
commit: _,
..
} = full_transfer_resp;

let request = SignPsbtRequest {
Expand Down Expand Up @@ -1969,6 +1970,7 @@ async fn allow_consecutive_full_transfer_bidirectional() -> anyhow::Result<()> {
consig,
psbt,
commit: _,
..
} = full_transfer_resp;

let request = SignPsbtRequest {
Expand Down Expand Up @@ -2082,6 +2084,7 @@ async fn allow_save_transfer_and_verify() -> anyhow::Result<()> {
consig,
psbt,
commit: _,
..
} = resp;

let request = SignPsbtRequest {
Expand Down