Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 56 additions & 112 deletions cmd/git-remote-gitopia/gitopia.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,8 @@ import (
"strings"

"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
clientTx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
"github.com/cosmos/cosmos-sdk/crypto/hd"
cosmoscryptoed "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cosmoscryptosecp "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authTx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authType "github.com/cosmos/cosmos-sdk/x/auth/types"
core "github.com/gitopia/git-remote-gitopia/core"
gitopiaTypes "github.com/gitopia/gitopia/x/gitopia/types"
"github.com/gitopia/gitopia/x/gitopia/utils"
Expand Down Expand Up @@ -70,9 +59,8 @@ type SaveToArweavePostBody struct {
}

type GitopiaHandler struct {
queryClient gitopiaTypes.QueryClient
accountQueryClient authType.QueryClient
txClient tx.ServiceClient
grpcConn *grpc.ClientConn
queryClient gitopiaTypes.QueryClient

chainId string
remoteUserId string
Expand All @@ -83,17 +71,18 @@ type GitopiaHandler struct {
}

func (h *GitopiaHandler) Initialize(remote *core.Remote) error {
grpcConn, err := grpc.Dial(apiURL,
var err error

h.grpcConn, err = grpc.Dial(apiURL,
grpc.WithInsecure(),
)
if err != nil {
return err
}
// defer grpcConn.Close()

h.queryClient = gitopiaTypes.NewQueryClient(grpcConn)
h.accountQueryClient = authType.NewQueryClient(grpcConn)
serviceClient := tmservice.NewServiceClient(grpcConn)
h.queryClient = gitopiaTypes.NewQueryClient(h.grpcConn)
serviceClient := tmservice.NewServiceClient(h.grpcConn)

// Get chain id for signing transaction
nodeInfoRes, err := serviceClient.GetNodeInfo(context.Background(), &tmservice.GetNodeInfoRequest{})
Expand Down Expand Up @@ -172,18 +161,8 @@ func (h *GitopiaHandler) Fetch(remote *core.Remote, sha, ref string) error {

func (h *GitopiaHandler) Push(remote *core.Remote, local string, remoteRef string) (string, error) {
h.didPush = true
remoteURL := fmt.Sprintf("%v/%v.git", objectsURL, h.remoteRepository.Id)
remoteConfig := &goGitConfig.RemoteConfig{
Name: "gitopia-objects-store",
URLs: []string{remoteURL},
}

_, err := remote.Repo.CreateRemote(remoteConfig)
if err != nil {
return "", err
}
defer remote.Repo.DeleteRemote("gitopia-objects-store")

// Read wallet file
gitopiaWalletPath := os.Getenv("GITOPIA_WALLET")
if gitopiaWalletPath == "" {
return "", fmt.Errorf("fatal: GITOPIA_WALLET environment variable is not set")
Expand All @@ -200,6 +179,7 @@ func (h *GitopiaHandler) Push(remote *core.Remote, local string, remoteRef strin
return "", fmt.Errorf("fatal: error decoding wallet file")
}

// Generate private key
derivedPriv, err := hd.Secp256k1.Derive()(gitopiaWallet.Mnemonic, "", gitopiaWallet.HDpath)
if err != nil {
return "", err
Expand All @@ -221,36 +201,65 @@ func (h *GitopiaHandler) Push(remote *core.Remote, local string, remoteRef strin
return "", fmt.Errorf("fatal: you don't have write permissions to this repository")
}

var msg sdk.Msg

// Delete branch/tag
if local == "" {
if strings.HasPrefix(remoteRef, branchPrefix) {
remoteBranchName := strings.TrimPrefix(remoteRef, branchPrefix)

// Check if it's the default branch
if remoteBranchName == h.remoteRepository.DefaultBranch {
return "", fmt.Errorf("fatal: cannot delete default branch, %v", remoteBranchName)
}

msg = gitopiaTypes.NewMsgDeleteBranch(walletAddress.String(), h.remoteRepository.Id, remoteBranchName)
} else if strings.HasPrefix(remoteRef, tagPrefix) {
remoteTagName := strings.TrimPrefix(remoteRef, tagPrefix)
msg = gitopiaTypes.NewMsgDeleteTag(walletAddress.String(), h.remoteRepository.Id, remoteTagName)
}

err := signAndBroadcastTx(h.grpcConn, walletAddress.String(), h.chainId, privKey, msg)
if err != nil {
return "", err
}

return local, nil
}

remoteURL := fmt.Sprintf("%v/%v.git", objectsURL, h.remoteRepository.Id)
remoteConfig := &goGitConfig.RemoteConfig{
Name: "gitopia-objects-store",
URLs: []string{remoteURL},
}

_, err = remote.Repo.CreateRemote(remoteConfig)
if err != nil {
return "", err
}
defer remote.Repo.DeleteRemote("gitopia-objects-store")

force := false
if strings.HasPrefix(local, "+") {
local = strings.TrimPrefix(local, "+")
force = true
}

pushOptions := &git.PushOptions{
RemoteName: "gitopia-objects-store",
RefSpecs: []goGitConfig.RefSpec{goGitConfig.RefSpec(fmt.Sprintf("%s:%s", local, remoteRef))},
Progress: os.Stdout,
Force: force,
}

err = remote.Repo.Push(pushOptions)
if err != nil && err != git.NoErrAlreadyUpToDate {
return "", fmt.Errorf("fatal: error pushing the git objects, %v", err.Error())
}

// Update ref on gitopia
interfaceRegistry := types.NewInterfaceRegistry()
interfaceRegistry.RegisterInterface(
"cosmos.auth.v1beta1.AccountI",
(*authType.AccountI)(nil),
&authType.BaseAccount{},
&authType.ModuleAccount{},
)
interfaceRegistry.RegisterInterface("cosmos.crypto.PubKey", (*cryptotypes.PubKey)(nil))
interfaceRegistry.RegisterImplementations((*cryptotypes.PubKey)(nil), &cosmoscryptosecp.PubKey{})
interfaceRegistry.RegisterImplementations((*cryptotypes.PubKey)(nil), &cosmoscryptoed.PubKey{})
marshaler := codec.NewProtoCodec(interfaceRegistry)
txCfg := authTx.NewTxConfig(marshaler, authTx.DefaultSignModes)

txBuilder := txCfg.NewTxBuilder()

var newRemoteRefSha, prevRemoteRefSha string
var msg sdk.Msg

// Update ref on gitopia
if strings.HasPrefix(local, branchPrefix) {
localCommitHash, err := remote.Repo.ResolveRevision(plumbing.Revision(local))
if err != nil {
Expand Down Expand Up @@ -290,76 +299,11 @@ func (h *GitopiaHandler) Push(remote *core.Remote, local string, remoteRef strin
return "", fmt.Errorf("fatal: not a valid branch/tag, %v", local)
}

txBuilder.SetMsgs(msg)
txBuilder.SetGasLimit(200000)

res, err := h.accountQueryClient.Account(context.Background(),
&authType.QueryAccountRequest{
Address: walletAddress.String(),
},
)
var acc authType.AccountI
if err := interfaceRegistry.UnpackAny(res.Account, &acc); err != nil {
return "", err
}

sigV2 := signing.SignatureV2{
PubKey: privKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: txCfg.SignModeHandler().DefaultMode(),
Signature: nil,
},
Sequence: acc.GetSequence(),
}
err = txBuilder.SetSignatures(sigV2)
if err != nil {
return "", err
}

signerData := xauthsigning.SignerData{
ChainID: h.chainId,
AccountNumber: acc.GetAccountNumber(),
Sequence: acc.GetSequence(),
}

sigV2, err = clientTx.SignWithPrivKey(txCfg.SignModeHandler().DefaultMode(), signerData,
txBuilder, privKey, txCfg, acc.GetSequence())
err = signAndBroadcastTx(h.grpcConn, walletAddress.String(), h.chainId, privKey, msg)
if err != nil {
return "", err
}

err = txBuilder.SetSignatures(sigV2)
if err != nil {
return "", err
}

err = txBuilder.GetTx().ValidateBasic()
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: tx validation failed: %v", err.Error())
}

var txBytes []byte
txBytes, err = txCfg.TxEncoder()(txBuilder.GetTx())
if err != nil {
return "", err
}

var grpcRes *tx.BroadcastTxResponse
grpcRes, err = h.txClient.BroadcastTx(
context.Background(),
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes,
},
)
if err != nil {
return "", err
}

if grpcRes.TxResponse.Code != 0 {
return "", fmt.Errorf("fatal: failed to broadcast transaction, code: %v", grpcRes.TxResponse.Code)
}

// Queue task to upload objects to arweave
saveToArweavePostBody := SaveToArweavePostBody{
RepositoryID: h.remoteRepository.Id,
Expand Down
113 changes: 113 additions & 0 deletions cmd/git-remote-gitopia/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"context"
"fmt"
"os"

clientTx "github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/codec/types"
cosmoscryptoed "github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
cosmoscryptosecp "github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/tx"
"github.com/cosmos/cosmos-sdk/types/tx/signing"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
authtx "github.com/cosmos/cosmos-sdk/x/auth/tx"
authtype "github.com/cosmos/cosmos-sdk/x/auth/types"

"google.golang.org/grpc"
)

func signAndBroadcastTx(cc *grpc.ClientConn, sender string, chainId string, privKey cryptotypes.PrivKey, msg sdk.Msg) error {
accountQueryClient := authtype.NewQueryClient(cc)
txClient := tx.NewServiceClient(cc)

interfaceRegistry := types.NewInterfaceRegistry()
interfaceRegistry.RegisterInterface(
"cosmos.auth.v1beta1.AccountI",
(*authtype.AccountI)(nil),
&authtype.BaseAccount{},
&authtype.ModuleAccount{},
)
interfaceRegistry.RegisterInterface("cosmos.crypto.PubKey", (*cryptotypes.PubKey)(nil))
interfaceRegistry.RegisterImplementations((*cryptotypes.PubKey)(nil), &cosmoscryptosecp.PubKey{})
interfaceRegistry.RegisterImplementations((*cryptotypes.PubKey)(nil), &cosmoscryptoed.PubKey{})
marshaler := codec.NewProtoCodec(interfaceRegistry)
txCfg := authtx.NewTxConfig(marshaler, authtx.DefaultSignModes)

txBuilder := txCfg.NewTxBuilder()
txBuilder.SetMsgs(msg)
txBuilder.SetGasLimit(200000)

res, err := accountQueryClient.Account(context.Background(),
&authtype.QueryAccountRequest{
Address: sender,
},
)
var acc authtype.AccountI
if err := interfaceRegistry.UnpackAny(res.Account, &acc); err != nil {
return err
}

sigV2 := signing.SignatureV2{
PubKey: privKey.PubKey(),
Data: &signing.SingleSignatureData{
SignMode: txCfg.SignModeHandler().DefaultMode(),
Signature: nil,
},
Sequence: acc.GetSequence(),
}
err = txBuilder.SetSignatures(sigV2)
if err != nil {
return err
}

signerData := xauthsigning.SignerData{
ChainID: chainId,
AccountNumber: acc.GetAccountNumber(),
Sequence: acc.GetSequence(),
}

sigV2, err = clientTx.SignWithPrivKey(txCfg.SignModeHandler().DefaultMode(), signerData,
txBuilder, privKey, txCfg, acc.GetSequence())
if err != nil {
return err
}

err = txBuilder.SetSignatures(sigV2)
if err != nil {
return err
}

err = txBuilder.GetTx().ValidateBasic()
if err != nil {
fmt.Fprintf(os.Stderr, "fatal: tx validation failed: %v", err.Error())
}

var txBytes []byte
txBytes, err = txCfg.TxEncoder()(txBuilder.GetTx())
if err != nil {
return err
}

var grpcRes *tx.BroadcastTxResponse
grpcRes, err = txClient.BroadcastTx(
context.Background(),
&tx.BroadcastTxRequest{
Mode: tx.BroadcastMode_BROADCAST_MODE_SYNC,
TxBytes: txBytes,
},
)
if err != nil {
return err
}

if grpcRes.TxResponse.Code != 0 {
return fmt.Errorf("fatal: failed to broadcast transaction, code: %v", grpcRes.TxResponse.Code)
}

return nil
}
4 changes: 2 additions & 2 deletions core/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (r *Remote) Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(r.writer, format, a...)
}

func (r *Remote) push(src, dst string, force bool) {
func (r *Remote) push(src, dst string) {
r.todo = append(r.todo, func() (string, error) {
done, err := r.Handler.Push(r, src, dst)
if err != nil {
Expand Down Expand Up @@ -126,7 +126,7 @@ loop:
r.Printf("\n")
case strings.HasPrefix(command, "push "):
refs := strings.Split(command[5:], ":")
r.push(refs[0], refs[1], false) //TODO: parse force
r.push(refs[0], refs[1])
case strings.HasPrefix(command, "fetch "):
parts := strings.Split(command, " ")
r.fetch(parts[1], parts[2])
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.16

require (
github.com/cosmos/cosmos-sdk v0.42.4 // indirect
github.com/gitopia/gitopia v0.9.0
github.com/gitopia/gitopia v0.10.0-rc.1
github.com/go-git/go-git/v5 v5.4.2
google.golang.org/genproto v0.0.0-20210903162649-d08c68adba83 // indirect
google.golang.org/grpc v1.40.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ github.com/gitopia/gitopia v0.7.1-0.20210908101447-c82c98352e19 h1:rpg74I7JG9XHs
github.com/gitopia/gitopia v0.7.1-0.20210908101447-c82c98352e19/go.mod h1:dNVXmTrslR7J20yRTu6DwfoVhgyG8zW1Oo9C5zTuLcI=
github.com/gitopia/gitopia v0.9.0 h1:yQy1xWeZI/Da6k5xh79RQhHouoCBf2MuuqdATCOIa9M=
github.com/gitopia/gitopia v0.9.0/go.mod h1:dNVXmTrslR7J20yRTu6DwfoVhgyG8zW1Oo9C5zTuLcI=
github.com/gitopia/gitopia v0.10.0-rc.1 h1:B57VcQiYO8xGVojBqF2Ui469xPwgpROlhGUZrnwtBRI=
github.com/gitopia/gitopia v0.10.0-rc.1/go.mod h1:dNVXmTrslR7J20yRTu6DwfoVhgyG8zW1Oo9C5zTuLcI=
github.com/gliderlabs/ssh v0.2.2 h1:6zsha5zo/TWhRhwqCD3+EarCAgZ2yN28ipRnGPnwkI0=
github.com/gliderlabs/ssh v0.2.2/go.mod h1:U7qILu1NlMHj9FlMhZLlkCdDnU1DBEAqr0aevW3Awn0=
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
Expand Down