From 60d7f522e254d2de376aedc89c0f5f9f9d9b567e Mon Sep 17 00:00:00 2001 From: Nick DeLuca Date: Thu, 20 Aug 2020 12:33:21 -0500 Subject: [PATCH] Fix Periodic Vesting Account JSON for Trust Wallet (#630) * massage vesting account json to match cosmos-sdk/Account json for trust wallet * remove comment --- migrate/rest_v0_3/rest.go | 1 + .../v0_8/sdk/auth/v18de63/vesting_account.go | 52 +++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/migrate/rest_v0_3/rest.go b/migrate/rest_v0_3/rest.go index c6f3d04e..9f12b622 100644 --- a/migrate/rest_v0_3/rest.go +++ b/migrate/rest_v0_3/rest.go @@ -239,6 +239,7 @@ func makeCodecV03() *codec.Codec { v0_3valvesting.RegisterCodec(v0_3Codec) return v0_3Codec } + func rollbackAccountType(newAccount authexported.Account) v18de63auth.Account { switch acc := newAccount.(type) { diff --git a/migrate/v0_8/sdk/auth/v18de63/vesting_account.go b/migrate/v0_8/sdk/auth/v18de63/vesting_account.go index 44ad1d01..7af6da59 100644 --- a/migrate/v0_8/sdk/auth/v18de63/vesting_account.go +++ b/migrate/v0_8/sdk/auth/v18de63/vesting_account.go @@ -1,6 +1,12 @@ package v18de63 import ( + "encoding/json" + "strconv" + + "github.com/tendermint/tendermint/crypto" + + "github.com/cosmos/cosmos-sdk/codec" sdk "github.com/cosmos/cosmos-sdk/types" ) @@ -31,6 +37,52 @@ type PeriodicVestingAccount struct { VestingPeriods Periods `json:"vesting_periods" yaml:"vesting_periods"` // the vesting schedule } +type prettyPublicKey struct { + PubKey crypto.PubKey +} + +func (p prettyPublicKey) MarshalJSON() ([]byte, error) { + cdc := codec.New() + codec.RegisterCrypto(cdc) + return cdc.MarshalJSON(p.PubKey) +} + +type vestingAccountPretty struct { + Address sdk.AccAddress `json:"address" yaml:"address"` + Coins sdk.Coins `json:"coins" yaml:"coins"` + PubKey prettyPublicKey `json:"public_key" yaml:"public_key"` + AccountNumber string `json:"account_number" yaml:"account_number"` + Sequence string `json:"sequence" yaml:"sequence"` + OriginalVesting sdk.Coins `json:"original_vesting" yaml:"original_vesting"` + DelegatedFree sdk.Coins `json:"delegated_free" yaml:"delegated_free"` + DelegatedVesting sdk.Coins `json:"delegated_vesting" yaml:"delegated_vesting"` + EndTime int64 `json:"end_time" yaml:"end_time"` + + // custom fields based on concrete vesting type which can be omitted + StartTime int64 `json:"start_time,omitempty" yaml:"start_time,omitempty"` + VestingPeriods Periods `json:"vesting_periods,omitempty" yaml:"vesting_periods,omitempty"` +} + +func (pva PeriodicVestingAccount) MarshalJSON() ([]byte, error) { + pubKey := prettyPublicKey{PubKey: pva.PubKey} + + alias := vestingAccountPretty{ + Address: pva.Address, + Coins: pva.Coins, + PubKey: pubKey, + AccountNumber: strconv.FormatUint(pva.AccountNumber, 10), + Sequence: strconv.FormatUint(pva.Sequence, 10), + OriginalVesting: pva.OriginalVesting, + DelegatedFree: pva.DelegatedFree, + DelegatedVesting: pva.DelegatedVesting, + EndTime: pva.EndTime, + StartTime: pva.StartTime, + VestingPeriods: pva.VestingPeriods, + } + + return json.Marshal(alias) +} + // DelayedVestingAccount implements the VestingAccount interface. It vests all // coins after a specific time, but non prior. In other words, it keeps them // locked until a specified time.