Skip to content

Commit

Permalink
cmd: Show ParaTime transaction details
Browse files Browse the repository at this point in the history
  • Loading branch information
matevz committed Mar 14, 2023
1 parent e0205e4 commit c10ca74
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
23 changes: 23 additions & 0 deletions cmd/common/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ import (
"fmt"

"github.com/spf13/cobra"

"github.com/oasisprotocol/oasis-sdk/client-sdk/go/testing"
"github.com/oasisprotocol/oasis-sdk/client-sdk/go/types"

"github.com/oasisprotocol/cli/config"
)

// CheckForceErr treats error as warning, if --force is provided.
Expand All @@ -24,3 +29,21 @@ func CheckForceErr(err interface{}) {
errMsg += "\nUse --force to ignore this check"
cobra.CheckErr(errMsg)
}

// GenAccountNames generates a map of all addresses -> account name for pretty printing.
func GenAccountNames() types.AccountNames {
an := types.AccountNames{}
for name, acc := range config.Global().Wallet.All {
an[acc.GetAddress().String()] = name
}

for name, acc := range config.Global().AddressBook.All {
an[acc.GetAddress().String()] = name
}

for name, acc := range testing.TestAccounts {
an[acc.Address.String()] = fmt.Sprintf("test:%s", name)
}

return an
}
27 changes: 24 additions & 3 deletions cmd/common/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/spf13/cobra"
flag "github.com/spf13/pflag"

"github.com/oasisprotocol/oasis-core/go/common"
"github.com/oasisprotocol/oasis-core/go/common/cbor"
coreSignature "github.com/oasisprotocol/oasis-core/go/common/crypto/signature"
consensusPretty "github.com/oasisprotocol/oasis-core/go/common/prettyprint"
Expand Down Expand Up @@ -57,6 +58,15 @@ func GetTransactionConfig() *TransactionConfig {
}
}

// isRuntimeTx returns true, if given object is a signed or unsigned runtime transaction.
func isRuntimeTx(tx interface{}) bool {
_, isRuntimeTx := tx.(*types.Transaction)
if !isRuntimeTx {
_, isRuntimeTx = tx.(*types.UnverifiedTransaction)
}
return isRuntimeTx
}

// SignConsensusTransaction signs a consensus transaction.
func SignConsensusTransaction(
ctx context.Context,
Expand Down Expand Up @@ -256,13 +266,24 @@ func SignParaTimeTransaction(

// PrintTransaction prints the transaction which can be either signed or unsigned.
func PrintTransaction(npa *NPASelection, tx interface{}) {
var isParaTimeTx bool
switch rtx := tx.(type) {
case consensusPretty.PrettyPrinter:
// Signed or unsigned consensus transaction.
// Signed or unsigned consensus or runtime transaction.
var ns common.Namespace
if npa.ParaTime != nil {
ns = npa.ParaTime.Namespace()
}
sigCtx := signature.RichContext{
RuntimeID: ns,
ChainContext: npa.Network.ChainContext,
Base: types.SignatureContextBase,
}
ctx := context.Background()
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenSymbol, npa.Network.Denomination.Symbol)
ctx = context.WithValue(ctx, consensusPretty.ContextKeyTokenValueExponent, npa.Network.Denomination.Decimals)
ctx = context.WithValue(ctx, config.ContextKeyParaTimeCfg, npa.ParaTime)
ctx = context.WithValue(ctx, signature.ContextKeySigContext, &sigCtx)
ctx = context.WithValue(ctx, types.ContextKeyAccountNames, GenAccountNames())

// Set up chain context for signature verification during pretty-printing.
coreSignature.UnsafeResetChainContext()
Expand All @@ -279,7 +300,7 @@ func PrintTransaction(npa *NPASelection, tx interface{}) {
}

fmt.Println()
if isParaTimeTx && npa.ParaTime != nil {
if isRuntimeTx(tx) && npa.ParaTime != nil {
fmt.Printf("ParaTime: %s", npa.ParaTimeName)
if len(npa.ParaTime.Description) > 0 {
fmt.Printf(" (%s)", npa.ParaTime.Description)
Expand Down

0 comments on commit c10ca74

Please sign in to comment.