github.com/0chain/gosdk@v1.17.11/core/zcncrypto/signature_scheme.go (about) 1 // Provides low-level functions and types to work with different cryptographic schemes with a unified interface and provide cryptographic operations. 2 package zcncrypto 3 4 import ( 5 "encoding/json" 6 "fmt" 7 "os" 8 9 "github.com/0chain/errors" 10 "github.com/0chain/gosdk/core/encryption" 11 "github.com/tyler-smith/go-bip39" 12 ) 13 14 // CryptoVersion - version of the crypto library 15 const CryptoVersion = "1.0" 16 17 // KeyPair private and publickey 18 type KeyPair struct { 19 PublicKey string `json:"public_key"` 20 PrivateKey string `json:"private_key"` 21 } 22 23 // Wallet represents client wallet information 24 type Wallet struct { 25 ClientID string `json:"client_id"` 26 ClientKey string `json:"client_key"` 27 PeerPublicKey string `json:"peer_public_key"` // Peer public key exists only in split wallet 28 Keys []KeyPair `json:"keys"` 29 Mnemonic string `json:"mnemonics"` 30 Version string `json:"version"` 31 DateCreated string `json:"date_created"` 32 Nonce int64 `json:"nonce"` 33 IsSplit bool `json:"is_split"` 34 } 35 36 // SignatureScheme - an encryption scheme for signing and verifying messages 37 type SignatureScheme interface { 38 // Generate fresh keys 39 GenerateKeys() (*Wallet, error) 40 // Generate fresh keys based on eth wallet 41 GenerateKeysWithEth(mnemonic, password string) (*Wallet, error) 42 43 // Generate keys from mnemonic for recovery 44 RecoverKeys(mnemonic string) (*Wallet, error) 45 GetMnemonic() string 46 47 // Signing - Set private key to sign 48 SetPrivateKey(privateKey string) error 49 Sign(hash string) (string, error) 50 51 // Signature verification - Set public key to verify 52 SetPublicKey(publicKey string) error 53 GetPublicKey() string 54 GetPrivateKey() string 55 Verify(signature string, msg string) (bool, error) 56 57 // Combine signature for schemes BLS 58 Add(signature, msg string) (string, error) 59 60 // implement SplitSignatureScheme 61 62 SplitKeys(numSplits int) (*Wallet, error) 63 64 GetPrivateKeyAsByteArray() ([]byte, error) 65 66 // // implement ThresholdSignatureScheme 67 68 SetID(id string) error 69 GetID() string 70 } 71 72 // Marshal returns json string 73 func (w *Wallet) Marshal() (string, error) { 74 ws, err := json.Marshal(w) 75 if err != nil { 76 return "", errors.New("wallet_marshal", "Invalid Wallet") 77 } 78 return string(ws), nil 79 } 80 81 func (w *Wallet) Sign(hash, scheme string) (string, error) { 82 sigScheme := NewSignatureScheme(scheme) 83 err := sigScheme.SetPrivateKey(w.Keys[0].PrivateKey) 84 if err != nil { 85 return "", err 86 } 87 return sigScheme.Sign(hash) 88 } 89 90 // SetSplitKeys sets split keys and wipes out mnemonic and original primary keys 91 func (w *Wallet) SetSplitKeys(sw *Wallet) { 92 *w = *sw 93 } 94 95 func (w *Wallet) SaveTo(file string) error { 96 d, err := json.Marshal(w) 97 if err != nil { 98 return err 99 } 100 101 fmt.Println("Saving wallet to file: ", string(d)) 102 103 return os.WriteFile(file, d, 0644) 104 } 105 106 func IsMnemonicValid(mnemonic string) bool { 107 return bip39.IsMnemonicValid(mnemonic) 108 } 109 110 func Sha3Sum256(data string) string { 111 return encryption.Hash(data) 112 }