1 Star 1 Fork 0

Hyperledger Fabric 国密/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
marshal.go 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
Jtyoui 提交于 2021-07-22 15:59 . 国密
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package server
import (
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"gitee.com/hyperledger-fabric-gm/fabric/common/util"
"gitee.com/hyperledger-fabric-gm/fabric/protos/token"
"github.com/pkg/errors"
)
// UnmarshalCommand unmarshal token.Command messages
func UnmarshalCommand(raw []byte) (*token.Command, error) {
command := &token.Command{}
err := proto.Unmarshal(raw, command)
if err != nil {
return nil, err
}
return command, nil
}
type TimeFunc func() time.Time
// ResponseMarshaler produces token.SignedCommandResponse
type ResponseMarshaler struct {
Signer Signer
Creator []byte
Time TimeFunc
}
func NewResponseMarshaler(signerID SignerIdentity) (*ResponseMarshaler, error) {
creator, err := signerID.Serialize()
if err != nil {
return nil, err
}
return &ResponseMarshaler{
Signer: signerID,
Creator: creator,
Time: time.Now,
}, nil
}
func (s *ResponseMarshaler) MarshalCommandResponse(command []byte, responsePayload interface{}) (*token.SignedCommandResponse, error) {
cr, err := commandResponseFromPayload(responsePayload)
if err != nil {
return nil, err
}
ts, err := ptypes.TimestampProto(s.Time())
if err != nil {
return nil, err
}
cr.Header = &token.CommandResponseHeader{
Creator: s.Creator,
CommandHash: util.ComputeSHA256(command),
Timestamp: ts,
}
return s.createSignedCommandResponse(cr)
}
func (s *ResponseMarshaler) createSignedCommandResponse(cr *token.CommandResponse) (*token.SignedCommandResponse, error) {
raw, err := proto.Marshal(cr)
if err != nil {
return nil, err
}
signature, err := s.Signer.Sign(raw)
if err != nil {
return nil, err
}
return &token.SignedCommandResponse{
Response: raw,
Signature: signature,
}, nil
}
func commandResponseFromPayload(payload interface{}) (*token.CommandResponse, error) {
switch t := payload.(type) {
case *token.CommandResponse_TokenTransaction:
return &token.CommandResponse{Payload: t}, nil
case *token.CommandResponse_Err:
return &token.CommandResponse{Payload: t}, nil
case *token.CommandResponse_UnspentTokens:
return &token.CommandResponse{Payload: t}, nil
default:
return nil, errors.Errorf("command type not recognized: %T", t)
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/hyperledger-fabric-gm/fabric.git
git@gitee.com:hyperledger-fabric-gm/fabric.git
hyperledger-fabric-gm
fabric
fabric
v1.4.9

搜索帮助