区块链教程Fabric1.0源代码分析MSP成员关系服务提供者二

简介:

3、MSP接口实现

MSP接口实现,即bccspmsp结构体及方法,bccspmsp定义如下:

type bccspmsp struct {
    rootCerts []Identity //信任的CA证书列表
    intermediateCerts []Identity //信任的中间证书列表
    tlsRootCerts [][]byte //信任的CA TLS 证书列表
    tlsIntermediateCerts [][]byte //信任的中间TLS 证书列表
    certificationTreeInternalNodesMap map[string]bool //待定
    signer SigningIdentity //签名身份
    admins []Identity //管理身份列表
    bccsp bccsp.BCCSP //加密服务提供者
    name string //MSP名字
    opts *x509.VerifyOptions //MSP成员验证选项
    CRL []*pkix.CertificateList //证书吊销列表
    ouIdentifiers map[string][][]byte //组织列表
    cryptoConfig *m.FabricCryptoConfig //加密选项
}
//代码在msp/mspimpl.go

涉及方法如下:

func NewBccspMsp() (MSP, error) //创建bccsp实例,以及创建并初始化bccspmsp实例
func (msp *bccspmsp) Setup(conf1 *m.MSPConfig) error ////根据MSPConfig设置MSP实例
func (msp *bccspmsp) GetType() ProviderType //获取MSP类型,即FABRIC
func (msp *bccspmsp) GetIdentifier() (string, error) //获取MSP名字
func (msp *bccspmsp) GetTLSRootCerts() [][]byte //获取信任的CA TLS 证书列表msp.tlsRootCerts
func (msp *bccspmsp) GetTLSIntermediateCerts() [][]byte //获取信任的中间TLS 证书列表msp.tlsIntermediateCerts
func (msp *bccspmsp) GetDefaultSigningIdentity() (SigningIdentity, error) ////获取默认的签名身份msp.signer
func (msp *bccspmsp) GetSigningIdentity(identifier *IdentityIdentifier) (SigningIdentity, error) //暂未实现,可忽略
func (msp *bccspmsp) Validate(id Identity) error //校验身份是否有效,调取msp.validateIdentity(id)实现
func (msp *bccspmsp) DeserializeIdentity(serializedID []byte) (Identity, error) //身份反序列化
func (msp *bccspmsp) SatisfiesPrincipal(id Identity, principal *m.MSPPrincipal) error //验证给定的身份与principal中所描述的类型是否相匹配
//代码在msp/mspimpl.go

func (msp bccspmsp) Setup(conf1 m.MSPConfig) error代码如下:

conf := &m.FabricMSPConfig{}
err := proto.Unmarshal(conf1.Config, conf) //将conf1.Config []byte解码为FabricMSPConfig
msp.name = conf.Name
err := msp.setupCrypto(conf) //设置加密选项msp.cryptoConfig
err := msp.setupCAs(conf) //设置MSP成员验证选项msp.opts,并添加信任的CA证书msp.rootCerts和信任的中间证书msp.intermediateCerts
err := msp.setupAdmins(conf) //设置管理身份列表msp.admins
err := msp.setupCRLs(conf) //设置证书吊销列表msp.CRL
err := msp.finalizeSetupCAs(conf); err != nil //设置msp.certificationTreeInternalNodesMap
err := msp.setupSigningIdentity(conf) //设置签名身份msp.signer
err := msp.setupOUs(conf) //设置组织列表msp.ouIdentifiers
err := msp.setupTLSCAs(conf) //设置并添加信任的CA TLS 证书列表msp.tlsRootCerts,以及信任的CA TLS 证书列表msp.tlsIntermediateCerts
for i, admin := range msp.admins {
    err = admin.Validate() //确保管理员是有效的成员
}
//代码在msp/mspimpl.go

func (msp bccspmsp) validateIdentity(id identity)代码如下:

validationChain, err := msp.getCertificationChainForBCCSPIdentity(id) //获取BCCSP身份认证链
err = msp.validateIdentityAgainstChain(id, validationChain) //根据链验证身份
err = msp.validateIdentityOUs(id) //验证身份中所携带的组织信息有效
//代码在msp/mspimpl.go

4、MSPManager接口实现

结构体定义:

type mspManagerImpl struct {
    mspsMap map[string]MSP //MSP的映射
    up bool //是否正常启用
}
//代码在msp/mspmgrimpl.go

方法:

func NewMSPManager() MSPManager //创建mspManagerImpl实例
func (mgr *mspManagerImpl) Setup(msps []MSP) error //将msps装入mgr.mspsMap
func (mgr *mspManagerImpl) GetMSPs() (map[string]MSP, error) //获取mgr.mspsMap
func (mgr *mspManagerImpl) DeserializeIdentity(serializedID []byte) (Identity, error) //调用msp.DeserializeIdentity()实现身份反序列化
//代码在msp/mspmgrimpl.go

5、Identity、SigningIdentity接口实现

identity结构体定义(身份):

type identity struct {
    id *IdentityIdentifier //身份标识符(含Mspid和Id,均为string)
    cert *x509.Certificate //代表身份的x509证书
    pk bccsp.Key //身份公钥
    msp *bccspmsp //拥有此实例的MSP实例
}
//代码在msp/identities.go

补充IdentityIdentifier结构体定义(身份标识符):

type IdentityIdentifier struct {
    Mspid string //Msp id
    Id string //Id
}
//代码在msp/msp.go

identity结构体涉及方法如下:

func newIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, msp *bccspmsp) (Identity, error) //创建identity实例
func NewSerializedIdentity(mspID string, certPEM []byte) ([]byte, error) //新建身份SerializedIdentity并序列化
func (id *identity) SatisfiesPrincipal(principal *msp.MSPPrincipal) error //调用msp的SatisfiesPrincipal检查身份与principal中所描述的类型是否匹配
func (id *identity) GetIdentifier() *IdentityIdentifier //获取id.id
func (id *identity) GetMSPIdentifier() string //获取id.id.Mspid
func (id *identity) Validate() error //调取id.msp.Validate(id)校验身份是否有效
func (id *identity) GetOrganizationalUnits() []*OUIdentifier //获取组织单元
func (id *identity) Verify(msg []byte, sig []byte) error //用这个身份校验消息签名
func (id *identity) Serialize() ([]byte, error)//身份序列化
func (id *identity) getHashOpt(hashFamily string) (bccsp.HashOpts, error) //调取bccsp.GetHashOpt
//代码在msp/identities.go

signingidentity结构体定义(签名身份):

type signingidentity struct {
    identity //嵌入identity
    signer crypto.Signer //crypto标准库中Signer接口
}
//代码在msp/identities.go

signingidentity结构体涉及方法如下:

//新建signingidentity实例
func newSigningIdentity(id *IdentityIdentifier, cert *x509.Certificate, pk bccsp.Key, signer crypto.Signer, msp *bccspmsp) (SigningIdentity, error) 
func (id *signingidentity) Sign(msg []byte) ([]byte, error) //签名msg
func (id *signingidentity) GetPublicVersion() Identity //获取id.identity
//代码在msp/identities.go

6、MSPConfig相关结构体及方法

MSPConfig相关结构体定义:
FabricMSPConfig定义与bccspmsp接近,FabricMSPConfig序列化后以[]byte存入MSPConfig.Config中。

type MSPConfig struct {
    Type int32
    Config []byte
}
type FabricMSPConfig struct {
    Name string //MSP名字
    RootCerts [][]byte //信任的CA证书列表
    IntermediateCerts [][]byte //信任的中间证书列表
    Admins [][]byte //管理身份列表
    RevocationList [][]byte //证书吊销列表
    SigningIdentity *SigningIdentityInfo //签名身份
    OrganizationalUnitIdentifiers []*FabricOUIdentifier //组织列表
    CryptoConfig *FabricCryptoConfig //加密选项
    TlsRootCerts [][]byte //信任的CA TLS 证书列表
    TlsIntermediateCerts [][]byte //信任的中间TLS 证书列表
}
//代码在protos/msp/msp_config.pb.go

涉及的方法如下:

func GetLocalMspConfig(dir string, bccspConfig *factory.FactoryOpts, ID string) (*msp.MSPConfig, error) //获取本地MSP配置
//代码在protos/msp/configbuilder.go

func GetLocalMspConfig(dir string, bccspConfig factory.FactoryOpts, ID string) (msp.MSPConfig, error)实现代码如下:
SetupBCCSPKeystoreConfig()核心代码为bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir},目的是在FileKeystore或KeyStorePath为空时设置默认值。

signcertDir := filepath.Join(dir, signcerts) //signcerts为"signcerts",signcertDir即/etc/hyperledger/fabric/msp/signcerts/
keystoreDir := filepath.Join(dir, keystore) //keystore为"keystore",keystoreDir即/etc/hyperledger/fabric/msp/keystore/
bccspConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir) //设置bccspConfig.SwOpts.Ephemeral = false和bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir}
    //bccspConfig.SwOpts.Ephemeral是否短暂的
err := factory.InitFactories(bccspConfig) //初始化bccsp factory,并创建bccsp实例
signcert, err := getPemMaterialFromDir(signcertDir) //读取X.509证书的PEM文件
sigid := &msp.SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: nil} //构造SigningIdentityInfo
return getMspConfig(dir, ID, sigid) //分别读取cacerts、admincerts、tlscacerts文件,以及config.yaml中组织信息,构造msp.FabricMSPConfig,序列化后用于构造msp.MSPConfig
//代码在msp/configbuilder.go

7、mgmt

mgmt涉及方法如下:

func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error //从指定目录加载本地MSP
func GetLocalMSP() msp.MSP //调取msp.NewBccspMsp()创建bccspmsp实例
func GetLocalSigningIdentityOrPanic() msp.SigningIdentity //GetLocalMSP().GetDefaultSigningIdentity()
//代码在msp/mgmt/mgmt.go

func LoadLocalMsp(dir string, bccspConfig *factory.FactoryOpts, mspID string) error代码如下:

conf, err := msp.GetLocalMspConfig(dir, bccspConfig, mspID) //获取本地MSP配置,序列化后写入msp.MSPConfig,即conf
return GetLocalMSP().Setup(conf) //调取msp.NewBccspMsp()创建bccspmsp实例,调取bccspmsp.Setup(conf)解码conf.Config并设置bccspmsp
//代码在msp/mgmt/mgmt.go

感谢关注兄弟连区块链教程分享!

相关文章
|
24天前
|
存储 供应链 监控
区块链技术在供应链管理中的应用与前景分析
随着信息化时代的到来,供应链管理面临着越来越多的挑战和机遇。本文主要探讨了区块链技术在供应链管理中的应用,以及未来的发展前景。通过对区块链技术的特点和优势进行分析,结合实际案例和趋势展望,展示了区块链技术在提升供应链透明度、效率和安全性方面的潜力,以及未来发展的可能方向。
|
24天前
|
安全 区块链
区块链积分商城系统开发详细指南//需求功能/指南教程/源码流程
Developing a blockchain points mall system involves multiple aspects such as blockchain technology, smart contracts, front-end development, and business logic design. The following is the general process for developing a blockchain points mall system
|
7月前
|
测试技术 区块链 Android开发
区块链项目开发的合作服务流程指南
区块链项目开发的合作服务流程指南
|
18天前
|
存储 算法 API
面向企业的区块链教程(一)(2)
面向企业的区块链教程(一)
46 6
|
8天前
|
存储 供应链 安全
基于区块链技术的智能合约安全性分析
【5月更文挑战第31天】本文深入探讨了区块链技术中智能合约的安全性问题,通过分析现有智能合约的安全漏洞和攻击手段,提出了一系列增强智能合约安全性的策略。文章首先介绍了区块链和智能合约的基本概念,随后详细讨论了智能合约面临的安全挑战,包括代码漏洞、重入攻击等问题,并对比分析了不同平台下智能合约的安全性差异。最后,文章提出了一系列提高智能合约安全性的建议,旨在为区块链应用的健康发展提供参考。
|
13天前
|
存储 算法 安全
区块链系统开发技术规则分析
区块链核心技术包括:1) 哈希算法,利用单向函数将任意数据转化为固定长度代码,确保安全验证;2) 非对称加密,使用公钥和私钥一对进行加密解密,保证信息安全;3) 共识机制,如PoW、PoS、DPoS等,实现快速交易验证和确认;4) 智能合约,自动执行的可信代码,一旦编写即不可更改,用于自动化交易;5) 分布式存储,将数据分散存储在网络各处,涵盖结构化、非结构化和半结构化数据。
|
24天前
|
供应链 安全 区块链
《区块链简易速速上手小册》第6章:区块链在金融服务领域的应用(2024 最新版)
《区块链简易速速上手小册》第6章:区块链在金融服务领域的应用(2024 最新版)
68 1
|
24天前
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
175 15
|
24天前
|
安全 区块链
区块链游戏系统开发步骤需求丨功能逻辑丨规则玩法丨指南教程丨源码详细
Developing blockchain game systems has been a highly anticipated field in recent years. By combining blockchain technology and game mechanics, players can enjoy a brand new gaming experience and higher game credibility.
|
11月前
Minecraft Fabric 教程 #8 添加附魔书
这就创建了一个FireBoom附魔书 onTargetDamaged //当目标被攻击 在mc FireballEntity类有一个 方法就是当火球碰撞了就创建一个火焰爆炸的效果
44 0