From ef7053a627a65593bc9e89c9bd8838cbc534d1d7 Mon Sep 17 00:00:00 2001 From: saito Date: Wed, 3 Jan 2024 15:16:14 +0800 Subject: [PATCH] feat(ioctl): ipfs endpoint and ipfs gateway config (#4038) --- ioctl/cmd/ws/ws.go | 19 ++++++++++++ ioctl/config/config.go | 16 ++++++++-- ioctl/config/configsetget.go | 31 ++++++++++++++----- ioctl/newcmd/config/config.go | 39 ++++++++++++++++++------ ioctl/newcmd/config/config_test.go | 48 ++++++++++++++++++++++++------ 5 files changed, 125 insertions(+), 28 deletions(-) diff --git a/ioctl/cmd/ws/ws.go b/ioctl/cmd/ws/ws.go index 777647b294..328dd6ad4b 100644 --- a/ioctl/cmd/ws/ws.go +++ b/ioctl/cmd/ws/ws.go @@ -28,6 +28,17 @@ var ( config.English: "set w3bsteram endpoint for once", config.Chinese: "一次设置w3bstream端点", } + + _flagIPFSEndpointUsages = map[config.Language]string{ + config.English: "set ipfs endpoint for resource uploading for once", + config.Chinese: "一次设置ipfs端点", + } + + _flagIPFSGatewayUsages = map[config.Language]string{ + config.English: "set ipfs gateway for resource fetching for once", + config.Chinese: "一次设置ipfs网关", + } + _flagContractAddressUsages = map[config.Language]string{ config.English: "set w3bsteram project register contract address for once", config.Chinese: "一次设置w3bstream项目注册合约地址", @@ -47,6 +58,14 @@ func init() { &config.ReadConfig.WsEndpoint, "ws-endpoint", config.ReadConfig.WsEndpoint, config.TranslateInLang(_flagWsEndpointUsages, config.UILanguage), ) + WsCmd.PersistentFlags().StringVar( + &config.ReadConfig.IPFSEndpoint, "ipfs-endpoint", + config.ReadConfig.IPFSEndpoint, config.TranslateInLang(_flagIPFSEndpointUsages, config.UILanguage), + ) + WsCmd.PersistentFlags().StringVar( + &config.ReadConfig.IPFSGateway, "ipfs-gateway", + config.ReadConfig.IPFSGateway, config.TranslateInLang(_flagIPFSGatewayUsages, config.UILanguage), + ) WsCmd.PersistentFlags().StringVar( &config.ReadConfig.WsRegisterContract, "contract-address", config.ReadConfig.WsRegisterContract, config.TranslateInLang(_flagContractAddressUsages, config.UILanguage), diff --git a/ioctl/config/config.go b/ioctl/config/config.go index 9956068452..99a140561c 100644 --- a/ioctl/config/config.go +++ b/ioctl/config/config.go @@ -69,8 +69,12 @@ type Config struct { AnalyserEndpoint string `json:"analyserEndpoint" yaml:"analyserEndpoint"` // WsEndpoint w3bstream endpoint WsEndpoint string `json:"wsEndpoint" yaml:"wsEndpoint"` + // IPFSEndpoint ipfs endpoint for uploading + IPFSEndpoint string `json:"ipfsEndpoint" yaml:"ipfsEndpoint"` + // IPFSGateway ipfs gateway for resource fetching (with scheme) + IPFSGateway string `json:"ipfsGateway" yaml:"ipfsGateway"` // WsRegisterContract w3bstream project register contract address - WsRegisterContract string `json:"wsProjectRegisterContract" yaml:"wsProjectRegisterContract"` + WsRegisterContract string `json:"wsRegisterContract" yaml:"wsRegisterContract"` } var ( @@ -122,8 +126,16 @@ func init() { ReadConfig.WsEndpoint = _defaultWsEndpoint completeness = false } + if ReadConfig.IPFSEndpoint == "" { + ReadConfig.IPFSEndpoint = _defaultIPFSEndpoint + completeness = false + } + if ReadConfig.IPFSGateway == "" { + ReadConfig.IPFSGateway = _defaultIPFSGateway + completeness = false + } if ReadConfig.WsRegisterContract == "" { - ReadConfig.WsRegisterContract = _defaultProjectRegisterContract + ReadConfig.WsRegisterContract = _defaultWsRegisterContract completeness = false } if !completeness { diff --git a/ioctl/config/configsetget.go b/ioctl/config/configsetget.go index b6df0f4ac0..2e2043dcad 100644 --- a/ioctl/config/configsetget.go +++ b/ioctl/config/configsetget.go @@ -27,15 +27,20 @@ const ( _localPattern = "localhost" _endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?` _defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org" - _defaultWsEndpoint = "sprout-staging.w3bstream.com:9000" - // _defaultProjectRegisterContract default project register contract address - _defaultProjectRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e" + // _defaultWsEndpoint default w3bstream endpoint + _defaultWsEndpoint = "sprout-staging.w3bstream.com:9000" + // _defaultIPFSEndpoint default IPFS endpoint for uploading + _defaultIPFSEndpoint = "ipfs.mainnet.iotex.io" + // _defaultIPFSGateway default IPFS gateway for resource fetching + _defaultIPFSGateway = "https://ipfs.io" + // _defaultWsRegisterContract default project register contract address + _defaultWsRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e" ) var ( _supportedLanguage = []string{"English", "中文"} - _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "projectRegisterContract"} - _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "projectRegisterContract", "all"} + _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsRegisterContract"} + _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsRegisterContract", "all"} _validExpl = []string{"iotexscan", "iotxplorer"} _endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$") ) @@ -153,7 +158,11 @@ func Get(arg string) error { fmt.Println(ReadConfig.AnalyserEndpoint) case "wsEndpoint": fmt.Println(ReadConfig.WsEndpoint) - case "projectRegisterContract": + case "ipfsEndpoint": + fmt.Println(ReadConfig.IPFSEndpoint) + case "ipfsGateway": + fmt.Println(ReadConfig.IPFSGateway) + case "wsRegisterContract": fmt.Println(ReadConfig.WsRegisterContract) case "all": fmt.Println(ReadConfig.String()) @@ -285,7 +294,11 @@ func set(args []string) error { ReadConfig.Nsv2height = height case "wsEndpoint": ReadConfig.WsEndpoint = args[1] - case "projectRegisterContract": + case "ipfsEndpoint": + ReadConfig.IPFSEndpoint = args[1] + case "ipfsGateway": + ReadConfig.IPFSGateway = args[1] + case "wsRegisterContract": ReadConfig.WsRegisterContract = args[1] } err := writeConfig() @@ -306,7 +319,9 @@ func reset() error { ReadConfig.Language = "English" ReadConfig.AnalyserEndpoint = _defaultAnalyserEndpoint ReadConfig.WsEndpoint = _defaultWsEndpoint - ReadConfig.WsRegisterContract = _defaultProjectRegisterContract + ReadConfig.IPFSEndpoint = _defaultIPFSEndpoint + ReadConfig.IPFSGateway = _defaultIPFSGateway + ReadConfig.WsRegisterContract = _defaultWsRegisterContract err := writeConfig() if err != nil { diff --git a/ioctl/newcmd/config/config.go b/ioctl/newcmd/config/config.go index a798825c1b..57b7ba0d43 100644 --- a/ioctl/newcmd/config/config.go +++ b/ioctl/newcmd/config/config.go @@ -33,16 +33,21 @@ const ( _localPattern = "localhost" _endpointPattern = "(" + _ipPattern + "|(" + _domainPattern + ")" + "|(" + _localPattern + "))" + `(:\d{1,5})?` _defaultAnalyserEndpoint = "https://iotex-analyser-api-mainnet.chainanalytics.org" - _defaultWsEndpoint = "sprout-staging.w3bstream.com:9000" _defaultConfigFileName = "config.default" - // _defaultWsProjectRegisterContract default w3bstream project register contract address - _defaultWsProjectRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e" + // _defaultWsEndpoint default w3bstream endpoint + _defaultWsEndpoint = "sprout-staging.w3bstream.com:9000" + // _defaultIPFSEndpoint default IPFS endpoint for uploading + _defaultIPFSEndpoint = "ipfs.mainnet.iotex.io" + // _defaultIPFSGateway default IPFS gateway for resource fetching + _defaultIPFSGateway = "https://ipfs.io" + // _defaultWsRegisterContract default w3bstream project register contract address + _defaultWsRegisterContract = "0x4F7e678B0203e0444E17512108dba4B08B39512e" ) var ( _supportedLanguage = []string{"English", "中文"} - _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "wsProjectRegisterContract"} - _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "analyserEndpoint", "wsEndpoint", "wsProjectRegisterContract", "all"} + _validArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "wsRegisterContract"} + _validGetArgs = []string{"endpoint", "wallet", "explorer", "defaultacc", "language", "nsv2height", "wsEndpoint", "ipfsEndpoint", "ipfsGateway", "analyserEndpoint", "wsRegisterContract", "all"} _validExpl = []string{"iotexscan", "iotxplorer"} _endpointCompile = regexp.MustCompile("^" + _endpointPattern + "$") _configDir = os.Getenv("HOME") + "/.config/ioctl/default" @@ -122,8 +127,14 @@ func InitConfig() (config.Config, string, error) { info.readConfig.WsEndpoint = _defaultWsEndpoint completeness = false } + if info.readConfig.IPFSEndpoint == "" { + info.readConfig.IPFSEndpoint = _defaultIPFSEndpoint + } + if info.readConfig.IPFSGateway == "" { + info.readConfig.IPFSGateway = _defaultIPFSGateway + } if info.readConfig.WsRegisterContract == "" { - info.readConfig.WsRegisterContract = _defaultWsProjectRegisterContract + info.readConfig.WsRegisterContract = _defaultWsRegisterContract } if !completeness { if err = info.writeConfig(); err != nil { @@ -155,7 +166,9 @@ func (c *info) reset() error { c.readConfig.Language = _supportedLanguage[0] c.readConfig.AnalyserEndpoint = _defaultAnalyserEndpoint c.readConfig.WsEndpoint = _defaultWsEndpoint - c.readConfig.WsRegisterContract = _defaultWsProjectRegisterContract + c.readConfig.IPFSEndpoint = _defaultIPFSEndpoint + c.readConfig.IPFSGateway = _defaultIPFSGateway + c.readConfig.WsRegisterContract = _defaultWsRegisterContract err := c.writeConfig() if err != nil { @@ -216,7 +229,11 @@ func (c *info) set(args []string, insecure bool, client ioctl.Client) (string, e c.readConfig.Nsv2height = height case "wsEndpoint": c.readConfig.WsEndpoint = args[1] - case "wsProjectRegisterContract": + case "ipfsEndpoint": + c.readConfig.IPFSEndpoint = args[1] + case "ipfsGateway": + c.readConfig.IPFSGateway = args[1] + case "wsRegisterContract": c.readConfig.WsRegisterContract = args[1] default: return "", config.ErrConfigNotMatch @@ -255,7 +272,11 @@ func (c *info) get(arg string) (string, error) { return c.readConfig.AnalyserEndpoint, nil case "wsEndpoint": return c.readConfig.WsEndpoint, nil - case "wsProjectRegisterContract": + case "ipfsEndpoint": + return c.readConfig.IPFSEndpoint, nil + case "ipfsGateway": + return c.readConfig.IPFSGateway, nil + case "wsRegisterContract": return c.readConfig.WsRegisterContract, nil case "all": return jsonString(c.readConfig) diff --git a/ioctl/newcmd/config/config_test.go b/ioctl/newcmd/config/config_test.go index 79e042a800..0ab84d2b62 100644 --- a/ioctl/newcmd/config/config_test.go +++ b/ioctl/newcmd/config/config_test.go @@ -41,6 +41,9 @@ func TestInitConfig(t *testing.T) { require.Equal(_validExpl[0], cfg.Explorer) require.Equal(_supportedLanguage[0], cfg.Language) require.Equal(filepath.Join(testPath, _defaultConfigFileName), cfgFilePath) + require.Equal(_defaultWsEndpoint, cfg.WsEndpoint) + require.Equal(_defaultIPFSEndpoint, cfg.IPFSEndpoint) + require.Equal(_defaultIPFSGateway, cfg.IPFSGateway) } func TestConfigGet(t *testing.T) { @@ -55,7 +58,9 @@ func TestConfigGet(t *testing.T) { Language: "English", AnalyserEndpoint: "testAnalyser", WsEndpoint: "testWsEndpoint", - WsRegisterContract: "testWsProjectRegisterContract", + IPFSEndpoint: "testIPFSEndpoint", + IPFSGateway: "testIPFSGateway", + WsRegisterContract: "testWsRegisterContract", }, testPath) tcs := []struct { @@ -95,12 +100,20 @@ func TestConfigGet(t *testing.T) { "testWsEndpoint", }, { - "wsProjectRegisterContract", - "testWsProjectRegisterContract", + "ipfsEndpoint", + "testIPFSEndpoint", + }, + { + "ipfsGateway", + "testIPFSGateway", + }, + { + "wsRegisterContract", + "testWsRegisterContract", }, { "all", - "\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\",\n \"wsProjectRegisterContract\": \"testWsProjectRegisterContract\"\n}", + "\"endpoint\": \"\",\n \"secureConnect\": true,\n \"aliases\": {},\n \"defaultAccount\": {\n \"addressOrAlias\": \"test\"\n },\n \"explorer\": \"iotexscan\",\n \"language\": \"English\",\n \"nsv2height\": 0,\n \"analyserEndpoint\": \"testAnalyser\",\n \"wsEndpoint\": \"testWsEndpoint\",\n \"ipfsEndpoint\": \"testIPFSEndpoint\",\n \"ipfsGateway\": \"testIPFSGateway\",\n \"wsRegisterContract\": \"testWsRegisterContract\"\n}", }, } @@ -128,7 +141,9 @@ func TestConfigReset(t *testing.T) { Language: "Croatian", AnalyserEndpoint: "testAnalyser", WsEndpoint: "testWsEndpoint", - WsRegisterContract: "testProjectRegisterContract", + IPFSEndpoint: "testIPFSEndpoint", + IPFSGateway: "testIPFSGateway", + WsRegisterContract: "testWsRegisterContract", }, cfgFile) // write the config to the temp dir and then reset @@ -144,7 +159,9 @@ func TestConfigReset(t *testing.T) { require.Equal("explorer", cfg.Explorer) require.Equal(config.Context{AddressOrAlias: ""}, cfg.DefaultAccount) require.Equal("testWsEndpoint", cfg.WsEndpoint) - require.Equal("testProjectRegisterContract", cfg.WsRegisterContract) + require.Equal("testIPFSEndpoint", cfg.IPFSEndpoint) + require.Equal("testIPFSGateway", cfg.IPFSGateway) + require.Equal("testWsRegisterContract", cfg.WsRegisterContract) require.NoError(info.reset()) require.NoError(info.loadConfig()) @@ -157,7 +174,9 @@ func TestConfigReset(t *testing.T) { require.Equal("English", resetCfg.Language) require.Equal(_defaultAnalyserEndpoint, resetCfg.AnalyserEndpoint) require.Equal(_defaultWsEndpoint, resetCfg.WsEndpoint) - require.Equal(_defaultWsProjectRegisterContract, resetCfg.WsRegisterContract) + require.Equal(_defaultIPFSEndpoint, resetCfg.IPFSEndpoint) + require.Equal(_defaultIPFSGateway, resetCfg.IPFSGateway) + require.Equal(_defaultWsRegisterContract, resetCfg.WsRegisterContract) require.Equal("iotexscan", resetCfg.Explorer) require.Equal(*new(config.Context), resetCfg.DefaultAccount) } @@ -175,6 +194,9 @@ func TestConfigSet(t *testing.T) { Explorer: "iotexscan", Language: "English", AnalyserEndpoint: "testAnalyser", + WsEndpoint: "testWsEndpoint", + IPFSEndpoint: "testIPFSEndpoint", + IPFSGateway: "testIPFSGateway", }, cfgFile) tcs := []struct { @@ -230,8 +252,16 @@ func TestConfigSet(t *testing.T) { "testWsEndpoint", }, { - []string{"wsProjectRegisterContract", "testWsProjectRegisterContract"}, - "testWsProjectRegisterContract", + []string{"ipfsEndpoint", "testIPFSEndpoint"}, + "testIPFSEndpoint", + }, + { + []string{"ipfsGateway", "testIPFSGateway"}, + "testIPFSGateway", + }, + { + []string{"wsRegisterContract", "testWsRegisterContract"}, + "testWsRegisterContract", }, }