@@ -20,14 +20,39 @@ pub struct Account {
2020 pub btc_pending_verify_list : HashSet < String > ,
2121}
2222
23+ /// Old Account format (v0.7.5 and earlier) with single pending sign id.
24+ #[ near( serializers = [ borsh] ) ]
25+ #[ derive( Clone ) ]
26+ pub struct AccountV0 {
27+ pub account_id : AccountId ,
28+ pub btc_pending_sign_id : Option < String > ,
29+ pub btc_pending_verify_list : HashSet < String > ,
30+ }
31+
32+ impl From < AccountV0 > for Account {
33+ fn from ( v : AccountV0 ) -> Self {
34+ let mut btc_pending_sign_ids = HashSet :: new ( ) ;
35+ if let Some ( id) = v. btc_pending_sign_id {
36+ btc_pending_sign_ids. insert ( id) ;
37+ }
38+ Self {
39+ account_id : v. account_id ,
40+ btc_pending_sign_ids,
41+ btc_pending_verify_list : v. btc_pending_verify_list ,
42+ }
43+ }
44+ }
45+
2346#[ near( serializers = [ borsh] ) ]
2447pub enum VAccount {
48+ V0 ( AccountV0 ) ,
2549 Current ( Account ) ,
2650}
2751
2852impl From < VAccount > for Account {
2953 fn from ( v : VAccount ) -> Self {
3054 match v {
55+ VAccount :: V0 ( c) => c. into ( ) ,
3156 VAccount :: Current ( c) => c,
3257 }
3358 }
@@ -36,23 +61,22 @@ impl From<VAccount> for Account {
3661impl From < & VAccount > for Account {
3762 fn from ( v : & VAccount ) -> Self {
3863 match v {
64+ VAccount :: V0 ( c) => c. clone ( ) . into ( ) ,
3965 VAccount :: Current ( c) => c. clone ( ) ,
4066 }
4167 }
4268}
4369
4470impl < ' a > From < & ' a mut VAccount > for & ' a mut Account {
4571 fn from ( v : & ' a mut VAccount ) -> Self {
46- match v {
47- VAccount :: Current ( c) => c,
72+ // Lazy migrate V0 -> Current on first mutable access
73+ if let VAccount :: V0 ( old) = v {
74+ let migrated: Account = old. clone ( ) . into ( ) ;
75+ * v = VAccount :: Current ( migrated) ;
4876 }
49- }
50- }
51-
52- impl < ' a > From < & ' a VAccount > for & ' a Account {
53- fn from ( v : & ' a VAccount ) -> Self {
5477 match v {
5578 VAccount :: Current ( c) => c,
79+ _ => unreachable ! ( ) ,
5680 }
5781 }
5882}
@@ -78,15 +102,12 @@ impl Contract {
78102 self . data ( ) . accounts . contains_key ( account_id)
79103 }
80104
81- pub fn internal_get_account ( & self , account_id : & AccountId ) -> Option < & Account > {
82- self . data ( ) . accounts . get ( account_id) . map ( Into :: into )
105+ pub fn internal_get_account ( & self , account_id : & AccountId ) -> Option < Account > {
106+ self . data ( ) . accounts . get ( account_id) . map ( Account :: from )
83107 }
84108
85- pub fn internal_unwrap_account ( & self , account_id : & AccountId ) -> & Account {
86- self . data ( )
87- . accounts
88- . get ( account_id)
89- . map ( |o| o. into ( ) )
109+ pub fn internal_unwrap_account ( & self , account_id : & AccountId ) -> Account {
110+ self . internal_get_account ( account_id)
90111 . unwrap_or_else ( || {
91112 env:: panic_str ( & format ! ( "ERR_ACCOUNT_NOT_REGISTERED: {}" , account_id) )
92113 } )
0 commit comments