-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.go
46 lines (39 loc) · 1.09 KB
/
program.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package api
import (
"context"
"encoding/hex"
"fmt"
"github.com/bytom/bystack/consensus/segwit"
"github.com/bytom/bystack/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})
}