Skip to content

Commit

Permalink
add crpto interface as repare for Hyperledger-TWGC#127
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Yuan <yy19902439@126.com>
  • Loading branch information
SamYuan1990 committed Jul 3, 2021
1 parent e87f926 commit 0334dc7
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 58 deletions.
2 changes: 1 addition & 1 deletion pkg/infra/assembler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Elements struct {
}

type Assembler struct {
Signer *Crypto
Signer Crypto
}

func (a *Assembler) assemble(e *Elements) (*Elements, error) {
Expand Down
39 changes: 37 additions & 2 deletions pkg/infra/config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package infra

import (
"crypto/ecdsa"
"crypto/x509"
"encoding/pem"
"io/ioutil"
"tape/internal/fabric/bccsp/utils"

"github.com/gogo/protobuf/proto"
"github.com/hyperledger/fabric-protos-go/msp"
Expand Down Expand Up @@ -65,7 +69,7 @@ func LoadConfig(f string) (Config, error) {
return config, nil
}

func (c Config) LoadCrypto() (*Crypto, error) {
func (c Config) LoadCrypto() (*CryptoImpl, error) {
var allcerts []string
for _, p := range c.Endorsers {
allcerts = append(allcerts, p.TLSCACert)
Expand Down Expand Up @@ -98,7 +102,7 @@ func (c Config) LoadCrypto() (*Crypto, error) {
return nil, errors.Wrapf(err, "error get msp id")
}

return &Crypto{
return &CryptoImpl{
Creator: name,
PrivKey: priv,
SignCert: cert,
Expand Down Expand Up @@ -136,3 +140,34 @@ func (n *Node) loadConfig() error {
n.TLSCARootByte = TLSCARoot
return nil
}

func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) {
in, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}

k, err := utils.PEMtoPrivateKey(in, []byte{})
if err != nil {
return nil, err
}

key, ok := k.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.Errorf("expecting ecdsa key")
}

return key, nil
}

func GetCertificate(f string) (*x509.Certificate, []byte, error) {
in, err := ioutil.ReadFile(f)
if err != nil {
return nil, nil, err
}

block, _ := pem.Decode(in)

c, err := x509.ParseCertificate(block.Bytes)
return c, in, err
}
52 changes: 4 additions & 48 deletions pkg/infra/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ import (
"crypto/sha256"
"crypto/x509"
"encoding/asn1"
"encoding/base64"
"encoding/pem"
"io/ioutil"
"math/big"

"tape/internal/fabric/bccsp/utils"
"tape/internal/fabric/common/crypto"

"github.com/hyperledger/fabric-protos-go/common"
"github.com/pkg/errors"
)

type CryptoConfig struct {
Expand All @@ -29,13 +25,13 @@ type ECDSASignature struct {
R, S *big.Int
}

type Crypto struct {
type CryptoImpl struct {
Creator []byte
PrivKey *ecdsa.PrivateKey
SignCert *x509.Certificate
}

func (s *Crypto) Sign(message []byte) ([]byte, error) {
func (s *CryptoImpl) Sign(message []byte) ([]byte, error) {
ri, si, err := ecdsa.Sign(rand.Reader, s.PrivKey, digest(message))
if err != nil {
return nil, err
Expand All @@ -49,11 +45,11 @@ func (s *Crypto) Sign(message []byte) ([]byte, error) {
return asn1.Marshal(ECDSASignature{ri, si})
}

func (s *Crypto) Serialize() ([]byte, error) {
func (s *CryptoImpl) Serialize() ([]byte, error) {
return s.Creator, nil
}

func (s *Crypto) NewSignatureHeader() (*common.SignatureHeader, error) {
func (s *CryptoImpl) NewSignatureHeader() (*common.SignatureHeader, error) {
creator, err := s.Serialize()
if err != nil {
return nil, err
Expand All @@ -74,43 +70,3 @@ func digest(in []byte) []byte {
h.Write(in)
return h.Sum(nil)
}

func toPEM(in []byte) ([]byte, error) {
d := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
n, err := base64.StdEncoding.Decode(d, in)
if err != nil {
return nil, err
}
return d[:n], nil
}

func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) {
in, err := ioutil.ReadFile(f)
if err != nil {
return nil, err
}

k, err := utils.PEMtoPrivateKey(in, []byte{})
if err != nil {
return nil, err
}

key, ok := k.(*ecdsa.PrivateKey)
if !ok {
return nil, errors.Errorf("expecting ecdsa key")
}

return key, nil
}

func GetCertificate(f string) (*x509.Certificate, []byte, error) {
in, err := ioutil.ReadFile(f)
if err != nil {
return nil, nil, err
}

block, _ := pem.Decode(in)

c, err := x509.ParseCertificate(block.Bytes)
return c, in, err
}
2 changes: 1 addition & 1 deletion pkg/infra/initiator.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"golang.org/x/time/rate"
)

func StartCreateProposal(num int, burst int, r float64, config Config, crypto *Crypto, raw chan *Elements, errorCh chan error) {
func StartCreateProposal(num int, burst int, r float64, config Config, crypto Crypto, raw chan *Elements, errorCh chan error) {
limit := rate.Inf
ctx := context.Background()
if r > 0 {
Expand Down
17 changes: 17 additions & 0 deletions pkg/infra/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package infra

import (
"github.com/hyperledger/fabric-protos-go/common"
)

/*
to do for #127 SM crypto
just need to do an impl for this interface and replace
and impl a function for func (c Config) LoadCrypto() (*CryptoImpl, error) {
as generator
*/
type Crypto interface {
NewSignatureHeader() (*common.SignatureHeader, error)
Serialize() ([]byte, error)
Sign(message []byte) ([]byte, error)
}
4 changes: 2 additions & 2 deletions pkg/infra/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Observer struct {
logger *log.Logger
}

func CreateObservers(ctx context.Context, channel string, nodes []Node, crypto *Crypto, logger *log.Logger) (*Observers, error) {
func CreateObservers(ctx context.Context, channel string, nodes []Node, crypto Crypto, logger *log.Logger) (*Observers, error) {
var workers []*Observer
for i, node := range nodes {
worker, err := CreateObserver(ctx, channel, node, crypto, logger)
Expand All @@ -39,7 +39,7 @@ func (o *Observers) Start(errorCh chan error, blockCh chan<- *AddressedBlock, no
}
}

func CreateObserver(ctx context.Context, channel string, node Node, crypto *Crypto, logger *log.Logger) (*Observer, error) {
func CreateObserver(ctx context.Context, channel string, node Node, crypto Crypto, logger *log.Logger) (*Observer, error) {
seek, err := CreateSignedDeliverNewestEnv(channel, crypto)
if err != nil {
return nil, err
Expand Down
8 changes: 4 additions & 4 deletions pkg/infra/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/pkg/errors"
)

func CreateProposal(signer *Crypto, channel, ccname, version string, args ...string) (*peer.Proposal, error) {
func CreateProposal(signer Crypto, channel, ccname, version string, args ...string) (*peer.Proposal, error) {
var argsInByte [][]byte
for _, arg := range args {
argsInByte = append(argsInByte, []byte(arg))
Expand All @@ -40,7 +40,7 @@ func CreateProposal(signer *Crypto, channel, ccname, version string, args ...str
return prop, nil
}

func SignProposal(prop *peer.Proposal, signer *Crypto) (*peer.SignedProposal, error) {
func SignProposal(prop *peer.Proposal, signer Crypto) (*peer.SignedProposal, error) {
propBytes, err := proto.Marshal(prop)
if err != nil {
return nil, err
Expand All @@ -54,7 +54,7 @@ func SignProposal(prop *peer.Proposal, signer *Crypto) (*peer.SignedProposal, er
return &peer.SignedProposal{ProposalBytes: propBytes, Signature: sig}, nil
}

func CreateSignedTx(proposal *peer.Proposal, signer *Crypto, resps []*peer.ProposalResponse) (*common.Envelope, error) {
func CreateSignedTx(proposal *peer.Proposal, signer Crypto, resps []*peer.ProposalResponse) (*common.Envelope, error) {
if len(resps) == 0 {
return nil, errors.Errorf("at least one proposal response is required")
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func CreateSignedTx(proposal *peer.Proposal, signer *Crypto, resps []*peer.Propo
return &common.Envelope{Payload: paylBytes, Signature: sig}, nil
}

func CreateSignedDeliverNewestEnv(ch string, signer *Crypto) (*common.Envelope, error) {
func CreateSignedDeliverNewestEnv(ch string, signer Crypto) (*common.Envelope, error) {
start := &orderer.SeekPosition{
Type: &orderer.SeekPosition_Newest{
Newest: &orderer.SeekNewest{},
Expand Down

0 comments on commit 0334dc7

Please sign in to comment.