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