Skip to content

Commit

Permalink
refactor: encrypt bls key to json string
Browse files Browse the repository at this point in the history
  • Loading branch information
renlulu committed Aug 6, 2024
1 parent 67787e9 commit b069153
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 66 deletions.
2 changes: 1 addition & 1 deletion contracts/lib/eigenlayer-middleware
Submodule eigenlayer-middleware updated 54 files
+3 −0 .gitmodules
+3 −29 LICENSE
+10 −24 README.md
+ audits/Dedaub - Middleware Audit - Final - Feb'24.pdf
+2 −2 docs/README.md
+1 −1 docs/ServiceManagerBase.md
+1 −1 docs/experimental/AVS-Guide.md
+0 −1 foundry.toml
+1 −0 lib/ds-test
+1 −1 lib/eigenlayer-contracts
+1 −1 lib/forge-std
+4 −8 src/BLSApkRegistry.sol
+62 −116 src/BLSSignatureChecker.sol
+0 −175 src/EjectionManager.sol
+1 −5 src/IndexRegistry.sol
+0 −33 src/OperatorStateRetriever.sol
+9 −52 src/RegistryCoordinator.sol
+1 −6 src/RegistryCoordinatorStorage.sol
+28 −93 src/ServiceManagerBase.sol
+0 −53 src/ServiceManagerBaseStorage.sol
+3 −3 src/ServiceManagerRouter.sol
+10 −22 src/StakeRegistry.sol
+3 −24 src/interfaces/IECDSAStakeRegistryEventsAndErrors.sol
+0 −55 src/interfaces/IEjectionManager.sol
+41 −15 src/interfaces/IServiceManager.sol
+0 −61 src/interfaces/IServiceManagerUI.sol
+0 −283 src/unaudited/ECDSAServiceManagerBase.sol
+75 −204 src/unaudited/ECDSAStakeRegistry.sol
+3 −10 src/unaudited/ECDSAStakeRegistryStorage.sol
+10 −14 src/unaudited/examples/ECDSAStakeRegistryPermissioned.sol
+0 −69 test/events/IServiceManagerBaseEvents.sol
+1 −1 test/ffi/BLSPubKeyCompendiumFFI.t.sol
+1 −6 test/integration/CoreRegistration.t.sol
+61 −117 test/integration/IntegrationDeployer.t.sol
+1 −1 test/integration/TimeMachine.t.sol
+2 −4 test/integration/User.t.sol
+3 −3 test/mocks/DelegationMock.sol
+0 −22 test/mocks/ECDSAServiceManagerMock.sol
+0 −14 test/mocks/ECDSAStakeRegistryMock.sol
+0 −78 test/mocks/RewardsCoordinatorMock.sol
+3 −9 test/mocks/ServiceManagerMock.sol
+146 −368 test/unit/BLSApkRegistryUnit.t.sol
+1 −1 test/unit/BitmapUtils.t.sol
+0 −186 test/unit/ECDSAServiceManager.t.sol
+14 −60 test/unit/ECDSAStakeRegistryEqualWeightUnit.t.sol
+16 −48 test/unit/ECDSAStakeRegistryPermissionedUnit.t.sol
+105 −472 test/unit/ECDSAStakeRegistryUnit.t.sol
+0 −387 test/unit/EjectionManagerUnit.t.sol
+110 −266 test/unit/OperatorStateRetrieverUnit.t.sol
+0 −56 test/unit/RegistryCoordinatorUnit.t.sol
+0 −527 test/unit/ServiceManagerBase.t.sol
+0 −1 test/unit/ServiceManagerRouter.t.sol
+411 −820 test/unit/StakeRegistryUnit.t.sol
+91 −122 test/utils/MockAVSDeployer.sol
36 changes: 22 additions & 14 deletions crypto/bls/attestation.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,24 @@ func GenRandomBlsKeys() (*KeyPair, error) {

// SaveToFile saves the private key in an encrypted keystore file
func (k *KeyPair) SaveToFile(path string, password string) error {
data,err := k.EncryptedString(path, password)
if err != nil {
return err
}

dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Println("Error creating directories:", err)
return err
}
err = os.WriteFile(path, data, 0644)
if err != nil {
return err
}
return nil
}

func (k *KeyPair) EncryptedString(path string, password string) ([]byte,error) {
sk32Bytes := k.PrivKey.Bytes()
skBytes := make([]byte, 32)
for i := 0; i < 32; i++ {
Expand All @@ -199,7 +217,7 @@ func (k *KeyPair) SaveToFile(path string, password string) error {
keystore.StandardScryptP,
)
if err != nil {
return err
return nil,err
}

encryptedBLSStruct := encryptedBLSKeyJSONV3{
Expand All @@ -208,20 +226,10 @@ func (k *KeyPair) SaveToFile(path string, password string) error {
}
data, err := json.Marshal(encryptedBLSStruct)
if err != nil {
return err
return nil,err
}

dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
fmt.Println("Error creating directories:", err)
return err
}
err = os.WriteFile(path, data, 0644)
if err != nil {
return err
}
return nil
}
return data,nil
}

func ReadPrivateKeyFromFile(path string, password string) (*KeyPair, error) {
keyStoreContents, err := os.ReadFile(path)
Expand Down
32 changes: 17 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ require (
github.com/ethereum/go-ethereum v1.14.0
github.com/google/uuid v1.6.0
github.com/lmittmann/tint v1.0.4
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/client_golang v1.19.1
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.30.0
github.com/urfave/cli/v2 v2.27.1
Expand All @@ -32,8 +33,9 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/shurcooL/graphql v0.0.0-20230722043721-ed46e5a46466 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
Expand All @@ -54,7 +56,7 @@ require (
github.com/bits-and-blooms/bitset v1.10.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/consensys/bavard v0.1.13 // indirect
github.com/containerd/containerd v1.7.12 // indirect
github.com/containerd/log v0.1.0 // indirect
Expand All @@ -63,7 +65,7 @@ require (
github.com/crate-crypto/go-kzg-4844 v1.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set/v2 v2.1.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect
github.com/docker/docker v25.0.5+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand All @@ -73,9 +75,9 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/golang/protobuf v1.5.4 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/holiman/uint256 v1.2.4 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/klauspost/compress v1.17.9 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mmcloughlin/addchain v0.4.0 // indirect
Expand All @@ -88,9 +90,9 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.55.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shirou/gopsutil v3.21.4-0.20210419000835-c7a38de76ee5+incompatible // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
Expand All @@ -102,14 +104,14 @@ require (
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.22.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/mod v0.19.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/tools v0.20.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/tools v0.23.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
rsc.io/tmplfunc v0.0.3 // indirect
)
Loading

0 comments on commit b069153

Please sign in to comment.