// SPDX-License-Identifier: CC0-1.0

//! The JSON-RPC API for Bitcoin Core `v0.17` - util.
//!
//! Types for methods found under the `== Util ==` section of the API docs.

mod error;
mod into;

use serde::{Deserialize, Serialize};

#[rustfmt::skip]                // Keep public re-exports separate.
pub use self::error::{CreateMultisigError, ValidateAddressError};

/// Result of JSON-RPC method `createmultisig`.
///
/// > createmultisig nrequired ["key",...] ( "address_type" )
/// >
/// > Creates a multi-signature address with n signature of m keys required.
/// > It returns a json object with the address and redeemScript.
/// >
/// > Arguments:
/// > 1. nrequired                    (numeric, required) The number of required signatures out of the n keys.
/// > 2. "keys"                       (string, required) A json array of hex-encoded public keys
/// >      [
/// >        "key"                    (string) The hex-encoded public key
/// >        ,...
/// >      ]
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct CreateMultisig {
    /// The value of the new multisig address.
    pub address: String,
    /// The string value of the hex-encoded redemption script.
    #[serde(rename = "redeemScript")]
    pub redeem_script: String,
}

/// Result of JSON-RPC method `estimatesmartfee`.
///
/// > estimatesmartfee conf_target ("estimate_mode")
/// >
/// > Estimates the approximate fee per kilobyte needed for a transaction to begin
/// > confirmation within conf_target blocks if possible and return the number of blocks
/// > for which the estimate is valid. Uses virtual transaction size as defined
/// > in BIP 141 (witness data is discounted).
/// >
/// > Arguments:
/// > 1. conf_target     (numeric) Confirmation target in blocks (1 - 1008)
/// > 2. "estimate_mode" (string, optional, default=CONSERVATIVE) The fee estimate mode.
/// >                    Whether to return a more conservative estimate which also satisfies
/// >                    a longer history. A conservative estimate potentially returns a
/// >                    higher feerate and is more likely to be sufficient for the desired
/// >                    target, but is not as responsive to short term drops in the
/// >                    prevailing fee market.  Must be one of:
/// >        "UNSET" (defaults to CONSERVATIVE)
/// >        "ECONOMICAL"
/// >        "CONSERVATIVE"
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct EstimateSmartFee {
    /// Estimate fee rate in BTC/kB.
    #[serde(rename = "feerate")]
    pub fee_rate: Option<f64>,
    /// Errors encountered during processing.
    pub errors: Option<Vec<String>>,
    /// Block number where estimate was found.
    pub blocks: u32,
}

/// Result of JSON-RPC method `signmessagewithprivkey`.
///
/// > signmessagewithprivkey "privkey" "message"
/// >
/// > Sign a message with the private key of an address
/// >
/// > Arguments:
/// > 1. "privkey"         (string, required) The private key to sign the message with.
/// > 2. "message"         (string, required) The message to create a signature of.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct SignMessageWithPrivKey(pub String);

/// Result of JSON-RPC method `validateaddress`.
///
/// > validateaddress "address"
/// >
/// > Return information about the given bitcoin address.
/// > DEPRECATION WARNING: Parts of this command have been deprecated and moved to getaddressinfo. Clients must
/// > transition to using getaddressinfo to access this information before upgrading to v0.18. The following deprecated
/// > fields have moved to getaddressinfo and will only be shown here with -deprecatedrpc=validateaddress: ismine, iswatchonly,
/// > script, hex, pubkeys, sigsrequired, pubkey, addresses, embedded, iscompressed, account, timestamp, hdkeypath, kdmasterkeyid.
/// >
/// > Arguments:
/// > 1. "address"                    (string, required) The bitcoin address to validate
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct ValidateAddress {
    /// If the address is valid or not. If not, this is the only property returned.
    #[serde(rename = "isvalid")]
    pub is_valid: bool,
    /// The bitcoin address validated.
    pub address: String,
    /// The hex encoded scriptPubKey generated by the address.
    #[serde(rename = "scriptPubKey")]
    pub script_pubkey: String,
    /// If the key is a script.
    #[serde(rename = "isscript")]
    pub is_script: bool,
    /// If the address is a witness address.
    #[serde(rename = "iswitness")]
    pub is_witness: bool,
    /// The version number of the witness program.
    pub witness_version: Option<i64>,
    /// The hex value of the witness program.
    pub witness_program: Option<String>,
}

/// Result of JSON-RPC method `verifymessage`.
///
/// > verifymessage "address" "signature" "message"
/// >
/// > Verify a signed message
/// >
/// > Arguments:
/// > 1. "address"         (string, required) The bitcoin address to use for the signature.
/// > 2. "signature"       (string, required) The signature provided by the signer in base 64 encoding (see signmessage).
/// > 3. "message"         (string, required) The message that was signed.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct VerifyMessage(pub bool);
