github.com/lbryio/lbcd@v0.22.119/btcjson/walletsvrresults.go (about)

     1  // Copyright (c) 2014-2020 The btcsuite developers
     2  // Use of this source code is governed by an ISC
     3  // license that can be found in the LICENSE file.
     4  
     5  package btcjson
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  
    11  	"github.com/lbryio/lbcd/txscript"
    12  )
    13  
    14  // CreateWalletResult models the result of the createwallet command.
    15  type CreateWalletResult struct {
    16  	Name    string `json:"name"`
    17  	Warning string `json:"warning"`
    18  }
    19  
    20  // embeddedAddressInfo includes all getaddressinfo output fields, excluding
    21  // metadata and relation to the wallet.
    22  //
    23  // It represents the non-metadata/non-wallet fields for GetAddressInfo, as well
    24  // as the precise fields for an embedded P2SH or P2WSH address.
    25  type embeddedAddressInfo struct {
    26  	Address             string                `json:"address"`
    27  	ScriptPubKey        string                `json:"scriptPubKey"`
    28  	Descriptor          *string               `json:"desc,omitempty"`
    29  	IsScript            bool                  `json:"isscript"`
    30  	IsChange            bool                  `json:"ischange"`
    31  	IsWitness           bool                  `json:"iswitness"`
    32  	WitnessVersion      int                   `json:"witness_version,omitempty"`
    33  	WitnessProgram      *string               `json:"witness_program,omitempty"`
    34  	ScriptType          *txscript.ScriptClass `json:"script,omitempty"`
    35  	Hex                 *string               `json:"hex,omitempty"`
    36  	PubKeys             *[]string             `json:"pubkeys,omitempty"`
    37  	SignaturesRequired  *int                  `json:"sigsrequired,omitempty"`
    38  	PubKey              *string               `json:"pubkey,omitempty"`
    39  	IsCompressed        *bool                 `json:"iscompressed,omitempty"`
    40  	HDMasterFingerprint *string               `json:"hdmasterfingerprint,omitempty"`
    41  	Labels              []string              `json:"labels"`
    42  }
    43  
    44  // GetAddressInfoResult models the result of the getaddressinfo command. It
    45  // contains information about a bitcoin address.
    46  //
    47  // Reference: https://bitcoincore.org/en/doc/0.20.0/rpc/wallet/getaddressinfo
    48  //
    49  // The GetAddressInfoResult has three segments:
    50  //  1. General information about the address.
    51  //  2. Metadata (Timestamp, HDKeyPath, HDSeedID) and wallet fields
    52  //     (IsMine, IsWatchOnly).
    53  //  3. Information about the embedded address in case of P2SH or P2WSH.
    54  //     Same structure as (1).
    55  type GetAddressInfoResult struct {
    56  	// The following fields are identical to embeddedAddressInfo.
    57  	// However, the utility to generate RPC help message can't handle
    58  	// embedded field properly. So, spell out the attributes individually.
    59  	Address             string                `json:"address"`
    60  	ScriptPubKey        string                `json:"scriptPubKey"`
    61  	Descriptor          *string               `json:"desc,omitempty"`
    62  	IsScript            bool                  `json:"isscript"`
    63  	IsChange            bool                  `json:"ischange"`
    64  	IsWitness           bool                  `json:"iswitness"`
    65  	WitnessVersion      int                   `json:"witness_version,omitempty"`
    66  	WitnessProgram      *string               `json:"witness_program,omitempty"`
    67  	ScriptType          *txscript.ScriptClass `json:"script,omitempty"`
    68  	Hex                 *string               `json:"hex,omitempty"`
    69  	PubKeys             *[]string             `json:"pubkeys,omitempty"`
    70  	SignaturesRequired  *int                  `json:"sigsrequired,omitempty"`
    71  	PubKey              *string               `json:"pubkey,omitempty"`
    72  	IsCompressed        *bool                 `json:"iscompressed,omitempty"`
    73  	HDMasterFingerprint *string               `json:"hdmasterfingerprint,omitempty"`
    74  	Labels              []string              `json:"labels"`
    75  
    76  	IsMine      bool                 `json:"ismine"`
    77  	IsWatchOnly bool                 `json:"iswatchonly"`
    78  	Timestamp   *int                 `json:"timestamp,omitempty"`
    79  	HDKeyPath   *string              `json:"hdkeypath,omitempty"`
    80  	HDSeedID    *string              `json:"hdseedid,omitempty"`
    81  	Embedded    *embeddedAddressInfo `json:"embedded,omitempty"`
    82  }
    83  
    84  // UnmarshalJSON provides a custom unmarshaller for GetAddressInfoResult.
    85  // It is adapted to avoid creating a duplicate raw struct for unmarshalling
    86  // the JSON bytes into.
    87  //
    88  // Reference: http://choly.ca/post/go-json-marshalling
    89  func (e *GetAddressInfoResult) UnmarshalJSON(data []byte) error {
    90  	// Step 1: Create type aliases of the original struct, including the
    91  	// embedded one.
    92  	type Alias GetAddressInfoResult
    93  	type EmbeddedAlias embeddedAddressInfo
    94  
    95  	// Step 2: Create an anonymous struct with raw replacements for the special
    96  	// fields.
    97  	aux := &struct {
    98  		ScriptType *string `json:"script,omitempty"`
    99  		Embedded   *struct {
   100  			ScriptType *string `json:"script,omitempty"`
   101  			*EmbeddedAlias
   102  		} `json:"embedded,omitempty"`
   103  		*Alias
   104  	}{
   105  		Alias: (*Alias)(e),
   106  	}
   107  
   108  	// Step 3: Unmarshal the data into the anonymous struct.
   109  	if err := json.Unmarshal(data, &aux); err != nil {
   110  		return err
   111  	}
   112  
   113  	// Step 4: Convert the raw fields to the desired types
   114  	var (
   115  		sc  *txscript.ScriptClass
   116  		err error
   117  	)
   118  
   119  	if aux.ScriptType != nil {
   120  		sc, err = txscript.NewScriptClass(*aux.ScriptType)
   121  		if err != nil {
   122  			return err
   123  		}
   124  	}
   125  
   126  	e.ScriptType = sc
   127  
   128  	if aux.Embedded != nil {
   129  		var (
   130  			embeddedSc *txscript.ScriptClass
   131  			err        error
   132  		)
   133  
   134  		if aux.Embedded.ScriptType != nil {
   135  			embeddedSc, err = txscript.NewScriptClass(*aux.Embedded.ScriptType)
   136  			if err != nil {
   137  				return err
   138  			}
   139  		}
   140  
   141  		e.Embedded = (*embeddedAddressInfo)(aux.Embedded.EmbeddedAlias)
   142  		e.Embedded.ScriptType = embeddedSc
   143  	}
   144  
   145  	return nil
   146  }
   147  
   148  // GetTransactionDetailsResult models the details data from the gettransaction command.
   149  //
   150  // This models the "short" version of the ListTransactionsResult type, which
   151  // excludes fields common to the transaction.  These common fields are instead
   152  // part of the GetTransactionResult.
   153  type GetTransactionDetailsResult struct {
   154  	Account           string   `json:"account"`
   155  	Address           string   `json:"address,omitempty"`
   156  	Amount            float64  `json:"amount"`
   157  	Category          string   `json:"category"`
   158  	InvolvesWatchOnly bool     `json:"involveswatchonly,omitempty"`
   159  	Fee               *float64 `json:"fee,omitempty"`
   160  	Vout              uint32   `json:"vout"`
   161  }
   162  
   163  // GetTransactionResult models the data from the gettransaction command.
   164  type GetTransactionResult struct {
   165  	Amount          float64                       `json:"amount"`
   166  	Fee             float64                       `json:"fee,omitempty"`
   167  	Confirmations   int64                         `json:"confirmations"`
   168  	BlockHash       string                        `json:"blockhash"`
   169  	BlockIndex      int64                         `json:"blockindex"`
   170  	BlockTime       int64                         `json:"blocktime"`
   171  	TxID            string                        `json:"txid"`
   172  	WalletConflicts []string                      `json:"walletconflicts"`
   173  	Time            int64                         `json:"time"`
   174  	TimeReceived    int64                         `json:"timereceived"`
   175  	Details         []GetTransactionDetailsResult `json:"details"`
   176  	Hex             string                        `json:"hex"`
   177  	Generated       bool                          `json:"generated"`
   178  }
   179  
   180  type ScanningOrFalse struct {
   181  	Value interface{}
   182  }
   183  
   184  type ScanProgress struct {
   185  	Duration int     `json:"duration"`
   186  	Progress float64 `json:"progress"`
   187  }
   188  
   189  // MarshalJSON implements the json.Marshaler interface
   190  func (h ScanningOrFalse) MarshalJSON() ([]byte, error) {
   191  	return json.Marshal(h.Value)
   192  }
   193  
   194  // UnmarshalJSON implements the json.Unmarshaler interface
   195  func (h *ScanningOrFalse) UnmarshalJSON(data []byte) error {
   196  	var unmarshalled interface{}
   197  	if err := json.Unmarshal(data, &unmarshalled); err != nil {
   198  		return err
   199  	}
   200  
   201  	switch v := unmarshalled.(type) {
   202  	case bool:
   203  		h.Value = v
   204  	case map[string]interface{}:
   205  		h.Value = ScanProgress{
   206  			Duration: int(v["duration"].(float64)),
   207  			Progress: v["progress"].(float64),
   208  		}
   209  	default:
   210  		return fmt.Errorf("invalid scanning value: %v", unmarshalled)
   211  	}
   212  
   213  	return nil
   214  }
   215  
   216  // GetWalletInfoResult models the result of the getwalletinfo command.
   217  type GetWalletInfoResult struct {
   218  	WalletName            string          `json:"walletname"`
   219  	WalletVersion         int             `json:"walletversion"`
   220  	TransactionCount      int             `json:"txcount"`
   221  	KeyPoolOldest         int             `json:"keypoololdest"`
   222  	KeyPoolSize           int             `json:"keypoolsize"`
   223  	KeyPoolSizeHDInternal *int            `json:"keypoolsize_hd_internal,omitempty"`
   224  	UnlockedUntil         *int            `json:"unlocked_until,omitempty"`
   225  	PayTransactionFee     float64         `json:"paytxfee"`
   226  	HDSeedID              *string         `json:"hdseedid,omitempty"`
   227  	PrivateKeysEnabled    bool            `json:"private_keys_enabled"`
   228  	AvoidReuse            bool            `json:"avoid_reuse"`
   229  	Scanning              ScanningOrFalse `json:"scanning"`
   230  }
   231  
   232  // InfoWalletResult models the data returned by the wallet server getinfo
   233  // command.
   234  type InfoWalletResult struct {
   235  	Version         int32   `json:"version"`
   236  	ProtocolVersion int32   `json:"protocolversion"`
   237  	WalletVersion   int32   `json:"walletversion"`
   238  	Balance         float64 `json:"balance"`
   239  	Blocks          int32   `json:"blocks"`
   240  	TimeOffset      int64   `json:"timeoffset"`
   241  	Connections     int32   `json:"connections"`
   242  	Proxy           string  `json:"proxy"`
   243  	Difficulty      float64 `json:"difficulty"`
   244  	TestNet         bool    `json:"testnet"`
   245  	KeypoolOldest   int64   `json:"keypoololdest"`
   246  	KeypoolSize     int32   `json:"keypoolsize"`
   247  	UnlockedUntil   int64   `json:"unlocked_until"`
   248  	PaytxFee        float64 `json:"paytxfee"`
   249  	RelayFee        float64 `json:"relayfee"`
   250  	Errors          string  `json:"errors"`
   251  	Staked          float64 `json:"staked"`
   252  }
   253  
   254  // ListTransactionsResult models the data from the listtransactions command.
   255  type ListTransactionsResult struct {
   256  	Abandoned         bool     `json:"abandoned"`
   257  	Account           string   `json:"account"`
   258  	Address           string   `json:"address,omitempty"`
   259  	Amount            float64  `json:"amount"`
   260  	BIP125Replaceable string   `json:"bip125-replaceable,omitempty"`
   261  	BlockHash         string   `json:"blockhash,omitempty"`
   262  	BlockHeight       *int32   `json:"blockheight,omitempty"`
   263  	BlockIndex        *int64   `json:"blockindex,omitempty"`
   264  	BlockTime         int64    `json:"blocktime,omitempty"`
   265  	Category          string   `json:"category"`
   266  	Confirmations     int64    `json:"confirmations"`
   267  	Fee               *float64 `json:"fee,omitempty"`
   268  	Generated         bool     `json:"generated,omitempty"`
   269  	InvolvesWatchOnly bool     `json:"involveswatchonly,omitempty"`
   270  	Label             *string  `json:"label,omitempty"`
   271  	Time              int64    `json:"time"`
   272  	TimeReceived      int64    `json:"timereceived"`
   273  	Trusted           bool     `json:"trusted"`
   274  	TxID              string   `json:"txid"`
   275  	Vout              uint32   `json:"vout"`
   276  	WalletConflicts   []string `json:"walletconflicts"`
   277  	Comment           string   `json:"comment,omitempty"`
   278  	OtherAccount      string   `json:"otheraccount,omitempty"`
   279  }
   280  
   281  // ListReceivedByAccountResult models the data from the listreceivedbyaccount
   282  // command.
   283  type ListReceivedByAccountResult struct {
   284  	Account       string  `json:"account"`
   285  	Amount        float64 `json:"amount"`
   286  	Confirmations uint64  `json:"confirmations"`
   287  }
   288  
   289  // ListReceivedByAddressResult models the data from the listreceivedbyaddress
   290  // command.
   291  type ListReceivedByAddressResult struct {
   292  	Address           string   `json:"address"`
   293  	Amount            float64  `json:"amount"`
   294  	Confirmations     uint64   `json:"confirmations"`
   295  	TxIDs             []string `json:"txids,omitempty"`
   296  	InvolvesWatchonly bool     `json:"involvesWatchonly,omitempty"`
   297  }
   298  
   299  // ListSinceBlockResult models the data from the listsinceblock command.
   300  type ListSinceBlockResult struct {
   301  	Transactions []ListTransactionsResult `json:"transactions"`
   302  	LastBlock    string                   `json:"lastblock"`
   303  }
   304  
   305  // ListUnspentResult models a successful response from the listunspent request.
   306  type ListUnspentResult struct {
   307  	TxID          string  `json:"txid"`
   308  	Vout          uint32  `json:"vout"`
   309  	Address       string  `json:"address"`
   310  	Account       string  `json:"account"`
   311  	ScriptPubKey  string  `json:"scriptPubKey"`
   312  	RedeemScript  string  `json:"redeemScript,omitempty"`
   313  	Amount        float64 `json:"amount"`
   314  	Confirmations int64   `json:"confirmations"`
   315  	Solvable      bool    `json:"solvable"`
   316  	Spendable     bool    `json:"spendable"`
   317  	IsStake       bool    `json:"isstake"`
   318  }
   319  
   320  // RescanBlockchainResult models the data returned from the rescanblockchain command.
   321  type RescanBlockchainResult struct {
   322  	StartHeight int32 `json:"start_height"`
   323  	StoptHeight int32 `json:"stop_height"`
   324  }
   325  
   326  // SignRawTransactionError models the data that contains script verification
   327  // errors from the signrawtransaction request.
   328  type SignRawTransactionError struct {
   329  	TxID      string `json:"txid"`
   330  	Vout      uint32 `json:"vout"`
   331  	ScriptSig string `json:"scriptSig"`
   332  	Sequence  uint32 `json:"sequence"`
   333  	Error     string `json:"error"`
   334  }
   335  
   336  // SignRawTransactionResult models the data from the signrawtransaction
   337  // command.
   338  type SignRawTransactionResult struct {
   339  	Hex      string                    `json:"hex"`
   340  	Complete bool                      `json:"complete"`
   341  	Errors   []SignRawTransactionError `json:"errors,omitempty"`
   342  }
   343  
   344  // SignRawTransactionWithWalletResult models the data from the
   345  // signrawtransactionwithwallet command.
   346  type SignRawTransactionWithWalletResult struct {
   347  	Hex      string                    `json:"hex"`
   348  	Complete bool                      `json:"complete"`
   349  	Errors   []SignRawTransactionError `json:"errors,omitempty"`
   350  }
   351  
   352  // ValidateAddressWalletResult models the data returned by the wallet server
   353  // validateaddress command.
   354  type ValidateAddressWalletResult struct {
   355  	IsValid      bool     `json:"isvalid"`
   356  	Address      string   `json:"address,omitempty"`
   357  	IsMine       bool     `json:"ismine,omitempty"`
   358  	IsWatchOnly  bool     `json:"iswatchonly,omitempty"`
   359  	IsScript     bool     `json:"isscript,omitempty"`
   360  	PubKey       string   `json:"pubkey,omitempty"`
   361  	IsCompressed bool     `json:"iscompressed,omitempty"`
   362  	Account      string   `json:"account,omitempty"`
   363  	Addresses    []string `json:"addresses,omitempty"`
   364  	Hex          string   `json:"hex,omitempty"`
   365  	Script       string   `json:"script,omitempty"`
   366  	SigsRequired int32    `json:"sigsrequired,omitempty"`
   367  }
   368  
   369  // GetBestBlockResult models the data from the getbestblock command.
   370  type GetBestBlockResult struct {
   371  	Hash   string `json:"hash"`
   372  	Height int32  `json:"height"`
   373  }
   374  
   375  // BalanceDetailsResult models the details data from the `getbalances` command.
   376  type BalanceDetailsResult struct {
   377  	Trusted          float64  `json:"trusted"`
   378  	UntrustedPending float64  `json:"untrusted_pending"`
   379  	Immature         float64  `json:"immature"`
   380  	Used             *float64 `json:"used"`
   381  }
   382  
   383  // GetBalancesResult models the data returned from the getbalances command.
   384  type GetBalancesResult struct {
   385  	Mine      BalanceDetailsResult  `json:"mine"`
   386  	WatchOnly *BalanceDetailsResult `json:"watchonly"`
   387  }
   388  
   389  // ImportMultiResults is a slice that models the result of the importmulti command.
   390  //
   391  // Each item in the slice contains the execution result corresponding to the input
   392  // requests of type btcjson.ImportMultiRequest, passed to the ImportMulti[Async]
   393  // function.
   394  type ImportMultiResults []struct {
   395  	Success  bool      `json:"success"`
   396  	Error    *RPCError `json:"error,omitempty"`
   397  	Warnings *[]string `json:"warnings,omitempty"`
   398  }
   399  
   400  // WalletCreateFundedPsbtResult models the data returned from the
   401  // walletcreatefundedpsbtresult command.
   402  type WalletCreateFundedPsbtResult struct {
   403  	Psbt      string  `json:"psbt"`
   404  	Fee       float64 `json:"fee"`
   405  	ChangePos int64   `json:"changepos"`
   406  }
   407  
   408  // WalletProcessPsbtResult models the data returned from the
   409  // walletprocesspsbtresult command.
   410  type WalletProcessPsbtResult struct {
   411  	Psbt     string `json:"psbt"`
   412  	Complete bool   `json:"complete"`
   413  }