@bennyblader/ddk-rn 0.3.5 → 0.3.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ddk-ffi/Cargo.lock +1 -1
- package/ddk-ffi/Cargo.toml +1 -1
- package/ddk-ffi/src/lib.rs +62 -34
- package/package.json +1 -1
package/ddk-ffi/Cargo.lock
CHANGED
package/ddk-ffi/Cargo.toml
CHANGED
package/ddk-ffi/src/lib.rs
CHANGED
|
@@ -52,8 +52,8 @@ pub enum DLCError {
|
|
|
52
52
|
InvalidTransaction,
|
|
53
53
|
#[error("Insufficient funds")]
|
|
54
54
|
InsufficientFunds,
|
|
55
|
-
#[error("Invalid argument")]
|
|
56
|
-
InvalidArgument,
|
|
55
|
+
#[error("Invalid argument: {0}")]
|
|
56
|
+
InvalidArgument(String),
|
|
57
57
|
#[error("Serialization error")]
|
|
58
58
|
SerializationError,
|
|
59
59
|
#[error("Secp256k1 error: {0}")]
|
|
@@ -82,10 +82,14 @@ impl From<dlc::Error> for DLCError {
|
|
|
82
82
|
fn from(err: dlc::Error) -> Self {
|
|
83
83
|
match err {
|
|
84
84
|
dlc::Error::Secp256k1(_) => DLCError::Secp256k1Error(err.to_string()),
|
|
85
|
-
dlc::Error::InvalidArgument =>
|
|
85
|
+
dlc::Error::InvalidArgument => {
|
|
86
|
+
DLCError::InvalidArgument("Error from rust-dlc".to_string())
|
|
87
|
+
}
|
|
86
88
|
dlc::Error::Miniscript(_) => DLCError::MiniscriptError,
|
|
87
89
|
dlc::Error::P2wpkh(_) => DLCError::InvalidTransaction,
|
|
88
|
-
dlc::Error::InputsIndex(_) =>
|
|
90
|
+
dlc::Error::InputsIndex(_) => {
|
|
91
|
+
DLCError::InvalidArgument("Error from rust-dlc: InputsIndex".to_string())
|
|
92
|
+
}
|
|
89
93
|
}
|
|
90
94
|
}
|
|
91
95
|
}
|
|
@@ -236,11 +240,9 @@ pub fn dlc_input_info_to_rust(input: &DlcInputInfo) -> Result<RustDlcInputInfo,
|
|
|
236
240
|
PublicKey::from_slice(&input.local_fund_pubkey).map_err(|_| DLCError::InvalidPublicKey)?;
|
|
237
241
|
let remote_fund_pubkey =
|
|
238
242
|
PublicKey::from_slice(&input.remote_fund_pubkey).map_err(|_| DLCError::InvalidPublicKey)?;
|
|
239
|
-
let contract_id: [u8; 32] = input
|
|
240
|
-
.
|
|
241
|
-
|
|
242
|
-
.try_into()
|
|
243
|
-
.map_err(|_| DLCError::InvalidArgument)?;
|
|
243
|
+
let contract_id: [u8; 32] = input.contract_id.as_slice().try_into().map_err(|_| {
|
|
244
|
+
DLCError::InvalidArgument("Contract id length must be 32 bytes.".to_string())
|
|
245
|
+
})?;
|
|
244
246
|
Ok(RustDlcInputInfo {
|
|
245
247
|
fund_tx: btc_tx,
|
|
246
248
|
fund_vout: input.fund_vout,
|
|
@@ -255,7 +257,8 @@ pub fn dlc_input_info_to_rust(input: &DlcInputInfo) -> Result<RustDlcInputInfo,
|
|
|
255
257
|
|
|
256
258
|
/// Convert UniFFI TxInputInfo to rust-dlc TxInputInfo
|
|
257
259
|
pub fn tx_input_info_to_rust(input: &TxInputInfo) -> Result<DlcTxInputInfo, DLCError> {
|
|
258
|
-
let txid = Txid::from_str(&input.txid)
|
|
260
|
+
let txid = Txid::from_str(&input.txid)
|
|
261
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
259
262
|
Ok(DlcTxInputInfo {
|
|
260
263
|
outpoint: OutPoint {
|
|
261
264
|
txid,
|
|
@@ -409,7 +412,8 @@ pub fn create_cet(
|
|
|
409
412
|
fund_vout: u32,
|
|
410
413
|
lock_time: u32,
|
|
411
414
|
) -> Result<Transaction, DLCError> {
|
|
412
|
-
let txid = Txid::from_str(&fund_tx_id)
|
|
415
|
+
let txid = Txid::from_str(&fund_tx_id)
|
|
416
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
413
417
|
|
|
414
418
|
let local_btc_output = BtcTxOut {
|
|
415
419
|
value: Amount::from_sat(local_output.value),
|
|
@@ -454,7 +458,8 @@ pub fn create_cets(
|
|
|
454
458
|
local_serial_id: u64,
|
|
455
459
|
remote_serial_id: u64,
|
|
456
460
|
) -> Result<Vec<Transaction>, DLCError> {
|
|
457
|
-
let txid = Txid::from_str(&fund_tx_id)
|
|
461
|
+
let txid = Txid::from_str(&fund_tx_id)
|
|
462
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
458
463
|
|
|
459
464
|
let fund_tx_input = TxIn {
|
|
460
465
|
previous_output: OutPoint {
|
|
@@ -500,7 +505,8 @@ pub fn create_refund_transaction(
|
|
|
500
505
|
fund_tx_id: String,
|
|
501
506
|
fund_vout: u32,
|
|
502
507
|
) -> Result<Transaction, DLCError> {
|
|
503
|
-
let txid = Txid::from_str(&fund_tx_id)
|
|
508
|
+
let txid = Txid::from_str(&fund_tx_id)
|
|
509
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
504
510
|
|
|
505
511
|
let local_output = BtcTxOut {
|
|
506
512
|
value: Amount::from_sat(local_amount),
|
|
@@ -574,7 +580,8 @@ pub fn verify_fund_tx_signature(
|
|
|
574
580
|
) -> Result<bool, DLCError> {
|
|
575
581
|
let btc_tx = transaction_to_btc_tx(&fund_tx)?;
|
|
576
582
|
let pk = PublicKey::from_slice(&pubkey).map_err(|_| DLCError::InvalidPublicKey)?;
|
|
577
|
-
let input_txid = Txid::from_str(&txid)
|
|
583
|
+
let input_txid = Txid::from_str(&txid)
|
|
584
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
578
585
|
|
|
579
586
|
// Find the input index
|
|
580
587
|
let input_index = btc_tx
|
|
@@ -583,7 +590,10 @@ pub fn verify_fund_tx_signature(
|
|
|
583
590
|
.position(|input| {
|
|
584
591
|
input.previous_output.txid == input_txid && input.previous_output.vout == vout
|
|
585
592
|
})
|
|
586
|
-
.ok_or(DLCError::InvalidArgument
|
|
593
|
+
.ok_or(DLCError::InvalidArgument(format!(
|
|
594
|
+
"Input index not found in {}",
|
|
595
|
+
input_txid
|
|
596
|
+
)))?;
|
|
587
597
|
|
|
588
598
|
// Create a simple P2WPKH script for verification
|
|
589
599
|
let wpkh = WPubkeyHash::hash(&pk.serialize());
|
|
@@ -626,8 +636,10 @@ pub fn get_raw_funding_transaction_input_signature(
|
|
|
626
636
|
value: u64,
|
|
627
637
|
) -> Result<Vec<u8>, DLCError> {
|
|
628
638
|
let btc_tx = transaction_to_btc_tx(&funding_transaction)?;
|
|
629
|
-
let sk = SecretKey::from_slice(&privkey)
|
|
630
|
-
|
|
639
|
+
let sk = SecretKey::from_slice(&privkey)
|
|
640
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid private key".to_string()))?;
|
|
641
|
+
let prev_txid = Txid::from_str(&prev_tx_id)
|
|
642
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
631
643
|
|
|
632
644
|
// Find the input index
|
|
633
645
|
let input_index = btc_tx
|
|
@@ -636,7 +648,10 @@ pub fn get_raw_funding_transaction_input_signature(
|
|
|
636
648
|
.position(|input| {
|
|
637
649
|
input.previous_output.txid == prev_txid && input.previous_output.vout == prev_tx_vout
|
|
638
650
|
})
|
|
639
|
-
.ok_or(DLCError::InvalidArgument
|
|
651
|
+
.ok_or(DLCError::InvalidArgument(format!(
|
|
652
|
+
"Input index not found in {}",
|
|
653
|
+
prev_txid
|
|
654
|
+
)))?;
|
|
640
655
|
|
|
641
656
|
let secp = get_secp_context();
|
|
642
657
|
// Create P2WPKH script for signing
|
|
@@ -666,8 +681,10 @@ pub fn sign_fund_transaction_input(
|
|
|
666
681
|
value: u64,
|
|
667
682
|
) -> Result<Transaction, DLCError> {
|
|
668
683
|
let mut btc_tx = transaction_to_btc_tx(&fund_transaction)?;
|
|
669
|
-
let sk = SecretKey::from_slice(&privkey)
|
|
670
|
-
|
|
684
|
+
let sk = SecretKey::from_slice(&privkey)
|
|
685
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid private key".to_string()))?;
|
|
686
|
+
let prev_txid = Txid::from_str(&prev_tx_id)
|
|
687
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid transaction id".to_string()))?;
|
|
671
688
|
|
|
672
689
|
// Find the input index
|
|
673
690
|
let input_index = btc_tx
|
|
@@ -676,7 +693,10 @@ pub fn sign_fund_transaction_input(
|
|
|
676
693
|
.position(|input| {
|
|
677
694
|
input.previous_output.txid == prev_txid && input.previous_output.vout == prev_tx_vout
|
|
678
695
|
})
|
|
679
|
-
.ok_or(DLCError::InvalidArgument
|
|
696
|
+
.ok_or(DLCError::InvalidArgument(format!(
|
|
697
|
+
"Input index not found in {}",
|
|
698
|
+
prev_txid
|
|
699
|
+
)))?;
|
|
680
700
|
|
|
681
701
|
let secp = Secp256k1::signing_only();
|
|
682
702
|
dlc::util::sign_p2wpkh_input(
|
|
@@ -707,8 +727,8 @@ pub fn sign_cet(
|
|
|
707
727
|
.iter()
|
|
708
728
|
.map(|sig| vec_to_schnorr_signature(sig))
|
|
709
729
|
.collect::<Result<Vec<_>, _>>()?;
|
|
710
|
-
let funding_sk =
|
|
711
|
-
|
|
730
|
+
let funding_sk = SecretKey::from_slice(&funding_secret_key)
|
|
731
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid funding secret key".to_string()))?;
|
|
712
732
|
let other_pk = PublicKey::from_slice(&other_pubkey).map_err(|_| DLCError::InvalidPublicKey)?;
|
|
713
733
|
let funding_script = Script::from_bytes(&funding_script_pubkey);
|
|
714
734
|
let secp = get_secp_context();
|
|
@@ -746,7 +766,9 @@ pub fn create_cet_adaptor_sigs_from_oracle_info(
|
|
|
746
766
|
msgs: Vec<Vec<Vec<u8>>>,
|
|
747
767
|
) -> Result<Vec<AdaptorSignature>, DLCError> {
|
|
748
768
|
if msgs.len() != cets.len() {
|
|
749
|
-
return Err(DLCError::InvalidArgument
|
|
769
|
+
return Err(DLCError::InvalidArgument(
|
|
770
|
+
"Messages length must be equal to cets length".to_string(),
|
|
771
|
+
));
|
|
750
772
|
}
|
|
751
773
|
|
|
752
774
|
let cets = cets
|
|
@@ -756,25 +778,30 @@ pub fn create_cet_adaptor_sigs_from_oracle_info(
|
|
|
756
778
|
let oracle_infos = oracle_info
|
|
757
779
|
.iter()
|
|
758
780
|
.map(|info| {
|
|
759
|
-
let public_key = XOnlyPublicKey::from_slice(&info.public_key)
|
|
781
|
+
let public_key = XOnlyPublicKey::from_slice(&info.public_key)
|
|
782
|
+
.map_err(|_| DLCError::InvalidPublicKey)?;
|
|
760
783
|
let nonces = info
|
|
761
784
|
.nonces
|
|
762
785
|
.iter()
|
|
763
786
|
.map(|nonce| XOnlyPublicKey::from_slice(nonce))
|
|
764
|
-
.collect::<Result<Vec<_>, _>>()
|
|
787
|
+
.collect::<Result<Vec<_>, _>>()
|
|
788
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid nonce pubkey".to_string()))?;
|
|
765
789
|
Ok(DlcOracleInfo { public_key, nonces })
|
|
766
790
|
})
|
|
767
|
-
.collect::<Result<Vec<_>,
|
|
768
|
-
.map_err(|_| DLCError::InvalidArgument)?;
|
|
791
|
+
.collect::<Result<Vec<_>, DLCError>>()
|
|
792
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid oracle info".to_string()))?;
|
|
769
793
|
|
|
770
|
-
let funding_sk =
|
|
771
|
-
|
|
794
|
+
let funding_sk = SecretKey::from_slice(&funding_secret_key)
|
|
795
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid funding secret key".to_string()))?;
|
|
772
796
|
let funding_script = Script::from_bytes(&funding_script_pubkey);
|
|
773
797
|
let msgs = msgs
|
|
774
798
|
.iter()
|
|
775
799
|
.map(|msg| {
|
|
776
800
|
msg.iter()
|
|
777
|
-
.map(|m|
|
|
801
|
+
.map(|m| {
|
|
802
|
+
Message::from_digest_slice(m)
|
|
803
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid message".to_string()))
|
|
804
|
+
})
|
|
778
805
|
.collect::<Result<Vec<_>, _>>()
|
|
779
806
|
})
|
|
780
807
|
.collect::<Result<Vec<_>, _>>()?;
|
|
@@ -901,7 +928,8 @@ pub fn create_cet_adaptor_signature_from_oracle_info(
|
|
|
901
928
|
msgs: Vec<Vec<u8>>,
|
|
902
929
|
) -> Result<AdaptorSignature, DLCError> {
|
|
903
930
|
let btc_tx = transaction_to_btc_tx(&cet)?;
|
|
904
|
-
let sk = SecretKey::from_slice(&funding_sk)
|
|
931
|
+
let sk = SecretKey::from_slice(&funding_sk)
|
|
932
|
+
.map_err(|_| DLCError::InvalidArgument("Invalid funding secret key".to_string()))?;
|
|
905
933
|
let funding_script = Script::from_bytes(&funding_script_pubkey);
|
|
906
934
|
|
|
907
935
|
// Convert oracle info
|
|
@@ -924,7 +952,7 @@ pub fn create_cet_adaptor_signature_from_oracle_info(
|
|
|
924
952
|
.iter()
|
|
925
953
|
.map(|msg| Message::from_digest_slice(msg))
|
|
926
954
|
.collect();
|
|
927
|
-
let msg_vec = messages.map_err(|_| DLCError::InvalidArgument)?;
|
|
955
|
+
let msg_vec = messages.map_err(|_| DLCError::InvalidArgument("Invalid message".to_string()))?;
|
|
928
956
|
let nested_msgs = vec![msg_vec]; // Wrap in vector for single oracle
|
|
929
957
|
|
|
930
958
|
let secp = get_secp_context();
|
|
@@ -1395,6 +1423,6 @@ mod tests {
|
|
|
1395
1423
|
0,
|
|
1396
1424
|
0,
|
|
1397
1425
|
);
|
|
1398
|
-
assert!(matches!(result, Err(DLCError::InvalidArgument)));
|
|
1426
|
+
assert!(matches!(result, Err(DLCError::InvalidArgument(_))));
|
|
1399
1427
|
}
|
|
1400
1428
|
}
|