Skip to content
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
131 changes: 129 additions & 2 deletions bindings/uniffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ use rgb_lib::{
InvoiceData as RgbLibInvoiceData, Media, Metadata, MultisigKeys, MultisigOnlineOptions,
MultisigVotingStatus as RgbLibMultisigVotingStatus, MultisigWallet as RgbLibMultisigWallet,
Online, OnlineOptions, Operation as RgbLibOperation, OperationInfo as RgbLibOperationInfo,
OperationResult, Outpoint, PendingVanillaTx, ProofOfReserves, PsbtInputInfo,
PsbtInspection, PsbtOutputInfo, ReceiveData, Recipient as RgbLibRecipient,
OnchainSwapAssetHistory, OnchainSwapCompletion, OnchainSwapConsignment, OnchainSwapInput,
OnchainSwapLeg, OnchainSwapLegKind, OnchainSwapOffer, OnchainSwapProposal,
OnchainSwapReceiveResult as RgbLibOnchainSwapReceiveResult, OnchainSwapRequest,
OnchainSwapRole, OperationResult, Outpoint, PendingVanillaTx, ProofOfReserves,
PsbtInputInfo, PsbtInspection, PsbtOutputInfo, ReceiveData, Recipient as RgbLibRecipient,
RecipientInfo as RgbLibRecipientInfo, RecipientType, RefreshFilter, RefreshTransferStatus,
RefreshedTransfer, RespondToOperation as RgbLibRespondToOperation,
RgbAllocation as RgbLibRgbAllocation, RgbInputInfo as RgbLibRgbInputInfo,
Expand Down Expand Up @@ -98,6 +101,16 @@ impl From<Assignment> for RgbLibAssignment {
}
}
}
pub struct OnchainSwapReceiveResult {
pub assignments: Vec<Assignment>,
}
impl From<RgbLibOnchainSwapReceiveResult> for OnchainSwapReceiveResult {
fn from(orig: RgbLibOnchainSwapReceiveResult) -> Self {
Self {
assignments: orig.assignments.into_iter().map(|a| a.into()).collect(),
}
}
}
pub struct InvoiceData {
pub recipient_id: String,
pub asset_schema: Option<AssetSchema>,
Expand Down Expand Up @@ -1390,6 +1403,86 @@ impl Wallet {
self._get_wallet().send_btc_end(online, signed_psbt)
}

fn create_swap_offer(
&self,
maker_gives: OnchainSwapLeg,
maker_receives: OnchainSwapLeg,
network_fee_sat: u64,
expiration_timestamp: Option<u64>,
proxy_url: Option<String>,
) -> Result<OnchainSwapOffer, RgbLibError> {
self._get_wallet().create_swap_offer(
maker_gives,
maker_receives,
network_fee_sat,
expiration_timestamp,
proxy_url,
)
}

fn accept_swap_offer(
&self,
online: Online,
offer: OnchainSwapOffer,
min_confirmations: u8,
skip_sync: bool,
) -> Result<OnchainSwapRequest, RgbLibError> {
self._get_wallet()
.accept_swap_offer(online, offer, min_confirmations, skip_sync)
}

fn accept_swap_request(
&self,
online: Online,
request: OnchainSwapRequest,
min_confirmations: u8,
skip_sync: bool,
) -> Result<OnchainSwapProposal, RgbLibError> {
self._get_wallet()
.accept_swap_request(online, request, min_confirmations, skip_sync)
}

fn complete_swap_proposal(
&self,
online: Online,
proposal: OnchainSwapProposal,
min_confirmations: u8,
skip_sync: bool,
) -> Result<OnchainSwapCompletion, RgbLibError> {
self._get_wallet()
.complete_swap_proposal(online, proposal, min_confirmations, skip_sync)
}

fn process_swap_completion(
&self,
online: Online,
completion: OnchainSwapCompletion,
) -> Result<OnchainSwapCompletion, RgbLibError> {
self._get_wallet().process_swap_completion(online, completion)
}

fn broadcast_swap_completion(
&self,
online: Online,
completion: OnchainSwapCompletion,
) -> Result<String, RgbLibError> {
self._get_wallet()
.broadcast_swap_completion(online, completion)
}

fn accept_swap_transfers(
&self,
online: Online,
completion: OnchainSwapCompletion,
role: OnchainSwapRole,
skip_sync: bool,
) -> Result<OnchainSwapReceiveResult, RgbLibError> {
Ok(self
._get_wallet()
.accept_swap_transfers(online, completion, role, skip_sync)?
.into())
}

fn sync(&self, online: Online, options: SyncOptions) -> Result<(), RgbLibError> {
self._get_wallet().sync(online, options.into())
}
Expand Down Expand Up @@ -1812,3 +1905,37 @@ impl MultisigWallet {
}

uniffi::deps::static_assertions::assert_impl_all!(MultisigWallet: Sync, Send);

#[cfg(test)]
mod tests {
const UDL: &str = include_str!("rgb-lib.udl");

#[test]
fn uniffi_exposes_swap_api_without_raw_rgb_primitives() {
for method in [
"create_swap_offer",
"accept_swap_offer",
"accept_swap_request",
"complete_swap_proposal",
"process_swap_completion",
"broadcast_swap_completion",
"accept_swap_transfers",
] {
assert!(UDL.contains(method), "missing {method} from UDL");
}

for raw_name in [
"color_psbt",
"color_psbt_and_consume",
"consume_fascia",
"Fascia",
"RgbTransfer",
"BuilderSeal",
] {
assert!(
!UDL.contains(raw_name),
"raw primitive {raw_name} must not be exposed in UDL"
);
}
}
}
130 changes: 130 additions & 0 deletions bindings/uniffi/src/rgb-lib.udl
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,103 @@ dictionary RgbInspection {
sequence<RgbOperationInfo> operations;
};

[Remote]
enum OnchainSwapRole {
"Maker",
"Taker",
};

[Remote]
enum OnchainSwapLegKind {
"Btc",
"Rgb",
};

[Remote]
dictionary OnchainSwapLeg {
OnchainSwapLegKind kind;
string? asset_id;
u64 amount;
};

[Remote]
dictionary OnchainSwapInput {
Outpoint outpoint;
u64 amount_sat;
string script_pubkey_hex;
};

[Remote]
dictionary OnchainSwapConsignment {
string asset_id;
string path;
string? endpoint;
string txid;
u32 vout;
u64 blinding;
string recipient_id;
};

[Remote]
dictionary OnchainSwapAssetHistory {
string asset_id;
string path;
string? endpoint;
string recipient_id;
};

[Remote]
dictionary OnchainSwapOffer {
string swap_id;
OnchainSwapLeg maker_gives;
OnchainSwapLeg maker_receives;
BitcoinNetwork bitcoin_network;
u64 network_fee_sat;
u64 rgb_output_sat;
u64? expiration_timestamp;
string? maker_btc_address;
string? maker_rgb_recipient_id;
string? maker_rgb_script_pubkey_hex;
u64? maker_rgb_blinding;
string? proxy_url;
};

[Remote]
dictionary OnchainSwapRequest {
OnchainSwapOffer offer;
sequence<OnchainSwapInput> taker_inputs;
string? taker_btc_address;
string? taker_rgb_recipient_id;
string? taker_rgb_script_pubkey_hex;
u64? taker_rgb_blinding;
string taker_change_script_pubkey_hex;
};

[Remote]
dictionary OnchainSwapProposal {
OnchainSwapRequest request;
sequence<OnchainSwapInput> maker_inputs;
string maker_change_script_pubkey_hex;
string psbt;
string txid;
sequence<OnchainSwapConsignment> consignments;
OnchainSwapAssetHistory? maker_history;
};

[Remote]
dictionary OnchainSwapCompletion {
OnchainSwapProposal proposal;
string psbt;
string? finalized_psbt;
string txid;
sequence<OnchainSwapConsignment> consignments;
OnchainSwapAssetHistory? taker_history;
};

dictionary OnchainSwapReceiveResult {
sequence<Assignment> assignments;
};

[Remote]
dictionary InitOperationResult {
string psbt;
Expand Down Expand Up @@ -979,6 +1076,39 @@ interface Wallet {
[Throws=RgbLibError]
string send_btc_end(Online online, string signed_psbt);

[Throws=RgbLibError]
OnchainSwapOffer create_swap_offer(
OnchainSwapLeg maker_gives, OnchainSwapLeg maker_receives,
u64 network_fee_sat, u64? expiration_timestamp, string? proxy_url);

[Throws=RgbLibError]
OnchainSwapRequest accept_swap_offer(
Online online, OnchainSwapOffer offer, u8 min_confirmations,
boolean skip_sync);

[Throws=RgbLibError]
OnchainSwapProposal accept_swap_request(
Online online, OnchainSwapRequest request, u8 min_confirmations,
boolean skip_sync);

[Throws=RgbLibError]
OnchainSwapCompletion complete_swap_proposal(
Online online, OnchainSwapProposal proposal, u8 min_confirmations,
boolean skip_sync);

[Throws=RgbLibError]
OnchainSwapCompletion process_swap_completion(
Online online, OnchainSwapCompletion completion);

[Throws=RgbLibError]
string broadcast_swap_completion(
Online online, OnchainSwapCompletion completion);

[Throws=RgbLibError]
OnchainSwapReceiveResult accept_swap_transfers(
Online online, OnchainSwapCompletion completion, OnchainSwapRole role,
boolean skip_sync);

[Throws=RgbLibError]
void sync(Online online, SyncOptions options);

Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ use bdk_wallet::{
#[cfg(any(feature = "electrum", feature = "esplora"))]
use bdk_wallet::{
Update,
bitcoin::{Transaction as BdkTransaction, blockdata::fee_rate::FeeRate, hashes::HashEngine},
bitcoin::{
Sequence, Transaction as BdkTransaction, TxIn, Witness, absolute::LockTime,
blockdata::fee_rate::FeeRate, hashes::HashEngine, transaction::Version as TxVersion,
},
chain::{
DescriptorExt,
spk_client::{FullScanRequest, FullScanResponse, SyncRequest, SyncResponse},
Expand Down Expand Up @@ -270,7 +273,7 @@ use crate::{
error::IndexerError,
utils::{
INDEXER_STOP_GAP, OffchainResolver, check_proxy, get_indexer_and_resolver, hash_file,
script_buf_from_recipient_id,
recipient_id_from_script_buf, script_buf_from_recipient_id,
},
wallet::{AssignmentsCollection, Indexer, multisig::RespondToOperation},
};
Expand Down
8 changes: 5 additions & 3 deletions src/wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,11 @@ pub use objects::{
};
#[cfg(any(feature = "electrum", feature = "esplora"))]
pub use objects::{
BurnBeginResult, BurnDetails, InflateBeginResult, InflateDetails, OnlineOptions,
OperationResult, RefreshFilter, RefreshResult, RefreshTransferStatus, RefreshedTransfer,
SendBeginResult, SendDetails,
BurnBeginResult, BurnDetails, InflateBeginResult, InflateDetails, OnchainSwapAssetHistory,
OnchainSwapCompletion, OnchainSwapConsignment, OnchainSwapInput, OnchainSwapLeg,
OnchainSwapLegKind, OnchainSwapOffer, OnchainSwapProposal, OnchainSwapReceiveResult,
OnchainSwapRequest, OnchainSwapRole, OnlineOptions, OperationResult, RefreshFilter,
RefreshResult, RefreshTransferStatus, RefreshedTransfer, SendBeginResult, SendDetails,
};
pub use offline::RgbWalletOpsOffline;
#[cfg(any(feature = "electrum", feature = "esplora"))]
Expand Down
Loading