aboutsummaryrefslogtreecommitdiffstats
path: root/core/crypto/utils.go
diff options
context:
space:
mode:
authorJimmy Hu <jimmy.hu@dexon.org>2018-09-26 17:13:37 +0800
committerGitHub <noreply@github.com>2018-09-26 17:13:37 +0800
commit7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe (patch)
tree41b823a05f81615558a37567dab85e2958b59329 /core/crypto/utils.go
parent663817d3e0d5a3c28cb0c5e378a533e242af5fdf (diff)
downloadtangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.tar
tangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.tar.gz
tangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.tar.bz2
tangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.tar.lz
tangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.tar.xz
tangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.tar.zst
tangerine-consensus-7450e6ba7f7299d03b04a7e2a9b3bc5911b94cfe.zip
crypto: sigtopub to crypto package. remove SigToPubFn (#141)
Diffstat (limited to 'core/crypto/utils.go')
-rw-r--r--core/crypto/utils.go43
1 files changed, 41 insertions, 2 deletions
diff --git a/core/crypto/utils.go b/core/crypto/utils.go
index 07a8b2b..cb4decd 100644
--- a/core/crypto/utils.go
+++ b/core/crypto/utils.go
@@ -19,12 +19,30 @@ package crypto
import (
"encoding/hex"
+ "fmt"
"github.com/ethereum/go-ethereum/crypto"
"github.com/dexon-foundation/dexon-consensus-core/common"
)
+var (
+ // ErrSigToPubTypeNotFound is reported if the type is already used.
+ ErrSigToPubTypeNotFound = fmt.Errorf("type of sigToPub is not found")
+
+ // ErrSigToPubTypeAlreadyExist is reported if the type is already used.
+ ErrSigToPubTypeAlreadyExist = fmt.Errorf("type of sigToPub is already exist")
+)
+
+// SigToPubFn is a function to recover public key from signature.
+type SigToPubFn func(hash common.Hash, signature Signature) (PublicKey, error)
+
+var sigToPubCB map[string]SigToPubFn
+
+func init() {
+ sigToPubCB = make(map[string]SigToPubFn)
+}
+
// Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) (h common.Hash) {
@@ -33,9 +51,30 @@ func Keccak256Hash(data ...[]byte) (h common.Hash) {
// Clone returns a deep copy of a signature.
func (sig Signature) Clone() Signature {
- return append(Signature{}, sig...)
+ return Signature{
+ Type: sig.Type,
+ Signature: sig.Signature[:],
+ }
}
func (sig Signature) String() string {
- return hex.EncodeToString([]byte(sig[:]))
+ return hex.EncodeToString([]byte(sig.Signature[:]))
+}
+
+// RegisterSigToPub registers a sigToPub function of type.
+func RegisterSigToPub(sigType string, sigToPub SigToPubFn) error {
+ if _, exist := sigToPubCB[sigType]; exist {
+ return ErrSigToPubTypeAlreadyExist
+ }
+ sigToPubCB[sigType] = sigToPub
+ return nil
+}
+
+// SigToPub recovers public key from signature.
+func SigToPub(hash common.Hash, signature Signature) (PublicKey, error) {
+ sigToPub, exist := sigToPubCB[signature.Type]
+ if !exist {
+ return nil, ErrSigToPubTypeNotFound
+ }
+ return sigToPub(hash, signature)
}