From f99ebbacf850d9b91a66ba561dad212f775e492e Mon Sep 17 00:00:00 2001 From: ATMackay Date: Wed, 28 Feb 2024 11:03:20 +0000 Subject: [PATCH] chore: add err checks and other lint fixes --- certificate.go | 4 ++-- cli/cmd/new_address.go | 8 ++++++-- crypto/key_test.go | 4 ++-- internal/cbor/decode.go | 30 ++++++++++++++---------------- protocol.go | 8 ++++---- script.go | 3 +-- wallet/db.go | 4 +++- wallet/wallet.go | 5 ++++- 8 files changed, 36 insertions(+), 30 deletions(-) diff --git a/certificate.go b/certificate.go index 9ba473a..9a679ea 100644 --- a/certificate.go +++ b/certificate.go @@ -256,8 +256,8 @@ type RelayType uint64 const ( SingleHostAddr RelayType = 0 - SingleHostName = 1 - MultiHostName = 2 + SingleHostName RelayType = 1 + MultiHostName RelayType = 2 ) type singleHostAddr struct { diff --git a/cli/cmd/new_address.go b/cli/cmd/new_address.go index 2468ec3..7e8f3d8 100644 --- a/cli/cmd/new_address.go +++ b/cli/cmd/new_address.go @@ -30,8 +30,12 @@ var newAddressCmd = &cobra.Command{ if err != nil { return err } - w.AddAddress() - client.SaveWallet(w) + if _, err := w.AddAddress(); err != nil { + return err + } + if err := client.SaveWallet(w); err != nil { + return err + } return nil }, diff --git a/crypto/key_test.go b/crypto/key_test.go index 02b91fa..a419613 100644 --- a/crypto/key_test.go +++ b/crypto/key_test.go @@ -21,7 +21,7 @@ func TestExtendedSigningKeyWithoutPassphrase(t *testing.T) { got := NewXPrvKeyFromEntropy(entropy, "") want, _ := hex.DecodeString(masterKeyWithoutPassphrase) - if bytes.Compare(got, want) != 0 { + if !bytes.Equal(got, want) { t.Errorf("invalid master key\ngot: %x\nwant: %x\n", got, want) } } @@ -32,7 +32,7 @@ func TestExtendedSigningKeyWithPassphrase(t *testing.T) { got := NewXPrvKeyFromEntropy(entropy, passphrase) want, _ := hex.DecodeString(masterKeyWithPassphrase) - if bytes.Compare(got, want) != 0 { + if !bytes.Equal(got, want) { t.Errorf("invalid master key\ngot: %x\nwant: %x\n", got, want) } } diff --git a/internal/cbor/decode.go b/internal/cbor/decode.go index 57cb2c8..09a59f2 100644 --- a/internal/cbor/decode.go +++ b/internal/cbor/decode.go @@ -40,17 +40,17 @@ import ( // To unmarshal CBOR into an empty interface value, Unmarshal uses the // following rules: // -// CBOR booleans decode to bool. -// CBOR positive integers decode to uint64. -// CBOR negative integers decode to int64 (big.Int if value overflows). -// CBOR floating points decode to float64. -// CBOR byte strings decode to []byte. -// CBOR text strings decode to string. -// CBOR arrays decode to []interface{}. -// CBOR maps decode to map[interface{}]interface{}. -// CBOR null and undefined values decode to nil. -// CBOR times (tag 0 and 1) decode to time.Time. -// CBOR bignums (tag 2 and 3) decode to big.Int. +// CBOR booleans decode to bool. +// CBOR positive integers decode to uint64. +// CBOR negative integers decode to int64 (big.Int if value overflows). +// CBOR floating points decode to float64. +// CBOR byte strings decode to []byte. +// CBOR text strings decode to string. +// CBOR arrays decode to []interface{}. +// CBOR maps decode to map[interface{}]interface{}. +// CBOR null and undefined values decode to nil. +// CBOR times (tag 0 and 1) decode to time.Time. +// CBOR bignums (tag 2 and 3) decode to big.Int. // // To unmarshal a CBOR array into a slice, Unmarshal allocates a new slice // if the CBOR array is empty or slice capacity is less than CBOR array length. @@ -75,9 +75,9 @@ import ( // To unmarshal a CBOR map into a struct, Unmarshal matches CBOR map keys to the // keys in the following priority: // -// 1. "cbor" key in struct field tag, -// 2. "json" key in struct field tag, -// 3. struct field name. +// 1. "cbor" key in struct field tag, +// 2. "json" key in struct field tag, +// 3. struct field name. // // Unmarshal tries an exact match for field name, then a case-insensitive match. // Map key-value pairs without corresponding struct fields are ignored. See @@ -728,8 +728,6 @@ func (d *decoder) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolin } v.Set(reflect.ValueOf(NewByteString(rb.Bytes()))) return nil - } else { - return fillByteString(t, b, v) } return fillByteString(t, b, v) case cborTypeTextString: diff --git a/protocol.go b/protocol.go index 54ff274..69ecd4d 100644 --- a/protocol.go +++ b/protocol.go @@ -19,10 +19,10 @@ type ProtocolParams struct { ProtocolVersion ProtocolVersion MinPoolCost Coin CoinsPerUTXOWord Coin - CostModels interface{} - ExecutionCosts interface{} - MaxTxExUnits interface{} - MaxBlockTxExUnits interface{} + CostModels any + ExecutionCosts any + MaxTxExUnits any + MaxBlockTxExUnits any MaxValueSize uint CollateralPercentage uint MaxCollateralInputs uint diff --git a/script.go b/script.go index 35c5073..2ab8168 100644 --- a/script.go +++ b/script.go @@ -85,8 +85,7 @@ func (ns *NativeScript) Hash() (Hash28, error) { if err != nil { return nil, err } - bytes = append([]byte{byte(NativeScriptNamespace)}, bytes...) - return Blake224Hash(append(bytes)) + return Blake224Hash(append([]byte{byte(NativeScriptNamespace)}, bytes...)) } // Bytes returns the CBOR encoding of the script as bytes. diff --git a/wallet/db.go b/wallet/db.go index 7b9a2a7..b4cef17 100644 --- a/wallet/db.go +++ b/wallet/db.go @@ -64,7 +64,9 @@ func (bdb *badgerDB) Get() ([]*Wallet, error) { return err } wallet := &Wallet{} - wallet.unmarshal(value) + if err := wallet.unmarshal(value); err != nil { + return err + } wallets = append(wallets, wallet) } return nil diff --git a/wallet/wallet.go b/wallet/wallet.go index f1a0695..86570ae 100644 --- a/wallet/wallet.go +++ b/wallet/wallet.go @@ -40,12 +40,15 @@ func (w *Wallet) Transfer(receiver cardano.Address, amount *cardano.Value) (*car } if cmp := balance.Cmp(amount); cmp == -1 || cmp == 2 { - return nil, fmt.Errorf("Not enough balance, %v > %v", amount, balance) + return nil, fmt.Errorf("not enough balance, %v > %v", amount, balance) } // Find utxos that cover the amount to transfer pickedUtxos := []cardano.UTxO{} utxos, err := w.findUtxos() + if err != nil { + return nil, fmt.Errorf("cannot find utxos: %v", err) + } pickedUtxosAmount := cardano.NewValue(0) for _, utxo := range utxos { if pickedUtxosAmount.Cmp(amount) == 1 {