区块链教程

兄弟连区块链教程Fabric1.0源代码分析MSP成员关系服务提

2018-11-02  本文已影响0人  ab6973df9221

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

3、MSP接口实现

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

```go

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

```

涉及方法如下:

```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代码如下:

```go

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)代码如下:

```go

validationChain, err := msp.getCertificationChainForBCCSPIdentity(id) //获取BCCSP身份认证链

err = msp.validateIdentityAgainstChain(id, validationChain) //根据链验证身份

err = msp.validateIdentityOUs(id) //验证身份中所携带的组织信息有效

//代码在msp/mspimpl.go

```

## 4、MSPManager接口实现

结构体定义:

```go

type mspManagerImpl struct {

    mspsMap map[string]MSP //MSP的映射

    up bool //是否正常启用

}

//代码在msp/mspmgrimpl.go

```

方法:

```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结构体定义(身份):

```go

type identity struct {

    id *IdentityIdentifier //身份标识符(含Mspid和Id,均为string)

    cert *x509.Certificate //代表身份的x509证书

    pk bccsp.Key //身份公钥

    msp *bccspmsp //拥有此实例的MSP实例

}

//代码在msp/identities.go

```

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

```go

type IdentityIdentifier struct {

    Mspid string //Msp id

    Id string //Id

}

//代码在msp/msp.go

```

identity结构体涉及方法如下:

```go

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结构体定义(签名身份):

```go

type signingidentity struct {

    identity //嵌入identity

    signer crypto.Signer //crypto标准库中Signer接口

}

//代码在msp/identities.go

```

signingidentity结构体涉及方法如下:

```go

//新建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中。

```go

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

```

涉及的方法如下:

```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为空时设置默认值。

```go

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涉及方法如下:

```go

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代码如下:

```go

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

```

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

��~� tv

上一篇 下一篇

猜你喜欢

热点阅读