Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd: Show ParaTime transaction details #24

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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