Skip to content

Commit 1bcc36f

Browse files
committed
Problem: no exposed db keyring used in the keystore
simpily new keyring
1 parent d41d670 commit 1bcc36f

File tree

6 files changed

+33
-176
lines changed

6 files changed

+33
-176
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
* (e2ee)[#1421](https://github.com/crypto-org-chain/cronos/pull/1421) Validate e2ee key when register.
2222
* (store) [#1448](https://github.com/crypto-org-chain/cronos/pull/1448) Upgrade rocksdb to `v9.1.1`.
2323
* [#1431](https://github.com/crypto-org-chain/cronos/pull/1431) Integrate testground to run benchmark on cluster.
24+
* (e2ee)[#](https://github.com/crypto-org-chain/cronos/pull/) Simpily new keyring for e2ee module.
2425

2526
### Bug Fixes
2627

go.mod

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ require (
4242
github.com/spf13/pflag v1.0.5
4343
github.com/spf13/viper v1.18.2
4444
github.com/stretchr/testify v1.9.0
45-
github.com/test-go/testify v1.1.4
46-
golang.org/x/crypto v0.21.0
4745
google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de
4846
google.golang.org/grpc v1.63.2
4947
google.golang.org/protobuf v1.33.0
@@ -228,6 +226,7 @@ require (
228226
go.opentelemetry.io/otel/metric v1.22.0 // indirect
229227
go.opentelemetry.io/otel/trace v1.22.0 // indirect
230228
go.uber.org/multierr v1.11.0 // indirect
229+
golang.org/x/crypto v0.21.0 // indirect
231230
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
232231
golang.org/x/mod v0.15.0 // indirect
233232
golang.org/x/net v0.23.0 // indirect
@@ -253,10 +252,10 @@ require (
253252

254253
// release/v0.50.x
255254
replace (
256-
cosmossdk.io/client/v2 => github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240415105151-0108877a3201
257-
cosmossdk.io/store => github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240415105151-0108877a3201
258-
cosmossdk.io/x/tx => github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240415105151-0108877a3201
259-
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240415105151-0108877a3201
255+
cosmossdk.io/client/v2 => github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240603035522-8d30134159e0
256+
cosmossdk.io/store => github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240603035522-8d30134159e0
257+
cosmossdk.io/x/tx => github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240603035522-8d30134159e0
258+
github.com/cosmos/cosmos-sdk => github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240603035522-8d30134159e0
260259
)
261260

262261
replace (

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,14 @@ github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c h1:MOgfS4+F
422422
github.com/crypto-org-chain/btree v0.0.0-20240406140148-2687063b042c/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
423423
github.com/crypto-org-chain/cometbft-db v0.0.0-20231011055109-57922ac52a63 h1:R1QJ9a3XdYMSKo+1RdFifxb/g3lNypC52L/rpYrWoKo=
424424
github.com/crypto-org-chain/cometbft-db v0.0.0-20231011055109-57922ac52a63/go.mod h1:rocwIfnS+kA060x64gkSIRvWB9StSppIkJuo5MWzL24=
425-
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240415105151-0108877a3201 h1:3R54xSBI4geLX3H5Ljk0FSVDMPRpywq5L5K//aHwo8s=
426-
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240415105151-0108877a3201/go.mod h1:DkCxCPi3veciROiq36PbpXDhboMjqHAS0Xyv2dEEW04=
427-
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240415105151-0108877a3201 h1:O1qN/jmh/BZK6JiEADgwDK7jwSQNKqvs5qVYjo0UFa4=
428-
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240415105151-0108877a3201/go.mod h1:GjpaaxatOEnkSlNKjYxSbzVVtHHAG0tWm26AoPA1g9Q=
429-
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240415105151-0108877a3201 h1:0T8U5tgQLfD8k8kxisez5ks9s7yxU2JSRhi5MUQ0Cp0=
430-
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240415105151-0108877a3201/go.mod h1:lfuLI1f4o+0SGtlHQS4x5qsjRcZZfYqG8bp3k8hM0M8=
431-
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240415105151-0108877a3201 h1:DbCOM19ywdL5K+bOy4h+0MppzcPgI2guHnYCfDNnAcM=
432-
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240415105151-0108877a3201/go.mod h1:CBCU6fsRVz23QGFIQBb1DNX2DztJCf3jWyEkHY2nJQ0=
425+
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240603035522-8d30134159e0 h1:nnpQA65+0qbg9fBhnzJvvBvco3+9hNxp3gKrOFhK7lQ=
426+
github.com/crypto-org-chain/cosmos-sdk v0.0.0-20240603035522-8d30134159e0/go.mod h1:DkCxCPi3veciROiq36PbpXDhboMjqHAS0Xyv2dEEW04=
427+
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240603035522-8d30134159e0 h1:mZoahlBJjDnf9nMoDgq4m4mzGZLolfdWhmUh48Vf1XM=
428+
github.com/crypto-org-chain/cosmos-sdk/client/v2 v2.0.0-20240603035522-8d30134159e0/go.mod h1:GjpaaxatOEnkSlNKjYxSbzVVtHHAG0tWm26AoPA1g9Q=
429+
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240603035522-8d30134159e0 h1:VBEgrOHB6XHInt8lDxCVZNRh76qFkK69PgHr0MjawvE=
430+
github.com/crypto-org-chain/cosmos-sdk/store v0.0.0-20240603035522-8d30134159e0/go.mod h1:lfuLI1f4o+0SGtlHQS4x5qsjRcZZfYqG8bp3k8hM0M8=
431+
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240603035522-8d30134159e0 h1:j0AYThWObuRsdrMgww7/jHmpVNRZEKjchwlDe1vOy6s=
432+
github.com/crypto-org-chain/cosmos-sdk/x/tx v0.0.0-20240603035522-8d30134159e0/go.mod h1:CBCU6fsRVz23QGFIQBb1DNX2DztJCf3jWyEkHY2nJQ0=
433433
github.com/crypto-org-chain/ethermint v0.6.1-0.20240502052908-179e436703b3 h1:YYmMJowZyiyioNHYnps5hw3XkV1zcXSC3jy/xzqK2Rg=
434434
github.com/crypto-org-chain/ethermint v0.6.1-0.20240502052908-179e436703b3/go.mod h1:9MVSajfKloRP8h2chP78LhCKx5u9O2pCMBvxrmx6+0s=
435435
github.com/crypto-org-chain/go-block-stm v0.0.0-20240408011717-9f11af197bde h1:sQIHTJfVt5VTrF7po9eZiFkZiPjlHbFvnXtGCOoBjNM=

gomod2nix.toml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ schema = 3
2020
version = "v0.7.3"
2121
hash = "sha256-G72m5tUKLrpl89YKM/S7J/e8rYkqctuDOqHbqxyzTBI="
2222
[mod."cosmossdk.io/client/v2"]
23-
version = "v2.0.0-20240415105151-0108877a3201"
23+
version = "v2.0.0-20240603035522-8d30134159e0"
2424
hash = "sha256-4RYdjGJDIKLHfRE3LHwKmAE1C/JZPHtLMl3Sne7i//g="
2525
replaced = "github.com/crypto-org-chain/cosmos-sdk/client/v2"
2626
[mod."cosmossdk.io/collections"]
@@ -45,7 +45,7 @@ schema = 3
4545
version = "v0.0.0-20230608160436-666c345ad23d"
4646
hash = "sha256-6BMBA98BpK3jG6++ZE4LdPQwwpS+lZ0GLMRF1fO4UfM="
4747
[mod."cosmossdk.io/store"]
48-
version = "v0.0.0-20240415105151-0108877a3201"
48+
version = "v0.0.0-20240603035522-8d30134159e0"
4949
hash = "sha256-5fFhveNdA4rEwtTVNE4MIzK7udgNF80q9fkiDcw7T/Q="
5050
replaced = "github.com/crypto-org-chain/cosmos-sdk/store"
5151
[mod."cosmossdk.io/tools/confix"]
@@ -58,7 +58,7 @@ schema = 3
5858
version = "v0.1.0"
5959
hash = "sha256-/gWvrqvy6bW90+NU66T+9QysYgvG1VbwfYJZ8tkqpeA="
6060
[mod."cosmossdk.io/x/tx"]
61-
version = "v0.0.0-20240415105151-0108877a3201"
61+
version = "v0.0.0-20240603035522-8d30134159e0"
6262
hash = "sha256-jhCBzToU7KglOA4CuY16ogOA0dZ4ESX2HWshhgYUjvE="
6363
replaced = "github.com/crypto-org-chain/cosmos-sdk/x/tx"
6464
[mod."cosmossdk.io/x/upgrade"]
@@ -170,8 +170,8 @@ schema = 3
170170
version = "v1.0.0-beta.4"
171171
hash = "sha256-5Kn82nsZfiEtuwhhLZqmMxdAY1tX/Fi3HJ0/MEaRohw="
172172
[mod."github.com/cosmos/cosmos-sdk"]
173-
version = "v0.0.0-20240415105151-0108877a3201"
174-
hash = "sha256-qgfVUWIH2nX4l339+e1SVQ1iVQkv8Ew4Aj6zcHH2tPg="
173+
version = "v0.0.0-20240603035522-8d30134159e0"
174+
hash = "sha256-Mdw7biEUIzKFD6WQr607ZOoklWJkvR/ZLBN3jexhppA="
175175
replaced = "github.com/crypto-org-chain/cosmos-sdk"
176176
[mod."github.com/cosmos/go-bip39"]
177177
version = "v1.0.0"
@@ -578,9 +578,6 @@ schema = 3
578578
[mod."github.com/tendermint/go-amino"]
579579
version = "v0.16.0"
580580
hash = "sha256-JW4zO/0vMzf1dXLePOqaMtiLUZgNbuIseh9GV+jQlf0="
581-
[mod."github.com/test-go/testify"]
582-
version = "v1.1.4"
583-
hash = "sha256-8xygO1Rd4eTrmRe/g7zaifpNkeb6EmjNfUvTWbjDtPg="
584581
[mod."github.com/tidwall/btree"]
585582
version = "v0.0.0-20240406140148-2687063b042c"
586583
hash = "sha256-8eDLGHhw4qXG6MEa7w5Q9KLwOobXr8Vn5qqyQhuipQw="

x/e2ee/keyring/keyring.go

Lines changed: 9 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,13 @@
11
package keyring
22

33
import (
4-
"bufio"
5-
"fmt"
64
"io"
7-
"os"
8-
"path/filepath"
95

106
"github.com/99designs/keyring"
11-
"golang.org/x/crypto/bcrypt"
127

13-
errorsmod "cosmossdk.io/errors"
14-
"github.com/cosmos/cosmos-sdk/client/input"
158
sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
169
)
1710

18-
const (
19-
keyringFileDirName = "e2ee-keyring-file"
20-
keyringTestDirName = "e2ee-keyring-test"
21-
passKeyringPrefix = "e2ee-keyring-%s" //nolint: gosec
22-
maxPassphraseEntryAttempts = 3
23-
)
24-
2511
type Keyring interface {
2612
Get(string) ([]byte, error)
2713
Set(string, []byte) error
@@ -30,60 +16,17 @@ type Keyring interface {
3016
func New(
3117
appName, backend, rootDir string, userInput io.Reader,
3218
) (Keyring, error) {
33-
var (
34-
db keyring.Keyring
35-
err error
36-
)
3719
serviceName := appName + "-e2ee"
38-
switch backend {
39-
case sdkkeyring.BackendMemory:
40-
return newKeystore(keyring.NewArrayKeyring(nil), sdkkeyring.BackendMemory), nil
41-
case sdkkeyring.BackendTest:
42-
db, err = keyring.Open(keyring.Config{
43-
AllowedBackends: []keyring.BackendType{keyring.FileBackend},
44-
ServiceName: serviceName,
45-
FileDir: filepath.Join(rootDir, keyringTestDirName),
46-
FilePasswordFunc: func(_ string) (string, error) {
47-
return "test", nil
48-
},
49-
})
50-
case sdkkeyring.BackendFile:
51-
fileDir := filepath.Join(rootDir, keyringFileDirName)
52-
db, err = keyring.Open(keyring.Config{
53-
AllowedBackends: []keyring.BackendType{keyring.FileBackend},
54-
ServiceName: serviceName,
55-
FileDir: fileDir,
56-
FilePasswordFunc: newRealPrompt(fileDir, userInput),
57-
})
58-
case sdkkeyring.BackendOS:
59-
db, err = keyring.Open(keyring.Config{
60-
ServiceName: serviceName,
61-
FileDir: rootDir,
62-
KeychainTrustApplication: true,
63-
FilePasswordFunc: newRealPrompt(rootDir, userInput),
64-
})
65-
case sdkkeyring.BackendKWallet:
66-
db, err = keyring.Open(keyring.Config{
67-
AllowedBackends: []keyring.BackendType{keyring.KWalletBackend},
68-
ServiceName: "kdewallet",
69-
KWalletAppID: serviceName,
70-
KWalletFolder: "",
71-
})
72-
case sdkkeyring.BackendPass:
73-
prefix := fmt.Sprintf(passKeyringPrefix, serviceName)
74-
db, err = keyring.Open(keyring.Config{
75-
AllowedBackends: []keyring.BackendType{keyring.PassBackend},
76-
ServiceName: serviceName,
77-
PassPrefix: prefix,
78-
})
79-
default:
80-
return nil, errorsmod.Wrap(sdkkeyring.ErrUnknownBacked, backend)
81-
}
82-
83-
if err != nil {
84-
return nil, err
20+
var db keyring.Keyring
21+
if backend == sdkkeyring.BackendMemory {
22+
db = keyring.NewArrayKeyring(nil)
23+
} else {
24+
kr, err := sdkkeyring.New(serviceName, backend, rootDir, userInput, nil)
25+
if err != nil {
26+
return nil, err
27+
}
28+
db = kr.DB()
8529
}
86-
8730
return newKeystore(db, backend), nil
8831
}
8932

@@ -117,86 +60,3 @@ func (ks keystore) Set(name string, secret []byte) error {
11760
Label: name,
11861
})
11962
}
120-
121-
func newRealPrompt(dir string, buf io.Reader) func(string) (string, error) {
122-
return func(prompt string) (string, error) {
123-
keyhashStored := false
124-
keyhashFilePath := filepath.Join(dir, "keyhash")
125-
126-
var keyhash []byte
127-
128-
_, err := os.Stat(keyhashFilePath)
129-
130-
switch {
131-
case err == nil:
132-
keyhash, err = os.ReadFile(keyhashFilePath)
133-
if err != nil {
134-
return "", errorsmod.Wrap(err, fmt.Sprintf("failed to read %s", keyhashFilePath))
135-
}
136-
137-
keyhashStored = true
138-
139-
case os.IsNotExist(err):
140-
keyhashStored = false
141-
142-
default:
143-
return "", errorsmod.Wrap(err, fmt.Sprintf("failed to open %s", keyhashFilePath))
144-
}
145-
146-
failureCounter := 0
147-
148-
for {
149-
failureCounter++
150-
if failureCounter > maxPassphraseEntryAttempts {
151-
return "", sdkkeyring.ErrMaxPassPhraseAttempts
152-
}
153-
154-
buf := bufio.NewReader(buf)
155-
pass, err := input.GetPassword(fmt.Sprintf("Enter keyring passphrase (attempt %d/%d):", failureCounter, maxPassphraseEntryAttempts), buf)
156-
if err != nil {
157-
// NOTE: LGTM.io reports a false positive alert that states we are printing the password,
158-
// but we only log the error.
159-
//
160-
// lgtm [go/clear-text-logging]
161-
fmt.Fprintln(os.Stderr, err)
162-
continue
163-
}
164-
165-
if keyhashStored {
166-
if err := bcrypt.CompareHashAndPassword(keyhash, []byte(pass)); err != nil {
167-
fmt.Fprintln(os.Stderr, "incorrect passphrase")
168-
continue
169-
}
170-
171-
return pass, nil
172-
}
173-
174-
reEnteredPass, err := input.GetPassword("Re-enter keyring passphrase:", buf)
175-
if err != nil {
176-
// NOTE: LGTM.io reports a false positive alert that states we are printing the password,
177-
// but we only log the error.
178-
//
179-
// lgtm [go/clear-text-logging]
180-
fmt.Fprintln(os.Stderr, err)
181-
continue
182-
}
183-
184-
if pass != reEnteredPass {
185-
fmt.Fprintln(os.Stderr, "passphrase do not match")
186-
continue
187-
}
188-
189-
passwordHash, err := bcrypt.GenerateFromPassword([]byte(pass), 2)
190-
if err != nil {
191-
fmt.Fprintln(os.Stderr, err)
192-
continue
193-
}
194-
195-
if err := os.WriteFile(keyhashFilePath, passwordHash, 0o600); err != nil {
196-
return "", err
197-
}
198-
199-
return pass, nil
200-
}
201-
}
202-
}

x/e2ee/keyring/keyring_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
package keyring
1+
package keyring_test
22

33
import (
44
"bytes"
55
"io"
66
"testing"
77

88
"filippo.io/age"
9-
"github.com/test-go/testify/require"
10-
11-
"github.com/cosmos/cosmos-sdk/crypto/keyring"
9+
sdkkeyring "github.com/cosmos/cosmos-sdk/crypto/keyring"
10+
"github.com/crypto-org-chain/cronos/v2/x/e2ee/keyring"
11+
"github.com/stretchr/testify/require"
1212
)
1313

1414
func TestKeyring(t *testing.T) {
15-
kr, err := New("cronosd", keyring.BackendTest, t.TempDir(), nil)
15+
kr, err := keyring.New("cronosd", sdkkeyring.BackendTest, t.TempDir(), nil)
1616
require.NoError(t, err)
1717

1818
identity, err := age.GenerateX25519Identity()

0 commit comments

Comments
 (0)