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 , Clone , 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 , Clone , 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 {
@@ -714,3 +752,150 @@ pub struct UnconfirmedTx {
714
752
pub tx : Arc < Transaction > ,
715
753
pub last_seen : u64 ,
716
754
}
755
+
756
+ /// Mapping of descriptors to their last revealed index.
757
+ #[ derive( Debug , Clone , uniffi:: Record ) ]
758
+ pub struct IndexerChangeSet {
759
+ pub last_revealed : HashMap < Arc < DescriptorId > , u32 > ,
760
+ }
761
+
762
+ impl From < bdk_wallet:: chain:: indexer:: keychain_txout:: ChangeSet > for IndexerChangeSet {
763
+ fn from ( mut value : bdk_wallet:: chain:: indexer:: keychain_txout:: ChangeSet ) -> Self {
764
+ let mut changes = HashMap :: new ( ) ;
765
+ for ( id, index) in core:: mem:: take ( & mut value. last_revealed ) {
766
+ changes. insert ( Arc :: new ( DescriptorId ( id. 0 ) ) , index) ;
767
+ }
768
+ Self {
769
+ last_revealed : changes,
770
+ }
771
+ }
772
+ }
773
+
774
+ impl From < IndexerChangeSet > for bdk_wallet:: chain:: indexer:: keychain_txout:: ChangeSet {
775
+ fn from ( mut value : IndexerChangeSet ) -> Self {
776
+ let mut changes = BTreeMap :: new ( ) ;
777
+ for ( id, index) in core:: mem:: take ( & mut value. last_revealed ) {
778
+ let descriptor_id = bdk_wallet:: chain:: DescriptorId ( id. 0 ) ;
779
+ changes. insert ( descriptor_id, index) ;
780
+ }
781
+ Self {
782
+ last_revealed : changes,
783
+ }
784
+ }
785
+ }
786
+
787
+ /// The hash added or removed at the given height.
788
+ #[ derive( Debug , Clone , uniffi:: Record ) ]
789
+ pub struct ChainChange {
790
+ /// Effected height
791
+ pub height : u32 ,
792
+ /// A hash was added or must be removed.
793
+ pub hash : Option < Arc < BlockHash > > ,
794
+ }
795
+
796
+ /// Changes to the local chain
797
+ #[ derive( Debug , Clone , uniffi:: Record ) ]
798
+ pub struct LocalChainChangeSet {
799
+ pub changes : Vec < ChainChange > ,
800
+ }
801
+
802
+ impl From < bdk_wallet:: chain:: local_chain:: ChangeSet > for LocalChainChangeSet {
803
+ fn from ( mut value : bdk_wallet:: chain:: local_chain:: ChangeSet ) -> Self {
804
+ let mut changes = Vec :: with_capacity ( value. blocks . len ( ) ) ;
805
+ for ( height, hash) in core:: mem:: take ( & mut value. blocks ) {
806
+ let hash = hash. map ( |h| Arc :: new ( BlockHash ( h) ) ) ;
807
+ let change = ChainChange { height, hash } ;
808
+ changes. push ( change) ;
809
+ }
810
+ Self { changes }
811
+ }
812
+ }
813
+
814
+ impl From < LocalChainChangeSet > for bdk_wallet:: chain:: local_chain:: ChangeSet {
815
+ fn from ( mut value : LocalChainChangeSet ) -> Self {
816
+ let mut changes = BTreeMap :: new ( ) ;
817
+ for change in core:: mem:: take ( & mut value. changes ) {
818
+ let height = change. height ;
819
+ let hash = change. hash . map ( |h| h. 0 ) ;
820
+ changes. insert ( height, hash) ;
821
+ }
822
+ Self { blocks : changes }
823
+ }
824
+ }
825
+
826
+ #[ derive( Debug , Clone , uniffi:: Record ) ]
827
+ pub struct Anchor {
828
+ pub confirmation_block_time : ConfirmationBlockTime ,
829
+ pub txid : Arc < Txid > ,
830
+ }
831
+
832
+ #[ derive( Debug , Clone , uniffi:: Record ) ]
833
+ pub struct TxGraphChangeSet {
834
+ pub txs : Vec < Arc < Transaction > > ,
835
+ pub txouts : HashMap < Arc < HashableOutPoint > , TxOut > ,
836
+ pub anchors : Vec < Anchor > ,
837
+ pub last_seen : HashMap < Arc < Txid > , u64 > ,
838
+ }
839
+
840
+ impl From < bdk_wallet:: chain:: tx_graph:: ChangeSet < BdkConfirmationBlockTime > > for TxGraphChangeSet {
841
+ fn from ( mut value : bdk_wallet:: chain:: tx_graph:: ChangeSet < BdkConfirmationBlockTime > ) -> Self {
842
+ let btree_txs = core:: mem:: take ( & mut value. txs ) ;
843
+ let txs = btree_txs
844
+ . into_iter ( )
845
+ . map ( |tx| Arc :: new ( tx. as_ref ( ) . into ( ) ) )
846
+ . collect :: < Vec < Arc < Transaction > > > ( ) ;
847
+ let mut txouts = HashMap :: new ( ) ;
848
+ for ( outpoint, txout) in core:: mem:: take ( & mut value. txouts ) {
849
+ txouts. insert ( Arc :: new ( HashableOutPoint ( outpoint. into ( ) ) ) , txout. into ( ) ) ;
850
+ }
851
+ let mut anchors = Vec :: new ( ) ;
852
+ for anchor in core:: mem:: take ( & mut value. anchors ) {
853
+ let confirmation_block_time = anchor. 0 . into ( ) ;
854
+ let txid = Arc :: new ( Txid ( anchor. 1 ) ) ;
855
+ let anchor = Anchor {
856
+ confirmation_block_time,
857
+ txid,
858
+ } ;
859
+ anchors. push ( anchor) ;
860
+ }
861
+ let mut last_seens = HashMap :: new ( ) ;
862
+ for ( txid, time) in core:: mem:: take ( & mut value. last_seen ) {
863
+ last_seens. insert ( Arc :: new ( Txid ( txid) ) , time) ;
864
+ }
865
+ TxGraphChangeSet {
866
+ txs,
867
+ txouts,
868
+ anchors,
869
+ last_seen : last_seens,
870
+ }
871
+ }
872
+ }
873
+
874
+ impl From < TxGraphChangeSet > for bdk_wallet:: chain:: tx_graph:: ChangeSet < BdkConfirmationBlockTime > {
875
+ fn from ( mut value : TxGraphChangeSet ) -> Self {
876
+ let mut txs = BTreeSet :: new ( ) ;
877
+ for tx in core:: mem:: take ( & mut value. txs ) {
878
+ let tx = Arc :: new ( tx. as_ref ( ) . into ( ) ) ;
879
+ txs. insert ( tx) ;
880
+ }
881
+ let mut txouts = BTreeMap :: new ( ) ;
882
+ for txout in core:: mem:: take ( & mut value. txouts ) {
883
+ txouts. insert ( txout. 0 . outpoint ( ) . into ( ) , txout. 1 . into ( ) ) ;
884
+ }
885
+ let mut anchors = BTreeSet :: new ( ) ;
886
+ for anchor in core:: mem:: take ( & mut value. anchors ) {
887
+ let txid = anchor. txid . 0 ;
888
+ anchors. insert ( ( anchor. confirmation_block_time . into ( ) , txid) ) ;
889
+ }
890
+ let mut last_seen = BTreeMap :: new ( ) ;
891
+ for ( txid, time) in core:: mem:: take ( & mut value. last_seen ) {
892
+ last_seen. insert ( txid. 0 , time) ;
893
+ }
894
+ Self {
895
+ txs,
896
+ txouts,
897
+ anchors,
898
+ last_seen,
899
+ }
900
+ }
901
+ }
0 commit comments