return account, nil
}
+func (m *Manager) UpdateAccountAlias(accountID string, newAlias string) (err error) {
+ m.accountMu.Lock()
+ defer m.accountMu.Unlock()
+
+ account, err := m.FindByID(accountID)
+ if err != nil {
+ return err
+ }
+ oldAlias := account.Alias
+
+ normalizedAlias := strings.ToLower(strings.TrimSpace(newAlias))
+ if existed := m.db.Get(aliasKey(normalizedAlias)); existed != nil {
+ return ErrDuplicateAlias
+ }
+
+ m.cacheMu.Lock()
+ m.aliasCache.Remove(oldAlias)
+ m.cacheMu.Unlock()
+
+ account.Alias = normalizedAlias
+ rawAccount, err := json.Marshal(account)
+ if err != nil {
+ return ErrMarshalAccount
+ }
+
+ storeBatch := m.db.NewBatch()
+ storeBatch.Delete(aliasKey(oldAlias))
+ storeBatch.Set(Key(accountID), rawAccount)
+ storeBatch.Set(aliasKey(normalizedAlias), []byte(accountID))
+ storeBatch.Write()
+ return nil
+}
+
// CreateAddress generate an address for the select account
func (m *Manager) CreateAddress(accountID string, change bool) (cp *CtrlProgram, err error) {
account, err := m.FindByID(accountID)
}
}
+func TestUpdateAccountAlias(t *testing.T) {
+ oldAlias := "test-alias"
+ newAlias := "my-alias"
+
+ m := mockAccountManager(t)
+ account := m.createTestAccount(t, oldAlias, nil)
+ if err := m.UpdateAccountAlias("testID", newAlias); err == nil {
+ t.Fatal("expected error when using an invalid account id")
+ }
+
+ err := m.UpdateAccountAlias(account.ID, oldAlias)
+ if errors.Root(err) != ErrDuplicateAlias {
+ t.Errorf("expected %s when using a duplicate alias, got %v", ErrDuplicateAlias, err)
+ }
+
+ if err := m.UpdateAccountAlias(account.ID, newAlias); err != nil {
+ t.Errorf("expected account %v alias should be update", account)
+ }
+
+ updatedAccount, err := m.FindByID(account.ID)
+ if err != nil {
+ t.Errorf("unexpected error %v", err)
+ }
+
+ if updatedAccount.Alias != newAlias {
+ t.Fatalf("alias:\ngot: %v\nwant: %v", updatedAccount.Alias, newAlias)
+ }
+
+ if _, err = m.FindByAlias(oldAlias); errors.Root(err) != ErrFindAccount {
+ t.Errorf("expected %s when using a old alias, got %v", ErrFindAccount, err)
+ }
+}
+
func TestDeleteAccount(t *testing.T) {
m := mockAccountManager(t)
if !reflect.DeepEqual(GetAccountIndexKey(xpubs1), GetAccountIndexKey(xpubs2)) {
t.Fatal("GetAccountIndexKey test err")
}
-
+
if reflect.DeepEqual(xpubs1, xpubs2) {
t.Fatal("GetAccountIndexKey test err")
}
return NewSuccessResponse(annotatedAccount)
}
+// POST update-account-alias
+func (a *API) updateAccountAlias(ctx context.Context, ins struct {
+ AccountID string `json:"account_id"`
+ AccountAlias string `json:"account_alias"`
+ NewAlias string `json:"new_alias"`
+}) Response {
+ accountID := ins.AccountID
+ if ins.AccountAlias != "" {
+ foundAccount, err := a.wallet.AccountMgr.FindByAlias(ins.AccountAlias)
+ if err != nil {
+ return NewErrorResponse(err)
+ }
+ accountID = foundAccount.ID
+ }
+ if err := a.wallet.AccountMgr.UpdateAccountAlias(accountID, ins.NewAlias); err != nil {
+ return NewErrorResponse(err)
+ }
+ return NewSuccessResponse(nil)
+}
+
// AccountInfo is request struct for deleteAccount
type AccountInfo struct {
Info string `json:"account_info"`
walletEnable = true
m.Handle("/create-account", jsonHandler(a.createAccount))
+ m.Handle("/update-account-alias", jsonHandler(a.updateAccountAlias))
m.Handle("/list-accounts", jsonHandler(a.listAccounts))
m.Handle("/delete-account", jsonHandler(a.deleteAccount))
createAccountCmd.PersistentFlags().IntVarP(&accountQuorum, "quorom", "q", 1, "quorum must be greater than 0 and less than or equal to the number of signers")
createAccountCmd.PersistentFlags().StringVarP(&accountToken, "access", "a", "", "access token")
+ updateAccountAliasCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
+ updateAccountAliasCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
+
listAccountsCmd.PersistentFlags().StringVar(&accountID, "id", "", "account ID")
listAccountsCmd.PersistentFlags().StringVar(&accountAlias, "alias", "", "account alias")
},
}
+var updateAccountAliasCmd = &cobra.Command{
+ Use: "update-account-alias <newAlias>",
+ Short: "update account alias",
+ Args: cobra.ExactArgs(1),
+ Run: func(cmd *cobra.Command, args []string) {
+ var ins = struct {
+ AccountID string `json:"account_id"`
+ AccountAlias string `json:"account_alias"`
+ NewAlias string `json:"new_alias"`
+ }{AccountID: accountID, AccountAlias: accountAlias, NewAlias: args[0]}
+
+ _, exitCode := util.ClientCall("/update-account-alias", &ins)
+ if exitCode != util.Success {
+ os.Exit(exitCode)
+ }
+
+ jww.FEEDBACK.Println("Successfully update account alias")
+ },
+}
+
var listAccountsCmd = &cobra.Command{
Use: "list-accounts",
Short: "List the existing accounts",
BytomcliCmd.AddCommand(createAccountCmd)
BytomcliCmd.AddCommand(deleteAccountCmd)
BytomcliCmd.AddCommand(listAccountsCmd)
+ BytomcliCmd.AddCommand(updateAccountAliasCmd)
BytomcliCmd.AddCommand(createAccountReceiverCmd)
BytomcliCmd.AddCommand(listAddressesCmd)
BytomcliCmd.AddCommand(validateAddressCmd)
createAccountCmd.Name(),
listAccountsCmd.Name(),
deleteAccountCmd.Name(),
+ updateAccountAliasCmd.Name(),
createAccountReceiverCmd.Name(),
listAddressesCmd.Name(),
validateAddressCmd.Name(),