@@ -22,7 +22,7 @@ use zeroize::Zeroizing;
22
22
use super :: alias:: { self , Alias } ;
23
23
use super :: derivation_path:: DerivationPath ;
24
24
use super :: pre_genesis;
25
- use crate :: { StoredKeypair , WalletIo } ;
25
+ use crate :: { StoreSpendingKey , StoredKeypair , WalletIo } ;
26
26
27
27
/// Actions that can be taken when there is an alias conflict
28
28
pub enum ConfirmationResponse {
@@ -67,7 +67,7 @@ pub struct Store {
67
67
/// Known viewing keys
68
68
view_keys : BTreeMap < Alias , ExtendedViewingKey > ,
69
69
/// Known spending keys
70
- spend_keys : BTreeMap < Alias , StoredKeypair < ExtendedSpendingKey > > ,
70
+ spend_keys : BTreeMap < Alias , StoredKeypair < StoreSpendingKey > > ,
71
71
/// Payment address book
72
72
payment_addrs : BiBTreeMap < Alias , PaymentAddress > ,
73
73
/// Cryptographic keypairs
@@ -136,7 +136,7 @@ impl Store {
136
136
pub fn find_spending_key (
137
137
& self ,
138
138
alias : impl AsRef < str > ,
139
- ) -> Option < & StoredKeypair < ExtendedSpendingKey > > {
139
+ ) -> Option < & StoredKeypair < StoreSpendingKey > > {
140
140
self . spend_keys . get ( & alias. into ( ) )
141
141
}
142
142
@@ -283,7 +283,7 @@ impl Store {
283
283
/// Get all known spending keys by their alias.
284
284
pub fn get_spending_keys (
285
285
& self ,
286
- ) -> & BTreeMap < Alias , StoredKeypair < ExtendedSpendingKey > > {
286
+ ) -> & BTreeMap < Alias , StoredKeypair < StoreSpendingKey > > {
287
287
& self . spend_keys
288
288
}
289
289
@@ -422,7 +422,7 @@ impl Store {
422
422
self . remove_alias ( & alias) ;
423
423
424
424
let ( spendkey_to_store, _raw_spendkey) =
425
- StoredKeypair :: new ( spendkey, password) ;
425
+ StoredKeypair :: new ( spendkey. into ( ) , password) ;
426
426
self . spend_keys . insert ( alias. clone ( ) , spendkey_to_store) ;
427
427
// Simultaneously add the derived viewing key to ease balance viewing
428
428
birthday. map ( |x| self . birthdays . insert ( alias. clone ( ) , x) ) ;
@@ -735,7 +735,13 @@ impl Store {
735
735
736
736
/// Decode a Store from the given bytes
737
737
pub fn decode ( data : Vec < u8 > ) -> Result < Self , toml:: de:: Error > {
738
- toml:: from_slice ( & data)
738
+ // First try to decode Store from current version (with separate
739
+ // birthdays)
740
+ toml:: from_slice ( & data) . or_else (
741
+ // Otherwise try to decode Store from older version (with
742
+ // integrated birthdays)
743
+ |_| toml:: from_slice :: < StoreV0 > ( & data) . map ( Into :: into) ,
744
+ )
739
745
}
740
746
741
747
/// Encode a store into a string of bytes
@@ -835,6 +841,77 @@ impl<'de> Deserialize<'de> for AddressVpType {
835
841
}
836
842
}
837
843
844
+ // A Storage area for keys and addresses. This is a deprecated format but it
845
+ // is required for compatability purposes.
846
+ #[ derive( Serialize , Deserialize , Debug , Default ) ]
847
+ pub struct StoreV0 {
848
+ /// Known viewing keys
849
+ view_keys : BTreeMap < Alias , crate :: DatedViewingKey > ,
850
+ /// Known spending keys
851
+ spend_keys : BTreeMap < Alias , StoredKeypair < crate :: DatedSpendingKey > > ,
852
+ /// Payment address book
853
+ payment_addrs : BiBTreeMap < Alias , PaymentAddress > ,
854
+ /// Cryptographic keypairs
855
+ secret_keys : BTreeMap < Alias , StoredKeypair < common:: SecretKey > > ,
856
+ /// Known public keys
857
+ public_keys : BTreeMap < Alias , common:: PublicKey > ,
858
+ /// Known derivation paths
859
+ derivation_paths : BTreeMap < Alias , DerivationPath > ,
860
+ /// Namada address book
861
+ addresses : BiBTreeMap < Alias , Address > ,
862
+ /// Known mappings of public key hashes to their aliases in the `keys`
863
+ /// field. Used for look-up by a public key.
864
+ pkhs : BTreeMap < PublicKeyHash , Alias > ,
865
+ /// Special keys if the wallet belongs to a validator
866
+ pub ( crate ) validator_data : Option < ValidatorData > ,
867
+ /// Namada address vp type
868
+ address_vp_types : BTreeMap < AddressVpType , HashSet < Address > > ,
869
+ }
870
+
871
+ impl From < StoreV0 > for Store {
872
+ fn from ( store : StoreV0 ) -> Self {
873
+ let mut to = Store {
874
+ payment_addrs : store. payment_addrs ,
875
+ secret_keys : store. secret_keys ,
876
+ public_keys : store. public_keys ,
877
+ derivation_paths : store. derivation_paths ,
878
+ addresses : store. addresses ,
879
+ pkhs : store. pkhs ,
880
+ validator_data : store. validator_data ,
881
+ address_vp_types : store. address_vp_types ,
882
+ ..Store :: default ( )
883
+ } ;
884
+ for ( alias, key) in store. view_keys {
885
+ // Extract the birthday into the birthdays map
886
+ to. birthdays . insert ( alias. clone ( ) , key. birthday ) ;
887
+ // Extrat the key into the viewing keys map
888
+ to. view_keys . insert ( alias, key. key ) ;
889
+ }
890
+ for ( alias, key) in store. spend_keys {
891
+ match key {
892
+ StoredKeypair :: Raw ( key) => {
893
+ // Extract the birthday into the birthdays map
894
+ to. birthdays . insert ( alias. clone ( ) , key. birthday ) ;
895
+ // Extract the key into the spending keys map
896
+ to. spend_keys
897
+ . insert ( alias, StoredKeypair :: Raw ( key. key . into ( ) ) ) ;
898
+ }
899
+ StoredKeypair :: Encrypted ( key) => {
900
+ // This map is fine because DatedSpendingKey has the same
901
+ // Borsh serialization as StoreSpendingKey
902
+ to. spend_keys . insert (
903
+ alias,
904
+ StoredKeypair :: Encrypted ( key. map :: < StoreSpendingKey > ( ) ) ,
905
+ ) ;
906
+ // Here we assume the birthday for the current alias is
907
+ // already given in a viewing key with the same alias.
908
+ }
909
+ }
910
+ }
911
+ to
912
+ }
913
+ }
914
+
838
915
#[ cfg( test) ]
839
916
mod test_wallet {
840
917
use base58:: FromBase58 ;
0 commit comments