区块链教程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

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

相关文章
|
2月前
|
安全 区块链
区块链积分商城系统开发详细指南//需求功能/指南教程/源码流程
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
|
6月前
|
测试技术 区块链 Android开发
区块链项目开发的合作服务流程指南
区块链项目开发的合作服务流程指南
|
2月前
|
供应链 区块链 数据安全/隐私保护
探索区块链技术在金融领域的应用与前景分析
本文将深入探讨区块链技术在金融领域的具体应用场景,分析其优势与挑战,并展望未来发展趋势。通过案例分析和技术解析,揭示区块链技术在金融行业中的革新意义及前景。
|
2月前
|
安全 区块链
区块链游戏系统开发步骤需求丨功能逻辑丨规则玩法丨指南教程丨源码详细
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.
|
10月前
Minecraft Fabric 教程 #8 添加附魔书
这就创建了一个FireBoom附魔书 onTargetDamaged //当目标被攻击 在mc FireballEntity类有一个 方法就是当火球碰撞了就创建一个火焰爆炸的效果
42 0
|
10月前
Minecraft Fabric 教程 #4 添加分组
在 ItemGroup 显示 使用 FabricItemGroupBuilder
37 0
|
8月前
|
区块链
区块链的发币流程技术分析
区块链现在是发展的如火如荼,很多人都想趁着这个风口,投入区块链创业的浪潮中。 那么我们该怎么做才能抓住这个机会呢? 进行区块链发币要求是很多的,主要有以下几个步骤。
|
8月前
|
安全 算法 区块链
区块链交易所开发技术说明:智能合约设计与实现步骤实现分析
智能合约是区块链技术的核心应用,其能够自动执行、验证和执行合同,并以可验证的方式进行操作。在区块链交易所中,智能合约扮演着重要的角色,它们保证了交易的透明性、效率和安全性。作为一名专业的交易所开发团队一员,在交易所开发这块拥有相对成熟的开发技术,目前已经有成熟的区块链交易所开发案例。本文将介绍如何设计和实现可靠的智能合约来支持区块链交易所。
|
8月前
|
区块链 安全 数据安全/隐私保护
区块链LP流动性SWAP博饼交易所系统开发分析模式
Web3在生态的每一个要素中,都体现出了去中心化的特点。
|
8月前
|
SQL 安全 区块链
交易所系统开发(案例项目)丨区块链交易所系统开发(稳定版)/成熟技术/步骤逻辑/源码教程
The development of a blockchain exchange system involves complex technologies and functions.

热门文章

最新文章