github.com/quantosnetwork/Quantos@v0.0.0-20220306172517-e20b28c5a29a/crypto/keys.go (about) 1 package crypto 2 3 import ( 4 "crypto/ed25519" 5 "encoding/hex" 6 "encoding/json" 7 "log" 8 "lukechampine.com/frand" 9 _ "go.dedis.ch/kyber/v3" 10 _ "go.dedis.ch/kyber/v3/suites" 11 ) 12 13 14 const ( 15 // SizePublicKey is the size in bytes of a nodes/peers public key. 16 SizePublicKey = ed25519.PublicKeySize 17 18 // SizePrivateKey is the size in bytes of a nodes/peers private key. 19 SizePrivateKey = ed25519.PrivateKeySize 20 21 // SizeSignature is the size in bytes of a cryptographic signature. 22 SizeSignature = ed25519.SignatureSize 23 ) 24 25 type PublicKey [SizePublicKey]byte 26 type PrivateKey [SizePrivateKey]byte 27 type SharedKey interface{} 28 29 30 var ( 31 // ZeroPublicKey is the zero-value for a node/peer public key. 32 ZeroPublicKey PublicKey 33 34 // ZeroPrivateKey is the zero-value for a node/peer private key. 35 ZeroPrivateKey PrivateKey 36 37 // ZeroSignature is the zero-value for a cryptographic signature. 38 ZeroSignature Signature 39 ) 40 41 42 type Keys struct { 43 pk PublicKey 44 sk PrivateKey 45 sharedKey SharedKey 46 kSig Signature 47 } 48 49 func GenerateKeys(rand frand.RNG) *Keys { 50 pub, priv, err := ed25519.GenerateKey(&rand) 51 if err != nil { 52 log.Fatal(err) 53 } 54 k := &Keys{} 55 copy(k.pk[:], pub) 56 copy(k.sk[:], priv) 57 58 return k 59 60 } 61 62 func (k PublicKey) String() string { 63 return hex.EncodeToString(k[:]) 64 } 65 66 func (k PublicKey) Address() string { 67 return "0x"+k.String() 68 } 69 70 func (k PublicKey) ToJSON() ([]byte, error) { 71 return json.Marshal(k.String()) 72 } 73