1
1
import { authenticatedMethod , RpcMethod } from './../../../../jsonRpc' ;
2
- import { isEmpty } from 'lodash' ;
2
+ import { isEmpty , isString } from 'lodash' ;
3
+
3
4
/**
4
5
* Delete an account from the wallet
5
6
*/
6
- class DeleteAccount extends RpcMethod
7
- {
7
+ class DeleteAccount extends RpcMethod {
8
8
constructor ( name , oWallet ) {
9
9
super ( name ) ;
10
10
this . _oWallet = oWallet ;
@@ -16,21 +16,30 @@ class DeleteAccount extends RpcMethod
16
16
17
17
let oAddress = this . _oWallet . getAddress ( sAddress ) ;
18
18
19
- if ( oAddress === null )
20
- {
19
+ if ( oAddress === null ) {
21
20
throw new Error ( 'Account not found.' ) ;
22
21
}
23
22
24
- if ( await this . _oWallet . isAddressEncrypted ( oAddress ) && isEmpty ( sPassword ) )
25
- {
23
+ const bAddressIsEncrypted = await this . _oWallet . isAddressEncrypted ( oAddress ) ;
24
+
25
+ if ( bAddressIsEncrypted && ( isEmpty ( sPassword ) || isString ( sPassword ) === false ) ) {
26
26
throw new Error ( 'Account is encrypted and a password was not provided. (Password must be provided as the second parameter).' ) ;
27
27
}
28
28
29
- let oDeleteResult = await this . _oWallet . deleteAddress ( oAddress , false , sPassword ) ;
29
+ let oDeleteResult = await this . _oWallet . deleteAddress ( oAddress , false , bAddressIsEncrypted ? sPassword . split ( ' ' ) : null ) ;
30
+
31
+ if ( oDeleteResult . result === false ) {
32
+ if ( bAddressIsEncrypted ) {
33
+ // try also with the password as non-array
34
+ oDeleteResult = await this . _oWallet . deleteAddress ( oAddress , false , sPassword ) ;
30
35
31
- if ( oDeleteResult . result === false )
32
- {
33
- throw new Error ( `Unable to delete the account. Reason: ${ oDeleteResult . message } ` ) ;
36
+ if ( oDeleteResult . result === false ) {
37
+ throw new Error ( `Unable to delete the account. Reason: ${ oDeleteResult . message } ` ) ;
38
+ }
39
+ }
40
+ else {
41
+ throw new Error ( `Unable to delete the account. Reason: ${ oDeleteResult . message } ` ) ;
42
+ }
34
43
}
35
44
36
45
return true ;
0 commit comments