Skip to content

Commit 4de2547

Browse files
authored
rethink some of the wallet RPCs to be more direct (keybase#18460)
* update change currency to plumb new currency straight through * update account name change to plumb updated account straight through * update account default setting rpc to plumb updates accounts straight through * remove unnecessary ts-ignore * fix incorrect type and remove ts-ignore * update wallet actions for changing mobile-only to assume correctness
1 parent 0bd76f6 commit 4de2547

14 files changed

Lines changed: 123 additions & 83 deletions

File tree

‎go/client/cmd_wallet_rename.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func (c *cmdWalletRename) Run() (err error) {
5252
if err != nil {
5353
return err
5454
}
55-
err = cli.ChangeWalletAccountNameLocal(context.TODO(), stellar1.ChangeWalletAccountNameLocalArg{
55+
_, err = cli.ChangeWalletAccountNameLocal(context.TODO(), stellar1.ChangeWalletAccountNameLocalArg{
5656
AccountID: c.AccountID,
5757
NewName: c.Name,
5858
})

‎go/client/cmd_wallet_set_primary.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (c *cmdWalletSetPrimary) Run() (err error) {
4848
AccountID: stellar1.AccountID(c.accountID),
4949
}
5050

51-
if err := cli.SetWalletAccountAsDefaultLocal(context.Background(), arg); err != nil {
51+
if _, err := cli.SetWalletAccountAsDefaultLocal(context.Background(), arg); err != nil {
5252
return err
5353
}
5454

‎go/protocol/stellar1/local.go‎

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go/stellar/stellarsvc/frontend.go‎

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -517,42 +517,50 @@ func (s *Server) ValidateAccountNameLocal(ctx context.Context, arg stellar1.Vali
517517
return nil
518518
}
519519

520-
func (s *Server) ChangeWalletAccountNameLocal(ctx context.Context, arg stellar1.ChangeWalletAccountNameLocalArg) (err error) {
520+
func (s *Server) ChangeWalletAccountNameLocal(ctx context.Context, arg stellar1.ChangeWalletAccountNameLocalArg) (acct stellar1.WalletAccountLocal, err error) {
521521
mctx, fin, err := s.Preamble(ctx, preambleArg{
522522
RPCName: "ChangeWalletAccountNameLocal",
523523
Err: &err,
524524
RequireWallet: true,
525525
})
526526
defer fin()
527527
if err != nil {
528-
return err
528+
return acct, err
529529
}
530530

531531
if arg.AccountID.IsNil() {
532532
mctx.Debug("ChangeWalletAccountNameLocal called with an empty account id")
533-
return ErrAccountIDMissing
533+
return acct, ErrAccountIDMissing
534534
}
535535

536-
return stellar.ChangeAccountName(mctx, s.walletState, arg.AccountID, arg.NewName)
536+
err = stellar.ChangeAccountName(mctx, s.walletState, arg.AccountID, arg.NewName)
537+
if err != nil {
538+
return acct, err
539+
}
540+
return stellar.WalletAccount(mctx, s.remoter, arg.AccountID)
537541
}
538542

539-
func (s *Server) SetWalletAccountAsDefaultLocal(ctx context.Context, arg stellar1.SetWalletAccountAsDefaultLocalArg) (err error) {
543+
func (s *Server) SetWalletAccountAsDefaultLocal(ctx context.Context, arg stellar1.SetWalletAccountAsDefaultLocalArg) (accts []stellar1.WalletAccountLocal, err error) {
540544
mctx, fin, err := s.Preamble(ctx, preambleArg{
541545
RPCName: "SetWalletAccountAsDefaultLocal",
542546
Err: &err,
543547
RequireWallet: true,
544548
})
545549
defer fin()
546550
if err != nil {
547-
return err
551+
return accts, err
548552
}
549553

550554
if arg.AccountID.IsNil() {
551555
mctx.Debug("SetWalletAccountAsDefaultLocal called with an empty account id")
552-
return ErrAccountIDMissing
556+
return accts, ErrAccountIDMissing
553557
}
554558

555-
return stellar.SetAccountAsPrimary(mctx, s.walletState, arg.AccountID)
559+
err = stellar.SetAccountAsPrimary(mctx, s.walletState, arg.AccountID)
560+
if err != nil {
561+
return accts, err
562+
}
563+
return stellar.AllWalletAccounts(mctx, s.remoter)
556564
}
557565

558566
func (s *Server) DeleteWalletAccountLocal(ctx context.Context, arg stellar1.DeleteWalletAccountLocalArg) (err error) {
@@ -578,21 +586,25 @@ func (s *Server) DeleteWalletAccountLocal(ctx context.Context, arg stellar1.Dele
578586
return stellar.DeleteAccount(mctx, arg.AccountID)
579587
}
580588

581-
func (s *Server) ChangeDisplayCurrencyLocal(ctx context.Context, arg stellar1.ChangeDisplayCurrencyLocalArg) (err error) {
589+
func (s *Server) ChangeDisplayCurrencyLocal(ctx context.Context, arg stellar1.ChangeDisplayCurrencyLocalArg) (res stellar1.CurrencyLocal, err error) {
582590
mctx, fin, err := s.Preamble(ctx, preambleArg{
583591
RPCName: "ChangeDisplayCurrencyLocal",
584592
Err: &err,
585593
RequireWallet: true,
586594
})
587595
defer fin()
588596
if err != nil {
589-
return err
597+
return res, err
590598
}
591599

592600
if arg.AccountID.IsNil() {
593-
return ErrAccountIDMissing
601+
return res, ErrAccountIDMissing
602+
}
603+
err = remote.SetAccountDefaultCurrency(mctx.Ctx(), s.G(), arg.AccountID, string(arg.Currency))
604+
if err != nil {
605+
return res, err
594606
}
595-
return remote.SetAccountDefaultCurrency(mctx.Ctx(), s.G(), arg.AccountID, string(arg.Currency))
607+
return stellar.GetCurrencySetting(mctx, arg.AccountID)
596608
}
597609

598610
func (s *Server) GetDisplayCurrencyLocal(ctx context.Context, arg stellar1.GetDisplayCurrencyLocalArg) (res stellar1.CurrencyLocal, err error) {

‎go/stellar/stellarsvc/frontend_test.go‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,12 @@ func TestGetAccountAssetsLocalWithCHFBalance(t *testing.T) {
193193
})
194194
require.NoError(t, err)
195195

196-
err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
196+
curr, err := tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
197197
AccountID: accountID,
198198
Currency: stellar1.OutsideCurrencyCode("CHF"),
199199
})
200200
require.NoError(t, err)
201+
require.Equal(t, stellar1.OutsideCurrencyCode("CHF"), curr.Code)
201202

202203
tcs[0].Backend.ImportAccountsForUser(tcs[0])
203204

@@ -351,11 +352,12 @@ func TestChangeWalletName(t *testing.T) {
351352
chk("", "name required")
352353
chk("office lunch money", "")
353354
chk("savings", "")
354-
err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
355+
res, err := tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
355356
AccountID: accs[0].AccountID,
356357
NewName: "office lunch money",
357358
})
358359
require.NoError(t, err)
360+
require.Equal(t, "office lunch money", res.Name)
359361
chk("office lunch money", "you already have an account with that name")
360362
chk("career debter", "")
361363

@@ -371,7 +373,7 @@ func TestChangeWalletName(t *testing.T) {
371373

372374
// Try invalid argument
373375
invalidAccID, _ := randomStellarKeypair()
374-
err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
376+
_, err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
375377
AccountID: invalidAccID,
376378
NewName: "savings",
377379
})
@@ -398,10 +400,11 @@ func TestSetAccountAsDefault(t *testing.T) {
398400

399401
// Should work for accounts that are already primary and not post
400402
// a bundle.
401-
err = tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), stellar1.SetWalletAccountAsDefaultLocalArg{
403+
res, err := tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), stellar1.SetWalletAccountAsDefaultLocalArg{
402404
AccountID: accs[0].AccountID,
403405
})
404406
require.NoError(t, err)
407+
require.Equal(t, accs[0].AccountID, res[0].AccountID)
405408

406409
mctx := libkb.NewMetaContextBackground(tcs[0].G)
407410
bundle, err := remote.FetchSecretlessBundle(mctx)
@@ -410,12 +413,12 @@ func TestSetAccountAsDefault(t *testing.T) {
410413

411414
// Test invalid arguments
412415
invalidAccID, _ := randomStellarKeypair()
413-
err = tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), stellar1.SetWalletAccountAsDefaultLocalArg{
416+
_, err = tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), stellar1.SetWalletAccountAsDefaultLocalArg{
414417
AccountID: invalidAccID,
415418
})
416419
require.Error(t, err)
417420

418-
err = tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), stellar1.SetWalletAccountAsDefaultLocalArg{
421+
_, err = tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), stellar1.SetWalletAccountAsDefaultLocalArg{
419422
AccountID: stellar1.AccountID(""),
420423
})
421424
require.Error(t, err)
@@ -439,7 +442,7 @@ func TestSetAccountAsDefault(t *testing.T) {
439442
arg := stellar1.SetWalletAccountAsDefaultLocalArg{
440443
AccountID: v,
441444
}
442-
err := tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), arg)
445+
_, err := tcs[0].Srv.SetWalletAccountAsDefaultLocal(context.Background(), arg)
443446
require.NoError(t, err)
444447

445448
accs, err := tcs[0].Srv.WalletGetAccountsCLILocal(context.Background())
@@ -575,22 +578,22 @@ func TestChangeDisplayCurrency(t *testing.T) {
575578
accID := getPrimaryAccountID(tcs[0])
576579

577580
// Try invalid currency first.
578-
err := tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
581+
_, err := tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
579582
AccountID: accID,
580583
Currency: stellar1.OutsideCurrencyCode("ZZZ"),
581584
})
582585
require.Error(t, err)
583586

584587
// Try empty account id.
585-
err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
588+
_, err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
586589
AccountID: stellar1.AccountID(""),
587590
Currency: stellar1.OutsideCurrencyCode("USD"),
588591
})
589592
require.Error(t, err)
590593

591594
// Try non-existant account id.
592595
invalidAccID, _ := randomStellarKeypair()
593-
err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
596+
_, err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
594597
AccountID: invalidAccID,
595598
Currency: stellar1.OutsideCurrencyCode("USD"),
596599
})
@@ -601,18 +604,19 @@ func TestChangeDisplayCurrency(t *testing.T) {
601604
acceptDisclaimer(tcs[1])
602605
tcs[1].Backend.ImportAccountsForUser(tcs[1])
603606
accID2 := getPrimaryAccountID(tcs[1])
604-
err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
607+
_, err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
605608
AccountID: accID2,
606609
Currency: stellar1.OutsideCurrencyCode("EUR"),
607610
})
608611
require.Error(t, err)
609612

610613
// Finally, a happy path.
611-
err = tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
614+
res, err := tcs[0].Srv.ChangeDisplayCurrencyLocal(context.Background(), stellar1.ChangeDisplayCurrencyLocalArg{
612615
AccountID: accID,
613616
Currency: stellar1.OutsideCurrencyCode("EUR"),
614617
})
615618
require.NoError(t, err)
619+
require.Equal(t, stellar1.OutsideCurrencyCode("EUR"), res.Code)
616620

617621
// Check both CLI and Frontend RPCs.
618622
accs, err := tcs[0].Srv.WalletGetAccountsCLILocal(context.Background())
@@ -737,7 +741,7 @@ func TestGetPaymentsLocal(t *testing.T) {
737741
})
738742
require.NoError(t, err)
739743

740-
err = srvSender.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
744+
_, err = srvSender.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
741745
AccountID: accountIDSender,
742746
NewName: "office lunch money",
743747
})
@@ -1064,7 +1068,7 @@ func TestSendToSelf(t *testing.T) {
10641068
})
10651069
require.NoError(t, err)
10661070

1067-
err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
1071+
_, err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
10681072
AccountID: accountID1,
10691073
NewName: "office lunch money",
10701074
})
@@ -1076,7 +1080,7 @@ func TestSendToSelf(t *testing.T) {
10761080
})
10771081
require.NoError(t, err)
10781082

1079-
err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
1083+
_, err = tcs[0].Srv.ChangeWalletAccountNameLocal(context.Background(), stellar1.ChangeWalletAccountNameLocalArg{
10801084
AccountID: accountID2,
10811085
NewName: "savings",
10821086
})

‎go/stellar/stellarsvc/service_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -954,10 +954,12 @@ func TestBundleFlows(t *testing.T) {
954954
assertFetchAccountBundles(t, tcs[0], a2)
955955

956956
// switch which account is primary
957-
err = tcs[0].Srv.SetWalletAccountAsDefaultLocal(ctx, stellar1.SetWalletAccountAsDefaultLocalArg{
957+
defaultChangeRes, err := tcs[0].Srv.SetWalletAccountAsDefaultLocal(ctx, stellar1.SetWalletAccountAsDefaultLocalArg{
958958
AccountID: a1,
959959
})
960960
require.NoError(t, err)
961+
require.Equal(t, a1, defaultChangeRes[0].AccountID)
962+
require.True(t, defaultChangeRes[0].IsDefault)
961963
assertFetchAccountBundles(t, tcs[0], a1)
962964

963965
fullBundle, err := fetchWholeBundleForTesting(mctx)
@@ -981,11 +983,12 @@ func TestBundleFlows(t *testing.T) {
981983
require.EqualValues(t, s2, privKey)
982984

983985
// ChangeAccountName
984-
err = tcs[0].Srv.ChangeWalletAccountNameLocal(ctx, stellar1.ChangeWalletAccountNameLocalArg{
986+
res, err := tcs[0].Srv.ChangeWalletAccountNameLocal(ctx, stellar1.ChangeWalletAccountNameLocalArg{
985987
AccountID: a2,
986988
NewName: "rename",
987989
})
988990
require.NoError(t, err)
991+
require.Equal(t, "rename", res.Name)
989992
bundle, err = remote.FetchAccountBundle(mctx, a2)
990993
require.NoError(t, err)
991994
for _, acc := range bundle.Accounts {

0 commit comments

Comments
 (0)