1 Star 1 Fork 0

Hyperledger Fabric 国密 / fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
inquire.go 2.42 KB
一键复制 编辑 原始数据 按行查看 历史
Jtyoui 提交于 2021-07-22 15:59 . 国密
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package inquire
import (
"fmt"
"gitee.com/hyperledger-fabric-gm/fabric/common/flogging"
"gitee.com/hyperledger-fabric-gm/fabric/common/graph"
"gitee.com/hyperledger-fabric-gm/fabric/common/policies"
"gitee.com/hyperledger-fabric-gm/fabric/protos/common"
)
var logger = flogging.MustGetLogger("policies.inquire")
const (
combinationsUpperBound = 10000
)
type inquireableSignaturePolicy struct {
sigPol *common.SignaturePolicyEnvelope
}
// NewInquireableSignaturePolicy creates a signature policy that can be inquired,
// from a policy and a signature policy.
func NewInquireableSignaturePolicy(sigPol *common.SignaturePolicyEnvelope) policies.InquireablePolicy {
return &inquireableSignaturePolicy{
sigPol: sigPol,
}
}
// SatisfiedBy returns a slice of PrincipalSets that each of them
// satisfies the policy.
func (isp *inquireableSignaturePolicy) SatisfiedBy() []policies.PrincipalSet {
rootId := fmt.Sprintf("%d", 0)
root := graph.NewTreeVertex(rootId, isp.sigPol.Rule)
computePolicyTree(root)
var res []policies.PrincipalSet
for _, perm := range root.ToTree().Permute(combinationsUpperBound) {
principalSet := principalsOfTree(perm, isp.sigPol.Identities)
if len(principalSet) == 0 {
return nil
}
res = append(res, principalSet)
}
return res
}
func principalsOfTree(tree *graph.Tree, principals policies.PrincipalSet) policies.PrincipalSet {
var principalSet policies.PrincipalSet
i := tree.BFS()
for {
v := i.Next()
if v == nil {
break
}
if !v.IsLeaf() {
continue
}
pol := v.Data.(*common.SignaturePolicy)
switch principalIndex := pol.Type.(type) {
case *common.SignaturePolicy_SignedBy:
if len(principals) <= int(principalIndex.SignedBy) {
logger.Warning("Failed computing principalsOfTree, index out of bounds")
return nil
}
principal := principals[principalIndex.SignedBy]
principalSet = append(principalSet, principal)
default:
// Leaf vertex is not of type SignedBy
logger.Warning("Leaf vertex", v.Id, "is of type", pol.GetType())
return nil
}
}
return principalSet
}
func computePolicyTree(v *graph.TreeVertex) {
sigPol := v.Data.(*common.SignaturePolicy)
if p := sigPol.GetNOutOf(); p != nil {
v.Threshold = int(p.N)
for i, rule := range p.Rules {
id := fmt.Sprintf("%s.%d", v.Id, i)
u := v.AddDescendant(graph.NewTreeVertex(id, rule))
computePolicyTree(u)
}
}
}
Go
1
https://gitee.com/hyperledger-fabric-gm/fabric.git
git@gitee.com:hyperledger-fabric-gm/fabric.git
hyperledger-fabric-gm
fabric
fabric
v1.4.9

搜索帮助