From ff7f791e9e39b539cfe292f173737ba82f9aece9 Mon Sep 17 00:00:00 2001 From: Brian Date: Tue, 10 Jan 2023 09:57:57 +0800 Subject: [PATCH] Refine readability #378 (#394) --- bcs/contract/evm/abi/abi.go | 6 ++-- bcs/contract/evm/address.go | 14 ++++----- kernel/permission/acl/ptree/ptree.go | 4 +-- kernel/permission/acl/utils/utils.go | 47 +++++++++++----------------- 4 files changed, 31 insertions(+), 40 deletions(-) diff --git a/bcs/contract/evm/abi/abi.go b/bcs/contract/evm/abi/abi.go index e89aa512..5bacbb07 100644 --- a/bcs/contract/evm/abi/abi.go +++ b/bcs/contract/evm/abi/abi.go @@ -11,8 +11,8 @@ type ABI struct { spec *abi.Spec } -func LoadFile(fpath string) (*ABI, error) { - spec, err := abi.LoadPath(fpath) +func LoadFile(path string) (*ABI, error) { + spec, err := abi.LoadPath(path) if err != nil { return nil, err } @@ -33,6 +33,7 @@ func newABI(spec *abi.Spec) *ABI { } } +// Encode ABI encodes a function call func (a *ABI) Encode(methodName string, args map[string]interface{}) ([]byte, error) { if methodName == "" { if a.spec.Constructor != nil { @@ -47,6 +48,7 @@ func (a *ABI) Encode(methodName string, args map[string]interface{}) ([]byte, er return a.encodeMethod(method, args) } +// encodeMethod ABI encodes a function call func (a *ABI) encodeMethod(method *abi.FunctionSpec, args map[string]interface{}) ([]byte, error) { var inputs []interface{} for _, input := range method.Inputs { diff --git a/bcs/contract/evm/address.go b/bcs/contract/evm/address.go index 4e393175..19e14d6a 100644 --- a/bcs/contract/evm/address.go +++ b/bcs/contract/evm/address.go @@ -16,8 +16,8 @@ import ( const ( evmAddressFiller = "-" - contractNamePrefixs = "1111" - contractAccountPrefixs = "1112" + contractNamePrefix = "1111" + contractAccountPrefix = "1112" xchainAddrType = "xchain" contractNameType = "contract-name" @@ -63,7 +63,7 @@ func ContractNameToEVMAddress(contractName string) (crypto.Address, error) { prefixStr += evmAddressFiller } contractName = prefixStr + contractName - contractName = contractNamePrefixs + contractName + contractName = contractNamePrefix + contractName return crypto.AddressFromBytes([]byte(contractName)) } @@ -81,7 +81,7 @@ func EVMAddressToContractName(evmAddr crypto.Address) (string, error) { // transfer contract account to evm address func ContractAccountToEVMAddress(contractAccount string) (crypto.Address, error) { contractAccountValid := contractAccount[2:18] - contractAccountValid = contractAccountPrefixs + contractAccountValid + contractAccountValid = contractAccountPrefix + contractAccountValid return crypto.AddressFromBytes([]byte(contractAccountValid)) } @@ -128,7 +128,7 @@ func DetermineContractNameFromEVM(evmAddr crypto.Address) (string, error) { evmAddrWithPrefix := evmAddr.Bytes() evmAddrStrWithPrefix := string(evmAddrWithPrefix) - if evmAddrStrWithPrefix[0:4] != contractNamePrefixs { + if evmAddrStrWithPrefix[0:4] != contractNamePrefix { return "", fmt.Errorf("not a valid contract name from evm") } else { addr, err = EVMAddressToContractName(evmAddr) @@ -148,11 +148,11 @@ func DetermineEVMAddress(evmAddr crypto.Address) (string, string, error) { var addr, addrType string var err error - if evmAddrStrWithPrefix[0:4] == contractAccountPrefixs { + if evmAddrStrWithPrefix[0:4] == contractAccountPrefix { // 此时 addr 不包括前缀和后缀! addr, err = EVMAddressToContractAccountWithoutPrefixAndSuffix(evmAddr) addrType = contractAccountType - } else if evmAddrStrWithPrefix[0:4] == contractNamePrefixs { + } else if evmAddrStrWithPrefix[0:4] == contractNamePrefix { addr, err = EVMAddressToContractName(evmAddr) addrType = contractNameType } else { diff --git a/kernel/permission/acl/ptree/ptree.go b/kernel/permission/acl/ptree/ptree.go index 264f1a8a..cb00df05 100644 --- a/kernel/permission/acl/ptree/ptree.go +++ b/kernel/permission/acl/ptree/ptree.go @@ -54,14 +54,14 @@ func (pn *PermNode) FindChild(name string) *PermNode { } // BuildAccountPermTree build PermTree for account -func BuildAccountPermTree(aclMgr base.AclManager, account string, aksuri []string) (*PermNode, error) { +func BuildAccountPermTree(aclMgr base.AclManager, account string, akURIs []string) (*PermNode, error) { accountACL, err := aclMgr.GetAccountACL(account) if err != nil { return nil, err } root := NewPermNode(account, accountACL) - root, err = buildPermTree(root, aclMgr, aksuri, true) + root, err = buildPermTree(root, aclMgr, akURIs, true) if err != nil { return nil, err } diff --git a/kernel/permission/acl/utils/utils.go b/kernel/permission/acl/utils/utils.go index b97ee00d..c3fe4a38 100644 --- a/kernel/permission/acl/utils/utils.go +++ b/kernel/permission/acl/utils/utils.go @@ -5,42 +5,41 @@ import ( "fmt" "strings" + "github.com/xuperchain/xupercore/kernel/permission/acl/base" "github.com/xuperchain/xupercore/kernel/permission/acl/ptree" "github.com/xuperchain/xupercore/kernel/permission/acl/rule" - crypto_client "github.com/xuperchain/xupercore/lib/crypto/client" + "github.com/xuperchain/xupercore/lib/crypto/client" pb "github.com/xuperchain/xupercore/protos" - - "github.com/xuperchain/xupercore/kernel/permission/acl/base" ) -func IdentifyAK(akuri string, sign *pb.SignatureInfo, msg []byte) (bool, error) { +func IdentifyAK(akURI string, sign *pb.SignatureInfo, msg []byte) (bool, error) { if sign == nil { return false, errors.New("sign is nil") } - akpath := SplitAccountURI(akuri) - if len(akpath) < 1 { + akPath := SplitAccountURI(akURI) + if len(akPath) < 1 { return false, errors.New("Invalid address") } - ak := akpath[len(akpath)-1] + ak := akPath[len(akPath)-1] return VerifySign(ak, sign, msg) } -func IdentifyAccount(aclMgr base.AclManager, account string, aksuri []string) (bool, error) { +func IdentifyAccount(aclMgr base.AclManager, account string, akURIs []string) (bool, error) { // aks and signs could have zero length for permission rule Null if aclMgr == nil { return false, fmt.Errorf("Invalid Param, aclMgr=%v", aclMgr) } // build perm tree - pnode, err := ptree.BuildAccountPermTree(aclMgr, account, aksuri) + tree, err := ptree.BuildAccountPermTree(aclMgr, account, akURIs) if err != nil { return false, err } - return validatePermTree(pnode, true) + return validatePermTree(tree, true) } -func CheckContractMethodPerm(aclMgr base.AclManager, aksuri []string, +func CheckContractMethodPerm(aclMgr base.AclManager, akURIs []string, contractName, methodName string) (bool, error) { // aks and signs could have zero length for permission rule Null @@ -49,13 +48,13 @@ func CheckContractMethodPerm(aclMgr base.AclManager, aksuri []string, } // build perm tree - pnode, err := ptree.BuildMethodPermTree(aclMgr, contractName, methodName, aksuri) + tree, err := ptree.BuildMethodPermTree(aclMgr, contractName, methodName, akURIs) if err != nil { return false, err } // validate perm tree - return validatePermTree(pnode, false) + return validatePermTree(tree, false) } func validatePermTree(root *ptree.PermNode, isAccount bool) (bool, error) { @@ -118,32 +117,22 @@ func validatePermTree(root *ptree.PermNode, isAccount bool) (bool, error) { node.Status = ptree.Failed } } - return (root.Status == ptree.Success), nil + return root.Status == ptree.Success, nil } -func SplitAccountURI(akuri string) []string { - ids := strings.Split(akuri, "/") +func SplitAccountURI(akURI string) []string { + ids := strings.Split(akURI, "/") return ids } -// GetAccountACL return account acl -func GetAccountACL(aclMgr base.AclManager, account string) (*pb.Acl, error) { - return aclMgr.GetAccountACL(account) -} - -// GetContractMethodACL return contract method acl -func GetContractMethodACL(aclMgr base.AclManager, contractName, methodName string) (*pb.Acl, error) { - return aclMgr.GetContractMethodACL(contractName, methodName) -} - func VerifySign(ak string, si *pb.SignatureInfo, data []byte) (bool, error) { - bytespk := []byte(si.PublicKey) - xcc, err := crypto_client.CreateCryptoClientFromJSONPublicKey(bytespk) + pk := []byte(si.PublicKey) + xcc, err := client.CreateCryptoClientFromJSONPublicKey(pk) if err != nil { return false, err } - ecdsaKey, err := xcc.GetEcdsaPublicKeyFromJsonStr(string(bytespk[:])) + ecdsaKey, err := xcc.GetEcdsaPublicKeyFromJsonStr(string(pk)) if err != nil { return false, err }