forked from hyperledger/fabric
-
Notifications
You must be signed in to change notification settings - Fork 1
/
signer.go
68 lines (56 loc) · 1.66 KB
/
signer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package crypto
import (
cb "github.com/hyperledger/fabric/protos/common"
)
// LocalSigner is a temporary stub interface which will be implemented by the local MSP
type LocalSigner interface {
SignatureHeaderMaker
Signer
}
// Signer signs messages
type Signer interface {
// Sign a message and return the signature over the digest, or error on failure
Sign(message []byte) ([]byte, error)
}
// IdentitySerializer serializes identities
type IdentitySerializer interface {
// Serialize converts an identity to bytes
Serialize() ([]byte, error)
}
// SignatureHeaderMaker creates a new SignatureHeader
type SignatureHeaderMaker interface {
// NewSignatureHeader creates a SignatureHeader with the correct signing identity and a valid nonce
NewSignatureHeader() (*cb.SignatureHeader, error)
}
// SignatureHeaderCreator creates signature headers
type SignatureHeaderCreator struct {
SignerSupport
}
// SignerSupport implements the needed support for LocalSigner
type SignerSupport interface {
Signer
IdentitySerializer
}
// NewSignatureHeaderCreator creates new signature headers
func NewSignatureHeaderCreator(ss SignerSupport) *SignatureHeaderCreator {
return &SignatureHeaderCreator{ss}
}
// NewSignatureHeader creates a SignatureHeader with the correct signing identity and a valid nonce
func (bs *SignatureHeaderCreator) NewSignatureHeader() (*cb.SignatureHeader, error) {
creator, err := bs.Serialize()
if err != nil {
return nil, err
}
nonce, err := GetRandomNonce()
if err != nil {
return nil, err
}
return &cb.SignatureHeader{
Creator: creator,
Nonce: nonce,
}, nil
}