From e7a3f24ac4587e095f76f1dfd91bc54391c33687 Mon Sep 17 00:00:00 2001 From: Lane Rettig Date: Fri, 14 Jul 2023 06:57:14 -0400 Subject: [PATCH] Add HRP support --- cmd/wallet.go | 9 +++++++-- wallet/wallet.go | 4 +++- wallet/wallet_test.go | 7 ++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/wallet.go b/cmd/wallet.go index f93daa4..7b78b03 100644 --- a/cmd/wallet.go +++ b/cmd/wallet.go @@ -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" ) @@ -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. @@ -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(), @@ -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, @@ -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") } diff --git a/wallet/wallet.go b/wallet/wallet.go index d52b4e1..5849d21 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -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" @@ -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} diff --git a/wallet/wallet_test.go b/wallet/wallet_test.go index d76efc6..cbff256 100644 --- a/wallet/wallet_test.go +++ b/wallet/wallet_test.go @@ -4,6 +4,7 @@ import ( "crypto/ed25519" "encoding/hex" "fmt" + "github.com/spacemeshos/go-spacemesh/common/types" "testing" "github.com/stretchr/testify/require" @@ -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) {