Skip to content

Commit 04d3a4e

Browse files
committed
add crpto interface as repare for Hyperledger-TWGC#127
Signed-off-by: Sam Yuan <yy19902439@126.com>
1 parent e87f926 commit 04d3a4e

File tree

7 files changed

+63
-58
lines changed

7 files changed

+63
-58
lines changed

pkg/infra/assembler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Elements struct {
1717
}
1818

1919
type Assembler struct {
20-
Signer *Crypto
20+
Signer Crypto
2121
}
2222

2323
func (a *Assembler) assemble(e *Elements) (*Elements, error) {

pkg/infra/config.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package infra
22

33
import (
4+
"crypto/ecdsa"
5+
"crypto/x509"
6+
"encoding/pem"
47
"io/ioutil"
8+
"tape/internal/fabric/bccsp/utils"
59

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

68-
func (c Config) LoadCrypto() (*Crypto, error) {
72+
func (c Config) LoadCrypto() (*CryptoImpl, error) {
6973
var allcerts []string
7074
for _, p := range c.Endorsers {
7175
allcerts = append(allcerts, p.TLSCACert)
@@ -98,7 +102,7 @@ func (c Config) LoadCrypto() (*Crypto, error) {
98102
return nil, errors.Wrapf(err, "error get msp id")
99103
}
100104

101-
return &Crypto{
105+
return &CryptoImpl{
102106
Creator: name,
103107
PrivKey: priv,
104108
SignCert: cert,
@@ -136,3 +140,34 @@ func (n *Node) loadConfig() error {
136140
n.TLSCARootByte = TLSCARoot
137141
return nil
138142
}
143+
144+
func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) {
145+
in, err := ioutil.ReadFile(f)
146+
if err != nil {
147+
return nil, err
148+
}
149+
150+
k, err := utils.PEMtoPrivateKey(in, []byte{})
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
key, ok := k.(*ecdsa.PrivateKey)
156+
if !ok {
157+
return nil, errors.Errorf("expecting ecdsa key")
158+
}
159+
160+
return key, nil
161+
}
162+
163+
func GetCertificate(f string) (*x509.Certificate, []byte, error) {
164+
in, err := ioutil.ReadFile(f)
165+
if err != nil {
166+
return nil, nil, err
167+
}
168+
169+
block, _ := pem.Decode(in)
170+
171+
c, err := x509.ParseCertificate(block.Bytes)
172+
return c, in, err
173+
}

pkg/infra/crypto.go

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,12 @@ import (
66
"crypto/sha256"
77
"crypto/x509"
88
"encoding/asn1"
9-
"encoding/base64"
10-
"encoding/pem"
11-
"io/ioutil"
129
"math/big"
1310

1411
"tape/internal/fabric/bccsp/utils"
1512
"tape/internal/fabric/common/crypto"
1613

1714
"github.com/hyperledger/fabric-protos-go/common"
18-
"github.com/pkg/errors"
1915
)
2016

2117
type CryptoConfig struct {
@@ -29,13 +25,13 @@ type ECDSASignature struct {
2925
R, S *big.Int
3026
}
3127

32-
type Crypto struct {
28+
type CryptoImpl struct {
3329
Creator []byte
3430
PrivKey *ecdsa.PrivateKey
3531
SignCert *x509.Certificate
3632
}
3733

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

52-
func (s *Crypto) Serialize() ([]byte, error) {
48+
func (s *CryptoImpl) Serialize() ([]byte, error) {
5349
return s.Creator, nil
5450
}
5551

56-
func (s *Crypto) NewSignatureHeader() (*common.SignatureHeader, error) {
52+
func (s *CryptoImpl) NewSignatureHeader() (*common.SignatureHeader, error) {
5753
creator, err := s.Serialize()
5854
if err != nil {
5955
return nil, err
@@ -74,43 +70,3 @@ func digest(in []byte) []byte {
7470
h.Write(in)
7571
return h.Sum(nil)
7672
}
77-
78-
func toPEM(in []byte) ([]byte, error) {
79-
d := make([]byte, base64.StdEncoding.DecodedLen(len(in)))
80-
n, err := base64.StdEncoding.Decode(d, in)
81-
if err != nil {
82-
return nil, err
83-
}
84-
return d[:n], nil
85-
}
86-
87-
func GetPrivateKey(f string) (*ecdsa.PrivateKey, error) {
88-
in, err := ioutil.ReadFile(f)
89-
if err != nil {
90-
return nil, err
91-
}
92-
93-
k, err := utils.PEMtoPrivateKey(in, []byte{})
94-
if err != nil {
95-
return nil, err
96-
}
97-
98-
key, ok := k.(*ecdsa.PrivateKey)
99-
if !ok {
100-
return nil, errors.Errorf("expecting ecdsa key")
101-
}
102-
103-
return key, nil
104-
}
105-
106-
func GetCertificate(f string) (*x509.Certificate, []byte, error) {
107-
in, err := ioutil.ReadFile(f)
108-
if err != nil {
109-
return nil, nil, err
110-
}
111-
112-
block, _ := pem.Decode(in)
113-
114-
c, err := x509.ParseCertificate(block.Bytes)
115-
return c, in, err
116-
}

pkg/infra/initiator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"golang.org/x/time/rate"
88
)
99

10-
func StartCreateProposal(num int, burst int, r float64, config Config, crypto *Crypto, raw chan *Elements, errorCh chan error) {
10+
func StartCreateProposal(num int, burst int, r float64, config Config, crypto Crypto, raw chan *Elements, errorCh chan error) {
1111
limit := rate.Inf
1212
ctx := context.Background()
1313
if r > 0 {

pkg/infra/interface.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package infra
2+
3+
import (
4+
"github.com/hyperledger/fabric-protos-go/common"
5+
)
6+
7+
//to do for #127 SM crypto
8+
//just need to do an impl for this interface and replace
9+
10+
type Crypto interface {
11+
NewSignatureHeader() (*common.SignatureHeader, error)
12+
Serialize() ([]byte, error)
13+
Sign(message []byte) ([]byte, error)
14+
}

pkg/infra/observer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Observer struct {
2020
logger *log.Logger
2121
}
2222

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

42-
func CreateObserver(ctx context.Context, channel string, node Node, crypto *Crypto, logger *log.Logger) (*Observer, error) {
42+
func CreateObserver(ctx context.Context, channel string, node Node, crypto Crypto, logger *log.Logger) (*Observer, error) {
4343
seek, err := CreateSignedDeliverNewestEnv(channel, crypto)
4444
if err != nil {
4545
return nil, err

pkg/infra/proposal.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"github.com/pkg/errors"
1414
)
1515

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

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

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

155-
func CreateSignedDeliverNewestEnv(ch string, signer *Crypto) (*common.Envelope, error) {
155+
func CreateSignedDeliverNewestEnv(ch string, signer Crypto) (*common.Envelope, error) {
156156
start := &orderer.SeekPosition{
157157
Type: &orderer.SeekPosition_Newest{
158158
Newest: &orderer.SeekNewest{},

0 commit comments

Comments
 (0)