Improve SDK account creation
@@ -19,66 +19,54 @@ var Client = ljsonrpc.NewClient(config.Settings.GetString("Lbrynet"))
var logger = monitor.NewModuleLogger("lbrynet")
-// AccountNameFromUID returns uid formatted for internal use
-func AccountNameFromUID(uid string) string {
- if uid != "" {
- return fmt.Sprintf(accountNameTemplate, uid)
+// AccountNameFromUID formats the UID for use with SDK.
+// UID can be an email or an empty string, in which case a random identifier will be generated.
+func AccountNameFromUID(UID string) string {
+ if UID != "" {
+ return fmt.Sprintf(accountNameTemplate, UID)
}
return fmt.Sprintf(accountNameTemplate, crypto.RandString(32))
}
-// GetAccount finds account in account_list by uid
-func GetAccount(uid string) (*ljsonrpc.Account, error) {
+// GetAccount finds account in account_list by UID
+func GetAccount(UID string) (*ljsonrpc.Account, error) {
accounts, err := Client.AccountList()
if err != nil {
return nil, err
}
for _, account := range accounts.LBCMainnet {
accountUID := strings.TrimPrefix(account.Name, accountNamePrefix)
- if accountUID == uid {
+ if accountUID == UID {
return &account, nil
}
}
- return nil, AccountNotFound{Email: uid}
+ return nil, AccountNotFound{Email: UID}
}
-// AccountExists checks if account exists at the local SDK instance.
-// In case of any errors apart from AccountNotFound we like want to break the flow of the caller and return true.
-func AccountExists(uid string) bool {
- _, err := GetAccount(uid)
- if err != nil {
- switch err.(type) {
- case AccountNotFound:
- return false
- default:
- return true
- }
- }
- return true
-}
-
-// CreateAccount creates a new account with the SDK
-func CreateAccount(uid string) (*ljsonrpc.AccountCreateResponse, error) {
- accountName := AccountNameFromUID(uid)
- if uid != "" && AccountExists(uid) {
- logger.Log().Errorf("uid %v is registered with the daemon", uid)
- return nil, AccountConflict{Email: uid}
+// CreateAccount creates a new account with the SDK.
+// Will return an error if account with this UID already exists.
+func CreateAccount(UID string) (*ljsonrpc.AccountCreateResponse, error) {
+ accountName := AccountNameFromUID(UID)
+ account, err := GetAccount(UID)
+ if err == nil {
+ logger.LogF(monitor.F{"uid": UID, "account_id": account.ID}).Error("account is already registered with lbrynet")
+ return nil, AccountConflict{Email: UID}
}
- logger.Log().Infof("creating account %v", uid)
r, err := Client.AccountCreate(accountName, false)
if err != nil {
return nil, err
}
+ logger.LogF(monitor.F{"uid": UID, "account_id": r.ID}).Infof("registered a new account with lbrynet: %v", r)
return r, nil
}
// RemoveAccount removes an account from the SDK by uid
-func RemoveAccount(uid string) (*ljsonrpc.AccountRemoveResponse, error) {
- acc, err := GetAccount(uid)
+func RemoveAccount(UID string) (*ljsonrpc.AccountRemoveResponse, error) {
+ acc, err := GetAccount(UID)
if err != nil {
return nil, err
}
- logger.Log().Infof("removing account %v", uid)
+ logger.Log().Infof("removing account %v", UID)
r, err := Client.AccountRemove(acc.ID)
if err != nil {
return nil, err
