代码拉取完成,页面将自动刷新
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package client
import (
"context"
"io"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
"github.com/hyperledger/fabric/protos/token"
tk "github.com/hyperledger/fabric/token"
"github.com/pkg/errors"
)
type TimeFunc func() time.Time
type ProverPeer struct {
ChannelID string
ProverClient token.ProverClient
RandomnessReader io.Reader
Time TimeFunc
}
func (prover *ProverPeer) RequestImport(tokensToIssue []*token.TokenToIssue, signingIdentity tk.SigningIdentity) ([]byte, error) {
ir := &token.ImportRequest{
TokensToIssue: tokensToIssue,
}
payload := &token.Command_ImportRequest{ImportRequest: ir}
sc, err := prover.CreateSignedCommand(payload, signingIdentity)
if err != nil {
return nil, err
}
scr, err := prover.ProverClient.ProcessCommand(context.Background(), sc)
if err != nil {
return nil, err
}
return scr.Response, nil
}
func (prover *ProverPeer) RequestTransfer(
tokenIDs [][]byte,
shares []*token.RecipientTransferShare,
signingIdentity tk.SigningIdentity) ([]byte, error) {
tr := &token.TransferRequest{
Shares: shares,
TokenIds: tokenIDs,
}
payload := &token.Command_TransferRequest{TransferRequest: tr}
sc, err := prover.CreateSignedCommand(payload, signingIdentity)
if err != nil {
return nil, err
}
scr, err := prover.ProverClient.ProcessCommand(context.Background(), sc)
if err != nil {
return nil, err
}
return scr.Response, nil
}
func (prover *ProverPeer) CreateSignedCommand(payload interface{}, signingIdentity tk.SigningIdentity) (*token.SignedCommand, error) {
command, err := commandFromPayload(payload)
if err != nil {
return nil, err
}
nonce := make([]byte, 32)
_, err = io.ReadFull(prover.RandomnessReader, nonce)
if err != nil {
return nil, err
}
ts, err := ptypes.TimestampProto(prover.Time())
if err != nil {
return nil, err
}
creator, err := signingIdentity.GetPublicVersion().Serialize()
if err != nil {
return nil, err
}
header := &token.Header{Timestamp: ts,
Nonce: nonce,
Creator: creator,
ChannelId: prover.ChannelID,
}
command.Header = header
raw, err := proto.Marshal(command)
if err != nil {
return nil, err
}
signature, err := signingIdentity.Sign(raw)
if err != nil {
return nil, err
}
sc := &token.SignedCommand{
Command: raw,
Signature: signature,
}
return sc, nil
}
func commandFromPayload(payload interface{}) (*token.Command, error) {
switch t := payload.(type) {
case *token.Command_ImportRequest:
return &token.Command{Payload: t}, nil
case *token.Command_TransferRequest:
return &token.Command{Payload: t}, nil
default:
return nil, errors.Errorf("command type not recognized: %T", t)
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。