Skip to content

Commit

Permalink
chore: add in revoke dkim pubkey integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
Peartes committed Nov 19, 2024
1 parent f8ece97 commit 168d833
Show file tree
Hide file tree
Showing 10 changed files with 457 additions and 97 deletions.
94 changes: 85 additions & 9 deletions api/xion/dkim/v1/tx.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 72 additions & 0 deletions integration_tests/dkim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@ package integration_tests

import (
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/base64"
"encoding/json"
"encoding/pem"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -73,6 +78,7 @@ func TestDKIMModule(t *testing.T) {
config.SetBech32PrefixForAccount("xion", "xionpub")
xion.Config().EncodingConfig.InterfaceRegistry.RegisterImplementations((*sdk.Msg)(nil), &dkimTypes.MsgAddDkimPubKeys{})
xion.Config().EncodingConfig.InterfaceRegistry.RegisterImplementations((*sdk.Msg)(nil), &dkimTypes.MsgRemoveDkimPubKey{})
xion.Config().EncodingConfig.InterfaceRegistry.RegisterImplementations((*sdk.Msg)(nil), &dkimTypes.MsgRevokeDkimPubKey{})

fundAmount := math.NewInt(10_000_000_000)
users := interchaintest.GetAndFundTestUsers(t, ctx, "default", fundAmount, xion)
Expand Down Expand Up @@ -136,6 +142,72 @@ func TestDKIMModule(t *testing.T) {
_, err = ExecQuery(t, ctx, xion.GetNode(), "dkim", "dkim-pubkey", customDomain, customSelector)
require.Error(t, err)
require.Contains(t, err.Error(), "not found")

// let's create a new key pair and submit a proposal to add it
privateKey, err := rsa.GenerateKey(rand.Reader, 2048)
require.NoError(t, err)

privKeyPEM := pem.EncodeToMemory(
&pem.Block{
Type: "RSA PRIVATE KEY",
Bytes: x509.MarshalPKCS1PrivateKey(privateKey),
},
)

publicKey := privateKey.PublicKey
// Marshal the public key to PKCS1 DER format
pubKeyDER := x509.MarshalPKCS1PublicKey(&publicKey)

// Encode the public key in PEM format
pubKeyPEM := pem.EncodeToMemory(&pem.Block{
Type: "RSA PUBLIC KEY",
Bytes: pubKeyDER,
})
// remove the PEM header and footer from the public key
after, _ := strings.CutPrefix(string(pubKeyPEM), "-----BEGIN RSA PUBLIC KEY-----\n")
pubKey, _ := strings.CutSuffix(after, "\n-----END RSA PUBLIC KEY-----\n")
pubKey = strings.ReplaceAll(pubKey, "\n", "")
hash, err := dkimTypes.ComputePoseidonHash(pubKey)
require.NoError(t, err)

// remove the PEM header and footer from the private key
after, _ = strings.CutPrefix(string(privKeyPEM), "-----BEGIN RSA PRIVATE KEY-----\n")
privKey, _ := strings.CutSuffix(after, "\n-----END RSA PRIVATE KEY-----\n")
privKey = strings.ReplaceAll(privKey, "\n", "")

governancePubKeys = []dkimTypes.DkimPubKey{
{
Domain: domain_1,
Selector: "personal_key",
PubKey: pubKey,
PoseidonHash: []byte(hash.String()),
},
}

createDkimMsg = dkimTypes.NewMsgAddDkimPubKeys(sdk.MustAccAddressFromBech32(govModAddress), governancePubKeys)
require.NoError(t, createDkimMsg.ValidateBasic())

err = createAndSubmitProposal(t, xion, ctx, chainUser, []cosmos.ProtoMessage{createDkimMsg}, "Add Xion DKIM record", "Add Xion DKIM record", "Add Xion DKIM record", 3)
require.NoError(t, err)

// proposal must have gone through and msg submitted; let's query the chain for the pubkey
dkimRecord, err = ExecQuery(t, ctx, xion.GetNode(), "dkim", "dkim-pubkey", domain_1, "personal_key")
require.NoError(t, err)
require.Equal(t, dkimRecord["dkim_pub_key"].(map[string]interface{})["pub_key"].(string), pubKey)

// let's revoke the key
revokeDkimMsg := dkimTypes.NewMsgReVokeDkimPubKey(sdk.MustAccAddressFromBech32(chainUser.FormattedAddress()), domain_1, privKeyPEM)
require.NoError(t, revokeDkimMsg.ValidateBasic())

// execute the revoke tx using the CLI command
_, err = ExecTx(t, ctx, xion.GetNode(), chainUser.KeyName(), "dkim", "rdkim", domain_1, privKey, "--chain-id", xion.Config().ChainID)
require.NoError(t, err)

// query the chain for the revoked key
_, err = ExecQuery(t, ctx, xion.GetNode(), "dkim", "dkim-pubkey", domain_1, "personal_key")
require.Error(t, err)
require.Contains(t, err.Error(), "not found")

}

func createAndSubmitProposal(t *testing.T, xion *cosmos.CosmosChain, ctx context.Context, proposer ibc.Wallet, proposalMsgs []cosmos.ProtoMessage, title, summary, metadata string, proposalId int) error {
Expand Down
7 changes: 5 additions & 2 deletions proto/xion/dkim/v1/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ message MsgRemoveDkimPubKeyResponse {}

// MsgRevokeDkimPubKey is the Msg/RevokeDkimPubKey request type.
message MsgRevokeDkimPubKey {
string domain = 1;
bytes priv_key = 2;
option (cosmos.msg.v1.signer) = "signer";

string signer = 1 [ (cosmos_proto.scalar) = "cosmos.AddressString" ];
string domain = 2;
bytes priv_key = 3;
}

// MsgRevokeDkimPubKeyResponse defines the response structure for executing a
Expand Down
45 changes: 45 additions & 0 deletions x/dkim/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cli

import (
"encoding/pem"

"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
Expand All @@ -22,6 +24,7 @@ func NewTxCmd() *cobra.Command {
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
txCmd.AddCommand(MsgRevokeDkimPubKey())
return txCmd
}

Expand Down Expand Up @@ -56,3 +59,45 @@ func MsgUpdateParams() *cobra.Command {
flags.AddTxFlagsToCmd(cmd)
return cmd
}

// Returns a CLI command handler for registering a
// contract for the module.
func MsgRevokeDkimPubKey() *cobra.Command {
cmd := &cobra.Command{
Use: "revoke-dkim <domain> <priv_key>",
Short: "Revoke a Dkim pubkey without governance.",
Long: "Revoke a Dkim pubkey without governance. The private key is a PEM encoded private key without the headers and must be a contiguous string with no new line character.",
Aliases: []string{"rdkim"},
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

domain := args[0]
pemKey := types.FormatToPemKey(args[1], true)
block, _ := pem.Decode([]byte(pemKey))
if block == nil {
return types.ErrParsingPrivKey
}

msg := &types.MsgRevokeDkimPubKey{
Signer: cliCtx.GetFromAddress().String(),
Domain: domain,
PrivKey: pem.EncodeToMemory(
block,
),
}
err = msg.ValidateBasic()
if err != nil {
return err
}

return tx.GenerateOrBroadcastTxCLI(cliCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}
Loading

0 comments on commit 168d833

Please sign in to comment.