From 61363c3a5020440e7ef62905bf83607fd38ad986 Mon Sep 17 00:00:00 2001 From: asilenced Date: Thu, 14 Dec 2023 15:00:36 +0800 Subject: [PATCH] add set mint authority --- cmd/soltool/main.go | 13 ++++ cmd/soltool/set_mint_authority.go | 110 ++++++++++++++++++++++++++++++ example_sol_create_bridge.json | 5 +- go.mod | 2 +- go.sum | 4 +- 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 cmd/soltool/set_mint_authority.go diff --git a/cmd/soltool/main.go b/cmd/soltool/main.go index 2532922..ccd0a8f 100644 --- a/cmd/soltool/main.go +++ b/cmd/soltool/main.go @@ -38,6 +38,7 @@ func init() { &setSupportChainIdCommand, &setFeeReceiverCommand, &setFeeAmountCommand, + &setMintAuthorityCommand, } } @@ -85,6 +86,14 @@ var setFeeReceiverCommand = cli.Command{ Flags: cliFlags, } +var setMintAuthorityCommand = cli.Command{ + Name: "setMintAuthority", + Usage: "set mint authority", + Description: "The setMintAuthority command is used to set mint authority", + Action: setMintAuthority, + Flags: cliFlags, +} + var setFeeAmountCommand = cli.Command{ Name: "setFeeAmount", Usage: "set fee amount", @@ -114,6 +123,10 @@ type PoolAccounts struct { ResourceIdToMint map[string]string `json:"resourceIdToMint"` FeeReceiverAccount string `json:"feeReceiverAccount"` //need private key FeeAmounts map[string]uint64 `json:"feeAmounts"` + + StakePool string `json:"stakePool"` + BridgeSigner string `json:"bridgeSigner"` + RSolMint string `json:"rsolMint"` } func loadConfig(file string, config *PoolAccounts) (err error) { diff --git a/cmd/soltool/set_mint_authority.go b/cmd/soltool/set_mint_authority.go new file mode 100644 index 0000000..7b16651 --- /dev/null +++ b/cmd/soltool/set_mint_authority.go @@ -0,0 +1,110 @@ +package main + +import ( + "context" + "fmt" + "time" + + "github.com/stafiprotocol/chainbridge/shared/solana/vault" + "github.com/stafiprotocol/solana-go-sdk/bridgeprog" + solClient "github.com/stafiprotocol/solana-go-sdk/client" + solCommon "github.com/stafiprotocol/solana-go-sdk/common" + solTypes "github.com/stafiprotocol/solana-go-sdk/types" + "github.com/urfave/cli/v2" +) + +func setMintAuthority(ctx *cli.Context) error { + path := ctx.String(configFlag.Name) + pc := PoolAccounts{} + err := loadConfig(path, &pc) + if err != nil { + return err + } + if len(pc.StakePool) == 0 { + return fmt.Errorf("stakepool empty") + } + fmt.Printf("\naccounts info:\n %+v\n", pc) + v, err := vault.NewVaultFromWalletFile(pc.KeystorePath) + if err != nil { + return err + } + boxer, err := vault.SecretBoxerForType(v.SecretBoxWrap) + if err != nil { + return fmt.Errorf("secret boxer: %w", err) + } + + if err := v.Open(boxer); err != nil { + return fmt.Errorf("opening: %w", err) + } + + privKeyMap := make(map[string]vault.PrivateKey) + for _, privKey := range v.KeyBag { + privKeyMap[privKey.PublicKey().String()] = privKey + } + + FeeAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.FeeAccount]) + BridgeAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.BridgeAccount]) + AdminAccount := solTypes.AccountFromPrivateKeyBytes(privKeyMap[pc.AdminAccountPubkey]) + BridgeProgramId := solCommon.PublicKeyFromString(pc.BridgeProgramId) + + owners := make([]solCommon.PublicKey, 0) + owners = append(owners, FeeAccount.PublicKey) + for _, account := range pc.OtherFeeAccountPubkey { + a := solTypes.AccountFromPrivateKeyBytes(privKeyMap[account]) + owners = append(owners, a.PublicKey) + } + if len(owners) < int(pc.Threshold) { + return fmt.Errorf("owner len < threshold") + } + + //start inter with solana chain + c := solClient.NewClient([]string{pc.Endpoint}) + //check if exist + bridgeInfo, err := c.GetBridgeAccountInfo(context.Background(), BridgeAccount.PublicKey.ToBase58()) + if err != nil { + return fmt.Errorf("\nbridge account not exist:\n %+v", bridgeInfo) + } + + res, err := c.GetLatestBlockhash(context.Background(), solClient.GetLatestBlockhashConfig{ + Commitment: solClient.CommitmentConfirmed, + }) + if err != nil { + return err + } + + fmt.Printf("bridgeAccount %s\n", BridgeAccount.PublicKey.ToBase58()) + fmt.Printf("owners %+v\n", owners) + fmt.Printf("supportChainIds %+v\n", pc.SupportChainIds) + fmt.Printf("feeReceiver %+v\n", pc.FeeReceiverAccount) + fmt.Printf("stakePool %+v\n", solCommon.PublicKeyFromString(pc.StakePool).ToBase58()) + fmt.Printf("bridgeSigner %+v\n", pc.BridgeSigner) + + time.Sleep(3 * time.Second) + + //create bridge account + rawTx, err := solTypes.CreateRawTransaction(solTypes.CreateRawTransactionParam{ + Instructions: []solTypes.Instruction{ + bridgeprog.SetMintAuthority( + BridgeProgramId, + BridgeAccount.PublicKey, + AdminAccount.PublicKey, + solCommon.PublicKeyFromString(pc.BridgeSigner), + solCommon.PublicKeyFromString(pc.RSolMint), + solCommon.PublicKeyFromString(pc.StakePool), + ), + }, + Signers: []solTypes.Account{FeeAccount, AdminAccount}, + FeePayer: FeeAccount.PublicKey, + RecentBlockHash: res.Blockhash, + }) + if err != nil { + return fmt.Errorf("generate tx error, err: %v", err) + } + txHash, err := c.SendRawTransaction(context.Background(), rawTx) + if err != nil { + return fmt.Errorf("send tx error, err: %v", err) + } + fmt.Println("SetMintAuthority txHash:", txHash) + + return nil +} diff --git a/example_sol_create_bridge.json b/example_sol_create_bridge.json index 6508c8e..8f1566e 100644 --- a/example_sol_create_bridge.json +++ b/example_sol_create_bridge.json @@ -25,5 +25,8 @@ "feeReceiverAccount": "9Riwnxn53S4wmy5h5nbQN1gxTCm1EvgqB4Gc5aKDAPyc", "feeAmounts": { "1": 50000 - } + }, + "stakePool": "", + "bridgeSigner": "", + "rsolMint": "" } \ No newline at end of file diff --git a/go.mod b/go.mod index 9397dc7..f4b513f 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/stafihub/stafi-hub-relay-sdk v1.7.0 github.com/stafihub/stafihub v0.1.0 github.com/stafiprotocol/go-substrate-rpc-client v1.2.1 - github.com/stafiprotocol/solana-go-sdk v1.3.0 + github.com/stafiprotocol/solana-go-sdk v1.4.0 github.com/stretchr/testify v1.8.0 github.com/urfave/cli/v2 v2.10.2 golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d diff --git a/go.sum b/go.sum index 526c90c..423cf30 100644 --- a/go.sum +++ b/go.sum @@ -1287,8 +1287,8 @@ github.com/stafihub/stafihub v0.1.0 h1:3EMn615R8mEg6eskZgok+67wKC11rD0sfIM+iRF8E github.com/stafihub/stafihub v0.1.0/go.mod h1:xY155chryZY5plerYGnk/KKdex2LkGlQ93bSEEDOJJY= github.com/stafiprotocol/go-substrate-rpc-client v1.2.1 h1:dOViH/pvHoAxhTkIwg/7NESTd6TsHgN7w/qNjtmlVLk= github.com/stafiprotocol/go-substrate-rpc-client v1.2.1/go.mod h1:iVtW5HNDMp68AEEpL3XDuC6iE3amtkz+t26bdblYW18= -github.com/stafiprotocol/solana-go-sdk v1.3.0 h1:hOpO+YsquFI4oEgdv5hb4UJMaMGLloZE9BT9g9wlUaw= -github.com/stafiprotocol/solana-go-sdk v1.3.0/go.mod h1:/0nE9JnF+mhWxwJPhyyTsUHNKC41VsxZ2uKpyEWb+bM= +github.com/stafiprotocol/solana-go-sdk v1.4.0 h1:8Ihuunrf8gB0KFNsBfI3cKfqQgSWaIxlYP2SQnl9GgI= +github.com/stafiprotocol/solana-go-sdk v1.4.0/go.mod h1:/0nE9JnF+mhWxwJPhyyTsUHNKC41VsxZ2uKpyEWb+bM= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4 h1:Gb2Tyox57NRNuZ2d3rmvB3pcmbu7O1RS3m8WRx7ilrg= github.com/status-im/keycard-go v0.0.0-20190316090335-8537d3370df4/go.mod h1:RZLeN1LMWmRsyYjvAu+I6Dm9QmlDaIIt+Y+4Kd7Tp+Q= github.com/stbenjam/no-sprintf-host-port v0.1.1/go.mod h1:TLhvtIvONRzdmkFiio4O8LHsN9N74I+PhRquPsxpL0I=