Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI auth support #375

Open
wants to merge 10 commits into
base: sprint-1.14
Choose a base branch
from
Open
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
21 changes: 16 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
"strings"
"sync"

"github.com/0chain/gosdk/core/sys"

Check failure on line 12 in cmd/root.go

View workflow job for this annotation

GitHub Actions / unit-test

github.com/0chain/[email protected]: replacement directory ../gosdk does not exist
"github.com/0chain/gosdk/core/zcncrypto"

Check failure on line 13 in cmd/root.go

View workflow job for this annotation

GitHub Actions / unit-test

github.com/0chain/[email protected]: replacement directory ../gosdk does not exist
"github.com/0chain/gosdk/zboxcore/client"

Check failure on line 14 in cmd/root.go

View workflow job for this annotation

GitHub Actions / unit-test

github.com/0chain/[email protected]: replacement directory ../gosdk does not exist
"github.com/0chain/gosdk/zboxcore/sdk"
bridge "github.com/0chain/gosdk/zcnbridge/http"

Check failure on line 16 in cmd/root.go

View workflow job for this annotation

GitHub Actions / unit-test

github.com/0chain/[email protected]: replacement directory ../gosdk does not exist
"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -96,22 +98,31 @@
blockWorker := cfgConfig.GetString("block_worker")
chainID := cfgConfig.GetString("chain_id")
ethereumNodeURL := cfgConfig.GetString("ethereum_node_url")
zauthServer := cfgConfig.GetString("zauth.server")

err := zcncore.InitZCNSDK(blockWorker, signatureScheme,
zcncore.WithChainID(chainID),
zcncore.WithMinSubmit(minSubmit),
zcncore.WithMinConfirmation(minCfm),
zcncore.WithConfirmationChainLength(CfmChainLength),
zcncore.WithEthereumNode(ethereumNodeURL))
zcncore.WithEthereumNode(ethereumNodeURL),
zcncore.WithIsSplitWallet(clientWallet.IsSplit),
)
if err != nil {
ExitWithError(err.Error())
}

if zauthServer != "" {
sys.SetAuthorize(zcncore.ZauthSignTxn(zauthServer))
client.SetClient(clientWallet, signatureScheme, getTxnFee())
}

miners := cfgNetwork.GetStringSlice("miners")
sharders := cfgNetwork.GetStringSlice("sharders")
if len(miners) > 0 && len(sharders) > 0 {
zcncore.SetNetwork(miners, sharders)
}

}

func loadConfigs() {
Expand Down Expand Up @@ -169,14 +180,14 @@

func initCmdContext(cmd *cobra.Command, args []string) {

_, ok := withoutZCNCoreCmds[cmd]
_, ok := withoutWalletCmds[cmd]
if !ok {
initZCNCoreContext()
initZwalletContext()
}

_, ok = withoutWalletCmds[cmd]
_, ok = withoutZCNCoreCmds[cmd]
if !ok {
initZwalletContext()
initZCNCoreContext()
}

}
Expand Down
72 changes: 72 additions & 0 deletions cmd/zauth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package cmd

import (
"log"

"github.com/0chain/gosdk/zcncore"
"github.com/spf13/cobra"
)

var zauthCmd = &cobra.Command{
Use: "zauth",
Short: "Enable zauth",
Long: `Enable zauth to sign transactions and messages, setup split keys and configure the zauth service.`,
Run: func(cmd *cobra.Command, args []string) {
// Add your code here
serverAddr, err := cmd.Flags().GetString("server")
if err != nil {
log.Fatalf("Could not find zauth server address")
}

token, err := cmd.Flags().GetString("token")
if err != nil {
log.Fatalf("Could not find zauth server setup access token")
}

// update or setup the zauth server address
cfgConfig.Set("zauth.server", serverAddr)
if err := cfgConfig.WriteConfig(); err != nil {
log.Fatalf("Could not save config: %v", err)
}

if clientWallet == nil {
log.Fatalf("Wallet is initialized yet")
}

if clientWallet.IsSplit {
log.Fatalln("Wallet is already split")
}

sw, err := zcncore.SplitKeysWallet(clientWallet.Keys[0].PrivateKey, 2)
if err != nil {
log.Fatalf("Failed to split keys: %v", err)
}

if err := zcncore.CallZauthSetup(serverAddr, token, zcncore.SplitWallet{
ClientID: sw.ClientID,
ClientKey: sw.ClientKey,
PublicKey: sw.Keys[1].PublicKey,
PrivateKey: sw.Keys[1].PrivateKey,
PeerPublicKey: sw.Keys[0].PublicKey,
}); err != nil {
log.Fatalf("Failed to setup zauth server: %v", err)
}

// remove the keys[1]
sw.PeerPublicKey = sw.Keys[1].PublicKey
sw.Keys = sw.Keys[:1]
clientWallet.SetSplitKeys(sw)
if err := clientWallet.SaveTo(cfgWallet); err != nil {
log.Fatalf("Failed to save wallet: %v", err)
}

log.Printf("Setup zauth server successfully")
},
}

func init() {
rootCmd.AddCommand(zauthCmd)
zauthCmd.PersistentFlags().String("server", "s", "The zauth server address")
zauthCmd.PersistentFlags().String("token", "t", "The zauth server setup access token")
zauthCmd.MarkFlagRequired("server")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,4 @@ require (
)

// temporary, for development
// replace github.com/0chain/gosdk => ../gosdk
replace github.com/0chain/gosdk => ../gosdk
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ github.com/0chain/common v0.0.7-0.20231108122201-3e2bad6b9d20 h1:c46aB5l0xbD7nc/
github.com/0chain/common v0.0.7-0.20231108122201-3e2bad6b9d20/go.mod h1:gbmUdgY4Gu2jKmnYnHr8533gcokviV3MDMs8wNk74sk=
github.com/0chain/errors v1.0.3 h1:QQZPFxTfnMcRdt32DXbzRQIfGWmBsKoEdszKQDb0rRM=
github.com/0chain/errors v1.0.3/go.mod h1:xymD6nVgrbgttWwkpSCfLLEJbFO6iHGQwk/yeSuYkIc=
github.com/0chain/gosdk v1.14.0-RC1 h1:FaQIXMDtchKjJqhpxMh4dA52QUe46DKixibrZXcFS+o=
github.com/0chain/gosdk v1.14.0-RC1/go.mod h1:tgAiVAuIy+Vs1tGfKCPEuuWWARwNQBEw32y950LrqrU=
github.com/AndreasBriese/bbloom v0.0.0-20190306092124-e2d15f34fcf9/go.mod h1:bOvUY6CB00SOBii9/FifXqc0awNKxLFCL/+pkDPuyl8=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
Expand Down
Loading