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