github.com/lbryio/lbcd@v0.22.119/btcjson/chainsvrresults.go (about) 1 // Copyright (c) 2014-2017 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 "bytes" 9 "encoding/hex" 10 "encoding/json" 11 12 "github.com/lbryio/lbcd/chaincfg/chainhash" 13 14 "github.com/lbryio/lbcd/wire" 15 btcutil "github.com/lbryio/lbcutil" 16 ) 17 18 // GetBlockHeaderVerboseResult models the data from the getblockheader command when 19 // the verbose flag is set. When the verbose flag is not set, getblockheader 20 // returns a hex-encoded string. 21 type GetBlockHeaderVerboseResult struct { 22 Hash string `json:"hash"` 23 Confirmations int64 `json:"confirmations"` 24 Height int32 `json:"height"` 25 Version int32 `json:"version"` 26 VersionHex string `json:"versionHex"` 27 MerkleRoot string `json:"merkleroot"` 28 ClaimTrie string `json:"nameclaimroot,omitempty"` 29 Time int64 `json:"time"` 30 Nonce uint64 `json:"nonce"` 31 Bits string `json:"bits"` 32 Difficulty float64 `json:"difficulty"` 33 PreviousHash string `json:"previousblockhash,omitempty"` 34 NextHash string `json:"nextblockhash,omitempty"` 35 } 36 37 // GetBlockStatsResult models the data from the getblockstats command. 38 // Pointers are used instead of values to allow for optional fields. 39 type GetBlockStatsResult struct { 40 AverageFee *int64 `json:"avgfee,omitempty"` 41 AverageFeeRate *int64 `json:"avgfeerate,omitempty"` 42 AverageTxSize *int64 `json:"avgtxsize,omitempty"` 43 FeeratePercentiles *[]int64 `json:"feerate_percentiles,omitempty"` 44 Hash *string `json:"blockhash,omitempty"` 45 Height *int64 `json:"height,omitempty"` 46 Ins *int64 `json:"ins,omitempty"` 47 MaxFee *int64 `json:"maxfee,omitempty"` 48 MaxFeeRate *int64 `json:"maxfeerate,omitempty"` 49 MaxTxSize *int64 `json:"maxtxsize,omitempty"` 50 MedianFee *int64 `json:"medianfee,omitempty"` 51 MedianTime *int64 `json:"mediantime,omitempty"` 52 MedianTxSize *int64 `json:"mediantxsize,omitempty"` 53 MinFee *int64 `json:"minfee,omitempty"` 54 MinFeeRate *int64 `json:"minfeerate,omitempty"` 55 MinTxSize *int64 `json:"mintxsize,omitempty"` 56 Outs *int64 `json:"outs,omitempty"` 57 SegWitTotalSize *int64 `json:"swtotal_size,omitempty"` 58 SegWitTotalWeight *int64 `json:"swtotal_weight,omitempty"` 59 SegWitTxs *int64 `json:"swtxs,omitempty"` 60 Subsidy *int64 `json:"subsidy,omitempty"` 61 Time *int64 `json:"time,omitempty"` 62 TotalOut *int64 `json:"total_out,omitempty"` 63 TotalSize *int64 `json:"total_size,omitempty"` 64 TotalWeight *int64 `json:"total_weight,omitempty"` 65 TotalFee *int64 `json:"totalfee,omitempty"` 66 Txs *int64 `json:"txs,omitempty"` 67 UTXOIncrease *int64 `json:"utxo_increase,omitempty"` 68 UTXOSizeIncrease *int64 `json:"utxo_size_inc,omitempty"` 69 } 70 71 type GetBlockVerboseResultBase struct { 72 Hash string `json:"hash"` 73 Confirmations int64 `json:"confirmations"` 74 StrippedSize int32 `json:"strippedsize"` 75 Size int32 `json:"size"` 76 Weight int32 `json:"weight"` 77 Height int64 `json:"height"` 78 Version int32 `json:"version"` 79 VersionHex string `json:"versionHex"` 80 MerkleRoot string `json:"merkleroot"` 81 Time int64 `json:"time"` 82 MedianTime int64 `json:"mediantime"` 83 Nonce uint32 `json:"nonce"` 84 Bits string `json:"bits"` 85 Difficulty float64 `json:"difficulty"` 86 ChainWork string `json:"chainwork"` 87 PreviousHash string `json:"previousblockhash,omitempty"` 88 NextHash string `json:"nextblockhash,omitempty"` 89 90 ClaimTrie string `json:"nameclaimroot,omitempty"` 91 TxCount int `json:"nTx"` // For backwards compatibility only 92 } 93 94 // GetBlockVerboseResult models the data from the getblock command when the 95 // verbose flag is set to 1. When the verbose flag is set to 0, getblock returns a 96 // hex-encoded string. When the verbose flag is set to 1, getblock returns an object 97 // whose tx field is an array of transaction hashes. When the verbose flag is set to 2, 98 // getblock returns an object whose tx field is an array of raw transactions. 99 // Use GetBlockVerboseTxResult to unmarshal data received from passing verbose=2 to getblock. 100 type GetBlockVerboseResult struct { 101 GetBlockVerboseResultBase 102 Tx []string `json:"tx"` 103 } 104 105 // GetBlockVerboseTxResult models the data from the getblock command when the 106 // verbose flag is set to 2. When the verbose flag is set to 0, getblock returns a 107 // hex-encoded string. When the verbose flag is set to 1, getblock returns an object 108 // whose tx field is an array of transaction hashes. When the verbose flag is set to 2, 109 // getblock returns an object whose tx field is an array of raw transactions. 110 // Use GetBlockVerboseResult to unmarshal data received from passing verbose=1 to getblock. 111 type GetBlockVerboseTxResult struct { 112 GetBlockVerboseResultBase 113 Tx []TxRawResult `json:"tx"` 114 } 115 116 // GetChainTxStatsResult models the data from the getchaintxstats command. 117 type GetChainTxStatsResult struct { 118 Time int64 `json:"time"` 119 TxCount int64 `json:"txcount"` 120 WindowFinalBlockHash string `json:"window_final_block_hash"` 121 WindowFinalBlockHeight int32 `json:"window_final_block_height"` 122 WindowBlockCount int32 `json:"window_block_count"` 123 WindowTxCount int32 `json:"window_tx_count"` 124 WindowInterval int32 `json:"window_interval"` 125 TxRate float64 `json:"txrate"` 126 } 127 128 // CreateMultiSigResult models the data returned from the createmultisig 129 // command. 130 type CreateMultiSigResult struct { 131 Address string `json:"address"` 132 RedeemScript string `json:"redeemScript"` 133 } 134 135 // DecodeScriptResult models the data returned from the decodescript command. 136 type DecodeScriptResult struct { 137 Asm string `json:"asm"` 138 ReqSigs int32 `json:"reqSigs,omitempty"` 139 Type string `json:"type"` 140 Addresses []string `json:"addresses,omitempty"` 141 P2sh string `json:"p2sh,omitempty"` 142 } 143 144 // GetAddedNodeInfoResultAddr models the data of the addresses portion of the 145 // getaddednodeinfo command. 146 type GetAddedNodeInfoResultAddr struct { 147 Address string `json:"address"` 148 Connected string `json:"connected"` 149 } 150 151 // GetAddedNodeInfoResult models the data from the getaddednodeinfo command. 152 type GetAddedNodeInfoResult struct { 153 AddedNode string `json:"addednode"` 154 Connected *bool `json:"connected,omitempty"` 155 Addresses *[]GetAddedNodeInfoResultAddr `json:"addresses,omitempty"` 156 } 157 158 // SoftForkDescription describes the current state of a soft-fork which was 159 // deployed using a super-majority block signalling. 160 type SoftForkDescription struct { 161 ID string `json:"id"` 162 Version uint32 `json:"version"` 163 Reject struct { 164 Status bool `json:"status"` 165 } `json:"reject"` 166 } 167 168 // Bip9SoftForkDescription describes the current state of a defined BIP0009 169 // version bits soft-fork. 170 type Bip9SoftForkDescription struct { 171 Status string `json:"status"` 172 Bit uint8 `json:"bit"` 173 StartTime1 int64 `json:"startTime"` 174 StartTime2 int64 `json:"start_time"` 175 Timeout int64 `json:"timeout"` 176 Since int32 `json:"since"` 177 } 178 179 // StartTime returns the starting time of the softfork as a Unix epoch. 180 func (d *Bip9SoftForkDescription) StartTime() int64 { 181 if d.StartTime1 != 0 { 182 return d.StartTime1 183 } 184 return d.StartTime2 185 } 186 187 // SoftForks describes the current softforks enabled by the backend. Softforks 188 // activated through BIP9 are grouped together separate from any other softforks 189 // with different activation types. 190 type SoftForks struct { 191 SoftForks []*SoftForkDescription `json:"softforks"` 192 Bip9SoftForks map[string]*Bip9SoftForkDescription `json:"bip9_softforks"` 193 } 194 195 // UnifiedSoftForks describes a softforks in a general manner, irrespective of 196 // its activation type. This was a format introduced by bitcoind v0.19.0 197 type UnifiedSoftFork struct { 198 Type string `json:"type"` 199 BIP9SoftForkDescription *Bip9SoftForkDescription `json:"bip9"` 200 Height int32 `json:"height"` 201 Active bool `json:"active"` 202 } 203 204 // UnifiedSoftForks describes the current softforks enabled the by the backend 205 // in a unified manner, i.e, softforks with different activation types are 206 // grouped together. This was a format introduced by bitcoind v0.19.0 207 type UnifiedSoftForks struct { 208 SoftForks map[string]*UnifiedSoftFork `json:"softforks"` 209 } 210 211 // GetBlockChainInfoResult models the data returned from the getblockchaininfo 212 // command. 213 type GetBlockChainInfoResult struct { 214 Chain string `json:"chain"` 215 Blocks int32 `json:"blocks"` 216 Headers int32 `json:"headers"` 217 BestBlockHash string `json:"bestblockhash"` 218 Difficulty float64 `json:"difficulty"` 219 MedianTime int64 `json:"mediantime"` 220 VerificationProgress float64 `json:"verificationprogress,omitempty"` 221 InitialBlockDownload bool `json:"initialblockdownload,omitempty"` 222 Pruned bool `json:"pruned"` 223 PruneHeight int32 `json:"pruneheight,omitempty"` 224 ChainWork string `json:"chainwork,omitempty"` 225 SizeOnDisk int64 `json:"size_on_disk,omitempty"` 226 *SoftForks 227 *UnifiedSoftForks 228 } 229 230 // GetBlockFilterResult models the data returned from the getblockfilter 231 // command. 232 type GetBlockFilterResult struct { 233 Filter string `json:"filter"` // the hex-encoded filter data 234 Header string `json:"header"` // the hex-encoded filter header 235 } 236 237 // GetBlockTemplateResultTx models the transactions field of the 238 // getblocktemplate command. 239 type GetBlockTemplateResultTx struct { 240 Data string `json:"data"` 241 Hash string `json:"hash"` 242 TxID string `json:"txid"` 243 Depends []int64 `json:"depends"` 244 Fee int64 `json:"fee"` 245 SigOps int64 `json:"sigops"` 246 Weight int64 `json:"weight"` 247 } 248 249 // GetBlockTemplateResultAux models the coinbaseaux field of the 250 // getblocktemplate command. 251 type GetBlockTemplateResultAux struct { 252 Flags string `json:"flags"` 253 } 254 255 // GetBlockTemplateResult models the data returned from the getblocktemplate 256 // command. 257 type GetBlockTemplateResult struct { 258 // Base fields from BIP 0022. CoinbaseAux is optional. One of 259 // CoinbaseTxn or CoinbaseValue must be specified, but not both. 260 Bits string `json:"bits"` 261 CurTime int64 `json:"curtime"` 262 Height int64 `json:"height"` 263 PreviousHash string `json:"previousblockhash"` 264 SigOpLimit int64 `json:"sigoplimit,omitempty"` 265 SizeLimit int64 `json:"sizelimit,omitempty"` 266 WeightLimit int64 `json:"weightlimit,omitempty"` 267 Transactions []GetBlockTemplateResultTx `json:"transactions"` 268 Version int32 `json:"version"` 269 CoinbaseAux *GetBlockTemplateResultAux `json:"coinbaseaux,omitempty"` 270 CoinbaseTxn *GetBlockTemplateResultTx `json:"coinbasetxn,omitempty"` 271 CoinbaseValue *int64 `json:"coinbasevalue,omitempty"` 272 WorkID string `json:"workid,omitempty"` 273 274 // Witness commitment defined in BIP 0141. 275 DefaultWitnessCommitment string `json:"default_witness_commitment,omitempty"` 276 277 // Optional long polling from BIP 0022. 278 LongPollID string `json:"longpollid,omitempty"` 279 LongPollURI string `json:"longpolluri,omitempty"` 280 SubmitOld *bool `json:"submitold,omitempty"` 281 282 // Basic pool extension from BIP 0023. 283 Target string `json:"target,omitempty"` 284 Expires int64 `json:"expires,omitempty"` 285 286 // Mutations from BIP 0023. 287 MaxTime int64 `json:"maxtime,omitempty"` 288 MinTime int64 `json:"mintime,omitempty"` 289 Mutable []string `json:"mutable,omitempty"` 290 NonceRange string `json:"noncerange,omitempty"` 291 292 // Block proposal from BIP 0023. 293 Capabilities []string `json:"capabilities,omitempty"` 294 RejectReasion string `json:"reject-reason,omitempty"` 295 296 ClaimTrieHash string `json:"claimtrie"` 297 298 Rules []string `json:"rules,omitempty"` 299 } 300 301 // GetMempoolEntryResult models the data returned from the getmempoolentry's 302 // fee field 303 304 type MempoolFees struct { 305 Base float64 `json:"base"` 306 Modified float64 `json:"modified"` 307 Ancestor float64 `json:"ancestor"` 308 Descendant float64 `json:"descendant"` 309 } 310 311 // GetMempoolEntryResult models the data returned from the getmempoolentry 312 // command. 313 type GetMempoolEntryResult struct { 314 VSize int32 `json:"vsize"` 315 Size int32 `json:"size"` 316 Weight int64 `json:"weight"` 317 Fee float64 `json:"fee"` 318 ModifiedFee float64 `json:"modifiedfee"` 319 Time int64 `json:"time"` 320 Height int64 `json:"height"` 321 DescendantCount int64 `json:"descendantcount"` 322 DescendantSize int64 `json:"descendantsize"` 323 DescendantFees float64 `json:"descendantfees"` 324 AncestorCount int64 `json:"ancestorcount"` 325 AncestorSize int64 `json:"ancestorsize"` 326 AncestorFees float64 `json:"ancestorfees"` 327 WTxId string `json:"wtxid"` 328 Fees MempoolFees `json:"fees"` 329 Depends []string `json:"depends"` 330 SpentBy []string `json:"spentby"` 331 } 332 333 // GetChainTipsResult models the data returns from the getchaintips command. 334 type GetChainTipsResult struct { 335 Height int64 `json:"height"` 336 Hash string `json:"hash"` 337 BranchLen int64 `json:"branchlen"` 338 Status string `json:"status"` 339 } 340 341 // GetMempoolInfoResult models the data returned from the getmempoolinfo 342 // command. 343 type GetMempoolInfoResult struct { 344 Size int64 `json:"size"` // Current tx count 345 Bytes int64 `json:"bytes"` // Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted 346 Usage int64 `json:"usage"` // Total memory usage for the mempool 347 TotalFee float64 `json:"total_fee"` // Total fees for the mempool in LBC, ignoring modified fees through prioritizetransaction 348 MemPoolMinFee float64 `json:"mempoolminfee"` // Minimum fee rate in LBC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee 349 MinRelayTxFee float64 `json:"minrelaytxfee"` // Current minimum relay fee for transactions 350 UnbroadcastCount int64 `json:"unbroadcastcount"` // Current number of transactions that haven't passed initial broadcast yet 351 } 352 353 // NetworksResult models the networks data from the getnetworkinfo command. 354 type NetworksResult struct { 355 Name string `json:"name"` 356 Limited bool `json:"limited"` 357 Reachable bool `json:"reachable"` 358 Proxy string `json:"proxy"` 359 ProxyRandomizeCredentials bool `json:"proxy_randomize_credentials"` 360 } 361 362 // LocalAddressesResult models the localaddresses data from the getnetworkinfo 363 // command. 364 type LocalAddressesResult struct { 365 Address string `json:"address"` 366 Port uint16 `json:"port"` 367 Score int32 `json:"score"` 368 } 369 370 // GetNetworkInfoResult models the data returned from the getnetworkinfo 371 // command. 372 type GetNetworkInfoResult struct { 373 Version int32 `json:"version"` 374 SubVersion string `json:"subversion"` 375 ProtocolVersion int32 `json:"protocolversion"` 376 LocalServices string `json:"localservices"` 377 LocalRelay bool `json:"localrelay"` 378 TimeOffset int64 `json:"timeoffset"` 379 Connections int32 `json:"connections"` 380 NetworkActive bool `json:"networkactive"` 381 Networks []NetworksResult `json:"networks"` 382 RelayFee float64 `json:"relayfee"` 383 IncrementalFee float64 `json:"incrementalfee"` 384 LocalAddresses []LocalAddressesResult `json:"localaddresses"` 385 Warnings string `json:"warnings"` 386 } 387 388 // GetNodeAddressesResult models the data returned from the getnodeaddresses 389 // command. 390 type GetNodeAddressesResult struct { 391 // Timestamp in seconds since epoch (Jan 1 1970 GMT) keeping track of when the node was last seen 392 Time int64 `json:"time"` 393 Services uint64 `json:"services"` // The services offered 394 Address string `json:"address"` // The address of the node 395 Port uint16 `json:"port"` // The port of the node 396 } 397 398 // GetPeerInfoResult models the data returned from the getpeerinfo command. 399 type GetPeerInfoResult struct { 400 ID int32 `json:"id"` 401 Addr string `json:"addr"` 402 AddrLocal string `json:"addrlocal,omitempty"` 403 Services string `json:"services"` 404 RelayTxes bool `json:"relaytxes"` 405 LastSend int64 `json:"lastsend"` 406 LastRecv int64 `json:"lastrecv"` 407 BytesSent uint64 `json:"bytessent"` 408 BytesRecv uint64 `json:"bytesrecv"` 409 ConnTime int64 `json:"conntime"` 410 TimeOffset int64 `json:"timeoffset"` 411 PingTime float64 `json:"pingtime"` 412 PingWait float64 `json:"pingwait,omitempty"` 413 Version uint32 `json:"version"` 414 SubVer string `json:"subver"` 415 Inbound bool `json:"inbound"` 416 StartingHeight int32 `json:"startingheight"` 417 CurrentHeight int32 `json:"currentheight,omitempty"` 418 BanScore int32 `json:"banscore"` 419 FeeFilter int64 `json:"feefilter"` 420 SyncNode bool `json:"syncnode"` 421 } 422 423 // GetRawMempoolVerboseResult models the data returned from the getrawmempool 424 // command when the verbose flag is set. When the verbose flag is not set, 425 // getrawmempool returns an array of transaction hashes. 426 type GetRawMempoolVerboseResult struct { 427 Size int32 `json:"size"` 428 Vsize int32 `json:"vsize"` 429 Weight int32 `json:"weight"` 430 Fee float64 `json:"fee"` 431 Time int64 `json:"time"` 432 Height int64 `json:"height"` 433 StartingPriority float64 `json:"startingpriority"` 434 CurrentPriority float64 `json:"currentpriority"` 435 Depends []string `json:"depends"` 436 } 437 438 // ScriptPubKeyResult models the scriptPubKey data of a tx script. It is 439 // defined separately since it is used by multiple commands. 440 type ScriptPubKeyResult struct { 441 Asm string `json:"asm"` 442 Hex string `json:"hex,omitempty"` 443 ReqSigs int32 `json:"reqSigs,omitempty"` 444 Type string `json:"type"` 445 SubType string `json:"subtype"` 446 IsClaim bool `json:"isclaim"` 447 IsSupport bool `json:"issupport"` 448 Addresses []string `json:"addresses,omitempty"` 449 } 450 451 // GetTxOutResult models the data from the gettxout command. 452 type GetTxOutResult struct { 453 BestBlock string `json:"bestblock"` 454 Confirmations int64 `json:"confirmations"` 455 Value float64 `json:"value"` 456 ScriptPubKey ScriptPubKeyResult `json:"scriptPubKey"` 457 Coinbase bool `json:"coinbase"` 458 } 459 460 // GetTxOutSetInfoResult models the data from the gettxoutsetinfo command. 461 type GetTxOutSetInfoResult struct { 462 Height int64 `json:"height"` 463 BestBlock chainhash.Hash `json:"bestblock"` 464 Transactions int64 `json:"transactions"` 465 TxOuts int64 `json:"txouts"` 466 BogoSize int64 `json:"bogosize"` 467 HashSerialized chainhash.Hash `json:"hash_serialized_2"` 468 DiskSize int64 `json:"disk_size"` 469 TotalAmount btcutil.Amount `json:"total_amount"` 470 } 471 472 // UnmarshalJSON unmarshals the result of the gettxoutsetinfo JSON-RPC call 473 func (g *GetTxOutSetInfoResult) UnmarshalJSON(data []byte) error { 474 // Step 1: Create type aliases of the original struct. 475 type Alias GetTxOutSetInfoResult 476 477 // Step 2: Create an anonymous struct with raw replacements for the special 478 // fields. 479 aux := &struct { 480 BestBlock string `json:"bestblock"` 481 HashSerialized string `json:"hash_serialized_2"` 482 TotalAmount float64 `json:"total_amount"` 483 *Alias 484 }{ 485 Alias: (*Alias)(g), 486 } 487 488 // Step 3: Unmarshal the data into the anonymous struct. 489 if err := json.Unmarshal(data, &aux); err != nil { 490 return err 491 } 492 493 // Step 4: Convert the raw fields to the desired types 494 blockHash, err := chainhash.NewHashFromStr(aux.BestBlock) 495 if err != nil { 496 return err 497 } 498 499 g.BestBlock = *blockHash 500 501 serializedHash, err := chainhash.NewHashFromStr(aux.HashSerialized) 502 if err != nil { 503 return err 504 } 505 506 g.HashSerialized = *serializedHash 507 508 amount, err := btcutil.NewAmount(aux.TotalAmount) 509 if err != nil { 510 return err 511 } 512 513 g.TotalAmount = amount 514 515 return nil 516 } 517 518 // GetNetTotalsResult models the data returned from the getnettotals command. 519 type GetNetTotalsResult struct { 520 TotalBytesRecv uint64 `json:"totalbytesrecv"` 521 TotalBytesSent uint64 `json:"totalbytessent"` 522 TimeMillis int64 `json:"timemillis"` 523 } 524 525 // ScriptSig models a signature script. It is defined separately since it only 526 // applies to non-coinbase. Therefore the field in the Vin structure needs 527 // to be a pointer. 528 type ScriptSig struct { 529 Asm string `json:"asm"` 530 Hex string `json:"hex"` 531 } 532 533 // Vin models parts of the tx data. It is defined separately since 534 // getrawtransaction, decoderawtransaction, and searchrawtransaction use the 535 // same structure. 536 type Vin struct { 537 Coinbase string `json:"coinbase"` 538 Txid string `json:"txid"` 539 Vout uint32 `json:"vout"` 540 ScriptSig *ScriptSig `json:"scriptSig"` 541 Sequence uint32 `json:"sequence"` 542 Witness []string `json:"txinwitness"` 543 } 544 545 // IsCoinBase returns a bool to show if a Vin is a Coinbase one or not. 546 func (v *Vin) IsCoinBase() bool { 547 return len(v.Coinbase) > 0 548 } 549 550 // HasWitness returns a bool to show if a Vin has any witness data associated 551 // with it or not. 552 func (v *Vin) HasWitness() bool { 553 return len(v.Witness) > 0 554 } 555 556 // MarshalJSON provides a custom Marshal method for Vin. 557 func (v *Vin) MarshalJSON() ([]byte, error) { 558 if v.IsCoinBase() { 559 coinbaseStruct := struct { 560 Coinbase string `json:"coinbase"` 561 Sequence uint32 `json:"sequence"` 562 Witness []string `json:"witness,omitempty"` 563 }{ 564 Coinbase: v.Coinbase, 565 Sequence: v.Sequence, 566 Witness: v.Witness, 567 } 568 return json.Marshal(coinbaseStruct) 569 } 570 571 if v.HasWitness() { 572 txStruct := struct { 573 Txid string `json:"txid"` 574 Vout uint32 `json:"vout"` 575 ScriptSig *ScriptSig `json:"scriptSig"` 576 Witness []string `json:"txinwitness"` 577 Sequence uint32 `json:"sequence"` 578 }{ 579 Txid: v.Txid, 580 Vout: v.Vout, 581 ScriptSig: v.ScriptSig, 582 Witness: v.Witness, 583 Sequence: v.Sequence, 584 } 585 return json.Marshal(txStruct) 586 } 587 588 txStruct := struct { 589 Txid string `json:"txid"` 590 Vout uint32 `json:"vout"` 591 ScriptSig *ScriptSig `json:"scriptSig"` 592 Sequence uint32 `json:"sequence"` 593 }{ 594 Txid: v.Txid, 595 Vout: v.Vout, 596 ScriptSig: v.ScriptSig, 597 Sequence: v.Sequence, 598 } 599 return json.Marshal(txStruct) 600 } 601 602 // PrevOut represents previous output for an input Vin. 603 type PrevOut struct { 604 Addresses []string `json:"addresses,omitempty"` 605 Value float64 `json:"value"` 606 IsClaim bool `json:"isclaim"` 607 IsSupport bool `json:"issupport"` 608 } 609 610 // VinPrevOut is like Vin except it includes PrevOut. It is used by searchrawtransaction 611 type VinPrevOut struct { 612 Coinbase string `json:"coinbase"` 613 Txid string `json:"txid"` 614 Vout uint32 `json:"vout"` 615 ScriptSig *ScriptSig `json:"scriptSig"` 616 Witness []string `json:"txinwitness"` 617 PrevOut *PrevOut `json:"prevOut"` 618 Sequence uint32 `json:"sequence"` 619 } 620 621 // IsCoinBase returns a bool to show if a Vin is a Coinbase one or not. 622 func (v *VinPrevOut) IsCoinBase() bool { 623 return len(v.Coinbase) > 0 624 } 625 626 // HasWitness returns a bool to show if a Vin has any witness data associated 627 // with it or not. 628 func (v *VinPrevOut) HasWitness() bool { 629 return len(v.Witness) > 0 630 } 631 632 // MarshalJSON provides a custom Marshal method for VinPrevOut. 633 func (v *VinPrevOut) MarshalJSON() ([]byte, error) { 634 if v.IsCoinBase() { 635 coinbaseStruct := struct { 636 Coinbase string `json:"coinbase"` 637 Sequence uint32 `json:"sequence"` 638 }{ 639 Coinbase: v.Coinbase, 640 Sequence: v.Sequence, 641 } 642 return json.Marshal(coinbaseStruct) 643 } 644 645 if v.HasWitness() { 646 txStruct := struct { 647 Txid string `json:"txid"` 648 Vout uint32 `json:"vout"` 649 ScriptSig *ScriptSig `json:"scriptSig"` 650 Witness []string `json:"txinwitness"` 651 PrevOut *PrevOut `json:"prevOut,omitempty"` 652 Sequence uint32 `json:"sequence"` 653 }{ 654 Txid: v.Txid, 655 Vout: v.Vout, 656 ScriptSig: v.ScriptSig, 657 Witness: v.Witness, 658 PrevOut: v.PrevOut, 659 Sequence: v.Sequence, 660 } 661 return json.Marshal(txStruct) 662 } 663 664 txStruct := struct { 665 Txid string `json:"txid"` 666 Vout uint32 `json:"vout"` 667 ScriptSig *ScriptSig `json:"scriptSig"` 668 PrevOut *PrevOut `json:"prevOut,omitempty"` 669 Sequence uint32 `json:"sequence"` 670 }{ 671 Txid: v.Txid, 672 Vout: v.Vout, 673 ScriptSig: v.ScriptSig, 674 PrevOut: v.PrevOut, 675 Sequence: v.Sequence, 676 } 677 return json.Marshal(txStruct) 678 } 679 680 // Vout models parts of the tx data. It is defined separately since both 681 // getrawtransaction and decoderawtransaction use the same structure. 682 type Vout struct { 683 Value float64 `json:"value"` 684 N uint32 `json:"n"` 685 ScriptPubKey ScriptPubKeyResult `json:"scriptPubKey"` 686 } 687 688 // GetMiningInfoResult models the data from the getmininginfo command. 689 type GetMiningInfoResult struct { 690 Blocks int64 `json:"blocks"` 691 CurrentBlockSize uint64 `json:"currentblocksize"` 692 CurrentBlockWeight uint64 `json:"currentblockweight"` 693 CurrentBlockTx uint64 `json:"currentblocktx"` 694 Difficulty float64 `json:"difficulty"` 695 Errors string `json:"errors"` 696 Generate bool `json:"generate"` 697 GenProcLimit int32 `json:"genproclimit"` 698 HashesPerSec float64 `json:"hashespersec"` 699 NetworkHashPS float64 `json:"networkhashps"` 700 PooledTx uint64 `json:"pooledtx"` 701 TestNet bool `json:"testnet"` 702 } 703 704 // GetWorkResult models the data from the getwork command. 705 type GetWorkResult struct { 706 Data string `json:"data"` 707 Hash1 string `json:"hash1"` 708 Midstate string `json:"midstate"` 709 Target string `json:"target"` 710 } 711 712 // InfoChainResult models the data returned by the chain server getinfo command. 713 type InfoChainResult struct { 714 Version int32 `json:"version"` 715 ProtocolVersion int32 `json:"protocolversion"` 716 Blocks int32 `json:"blocks"` 717 TimeOffset int64 `json:"timeoffset"` 718 Connections int32 `json:"connections"` 719 Proxy string `json:"proxy"` 720 Difficulty float64 `json:"difficulty"` 721 TestNet bool `json:"testnet"` 722 RelayFee float64 `json:"relayfee"` 723 Errors string `json:"errors"` 724 } 725 726 // ListBannedResult models the data returned from the listbanned command. 727 type ListBannedResult struct { 728 Address string `json:"address"` 729 BanCreated int64 `json:"ban_created"` 730 BannedUntil int64 `json:"banned_until"` 731 BanDuration int64 `json:"ban_duration"` 732 TimeRemaining int64 `json:"time_remaining"` 733 } 734 735 // TxRawResult models the data from the getrawtransaction command. 736 type TxRawResult struct { 737 Hex string `json:"hex"` 738 Txid string `json:"txid"` 739 Hash string `json:"hash,omitempty"` 740 Size int32 `json:"size,omitempty"` 741 Vsize int32 `json:"vsize,omitempty"` 742 Weight int32 `json:"weight,omitempty"` 743 Version uint32 `json:"version"` 744 LockTime uint32 `json:"locktime"` 745 Vin []Vin `json:"vin"` 746 Vout []Vout `json:"vout"` 747 BlockHash string `json:"blockhash,omitempty"` 748 Confirmations uint64 `json:"confirmations,omitempty"` 749 Time int64 `json:"time,omitempty"` 750 Blocktime int64 `json:"blocktime,omitempty"` 751 } 752 753 // SearchRawTransactionsResult models the data from the searchrawtransaction 754 // command. 755 type SearchRawTransactionsResult struct { 756 Hex string `json:"hex,omitempty"` 757 Txid string `json:"txid"` 758 Hash string `json:"hash"` 759 Size string `json:"size"` 760 Vsize string `json:"vsize"` 761 Weight string `json:"weight"` 762 Version int32 `json:"version"` 763 LockTime uint32 `json:"locktime"` 764 Vin []VinPrevOut `json:"vin"` 765 Vout []Vout `json:"vout"` 766 BlockHash string `json:"blockhash,omitempty"` 767 Confirmations uint64 `json:"confirmations,omitempty"` 768 Time int64 `json:"time,omitempty"` 769 Blocktime int64 `json:"blocktime,omitempty"` 770 } 771 772 // TxRawDecodeResult models the data from the decoderawtransaction command. 773 type TxRawDecodeResult struct { 774 Txid string `json:"txid"` 775 Version int32 `json:"version"` 776 Locktime uint32 `json:"locktime"` 777 Vin []Vin `json:"vin"` 778 Vout []Vout `json:"vout"` 779 } 780 781 // ValidateAddressChainResult models the data returned by the chain server 782 // validateaddress command. 783 // 784 // Compared to the Bitcoin Core version, this struct lacks the scriptPubKey 785 // field since it requires wallet access, which is outside the scope of btcd. 786 // Ref: https://bitcoincore.org/en/doc/0.20.0/rpc/util/validateaddress/ 787 type ValidateAddressChainResult struct { 788 IsValid bool `json:"isvalid"` 789 Address string `json:"address,omitempty"` 790 IsScript *bool `json:"isscript,omitempty"` 791 IsWitness *bool `json:"iswitness,omitempty"` 792 WitnessVersion *int32 `json:"witness_version,omitempty"` 793 WitnessProgram *string `json:"witness_program,omitempty"` 794 } 795 796 // EstimateSmartFeeResult models the data returned buy the chain server 797 // estimatesmartfee command 798 type EstimateSmartFeeResult struct { 799 FeeRate *float64 `json:"feerate,omitempty"` 800 Errors []string `json:"errors,omitempty"` 801 Blocks int64 `json:"blocks"` 802 } 803 804 var _ json.Unmarshaler = &FundRawTransactionResult{} 805 806 type rawFundRawTransactionResult struct { 807 Transaction string `json:"hex"` 808 Fee float64 `json:"fee"` 809 ChangePosition int `json:"changepos"` 810 } 811 812 // FundRawTransactionResult is the result of the fundrawtransaction JSON-RPC call 813 type FundRawTransactionResult struct { 814 Transaction *wire.MsgTx 815 Fee btcutil.Amount 816 ChangePosition int // the position of the added change output, or -1 817 } 818 819 // UnmarshalJSON unmarshals the result of the fundrawtransaction JSON-RPC call 820 func (f *FundRawTransactionResult) UnmarshalJSON(data []byte) error { 821 var rawRes rawFundRawTransactionResult 822 if err := json.Unmarshal(data, &rawRes); err != nil { 823 return err 824 } 825 826 txBytes, err := hex.DecodeString(rawRes.Transaction) 827 if err != nil { 828 return err 829 } 830 831 var msgTx wire.MsgTx 832 witnessErr := msgTx.Deserialize(bytes.NewReader(txBytes)) 833 if witnessErr != nil { 834 legacyErr := msgTx.DeserializeNoWitness(bytes.NewReader(txBytes)) 835 if legacyErr != nil { 836 return legacyErr 837 } 838 } 839 840 fee, err := btcutil.NewAmount(rawRes.Fee) 841 if err != nil { 842 return err 843 } 844 845 f.Transaction = &msgTx 846 f.Fee = fee 847 f.ChangePosition = rawRes.ChangePosition 848 return nil 849 } 850 851 // GetDescriptorInfoResult models the data from the getdescriptorinfo command. 852 type GetDescriptorInfoResult struct { 853 Descriptor string `json:"descriptor"` // descriptor in canonical form, without private keys 854 Checksum string `json:"checksum"` // checksum for the input descriptor 855 IsRange bool `json:"isrange"` // whether the descriptor is ranged 856 IsSolvable bool `json:"issolvable"` // whether the descriptor is solvable 857 HasPrivateKeys bool `json:"hasprivatekeys"` // whether the descriptor has at least one private key 858 } 859 860 // DeriveAddressesResult models the data from the deriveaddresses command. 861 type DeriveAddressesResult []string 862 863 // LoadWalletResult models the data from the loadwallet command 864 type LoadWalletResult struct { 865 Name string `json:"name"` 866 Warning string `json:"warning"` 867 } 868 869 // DumpWalletResult models the data from the dumpwallet command 870 type DumpWalletResult struct { 871 Filename string `json:"filename"` 872 }