-
Notifications
You must be signed in to change notification settings - Fork 26
/
signature_scheme.go
92 lines (72 loc) · 2.2 KB
/
signature_scheme.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package zcncrypto
import (
"encoding/json"
"github.com/0chain/errors"
"github.com/0chain/gosdk/core/encryption"
"github.com/tyler-smith/go-bip39"
)
const CryptoVersion = "1.0"
// KeyPair private and publickey
type KeyPair struct {
PublicKey string `json:"public_key"`
PrivateKey string `json:"private_key"`
}
// Wallet structure
type Wallet struct {
ClientID string `json:"client_id"`
ClientKey string `json:"client_key"`
Keys []KeyPair `json:"keys"`
Mnemonic string `json:"mnemonics"`
Version string `json:"version"`
DateCreated string `json:"date_created"`
}
//SignatureScheme - an encryption scheme for signing and verifying messages
type SignatureScheme interface {
// Generate fresh keys
GenerateKeys() (*Wallet, error)
// Generate fresh keys based on eth wallet
GenerateKeysWithEth(mnemonic, password string) (*Wallet, error)
// Generate keys from mnemonic for recovery
RecoverKeys(mnemonic string) (*Wallet, error)
GetMnemonic() string
// Signing - Set private key to sign
SetPrivateKey(privateKey string) error
Sign(hash string) (string, error)
// Signature verification - Set public key to verify
SetPublicKey(publicKey string) error
GetPublicKey() string
GetPrivateKey() string
Verify(signature string, msg string) (bool, error)
// Combine signature for schemes BLS
Add(signature, msg string) (string, error)
// implement SplitSignatureScheme
SplitKeys(numSplits int) (*Wallet, error)
GetPrivateKeyAsByteArray() ([]byte, error)
// // implement ThresholdSignatureScheme
// SetID(id string) error
// GetID() string
}
// SplitSignatureScheme splits the primary key into number of parts.
type SplitSignatureScheme interface {
SignatureScheme
SplitKeys(numSplits int) (*Wallet, error)
}
type ThresholdSignatureScheme interface {
SignatureScheme
SetID(id string) error
GetID() string
}
// Marshal returns json string
func (w *Wallet) Marshal() (string, error) {
ws, err := json.Marshal(w)
if err != nil {
return "", errors.New("wallet_marshal", "Invalid Wallet")
}
return string(ws), nil
}
func IsMnemonicValid(mnemonic string) bool {
return bip39.IsMnemonicValid(mnemonic)
}
func Sha3Sum256(data string) string {
return encryption.Hash(data)
}