diff --git a/jsonapi.go b/jsonapi.go index cf4ec150..b0514929 100644 --- a/jsonapi.go +++ b/jsonapi.go @@ -173,6 +173,14 @@ type GetMiningInfoResult struct { HashesPerSec float64 `json:"hashespersec"` } +// GetWorkResult models the data from the getwork command. +type GetWorkResult struct { + Data string `json:"data"` + Hash1 string `json:"hash1"` + Midstate string `json:"midstate"` + Target string `json:"target"` +} + // ValidateAddressResult models the data from the validateaddress command. type ValidateAddressResult struct { IsValid bool `json:"isvalid"` @@ -872,6 +880,12 @@ func ReadResultCmd(cmd string, message []byte) (Reply, error) { if err == nil { result.Result = res } + case "getwork": + var res GetWorkResult + err = json.Unmarshal(objmap["result"], &res) + if err == nil { + result.Result = res + } case "validateaddress": var res ValidateAddressResult err = json.Unmarshal(objmap["result"], &res) diff --git a/jsoncmd.go b/jsoncmd.go index 5224d948..55b44c0a 100644 --- a/jsoncmd.go +++ b/jsoncmd.go @@ -3751,49 +3751,48 @@ func (cmd *GetWorkCmd) UnmarshalJSON(b []byte) error { if err := json.Unmarshal(b, &r); err != nil { return err } - - if len(r.Params) != 1 { + if len(r.Params) > 1 { return ErrWrongNumberOfParams } - - rmap, ok := r.Params[0].(map[string]interface{}) - if !ok { - return errors.New("first optional parameter template request must be an object") - } - wrequest := new(WorkRequest) - // data is required - data, ok := rmap["data"] - if !ok { - return errors.New("WorkRequest data must be present") - } - sdata, ok := data.(string) - if !ok { - return errors.New("WorkRequest data must be a string") - } - wrequest.Data = sdata - - // target is required - target, ok := rmap["target"] - if !ok { - return errors.New("WorkRequest target must be present") - } - starget, ok := target.(string) - if !ok { - return errors.New("WorkRequest target must be a string") - } - wrequest.Target = starget - - // algorithm is optional - algo, ok := rmap["algorithm"] - if ok { - salgo, ok := algo.(string) + if len(r.Params) == 1 { + rmap, ok := r.Params[0].(map[string]interface{}) if !ok { - return errors.New("WorkRequest algorithm must be a string") + return errors.New("first optional parameter template request must be an object") } - wrequest.Algorithm = salgo - } + // data is required + data, ok := rmap["data"] + if !ok { + return errors.New("WorkRequest data must be present") + } + sdata, ok := data.(string) + if !ok { + return errors.New("WorkRequest data must be a string") + } + wrequest.Data = sdata + + // target is required + target, ok := rmap["target"] + if !ok { + return errors.New("WorkRequest target must be present") + } + starget, ok := target.(string) + if !ok { + return errors.New("WorkRequest target must be a string") + } + wrequest.Target = starget + + // algorithm is optional + algo, ok := rmap["algorithm"] + if ok { + salgo, ok := algo.(string) + if !ok { + return errors.New("WorkRequest algorithm must be a string") + } + wrequest.Algorithm = salgo + } + } newCmd, err := NewGetWorkCmd(r.Id, wrequest) if err != nil { return err