github.com/s7techlab/cckit@v0.10.5/testing/txcreator_transformer.go (about) 1 package testing 2 3 import ( 4 "github.com/golang/protobuf/proto" 5 "github.com/hyperledger/fabric/msp" 6 "github.com/s7techlab/cckit/identity" 7 8 pmsp "github.com/hyperledger/fabric-protos-go/msp" 9 ) 10 11 func CreatorFromSigningIdentity(creator msp.SigningIdentity) (mspID string, certPEM []byte, err error) { 12 serialized, err := creator.Serialize() 13 if err != nil { 14 return ``, nil, err 15 } 16 17 sid := &pmsp.SerializedIdentity{} 18 if err = proto.Unmarshal(serialized, sid); err != nil { 19 return ``, nil, err 20 } 21 return sid.Mspid, sid.IdBytes, nil 22 } 23 24 // TransformCreator transforms arbitrary tx creator (pmsp.SerializedIdentity etc) to mspID string, certPEM []byte, 25 func TransformCreator(txCreator ...interface{}) (mspID string, certPEM []byte, err error) { 26 if len(txCreator) == 1 { 27 switch c := txCreator[0].(type) { 28 29 case identity.CertIdentity: 30 return c.MspID, c.GetPEM(), nil 31 32 case *identity.CertIdentity: 33 return c.MspID, c.GetPEM(), nil 34 35 case pmsp.SerializedIdentity: 36 return c.Mspid, c.IdBytes, nil 37 38 case IdentitySample: 39 id := c.SigningIdentity() 40 return CreatorFromSigningIdentity(id) 41 42 case msp.SigningIdentity: 43 return CreatorFromSigningIdentity(c) 44 45 case [2]string: 46 // array with 2 elements - mspId and ca cert 47 return c[0], []byte(c[1]), nil 48 } 49 } else if len(txCreator) == 2 { 50 return txCreator[0].(string), txCreator[1].([]byte), nil 51 } 52 53 return ``, nil, ErrUnknownFromArgsType 54 }