diff --git a/crypto/hd/algorithm.go b/crypto/hd/algorithm.go index 48688dcb..618caa4a 100644 --- a/crypto/hd/algorithm.go +++ b/crypto/hd/algorithm.go @@ -1,14 +1,16 @@ package hd import ( + "github.com/btcsuite/btcd/btcutil/hdkeychain" "github.com/btcsuite/btcd/chaincfg" - "github.com/btcsuite/btcutil/hdkeychain" + bip39 "github.com/tyler-smith/go-bip39" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/crypto" + "github.com/cosmos/cosmos-sdk/crypto/hd" "github.com/cosmos/cosmos-sdk/crypto/keyring" cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types" - "github.com/ethereum/go-ethereum/accounts" - "github.com/ethereum/go-ethereum/crypto" - bip39 "github.com/tyler-smith/go-bip39" "github.com/haqq-network/haqq/crypto/ethsecp256k1" ) diff --git a/crypto/hd/algorithm_test.go b/crypto/hd/algorithm_test.go index fdf7c226..dce1bb9c 100644 --- a/crypto/hd/algorithm_test.go +++ b/crypto/hd/algorithm_test.go @@ -7,11 +7,11 @@ import ( "github.com/stretchr/testify/require" + "github.com/ethereum/go-ethereum/common" + amino "github.com/cosmos/cosmos-sdk/codec" "github.com/cosmos/cosmos-sdk/codec/types" "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/ethereum/go-ethereum/common" - hdwallet "github.com/miguelmota/go-ethereum-hdwallet" cryptocodec "github.com/haqq-network/haqq/crypto/codec" enccodec "github.com/haqq-network/haqq/encoding/codec" @@ -73,11 +73,11 @@ func TestKeyring(t *testing.T) { addr := common.BytesToAddress(privkey.PubKey().Address().Bytes()) os.Setenv(hdWalletFixEnv, "true") - wallet, err := hdwallet.NewFromMnemonic(mnemonic) + wallet, err := NewFromMnemonic(mnemonic) os.Setenv(hdWalletFixEnv, "") require.NoError(t, err) - path := hdwallet.MustParseDerivationPath(hdPath) + path := MustParseDerivationPath(hdPath) account, err := wallet.Derive(path, false) require.NoError(t, err) @@ -100,14 +100,14 @@ func TestDerivation(t *testing.T) { require.False(t, privkey.Equals(badPrivKey)) - wallet, err := hdwallet.NewFromMnemonic(mnemonic) + wallet, err := NewFromMnemonic(mnemonic) require.NoError(t, err) - path := hdwallet.MustParseDerivationPath(haqqtypes.BIP44HDPath) + path := MustParseDerivationPath(haqqtypes.BIP44HDPath) account, err := wallet.Derive(path, false) require.NoError(t, err) - badPath := hdwallet.MustParseDerivationPath("44'/60'/0'/0/0") + badPath := MustParseDerivationPath("44'/60'/0'/0/0") badAccount, err := wallet.Derive(badPath, false) require.NoError(t, err) @@ -116,15 +116,15 @@ func TestDerivation(t *testing.T) { require.Equal(t, badAccount.Address.String(), "0xF8D6FDf2B8b488ea37e54903750dcd13F67E71cb") // Inequality of wrong derivation path address require.NotEqual(t, account.Address.String(), badAccount.Address.String()) - // Equality of Haqq Network implementation + // Equality of Haqq implementation require.Equal(t, common.BytesToAddress(privkey.PubKey().Address().Bytes()).String(), "0xA588C66983a81e800Db4dF74564F09f91c026351") require.Equal(t, common.BytesToAddress(badPrivKey.PubKey().Address().Bytes()).String(), "0xF8D6FDf2B8b488ea37e54903750dcd13F67E71cb") - // Equality of Eth and Haqq Network implementation + // Equality of Eth and Haqq implementation require.Equal(t, common.BytesToAddress(privkey.PubKey().Address()).String(), account.Address.String()) require.Equal(t, common.BytesToAddress(badPrivKey.PubKey().Address()).String(), badAccount.Address.String()) - // Inequality of wrong derivation path of Eth and Haqq Network implementation + // Inequality of wrong derivation path of Eth and Haqq implementation require.NotEqual(t, common.BytesToAddress(privkey.PubKey().Address()).String(), badAccount.Address.String()) require.NotEqual(t, common.BytesToAddress(badPrivKey.PubKey().Address()).String(), account.Address.Hex()) } diff --git a/crypto/hd/benchmark_test.go b/crypto/hd/benchmark_test.go index d208b06a..5317ea18 100644 --- a/crypto/hd/benchmark_test.go +++ b/crypto/hd/benchmark_test.go @@ -4,7 +4,6 @@ import ( "testing" "github.com/cosmos/cosmos-sdk/crypto/keyring" - "github.com/haqq-network/haqq/types" ) diff --git a/crypto/hd/utils_test.go b/crypto/hd/utils_test.go new file mode 100644 index 00000000..2082368d --- /dev/null +++ b/crypto/hd/utils_test.go @@ -0,0 +1,181 @@ +// NOTE: This code is being used as test helper functions. +package hd + +import ( + "crypto/ecdsa" + "errors" + "os" + "sync" + + "github.com/ethereum/go-ethereum/accounts" + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/crypto" + + "github.com/btcsuite/btcd/btcutil/hdkeychain" + "github.com/btcsuite/btcd/chaincfg" + bip39 "github.com/tyler-smith/go-bip39" +) + +const issue179FixEnvar = "GO_ETHEREUM_HDWALLET_FIX_ISSUE_179" + +// Wallet is the underlying wallet struct. +type Wallet struct { + mnemonic string + masterKey *hdkeychain.ExtendedKey + seed []byte + paths map[common.Address]accounts.DerivationPath + accounts []accounts.Account + stateLock sync.RWMutex + fixIssue172 bool +} + +// NewFromMnemonic returns a new wallet from a BIP-39 mnemonic. +func NewFromMnemonic(mnemonic string) (*Wallet, error) { + if mnemonic == "" { + return nil, errors.New("mnemonic is required") + } + + if !bip39.IsMnemonicValid(mnemonic) { + return nil, errors.New("mnemonic is invalid") + } + + seed, err := NewSeedFromMnemonic(mnemonic) + if err != nil { + return nil, err + } + + wallet, err := newWallet(seed) + if err != nil { + return nil, err + } + wallet.mnemonic = mnemonic + + return wallet, nil +} + +// NewSeedFromMnemonic returns a BIP-39 seed based on a BIP-39 mnemonic. +func NewSeedFromMnemonic(mnemonic string) ([]byte, error) { + if mnemonic == "" { + return nil, errors.New("mnemonic is required") + } + + return bip39.NewSeedWithErrorChecking(mnemonic, "") +} + +func newWallet(seed []byte) (*Wallet, error) { + masterKey, err := hdkeychain.NewMaster(seed, &chaincfg.MainNetParams) + if err != nil { + return nil, err + } + + return &Wallet{ + masterKey: masterKey, + seed: seed, + accounts: []accounts.Account{}, + paths: map[common.Address]accounts.DerivationPath{}, + fixIssue172: false || len(os.Getenv(issue179FixEnvar)) > 0, + }, nil +} + +// Derive implements accounts.Wallet, deriving a new account at the specific +// derivation path. If pin is set to true, the account will be added to the list +// of tracked accounts. +func (w *Wallet) Derive(path accounts.DerivationPath, pin bool) (accounts.Account, error) { + // Try to derive the actual account and update its URL if successful + w.stateLock.RLock() // Avoid device disappearing during derivation + + address, err := w.deriveAddress(path) + + w.stateLock.RUnlock() + + // If an error occurred or no pinning was requested, return + if err != nil { + return accounts.Account{}, err + } + + account := accounts.Account{ + Address: address, + URL: accounts.URL{ + Scheme: "", + Path: path.String(), + }, + } + + if !pin { + return account, nil + } + + // Pinning needs to modify the state + w.stateLock.Lock() + defer w.stateLock.Unlock() + + if _, ok := w.paths[address]; !ok { + w.accounts = append(w.accounts, account) + w.paths[address] = path + } + + return account, nil +} + +// MustParseDerivationPath parses the derivation path in string format into +// []uint32 but will panic if it can't parse it. +func MustParseDerivationPath(path string) accounts.DerivationPath { + parsed, err := accounts.ParseDerivationPath(path) + if err != nil { + panic(err) + } + + return parsed +} + +// DerivePrivateKey derives the private key of the derivation path. +func (w *Wallet) derivePrivateKey(path accounts.DerivationPath) (*ecdsa.PrivateKey, error) { + var err error + key := w.masterKey + for _, n := range path { + if w.fixIssue172 && key.IsAffectedByIssue172() { + key, err = key.Derive(n) + } else { + //lint:ignore SA1019 this is used for testing only + key, err = key.DeriveNonStandard(n) //nolint:staticcheck + } + if err != nil { + return nil, err + } + } + + privateKey, err := key.ECPrivKey() + privateKeyECDSA := privateKey.ToECDSA() + if err != nil { + return nil, err + } + + return privateKeyECDSA, nil +} + +// derivePublicKey derives the public key of the derivation path. +func (w *Wallet) derivePublicKey(path accounts.DerivationPath) (*ecdsa.PublicKey, error) { + privateKeyECDSA, err := w.derivePrivateKey(path) + if err != nil { + return nil, err + } + + publicKey := privateKeyECDSA.Public() + publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey) + if !ok { + return nil, errors.New("failed to get public key") + } + + return publicKeyECDSA, nil +} + +// DeriveAddress derives the account address of the derivation path. +func (w *Wallet) deriveAddress(path accounts.DerivationPath) (common.Address, error) { + publicKeyECDSA, err := w.derivePublicKey(path) + if err != nil { + return common.Address{}, err + } + + address := crypto.PubkeyToAddress(*publicKeyECDSA) + return address, nil +} diff --git a/go.mod b/go.mod index f5894e88..f90eabd3 100644 --- a/go.mod +++ b/go.mod @@ -1,13 +1,13 @@ module github.com/haqq-network/haqq -go 1.18 +go 1.20 require ( cosmossdk.io/errors v1.0.0-beta.7 cosmossdk.io/math v1.0.0-rc.0 github.com/armon/go-metrics v0.4.1 - github.com/btcsuite/btcd v0.22.2 - github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce + github.com/btcsuite/btcd v0.23.4 + github.com/btcsuite/btcd/btcutil v1.1.3 github.com/cosmos/cosmos-proto v1.0.0-beta.3 github.com/cosmos/cosmos-sdk v0.46.13 github.com/cosmos/go-bip39 v1.0.0 @@ -22,7 +22,6 @@ require ( github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/hashicorp/go-version v1.6.0 github.com/improbable-eng/grpc-web v0.15.0 - github.com/miguelmota/go-ethereum-hdwallet v0.1.1 github.com/onsi/ginkgo/v2 v2.9.0 github.com/onsi/gomega v1.27.2 github.com/ory/dockertest/v3 v3.9.1 @@ -64,6 +63,7 @@ require ( github.com/StackExchange/wmi v1.2.1 // indirect github.com/VictoriaMetrics/fastcache v1.6.0 // indirect github.com/Workiva/go-datastructures v1.0.53 // indirect + github.com/allegro/bigcache v1.2.1 // indirect github.com/aws/aws-sdk-go v1.44.122 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect @@ -224,7 +224,8 @@ replace ( // use cosmos fork of keyring github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 // use Cosmos-SDK fork to enable Ledger functionality - github.com/cosmos/cosmos-sdk => github.com/haqq-network/cosmos-sdk v0.46.13-haqq.4 + // github.com/cosmos/cosmos-sdk => github.com/haqq-network/cosmos-sdk v0.46.13-haqq.4 + github.com/cosmos/cosmos-sdk => github.com/evmos/cosmos-sdk v0.46.13-ledger.3 // use Evmos geth fork github.com/ethereum/go-ethereum => github.com/evmos/go-ethereum v1.10.26 // use cosmos flavored protobufs @@ -235,5 +236,6 @@ replace ( // replace broken goleveldb github.com/syndtr/goleveldb => github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 // use cometbft - github.com/tendermint/tendermint => github.com/haqq-network/cometbft v0.34.29-haqq.0 + // github.com/tendermint/tendermint => github.com/haqq-network/cometbft v0.34.29-haqq.0 + github.com/tendermint/tendermint => github.com/cometbft/cometbft v0.34.29 ) diff --git a/go.sum b/go.sum index 7b98de37..db41bc8a 100644 --- a/go.sum +++ b/go.sum @@ -286,22 +286,28 @@ github.com/bmizerany/pat v0.0.0-20170815010413-6226ea591a40/go.mod h1:8rLXio+Wji github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps= github.com/btcsuite/btcd v0.0.0-20190315201642-aa6e0f35703c/go.mod h1:DrZx5ec/dmnfpw9KyYoQyYo7d0KEvTkk/5M/vbZjAr8= github.com/btcsuite/btcd v0.20.1-beta/go.mod h1:wVuoA8VJLEcwgqHBwHmzLRazpKxTv13Px/pDuV7OomQ= -github.com/btcsuite/btcd v0.21.0-beta/go.mod h1:ZSWyehm27aAuS9bvkATT+Xte3hjHZ+MRgMY/8NJ7K94= github.com/btcsuite/btcd v0.21.0-beta.0.20201114000516-e9c7a5ac6401/go.mod h1:Sv4JPQ3/M+teHz9Bo5jBpkNcP0x6r7rdihlNL/7tTAs= +github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M= github.com/btcsuite/btcd v0.22.1/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y= -github.com/btcsuite/btcd v0.22.2 h1:vBZ+lGGd1XubpOWO67ITJpAEsICWhA0YzqkcpkgNBfo= -github.com/btcsuite/btcd v0.22.2/go.mod h1:wqgTSL29+50LRkmOVknEdmt8ZojIzhuWvgu/iptuN7Y= +github.com/btcsuite/btcd v0.23.0/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY= +github.com/btcsuite/btcd v0.23.4 h1:IzV6qqkfwbItOS/sg/aDfPDsjPP8twrCOE2R93hxMlQ= +github.com/btcsuite/btcd v0.23.4/go.mod h1:0QJIIN1wwIXF/3G/m87gIwGniDMDQqjVn4SZgnFpsYY= +github.com/btcsuite/btcd/btcec/v2 v2.1.0/go.mod h1:2VzYrv4Gm4apmbVVsSq5bqf1Ec8v56E48Vt0Y/umPgA= +github.com/btcsuite/btcd/btcec/v2 v2.1.3/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE= github.com/btcsuite/btcd/btcec/v2 v2.2.0/go.mod h1:U7MHm051Al6XmscBQ0BoNydpOTsFAn707034b5nY8zU= github.com/btcsuite/btcd/btcec/v2 v2.3.2 h1:5n0X6hX0Zk+6omWcihdYvdAlGf2DfasC0GMf7DClJ3U= github.com/btcsuite/btcd/btcec/v2 v2.3.2/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= -github.com/btcsuite/btcd/btcutil v1.1.2 h1:XLMbX8JQEiwMcYft2EGi8zPUkoa0abKIU6/BJSRsjzQ= +github.com/btcsuite/btcd/btcutil v1.0.0/go.mod h1:Uoxwv0pqYWhD//tfTiipkxNfdhG9UrLwaeswfjfdF0A= +github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUBTwmWH/0Jn8VHE= +github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ= +github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0= +github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc= github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/btcutil v0.0.0-20190207003914-4c204d697803/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg= github.com/btcsuite/btcutil v1.0.2/go.mod h1:j9HUFwoQRsZL3V4n+qG+CUnEGHOarIxfC3Le2Yhbcts= -github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce h1:YtWJF7RHm2pYCvA5t0RPmAaLUhREsKuKd+SLhxFbFeQ= github.com/btcsuite/btcutil v1.0.3-0.20201208143702-a53e38424cce/go.mod h1:0DVlHczLPewLcPGEIeUEzfOJhqGPQ0mJJRDBtD307+o= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= github.com/btcsuite/goleveldb v0.0.0-20160330041536-7834afc9e8cd/go.mod h1:F+uVaaLLH7j4eDXPRvw78tMflu7Ie2bzYOH4Y8rRKBY= @@ -357,6 +363,8 @@ github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE github.com/coinbase/kryptology v1.8.0/go.mod h1:RYXOAPdzOGUe3qlSFkMGn58i3xUA8hmxYHksuq+8ciI= github.com/coinbase/rosetta-sdk-go v0.7.9 h1:lqllBjMnazTjIqYrOGv8h8jxjg9+hJazIGZr9ZvoCcA= github.com/coinbase/rosetta-sdk-go v0.7.9/go.mod h1:0/knutI7XGVqXmmH4OQD8OckFrbQ8yMsUZTG7FXCR2M= +github.com/cometbft/cometbft v0.34.29 h1:Q4FqMevP9du2pOgryZJHpDV2eA6jg/kMYxBj9ZTY6VQ= +github.com/cometbft/cometbft v0.34.29/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw= github.com/cometbft/cometbft-db v0.7.0 h1:uBjbrBx4QzU0zOEnU8KxoDl18dMNgDh+zZRUE0ucsbo= github.com/cometbft/cometbft-db v0.7.0/go.mod h1:yiKJIm2WKrt6x8Cyxtq9YTEcIMPcEe4XPxhgX59Fzf0= github.com/confio/ics23/go v0.9.0 h1:cWs+wdbS2KRPZezoaaj+qBleXgUk5WOQFMP3CQFGTr4= @@ -477,6 +485,8 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.m github.com/envoyproxy/go-control-plane v0.9.10-0.20210907150352-cf90f659a021/go.mod h1:AFq3mo9L8Lqqiid3OhADV3RfLJnjiw63cSpi+fDTRC0= github.com/envoyproxy/go-control-plane v0.10.2-0.20220325020618-49ff273808a1/go.mod h1:KJwIaB5Mv44NWtYuAOFCVOjcI94vtpEz2JU/D2v6IjE= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/evmos/cosmos-sdk v0.46.13-ledger.3 h1:GYoD+tM3lpn6TTRra6YCcfrxCXO39ATHWwvZkl53Mv4= +github.com/evmos/cosmos-sdk v0.46.13-ledger.3/go.mod h1:EfY521ATNEla8eJ6oJuZBdgP5+p360s7InnRqX+TWdM= github.com/evmos/go-ethereum v1.10.26 h1:7wlczxUWTwhzJJUyh3Kkqt3/5fdSJzh8c42boc9GuII= github.com/evmos/go-ethereum v1.10.26/go.mod h1:EYFyF19u3ezGLD4RqOkLq+ZCXzYbLoNDdZlMt7kyKFg= github.com/facebookgo/ensure v0.0.0-20200202191622-63f1cf65ac4c h1:8ISkoahWXwZR41ois5lSJBSVw4D0OV19Ht/JSTzvSv0= @@ -720,10 +730,6 @@ github.com/gtank/merlin v0.1.1 h1:eQ90iG7K9pOhtereWsmyRJ6RAwcP4tHTDBHXNg+u5is= github.com/gtank/merlin v0.1.1/go.mod h1:T86dnYJhcGOh5BjZFCJWTDeTK7XW8uE+E21Cy/bIQ+s= github.com/gtank/ristretto255 v0.1.2 h1:JEqUCPA1NvLq5DwYtuzigd7ss8fwbYay9fi4/5uMzcc= github.com/gtank/ristretto255 v0.1.2/go.mod h1:Ph5OpO6c7xKUGROZfWVLiJf9icMDwUeIvY4OmlYW69o= -github.com/haqq-network/cometbft v0.34.29-haqq.0 h1:183O+iia8xi56xRCP/k1/iY2V5H459WkGILT+8zAkg0= -github.com/haqq-network/cometbft v0.34.29-haqq.0/go.mod h1:L9shMfbkZ8B+7JlwANEr+NZbBcn+hBpwdbeYvA5rLCw= -github.com/haqq-network/cosmos-sdk v0.46.13-haqq.4 h1:VXpIzDyKmGbhw/e3+yDLTQJKf2MBBl5djJ11YJZD728= -github.com/haqq-network/cosmos-sdk v0.46.13-haqq.4/go.mod h1:+gsmp/TvbqpIJNyheeAHJSKeJZPcq8OP/TQU7GuLP00= github.com/hashicorp/consul/api v1.3.0/go.mod h1:MmDNSzIMUjNpY/mQ398R4bk2FnqQLoPndWW5VkKPlCE= github.com/hashicorp/consul/sdk v0.3.0/go.mod h1:VKf9jXwCTEY1QZP2MOLRhb5i/I/ssyNV1vwHyQBF0x8= github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= @@ -903,8 +909,6 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5 github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo= github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg= -github.com/miguelmota/go-ethereum-hdwallet v0.1.1 h1:zdXGlHao7idpCBjEGTXThVAtMKs+IxAgivZ75xqkWK0= -github.com/miguelmota/go-ethereum-hdwallet v0.1.1/go.mod h1:f9m9uXokAHA6WNoYOPjj4AqjJS5pquQRiYYj/XSyPYc= github.com/mimoo/StrobeGo v0.0.0-20181016162300-f8f6d4d2b643/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0 h1:QRUSJEgZn2Snx0EmT/QLXibWjSUDjKWvXIT19NBVp94= github.com/mimoo/StrobeGo v0.0.0-20210601165009-122bf33a46e0/go.mod h1:43+3pMjjKimDBf5Kr4ZFNGbLql1zKkbImw+fZbw3geM= diff --git a/tests/e2e/upgrade/govexec.go b/tests/e2e/upgrade/govexec.go index 3337e840..52a3d549 100644 --- a/tests/e2e/upgrade/govexec.go +++ b/tests/e2e/upgrade/govexec.go @@ -27,9 +27,10 @@ func (m *Manager) CreateExec(cmd []string, containerID string) (string, error) { Context: ctx, AttachStdout: true, AttachStderr: true, - User: "root", - Container: containerID, - Cmd: cmd, + // deepcode ignore NoHardcodedCredentials/test: + User: "root", + Container: containerID, + Cmd: cmd, }) if err != nil { return "", err diff --git a/tests/e2e/upgrade/utils.go b/tests/e2e/upgrade/utils.go index 953d6181..dbd296a4 100644 --- a/tests/e2e/upgrade/utils.go +++ b/tests/e2e/upgrade/utils.go @@ -70,6 +70,7 @@ func RetrieveUpgradesList(upgradesPath string) ([]string, error) { // creating path to upgrade dir file with constant upgrade version constantsPath := fmt.Sprintf("%s/%s/constants.go", upgradesPath, d.Name()) + // deepcode ignore PT/test: f, err := os.ReadFile(constantsPath) if err != nil { return nil, err diff --git a/x/coinomics/keeper/inflation.go b/x/coinomics/keeper/inflation.go index d021db38..6343fbaa 100644 --- a/x/coinomics/keeper/inflation.go +++ b/x/coinomics/keeper/inflation.go @@ -1,6 +1,7 @@ package keeper import ( + "fmt" "log" "cosmossdk.io/math" @@ -41,13 +42,6 @@ func (k Keeper) MintAndAllocate(ctx sdk.Context) error { // totalBonded * rewardCoefficient * ((currentBlockTS - prevBlockTS) / yearInMillis) blockMint := totalBonded.Mul(rewardCoefficient).Mul((currentBlockTS.Sub(prevBlockTS)).Quo(yearInMillis)) - log.Printf("totalBonded: %s", totalBonded.String()) - log.Printf("rewardCoefficient: %s", rewardCoefficient.String()) - log.Printf("currentBlockTS: %s", currentBlockTS.String()) - log.Printf("prevBlockTS: %s", prevBlockTS.String()) - log.Printf("yearInMillis: %s", yearInMillis.String()) - log.Printf("blockMint #1: %s", blockMint.String()) - bankTotalSupply, _ := sdk.NewDecFromStr(k.bankKeeper.GetSupply(ctx, params.MintDenom).Amount.String()) maxSupply, _ := sdk.NewDecFromStr(k.GetMaxSupply(ctx).Amount.String()) @@ -62,11 +56,14 @@ func (k Keeper) MintAndAllocate(ctx sdk.Context) error { } if blockMint.IsNegative() { + // state is corrupted + errStr := fmt.Sprintf("MintAndAllocate # blockMint is negative # blockMint: %s, totalBonded: %s, rewardCoefficient: %s, currentBlockTS: %s, prevBlockTS: %s, yearInMillis: %s", blockMint.String(), totalBonded.String(), rewardCoefficient.String(), currentBlockTS.String(), prevBlockTS.String(), yearInMillis.String()) + + ctx.Logger().Error(errStr) + return nil } - log.Printf("blockMint #2: %s", blockMint.String()) - // Mint and allocate the calculated coin amount totalMintOnBlockCoin := sdk.NewCoin(params.MintDenom, blockMint.RoundInt()) if err := k.MintCoins(ctx, totalMintOnBlockCoin); err != nil { diff --git a/x/coinomics/keeper/keeper.go b/x/coinomics/keeper/keeper.go index 0579f8b0..4bb77015 100644 --- a/x/coinomics/keeper/keeper.go +++ b/x/coinomics/keeper/keeper.go @@ -1,6 +1,7 @@ package keeper import ( + sdkmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/codec" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" @@ -61,6 +62,6 @@ func (k Keeper) Logger(ctx sdk.Context) log.Logger { return ctx.Logger().With("module", "x/"+types.ModuleName) } -func (k Keeper) TokenSupply(ctx sdk.Context, denom string) sdk.Int { +func (k Keeper) TokenSupply(ctx sdk.Context, denom string) sdkmath.Int { return k.bankKeeper.GetSupply(ctx, denom).Amount } diff --git a/x/coinomics/keeper/mint_info.go b/x/coinomics/keeper/mint_info.go index 9f7931fe..e08702dc 100644 --- a/x/coinomics/keeper/mint_info.go +++ b/x/coinomics/keeper/mint_info.go @@ -3,19 +3,20 @@ package keeper import ( "fmt" + sdkmath "cosmossdk.io/math" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/haqq-network/haqq/x/coinomics/types" ) -func (k Keeper) GetPrevBlockTS(ctx sdk.Context) sdk.Int { +func (k Keeper) GetPrevBlockTS(ctx sdk.Context) sdkmath.Int { store := ctx.KVStore(k.storeKey) bz := store.Get(types.KeyPrefixPrevBlockTS) if len(bz) == 0 { - return sdk.ZeroInt() + return sdkmath.ZeroInt() } - var prevBlockTSValue sdk.Int + var prevBlockTSValue sdkmath.Int err := prevBlockTSValue.Unmarshal(bz) if err != nil { panic(fmt.Errorf("unable to unmarshal prevBlockTSValue value: %w", err)) @@ -24,7 +25,7 @@ func (k Keeper) GetPrevBlockTS(ctx sdk.Context) sdk.Int { return prevBlockTSValue } -func (k Keeper) SetPrevBlockTS(ctx sdk.Context, prevBlockTS sdk.Int) { +func (k Keeper) SetPrevBlockTS(ctx sdk.Context, prevBlockTS sdkmath.Int) { binaryInfValue, err := prevBlockTS.Marshal() if err != nil { panic(fmt.Errorf("unable to marshal amount value: %w", err))