kaspad/server/rpc/handle_decode_script.go
Ori Newman 30fe0c279b
[NOD-738] Move rpcmodel helper functions to pointers package (#629)
* [NOD-738] Move rpcmodel helper functions to copytopointer package

* [NOD-738] Rename copytopointer->pointers
2020-02-18 14:06:34 +02:00

57 lines
1.6 KiB
Go

package rpc
import (
"encoding/hex"
"github.com/kaspanet/kaspad/rpcmodel"
"github.com/kaspanet/kaspad/txscript"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/pointers"
)
// handleDecodeScript handles decodeScript commands.
func handleDecodeScript(s *Server, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
c := cmd.(*rpcmodel.DecodeScriptCmd)
// Convert the hex script to bytes.
hexStr := c.HexScript
if len(hexStr)%2 != 0 {
hexStr = "0" + hexStr
}
script, err := hex.DecodeString(hexStr)
if err != nil {
return nil, rpcDecodeHexError(hexStr)
}
// The disassembled string will contain [error] inline if the script
// doesn't fully parse, so ignore the error here.
disbuf, _ := txscript.DisasmString(script)
// Get information about the script.
// Ignore the error here since an error means the script couldn't parse
// and there is no additinal information about it anyways.
scriptClass, addr, _ := txscript.ExtractScriptPubKeyAddress(script,
s.cfg.DAGParams)
var address *string
if addr != nil {
address = pointers.String(addr.EncodeAddress())
}
// Convert the script itself to a pay-to-script-hash address.
p2sh, err := util.NewAddressScriptHash(script, s.cfg.DAGParams.Prefix)
if err != nil {
context := "Failed to convert script to pay-to-script-hash"
return nil, internalRPCError(err.Error(), context)
}
// Generate and return the reply.
reply := rpcmodel.DecodeScriptResult{
Asm: disbuf,
Type: scriptClass.String(),
Address: address,
}
if scriptClass != txscript.ScriptHashTy {
reply.P2sh = p2sh.EncodeAddress()
}
return reply, nil
}