-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathenclave_key.go
50 lines (41 loc) · 1.29 KB
/
enclave_key.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
package crypto
import (
"crypto/ecdsa"
"fmt"
gethcrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/ten-protocol/go-ten/go/common"
)
func GenerateEnclaveKey() (*EnclaveKey, error) {
privKey, err := gethcrypto.GenerateKey()
if err != nil {
return nil, fmt.Errorf("could not generate enclave key. Cause: %w", err)
}
return NewEnclaveKey(privKey), nil
}
func NewEnclaveKey(privKey *ecdsa.PrivateKey) *EnclaveKey {
pubKey := gethcrypto.CompressPubkey(&privKey.PublicKey)
enclaveID := gethcrypto.PubkeyToAddress(privKey.PublicKey)
return &EnclaveKey{
privateKey: privKey,
publicKeyBytes: pubKey,
enclaveID: enclaveID,
}
}
// EnclaveKey - encapsulates behaviour for the enclave's private key (used to identify the enclave and sign messages)
type EnclaveKey struct {
privateKey *ecdsa.PrivateKey // generated by the enclave at startup, used to sign messages
enclaveID common.EnclaveID // the enclave's ID, derived from the public key
publicKeyBytes []byte
}
func (k *EnclaveKey) PrivateKey() *ecdsa.PrivateKey {
return k.privateKey
}
func (k *EnclaveKey) PublicKey() *ecdsa.PublicKey {
return &k.privateKey.PublicKey
}
func (k *EnclaveKey) EnclaveID() common.EnclaveID {
return k.enclaveID
}
func (k *EnclaveKey) PublicKeyBytes() []byte {
return k.publicKeyBytes
}