1
- use crate :: bitcoin:: { Address , Amount , OutPoint , Script , Transaction , TxOut } ;
1
+ use crate :: bitcoin:: {
2
+ Address , Amount , BlockHash , DescriptorId , HashableOutPoint , OutPoint , Script , Transaction , TxOut , Txid
3
+ } ;
2
4
use crate :: error:: { CreateTxError , RequestBuilderError } ;
3
5
4
6
use bdk_core:: bitcoin:: absolute:: LockTime as BdkLockTime ;
@@ -23,7 +25,7 @@ use bdk_wallet::Balance as BdkBalance;
23
25
use bdk_wallet:: LocalOutput as BdkLocalOutput ;
24
26
use bdk_wallet:: Update as BdkUpdate ;
25
27
26
- use std:: collections:: HashMap ;
28
+ use std:: collections:: { BTreeMap , BTreeSet , HashMap } ;
27
29
use std:: convert:: TryFrom ;
28
30
use std:: sync:: { Arc , Mutex } ;
29
31
@@ -66,7 +68,7 @@ impl From<BdkChainPosition<BdkConfirmationBlockTime>> for ChainPosition {
66
68
} => {
67
69
let block_id = BlockId {
68
70
height : anchor. block_id . height ,
69
- hash : anchor. block_id . hash . to_string ( ) ,
71
+ hash : Arc :: new ( BlockHash ( anchor. block_id . hash ) ) ,
70
72
} ;
71
73
ChainPosition :: Confirmed {
72
74
confirmation_block_time : ConfirmationBlockTime {
@@ -84,21 +86,57 @@ impl From<BdkChainPosition<BdkConfirmationBlockTime>> for ChainPosition {
84
86
}
85
87
86
88
/// Represents the confirmation block and time of a transaction.
87
- #[ derive( Debug , uniffi:: Record ) ]
89
+ #[ derive( Debug , PartialEq , Eq , std :: hash :: Hash , uniffi:: Record ) ]
88
90
pub struct ConfirmationBlockTime {
89
91
/// The anchor block.
90
92
pub block_id : BlockId ,
91
93
/// The confirmation time of the transaction being anchored.
92
94
pub confirmation_time : u64 ,
93
95
}
94
96
97
+ impl From < BdkConfirmationBlockTime > for ConfirmationBlockTime {
98
+ fn from ( value : BdkConfirmationBlockTime ) -> Self {
99
+ Self {
100
+ block_id : value. block_id . into ( ) ,
101
+ confirmation_time : value. confirmation_time ,
102
+ }
103
+ }
104
+ }
105
+
106
+ impl From < ConfirmationBlockTime > for BdkConfirmationBlockTime {
107
+ fn from ( value : ConfirmationBlockTime ) -> Self {
108
+ Self {
109
+ block_id : value. block_id . into ( ) ,
110
+ confirmation_time : value. confirmation_time ,
111
+ }
112
+ }
113
+ }
114
+
95
115
/// A reference to a block in the canonical chain.
96
- #[ derive( Debug , uniffi:: Record ) ]
116
+ #[ derive( Debug , PartialEq , Eq , std :: hash :: Hash , uniffi:: Record ) ]
97
117
pub struct BlockId {
98
118
/// The height of the block.
99
119
pub height : u32 ,
100
120
/// The hash of the block.
101
- pub hash : String ,
121
+ pub hash : Arc < BlockHash > ,
122
+ }
123
+
124
+ impl From < bdk_wallet:: chain:: BlockId > for BlockId {
125
+ fn from ( value : bdk_wallet:: chain:: BlockId ) -> Self {
126
+ Self {
127
+ height : value. height ,
128
+ hash : Arc :: new ( BlockHash ( value. hash ) ) ,
129
+ }
130
+ }
131
+ }
132
+
133
+ impl From < BlockId > for bdk_wallet:: chain:: BlockId {
134
+ fn from ( value : BlockId ) -> Self {
135
+ Self {
136
+ height : value. height ,
137
+ hash : value. hash . 0 ,
138
+ }
139
+ }
102
140
}
103
141
104
142
/// A transaction that is deemed to be part of the canonical history.
@@ -203,7 +241,7 @@ impl From<BdkLocalOutput> for LocalOutput {
203
241
fn from ( local_utxo : BdkLocalOutput ) -> Self {
204
242
LocalOutput {
205
243
outpoint : OutPoint {
206
- txid : local_utxo. outpoint . txid . to_string ( ) ,
244
+ txid : Arc :: new ( Txid ( local_utxo. outpoint . txid ) ) ,
207
245
vout : local_utxo. outpoint . vout ,
208
246
} ,
209
247
txout : TxOut {
@@ -711,3 +749,150 @@ pub struct UnconfirmedTx {
711
749
pub tx : Arc < Transaction > ,
712
750
pub last_seen : u64 ,
713
751
}
752
+
753
+ /// Mapping of descriptors to their last revealed index.
754
+ #[ derive( Debug , uniffi:: Record ) ]
755
+ pub struct IndexerChangeSet {
756
+ pub last_revealed : HashMap < Arc < DescriptorId > , u32 > ,
757
+ }
758
+
759
+ impl From < bdk_wallet:: chain:: indexer:: keychain_txout:: ChangeSet > for IndexerChangeSet {
760
+ fn from ( mut value : bdk_wallet:: chain:: indexer:: keychain_txout:: ChangeSet ) -> Self {
761
+ let mut changes = HashMap :: new ( ) ;
762
+ for ( id, index) in core:: mem:: take ( & mut value. last_revealed ) {
763
+ changes. insert ( Arc :: new ( DescriptorId ( id. 0 ) ) , index) ;
764
+ }
765
+ Self {
766
+ last_revealed : changes,
767
+ }
768
+ }
769
+ }
770
+
771
+ impl From < IndexerChangeSet > for bdk_wallet:: chain:: indexer:: keychain_txout:: ChangeSet {
772
+ fn from ( mut value : IndexerChangeSet ) -> Self {
773
+ let mut changes = BTreeMap :: new ( ) ;
774
+ for ( id, index) in core:: mem:: take ( & mut value. last_revealed ) {
775
+ let descriptor_id = bdk_wallet:: chain:: DescriptorId ( id. 0 ) ;
776
+ changes. insert ( descriptor_id, index) ;
777
+ }
778
+ Self {
779
+ last_revealed : changes,
780
+ }
781
+ }
782
+ }
783
+
784
+ /// The hash added or removed at the given height.
785
+ #[ derive( Debug , uniffi:: Record ) ]
786
+ pub struct ChainChange {
787
+ /// Effected height
788
+ pub height : u32 ,
789
+ /// A hash was added or must be removed.
790
+ pub hash : Option < Arc < BlockHash > > ,
791
+ }
792
+
793
+ /// Changes to the local chain
794
+ #[ derive( Debug , uniffi:: Record ) ]
795
+ pub struct LocalChainChangeSet {
796
+ pub changes : Vec < ChainChange > ,
797
+ }
798
+
799
+ impl From < bdk_wallet:: chain:: local_chain:: ChangeSet > for LocalChainChangeSet {
800
+ fn from ( mut value : bdk_wallet:: chain:: local_chain:: ChangeSet ) -> Self {
801
+ let mut changes = Vec :: with_capacity ( value. blocks . len ( ) ) ;
802
+ for ( height, hash) in core:: mem:: take ( & mut value. blocks ) {
803
+ let hash = hash. map ( |h| Arc :: new ( BlockHash ( h) ) ) ;
804
+ let change = ChainChange { height, hash } ;
805
+ changes. push ( change) ;
806
+ }
807
+ Self { changes }
808
+ }
809
+ }
810
+
811
+ impl From < LocalChainChangeSet > for bdk_wallet:: chain:: local_chain:: ChangeSet {
812
+ fn from ( mut value : LocalChainChangeSet ) -> Self {
813
+ let mut changes = BTreeMap :: new ( ) ;
814
+ for change in core:: mem:: take ( & mut value. changes ) {
815
+ let height = change. height ;
816
+ let hash = change. hash . map ( |h| h. 0 ) ;
817
+ changes. insert ( height, hash) ;
818
+ }
819
+ Self { blocks : changes }
820
+ }
821
+ }
822
+
823
+ #[ derive( Debug , uniffi:: Record ) ]
824
+ pub struct Anchor {
825
+ pub confirmation_block_time : ConfirmationBlockTime ,
826
+ pub txid : Arc < Txid > ,
827
+ }
828
+
829
+ #[ derive( Debug , uniffi:: Record ) ]
830
+ pub struct TxGraphChangeSet {
831
+ pub txs : Vec < Arc < Transaction > > ,
832
+ pub txouts : HashMap < Arc < HashableOutPoint > , TxOut > ,
833
+ pub anchors : Vec < Anchor > ,
834
+ pub last_seen : HashMap < Arc < Txid > , u64 > ,
835
+ }
836
+
837
+ impl From < bdk_wallet:: chain:: tx_graph:: ChangeSet < BdkConfirmationBlockTime > > for TxGraphChangeSet {
838
+ fn from ( mut value : bdk_wallet:: chain:: tx_graph:: ChangeSet < BdkConfirmationBlockTime > ) -> Self {
839
+ let btree_txs = core:: mem:: take ( & mut value. txs ) ;
840
+ let txs = btree_txs
841
+ . into_iter ( )
842
+ . map ( |tx| Arc :: new ( tx. as_ref ( ) . into ( ) ) )
843
+ . collect :: < Vec < Arc < Transaction > > > ( ) ;
844
+ let mut txouts = HashMap :: new ( ) ;
845
+ for ( outpoint, txout) in core:: mem:: take ( & mut value. txouts ) {
846
+ txouts. insert ( Arc :: new ( HashableOutPoint ( outpoint. into ( ) ) ) , txout. into ( ) ) ;
847
+ }
848
+ let mut anchors = Vec :: new ( ) ;
849
+ for anchor in core:: mem:: take ( & mut value. anchors ) {
850
+ let confirmation_block_time = anchor. 0 . into ( ) ;
851
+ let txid = Arc :: new ( Txid ( anchor. 1 ) ) ;
852
+ let anchor = Anchor {
853
+ confirmation_block_time,
854
+ txid,
855
+ } ;
856
+ anchors. push ( anchor) ;
857
+ }
858
+ let mut last_seens = HashMap :: new ( ) ;
859
+ for ( txid, time) in core:: mem:: take ( & mut value. last_seen ) {
860
+ last_seens. insert ( Arc :: new ( Txid ( txid) ) , time) ;
861
+ }
862
+ TxGraphChangeSet {
863
+ txs,
864
+ txouts,
865
+ anchors,
866
+ last_seen : last_seens,
867
+ }
868
+ }
869
+ }
870
+
871
+ impl From < TxGraphChangeSet > for bdk_wallet:: chain:: tx_graph:: ChangeSet < BdkConfirmationBlockTime > {
872
+ fn from ( mut value : TxGraphChangeSet ) -> Self {
873
+ let mut txs = BTreeSet :: new ( ) ;
874
+ for tx in core:: mem:: take ( & mut value. txs ) {
875
+ let tx = Arc :: new ( tx. as_ref ( ) . into ( ) ) ;
876
+ txs. insert ( tx) ;
877
+ }
878
+ let mut txouts = BTreeMap :: new ( ) ;
879
+ for txout in core:: mem:: take ( & mut value. txouts ) {
880
+ txouts. insert ( txout. 0 . outpoint ( ) . into ( ) , txout. 1 . into ( ) ) ;
881
+ }
882
+ let mut anchors = BTreeSet :: new ( ) ;
883
+ for anchor in core:: mem:: take ( & mut value. anchors ) {
884
+ let txid = anchor. txid . 0 ;
885
+ anchors. insert ( ( anchor. confirmation_block_time . into ( ) , txid) ) ;
886
+ }
887
+ let mut last_seen = BTreeMap :: new ( ) ;
888
+ for ( txid, time) in core:: mem:: take ( & mut value. last_seen ) {
889
+ last_seen. insert ( txid. 0 , time) ;
890
+ }
891
+ Self {
892
+ txs,
893
+ txouts,
894
+ anchors,
895
+ last_seen,
896
+ }
897
+ }
898
+ }
0 commit comments