1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
use std::error::Error;
// src/bitcoin/sidechains/liquid/client.rs
//! Liquid client implementation
//!
//! Provides a client for interacting with the Liquid sidechain.
//! Implements Blockstream's Liquid protocol specifications.
//! [AIR-3][AIS-3][AIM-3][AIP-3][RES-3]
use serde::{Serialize, Deserialize};
use std::str::FromStr;
use std::fmt;
use std::sync::Arc;
use std::collections::HashMap;
use url::Url;
use thiserror::Error;
use async_trait::async_trait;
use elements::{
encode::serialize,
Address,
Script,
Transaction,
TxOut,
BlockHash,
Txid,
AssetId,
hex::ToHex,
confidential::{Asset, Value},
issuance::{AssetIssuance, ContractHash, IssuanceEntropy},
};
use bitcoin::Amount;
use tokio::sync::Mutex;
use elements_rpc::{RawTransaction, Client as ElementsClient, ElementsRpcApi};
use crate::AnyaResult;
/// Errors that can occur during Liquid client operations
#[derive(Error, Debug)]
pub enum LiquidError {
/// Network error
#[error("Network error: {0}")]
NetworkError(String),
/// RPC error
#[error("RPC error: {0}")]
RpcError(String),
/// Transaction error
#[error("Transaction error: {0}")]
TransactionError(String),
/// Encoding error
#[error("Encoding error: {0}")]
EncodingError(String),
/// Wallet error
#[error("Wallet error: {0}")]
WalletError(String),
/// Asset error
#[error("Asset error: {0}")]
AssetError(String),
/// Validation error
#[error("Validation error: {0}")]
ValidationError(String),
/// Configuration error
#[error("Configuration error: {0}")]
ConfigurationError(String),
/// Peg operation error
#[error("Peg operation error: {0}")]
PegError(String),
/// No such asset
#[error("No such asset: {0}")]
NoSuchAsset(String),
/// Insufficient funds
#[error("Insufficient funds: {0}")]
InsufficientFunds(String),
}
/// Network type for Liquid
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum NetworkType {
/// Mainnet
Mainnet,
/// Testnet
Testnet,
/// Regtest (local development)
Regtest,
}
impl fmt::Display for NetworkType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NetworkType::Mainnet => write!(f, "mainnet"),
NetworkType::Testnet => write!(f, "testnet"),
NetworkType::Regtest => write!(f, "regtest"),
}
}
}
impl NetworkType {
/// Get a default node URL for this network
pub fn default_node_url(&self) -> &'static str {
match self {
NetworkType::Mainnet => "https://liquid.network:7041",
NetworkType::Testnet => "https://liquidtestnet.com:18891",
NetworkType::Regtest => "http://localhost:7041",
}
}
/// Get the policy asset (L-BTC) ID for this network
pub fn policy_asset(&self) -> &'static str {
match self {
NetworkType::Mainnet => "6f0279e9ed041c3d710a9f57d0c02928416460c4b722ae3457a11eec381c526d",
NetworkType::Testnet => "144c654344aa716d6f3abcc1ca90e5641e4e2a7f633bc09fe3baf64585819a49",
NetworkType::Regtest => "5ac9f65c0efcc4775e0baec4ec03abdde22473cd3cf33c0419ca290e0751b225",
}
}
}
/// Liquid client configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiquidClientConfig {
/// Network type
pub network: NetworkType,
/// Node URL
pub node_url: String,
/// RPC username
pub rpc_user: String,
/// RPC password
pub rpc_password: String,
/// Transaction confirmation blocks
pub tx_confirmation_blocks: u32,
/// Transaction confirmation timeout (in seconds)
pub tx_confirmation_timeout: u64,
/// Default fee rate (satoshis per vbyte)
pub default_fee_rate: u64,
}
impl Default for LiquidClientConfig {
fn default() -> Self {
Self {
network: NetworkType::Testnet,
node_url: NetworkType::Testnet.default_node_url().to_string(),
rpc_user: "user".to_string(),
rpc_password: "password".to_string(),
tx_confirmation_blocks: 2,
tx_confirmation_timeout: 300, // 5 minutes
default_fee_rate: 1, // 1 sat/vbyte
}
}
}
/// A transaction response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiquidTransactionResponse {
/// Transaction ID
pub txid: String,
/// Transaction version
pub version: u32,
/// Transaction size
pub size: u32,
/// Transaction locktime
pub locktime: u32,
/// Block hash (None if unconfirmed)
pub block_hash: Option<String>,
/// Block height (None if unconfirmed)
pub block_height: Option<u32>,
/// Timestamp (None if unconfirmed)
pub timestamp: Option<u64>,
/// Inputs
pub vin: Vec<LiquidTxInputResponse>,
/// Outputs
pub vout: Vec<LiquidTxOutputResponse>,
/// Fee
pub fee: u64,
/// Fee asset
pub fee_asset: String,
/// Status (confirmed or unconfirmed)
pub status: TransactionStatus,
/// Hex representation of the transaction
pub hex: String,
}
/// Input of a transaction response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiquidTxInputResponse {
/// Previous transaction ID
pub txid: String,
/// Previous output index
pub vout: u32,
/// Input value (if confidential, this is revealed if you own the key)
pub value: Option<u64>,
/// Input asset (if confidential, this is revealed if you own the key)
pub asset: Option<String>,
/// Input address (if available)
pub address: Option<String>,
/// Input script
pub script_sig: Option<String>,
/// Witness data
pub witness: Option<Vec<String>>,
/// Is this a pegin input
pub is_pegin: bool,
/// Sequence
pub sequence: u32,
}
/// Output of a transaction response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LiquidTxOutputResponse {
/// Output value
pub value: u64,
/// Output asset
pub asset: String,
/// Output address
pub address: Option<String>,
/// Output script
pub script_pubkey: String,
/// Is this value confidential
pub value_blinding_factor: Option<String>,
/// Is this asset confidential
pub asset_blinding_factor: Option<String>,
/// Is this a pegout output
pub is_pegout: bool,
}
/// Transaction status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TransactionStatus {
/// Transaction is confirmed
Confirmed,
/// Transaction is in the mempool
Mempool,
/// Transaction is unconfirmed and not in mempool (possibly replaced by fee)
Unknown,
}
/// Asset issuance parameters
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetIssuanceParams {
/// Asset name
pub asset_name: String,
/// Asset ticker symbol
pub ticker: String,
/// Precision (decimal places)
pub precision: u8,
/// Initial issuance amount
pub initial_amount: u64,
/// Is the asset reissuable
pub reissuable: bool,
/// Domain (helps with verification)
pub domain: Option<String>,
/// Contract hash (to uniquely identify the asset contract)
pub contract_hash: Option<String>,
}
/// Asset metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetMetadata {
/// Asset name
pub name: String,
/// Asset ticker symbol
pub ticker: String,
/// Precision (decimal places)
pub precision: u8,
/// Domain (helps with verification)
pub domain: Option<String>,
/// Description of the asset
pub description: Option<String>,
/// Issuer name
pub issuer: Option<String>,
/// Issue date (Unix timestamp)
pub issue_date: Option<u64>,
/// Total issuance
pub total_issuance: Option<u64>,
/// Is the asset reissuable
pub reissuable: bool,
/// Additional metadata (key-value pairs)
pub extra: HashMap<String, String>,
}
/// Asset information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssetInfo {
/// Asset ID
pub asset_id: String,
/// Asset metadata
pub metadata: AssetMetadata,
/// Whether it's the policy asset (L-BTC)
pub is_policy_asset: bool,
/// Total issuance so far
pub issuance_amount: u64,
/// Issuance txid
pub issuance_txid: Option<String>,
/// Issuance entropy (used to derive asset IDs)
pub issuance_entropy: Option<String>,
/// Reissuance token asset ID (if reissuable)
pub reissuance_token: Option<String>,
/// Contract hash
pub contract_hash: Option<String>,
}
/// Liquid client trait
#[async_trait]
pub trait LiquidClientTrait {
/// Get the current block count
async fn get_block_count(&self) -> Result<u32, LiquidError>;
/// Get a block by hash
async fn get_block(&self, hash: &str) -> Result<elements::Block, LiquidError>;
/// Get raw transaction
async fn get_raw_transaction(&self, txid: &str) -> Result<Transaction, LiquidError>;
/// Get transaction information
async fn get_transaction(&self, txid: &str) -> Result<LiquidTransactionResponse, LiquidError>;
/// Send raw transaction
async fn send_raw_transaction(&self, tx: &Transaction) -> Result<Txid, LiquidError>;
/// Get asset information
async fn get_asset_info(&self, asset_id: &str) -> Result<AssetInfo, LiquidError>;
/// Issue a new asset
async fn issue_asset(&self, params: AssetIssuanceParams) -> Result<(String, Transaction), LiquidError>;
/// Reissue an existing asset
async fn reissue_asset(&self, asset_id: &str, amount: u64) -> Result<(String, Transaction), LiquidError>;
/// Wait for transaction confirmation
async fn wait_for_confirmation(&self, txid: &Txid) -> Result<LiquidTransactionResponse, LiquidError>;
}
/// Liquid client implementation
pub struct LiquidClient {
/// Client configuration
config: LiquidClientConfig,
/// Elements RPC client
rpc: ElementsClient,
/// Known assets cache
assets_cache: Arc<Mutex<HashMap<String, AssetInfo>>>,
}
impl LiquidClient {
/// Create a new Liquid client
pub async fn new(config: LiquidClientConfig) -> Result<Self, LiquidError> {
// Validate URL
let url = Url::parse(&config.node_url)
.map_err(|e| LiquidError::ConfigurationError(format!("Invalid node URL: {}", e)))?;
// Create RPC client
let rpc = ElementsClient::new(
&config.node_url,
&config.rpc_user,
&config.rpc_password
).map_err(|e| LiquidError::NetworkError(format!("Failed to create RPC client: {}", e)))?;
// Test connection
let _block_count = rpc.get_block_count().await
.map_err(|e| LiquidError::NetworkError(format!("Failed to connect to node: {}", e)))?;
// Create client
let client = Self {
config,
rpc,
assets_cache: Arc::new(Mutex::new(HashMap::new())),
};
// Initialize cache with policy asset
let policy_asset_id = client.config.network.policy_asset();
let mut cache = client.assets_cache.lock().await;
cache.insert(policy_asset_id.to_string(), AssetInfo {
asset_id: policy_asset_id.to_string(),
metadata: AssetMetadata {
name: "Liquid Bitcoin".to_string(),
ticker: "L-BTC".to_string(),
precision: 8,
domain: Some("blockstream.com".to_string()),
description: Some("Bitcoin on the Liquid Network".to_string()),
issuer: Some("Liquid Federation".to_string()),
issue_date: None,
total_issuance: None,
reissuable: false,
extra: HashMap::new(),
},
is_policy_asset: true,
issuance_amount: 0, // Unknown
issuance_txid: None,
issuance_entropy: None,
reissuance_token: None,
contract_hash: None,
});
Ok(client)
}
/// Get the policy asset ID (L-BTC)
pub fn policy_asset_id(&self) -> String {
self.config.network.policy_asset().to_string()
}
/// Create a blinded transaction
pub async fn create_transaction(
&self,
inputs: Vec<(Txid, u32)>,
outputs: Vec<(Address, AssetId, u64)>,
fee_rate: Option<u64>,
) -> Result<Transaction, LiquidError> {
// Fetch UTXOs for inputs to get their values and assets
let mut tx_inputs = Vec::new();
let mut input_values = Vec::new();
let mut input_assets = Vec::new();
for (txid, vout) in inputs {
// Get the transaction
let prev_tx = self.get_raw_transaction(&txid.to_hex())
.await?;
// Get the output being spent
let output = prev_tx.output.get(vout as usize)
.ok_or_else(|| LiquidError::ValidationError(
format!("Output index {} not found in transaction {}", vout, txid)
))?;
// Extract value and asset
let value = match output.value {
Value::Explicit(value) => value,
_ => return Err(LiquidError::ValidationError(
format!("Confidential value in input {}:{}", txid, vout)
)),
};
let asset = match output.asset {
Asset::Explicit(asset) => asset,
_ => return Err(LiquidError::ValidationError(
format!("Confidential asset in input {}:{}", txid, vout)
)),
};
// Add to our collections
tx_inputs.push((txid, vout));
input_values.push(value);
input_assets.push(asset);
}
// Group outputs by asset
let mut output_map: HashMap<AssetId, Vec<(Address, u64)>> = HashMap::new();
for (address, asset_id, value) in outputs {
output_map.entry(asset_id)
.or_insert_with(Vec::new)
.push((address, value));
}
// Calculate fee based on estimated size
let estimated_size = 100 + tx_inputs.len() * 150 + output_map.len() * 50;
let fee_rate = fee_rate.unwrap_or(self.config.default_fee_rate);
let fee = (estimated_size as u64 * fee_rate) / 1000;
// Ensure we have enough of each asset
for (asset_id, outputs) in &output_map {
let total_output = outputs.iter().map(|(_, value)| *value).sum::<u64>();
// Find matching inputs
let matching_inputs = input_assets.iter()
.enumerate()
.filter(|(_, a)| **a == *asset_id)
.map(|(i, _)| input_values[i])
.sum::<u64>();
if matching_inputs < total_output {
return Err(LiquidError::InsufficientFunds(
format!("Insufficient funds for asset {}: {} < {}", asset_id, matching_inputs, total_output)
));
}
}
// Create the transaction
let mut tx = Transaction {
version: 2,
lock_time: 0,
input: Vec::new(),
output: Vec::new(),
};
// Add inputs
for (txid, vout) in tx_inputs {
tx.input.push(elements::TxIn {
previous_output: elements::OutPoint {
txid,
vout,
},
script_sig: Script::new(),
sequence: 0xFFFFFFFF,
asset_issuance: AssetIssuance::default(),
witness: elements::TxInWitness::default(),
});
}
// Add outputs
for (asset_id, outputs) in output_map {
for (address, value) in outputs {
tx.output.push(TxOut {
asset: Asset::Explicit(asset_id),
value: Value::Explicit(value),
nonce: elements::confidential::Nonce::Null,
script_pubkey: address.script_pubkey(),
witness: elements::TxOutWitness::default(),
});
}
}
// Add fee output
let policy_asset = AssetId::from_str(self.config.network.policy_asset())
.map_err(|e| LiquidError::ValidationError(
format!("Invalid policy asset ID: {}", e)
))?;
tx.output.push(TxOut {
asset: Asset::Explicit(policy_asset),
value: Value::Explicit(fee),
nonce: elements::confidential::Nonce::Null,
script_pubkey: Script::new(),
witness: elements::TxOutWitness::default(),
});
Ok(tx)
}
/// Generate asset metadata to include in issuance
fn generate_asset_metadata(params: &AssetIssuanceParams) -> Result<String, LiquidError> {
let metadata = AssetMetadata {
name: params.asset_name.clone(),
ticker: params.ticker.clone(),
precision: params.precision,
domain: params.domain.clone(),
description: None,
issuer: None,
issue_date: Some(std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap_or_default()
.as_secs()),
total_issuance: Some(params.initial_amount),
reissuable: params.reissuable,
extra: HashMap::new(),
};
serde_json::to_string(&metadata)
.map_err(|e| LiquidError::EncodingError(format!("Failed to encode asset metadata: {}", e)))
}
/// Parse transaction to extract asset details
async fn parse_transaction_assets(&self, tx: &Transaction) -> Result<HashMap<String, AssetInfo>, LiquidError> {
let mut assets = HashMap::new();
// Look for asset issuances
for (input_idx, input) in tx.input.iter().enumerate() {
if input.asset_issuance.is_issuance() {
// Extract issuance details
let issuance = &input.asset_issuance;
// Generate entropy from outpoint and contract hash
let outpoint = &input.previous_output;
let contract_hash = if issuance.asset_blinding_nonce != [0; 32] {
// Reissuance
ContractHash::from(issuance.asset_entropy)
} else {
// New issuance
// In a real implementation, we'd extract this from OP_RETURN data
ContractHash::from([0; 32])
};
// Calculate issuance entropy
let issuance_entropy = IssuanceEntropy::new(outpoint, contract_hash);
// Generate asset ID
let asset_id = AssetId::from_entropy(issuance_entropy);
// Extract asset amount
let issuance_amount = if let Some(value) = issuance.asset_amount {
value
} else {
0 // Confidential amount
};
// Try to extract metadata from OP_RETURN output
let mut metadata: Option<AssetMetadata> = None;
for output in &tx.output {
if output.script_pubkey.is_op_return() {
if let Some(data) = output.script_pubkey.op_return_data() {
if let Ok(meta_str) = String::from_utf8(data.to_vec()) {
if let Ok(meta) = serde_json::from_str::<AssetMetadata>(&meta_str) {
metadata = Some(meta);
break;
}
}
}
}
}
// Create asset info
let asset_info = AssetInfo {
asset_id: asset_id.to_hex(),
metadata: metadata.unwrap_or_else(|| AssetMetadata {
name: format!("Asset {}", asset_id.to_hex()),
ticker: "UNKN".to_string(),
precision: 8,
domain: None,
description: None,
issuer: None,
issue_date: None,
total_issuance: Some(issuance_amount as u64),
reissuable: issuance.token_amount.is_some(),
extra: HashMap::new(),
}),
is_policy_asset: false,
issuance_amount: issuance_amount as u64,
issuance_txid: Some(tx.txid().to_hex()),
issuance_entropy: Some(issuance_entropy.to_hex()),
reissuance_token: if issuance.token_amount.is_some() {
// Calculate reissuance token asset ID
Some(AssetId::reissuance_token_from_entropy(issuance_entropy, issuance.asset_blinding_nonce != [0; 32]).to_hex())
} else {
None
},
contract_hash: Some(contract_hash.to_hex()),
};
assets.insert(asset_id.to_hex(), asset_info);
}
}
Ok(assets)
}
}
#[async_trait]
impl LiquidClientTrait for LiquidClient {
async fn get_block_count(&self) -> Result<u32, LiquidError> {
self.rpc.get_block_count().await
.map_err(|e| LiquidError::RpcError(format!("Failed to get block count: {}", e)))
}
async fn get_block(&self, hash: &str) -> Result<elements::Block, LiquidError> {
// Parse hash
let hash = BlockHash::from_str(hash)
.map_err(|e| LiquidError::ValidationError(format!("Invalid block hash: {}", e)))?;
// Get block
let block_hex = self.rpc.get_block(&hash).await
.map_err(|e| LiquidError::RpcError(format!("Failed to get block: {}", e)))?;
// Parse block
elements::encode::deserialize(&hex::decode(block_hex)?)
.map_err(|e| LiquidError::EncodingError(format!("Failed to decode block: {}", e)))
}
async fn get_raw_transaction(&self, txid: &str) -> Result<Transaction, LiquidError> {
// Parse txid
let txid = Txid::from_str(txid)
.map_err(|e| LiquidError::ValidationError(format!("Invalid transaction ID: {}", e)))?;
// Get raw transaction
let tx_hex = self.rpc.get_raw_transaction_hex(&txid, None).await
.map_err(|e| LiquidError::RpcError(format!("Failed to get transaction: {}", e)))?;
// Parse transaction
elements::encode::deserialize(&hex::decode(tx_hex)?)
.map_err(|e| LiquidError::EncodingError(format!("Failed to decode transaction: {}", e)))
}
async fn get_transaction(&self, txid: &str) -> Result<LiquidTransactionResponse, LiquidError> {
// Get raw transaction first
let tx = self.get_raw_transaction(txid).await?;
// Get more details using getrawtransaction with verbose flag
let tx_info = self.rpc.get_raw_transaction(&Txid::from_str(txid).unwrap_or_default(), None).await
.map_err(|e| LiquidError::RpcError(format!("Failed to get transaction info: {}", e)))?;
// Convert RPC response to our format
let mut inputs = Vec::new();
for (i, input) in tx.input.iter().enumerate() {
inputs.push(LiquidTxInputResponse {
txid: input.previous_output.txid.to_hex(),
vout: input.previous_output.vout,
value: None, // Need to look up previous tx to get this
asset: None, // Need to look up previous tx to get this
address: None, // Need to decode script to get this
script_sig: Some(input.script_sig.to_hex()),
witness: Some(input.witness.to_vec().iter().map(|w| hex::encode(w)).collect()),
is_pegin: false, // Would need to check for pegin data
sequence: input.sequence,
});
}
let mut outputs = Vec::new();
for (i, output) in tx.output.iter().enumerate() {
let value = match output.value {
Value::Explicit(v) => v as u64,
Value::Confidential(_, _) => 0, // Confidential value
Value::Null => 0,
};
let asset = match output.asset {
Asset::Explicit(a) => a.to_hex(),
Asset::Confidential(_, _) => "confidential".to_string(),
Asset::Null => "null".to_string(),
};
outputs.push(LiquidTxOutputResponse {
value,
asset,
address: None, // Would need to decode script to address
script_pubkey: output.script_pubkey.to_hex(),
value_blinding_factor: None, // Would need blinding key to extract
asset_blinding_factor: None, // Would need blinding key to extract
is_pegout: false, // Would need to check for pegout data
});
}
// Find policy asset and extract fee
let policy_asset = self.config.network.policy_asset();
let fee = tx_info.fee.unwrap_or(0.0) * 100_000_000.0; // Convert BTC to satoshis
let response = LiquidTransactionResponse {
txid: tx.txid().to_hex(),
version: tx.version as u32,
size: serialize(&tx).len() as u32,
locktime: tx.lock_time,
block_hash: tx_info.blockhash.map(|h| h.to_hex()),
block_height: None, // Not directly available from RPC
timestamp: tx_info.time.map(|t| t as u64),
vin: inputs,
vout: outputs,
fee: fee as u64,
fee_asset: policy_asset.to_string(),
status: if tx_info.confirmations.unwrap_or(0) > 0 {
TransactionStatus::Confirmed
} else {
TransactionStatus::Mempool
},
hex: hex::encode(serialize(&tx)),
};
Ok(response)
}
async fn send_raw_transaction(&self, tx: &Transaction) -> Result<Txid, LiquidError> {
// Serialize transaction
let tx_hex = hex::encode(serialize(tx));
// Send transaction
self.rpc.send_raw_transaction(&tx_hex).await
.map_err(|e| LiquidError::TransactionError(format!("Failed to send transaction: {}", e)))
}
async fn get_asset_info(&self, asset_id: &str) -> Result<AssetInfo, LiquidError> {
// Check cache first
{
let cache = self.assets_cache.lock().await;
if let Some(info) = cache.get(asset_id) {
return Ok(info.clone());
}
}
// Check if it's the policy asset
if asset_id == self.config.network.policy_asset() {
return Ok(AssetInfo {
asset_id: asset_id.to_string(),
metadata: AssetMetadata {
name: "Liquid Bitcoin".to_string(),
ticker: "L-BTC".to_string(),
precision: 8,
domain: Some("blockstream.com".to_string()),
description: Some("Bitcoin on the Liquid Network".to_string()),
issuer: Some("Liquid Federation".to_string()),
issue_date: None,
total_issuance: None,
reissuable: false,
extra: HashMap::new(),
},
is_policy_asset: true,
issuance_amount: 0, // Unknown
issuance_txid: None,
issuance_entropy: None,
reissuance_token: None,
contract_hash: None,
});
}
// Try to retrieve from block explorer API or node
// This is a simplified implementation
// We'd need to implement logic to:
// 1. Scan the blockchain for issuance transactions
// 2. Extract metadata from OP_RETURN outputs
// 3. Calculate total issuance amount
// For now, return a placeholder
let info = AssetInfo {
asset_id: asset_id.to_string(),
metadata: AssetMetadata {
name: format!("Asset {}", asset_id),
ticker: "UNKN".to_string(),
precision: 8,
domain: None,
description: None,
issuer: None,
issue_date: None,
total_issuance: None,
reissuable: false,
extra: HashMap::new(),
},
is_policy_asset: false,
issuance_amount: 0,
issuance_txid: None,
issuance_entropy: None,
reissuance_token: None,
contract_hash: None,
};
// Cache it
{
let mut cache = self.assets_cache.lock().await;
cache.insert(asset_id.to_string(), info.clone());
}
Ok(info)
}
async fn issue_asset(&self, params: AssetIssuanceParams) -> Result<(String, Transaction), LiquidError> {
// In a real implementation, this would:
// 1. Get UTXOs from the wallet
// 2. Create a transaction with an issuance input
// 3. Add appropriate outputs for the asset and reissuance token
// 4. Sign and send the transaction
// For now, we'll just simulate the process
let contract_hash = match ¶ms.contract_hash {
Some(hash) => ContractHash::from_str(hash)
.map_err(|e| LiquidError::ValidationError(format!("Invalid contract hash: {}", e)))?,
None => ContractHash::from([0; 32]), // Default contract hash
};
// Generate asset ID (in a real implementation, this would be calculated from issuance entropy)
let asset_id = format!("asset_{}", uuid::Uuid::new_v4().to_string().replace("-", ""));
// Create transaction (this is a placeholder)
let tx = Transaction {
version: 2,
lock_time: 0,
input: Vec::new(),
output: Vec::new(),
};
// In a real implementation, we'd return the actual asset ID
Ok((asset_id, tx))
}
async fn reissue_asset(&self, asset_id: &str, amount: u64) -> Result<(String, Transaction), LiquidError> {
// In a real implementation, this would:
// 1. Find the reissuance token UTXO
// 2. Create a transaction with a reissuance input
// 3. Add appropriate outputs for the additional asset amount
// 4. Sign and send the transaction
// For now, we'll just simulate the process
// Validate asset ID
let _ = AssetId::from_str(asset_id)
.map_err(|e| LiquidError::ValidationError(format!("Invalid asset ID: {}", e)))?;
// Get asset info to check if it's reissuable
let asset_info = self.get_asset_info(asset_id).await?;
if asset_info.reissuance_token.is_none() {
return Err(LiquidError::AssetError(
format!("Asset {} is not reissuable", asset_id)
));
}
// Create transaction (this is a placeholder)
let tx = Transaction {
version: 2,
lock_time: 0,
input: Vec::new(),
output: Vec::new(),
};
// In a real implementation, we'd create and send the actual transaction
Ok((asset_id.to_string(), tx))
}
async fn wait_for_confirmation(&self, txid: &Txid) -> Result<LiquidTransactionResponse, LiquidError> {
let start = std::time::Instant::now();
let timeout = std::time::Duration::from_secs(self.config.tx_confirmation_timeout);
let txid_str = txid.to_hex();
loop {
if start.elapsed() > timeout {
return Err(LiquidError::TransactionError(
format!("Timeout waiting for confirmation of transaction {}", txid_str)
));
}
// Get transaction info
match self.get_transaction(&txid_str).await {
Ok(tx_response) => {
match tx_response.status {
TransactionStatus::Confirmed => {
return Ok(tx_response);
},
_ => {
// Not confirmed yet, wait a bit
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
}
}
},
Err(e) => {
// If the error is "not found", the transaction might be still propagating
if format!("{}", e).contains("not found") {
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
} else {
return Err(e);
}
}
}
}
}
}