github.com/iotexproject/iotex-core@v1.14.1-rc1/ioctl/cmd/ws/wscodeconvert.go (about)

     1  package ws
     2  
     3  import (
     4  	"bytes"
     5  	"compress/zlib"
     6  	"encoding/hex"
     7  	"encoding/json"
     8  	"fmt"
     9  	"io/fs"
    10  	"os"
    11  
    12  	"github.com/pkg/errors"
    13  	"github.com/spf13/cobra"
    14  
    15  	"github.com/iotexproject/iotex-core/ioctl/config"
    16  	"github.com/iotexproject/iotex-core/ioctl/output"
    17  )
    18  
    19  var (
    20  	// wsCodeConvert represents the w3bstream code convert command
    21  	wsCodeConvert = &cobra.Command{
    22  		Use:   "convert",
    23  		Short: config.TranslateInLang(wsCodeConvertShorts, config.UILanguage),
    24  		RunE: func(cmd *cobra.Command, args []string) error {
    25  			version, err := cmd.Flags().GetString("version")
    26  			if err != nil {
    27  				return errors.Wrap(err, "failed to get flag version")
    28  			}
    29  			vmType, err := cmd.Flags().GetString("vm-type")
    30  			if err != nil {
    31  				return errors.Wrap(err, "failed to get flag vm-type")
    32  			}
    33  			codeFile, err := cmd.Flags().GetString("code-file")
    34  			if err != nil {
    35  				return errors.Wrap(err, "failed to get flag code-file")
    36  			}
    37  			confFile, err := cmd.Flags().GetString("conf-file")
    38  			if err != nil {
    39  				return errors.Wrap(err, "failed to get flag conf-file")
    40  			}
    41  			expParam, err := cmd.Flags().GetString("expand-param")
    42  			if err != nil {
    43  				return errors.Wrap(err, "failed to get flag expand-param")
    44  			}
    45  
    46  			out, err := generateProjectFile(version, vmType, codeFile, confFile, expParam)
    47  			if err != nil {
    48  				return output.PrintError(err)
    49  			}
    50  			output.PrintResult(out)
    51  			return nil
    52  		},
    53  	}
    54  
    55  	// wsCodeConvertShorts w3bstream code convert shorts multi-lang support
    56  	wsCodeConvertShorts = map[config.Language]string{
    57  		config.English: "convert zkp code to hex string compressed with zlib",
    58  		config.Chinese: "将zkp代码通过zlib进行压缩之后转成hex字符串",
    59  	}
    60  
    61  	_flagVersionUsages = map[config.Language]string{
    62  		config.English: "version for the project config",
    63  		config.Chinese: "该project config的版本号",
    64  	}
    65  	_flagVMTypeUsages = map[config.Language]string{
    66  		config.English: "vm type, support risc0, halo2",
    67  		config.Chinese: "虚拟机类型,目前支持risc0和halo2",
    68  	}
    69  	_flagCodeFileUsages = map[config.Language]string{
    70  		config.English: "code file",
    71  		config.Chinese: "代码文件",
    72  	}
    73  	_flagConfFileUsages = map[config.Language]string{
    74  		config.English: "conf file",
    75  		config.Chinese: "配置文件",
    76  	}
    77  	_flagExpandParamUsages = map[config.Language]string{
    78  		config.English: "expand param, if you use risc0 vm, need it.",
    79  		config.Chinese: "扩展参数,risc0虚拟机需要此参数",
    80  	}
    81  )
    82  
    83  func init() {
    84  	wsCodeConvert.Flags().StringP("version", "v", "", config.TranslateInLang(_flagVersionUsages, config.UILanguage))
    85  	wsCodeConvert.Flags().StringP("vm-type", "t", "", config.TranslateInLang(_flagVMTypeUsages, config.UILanguage))
    86  	wsCodeConvert.Flags().StringP("code-file", "i", "", config.TranslateInLang(_flagCodeFileUsages, config.UILanguage))
    87  	wsCodeConvert.Flags().StringP("conf-file", "c", "", config.TranslateInLang(_flagConfFileUsages, config.UILanguage))
    88  	wsCodeConvert.Flags().StringP("expand-param", "e", "", config.TranslateInLang(_flagExpandParamUsages, config.UILanguage))
    89  
    90  	_ = wsCodeConvert.MarkFlagRequired("version")
    91  	_ = wsCodeConvert.MarkFlagRequired("vm-type")
    92  	_ = wsCodeConvert.MarkFlagRequired("code-file")
    93  }
    94  
    95  func generateProjectFile(version, vmType, codeFile, confFile, expParam string) (string, error) {
    96  	tye, err := stringToVMType(vmType)
    97  	if err != nil {
    98  		return "", err
    99  	}
   100  
   101  	hexString, err := convertCodeToZlibHex(codeFile)
   102  	if err != nil {
   103  		return "", err
   104  	}
   105  
   106  	confMaps := make([]map[string]interface{}, 0)
   107  
   108  	confMap := make(map[string]interface{})
   109  	if expParam != "" {
   110  		confMap["codeExpParam"] = expParam
   111  	}
   112  	confMap["vmType"] = string(tye)
   113  	confMap["code"] = hexString
   114  	confMap["version"] = version
   115  
   116  	confMaps = append(confMaps, confMap)
   117  	jsonConf, err := json.MarshalIndent(confMaps, "", "  ")
   118  	if err != nil {
   119  		return "", errors.Wrap(err, "failed to marshal config maps")
   120  	}
   121  
   122  	if confFile == "" {
   123  		confFile = fmt.Sprintf("./%s-config.json", vmType)
   124  	}
   125  	err = os.WriteFile(confFile, jsonConf, fs.FileMode(0666))
   126  	if err != nil {
   127  		return "", errors.Wrap(err, fmt.Sprintf("failed to write conf-file %s", confFile))
   128  	}
   129  
   130  	return fmt.Sprintf("conf file: %s", confFile), err
   131  }
   132  
   133  func convertCodeToZlibHex(codeFile string) (string, error) {
   134  	content, err := os.ReadFile(codeFile)
   135  	if err != nil {
   136  		return "", errors.Wrap(err, fmt.Sprintf("failed to read code-file %s", codeFile))
   137  	}
   138  	var b bytes.Buffer
   139  	w := zlib.NewWriter(&b)
   140  	_, err = w.Write(content)
   141  	if err != nil {
   142  		return "", errors.Wrap(err, fmt.Sprintf("failed to zlib code-file %s", codeFile))
   143  	}
   144  	w.Close()
   145  
   146  	hexString := hex.EncodeToString(b.Bytes())
   147  
   148  	return hexString, err
   149  }