Skip to content

Commit

Permalink
refactor: fix linter errors manually
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 8, 2021
1 parent 4e223f8 commit f78ad17
Show file tree
Hide file tree
Showing 23 changed files with 94 additions and 116 deletions.
2 changes: 1 addition & 1 deletion account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type Account struct {
func loadFromPrivateKeyRaw(privb []byte) (account *Account, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("Invalid private key")
err = fmt.Errorf("invalid private key")
}
}()
account = loadFromPrivateKey(ed25519.PrivateKey(privb))
Expand Down
2 changes: 1 addition & 1 deletion account/bip32.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// ErrHardenedChildPublicKey is returned when trying to create a hardened child
// from a public key.
var ErrHardenedChildPublicKey = errors.New("Can't create hardened child from public key")
var ErrHardenedChildPublicKey = errors.New("can't create hardened child from public key")

// ErrHardenedOnly is returned when a node in the path is not hardened.
var ErrHardenedOnly = errors.New("ed25519 only works with hardened children")
Expand Down
15 changes: 12 additions & 3 deletions account/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ func KeystoreOpen(data []byte, password string) (account *Account, err error) {
}
// build the key from the password
salt, err := hex.DecodeString(k.Crypto.KdfParams.Salt)
if err != nil {
return
}
argonKey := argon2.IDKey([]byte(password), salt,
k.Crypto.KdfParams.Opslimit,
k.Crypto.KdfParams.Memlimit,
Expand All @@ -68,14 +71,20 @@ func KeystoreOpen(data []byte, password string) (account *Account, err error) {
copy(key[:], argonKey)
// retrieve the nonce
v, err := hex.DecodeString(k.Crypto.CipherParams.Nonce)
if err != nil {
return
}
var decryptNonce [24]byte
copy(decryptNonce[:], v)
// now retrieve the cypertext
v, err = hex.DecodeString(k.Crypto.Ciphertext)
//
if err != nil {
return
}

decrypted, ok := secretbox.Open(nil, v, &decryptNonce, &key)
if !ok {
err = fmt.Errorf("Cannot decrypt secret")
err = fmt.Errorf("cannot decrypt secret")
return
}
// now load the account
Expand All @@ -97,7 +106,7 @@ func randomBytes(n int) ([]byte, error) {
}

// KeystoreSeal create an encrypted json keystore with the private key of the account
func KeystoreSeal(account *Account, password string) (j []byte, e error) {
func KeystoreSeal(account *Account, password string) (j []byte, err error) {
// normalize pwd
salt, err := randomBytes(16)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,6 @@ func findVMABIVersion(nodeVersion, compilerBackend string) (VMVersion, ABIVersio
} else if nodeVersion[0] == '4' {
return 4, 1, nil
} else {
return 0, 0, errors.New("Other node versions unsupported")
return 0, 0, errors.New("other node versions unsupported")
}
}
7 changes: 5 additions & 2 deletions aeternity/oracles.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,16 @@ func (o *Oracle) respondToQueries(queryChan chan *models.OracleQuery, errChan ch
fmt.Println("Received query", *q.ID, qStr)
resp, err := o.Handler(qStr)
if err != nil {
fmt.Println("Error responding to OracleQuery - reason:", err)
fmt.Println("Error responding to OracleQuery", err)
}
respTx, err := transactions.NewOracleRespondTx(o.ctx.SenderAccount(), o.ID, *q.ID, resp, config.Client.Oracles.ResponseTTLType, config.Client.Oracles.ResponseTTLValue, o.ctx.TTLNoncer())
if err != nil {
fmt.Println("Error generating OracleRespondTx", err)
}
fmt.Println("respTx", respTx)
receipt, err := o.ctx.SignBroadcastWait(respTx, config.Client.WaitBlocks)
if err != nil {
fmt.Println("Error sending a response to OracleQuery, reason:", err)
fmt.Println("Error sending a response to OracleQuery", err)
}
fmt.Println(receipt)
}
Expand Down
10 changes: 5 additions & 5 deletions binary/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package binary
import (
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"

rlp "github.com/aeternity/rlp-go"
Expand Down Expand Up @@ -31,9 +32,8 @@ func Encode(prefix HashPrefix, data []byte) string {
case Base64c:
return fmt.Sprint(prefix, base64.StdEncoding.EncodeToString(in))
default:
panic(fmt.Sprint("Encoding not supported"))
panic("Encoding not supported")
}

}

// Decode a string encoded with base58/base64 + checksum to a byte array
Expand All @@ -52,7 +52,7 @@ func Decode(in string) (out []byte, err error) {
// 3 (**_) + 5 (Single byte, prefixed with Base58 4 character hash)
// then split it into p(refix) and h(ash)
if len(in) <= 8 || string(in[2]) != PrefixSeparator {
err = fmt.Errorf("Invalid object encoding")
err = errors.New("invalid object encoding")
return
}
p = HashPrefix(in[0:3])
Expand All @@ -65,12 +65,12 @@ func Decode(in string) (out []byte, err error) {
raw, _ = base64.StdEncoding.DecodeString(h)
}
if len(raw) < 5 {
err = fmt.Errorf("Invalid input, %s cannot be decoded", in)
err = fmt.Errorf("invalid input, %s cannot be decoded", in)
return nil, err
}
out = raw[:len(raw)-4]
if chk := Encode(p, out); in != chk {
err = fmt.Errorf("Invalid checksum, expected %s got %s", chk, in)
err = fmt.Errorf("invalid checksum, expected %s got %s", chk, in)
return nil, err
}
return out, nil
Expand Down
15 changes: 10 additions & 5 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

var (
waitForTx bool
spendTxPayload string
printPrivateKey bool
accountFileName string
Expand Down Expand Up @@ -61,10 +60,7 @@ func getPassword() (p string, err error) {
return password, nil
}
p, err = AskPassword("Enter the password to unlock the keystore: ")
if err != nil {
return "", err
}
return p, nil
return
}

func addressFunc(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -134,6 +130,9 @@ var balanceCmd = &cobra.Command{

func balanceFunc(conn naet.GetAccounter, args []string) (err error) {
p, err := getPassword()
if err != nil {
return err
}

// load the account
account, err := account.LoadFromKeyStoreFile(args[0], p)
Expand Down Expand Up @@ -161,6 +160,9 @@ var signCmd = &cobra.Command{

func signFunc(cmd *cobra.Command, args []string) (err error) {
p, err := getPassword()
if err != nil {
return err
}

// load the account
account, err := account.LoadFromKeyStoreFile(args[0], p)
Expand Down Expand Up @@ -210,6 +212,9 @@ func saveFunc(cmd *cobra.Command, args []string) (err error) {
}

p, err := getPassword()
if err != nil {
return err
}

f, err := account.StoreToKeyStoreFile(acc, p, accountFileName)
if err != nil {
Expand Down
13 changes: 5 additions & 8 deletions cmd/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func playFunc(conn playFuncInterface, args []string) (err error) {

// deal with the height parameter
if startFromHeight > blockHeight {
err := fmt.Errorf("Height (%d) is greater that the top block (%d)", startFromHeight, blockHeight)
err := fmt.Errorf("height (%d) is greater that the top block (%d)", startFromHeight, blockHeight)
return err
}

Expand Down Expand Up @@ -133,8 +133,7 @@ func broadcastFunc(conn naet.PostTransactioner, args []string) (err error) {
txSignedBase64 := args[0]

if len(txSignedBase64) == 0 || txSignedBase64[0:3] != "tx_" {
err := errors.New("Error, missing or invalid recipient address")
return err
return errors.New("missing or invalid recipient address")
}

// Transform tx_ string back to an RLP bytearray to calculate its hash
Expand All @@ -150,8 +149,7 @@ func broadcastFunc(conn naet.PostTransactioner, args []string) (err error) {

err = conn.PostTransaction(txSignedBase64, signedEncodedTxHash)
if err != nil {
errFinal := fmt.Errorf("Error while broadcasting transaction: %v", err)
return errFinal
return fmt.Errorf("can't broadcast transaction: %v", err)
}

return nil
Expand All @@ -171,7 +169,7 @@ var ttlCmd = &cobra.Command{
func ttlFunc(conn naet.GetHeighter, args []string) (err error) {
height, err := conn.GetHeight()
if err != nil {
errFinal := fmt.Errorf("Error getting height from the node: %v", err)
errFinal := fmt.Errorf("can't get height from the node: %v", err)
return errFinal
}
ttl = height + config.Client.TTL
Expand All @@ -193,8 +191,7 @@ var networkIDCmd = &cobra.Command{
func networkIDFunc(conn naet.GetStatuser, args []string) (err error) {
resp, err := conn.GetStatus()
if err != nil {
errFinal := fmt.Errorf("Error getting status information from the node: %v", err)
return errFinal
return fmt.Errorf("can't get status information from the node: %v", err)
}
fmt.Println(*resp.NetworkID)
return nil
Expand Down
1 change: 1 addition & 0 deletions cmd/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,4 +168,5 @@ func init() {
contractCmd.AddCommand(encodeCalldataCmd)
contractCmd.AddCommand(decodeCalldataBytecodeCmd)
contractCmd.AddCommand(decodeCalldataSourceCmd)
contractCmd.AddCommand(generateAciCmd)
}
8 changes: 6 additions & 2 deletions cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,13 @@ func printOracleByPubkey(conn naet.GetOracleByPubkeyer, oracleID string) (err er
}

func inspectFunc(conn nodeGetters, args []string) (err error) {
numberRegexp, err := regexp.Compile(`^\d+$`)
if err != nil {
return
}
for _, object := range args {
// height
if matched, _ := regexp.MatchString(`^\d+$`, object); matched {
if numberRegexp.MatchString(object) {
height, _ := strconv.ParseUint(object, 10, 64)
PrintGenerationByHeight(conn, height)
continue
Expand All @@ -166,7 +170,7 @@ func inspectFunc(conn nodeGetters, args []string) (err error) {
case binary.PrefixOraclePubkey:
printOracleByPubkey(conn, object)
default:
return fmt.Errorf("Object %v not yet supported", object)
return fmt.Errorf("object %v not yet supported", object)
}
}
return nil
Expand Down
6 changes: 0 additions & 6 deletions cmd/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ var (
defaultIndentSize = 50
)

// Left left-pads the string with pad up to len runes
// len may be exceeded if
func left(str string, length int, pad string) string {
return times(pad, length-len(str)) + str
}

// Right right-pads the string with pad up to len runes
func right(str string, length int, pad string) string {
return str + times(pad, length-len(str))
Expand Down
6 changes: 3 additions & 3 deletions cmd/text_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"strings"
"syscall"

"golang.org/x/crypto/ssh/terminal"
"golang.org/x/term"
)

// IsEqStr tells if two strings a and b are equals after trimming spaces and lowercasing
func IsEqStr(a, b string) bool {
return strings.ToLower(strings.TrimSpace(a)) == strings.ToLower(strings.TrimSpace(b))
return strings.EqualFold(strings.TrimSpace(a), strings.TrimSpace(b))
}

// IsEmptyStr tells if a string is empty or not
Expand Down Expand Up @@ -46,7 +46,7 @@ func AskYes(question string, defaultYes bool) (isYes bool) {
// AskPassword ask a password
func AskPassword(question string) (password string, err error) {
fmt.Println(question)
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
bytePassword, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return
}
Expand Down
33 changes: 15 additions & 18 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/aeternity/aepp-sdk-go/v8/binary"
"github.com/aeternity/aepp-sdk-go/v8/config"
"github.com/aeternity/aepp-sdk-go/v8/naet"
"github.com/aeternity/aepp-sdk-go/v8/transactions"
"github.com/aeternity/aepp-sdk-go/v8/utils"

Expand Down Expand Up @@ -49,20 +48,23 @@ func txSpendFunc(ttlFunc transactions.TTLer, nonceFunc transactions.Noncer, args
sender = args[0]
recipient = args[1]
amount, err = utils.NewIntFromString(args[2])
if err != nil {
return
}
feeBigInt, _ = utils.NewIntFromString(fee)

// Validate arguments
if !IsAddress(sender) {
return errors.New("Error, missing or invalid sender address")
return errors.New("missing or invalid sender address")
}
if !IsAddress(recipient) {
return errors.New("Error, missing or invalid recipient address")
return errors.New("missing or invalid recipient address")
}
if amount.Cmp(big.NewInt(0)) == -1 {
return errors.New("Error, missing or invalid amount")
return errors.New("missing or invalid amount")
}
if feeBigInt.Cmp(big.NewInt(0)) == -1 {
return errors.New("Error, missing or invalid fee")
return errors.New("missing or invalid fee")
}

// If nonce or TTL was specified, no need to query the node
Expand Down Expand Up @@ -118,11 +120,6 @@ var txContractCreateCmd = &cobra.Command{
},
}

type getHeightAccounter interface {
naet.GetHeighter
naet.GetAccounter
}

func txContractCreateFunc(ttlFunc transactions.TTLer, nonceFunc transactions.Noncer, args []string) (err error) {
var (
owner string
Expand All @@ -133,15 +130,15 @@ func txContractCreateFunc(ttlFunc transactions.TTLer, nonceFunc transactions.Non
// Load variables from arguments and validate them
owner = args[0]
if !IsAddress(owner) {
return errors.New("Error, missing or invalid owner address")
return errors.New("missing or invalid owner address")
}
contract = args[1]
if !IsBytecode(contract) {
return errors.New("Error, missing or invalid contract bytecode")
return errors.New("missing or invalid contract bytecode")
}
calldata = args[2]
if !IsBytecode(calldata) {
return errors.New("Error, missing or invalid init calldata bytecode")
return errors.New("missing or invalid init calldata bytecode")
}

// If nonce was not specified as an argument, connect to the node to
Expand Down Expand Up @@ -206,10 +203,10 @@ func txVerifyFunc(cmd *cobra.Command, args []string) (err error) {
txSignedBase64 := args[1]

if !IsAddress(sender) {
return errors.New("Error, missing or invalid sender address")
return errors.New("missing or invalid sender address")
}
if !IsTransaction(txSignedBase64) {
return errors.New("Error, missing or invalid base64 encoded transaction")
return errors.New("missing or invalid base64 encoded transaction")
}
valid, err := transactions.VerifySignedTx(sender, txSignedBase64, config.Node.NetworkID)
if err != nil {
Expand Down Expand Up @@ -239,7 +236,7 @@ var txDumpRawCmd = &cobra.Command{
func txDumpRawFunc(cmd *cobra.Command, args []string) (err error) {
tx := args[0]
if !IsTransaction(tx) {
return errors.New("Error, missing or invalid base64 encoded transaction")
return errors.New("missing or invalid base64 encoded transaction")
}
txRaw, err := binary.Decode(tx)
if err != nil {
Expand All @@ -260,6 +257,6 @@ func init() {
// tx spend command
txSpendCmd.Flags().StringVar(&fee, "fee", config.Client.Fee.String(), fmt.Sprintf("Set the transaction fee (default=%s)", config.Client.Fee.String()))
txSpendCmd.Flags().Uint64Var(&ttl, "ttl", 0, fmt.Sprintf("Set the TTL in keyblocks (default=%d)", 0))
txSpendCmd.Flags().Uint64Var(&nonce, "nonce", 0, fmt.Sprint("Set the sender account nonce, if not the chain will be queried for its value"))
txSpendCmd.Flags().StringVar(&spendTxPayload, "payload", "", fmt.Sprint("Optional text payload for Spend Transactions, which will be turned into a bytearray"))
txSpendCmd.Flags().Uint64Var(&nonce, "nonce", 0, "Set the sender account nonce, if not the chain will be queried for its value")
txSpendCmd.Flags().StringVar(&spendTxPayload, "payload", "", "Optional text payload for Spend Transactions, which will be turned into a bytearray")
}

0 comments on commit f78ad17

Please sign in to comment.