OSDN Git Service

Merge pull request #987 from Bytom/dev-sourceid
authorPaladz <yzhu101@uottawa.ca>
Wed, 23 May 2018 15:08:30 +0000 (23:08 +0800)
committerGitHub <noreply@github.com>
Wed, 23 May 2018 15:08:30 +0000 (23:08 +0800)
get-block response transaction add source_id

api/api.go
api/program.go [new file with mode: 0644]
cmd/bytomcli/commands/bytomcli.go
cmd/bytomcli/commands/program.go [new file with mode: 0644]

index 10d8bb6..2bd6ced 100644 (file)
@@ -235,6 +235,8 @@ func (a *API) buildHandler() {
        m.Handle("/submit-work", jsonHandler(a.submitWork))
 
        m.Handle("/verify-message", jsonHandler(a.verifyMessage))
+       m.Handle("/decode-program", jsonHandler(a.decodeProgram))
+
        m.Handle("/gas-rate", jsonHandler(a.gasRate))
        m.Handle("/net-info", jsonHandler(a.getNetInfo))
 
diff --git a/api/program.go b/api/program.go
new file mode 100644 (file)
index 0000000..624503a
--- /dev/null
@@ -0,0 +1,46 @@
+package api
+
+import (
+       "context"
+       "encoding/hex"
+       "fmt"
+
+       "github.com/bytom/consensus/segwit"
+       "github.com/bytom/protocol/vm"
+)
+
+// DecodeProgResp is response for decode program
+type DecodeProgResp struct {
+       Instructions string `json:"instructions"`
+}
+
+func (a *API) decodeProgram(ctx context.Context, ins struct {
+       Program string `json:"program"`
+}) Response {
+       prog, err := hex.DecodeString(ins.Program)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
+       // if program is P2PKH or P2SH script, convert it into actual executed program
+       if segwit.IsP2WPKHScript(prog) {
+               if witnessProg, err := segwit.ConvertP2PKHSigProgram(prog); err == nil {
+                       prog = witnessProg
+               }
+       } else if segwit.IsP2WSHScript(prog) {
+               if witnessProg, err := segwit.ConvertP2SHProgram(prog); err == nil {
+                       prog = witnessProg
+               }
+       }
+
+       insts, err := vm.ParseProgram(prog)
+       if err != nil {
+               return NewErrorResponse(err)
+       }
+
+       var result string
+       for _, inst := range insts {
+               result += fmt.Sprintf("%s %s\n", inst.Op, hex.EncodeToString(inst.Data))
+       }
+       return NewSuccessResponse(DecodeProgResp{Instructions: result})
+}
index 7690d7c..0a28042 100644 (file)
@@ -152,6 +152,7 @@ func AddCommands() {
 
        BytomcliCmd.AddCommand(signMsgCmd)
        BytomcliCmd.AddCommand(verifyMsgCmd)
+       BytomcliCmd.AddCommand(decodeProgCmd)
 
        BytomcliCmd.AddCommand(createTransactionFeedCmd)
        BytomcliCmd.AddCommand(listTransactionFeedsCmd)
diff --git a/cmd/bytomcli/commands/program.go b/cmd/bytomcli/commands/program.go
new file mode 100644 (file)
index 0000000..6543227
--- /dev/null
@@ -0,0 +1,26 @@
+package commands
+
+import (
+       "os"
+
+       "github.com/spf13/cobra"
+
+       "github.com/bytom/util"
+)
+
+var decodeProgCmd = &cobra.Command{
+       Use:   "decode-program <program>",
+       Short: "decode program to instruction and data",
+       Args:  cobra.ExactArgs(1),
+       Run: func(cmd *cobra.Command, args []string) {
+               var req = struct {
+                       Program string `json:"program"`
+               }{Program: args[0]}
+
+               data, exitCode := util.ClientCall("/decode-program", &req)
+               if exitCode != util.Success {
+                       os.Exit(exitCode)
+               }
+               printJSON(data)
+       },
+}