Skip to content

Commit

Permalink
Refine readability OpenAtomFoundation#378
Browse files Browse the repository at this point in the history
  • Loading branch information
zhugelianglongming committed Dec 20, 2022
1 parent 49e4ec2 commit 99b0acd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 40 deletions.
6 changes: 4 additions & 2 deletions bcs/contract/evm/abi/abi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions bcs/contract/evm/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
const (
evmAddressFiller = "-"

contractNamePrefixs = "1111"
contractAccountPrefixs = "1112"
contractNamePrefix = "1111"
contractAccountPrefix = "1112"

xchainAddrType = "xchain"
contractNameType = "contract-name"
Expand Down Expand Up @@ -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))
}

Expand All @@ -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))
}

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions kernel/permission/acl/ptree/ptree.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
47 changes: 18 additions & 29 deletions kernel/permission/acl/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 99b0acd

Please sign in to comment.