|
| 1 | +package integrityattestation |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/ecdsa" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + |
| 9 | + awsconfig "github.com/aws/aws-sdk-go-v2/config" |
| 10 | + "github.com/distributed-lab/enclave-extras/nitro" |
| 11 | + |
| 12 | + "github.com/offchainlabs/nitro/util/signature" |
| 13 | +) |
| 14 | + |
| 15 | +func ReadEnclavePrivateKey(attestationsPath string) (*ecdsa.PrivateKey, signature.DataSignerFunc, error) { |
| 16 | + if err := os.MkdirAll(attestationsPath, os.ModePerm); err != nil { |
| 17 | + return nil, nil, fmt.Errorf("failed to create attestations path directory %s with error: %w", attestationsPath, err) |
| 18 | + } |
| 19 | + |
| 20 | + awsConfig, err := awsconfig.LoadDefaultConfig(context.Background()) |
| 21 | + if err != nil { |
| 22 | + return nil, nil, fmt.Errorf("failed to load AWS config: %w", err) |
| 23 | + } |
| 24 | + |
| 25 | + kmsKeyID, err := nitro.GetAttestedKMSKeyID(awsConfig, attestationsPath) |
| 26 | + if err != nil { |
| 27 | + return nil, nil, fmt.Errorf("failed to get attested KMS Key ID: %w", err) |
| 28 | + } |
| 29 | + |
| 30 | + privateKey, err := nitro.GetAttestedPrivateKey(awsConfig, kmsKeyID, attestationsPath) |
| 31 | + if err != nil { |
| 32 | + return nil, nil, fmt.Errorf("failed to get attested private key: %w", err) |
| 33 | + } |
| 34 | + |
| 35 | + publicKey, err := nitro.GetAttestedPublicKey(privateKey, attestationsPath) |
| 36 | + if err != nil { |
| 37 | + return nil, nil, fmt.Errorf("failed to get attested public key: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + if _, err = nitro.GetAttestedAddress(publicKey, attestationsPath); err != nil { |
| 41 | + return nil, nil, fmt.Errorf("failed to get attested address: %w", err) |
| 42 | + } |
| 43 | + |
| 44 | + return privateKey, signature.DataSignerFromPrivateKey(privateKey), nil |
| 45 | +} |
0 commit comments