hyperledger fabric 智能合约开发(六)------其他合约接口

其他合约接口

我们在开发中,还会遇到其他合约接口调用,可能不会太频繁,但会遇见。下面我来介绍一下其他合约接口开发:

Package cid(import "github.com/hyperledger/fabric/core/chaincode/shim/ext/cid")

func AssertAttributeValue检查属性值是否等于指定值

func AssertAttributeValue(stub ChaincodeStubInterface, attrName, attrValue string) error

func GetAttributeValue 返回指定属性的值

func GetAttributeValue(stub ChaincodeStubInterface, attrName string) (value string, found bool, err error)

func GetID返回与调用标识关联的ID。 此ID在MSP中保证是唯一的。

func GetID(stub ChaincodeStubInterface) (string, error)

func GetMSPID返回与提交事务的标识关联的MSP的ID

func GetMSPID(stub ChaincodeStubInterface) (string, error)

func GetX509Certificate 返回与客户端关联的X509证书,如果未通过X509证书识别,则返回nil。

func GetX509Certificate(stub ChaincodeStubInterface) (*x509.Certificate, error)

type ChaincodeStubInterface ¶ Uses

type ChaincodeStubInterface interface {

    GetCreator() ([]byte, error)

}部署链代码使用ChaincodeStubInterface来获取提交事务的代理(或用户)的身份。

type ClientIdentity

type ClientIdentity interface {

    GetID() (string, error)

    GetMSPID() (string, error)

    GetAttributeValue(attrName string) (value string, found bool, err error)

    AssertAttributeValue(attrName, attrValue string) error

    GetX509Certificate() (*x509.Certificate, error)

}//表示有关提交事务的标识的信息

func New

func New(stub ChaincodeStubInterface) (ClientIdentity, error)//返回ClientIdentity的实例

package attrmg(import "github.com/hyperledger/fabric/core/chaincode/shim/ext/attrmgr")

var

var(

     // AttrOID是一个属性扩展的ASN.1对象标识符X509证书

     AttrOID = asn1.ObjectIdentifier {1,2,3,4,5,6,7,8,1}

     // AttrOIDString是AttrOID的字符串版本

     AttrOIDString =“1.2.3.4.5.6.7.8.1”

type Attribute

type Attribute interface {

     // GetName返回属性的名称

     GetName()string

     // GetValue返回属性的值

     GetValue()string

}

attribute是一个键值对

type Attributes

type Attributes struct {

    Attrs map[string]string `json:"attrs"`

}

Attributes包含属性名称和值

func (*Attributes) Contains

func (a *Attributes) Contains(name string) bool //如果找到命名属性,则包含返回true。

func (*Attributes) Names

func (a *Attributes) Names() []string //Names返回属性的名称

func (*Attributes) True

func (a *Attributes) True(name string) error //如果属性'name'的值为true,则True返回nil; 否则,返回适当的错误。

func (*Attributes) Value

func (a *Attributes) Value(name string) (string, bool, error)//值返回属性的值

type Mgr

type Mgr struct{} //Mgr是属性管理器,是此包的主要对象

func New

func New() *Mgr//新构造属性管理器

func (*Mgr) AddAttributesToCert

func (mgr *Mgr) AddAttributesToCert(attrs *Attributes, cert *x509.Certificate) error

//AddAttributesToCert将公共属性信息添加到X509证书。

func (*Mgr) GetAttributesFromCert

func (mgr *Mgr) GetAttributesFromCert(cert *x509.Certificate) (*Attributes, error)//从证书中获取属性

func (*Mgr) GetAttributesFromIdemix

func (mgr *Mgr) GetAttributesFromIdemix(creator []byte) (*Attributes, error)

func (*Mgr) ProcessAttributeRequests

func (mgr *Mgr) ProcessAttributeRequests(requests []AttributeRequest, attributes []Attribute) (*Attributes, error)

//获取属性请求数组和标识属性,并返回包含所请求属性的Attributes对象。

func (*Mgr) ProcessAttributeRequestsForCert

func (mgr *Mgr) ProcessAttributeRequestsForCert(requests []AttributeRequest, attributes []Attribute, cert *x509.Certificate) error

//ProcessAttributeRequestsForCert在给定属性请求和属性的情况下向X509证书添加属性。

package statebased

(import "github.com/hyperledger/fabric/core/chaincode/shim/ext/statebased")

Constants

const (

 // RoleTypeMember标识组织的成员标识

    RoleTypeMember = RoleType("MEMBER")

//RoleTypePeer标识组织的对等节点

RoleTypePeer = RoleType("PEER")

)

type KeyEndorsementPolicy

type KeyEndorsementPolicy interface {

    // Policy 签名策略作为字节返回

    Policy() ([]byte, error)

 

    // AddOrgs 添加指定的组织到背书组织列表中,所有orgs MSP角色类型都将设置为第一个参数中指定的角色。在其他方面,期望的角色取决于通道的配置:如果它支持节点OU,则它可能是PEER角色,而如果不支持,则MEMBER角色是合适的角色。

    AddOrgs(roleType RoleType, organizations ...string) error

//DelOrgs从此KVS密钥背书策略中删除指定的通道组织。

    DelOrgs(organizations ...string)

    // ListOrgs 返回背书策略支持的组织列表

    ListOrgs() []string

}

KeyEndorsementPolicy提供了一组便捷方法来创建和修改基于状态的背书策略。由此便利层创建背书策略将始终是由调用者指定的一个或多个ORG的“<ORG> .peer”主体的逻辑AND。

func NewStateEP

func NewStateEP(policy []byte) (KeyEndorsementPolicy, error)

NewStateEP 从给定的序列化EP字节数组构造基于状态的认可策略。 如果字节数组为空,则创建新的EP。

type RoleType

type RoleType string

RoleType 背书策略身份

type RoleTypeDoesNotExistError

type RoleTypeDoesNotExistError struct {

    RoleType RoleType

}

如果与上面指定的角色类型不匹配的角色类型作为参数传递,则KeyEndorsementPolicy的函数AddOrgs将返回RoleTypeDoesNotExistError。

func (*RoleTypeDoesNotExistError) Error

func (r *RoleTypeDoesNotExistError) Error() string

package entities (import "github.com/hyperledger/fabric/core/chaincode/shim/ext/entities")

type BCCSPEncrypterEntity

type BCCSPEncrypterEntity struct {

    BCCSPEntity

    EKey  bccsp.Key

    EOpts bccsp.EncrypterOpts

    DOpts bccsp.DecrypterOpts

}

BCCSPEncrypterEntity是加密模块接口的实现

func NewAES256EncrypterEntity

func NewAES256EncrypterEntity(ID string, b bccsp.BCCSP, key, IV []byte) (*BCCSPEncrypterEntity, error)

NewAES256EncrypterEntity返回一个加密实体,它能够使用PKCS#7填充执行AES 256位加密。 可选地,可以提供IV,在这种情况下在加密期间使用; 另外,生成一个随机的。

func NewEncrypterEntity

func NewEncrypterEntity(ID string, bccsp bccsp.BCCSP, eKey bccsp.Key, eOpts bccsp.EncrypterOpts, dOpts bccsp.DecrypterOpts) (*BCCSPEncrypterEntity, error)

NewEncrypterEntity返回一个EncrypterEntity,它能够使用i)提供的BCCSP实例进行加密。 ii)提供的加密密钥和iii)提供的加密和解密选项。 实体的标识也作为参数提供 - 调用者有责任以有意义的方式选择它

func (*BCCSPEncrypterEntity) Decrypt //解码

func (e *BCCSPEncrypterEntity) Decrypt(ciphertext []byte) ([]byte, error)

func (*BCCSPEncrypterEntity) Encrypt  //加密

func (e *BCCSPEncrypterEntity) Encrypt(plaintext []byte) ([]byte, error)

func (*BCCSPEncrypterEntity) Equals  //等于

func (this *BCCSPEncrypterEntity) Equals(e Entity) bool

func (*BCCSPEncrypterEntity) Public  //公钥???

func (pe *BCCSPEncrypterEntity) Public() (Entity, error)

type BCCSPEncrypterSignerEntity

type BCCSPEncrypterSignerEntity struct {

    BCCSPEncrypterEntity

    BCCSPSignerEntity

}

BCCSPEncrypterSignerEntity 是一个加密签名模块的BCCSP接口。

func NewAES256EncrypterECDSASignerEntity

func NewAES256EncrypterECDSASignerEntity(ID string, b bccsp.BCCSP, encKeyBytes, signKeyBytes []byte) (*BCCSPEncrypterSignerEntity, error)

NewAES256EncrypterECDSASignerEntity返回一个加密实体,它能够使用PKCS#7填充执行AES 256位加密并使用ECDSA进行签名。

func NewEncrypterSignerEntity

func NewEncrypterSignerEntity(ID string, bccsp bccsp.BCCSP, eKey, sKey bccsp.Key, eOpts bccsp.EncrypterOpts, dOpts bccsp.DecrypterOpts, sOpts bccsp.SignerOpts, hOpts bccsp.HashOpts) (*BCCSPEncrypterSignerEntity, error)

NewEncrypterSignerEntity returns an EncrypterSignerEntity (which is also an EncrypterEntity)

能够执行加密和使用i)提供的BCCSP实例生成签名; ii)提供的加密和签名密钥以及iii)提供的加密,解密,签名和散列选项。 实体的标识符也作为参数提供 - 调用者有责任以有意义的方式选择它。

func (*BCCSPEncrypterSignerEntity) Equals

func (this *BCCSPEncrypterSignerEntity) Equals(e Entity) bool

func (*BCCSPEncrypterSignerEntity) ID

func (e *BCCSPEncrypterSignerEntity) ID() string

func (*BCCSPEncrypterSignerEntity) Public

func (pe *BCCSPEncrypterSignerEntity) Public() (Entity, error)

type BCCSPEntity

type BCCSPEntity struct {

    IDstr string

    BCCSP bccsp.BCCSP

}

BCCSPEntity 是包含BCCSP实例的Entity接口的实现。

func (*BCCSPEntity) ID

func (e *BCCSPEntity) ID() string

type BCCSPSignerEntity

type BCCSPSignerEntity struct {

    BCCSPEntity

    SKey  bccsp.Key

    SOpts bccsp.SignerOpts

    HOpts bccsp.HashOpts

}

BCCSPSignerEntity是一个签名模块接口的实现。

func NewECDSASignerEntity

func NewECDSASignerEntity(ID string, b bccsp.BCCSP, signKeyBytes []byte) (*BCCSPSignerEntity, error)

NewECDSASignerEntity 返回一个使用ECDSA签名的签名实例。

func NewECDSAVerifierEntity

func NewECDSAVerifierEntity(ID string, b bccsp.BCCSP, signKeyBytes []byte) (*BCCSPSignerEntity, error)

NewECDSAVerifierEntity 返回能够使用ECDSA进行验证的验证实例。

func NewSignerEntity

func NewSignerEntity(ID string, bccsp bccsp.BCCSP, sKey bccsp.Key, sOpts bccsp.SignerOpts, hOpts bccsp.HashOpts) (*BCCSPSignerEntity, error)

NewSignerEntity 返回签名者实例。

func (*BCCSPSignerEntity) Equals

func (this *BCCSPSignerEntity) Equals(e Entity) bool

func (*BCCSPSignerEntity) Public

func (e *BCCSPSignerEntity) Public() (Entity, error)

func (*BCCSPSignerEntity) Sign

func (e *BCCSPSignerEntity) Sign(msg []byte) ([]byte, error)

func (*BCCSPSignerEntity) Verify

func (e *BCCSPSignerEntity) Verify(signature, msg []byte) (bool, error)

type Encrypter

type Encrypter interface {

    // Encrypt 返回提供的纯文本消息的密文

    Encrypt(plaintext []byte) (ciphertext []byte, err error)

 

    // Decrypt返回解密密文消息的明文

    Decrypt(ciphertext []byte) (plaintext []byte, err error)

}

Encrypter 是一个提供基本加密/解密功能的接口

type EncrypterEntity

type EncrypterEntity interface {

    Entity

    Encrypter

}

Encrypter实例是能够执行加密的实例。

func GetEncrypterEntityForTest

func GetEncrypterEntityForTest(id string) (EncrypterEntity, error)

type EncrypterSignerEntity

type EncrypterSignerEntity interface {

    Entity

    Encrypter

    Signer

}

EncrypterSignerEntity是一个能够执行加密和生成签名的实体

func GetEncrypterSignerEntityForTest

func GetEncrypterSignerEntityForTest(id string) (EncrypterSignerEntity, error)

type Entity

type Entity interface {

   // ID返回实体的标识符;

     //标识符可以任意设置

     //实体的构造函数的方式与其在cc级别的使用相关

    ID() string

// Equals将此实体与提供的实体进行比较,并返回一个布尔值,如果两个实体相同则为true。 这包括实体使用的任何和所有关键材料

    Equals(Entity) bool

//如果使用非对称加密,则Public返回此实体的公共版本。 如果没有,Public返回自己

    Public() (Entity, error)

}

Entity is the basic interface for all crypto entities that are used by the library to obtain cc-level encryption

实体是一个基本接口,用于获取cc级加密库的所有加密实体。

type SignedMessage

type SignedMessage struct {

    // ID包含签署此消息的实体的描述

    ID  []byte `json:"id"`

 

// Payload包含已签名的消息

    Payload []byte `json:"payload"`

 

// Sig包含ID和Payload上的签名

    Sig []byte `json:"sig"`

}

//SignedMessage是一个简单的结构,包含payload和它上面的签名,以及签名,验证,序列化和反序列化的便利功能

func (*SignedMessage) FromBytes

func (m *SignedMessage) FromBytes(d []byte) error

FromBytes从提供的字节数组填充实例

func (*SignedMessage) Sign

func (m *SignedMessage) Sign(signer Signer) error

签署SignedMessage并将签名存储在Sig字段中

func (*SignedMessage) ToBytes

func (m *SignedMessage) ToBytes() ([]byte, error)

ToBytes将实例序列化为字节

func (*SignedMessage) Verify

func (m *SignedMessage) Verify(verifier Signer) (bool, error)

验证存储在Sig中的Payload上的签名

type Signer

type Signer interface {

// Sign返回所提供消息的签名(或错误)

    Sign(msg []byte) (signature []byte, err error)

 

    // 验证根据此接口检查提供的消息上提供的签名是否有效

    Verify(signature, msg []byte) (valid bool, err error)

}

Signer是一个提供基本签名/验证功能的接口

type SignerEntity

type SignerEntity interface {

    Entity

    Signer

}

SignerEntity是一个能够签名的实体

希望大家关注我的微信公众号,有疑问可以后台留言。

猜你喜欢

转载自blog.csdn.net/Rcvisual/article/details/88851762