Skip to content

Commit

Permalink
add age encrypt/decrypt in unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Apr 29, 2024
1 parent 1352c5b commit 3e9acaf
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
cosmossdk.io/x/evidence v0.1.0
cosmossdk.io/x/feegrant v0.1.0
cosmossdk.io/x/upgrade v0.1.1
filippo.io/age v1.1.1
github.com/99designs/keyring v1.2.2
github.com/cometbft/cometbft v0.38.7-0.20240412124004-1f67e396cf45
github.com/cosmos/cosmos-db v1.0.3-0.20240408151834-e75f6e4b28d8
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ cosmossdk.io/x/feegrant v0.1.0/go.mod h1:4r+FsViJRpcZif/yhTn+E0E6OFfg4n0Lx+6cCtn
cosmossdk.io/x/upgrade v0.1.1 h1:aoPe2gNvH+Gwt/Pgq3dOxxQVU3j5P6Xf+DaUJTDZATc=
cosmossdk.io/x/upgrade v0.1.1/go.mod h1:MNLptLPcIFK9CWt7Ra//8WUZAxweyRDNcbs5nkOcQy0=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
filippo.io/age v1.1.1 h1:pIpO7l151hCnQ4BdyBujnGP2YlUo0uj6sAVNHGBvXHg=
filippo.io/age v1.1.1/go.mod h1:l03SrzDUrBkdBx8+IILdnn2KZysqQdbEBUQ4p3sqEQE=
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs=
Expand Down
34 changes: 31 additions & 3 deletions x/e2ee/keyring/keyring_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,47 @@
package keyring

import (
"bytes"
"io"
"testing"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
"filippo.io/age"
"github.com/test-go/testify/require"

"github.com/cosmos/cosmos-sdk/crypto/keyring"
)

func TestKeyring(t *testing.T) {
kr, err := New("cronosd", keyring.BackendTest, t.TempDir(), nil)
require.NoError(t, err)

require.NoError(t, kr.Set("test", []byte("test")))
identity, err := age.GenerateX25519Identity()
require.NoError(t, err)

var ciphertext []byte
{
dst := bytes.NewBuffer(nil)
writer, err := age.Encrypt(dst, identity.Recipient())
require.NoError(t, err)
writer.Write([]byte("test"))
writer.Close()
ciphertext = dst.Bytes()
}

require.NoError(t, kr.Set("test", []byte(identity.String())))

secret, err := kr.Get("test")
require.NoError(t, err)
require.Equal(t, []byte("test"), secret)

identity, err = age.ParseX25519Identity(string(secret))
require.NoError(t, err)

{
reader, err := age.Decrypt(bytes.NewReader(ciphertext), identity)
require.NoError(t, err)
bz, err := io.ReadAll(reader)
require.NoError(t, err)

require.Equal(t, []byte("test"), bz)
}
}

0 comments on commit 3e9acaf

Please sign in to comment.