diff --git a/jsonapi.go b/jsonapi.go index 0429493d..a5c0c594 100644 --- a/jsonapi.go +++ b/jsonapi.go @@ -47,19 +47,21 @@ type InfoResult struct { // BlockResult models the data from the getblock command. type BlockResult struct { - Hash string `json:"hash"` - Confirmations uint64 `json:"confirmations"` - Size int `json:"size"` - Height int64 `json:"height"` - Version uint32 `json:"version"` - MerkleRoot string `json:"merkleroot"` - Tx []string `json:"tx"` - Time int64 `json:"time"` - Nonce uint32 `json:"nonce"` - Bits string `json:"bits"` - Difficulty float64 `json:"difficulty"` - PreviousHash string `json:"previousblockhash"` - NextHash string `json:"nextblockhash"` + Hex string `json:"hex,omitempty"` + Hash string `json:"hash"` + Confirmations uint64 `json:"confirmations"` + Size int `json:"size"` + Height int64 `json:"height"` + Version uint32 `json:"version"` + MerkleRoot string `json:"merkleroot"` + Tx []string `json:"tx,omitempty"` + RawTx []TxRawResult `json:"rawtx,omitempty"` + Time int64 `json:"time"` + Nonce uint32 `json:"nonce"` + Bits string `json:"bits"` + Difficulty float64 `json:"difficulty"` + PreviousHash string `json:"previousblockhash"` + NextHash string `json:"nextblockhash"` } // TxRawResult models the data from the getrawtransaction command. @@ -70,10 +72,10 @@ type TxRawResult struct { LockTime uint32 `json:"locktime"` Vin []Vin `json:"vin"` Vout []Vout `json:"vout"` - BlockHash string `json:"blockhash"` - Confirmations uint64 `json:"confirmations"` - Time int64 `json:"time"` - Blocktime int64 `json:"blocktime"` + BlockHash string `json:"blockhash,omitempty"` + Confirmations uint64 `json:"confirmations,omitempty"` + Time int64 `json:"time,omitempty"` + Blocktime int64 `json:"blocktime,omitempty"` } // TxRawDecodeResult models the data from the decoderawtransaction command. @@ -89,11 +91,11 @@ type TxRawDecodeResult struct { // getrawtransaction and decoderawtransaction use the same structure. type Vin struct { Coinbase string `json:"coinbase,omitempty"` + Txid string `json:"txid,omitempty"` Vout int `json:"vout,omitempty"` ScriptSig struct { - Txid string `json:"txid"` - Asm string `json:"asm"` - Hex string `json:"hex"` + Asm string `json:"asm"` + Hex string `json:"hex"` } `json:"scriptSig,omitempty"` Sequence float64 `json:"sequence"` } diff --git a/jsoncmd.go b/jsoncmd.go index 30e1df9c..b0968f28 100644 --- a/jsoncmd.go +++ b/jsoncmd.go @@ -1705,18 +1705,40 @@ func (cmd *GetBestBlockHashCmd) UnmarshalJSON(b []byte) error { // GetBlockCmd is a type handling custom marshaling and // unmarshaling of getblock JSON RPC commands. type GetBlockCmd struct { - id interface{} - Hash string + id interface{} + Hash string + Verbose bool + VerboseTx bool } // Enforce that GetBlockCmd satisifies the Cmd interface. var _ Cmd = &GetBlockCmd{} // NewGetBlockCmd creates a new GetBlockCmd. -func NewGetBlockCmd(id interface{}, hash string) (*GetBlockCmd, error) { +func NewGetBlockCmd(id interface{}, hash string, optArgs ...bool) (*GetBlockCmd, error) { + // default verbose is set to true to match old behavior + verbose, verboseTx := true, false + + optArgsLen := len(optArgs) + if optArgsLen > 0 { + if optArgsLen > 2 { + return nil, ErrTooManyOptArgs + } + verbose = optArgs[0] + if optArgsLen > 1 { + verboseTx = optArgs[1] + + if !verbose && verboseTx { + return nil, ErrInvalidParams + } + } + } + return &GetBlockCmd{ - id: id, - Hash: hash, + id: id, + Hash: hash, + Verbose: verbose, + VerboseTx: verboseTx, }, nil } @@ -1734,14 +1756,28 @@ func (cmd *GetBlockCmd) Method() string { func (cmd *GetBlockCmd) MarshalJSON() ([]byte, error) { // Fill and marshal a RawCmd. - return json.Marshal(RawCmd{ + raw := RawCmd{ Jsonrpc: "1.0", Method: "getblock", Id: cmd.id, Params: []interface{}{ cmd.Hash, }, - }) + } + + if !cmd.Verbose { + // set optional verbose argument to false + raw.Params = append(raw.Params, false) + } else { + if cmd.VerboseTx { + // set optional verbose argument to true + raw.Params = append(raw.Params, true) + // set optional verboseTx argument to true + raw.Params = append(raw.Params, true) + } + } + + return json.Marshal(raw) } // UnmarshalJSON unmarshals the JSON encoding of cmd into cmd. Part of @@ -1753,15 +1789,34 @@ func (cmd *GetBlockCmd) UnmarshalJSON(b []byte) error { return err } - if len(r.Params) != 1 { + if len(r.Params) > 3 || len(r.Params) < 1 { return ErrWrongNumberOfParams } + hash, ok := r.Params[0].(string) if !ok { return errors.New("first parameter hash must be a string") } - newCmd, err := NewGetBlockCmd(r.Id, hash) + optArgs := make([]bool, 0, 1) + if len(r.Params) > 1 { + verbose, ok := r.Params[1].(bool) + if !ok { + return errors.New("second optional parameter verbose must be a bool") + } + + optArgs = append(optArgs, verbose) + } + if len(r.Params) == 3 { + verboseTx, ok := r.Params[2].(bool) + if !ok { + return errors.New("third optional parameter verboseTx must be a bool") + } + + optArgs = append(optArgs, verboseTx) + } + + newCmd, err := NewGetBlockCmd(r.Id, hash, optArgs...) if err != nil { return err }