-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
61 lines (46 loc) · 1.46 KB
/
main.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
package main
import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"encoding/json"
"fmt"
"github.com/skip-mev/go-fast-solver/shared/keys"
"os"
)
func main() {
solverKeys := map[string]map[string]string{
"chain1": {"private_key": "key1"},
"chain2": {"private_key": "key2"},
}
jsonData, _ := json.Marshal(solverKeys)
// Create a test AES key
aesKey := []byte("0123456789abcdef0123456789abcdef")
aesKeyHex := hex.EncodeToString(aesKey)
// set key environment variable
os.Setenv("AES_KEY_HEX", aesKeyHex)
// Encrypt the test data
encryptedData := encryptSolverKeys(jsonData, aesKey)
encryptedDataHex := hex.EncodeToString(encryptedData)
// Create a temporary file with encrypted data
testFile := createTempFileWithContent([]byte(encryptedDataHex))
defer os.Remove(testFile.Name())
// Load keys from encrypted file
result, _ := keys.LoadKeyStoreFromEncryptedFile(testFile.Name())
resultJson, _ := json.Marshal(result)
// This is just an example, of course don't log your real private keys
fmt.Printf("Decrypted keys %s", string(resultJson))
}
func createTempFileWithContent(content []byte) *os.File {
file, _ := os.CreateTemp("", "keystore_test")
_, _ = file.Write(content)
_ = file.Close()
return file
}
func encryptSolverKeys(data []byte, key []byte) []byte {
block, _ := aes.NewCipher(key)
gcm, _ := cipher.NewGCM(block)
nonce := make([]byte, gcm.NonceSize())
ciphertext := gcm.Seal(nil, nonce, data, nil)
return append(nonce, ciphertext...)
}