Skip to content

Commit

Permalink
Add HRP support
Browse files Browse the repository at this point in the history
  • Loading branch information
lrettig committed Jul 14, 2023
1 parent e068fdd commit e7a3f24
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/smcli/common"
"github.com/spacemeshos/smcli/wallet"
)
Expand All @@ -36,6 +37,9 @@ var (

// useLedger indicates that the Ledger device should be used.
useLedger bool

// hrp is the human-readable network identifier used in Spacemesh network addresses
hrp string
)

// walletCmd represents the wallet command.
Expand Down Expand Up @@ -264,7 +268,7 @@ only child keys).`,
for _, a := range w.Secrets.Accounts {
if printPrivate {
t.AppendRow(table.Row{
wallet.PubkeyToAddress(a.Public),
wallet.PubkeyToAddress(a.Public, hrp),
encoder(a.Public),
privKeyEncoder(a.Private),
a.Path.String(),
Expand All @@ -273,7 +277,7 @@ only child keys).`,
})
} else {
t.AppendRow(table.Row{
wallet.PubkeyToAddress(a.Public),
wallet.PubkeyToAddress(a.Public, hrp),
encoder(a.Public),
a.Path.String(),
a.DisplayName,
Expand All @@ -293,6 +297,7 @@ func init() {
readCmd.Flags().BoolVarP(&printFull, "full", "f", false, "Print full keys (no abbreviation)")
readCmd.Flags().BoolVar(&printBase58, "base58", false, "Print keys in base58 (rather than hex)")
readCmd.Flags().BoolVar(&printParent, "parent", false, "Print parent key (not only child keys)")
readCmd.Flags().StringVar(&hrp, "hrp", types.NetworkHRP(), "Set human-readable address prefix")
readCmd.PersistentFlags().BoolVarP(&debug, "debug", "d", false, "enable debug mode")
createCmd.Flags().BoolVarP(&useLedger, "ledger", "l", false, "Create a wallet using a Ledger device")
}
4 changes: 3 additions & 1 deletion wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"strings"

"github.com/spacemeshos/go-spacemesh/common/types"
"github.com/spacemeshos/go-spacemesh/genvm/core"
walletTemplate "github.com/spacemeshos/go-spacemesh/genvm/templates/wallet"
"github.com/tyler-smith/go-bip39"
Expand Down Expand Up @@ -173,7 +174,8 @@ func (w *Wallet) Mnemonic() string {
return w.Secrets.Mnemonic
}

func PubkeyToAddress(pubkey []byte) string {
func PubkeyToAddress(pubkey []byte, hrp string) string {
types.SetNetworkHRP(hrp)
key := [ed25519.PublicKeySize]byte{}
copy(key[:], pubkey)
walletArgs := &walletTemplate.SpawnArguments{PublicKey: key}
Expand Down
7 changes: 6 additions & 1 deletion wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ed25519"
"encoding/hex"
"fmt"
"github.com/spacemeshos/go-spacemesh/common/types"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -91,8 +92,12 @@ func TestWalletFromGivenMnemonic(t *testing.T) {

// Test conversion to a Spacemesh wallet address
expAddress := "sm1qqqqqqz9rf583slhn38g6q6a562ctltv9fv5w8q2gdz9k"
address := PubkeyToAddress(w.Secrets.Accounts[0].Public)
address := PubkeyToAddress(w.Secrets.Accounts[0].Public, types.NetworkHRP())
require.Equal(t, expAddress, address)

expAddressTestnet := "stest1qqqqqqz9rf583slhn38g6q6a562ctltv9fv5w8qha56t0"
addressTestnet := PubkeyToAddress(w.Secrets.Accounts[0].Public, "stest")
require.Equal(t, expAddressTestnet, addressTestnet)
}

func TestKeysInWalletMaintainExpectedPath(t *testing.T) {
Expand Down

0 comments on commit e7a3f24

Please sign in to comment.