github.com/bigzoro/my_simplechain@v0.0.0-20240315012955-8ad0a2a29bb9/permission/utils.go (about)

     1  package permission
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"strconv"
     7  	"strings"
     8  
     9  	"github.com/bigzoro/my_simplechain/accounts"
    10  	"github.com/bigzoro/my_simplechain/accounts/keystore"
    11  	"github.com/bigzoro/my_simplechain/common"
    12  	"github.com/bigzoro/my_simplechain/ethdb"
    13  )
    14  
    15  func splitENode(eNode string) (string, string, string, error) {
    16  	eNodeIdAndHost := strings.Split(eNode, "@")
    17  	if len(eNodeIdAndHost) != 2 {
    18  		return "", "", "", errors.New("eNode format error")
    19  	}
    20  	eNodeId := eNodeIdAndHost[0]
    21  	hostAndPort := strings.Split(eNodeIdAndHost[1], ":")
    22  	if len(hostAndPort) != 2 {
    23  		return "", "", "", errors.New("eNode format error")
    24  	}
    25  	ip := hostAndPort[0]
    26  	port := hostAndPort[1]
    27  	//may be have ?discport=0
    28  	temp := strings.Split(port, "?")
    29  	port = temp[0]
    30  	return eNodeId, ip, port, nil
    31  }
    32  
    33  func splitPort(eNode string) (string, uint16, error) {
    34  	tmp := strings.Split(eNode, "&raftid=")
    35  	if len(tmp) != 2 {
    36  		return "", 0, errors.New("enode format error")
    37  	}
    38  	tmp2, err := strconv.Atoi(tmp[1])
    39  	if err != nil {
    40  		return "", 0, err
    41  	}
    42  	return tmp[0], uint16(tmp2), nil
    43  }
    44  
    45  func storeContractAddress(key []byte, value common.Address, db ethdb.Database) error {
    46  	if value == (common.Address{}) {
    47  		return errors.New("address is empty,please take care of it")
    48  	}
    49  	if db == nil {
    50  		return errors.New("ethDB is nil,please take care of it")
    51  	}
    52  	err := db.Put(key, value.Bytes())
    53  	if err != nil {
    54  		return err
    55  	}
    56  	return nil
    57  }
    58  
    59  func loadContractAddress(key []byte, db ethdb.Database) (common.Address, error) {
    60  	if db == nil {
    61  		return common.Address{}, errors.New("db is nil,please take care of it")
    62  	}
    63  	has, err := db.Has(key)
    64  	if err != nil {
    65  		return common.Address{}, err
    66  	}
    67  	if !has {
    68  		//还没有存在,我们直接返回nil,调用端需自行判断不为common.Address{}以后,再使用
    69  		return common.Address{}, nil
    70  	}
    71  	ret, err := db.Get(key)
    72  	if err != nil {
    73  		return common.Address{}, err
    74  	}
    75  	if ret == nil {
    76  		return common.Address{}, fmt.Errorf("can not get contract address")
    77  	}
    78  	addr := common.BytesToAddress(ret)
    79  	return addr, nil
    80  }
    81  
    82  // fetchKeystore retrives the encrypted keystore from the account manager.
    83  func fetchKeystore(am *accounts.Manager, account accounts.Account) (*keystore.KeyStore, error) {
    84  	index, err := am.FindAccIndex(account)
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  	return am.Backends(keystore.KeyStoreType)[index].(*keystore.KeyStore), nil
    89  }