Skip to content

Commit

Permalink
Fix linter complaints
Browse files Browse the repository at this point in the history
  • Loading branch information
fasmat committed Jun 21, 2024
1 parent 19b450a commit c9ac113
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 33 deletions.
5 changes: 3 additions & 2 deletions cmd/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ var verifyCmd = &cobra.Command{

// first, collect the keys
var keys []core.PublicKey
fmt.Print("First, let's collect your public keys. Keys must be entered in hex format: 64 characters, without 0x prefix.\n")
fmt.Print("Enter pubkeys one at a time; press enter again when done: ")
fmt.Print("First, let's collect your public keys. ")
fmt.Print("Keys must be entered in hex format: 64 characters, without 0x prefix.\n")
fmt.Print("Enter pub keys one at a time; press enter again when done: ")
for {
var keyStr string
_, err := fmt.Scanln(&keyStr)
Expand Down
12 changes: 8 additions & 4 deletions cmd/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,21 @@ sure the device is connected, unlocked, and the Spacemesh app is open.`,
text, err := password.Read(os.Stdin)
fmt.Println()
cobra.CheckErr(err)
fmt.Println("Note: This application does not yet support BIP-39-compatible optional passwords. Support will be added soon.")
fmt.Print("Note: This application does not yet support BIP-39-compatible optional passwords. ")
fmt.Println("Support will be added soon.")

// It's critical that we trim whitespace, including CRLF. Otherwise it will get included in the mnemonic.
text = strings.TrimSpace(text)

if text == "" {
w, err = wallet.NewMultiWalletRandomMnemonic(n)
cobra.CheckErr(err)
fmt.Println("\nThis is your mnemonic (seed phrase). Write it down and store it safely. It is the ONLY way to restore your wallet.")
fmt.Println("Neither Spacemesh nor anyone else can help you restore your wallet without this mnemonic.")
fmt.Println("\n***********************************\nSAVE THIS MNEMONIC IN A SAFE PLACE!\n***********************************")
fmt.Print("\nThis is your mnemonic (seed phrase). Write it down and store it safely.")
fmt.Print("It is the ONLY way to restore your wallet.\n")
fmt.Print("Neither Spacemesh nor anyone else can help you restore your wallet without this mnemonic.\n")
fmt.Print("\n***********************************\n")
fmt.Print("SAVE THIS MNEMONIC IN A SAFE PLACE!")
fmt.Print("\n***********************************\n")
fmt.Println()
fmt.Println(w.Mnemonic())
fmt.Println("\nPress enter when you have securely saved your mnemonic.")
Expand Down
5 changes: 3 additions & 2 deletions wallet/bip32.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"errors"
"fmt"

smbip32 "github.com/spacemeshos/smkeys/bip32"
Expand Down Expand Up @@ -95,7 +96,7 @@ func (kp *EDKeyPair) NewChildKeyPair(seed []byte, childIdx int) (*EDKeyPair, err
Path: path,
}, nil
default:
return nil, fmt.Errorf("unknown key type")
return nil, errors.New("unknown key type")
}
}

Expand All @@ -109,7 +110,7 @@ func pubkeyFromLedger(path HDPath, master bool) (*EDKeyPair, error) {
// the one they really care about, which is the first child key.
key, err := ledger.ReadPubkeyFromLedger("", HDPathToString(path), !master)
if err != nil {
return nil, fmt.Errorf("error reading pubkey from ledger. Are you sure it's connected, unlocked, and the Spacemesh app is open? err: %w", err)
return nil, fmt.Errorf("error reading pubkey from ledger: %w", err)
}

name := "Ledger Master Key"
Expand Down
13 changes: 7 additions & 6 deletions wallet/bip32_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ func TestChildKeyPair(t *testing.T) {
require.NoError(t, err)
childKeyPair1, err := masterKeyPair.NewChildKeyPair(goodSeed, 0)
require.Equal(t, path, childKeyPair1.Path)
require.Equal(t, ed25519.PrivateKeySize, len(childKeyPair1.Private))
require.Equal(t, ed25519.PublicKeySize, len(childKeyPair1.Public))
require.Len(t, childKeyPair1.Private, ed25519.PrivateKeySize)
require.Len(t, childKeyPair1.Public, ed25519.PublicKeySize)
require.NoError(t, err)
require.NotEmpty(t, childKeyPair1)

Expand All @@ -59,17 +59,18 @@ func TestChildKeyPair(t *testing.T) {
// generate second keypair and check lengths
childKeyPair2, err := bip32.Derive(HDPathToString(path), goodSeed)
require.NoError(t, err)
require.Equal(t, ed25519.PrivateKeySize, len(childKeyPair2))
require.Len(t, childKeyPair2, ed25519.PrivateKeySize)
privkey2 := PrivateKey(childKeyPair2[:])
require.Equal(t, ed25519.PrivateKeySize, len(privkey2))
require.Len(t, privkey2, ed25519.PrivateKeySize)
edpubkey2 := ed25519.PrivateKey(privkey2).Public().(ed25519.PublicKey)
require.Equal(t, ed25519.PublicKeySize, len(edpubkey2))
require.Len(t, edpubkey2, ed25519.PublicKeySize)
pubkey2 := PublicKey(edpubkey2)
require.Equal(t, ed25519.PublicKeySize, len(pubkey2))
require.Len(t, pubkey2, ed25519.PublicKeySize)

// make sure they agree
require.Equal(t, "feae6977b42bf3441d04314d09c72c5d6f2d1cb4bf94834680785b819f8738dd", hex.EncodeToString(pubkey2))
require.Equal(t, hex.EncodeToString(childKeyPair1.Public), hex.EncodeToString(pubkey2))
//nolint:lll
require.Equal(t, "05fe9affa5562ca833faf3803ce5f6f7615d3c37c4a27903492027f6853e486dfeae6977b42bf3441d04314d09c72c5d6f2d1cb4bf94834680785b819f8738dd", hex.EncodeToString(privkey2))
require.Equal(t, hex.EncodeToString(childKeyPair1.Private), hex.EncodeToString(privkey2))
}
13 changes: 9 additions & 4 deletions wallet/bip44.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"encoding/json"
"fmt"
"regexp"
"strconv"
"strings"
)

//lint:file-ignore SA4016 ignore ineffective bitwise operations to aid readability
Expand Down Expand Up @@ -128,15 +130,18 @@ func IsPathCompletelyHardened(path HDPath) bool {
// HDPathToString converts a BIP44 HD path to a string of the form
// "m/44'/540'/account'/chain'/address_index'".
func HDPathToString(path HDPath) string {
s := "m"
var sb strings.Builder
sb.WriteString("m")
for _, p := range path {
sb.WriteString("/")
if p >= BIP32HardenedKeyStart {
s += "/" + fmt.Sprint(p-BIP32HardenedKeyStart) + "'"
sb.WriteString(strconv.FormatUint(uint64(p-BIP32HardenedKeyStart), 10))
sb.WriteString("'")
} else {
s += "/" + fmt.Sprint(p)
sb.WriteString(strconv.FormatUint(uint64(p), 10))
}
}
return s
return sb.String()
}

func parseUint(s string) uint {
Expand Down
33 changes: 27 additions & 6 deletions wallet/bip44_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,33 @@ func TestStringToHDPath(t *testing.T) {
path string
expected HDPath
}{
{"m/44'/540'", HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540}},
{"m/44'/540'/0", HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, 0}},
{"m/44'/540'/0'/0'/0'", HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, BIP32HardenedKeyStart, BIP32HardenedKeyStart, BIP32HardenedKeyStart}},
{"m/44'/540'/0'/0/0", HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, BIP32HardenedKeyStart, 0, 0}},
{"m/44'/540'/0/0'/0", HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, 0, BIP32HardenedKeyStart, 0}},
{"m/44'/540'/2'/0/0", HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, BIP32HardenedKeyStart | 2, 0, 0}},
{
"m/44'/540'",
HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540},
},
{
"m/44'/540'/0",
HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, 0},
},
{
"m/44'/540'/0'/0'/0'",
HDPath{
BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, BIP32HardenedKeyStart,
BIP32HardenedKeyStart, BIP32HardenedKeyStart,
},
},
{
"m/44'/540'/0'/0/0",
HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, BIP32HardenedKeyStart, 0, 0},
},
{
"m/44'/540'/0/0'/0",
HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, 0, BIP32HardenedKeyStart, 0},
},
{
"m/44'/540'/2'/0/0",
HDPath{BIP32HardenedKeyStart | 44, BIP32HardenedKeyStart | 540, BIP32HardenedKeyStart | 2, 0, 0},
},
}

for _, tv := range testVectors {
Expand Down
10 changes: 5 additions & 5 deletions wallet/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"crypto/rand"
"crypto/sha512"
"encoding/json"
"fmt"
"errors"
"io"
"log"

Expand Down Expand Up @@ -171,7 +171,7 @@ func (k *WalletKey) Open(file io.Reader, debugMode bool) (*Wallet, error) {
var salt [Pbkdf2SaltBytesLen]byte
copy(salt[:], ew.Secrets.KDFParams.Salt)
if !bytes.Equal(salt[:], ew.Secrets.KDFParams.Salt) {
return nil, fmt.Errorf("error reading encrypted wallet file salt, check salt length")
return nil, errors.New("error reading encrypted wallet file salt, check salt length")
}
WithSalt(salt)(k)
} else if !bytes.Equal(ew.Secrets.KDFParams.Salt, k.salt) {
Expand Down Expand Up @@ -206,15 +206,15 @@ func (k *WalletKey) Open(file io.Reader, debugMode bool) (*Wallet, error) {
return w, nil
}

func (k *WalletKey) Export(file io.Writer, w *Wallet) (err error) {
func (k *WalletKey) Export(file io.Writer, w *Wallet) error {
// encrypt the secrets
plaintext, err := json.Marshal(w.Secrets)
if err != nil {
return
return err
}
ciphertext, nonce, err := k.encrypt(plaintext)
if err != nil {
return
return err
}
ew := &EncryptedWalletFile{
Meta: w.Meta,
Expand Down
11 changes: 7 additions & 4 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ed25519"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strings"

Expand All @@ -15,7 +16,7 @@ import (
"github.com/spacemeshos/smcli/common"
)

var errWhitespace = fmt.Errorf("whitespace violation in mnemonic phrase")
var errWhitespace = errors.New("whitespace violation in mnemonic phrase")

// Wallet is the basic data structure.
type Wallet struct {
Expand Down Expand Up @@ -95,7 +96,7 @@ func NewMultiWalletRandomMnemonic(n int) (*Wallet, error) {

func NewMultiWalletFromMnemonic(m string, n int) (*Wallet, error) {
if n < 0 || n > common.MaxAccountsPerWallet {
return nil, fmt.Errorf("invalid number of accounts")
return nil, errors.New("invalid number of accounts")
}

// bip39 lib doesn't properly validate whitespace so we have to do that manually.
Expand All @@ -105,7 +106,7 @@ func NewMultiWalletFromMnemonic(m string, n int) (*Wallet, error) {

// this checks the number of words and the checksum.
if !bip39.IsMnemonicValid(m) {
return nil, fmt.Errorf("invalid mnemonic")
return nil, errors.New("invalid mnemonic")
}

// TODO: add option for user to provide passphrase
Expand All @@ -125,10 +126,12 @@ func NewMultiWalletFromMnemonic(m string, n int) (*Wallet, error) {

func NewMultiWalletFromLedger(n int) (*Wallet, error) {
if n < 0 || n > common.MaxAccountsPerWallet {
return nil, fmt.Errorf("invalid number of accounts")
return nil, errors.New("invalid number of accounts")
}
masterKeyPair, err := NewMasterKeyPairFromLedger()
if err != nil {
fmt.Println("Error: ", err)
fmt.Println("Are you sure the ledger is connected, unlocked, and the Spacemesh app is open?")
return nil, err
}
// seed is not used in case of ledger
Expand Down
2 changes: 2 additions & 0 deletions wallet/wallet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func TestAccountFromSeed(t *testing.T) {
keypair := accts[0]

expPubKey := "feae6977b42bf3441d04314d09c72c5d6f2d1cb4bf94834680785b819f8738dd"
//nolint:lll
expPrivKey := "05fe9affa5562ca833faf3803ce5f6f7615d3c37c4a27903492027f6853e486dfeae6977b42bf3441d04314d09c72c5d6f2d1cb4bf94834680785b819f8738dd"

actualPubKey := hex.EncodeToString(keypair.Public)
Expand Down Expand Up @@ -77,6 +78,7 @@ func TestWalletFromGivenMnemonic(t *testing.T) {
w, err := NewMultiWalletFromMnemonic(mnemonic, 1)
require.NoError(t, err)
expPubKey := "de30fc9b812248583da6259433626fcdd2cb5ce589b00047b81e127950b9bca6"
//nolint:lll
expPrivKey := "cd85df73aa3bc31de2f0b69bb1421df7eb0cdca7cb170a457869ab337749dae1de30fc9b812248583da6259433626fcdd2cb5ce589b00047b81e127950b9bca6"

actualPubKey := hex.EncodeToString(w.Secrets.Accounts[0].Public)
Expand Down

0 comments on commit c9ac113

Please sign in to comment.