diff --git a/cmd/auth-register.go b/cmd/auth-register.go index 4d89b85..64ae42d 100644 --- a/cmd/auth-register.go +++ b/cmd/auth-register.go @@ -1,111 +1,108 @@ package cmd -// -//import ( -// "context" -// "log" -// "strings" -// -// "github.com/0chain/gosdk/zcnbridge" -// "github.com/0chain/gosdk/zcnbridge/transaction" -// "github.com/0chain/gosdk/zcncore" -// "github.com/pkg/errors" -//) -// -////goland:noinspection ALL -//func init() { -// rootCmd.AddCommand( -// createCommandWithBridge( -// "auth-register", -// "Register an authorizer manually", -// "Register an authorizer manually", -// registerAuthorizerInChain, -// true, -// &Option{ -// name: "url", -// typename: "string", -// value: "", -// usage: "authorizer endpoint url", -// required: true, -// }, -// &Option{ -// name: "client_id", -// typename: "string", -// value: "", -// usage: "the client_id of the wallet", -// required: true, -// }, -// &Option{ -// name: "client_key", -// typename: "string", -// value: "", -// usage: "the client_key which is the public key of the wallet", -// required: true, -// }, -// &Option{ -// name: "min_stake", -// typename: "int64", -// value: int64(1), -// usage: "the minimum stake value for the stake pool", -// required: false, -// }, -// &Option{ -// name: "max_stake", -// typename: "int64", -// value: int64(10), -// usage: "the maximum stake value for the stake pool", -// required: false, -// }, -// &Option{ -// name: "num_delegates", -// typename: "int", -// value: 5, -// usage: "the number of delegates in the authorizer stake pool", -// required: false, -// }, -// &Option{ -// name: "service_charge", -// typename: "float64", -// value: 0.0, -// usage: "the service charge for the authorizer stake pool", -// required: false, -// }, -// )) -//} -// -//// registerAuthorizerInChain registers a new authorizer -//// addAuthorizerPayload *addAuthorizerPayload -//func registerAuthorizerInChain(bc *zcnbridge.BridgeClient, args ...*Arg) { -// clientID := GetClientID(args) -// clientKey := GetClientKey(args) -// url := GetURL(args) -// numDelegates := GetNumDelegates(args) -// serviceCharge := GetServiceCharge(args) -// -// input := &zcncore.AddAuthorizerPayload{ -// PublicKey: clientKey, -// URL: url, -// StakePoolSettings: zcncore.AuthorizerStakePoolSettings{ -// DelegateWallet: clientID, -// NumDelegates: numDelegates, -// ServiceCharge: serviceCharge, -// }, -// } -// -// trx, err := transaction.AddAuthorizer(context.Background(), input) -// if err != nil { -// log.Fatal(err, "failed to add authorizer with transaction: '%s'", trx.GetHash()) -// } -// -// log.Printf("Authorizer submitted OK... " + trx.GetHash()) -// log.Printf("Starting verification: " + trx.GetHash()) -// -// err = trx.Verify(context.Background()) -// if err != nil { -// if strings.Contains(err.Error(), "already exists") { -// ExitWithError("Authorizer has already been added to 0Chain... Continue") -// } else { -// ExitWithError(errors.Wrapf(err, "failed to verify transaction: '%s'", trx.GetHash())) -// } -// } -//} +import ( + "github.com/0chain/gosdk/core/transaction" + "github.com/0chain/gosdk/zcnbridge" + "github.com/0chain/gosdk/zcncore" + "github.com/pkg/errors" + "log" + "strings" +) + +//goland:noinspection ALL +func init() { + rootCmd.AddCommand( + createCommandWithBridge( + "auth-register", + "Register an authorizer manually", + "Register an authorizer manually", + registerAuthorizerInChain, + true, + &Option{ + name: "url", + typename: "string", + value: "", + usage: "authorizer endpoint url", + required: true, + }, + &Option{ + name: "client_id", + typename: "string", + value: "", + usage: "the client_id of the wallet", + required: true, + }, + &Option{ + name: "client_key", + typename: "string", + value: "", + usage: "the client_key which is the public key of the wallet", + required: true, + }, + &Option{ + name: "min_stake", + typename: "int64", + value: int64(1), + usage: "the minimum stake value for the stake pool", + required: false, + }, + &Option{ + name: "max_stake", + typename: "int64", + value: int64(10), + usage: "the maximum stake value for the stake pool", + required: false, + }, + &Option{ + name: "num_delegates", + typename: "int", + value: 5, + usage: "the number of delegates in the authorizer stake pool", + required: false, + }, + &Option{ + name: "service_charge", + typename: "float64", + value: 0.0, + usage: "the service charge for the authorizer stake pool", + required: false, + }, + )) +} + +// registerAuthorizerInChain registers a new authorizer +// addAuthorizerPayload *addAuthorizerPayload +func registerAuthorizerInChain(bc *zcnbridge.BridgeClient, args ...*Arg) { + clientID := GetClientID(args) + clientKey := GetClientKey(args) + url := GetURL(args) + numDelegates := GetNumDelegates(args) + serviceCharge := GetServiceCharge(args) + + input := &zcncore.AddAuthorizerPayload{ + PublicKey: clientKey, + URL: url, + StakePoolSettings: zcncore.AuthorizerStakePoolSettings{ + DelegateWallet: clientID, + NumDelegates: numDelegates, + ServiceCharge: serviceCharge, + }, + } + + hash, _, _, txn, err := zcncore.ZCNSCAddAuthorizer(input) + if err != nil { + log.Fatal(err, "failed to add authorizer with transaction: '%s'", hash) + } + + log.Printf("Authorizer submitted OK... " + hash) + log.Printf("Starting verification: " + hash) + + txn, err = transaction.VerifyTransaction(hash) + if err != nil { + if strings.Contains(err.Error(), "already exists") { + ExitWithError("Authorizer has already been added to 0Chain... Continue") + } else { + ExitWithError(errors.Wrapf(err, "failed to verify transaction: '%s'", txn.Hash)) + } + } +} diff --git a/go.mod b/go.mod index a09d5cd..4b5b439 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ require ( github.com/ethereum/go-ethereum v1.13.2 github.com/icza/bitio v1.1.0 github.com/olekukonko/tablewriter v0.0.5 + github.com/pkg/errors v0.9.1 github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.16.0 @@ -68,7 +69,6 @@ require ( github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/pelletier/go-toml/v2 v2.1.0 // indirect github.com/philhofer/fwd v1.1.2-0.20210722190033-5c56ac6d0bb9 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/prometheus/client_golang v1.14.0 // indirect github.com/prometheus/common v0.39.0 // indirect github.com/prometheus/procfs v0.11.0 // indirect diff --git a/go.sum b/go.sum index d3d2e6b..0b8aa37 100644 --- a/go.sum +++ b/go.sum @@ -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.17.7-0.20240915215339-03ae61489c3a h1:S50oSGLap4c953VHWbQ6/pPWVHWHs7GMW21ZXfMBGq8= -github.com/0chain/gosdk v1.17.7-0.20240915215339-03ae61489c3a/go.mod h1:+9quBj37eUmdFrWbvDv1Fs4+gG/Vz8Ls/4Ywu/sd8GI= 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=