22
22
23
23
pub mod weights;
24
24
25
+ pub use pallet:: * ;
26
+
25
27
use crate :: weights:: WeightInfo ;
26
28
use np_crypto:: ecdsa:: EcdsaExt ;
27
- use np_runtime:: AccountName ;
28
- pub use pallet:: * ;
29
29
use parity_scale_codec:: { Decode , Encode , MaxEncodedLen } ;
30
30
use scale_info:: TypeInfo ;
31
- use sp_runtime:: {
32
- traits:: { LookupError , StaticLookup } ,
33
- DispatchError , MultiAddress ,
34
- } ;
35
- use sp_std:: prelude:: * ;
36
-
37
- type AccountIdLookupOf < T > = <<T as frame_system:: Config >:: Lookup as StaticLookup >:: Source ;
38
-
39
- /// A generator for tag number that discriminates the same name accounts.
40
- pub trait TagGenerator < T : Config > {
41
- fn tag ( id : & T :: AccountId , name : & str ) -> Result < u16 , ( ) > ;
42
- }
31
+ use sp_runtime:: { BoundedBTreeSet , DispatchError } ;
32
+ use sp_std:: { collections:: btree_set:: BTreeSet , prelude:: * } ;
43
33
44
34
#[ cfg_attr( feature = "std" , derive( Hash ) ) ]
45
35
#[ derive( Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Encode , Decode , MaxEncodedLen , TypeInfo ) ]
46
36
pub enum AccountAlias {
47
- AccountName ( AccountName ) ,
48
37
EthereumAddress ( [ u8 ; 20 ] ) ,
49
38
CosmosAddress ( [ u8 ; 20 ] ) ,
50
39
}
51
40
52
41
#[ frame_support:: pallet]
53
42
pub mod pallet {
54
-
55
43
use super :: * ;
56
44
use frame_support:: pallet_prelude:: * ;
57
- use frame_system:: pallet_prelude:: * ;
58
45
59
46
/// The module's config trait.
60
47
#[ pallet:: config]
@@ -63,126 +50,14 @@ pub mod pallet {
63
50
type RuntimeEvent : From < Event < Self > > + IsType < <Self as frame_system:: Config >:: RuntimeEvent > ;
64
51
/// Weight information for extrinsics in this pallet.
65
52
type WeightInfo : WeightInfo ;
66
- /// The generator for tag number that discriminates the same name accounts.
67
- type TagGenerator : TagGenerator < Self > ;
68
53
}
69
54
70
55
#[ pallet:: pallet]
71
- pub struct Pallet < T > ( PhantomData < T > ) ;
72
-
73
- #[ pallet:: call]
74
- impl < T : Config > Pallet < T >
75
- where
76
- T :: AccountId : EcdsaExt ,
77
- {
78
- #[ pallet:: call_index( 0 ) ]
79
- #[ pallet:: weight( T :: WeightInfo :: create_account_name( ) ) ]
80
- pub fn create_account_name ( origin : OriginFor < T > , name : Vec < u8 > ) -> DispatchResult {
81
- let who = ensure_signed ( origin) ?;
82
- ensure ! ( AccountNameOf :: <T >:: get( & who) . is_none( ) , Error :: <T >:: AlreadyExists ) ;
83
- let name =
84
- sp_std:: str:: from_utf8 ( & name[ ..] ) . map_err ( |_| Error :: < T > :: InvalidNameFormat ) ?;
85
- let tag =
86
- T :: TagGenerator :: tag ( & who, name) . map_err ( |_| Error :: < T > :: TagGenerationFailed ) ?;
87
- let account_name =
88
- AccountName :: new ( & name, tag) . map_err ( |_| Error :: < T > :: InvalidNameFormat ) ?;
89
- AccountIdOf :: < T > :: try_mutate (
90
- AccountAlias :: AccountName ( account_name) ,
91
- |maybe_value| -> DispatchResult {
92
- ensure ! ( maybe_value. is_none( ) , Error :: <T >:: InUse ) ;
93
- * maybe_value = Some ( who. clone ( ) ) ;
94
- Ok ( ( ) )
95
- } ,
96
- ) ?;
97
- AccountNameOf :: < T > :: insert ( & who, account_name) ;
98
- Self :: deposit_event ( Event :: < T > :: AccountNameUpdated {
99
- who,
100
- name : account_name,
101
- deleted : None ,
102
- } ) ;
103
- Ok ( ( ) )
104
- }
105
-
106
- #[ pallet:: call_index( 1 ) ]
107
- #[ pallet:: weight( T :: WeightInfo :: update_account_name( ) ) ]
108
- pub fn update_account_name ( origin : OriginFor < T > , new_name : Vec < u8 > ) -> DispatchResult {
109
- let who = ensure_signed ( origin) ?;
110
- let account_name = AccountNameOf :: < T > :: get ( & who) . ok_or ( Error :: < T > :: NotExists ) ?;
111
- let new_name =
112
- sp_std:: str:: from_utf8 ( & new_name[ ..] ) . map_err ( |_| Error :: < T > :: InvalidNameFormat ) ?;
113
- let tag = T :: TagGenerator :: tag ( & who, new_name)
114
- . map_err ( |_| Error :: < T > :: TagGenerationFailed ) ?;
115
- let new_account_name =
116
- AccountName :: new ( & new_name, tag) . map_err ( |_| Error :: < T > :: InvalidNameFormat ) ?;
117
- AccountIdOf :: < T > :: try_mutate (
118
- AccountAlias :: AccountName ( new_account_name) ,
119
- |maybe_value| -> DispatchResult {
120
- ensure ! ( maybe_value. is_none( ) , Error :: <T >:: InUse ) ;
121
- * maybe_value = Some ( who. clone ( ) ) ;
122
- Ok ( ( ) )
123
- } ,
124
- ) ?;
125
- AccountIdOf :: < T > :: remove ( AccountAlias :: AccountName ( account_name) ) ;
126
- AccountNameOf :: < T > :: insert ( & who, new_account_name) ;
127
- Self :: deposit_event ( Event :: < T > :: AccountNameUpdated {
128
- who,
129
- name : new_account_name,
130
- deleted : Some ( account_name) ,
131
- } ) ;
132
- Ok ( ( ) )
133
- }
134
-
135
- #[ pallet:: call_index( 2 ) ]
136
- #[ pallet:: weight( T :: WeightInfo :: connect_aliases( ) ) ]
137
- pub fn connect_aliases ( origin : OriginFor < T > ) -> DispatchResult {
138
- let who = ensure_signed ( origin) ?;
139
- Self :: connect_aliases_secp256k1 ( & who) ?;
140
- Ok ( ( ) )
141
- }
142
-
143
- #[ pallet:: call_index( 3 ) ]
144
- #[ pallet:: weight( T :: WeightInfo :: force_set_account_name( ) ) ]
145
- pub fn force_set_account_name (
146
- origin : OriginFor < T > ,
147
- dest : AccountIdLookupOf < T > ,
148
- name : Vec < u8 > ,
149
- tag : u16 ,
150
- ) -> DispatchResult {
151
- ensure_root ( origin) ?;
152
- let dest = T :: Lookup :: lookup ( dest) ?;
153
- let name =
154
- sp_std:: str:: from_utf8 ( & name[ ..] ) . map_err ( |_| Error :: < T > :: InvalidNameFormat ) ?;
155
- let new_account_name =
156
- AccountName :: new ( & name, tag) . map_err ( |_| Error :: < T > :: InvalidNameFormat ) ?;
157
- AccountIdOf :: < T > :: try_mutate (
158
- AccountAlias :: AccountName ( new_account_name) ,
159
- |maybe_value| -> DispatchResult {
160
- ensure ! ( maybe_value. is_none( ) , Error :: <T >:: InUse ) ;
161
- * maybe_value = Some ( dest. clone ( ) ) ;
162
- Ok ( ( ) )
163
- } ,
164
- ) ?;
165
-
166
- let past_name = AccountNameOf :: < T > :: get ( & dest) ;
167
- match past_name {
168
- Some ( past_name) => AccountIdOf :: < T > :: remove ( AccountAlias :: AccountName ( past_name) ) ,
169
- None => ( ) ,
170
- } ;
171
- AccountNameOf :: < T > :: insert ( & dest, new_account_name) ;
172
- Self :: deposit_event ( Event :: < T > :: AccountNameUpdated {
173
- who : dest,
174
- name : new_account_name,
175
- deleted : past_name,
176
- } ) ;
177
- Ok ( ( ) )
178
- }
179
- }
56
+ pub struct Pallet < T > ( _ ) ;
180
57
181
58
#[ pallet:: event]
182
59
#[ pallet:: generate_deposit( pub ( super ) fn deposit_event) ]
183
60
pub enum Event < T : Config > {
184
- /// An account name was updated.
185
- AccountNameUpdated { who : T :: AccountId , name : AccountName , deleted : Option < AccountName > } ,
186
61
/// An ethereum address was published.
187
62
EthereumAddressPublished { who : T :: AccountId , address : [ u8 ; 20 ] } ,
188
63
/// An cosmos address was published.
@@ -191,83 +66,66 @@ pub mod pallet {
191
66
192
67
#[ pallet:: error]
193
68
pub enum Error < T > {
194
- /// The account name already exists.
195
- AlreadyExists ,
196
- /// The account name does not exists.
197
- NotExists ,
198
- /// The account name is not available.
199
- InUse ,
200
- /// Invalid name foramt.
201
- InvalidNameFormat ,
202
- /// Tag generation failed.
203
- TagGenerationFailed ,
204
69
/// Ethereum address conversion failed.
205
70
EthereumAddressConversionFailed ,
206
71
/// Cosmos address conversion failed.
207
72
CosmosAddressConversionFailed ,
208
73
}
209
74
210
75
#[ pallet:: storage]
76
+ #[ pallet:: getter( fn accountid) ]
211
77
pub type AccountIdOf < T : Config > = StorageMap < _ , Blake2_128Concat , AccountAlias , T :: AccountId > ;
78
+
212
79
#[ pallet:: storage]
213
- pub type AccountNameOf < T : Config > = StorageMap < _ , Blake2_128Concat , T :: AccountId , AccountName > ;
80
+ #[ pallet:: getter( fn aliases) ]
81
+ pub type AccountAliases < T : Config > =
82
+ StorageMap < _ , Blake2_128Concat , T :: AccountId , BoundedBTreeSet < AccountAlias , ConstU32 < 2 > > > ;
214
83
}
215
84
216
85
impl < T : Config > Pallet < T >
217
86
where
218
87
T :: AccountId : EcdsaExt ,
219
88
{
220
- // PUBLIC IMMUTABLES
221
-
222
89
/// Lookup an AccountAlias to get an Id, if exists.
223
90
pub fn lookup ( alias : & AccountAlias ) -> Option < T :: AccountId > {
224
91
AccountIdOf :: < T > :: get ( alias) . map ( |x| x)
225
92
}
226
93
227
- pub fn connect_aliases_secp256k1 ( who : & T :: AccountId ) -> Result < ( ) , DispatchError > {
228
- let ethereum_address = who
94
+ pub fn alias_secp256k1 ( who : & T :: AccountId ) -> Result < ( ) , DispatchError > {
95
+ let mut aliases = BTreeSet :: new ( ) ;
96
+ let eth = who
229
97
. to_eth_address ( )
230
98
. map ( |x| x. into ( ) )
231
99
. ok_or ( Error :: < T > :: EthereumAddressConversionFailed ) ?;
232
- if AccountIdOf :: < T > :: get ( AccountAlias :: EthereumAddress ( ethereum_address) ) . is_none ( ) {
233
- AccountIdOf :: < T > :: insert ( AccountAlias :: EthereumAddress ( ethereum_address) , who) ;
100
+ let eth_alias = AccountAlias :: EthereumAddress ( eth) ;
101
+ if AccountIdOf :: < T > :: get ( eth_alias) . is_none ( ) {
102
+ AccountIdOf :: < T > :: insert ( eth_alias, who. clone ( ) ) ;
103
+ aliases. insert ( eth_alias) ;
234
104
Self :: deposit_event ( Event :: < T > :: EthereumAddressPublished {
235
105
who : who. clone ( ) ,
236
- address : ethereum_address ,
106
+ address : eth ,
237
107
} ) ;
238
108
}
239
- let cosmos_address = who
109
+ let cosm = who
240
110
. to_cosm_address ( )
241
111
. map ( |x| x. into ( ) )
242
112
. ok_or ( Error :: < T > :: CosmosAddressConversionFailed ) ?;
243
- if AccountIdOf :: < T > :: get ( AccountAlias :: CosmosAddress ( cosmos_address) ) . is_none ( ) {
244
- AccountIdOf :: < T > :: insert ( AccountAlias :: CosmosAddress ( cosmos_address) , who) ;
113
+ let cosm_alias = AccountAlias :: CosmosAddress ( cosm) ;
114
+ if AccountIdOf :: < T > :: get ( cosm_alias) . is_none ( ) {
115
+ AccountIdOf :: < T > :: insert ( cosm_alias, who. clone ( ) ) ;
116
+ aliases. insert ( cosm_alias) ;
245
117
Self :: deposit_event ( Event :: < T > :: CosmosAddressPublished {
246
118
who : who. clone ( ) ,
247
- address : cosmos_address ,
119
+ address : cosm ,
248
120
} ) ;
249
121
}
250
- Ok ( ( ) )
251
- }
252
- }
253
-
254
- impl < T : Config > StaticLookup for Pallet < T >
255
- where
256
- T :: AccountId : EcdsaExt ,
257
- {
258
- type Source = MultiAddress < T :: AccountId , AccountName > ;
259
- type Target = T :: AccountId ;
260
-
261
- fn lookup ( a : Self :: Source ) -> Result < Self :: Target , LookupError > {
262
- match a {
263
- MultiAddress :: Id ( id) => Ok ( id) ,
264
- MultiAddress :: Index ( name) =>
265
- Self :: lookup ( & AccountAlias :: AccountName ( name) ) . ok_or ( LookupError ) ,
266
- _ => Err ( LookupError ) ,
122
+ if !aliases. is_empty ( ) {
123
+ AccountAliases :: < T > :: insert (
124
+ who,
125
+ BoundedBTreeSet :: try_from ( aliases)
126
+ . map_err ( |_| DispatchError :: Other ( "Too many aliases" ) ) ?,
127
+ ) ;
267
128
}
268
- }
269
-
270
- fn unlookup ( a : Self :: Target ) -> Self :: Source {
271
- MultiAddress :: Id ( a)
129
+ Ok ( ( ) )
272
130
}
273
131
}
0 commit comments