From 53bbce84c80441aeb6c6c3c7ab944a47d29a8739 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Sun, 9 Jun 2019 00:01:11 -0700 Subject: [PATCH] Convert most benign uses of Hex to MustAddressToBech32 --- api/service/explorer/structs.go | 4 ++-- api/service/explorer/structs_test.go | 2 +- cmd/client/wallet/main.go | 16 ++++++++-------- cmd/harmony/main.go | 7 ++++--- cmd/keygen/main.go | 4 +++- consensus/consensus.go | 3 ++- consensus/consensus_service.go | 7 ++++--- consensus/consensus_service_test.go | 3 ++- core/state/statedb_test.go | 5 +++-- core/types/shard_state.go | 3 ++- drand/drand.go | 5 +++-- drand/drand_test.go | 4 +++- internal/hmyapi/util.go | 3 ++- node/contract.go | 6 ++++-- 14 files changed, 43 insertions(+), 29 deletions(-) diff --git a/api/service/explorer/structs.go b/api/service/explorer/structs.go index 6445a526c..18550bccd 100644 --- a/api/service/explorer/structs.go +++ b/api/service/explorer/structs.go @@ -94,8 +94,8 @@ func GetTransaction(tx *types.Transaction, accountBlock *types.Block) *Transacti return &Transaction{ ID: tx.Hash().Hex(), Timestamp: strconv.Itoa(int(accountBlock.Time().Int64() * 1000)), - From: msg.From().Hex(), - To: msg.To().Hex(), + From: msg.From().Hex(), // TODO ek – use bech32 + To: msg.To().Hex(), // TODO ek – use bech32 Value: msg.Value(), Bytes: strconv.Itoa(int(tx.Size())), Data: hex.EncodeToString(tx.Data()), diff --git a/api/service/explorer/structs_test.go b/api/service/explorer/structs_test.go index 954bd23e0..31ecb7af6 100644 --- a/api/service/explorer/structs_test.go +++ b/api/service/explorer/structs_test.go @@ -22,6 +22,6 @@ func TestGetTransaction(t *testing.T) { tx := GetTransaction(tx1, block) assert.Equal(t, tx.ID, tx1.Hash().Hex(), "should be equal tx1.Hash()") - assert.Equal(t, tx.To, tx1.To().Hex(), "should be equal tx1.To()") + assert.Equal(t, tx.To, tx1.To().Hex(), "should be equal tx1.To()") // TODO ek – use bech32 assert.Equal(t, tx.Bytes, strconv.Itoa(int(tx1.Size())), "should be equal tx1.Size()") } diff --git a/cmd/client/wallet/main.go b/cmd/client/wallet/main.go index c7d274501..0dc91a92a 100644 --- a/cmd/client/wallet/main.go +++ b/cmd/client/wallet/main.go @@ -272,19 +272,19 @@ func processNewCommnad() { if err != nil { fmt.Printf("new account error: %v\n", err) } - fmt.Printf("account: %s\n", account.Address.Hex()) + fmt.Printf("account: %s\n", common2.MustAddressToBech32(account.Address)) fmt.Printf("URL: %s\n", account.URL) } func _exportAccount(account accounts.Account) { - fmt.Printf("account: %s\n", account.Address.Hex()) + fmt.Printf("account: %s\n", common2.MustAddressToBech32(account.Address)) fmt.Printf("URL: %s\n", account.URL) pass := utils.AskForPassphrase("Original Passphrase: ") newpass := utils.AskForPassphrase("Export Passphrase: ") data, err := ks.Export(account, pass, newpass) if err == nil { - filename := fmt.Sprintf(".hmy/%s.key", account.Address.Hex()) + filename := fmt.Sprintf(".hmy/%s.key", common2.MustAddressToBech32(account.Address)) f, err := os.OpenFile(filename, os.O_CREATE|os.O_WRONLY, 0644) if err != nil { panic("Failed to open keystore") @@ -316,7 +316,7 @@ func processListCommand() { allAccounts := ks.Accounts() for _, account := range allAccounts { - fmt.Printf("account: %s\n", account.Address.Hex()) + fmt.Printf("account: %s\n", common2.MustAddressToBech32(account.Address)) fmt.Printf("URL: %s\n", account.URL) } } @@ -330,7 +330,7 @@ func processExportCommand() { allAccounts := ks.Accounts() for _, account := range allAccounts { - if acc == "" || acc == account.Address.Hex() { + if acc == "" || acc == common2.MustAddressToBech32(account.Address) { _exportAccount(account) } } @@ -360,7 +360,7 @@ func processImportCommnad() { if err != nil { panic("Failed to import the private key") } - fmt.Printf("Private key imported for account: %s\n", account.Address.Hex()) + fmt.Printf("Private key imported for account: %s\n", common2.MustAddressToBech32(account.Address)) } func processBalancesCommand() { @@ -373,14 +373,14 @@ func processBalancesCommand() { allAccounts := ks.Accounts() for i, account := range allAccounts { fmt.Printf("Account %d:\n", i) - fmt.Printf(" Address: %s\n", account.Address.Hex()) + fmt.Printf(" Address: %s\n", common2.MustAddressToBech32(account.Address)) for shardID, balanceNonce := range FetchBalance(account.Address) { fmt.Printf(" Balance in Shard %d: %s, nonce: %v \n", shardID, convertBalanceIntoReadableFormat(balanceNonce.balance), balanceNonce.nonce) } } } else { address := common2.ParseAddr(*balanceAddressPtr) - fmt.Printf("Account: %s:\n", address.Hex()) + fmt.Printf("Account: %s:\n", common2.MustAddressToBech32(address)) for shardID, balanceNonce := range FetchBalance(address) { fmt.Printf(" Balance in Shard %d: %s, nonce: %v \n", shardID, convertBalanceIntoReadableFormat(balanceNonce.balance), balanceNonce.nonce) } diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 063d498ee..716c84d0b 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -18,6 +18,7 @@ import ( "github.com/harmony-one/harmony/consensus" "github.com/harmony-one/harmony/core" "github.com/harmony-one/harmony/drand" + "github.com/harmony-one/harmony/internal/common" nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/genesis" @@ -173,7 +174,7 @@ func initSetup() { foundAccount := false for _, account := range allAccounts { - if genesisAccount.Address == account.Address.Hex() { + if common.ParseAddr(genesisAccount.Address) == account.Address { myAccount = account foundAccount = true break @@ -187,7 +188,7 @@ func initSetup() { genesisAccount.ShardID = uint32(accountIndex % core.GenesisShardNum) - fmt.Printf("My Account: %s\n", myAccount.Address.Hex()) + fmt.Printf("My Account: %s\n", common.MustAddressToBech32(myAccount.Address)) fmt.Printf("Key URL: %s\n", myAccount.URL) fmt.Printf("My Genesis Account: %v\n", *genesisAccount) @@ -313,7 +314,7 @@ func setUpConsensusAndNode(nodeConfig *nodeconfig.ConfigType) *node.Node { currentNode.NodeConfig.SetRole(nodeconfig.NewNode) currentNode.StakingAccount = myAccount utils.GetLogInstance().Info("node account set", - "address", currentNode.StakingAccount.Address.Hex()) + "address", common.MustAddressToBech32(currentNode.StakingAccount.Address)) if gsif, err := consensus.NewGenesisStakeInfoFinder(); err == nil { currentConsensus.SetStakeInfoFinder(gsif) diff --git a/cmd/keygen/main.go b/cmd/keygen/main.go index 6103ba5a7..d2407a3df 100644 --- a/cmd/keygen/main.go +++ b/cmd/keygen/main.go @@ -9,6 +9,8 @@ import ( "strconv" crypto2 "github.com/ethereum/go-ethereum/crypto" + + "github.com/harmony-one/harmony/internal/common" ) var ( @@ -38,7 +40,7 @@ func main() { } crypto2.FromECDSA(priKey) - fmt.Printf("{Address: \"%s\", Private: \"%s\", Public: \"%s\"},\n", crypto2.PubkeyToAddress(priKey.PublicKey).Hex(), hex.EncodeToString(crypto2.FromECDSA(priKey)), crypto2.PubkeyToAddress(priKey.PublicKey).Hex()) + fmt.Printf("{Address: \"%s\", Private: \"%s\", Public: \"%s\"},"+"\n", common.MustAddressToBech32(crypto2.PubkeyToAddress(priKey.PublicKey)), hex.EncodeToString(crypto2.FromECDSA(priKey)), common.MustAddressToBech32(crypto2.PubkeyToAddress(priKey.PublicKey))) } } else { fmt.Println("Unable to parse # as the argument.") diff --git a/consensus/consensus.go b/consensus/consensus.go index d0bb44aa2..01ed5eb7b 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -15,6 +15,7 @@ import ( "github.com/harmony-one/harmony/core/state" "github.com/harmony-one/harmony/core/types" bls_cosi "github.com/harmony-one/harmony/crypto/bls" + common2 "github.com/harmony-one/harmony/internal/common" nodeconfig "github.com/harmony-one/harmony/internal/configs/node" "github.com/harmony-one/harmony/internal/ctxerror" "github.com/harmony-one/harmony/internal/genesis" @@ -217,7 +218,7 @@ func New(host p2p.Host, ShardID uint32, leader p2p.Peer, blsPriKey *bls.SecretKe consensus.commitSigs = map[common.Address]*bls.Sign{} consensus.CommitteeAddresses = make(map[common.Address]bool) - consensus.validators.Store(utils.GetBlsAddress(leader.ConsensusPubKey).Hex(), leader) + consensus.validators.Store(common2.MustAddressToBech32(utils.GetBlsAddress(leader.ConsensusPubKey)), leader) // For now use socket address as ID // TODO: populate Id derived from address diff --git a/consensus/consensus_service.go b/consensus/consensus_service.go index bf7caa421..cf8c19438 100644 --- a/consensus/consensus_service.go +++ b/consensus/consensus_service.go @@ -8,6 +8,7 @@ import ( "time" "github.com/harmony-one/harmony/crypto/hash" + common2 "github.com/harmony-one/harmony/internal/common" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/rlp" @@ -380,7 +381,7 @@ func (consensus *Consensus) AddPeers(peers []*p2p.Peer) int { count := 0 for _, peer := range peers { - _, ok := consensus.validators.LoadOrStore(utils.GetBlsAddress(peer.ConsensusPubKey).Hex(), *peer) + _, ok := consensus.validators.LoadOrStore(common2.MustAddressToBech32(utils.GetBlsAddress(peer.ConsensusPubKey)), *peer) if !ok { consensus.pubKeyLock.Lock() if _, ok := consensus.CommitteeAddresses[peer.ConsensusPubKey.GetAddress()]; !ok { @@ -510,7 +511,7 @@ func (consensus *Consensus) verifySenderKey(msg *msg_pb.Message) (*bls.PublicKey senderAddr := common.BytesToAddress(addrBytes[:]) if !consensus.IsValidatorInCommittee(senderAddr) { - return nil, fmt.Errorf("Validator address %s is not in committee", senderAddr.Hex()) + return nil, fmt.Errorf("Validator address %s is not in committee", common2.MustAddressToBech32(senderAddr)) } return senderKey, nil } @@ -525,7 +526,7 @@ func (consensus *Consensus) verifyViewChangeSenderKey(msg *msg_pb.Message) (*bls senderAddr := common.BytesToAddress(addrBytes[:]) if !consensus.IsValidatorInCommittee(senderAddr) { - return nil, common.Address{}, fmt.Errorf("Validator address %s is not in committee", senderAddr.Hex()) + return nil, common.Address{}, fmt.Errorf("Validator address %s is not in committee", common2.MustAddressToBech32(senderAddr)) } return senderKey, senderAddr, nil } diff --git a/consensus/consensus_service_test.go b/consensus/consensus_service_test.go index fd3df7f66..c282ff6c3 100644 --- a/consensus/consensus_service_test.go +++ b/consensus/consensus_service_test.go @@ -7,6 +7,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/harmony/crypto/bls" + common2 "github.com/harmony-one/harmony/internal/common" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/internal/utils" @@ -28,7 +29,7 @@ func TestGetPeerFromID(t *testing.T) { t.Fatalf("Cannot craeate consensus: %v", err) } leaderAddress := utils.GetAddressFromBlsPubKey(leader.ConsensusPubKey) - l := consensus.GetPeerByAddress(leaderAddress.Hex()) + l := consensus.GetPeerByAddress(common2.MustAddressToBech32(leaderAddress)) if l.IP != leader.IP || l.Port != leader.Port { t.Errorf("leader IP not equal") } diff --git a/core/state/statedb_test.go b/core/state/statedb_test.go index edcae241d..b17e1fc31 100644 --- a/core/state/statedb_test.go +++ b/core/state/statedb_test.go @@ -33,6 +33,7 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethdb" "github.com/harmony-one/harmony/core/types" + common2 "github.com/harmony-one/harmony/internal/common" ) // Tests that updating a state trie does not leak any database writes prior to @@ -280,7 +281,7 @@ func newTestAction(addr common.Address, r *rand.Rand) testAction { action := actions[r.Intn(len(actions))] var nameargs []string if !action.noAddr { - nameargs = append(nameargs, addr.Hex()) + nameargs = append(nameargs, common2.MustAddressToBech32(addr)) } for _, i := range action.args { action.args[i] = rand.Int63n(100) @@ -366,7 +367,7 @@ func (test *snapshotTest) checkEqual(state, checkstate *DB) error { var err error checkeq := func(op string, a, b interface{}) bool { if err == nil && !reflect.DeepEqual(a, b) { - err = fmt.Errorf("got %s(%s) == %v, want %v", op, addr.Hex(), a, b) + err = fmt.Errorf("got %s(%s) == %v, want %v", op, common2.MustAddressToBech32(addr), a, b) return false } return true diff --git a/core/types/shard_state.go b/core/types/shard_state.go index 8dab0c683..912685e2a 100644 --- a/core/types/shard_state.go +++ b/core/types/shard_state.go @@ -9,6 +9,7 @@ import ( "github.com/harmony-one/bls/ffi/go/bls" "golang.org/x/crypto/sha3" + common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/ctxerror" ) @@ -208,5 +209,5 @@ func (n NodeID) Serialize() []byte { } func (n NodeID) String() string { - return "ECDSA: " + n.EcdsaAddress.Hex() + ", BLS: " + hex.EncodeToString(n.BlsPublicKey[:]) + return "ECDSA: " + common2.MustAddressToBech32(n.EcdsaAddress) + ", BLS: " + hex.EncodeToString(n.BlsPublicKey[:]) } diff --git a/drand/drand.go b/drand/drand.go index b8fd42076..9c1754b37 100644 --- a/drand/drand.go +++ b/drand/drand.go @@ -5,6 +5,7 @@ import ( "sync" "github.com/harmony-one/harmony/crypto/hash" + common2 "github.com/harmony-one/harmony/internal/common" "github.com/ethereum/go-ethereum/common" @@ -91,7 +92,7 @@ func New(host p2p.Host, ShardID uint32, peers []p2p.Peer, leader p2p.Peer, confi dRand.leader = leader dRand.CommitteeAddresses = map[common.Address]bool{} for _, peer := range peers { - dRand.validators.Store(utils.GetBlsAddress(peer.ConsensusPubKey).Hex(), peer) + dRand.validators.Store(common2.MustAddressToBech32(utils.GetBlsAddress(peer.ConsensusPubKey)), peer) dRand.CommitteeAddresses[utils.GetBlsAddress(peer.ConsensusPubKey)] = true } @@ -136,7 +137,7 @@ func (dRand *DRand) AddPeers(peers []*p2p.Peer) int { count := 0 for _, peer := range peers { - _, ok := dRand.validators.LoadOrStore(utils.GetBlsAddress(peer.ConsensusPubKey).Hex(), *peer) + _, ok := dRand.validators.LoadOrStore(common2.MustAddressToBech32(utils.GetBlsAddress(peer.ConsensusPubKey)), *peer) if !ok { dRand.pubKeyLock.Lock() if _, ok := dRand.CommitteeAddresses[peer.ConsensusPubKey.GetAddress()]; !ok { diff --git a/drand/drand_test.go b/drand/drand_test.go index 529747fe4..335550a65 100644 --- a/drand/drand_test.go +++ b/drand/drand_test.go @@ -6,9 +6,11 @@ import ( "testing" bls2 "github.com/harmony-one/harmony/crypto/bls" + common2 "github.com/harmony-one/harmony/internal/common" "github.com/ethereum/go-ethereum/common" "github.com/harmony-one/bls/ffi/go/bls" + msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/core/types" "github.com/harmony-one/harmony/internal/utils" @@ -95,7 +97,7 @@ func TestGetValidatorByPeerId(test *testing.T) { test.Error("dRand should belong to a leader") } - validatorAddress := utils.GetAddressFromBlsPubKey(validatorPubKey).Hex() + validatorAddress := common2.MustAddressToBech32(utils.GetAddressFromBlsPubKey(validatorPubKey)) if dRand.getValidatorPeerByAddress(validatorAddress) == nil { test.Error("Unable to get validator by Peerid") diff --git a/internal/hmyapi/util.go b/internal/hmyapi/util.go index 40a0e63b1..e75cbfb32 100644 --- a/internal/hmyapi/util.go +++ b/internal/hmyapi/util.go @@ -7,6 +7,7 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/harmony-one/harmony/core/types" + common2 "github.com/harmony-one/harmony/internal/common" ) // SubmitTransaction is a helper function that submits tx to txPool and logs a message. @@ -21,7 +22,7 @@ func SubmitTransaction(ctx context.Context, b Backend, tx *types.Transaction) (c return common.Hash{}, err } addr := crypto.CreateAddress(from, tx.Nonce()) - log.Info("Submitted contract creation", "fullhash", tx.Hash().Hex(), "contract", addr.Hex()) + log.Info("Submitted contract creation", "fullhash", tx.Hash().Hex(), "contract", common2.MustAddressToBech32(addr)) } else { log.Info("Submitted transaction", "fullhash", tx.Hash().Hex(), "recipient", tx.To()) } diff --git a/node/contract.go b/node/contract.go index 2affd4c64..65da5b8db 100644 --- a/node/contract.go +++ b/node/contract.go @@ -12,10 +12,12 @@ import ( "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/params" + "github.com/harmony-one/harmony/common/denominations" "github.com/harmony-one/harmony/contracts" "github.com/harmony-one/harmony/contracts/structs" "github.com/harmony-one/harmony/core/types" + common2 "github.com/harmony-one/harmony/internal/common" "github.com/harmony-one/harmony/internal/genesis" "github.com/harmony-one/harmony/internal/utils" ) @@ -170,7 +172,7 @@ func (node *Node) CallFaucetContract(address common.Address) common.Hash { // Temporary code to workaround explorer issue for searching new addresses (https://github.com/harmony-one/harmony/issues/503) nonce := atomic.AddUint64(&node.ContractDeployerCurrentNonce, 1) tx, _ := types.SignTx(types.NewTransaction(nonce-1, address, node.Consensus.ShardID, big.NewInt(0), params.TxGasContractCreation*10, nil, nil), types.HomesteadSigner{}, node.ContractDeployerKey) - utils.GetLogInstance().Info("Sending placeholder token to ", "Address", address.Hex()) + utils.GetLogInstance().Info("Sending placeholder token to ", "Address", common2.MustAddressToBech32(address)) node.addPendingTransactions(types.Transactions{tx}) // END Temporary code @@ -199,7 +201,7 @@ func (node *Node) callGetFreeTokenWithNonce(address common.Address, nonce uint64 return common.Hash{} } tx, _ := types.SignTx(types.NewTransaction(nonce, node.ContractAddresses[0], node.Consensus.ShardID, big.NewInt(0), params.TxGasContractCreation*10, nil, bytesData), types.HomesteadSigner{}, node.ContractDeployerKey) - utils.GetLogInstance().Info("Sending Free Token to ", "Address", address.Hex()) + utils.GetLogInstance().Info("Sending Free Token to ", "Address", common2.MustAddressToBech32(address)) node.addPendingTransactions(types.Transactions{tx}) return tx.Hash()