github.com/palcoin-project/palcd@v1.0.0/btcjson/chainsvrcmds.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 // NOTE: This file is intended to house the RPC commands that are supported by 6 // a chain server. 7 8 package btcjson 9 10 import ( 11 "encoding/hex" 12 "encoding/json" 13 "fmt" 14 "reflect" 15 16 "github.com/palcoin-project/palcd/wire" 17 ) 18 19 // AddNodeSubCmd defines the type used in the addnode JSON-RPC command for the 20 // sub command field. 21 type AddNodeSubCmd string 22 23 const ( 24 // ANAdd indicates the specified host should be added as a persistent 25 // peer. 26 ANAdd AddNodeSubCmd = "add" 27 28 // ANRemove indicates the specified peer should be removed. 29 ANRemove AddNodeSubCmd = "remove" 30 31 // ANOneTry indicates the specified host should try to connect once, 32 // but it should not be made persistent. 33 ANOneTry AddNodeSubCmd = "onetry" 34 ) 35 36 // AddNodeCmd defines the addnode JSON-RPC command. 37 type AddNodeCmd struct { 38 Addr string 39 SubCmd AddNodeSubCmd `jsonrpcusage:"\"add|remove|onetry\""` 40 } 41 42 // NewAddNodeCmd returns a new instance which can be used to issue an addnode 43 // JSON-RPC command. 44 func NewAddNodeCmd(addr string, subCmd AddNodeSubCmd) *AddNodeCmd { 45 return &AddNodeCmd{ 46 Addr: addr, 47 SubCmd: subCmd, 48 } 49 } 50 51 // TransactionInput represents the inputs to a transaction. Specifically a 52 // transaction hash and output number pair. 53 type TransactionInput struct { 54 Txid string `json:"txid"` 55 Vout uint32 `json:"vout"` 56 } 57 58 // CreateRawTransactionCmd defines the createrawtransaction JSON-RPC command. 59 type CreateRawTransactionCmd struct { 60 Inputs []TransactionInput 61 Amounts map[string]float64 `jsonrpcusage:"{\"address\":amount,...}"` // In BTC 62 LockTime *int64 63 } 64 65 // NewCreateRawTransactionCmd returns a new instance which can be used to issue 66 // a createrawtransaction JSON-RPC command. 67 // 68 // Amounts are in BTC. Passing in nil and the empty slice as inputs is equivalent, 69 // both gets interpreted as the empty slice. 70 func NewCreateRawTransactionCmd(inputs []TransactionInput, amounts map[string]float64, 71 lockTime *int64) *CreateRawTransactionCmd { 72 // to make sure we're serializing this to the empty list and not null, we 73 // explicitly initialize the list 74 if inputs == nil { 75 inputs = []TransactionInput{} 76 } 77 return &CreateRawTransactionCmd{ 78 Inputs: inputs, 79 Amounts: amounts, 80 LockTime: lockTime, 81 } 82 } 83 84 // DecodeRawTransactionCmd defines the decoderawtransaction JSON-RPC command. 85 type DecodeRawTransactionCmd struct { 86 HexTx string 87 } 88 89 // NewDecodeRawTransactionCmd returns a new instance which can be used to issue 90 // a decoderawtransaction JSON-RPC command. 91 func NewDecodeRawTransactionCmd(hexTx string) *DecodeRawTransactionCmd { 92 return &DecodeRawTransactionCmd{ 93 HexTx: hexTx, 94 } 95 } 96 97 // DecodeScriptCmd defines the decodescript JSON-RPC command. 98 type DecodeScriptCmd struct { 99 HexScript string 100 } 101 102 // NewDecodeScriptCmd returns a new instance which can be used to issue a 103 // decodescript JSON-RPC command. 104 func NewDecodeScriptCmd(hexScript string) *DecodeScriptCmd { 105 return &DecodeScriptCmd{ 106 HexScript: hexScript, 107 } 108 } 109 110 // DeriveAddressesCmd defines the deriveaddresses JSON-RPC command. 111 type DeriveAddressesCmd struct { 112 Descriptor string 113 Range *DescriptorRange 114 } 115 116 // NewDeriveAddressesCmd returns a new instance which can be used to issue a 117 // deriveaddresses JSON-RPC command. 118 func NewDeriveAddressesCmd(descriptor string, descriptorRange *DescriptorRange) *DeriveAddressesCmd { 119 return &DeriveAddressesCmd{ 120 Descriptor: descriptor, 121 Range: descriptorRange, 122 } 123 } 124 125 // ChangeType defines the different output types to use for the change address 126 // of a transaction built by the node. 127 type ChangeType string 128 129 var ( 130 // ChangeTypeLegacy indicates a P2PKH change address type. 131 ChangeTypeLegacy ChangeType = "legacy" 132 // ChangeTypeP2SHSegWit indicates a P2WPKH-in-P2SH change address type. 133 ChangeTypeP2SHSegWit ChangeType = "p2sh-segwit" 134 // ChangeTypeBech32 indicates a P2WPKH change address type. 135 ChangeTypeBech32 ChangeType = "bech32" 136 ) 137 138 // FundRawTransactionOpts are the different options that can be passed to rawtransaction 139 type FundRawTransactionOpts struct { 140 ChangeAddress *string `json:"changeAddress,omitempty"` 141 ChangePosition *int `json:"changePosition,omitempty"` 142 ChangeType *ChangeType `json:"change_type,omitempty"` 143 IncludeWatching *bool `json:"includeWatching,omitempty"` 144 LockUnspents *bool `json:"lockUnspents,omitempty"` 145 FeeRate *float64 `json:"feeRate,omitempty"` // BTC/kB 146 SubtractFeeFromOutputs []int `json:"subtractFeeFromOutputs,omitempty"` 147 Replaceable *bool `json:"replaceable,omitempty"` 148 ConfTarget *int `json:"conf_target,omitempty"` 149 EstimateMode *EstimateSmartFeeMode `json:"estimate_mode,omitempty"` 150 } 151 152 // FundRawTransactionCmd defines the fundrawtransaction JSON-RPC command 153 type FundRawTransactionCmd struct { 154 HexTx string 155 Options FundRawTransactionOpts 156 IsWitness *bool 157 } 158 159 // NewFundRawTransactionCmd returns a new instance which can be used to issue 160 // a fundrawtransaction JSON-RPC command 161 func NewFundRawTransactionCmd(serializedTx []byte, opts FundRawTransactionOpts, isWitness *bool) *FundRawTransactionCmd { 162 return &FundRawTransactionCmd{ 163 HexTx: hex.EncodeToString(serializedTx), 164 Options: opts, 165 IsWitness: isWitness, 166 } 167 } 168 169 // GetAddedNodeInfoCmd defines the getaddednodeinfo JSON-RPC command. 170 type GetAddedNodeInfoCmd struct { 171 DNS bool 172 Node *string 173 } 174 175 // NewGetAddedNodeInfoCmd returns a new instance which can be used to issue a 176 // getaddednodeinfo JSON-RPC command. 177 // 178 // The parameters which are pointers indicate they are optional. Passing nil 179 // for optional parameters will use the default value. 180 func NewGetAddedNodeInfoCmd(dns bool, node *string) *GetAddedNodeInfoCmd { 181 return &GetAddedNodeInfoCmd{ 182 DNS: dns, 183 Node: node, 184 } 185 } 186 187 // GetBestBlockHashCmd defines the getbestblockhash JSON-RPC command. 188 type GetBestBlockHashCmd struct{} 189 190 // NewGetBestBlockHashCmd returns a new instance which can be used to issue a 191 // getbestblockhash JSON-RPC command. 192 func NewGetBestBlockHashCmd() *GetBestBlockHashCmd { 193 return &GetBestBlockHashCmd{} 194 } 195 196 // GetBlockCmd defines the getblock JSON-RPC command. 197 type GetBlockCmd struct { 198 Hash string 199 Verbosity *int `jsonrpcdefault:"1"` 200 } 201 202 // NewGetBlockCmd returns a new instance which can be used to issue a getblock 203 // JSON-RPC command. 204 // 205 // The parameters which are pointers indicate they are optional. Passing nil 206 // for optional parameters will use the default value. 207 func NewGetBlockCmd(hash string, verbosity *int) *GetBlockCmd { 208 return &GetBlockCmd{ 209 Hash: hash, 210 Verbosity: verbosity, 211 } 212 } 213 214 // GetBlockChainInfoCmd defines the getblockchaininfo JSON-RPC command. 215 type GetBlockChainInfoCmd struct{} 216 217 // NewGetBlockChainInfoCmd returns a new instance which can be used to issue a 218 // getblockchaininfo JSON-RPC command. 219 func NewGetBlockChainInfoCmd() *GetBlockChainInfoCmd { 220 return &GetBlockChainInfoCmd{} 221 } 222 223 // GetBlockCountCmd defines the getblockcount JSON-RPC command. 224 type GetBlockCountCmd struct{} 225 226 // NewGetBlockCountCmd returns a new instance which can be used to issue a 227 // getblockcount JSON-RPC command. 228 func NewGetBlockCountCmd() *GetBlockCountCmd { 229 return &GetBlockCountCmd{} 230 } 231 232 // FilterTypeName defines the type used in the getblockfilter JSON-RPC command for the 233 // filter type field. 234 type FilterTypeName string 235 236 const ( 237 // FilterTypeBasic is the basic filter type defined in BIP0158. 238 FilterTypeBasic FilterTypeName = "basic" 239 ) 240 241 // GetBlockFilterCmd defines the getblockfilter JSON-RPC command. 242 type GetBlockFilterCmd struct { 243 BlockHash string // The hash of the block 244 FilterType *FilterTypeName // The type name of the filter, default=basic 245 } 246 247 // NewGetBlockFilterCmd returns a new instance which can be used to issue a 248 // getblockfilter JSON-RPC command. 249 // 250 // The parameters which are pointers indicate they are optional. Passing nil 251 // for optional parameters will use the default value. 252 func NewGetBlockFilterCmd(blockHash string, filterType *FilterTypeName) *GetBlockFilterCmd { 253 return &GetBlockFilterCmd{ 254 BlockHash: blockHash, 255 FilterType: filterType, 256 } 257 } 258 259 // GetBlockHashCmd defines the getblockhash JSON-RPC command. 260 type GetBlockHashCmd struct { 261 Index int64 262 } 263 264 // NewGetBlockHashCmd returns a new instance which can be used to issue a 265 // getblockhash JSON-RPC command. 266 func NewGetBlockHashCmd(index int64) *GetBlockHashCmd { 267 return &GetBlockHashCmd{ 268 Index: index, 269 } 270 } 271 272 // GetBlockHeaderCmd defines the getblockheader JSON-RPC command. 273 type GetBlockHeaderCmd struct { 274 Hash string 275 Verbose *bool `jsonrpcdefault:"true"` 276 } 277 278 // NewGetBlockHeaderCmd returns a new instance which can be used to issue a 279 // getblockheader JSON-RPC command. 280 func NewGetBlockHeaderCmd(hash string, verbose *bool) *GetBlockHeaderCmd { 281 return &GetBlockHeaderCmd{ 282 Hash: hash, 283 Verbose: verbose, 284 } 285 } 286 287 // HashOrHeight defines a type that can be used as hash_or_height value in JSON-RPC commands. 288 type HashOrHeight struct { 289 Value interface{} 290 } 291 292 // MarshalJSON implements the json.Marshaler interface 293 func (h HashOrHeight) MarshalJSON() ([]byte, error) { 294 return json.Marshal(h.Value) 295 } 296 297 // UnmarshalJSON implements the json.Unmarshaler interface 298 func (h *HashOrHeight) UnmarshalJSON(data []byte) error { 299 var unmarshalled interface{} 300 if err := json.Unmarshal(data, &unmarshalled); err != nil { 301 return err 302 } 303 304 switch v := unmarshalled.(type) { 305 case float64: 306 h.Value = int(v) 307 case string: 308 h.Value = v 309 default: 310 return fmt.Errorf("invalid hash_or_height value: %v", unmarshalled) 311 } 312 313 return nil 314 } 315 316 // GetBlockStatsCmd defines the getblockstats JSON-RPC command. 317 type GetBlockStatsCmd struct { 318 HashOrHeight HashOrHeight 319 Stats *[]string 320 } 321 322 // NewGetBlockStatsCmd returns a new instance which can be used to issue a 323 // getblockstats JSON-RPC command. Either height or hash must be specified. 324 func NewGetBlockStatsCmd(hashOrHeight HashOrHeight, stats *[]string) *GetBlockStatsCmd { 325 return &GetBlockStatsCmd{ 326 HashOrHeight: hashOrHeight, 327 Stats: stats, 328 } 329 } 330 331 // TemplateRequest is a request object as defined in BIP22 332 // (https://en.bitcoin.it/wiki/BIP_0022), it is optionally provided as an 333 // pointer argument to GetBlockTemplateCmd. 334 type TemplateRequest struct { 335 Mode string `json:"mode,omitempty"` 336 Capabilities []string `json:"capabilities,omitempty"` 337 338 // Optional long polling. 339 LongPollID string `json:"longpollid,omitempty"` 340 341 // Optional template tweaking. SigOpLimit and SizeLimit can be int64 342 // or bool. 343 SigOpLimit interface{} `json:"sigoplimit,omitempty"` 344 SizeLimit interface{} `json:"sizelimit,omitempty"` 345 MaxVersion uint32 `json:"maxversion,omitempty"` 346 347 // Basic pool extension from BIP 0023. 348 Target string `json:"target,omitempty"` 349 350 // Block proposal from BIP 0023. Data is only provided when Mode is 351 // "proposal". 352 Data string `json:"data,omitempty"` 353 WorkID string `json:"workid,omitempty"` 354 355 // list of supported softfork deployments, by name 356 // Ref: https://en.bitcoin.it/wiki/BIP_0009#getblocktemplate_changes. 357 Rules []string `json:"rules,omitempty"` 358 } 359 360 // convertTemplateRequestField potentially converts the provided value as 361 // needed. 362 func convertTemplateRequestField(fieldName string, iface interface{}) (interface{}, error) { 363 switch val := iface.(type) { 364 case nil: 365 return nil, nil 366 case bool: 367 return val, nil 368 case float64: 369 if val == float64(int64(val)) { 370 return int64(val), nil 371 } 372 } 373 374 str := fmt.Sprintf("the %s field must be unspecified, a boolean, or "+ 375 "a 64-bit integer", fieldName) 376 return nil, makeError(ErrInvalidType, str) 377 } 378 379 // UnmarshalJSON provides a custom Unmarshal method for TemplateRequest. This 380 // is necessary because the SigOpLimit and SizeLimit fields can only be specific 381 // types. 382 func (t *TemplateRequest) UnmarshalJSON(data []byte) error { 383 type templateRequest TemplateRequest 384 385 request := (*templateRequest)(t) 386 if err := json.Unmarshal(data, &request); err != nil { 387 return err 388 } 389 390 // The SigOpLimit field can only be nil, bool, or int64. 391 val, err := convertTemplateRequestField("sigoplimit", request.SigOpLimit) 392 if err != nil { 393 return err 394 } 395 request.SigOpLimit = val 396 397 // The SizeLimit field can only be nil, bool, or int64. 398 val, err = convertTemplateRequestField("sizelimit", request.SizeLimit) 399 if err != nil { 400 return err 401 } 402 request.SizeLimit = val 403 404 return nil 405 } 406 407 // GetBlockTemplateCmd defines the getblocktemplate JSON-RPC command. 408 type GetBlockTemplateCmd struct { 409 Request *TemplateRequest 410 } 411 412 // NewGetBlockTemplateCmd returns a new instance which can be used to issue a 413 // getblocktemplate JSON-RPC command. 414 // 415 // The parameters which are pointers indicate they are optional. Passing nil 416 // for optional parameters will use the default value. 417 func NewGetBlockTemplateCmd(request *TemplateRequest) *GetBlockTemplateCmd { 418 return &GetBlockTemplateCmd{ 419 Request: request, 420 } 421 } 422 423 // GetCFilterCmd defines the getcfilter JSON-RPC command. 424 type GetCFilterCmd struct { 425 Hash string 426 FilterType wire.FilterType 427 } 428 429 // NewGetCFilterCmd returns a new instance which can be used to issue a 430 // getcfilter JSON-RPC command. 431 func NewGetCFilterCmd(hash string, filterType wire.FilterType) *GetCFilterCmd { 432 return &GetCFilterCmd{ 433 Hash: hash, 434 FilterType: filterType, 435 } 436 } 437 438 // GetCFilterHeaderCmd defines the getcfilterheader JSON-RPC command. 439 type GetCFilterHeaderCmd struct { 440 Hash string 441 FilterType wire.FilterType 442 } 443 444 // NewGetCFilterHeaderCmd returns a new instance which can be used to issue a 445 // getcfilterheader JSON-RPC command. 446 func NewGetCFilterHeaderCmd(hash string, 447 filterType wire.FilterType) *GetCFilterHeaderCmd { 448 return &GetCFilterHeaderCmd{ 449 Hash: hash, 450 FilterType: filterType, 451 } 452 } 453 454 // GetChainTipsCmd defines the getchaintips JSON-RPC command. 455 type GetChainTipsCmd struct{} 456 457 // NewGetChainTipsCmd returns a new instance which can be used to issue a 458 // getchaintips JSON-RPC command. 459 func NewGetChainTipsCmd() *GetChainTipsCmd { 460 return &GetChainTipsCmd{} 461 } 462 463 // GetChainTxStatsCmd defines the getchaintxstats JSON-RPC command. 464 type GetChainTxStatsCmd struct { 465 NBlocks *int32 466 BlockHash *string 467 } 468 469 // NewGetChainTxStatsCmd returns a new instance which can be used to issue a 470 // getchaintxstats JSON-RPC command. 471 // 472 // The parameters which are pointers indicate they are optional. Passing nil 473 // for optional parameters will use the default value. 474 func NewGetChainTxStatsCmd(nBlocks *int32, blockHash *string) *GetChainTxStatsCmd { 475 return &GetChainTxStatsCmd{ 476 NBlocks: nBlocks, 477 BlockHash: blockHash, 478 } 479 } 480 481 // GetConnectionCountCmd defines the getconnectioncount JSON-RPC command. 482 type GetConnectionCountCmd struct{} 483 484 // NewGetConnectionCountCmd returns a new instance which can be used to issue a 485 // getconnectioncount JSON-RPC command. 486 func NewGetConnectionCountCmd() *GetConnectionCountCmd { 487 return &GetConnectionCountCmd{} 488 } 489 490 // GetDescriptorInfoCmd defines the getdescriptorinfo JSON-RPC command. 491 type GetDescriptorInfoCmd struct { 492 Descriptor string 493 } 494 495 // NewGetDescriptorInfoCmd returns a new instance which can be used to issue a 496 // getdescriptorinfo JSON-RPC command. 497 func NewGetDescriptorInfoCmd(descriptor string) *GetDescriptorInfoCmd { 498 return &GetDescriptorInfoCmd{ 499 Descriptor: descriptor, 500 } 501 } 502 503 // GetDifficultyCmd defines the getdifficulty JSON-RPC command. 504 type GetDifficultyCmd struct{} 505 506 // NewGetDifficultyCmd returns a new instance which can be used to issue a 507 // getdifficulty JSON-RPC command. 508 func NewGetDifficultyCmd() *GetDifficultyCmd { 509 return &GetDifficultyCmd{} 510 } 511 512 // GetGenerateCmd defines the getgenerate JSON-RPC command. 513 type GetGenerateCmd struct{} 514 515 // NewGetGenerateCmd returns a new instance which can be used to issue a 516 // getgenerate JSON-RPC command. 517 func NewGetGenerateCmd() *GetGenerateCmd { 518 return &GetGenerateCmd{} 519 } 520 521 // GetHashesPerSecCmd defines the gethashespersec JSON-RPC command. 522 type GetHashesPerSecCmd struct{} 523 524 // NewGetHashesPerSecCmd returns a new instance which can be used to issue a 525 // gethashespersec JSON-RPC command. 526 func NewGetHashesPerSecCmd() *GetHashesPerSecCmd { 527 return &GetHashesPerSecCmd{} 528 } 529 530 // GetInfoCmd defines the getinfo JSON-RPC command. 531 type GetInfoCmd struct{} 532 533 // NewGetInfoCmd returns a new instance which can be used to issue a 534 // getinfo JSON-RPC command. 535 func NewGetInfoCmd() *GetInfoCmd { 536 return &GetInfoCmd{} 537 } 538 539 // GetMempoolEntryCmd defines the getmempoolentry JSON-RPC command. 540 type GetMempoolEntryCmd struct { 541 TxID string 542 } 543 544 // NewGetMempoolEntryCmd returns a new instance which can be used to issue a 545 // getmempoolentry JSON-RPC command. 546 func NewGetMempoolEntryCmd(txHash string) *GetMempoolEntryCmd { 547 return &GetMempoolEntryCmd{ 548 TxID: txHash, 549 } 550 } 551 552 // GetMempoolInfoCmd defines the getmempoolinfo JSON-RPC command. 553 type GetMempoolInfoCmd struct{} 554 555 // NewGetMempoolInfoCmd returns a new instance which can be used to issue a 556 // getmempool JSON-RPC command. 557 func NewGetMempoolInfoCmd() *GetMempoolInfoCmd { 558 return &GetMempoolInfoCmd{} 559 } 560 561 // GetMiningInfoCmd defines the getmininginfo JSON-RPC command. 562 type GetMiningInfoCmd struct{} 563 564 // NewGetMiningInfoCmd returns a new instance which can be used to issue a 565 // getmininginfo JSON-RPC command. 566 func NewGetMiningInfoCmd() *GetMiningInfoCmd { 567 return &GetMiningInfoCmd{} 568 } 569 570 // GetNetworkInfoCmd defines the getnetworkinfo JSON-RPC command. 571 type GetNetworkInfoCmd struct{} 572 573 // NewGetNetworkInfoCmd returns a new instance which can be used to issue a 574 // getnetworkinfo JSON-RPC command. 575 func NewGetNetworkInfoCmd() *GetNetworkInfoCmd { 576 return &GetNetworkInfoCmd{} 577 } 578 579 // GetNetTotalsCmd defines the getnettotals JSON-RPC command. 580 type GetNetTotalsCmd struct{} 581 582 // NewGetNetTotalsCmd returns a new instance which can be used to issue a 583 // getnettotals JSON-RPC command. 584 func NewGetNetTotalsCmd() *GetNetTotalsCmd { 585 return &GetNetTotalsCmd{} 586 } 587 588 // GetNetworkHashPSCmd defines the getnetworkhashps JSON-RPC command. 589 type GetNetworkHashPSCmd struct { 590 Blocks *int `jsonrpcdefault:"120"` 591 Height *int `jsonrpcdefault:"-1"` 592 } 593 594 // NewGetNetworkHashPSCmd returns a new instance which can be used to issue a 595 // getnetworkhashps JSON-RPC command. 596 // 597 // The parameters which are pointers indicate they are optional. Passing nil 598 // for optional parameters will use the default value. 599 func NewGetNetworkHashPSCmd(numBlocks, height *int) *GetNetworkHashPSCmd { 600 return &GetNetworkHashPSCmd{ 601 Blocks: numBlocks, 602 Height: height, 603 } 604 } 605 606 // GetNodeAddressesCmd defines the getnodeaddresses JSON-RPC command. 607 type GetNodeAddressesCmd struct { 608 Count *int32 `jsonrpcdefault:"1"` 609 } 610 611 // NewGetNodeAddressesCmd returns a new instance which can be used to issue a 612 // getnodeaddresses JSON-RPC command. 613 // 614 // The parameters which are pointers indicate they are optional. Passing nil 615 // for optional parameters will use the default value. 616 func NewGetNodeAddressesCmd(count *int32) *GetNodeAddressesCmd { 617 return &GetNodeAddressesCmd{ 618 Count: count, 619 } 620 } 621 622 // GetPeerInfoCmd defines the getpeerinfo JSON-RPC command. 623 type GetPeerInfoCmd struct{} 624 625 // NewGetPeerInfoCmd returns a new instance which can be used to issue a getpeer 626 // JSON-RPC command. 627 func NewGetPeerInfoCmd() *GetPeerInfoCmd { 628 return &GetPeerInfoCmd{} 629 } 630 631 // GetRawMempoolCmd defines the getmempool JSON-RPC command. 632 type GetRawMempoolCmd struct { 633 Verbose *bool `jsonrpcdefault:"false"` 634 } 635 636 // NewGetRawMempoolCmd returns a new instance which can be used to issue a 637 // getrawmempool JSON-RPC command. 638 // 639 // The parameters which are pointers indicate they are optional. Passing nil 640 // for optional parameters will use the default value. 641 func NewGetRawMempoolCmd(verbose *bool) *GetRawMempoolCmd { 642 return &GetRawMempoolCmd{ 643 Verbose: verbose, 644 } 645 } 646 647 // GetRawTransactionCmd defines the getrawtransaction JSON-RPC command. 648 // 649 // NOTE: This field is an int versus a bool to remain compatible with Bitcoin 650 // Core even though it really should be a bool. 651 type GetRawTransactionCmd struct { 652 Txid string 653 Verbose *int `jsonrpcdefault:"0"` 654 } 655 656 // NewGetRawTransactionCmd returns a new instance which can be used to issue a 657 // getrawtransaction JSON-RPC command. 658 // 659 // The parameters which are pointers indicate they are optional. Passing nil 660 // for optional parameters will use the default value. 661 func NewGetRawTransactionCmd(txHash string, verbose *int) *GetRawTransactionCmd { 662 return &GetRawTransactionCmd{ 663 Txid: txHash, 664 Verbose: verbose, 665 } 666 } 667 668 // GetTxOutCmd defines the gettxout JSON-RPC command. 669 type GetTxOutCmd struct { 670 Txid string 671 Vout uint32 672 IncludeMempool *bool `jsonrpcdefault:"true"` 673 } 674 675 // NewGetTxOutCmd returns a new instance which can be used to issue a gettxout 676 // JSON-RPC command. 677 // 678 // The parameters which are pointers indicate they are optional. Passing nil 679 // for optional parameters will use the default value. 680 func NewGetTxOutCmd(txHash string, vout uint32, includeMempool *bool) *GetTxOutCmd { 681 return &GetTxOutCmd{ 682 Txid: txHash, 683 Vout: vout, 684 IncludeMempool: includeMempool, 685 } 686 } 687 688 // GetTxOutProofCmd defines the gettxoutproof JSON-RPC command. 689 type GetTxOutProofCmd struct { 690 TxIDs []string 691 BlockHash *string 692 } 693 694 // NewGetTxOutProofCmd returns a new instance which can be used to issue a 695 // gettxoutproof JSON-RPC command. 696 // 697 // The parameters which are pointers indicate they are optional. Passing nil 698 // for optional parameters will use the default value. 699 func NewGetTxOutProofCmd(txIDs []string, blockHash *string) *GetTxOutProofCmd { 700 return &GetTxOutProofCmd{ 701 TxIDs: txIDs, 702 BlockHash: blockHash, 703 } 704 } 705 706 // GetTxOutSetInfoCmd defines the gettxoutsetinfo JSON-RPC command. 707 type GetTxOutSetInfoCmd struct{} 708 709 // NewGetTxOutSetInfoCmd returns a new instance which can be used to issue a 710 // gettxoutsetinfo JSON-RPC command. 711 func NewGetTxOutSetInfoCmd() *GetTxOutSetInfoCmd { 712 return &GetTxOutSetInfoCmd{} 713 } 714 715 // GetWorkCmd defines the getwork JSON-RPC command. 716 type GetWorkCmd struct { 717 Data *string 718 } 719 720 // NewGetWorkCmd returns a new instance which can be used to issue a getwork 721 // JSON-RPC command. 722 // 723 // The parameters which are pointers indicate they are optional. Passing nil 724 // for optional parameters will use the default value. 725 func NewGetWorkCmd(data *string) *GetWorkCmd { 726 return &GetWorkCmd{ 727 Data: data, 728 } 729 } 730 731 // HelpCmd defines the help JSON-RPC command. 732 type HelpCmd struct { 733 Command *string 734 } 735 736 // NewHelpCmd returns a new instance which can be used to issue a help JSON-RPC 737 // command. 738 // 739 // The parameters which are pointers indicate they are optional. Passing nil 740 // for optional parameters will use the default value. 741 func NewHelpCmd(command *string) *HelpCmd { 742 return &HelpCmd{ 743 Command: command, 744 } 745 } 746 747 // InvalidateBlockCmd defines the invalidateblock JSON-RPC command. 748 type InvalidateBlockCmd struct { 749 BlockHash string 750 } 751 752 // NewInvalidateBlockCmd returns a new instance which can be used to issue a 753 // invalidateblock JSON-RPC command. 754 func NewInvalidateBlockCmd(blockHash string) *InvalidateBlockCmd { 755 return &InvalidateBlockCmd{ 756 BlockHash: blockHash, 757 } 758 } 759 760 // PingCmd defines the ping JSON-RPC command. 761 type PingCmd struct{} 762 763 // NewPingCmd returns a new instance which can be used to issue a ping JSON-RPC 764 // command. 765 func NewPingCmd() *PingCmd { 766 return &PingCmd{} 767 } 768 769 // PreciousBlockCmd defines the preciousblock JSON-RPC command. 770 type PreciousBlockCmd struct { 771 BlockHash string 772 } 773 774 // NewPreciousBlockCmd returns a new instance which can be used to issue a 775 // preciousblock JSON-RPC command. 776 func NewPreciousBlockCmd(blockHash string) *PreciousBlockCmd { 777 return &PreciousBlockCmd{ 778 BlockHash: blockHash, 779 } 780 } 781 782 // ReconsiderBlockCmd defines the reconsiderblock JSON-RPC command. 783 type ReconsiderBlockCmd struct { 784 BlockHash string 785 } 786 787 // NewReconsiderBlockCmd returns a new instance which can be used to issue a 788 // reconsiderblock JSON-RPC command. 789 func NewReconsiderBlockCmd(blockHash string) *ReconsiderBlockCmd { 790 return &ReconsiderBlockCmd{ 791 BlockHash: blockHash, 792 } 793 } 794 795 // SearchRawTransactionsCmd defines the searchrawtransactions JSON-RPC command. 796 type SearchRawTransactionsCmd struct { 797 Address string 798 Verbose *int `jsonrpcdefault:"1"` 799 Skip *int `jsonrpcdefault:"0"` 800 Count *int `jsonrpcdefault:"100"` 801 VinExtra *int `jsonrpcdefault:"0"` 802 Reverse *bool `jsonrpcdefault:"false"` 803 FilterAddrs *[]string 804 } 805 806 // NewSearchRawTransactionsCmd returns a new instance which can be used to issue a 807 // sendrawtransaction JSON-RPC command. 808 // 809 // The parameters which are pointers indicate they are optional. Passing nil 810 // for optional parameters will use the default value. 811 func NewSearchRawTransactionsCmd(address string, verbose, skip, count *int, vinExtra *int, reverse *bool, filterAddrs *[]string) *SearchRawTransactionsCmd { 812 return &SearchRawTransactionsCmd{ 813 Address: address, 814 Verbose: verbose, 815 Skip: skip, 816 Count: count, 817 VinExtra: vinExtra, 818 Reverse: reverse, 819 FilterAddrs: filterAddrs, 820 } 821 } 822 823 // AllowHighFeesOrMaxFeeRate defines a type that can either be the legacy 824 // allowhighfees boolean field or the new maxfeerate int field. 825 type AllowHighFeesOrMaxFeeRate struct { 826 Value interface{} 827 } 828 829 // String returns the string representation of this struct, used for printing 830 // the marshaled default value in the help text. 831 func (a AllowHighFeesOrMaxFeeRate) String() string { 832 b, _ := a.MarshalJSON() 833 return string(b) 834 } 835 836 // MarshalJSON implements the json.Marshaler interface 837 func (a AllowHighFeesOrMaxFeeRate) MarshalJSON() ([]byte, error) { 838 // The default value is false which only works with the legacy versions. 839 if a.Value == nil || 840 (reflect.ValueOf(a.Value).Kind() == reflect.Ptr && 841 reflect.ValueOf(a.Value).IsNil()) { 842 843 return json.Marshal(false) 844 } 845 846 return json.Marshal(a.Value) 847 } 848 849 // UnmarshalJSON implements the json.Unmarshaler interface 850 func (a *AllowHighFeesOrMaxFeeRate) UnmarshalJSON(data []byte) error { 851 if len(data) == 0 { 852 return nil 853 } 854 855 var unmarshalled interface{} 856 if err := json.Unmarshal(data, &unmarshalled); err != nil { 857 return err 858 } 859 860 switch v := unmarshalled.(type) { 861 case bool: 862 a.Value = Bool(v) 863 case float64: 864 a.Value = Int32(int32(v)) 865 default: 866 return fmt.Errorf("invalid allowhighfees or maxfeerate value: "+ 867 "%v", unmarshalled) 868 } 869 870 return nil 871 } 872 873 // SendRawTransactionCmd defines the sendrawtransaction JSON-RPC command. 874 type SendRawTransactionCmd struct { 875 HexTx string 876 FeeSetting *AllowHighFeesOrMaxFeeRate `jsonrpcdefault:"false"` 877 } 878 879 // NewSendRawTransactionCmd returns a new instance which can be used to issue a 880 // sendrawtransaction JSON-RPC command. 881 // 882 // The parameters which are pointers indicate they are optional. Passing nil 883 // for optional parameters will use the default value. 884 func NewSendRawTransactionCmd(hexTx string, allowHighFees *bool) *SendRawTransactionCmd { 885 return &SendRawTransactionCmd{ 886 HexTx: hexTx, 887 FeeSetting: &AllowHighFeesOrMaxFeeRate{ 888 Value: allowHighFees, 889 }, 890 } 891 } 892 893 // NewSendRawTransactionCmd returns a new instance which can be used to issue a 894 // sendrawtransaction JSON-RPC command to a bitcoind node. 895 // 896 // A 0 maxFeeRate indicates that a maximum fee rate won't be enforced. 897 func NewBitcoindSendRawTransactionCmd(hexTx string, maxFeeRate int32) *SendRawTransactionCmd { 898 return &SendRawTransactionCmd{ 899 HexTx: hexTx, 900 FeeSetting: &AllowHighFeesOrMaxFeeRate{ 901 Value: &maxFeeRate, 902 }, 903 } 904 } 905 906 // SetGenerateCmd defines the setgenerate JSON-RPC command. 907 type SetGenerateCmd struct { 908 Generate bool 909 GenProcLimit *int `jsonrpcdefault:"-1"` 910 } 911 912 // NewSetGenerateCmd returns a new instance which can be used to issue a 913 // setgenerate JSON-RPC command. 914 // 915 // The parameters which are pointers indicate they are optional. Passing nil 916 // for optional parameters will use the default value. 917 func NewSetGenerateCmd(generate bool, genProcLimit *int) *SetGenerateCmd { 918 return &SetGenerateCmd{ 919 Generate: generate, 920 GenProcLimit: genProcLimit, 921 } 922 } 923 924 // SignMessageWithPrivKeyCmd defines the signmessagewithprivkey JSON-RPC command. 925 type SignMessageWithPrivKeyCmd struct { 926 PrivKey string // base 58 Wallet Import format private key 927 Message string // Message to sign 928 } 929 930 // NewSignMessageWithPrivKey returns a new instance which can be used to issue a 931 // signmessagewithprivkey JSON-RPC command. 932 // 933 // The first parameter is a private key in base 58 Wallet Import format. 934 // The second parameter is the message to sign. 935 func NewSignMessageWithPrivKey(privKey, message string) *SignMessageWithPrivKeyCmd { 936 return &SignMessageWithPrivKeyCmd{ 937 PrivKey: privKey, 938 Message: message, 939 } 940 } 941 942 // StopCmd defines the stop JSON-RPC command. 943 type StopCmd struct{} 944 945 // NewStopCmd returns a new instance which can be used to issue a stop JSON-RPC 946 // command. 947 func NewStopCmd() *StopCmd { 948 return &StopCmd{} 949 } 950 951 // SubmitBlockOptions represents the optional options struct provided with a 952 // SubmitBlockCmd command. 953 type SubmitBlockOptions struct { 954 // must be provided if server provided a workid with template. 955 WorkID string `json:"workid,omitempty"` 956 } 957 958 // SubmitBlockCmd defines the submitblock JSON-RPC command. 959 type SubmitBlockCmd struct { 960 HexBlock string 961 Options *SubmitBlockOptions 962 } 963 964 // NewSubmitBlockCmd returns a new instance which can be used to issue a 965 // submitblock JSON-RPC command. 966 // 967 // The parameters which are pointers indicate they are optional. Passing nil 968 // for optional parameters will use the default value. 969 func NewSubmitBlockCmd(hexBlock string, options *SubmitBlockOptions) *SubmitBlockCmd { 970 return &SubmitBlockCmd{ 971 HexBlock: hexBlock, 972 Options: options, 973 } 974 } 975 976 // UptimeCmd defines the uptime JSON-RPC command. 977 type UptimeCmd struct{} 978 979 // NewUptimeCmd returns a new instance which can be used to issue an uptime JSON-RPC command. 980 func NewUptimeCmd() *UptimeCmd { 981 return &UptimeCmd{} 982 } 983 984 // ValidateAddressCmd defines the validateaddress JSON-RPC command. 985 type ValidateAddressCmd struct { 986 Address string 987 } 988 989 // NewValidateAddressCmd returns a new instance which can be used to issue a 990 // validateaddress JSON-RPC command. 991 func NewValidateAddressCmd(address string) *ValidateAddressCmd { 992 return &ValidateAddressCmd{ 993 Address: address, 994 } 995 } 996 997 // VerifyChainCmd defines the verifychain JSON-RPC command. 998 type VerifyChainCmd struct { 999 CheckLevel *int32 `jsonrpcdefault:"3"` 1000 CheckDepth *int32 `jsonrpcdefault:"288"` // 0 = all 1001 } 1002 1003 // NewVerifyChainCmd returns a new instance which can be used to issue a 1004 // verifychain JSON-RPC command. 1005 // 1006 // The parameters which are pointers indicate they are optional. Passing nil 1007 // for optional parameters will use the default value. 1008 func NewVerifyChainCmd(checkLevel, checkDepth *int32) *VerifyChainCmd { 1009 return &VerifyChainCmd{ 1010 CheckLevel: checkLevel, 1011 CheckDepth: checkDepth, 1012 } 1013 } 1014 1015 // VerifyMessageCmd defines the verifymessage JSON-RPC command. 1016 type VerifyMessageCmd struct { 1017 Address string 1018 Signature string 1019 Message string 1020 } 1021 1022 // NewVerifyMessageCmd returns a new instance which can be used to issue a 1023 // verifymessage JSON-RPC command. 1024 func NewVerifyMessageCmd(address, signature, message string) *VerifyMessageCmd { 1025 return &VerifyMessageCmd{ 1026 Address: address, 1027 Signature: signature, 1028 Message: message, 1029 } 1030 } 1031 1032 // VerifyTxOutProofCmd defines the verifytxoutproof JSON-RPC command. 1033 type VerifyTxOutProofCmd struct { 1034 Proof string 1035 } 1036 1037 // NewVerifyTxOutProofCmd returns a new instance which can be used to issue a 1038 // verifytxoutproof JSON-RPC command. 1039 func NewVerifyTxOutProofCmd(proof string) *VerifyTxOutProofCmd { 1040 return &VerifyTxOutProofCmd{ 1041 Proof: proof, 1042 } 1043 } 1044 1045 func init() { 1046 // No special flags for commands in this file. 1047 flags := UsageFlag(0) 1048 1049 MustRegisterCmd("addnode", (*AddNodeCmd)(nil), flags) 1050 MustRegisterCmd("createrawtransaction", (*CreateRawTransactionCmd)(nil), flags) 1051 MustRegisterCmd("decoderawtransaction", (*DecodeRawTransactionCmd)(nil), flags) 1052 MustRegisterCmd("decodescript", (*DecodeScriptCmd)(nil), flags) 1053 MustRegisterCmd("deriveaddresses", (*DeriveAddressesCmd)(nil), flags) 1054 MustRegisterCmd("fundrawtransaction", (*FundRawTransactionCmd)(nil), flags) 1055 MustRegisterCmd("getaddednodeinfo", (*GetAddedNodeInfoCmd)(nil), flags) 1056 MustRegisterCmd("getbestblockhash", (*GetBestBlockHashCmd)(nil), flags) 1057 MustRegisterCmd("getblock", (*GetBlockCmd)(nil), flags) 1058 MustRegisterCmd("getblockchaininfo", (*GetBlockChainInfoCmd)(nil), flags) 1059 MustRegisterCmd("getblockcount", (*GetBlockCountCmd)(nil), flags) 1060 MustRegisterCmd("getblockfilter", (*GetBlockFilterCmd)(nil), flags) 1061 MustRegisterCmd("getblockhash", (*GetBlockHashCmd)(nil), flags) 1062 MustRegisterCmd("getblockheader", (*GetBlockHeaderCmd)(nil), flags) 1063 MustRegisterCmd("getblockstats", (*GetBlockStatsCmd)(nil), flags) 1064 MustRegisterCmd("getblocktemplate", (*GetBlockTemplateCmd)(nil), flags) 1065 MustRegisterCmd("getcfilter", (*GetCFilterCmd)(nil), flags) 1066 MustRegisterCmd("getcfilterheader", (*GetCFilterHeaderCmd)(nil), flags) 1067 MustRegisterCmd("getchaintips", (*GetChainTipsCmd)(nil), flags) 1068 MustRegisterCmd("getchaintxstats", (*GetChainTxStatsCmd)(nil), flags) 1069 MustRegisterCmd("getconnectioncount", (*GetConnectionCountCmd)(nil), flags) 1070 MustRegisterCmd("getdescriptorinfo", (*GetDescriptorInfoCmd)(nil), flags) 1071 MustRegisterCmd("getdifficulty", (*GetDifficultyCmd)(nil), flags) 1072 MustRegisterCmd("getgenerate", (*GetGenerateCmd)(nil), flags) 1073 MustRegisterCmd("gethashespersec", (*GetHashesPerSecCmd)(nil), flags) 1074 MustRegisterCmd("getinfo", (*GetInfoCmd)(nil), flags) 1075 MustRegisterCmd("getmempoolentry", (*GetMempoolEntryCmd)(nil), flags) 1076 MustRegisterCmd("getmempoolinfo", (*GetMempoolInfoCmd)(nil), flags) 1077 MustRegisterCmd("getmininginfo", (*GetMiningInfoCmd)(nil), flags) 1078 MustRegisterCmd("getnetworkinfo", (*GetNetworkInfoCmd)(nil), flags) 1079 MustRegisterCmd("getnettotals", (*GetNetTotalsCmd)(nil), flags) 1080 MustRegisterCmd("getnetworkhashps", (*GetNetworkHashPSCmd)(nil), flags) 1081 MustRegisterCmd("getnodeaddresses", (*GetNodeAddressesCmd)(nil), flags) 1082 MustRegisterCmd("getpeerinfo", (*GetPeerInfoCmd)(nil), flags) 1083 MustRegisterCmd("getrawmempool", (*GetRawMempoolCmd)(nil), flags) 1084 MustRegisterCmd("getrawtransaction", (*GetRawTransactionCmd)(nil), flags) 1085 MustRegisterCmd("gettxout", (*GetTxOutCmd)(nil), flags) 1086 MustRegisterCmd("gettxoutproof", (*GetTxOutProofCmd)(nil), flags) 1087 MustRegisterCmd("gettxoutsetinfo", (*GetTxOutSetInfoCmd)(nil), flags) 1088 MustRegisterCmd("getwork", (*GetWorkCmd)(nil), flags) 1089 MustRegisterCmd("help", (*HelpCmd)(nil), flags) 1090 MustRegisterCmd("invalidateblock", (*InvalidateBlockCmd)(nil), flags) 1091 MustRegisterCmd("ping", (*PingCmd)(nil), flags) 1092 MustRegisterCmd("preciousblock", (*PreciousBlockCmd)(nil), flags) 1093 MustRegisterCmd("reconsiderblock", (*ReconsiderBlockCmd)(nil), flags) 1094 MustRegisterCmd("searchrawtransactions", (*SearchRawTransactionsCmd)(nil), flags) 1095 MustRegisterCmd("sendrawtransaction", (*SendRawTransactionCmd)(nil), flags) 1096 MustRegisterCmd("setgenerate", (*SetGenerateCmd)(nil), flags) 1097 MustRegisterCmd("signmessagewithprivkey", (*SignMessageWithPrivKeyCmd)(nil), flags) 1098 MustRegisterCmd("stop", (*StopCmd)(nil), flags) 1099 MustRegisterCmd("submitblock", (*SubmitBlockCmd)(nil), flags) 1100 MustRegisterCmd("uptime", (*UptimeCmd)(nil), flags) 1101 MustRegisterCmd("validateaddress", (*ValidateAddressCmd)(nil), flags) 1102 MustRegisterCmd("verifychain", (*VerifyChainCmd)(nil), flags) 1103 MustRegisterCmd("verifymessage", (*VerifyMessageCmd)(nil), flags) 1104 MustRegisterCmd("verifytxoutproof", (*VerifyTxOutProofCmd)(nil), flags) 1105 }