Documentation ¶
Index ¶
- Constants
- Variables
- func CheckRPCConfig(cfg *RPCConfig, name string, network dex.Network, ports NetPorts) error
- func DeepChild(root *hdkeychain.ExtendedKey, path []uint32) (*hdkeychain.ExtendedKey, error)
- func ExtractContractHash(scriptHex string) ([]byte, error)
- func ExtractPubKeyHash(script []byte) []byte
- func ExtractScriptHash(script []byte) []byte
- func ExtractSwapDetails(pkScript []byte, segwit bool, chainParams *chaincfg.Params) (sender, receiver btcutil.Address, lockTime uint64, secretHash []byte, ...)
- func FindKeyPush(witness [][]byte, sigScript, contractHash []byte, segwit bool, ...) ([]byte, error)
- func IsDust(txOut *wire.TxOut, minRelayTxFee uint64) bool
- func MakeContract(rAddr, sAddr btcutil.Address, secretHash []byte, lockTime int64, segwit bool, ...) ([]byte, error)
- func MsgTxVBytes(msgTx *wire.MsgTx) uint64
- func ParseKeyExtended(keyPart string) (key *hdkeychain.ExtendedKey, fingerprint, path string, isRange bool, err error)
- func ParsePath(p string) (path []uint32, isRange bool, err error)
- func ReadCloneParams(cloneParams *CloneParams) *chaincfg.Params
- func RedeemP2SHContract(contract, sig, pubkey, secret []byte) ([]byte, error)
- func RedeemP2WSHContract(contract, sig, pubkey, secret []byte) [][]byte
- func RefundP2SHContract(contract, sig, pubkey []byte) ([]byte, error)
- func RefundP2WSHContract(contract, sig, pubkey []byte) [][]byte
- func SystemConfigPath(asset string) string
- type AddressDecoder
- type AddressStringer
- type BTCScriptType
- type BtcScriptAddrs
- type CloneParams
- type Descriptor
- type KeyFmt
- type KeyOrigin
- type NetPorts
- type RPCConfig
- type SpendInfo
Constants ¶
const ( // SecretHashSize is the byte-length of the hash of the secret key used in an // atomic swap. SecretHashSize = 32 // SecretKeySize is the byte-length of the secret key used in an atomic swap. SecretKeySize = 32 // ContractHashSize is the size of the script-hash for an atomic swap // contract. ContractHashSize = 20 // PubKeyLength is the length of a serialized compressed public key. PubKeyLength = 33 // 4 bytes version + 4 bytes locktime + 2 bytes of varints for the number of // transaction inputs and outputs MinimumBlockOverHead = 10 // SwapContractSize is the worst case scenario size for a swap contract, // which is the pk-script of the non-change output of an initialization // transaction as used in execution of an atomic swap. // See ExtractSwapDetails for a breakdown of the bytes. SwapContractSize = 97 // DERSigLength is the maximum length of a DER encoded signature with a // sighash type byte. DERSigLength = 73 // RedeemSwapSigScriptSize is the worst case (largest) serialize size // of a transaction signature script that redeems atomic swap output contract. // It is calculated as: // // - OP_DATA_73 // - 72 bytes DER signature + 1 byte sighash // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_32 // - 32 bytes secret key // - OP_1 // - varint 97 // - 97 bytes redeem script RedeemSwapSigScriptSize = 1 + DERSigLength + 1 + 33 + 1 + 32 + 1 + 2 + 97 // RefundSigScriptSize is the worst case (largest) serialize size // of a transaction input script that refunds a compressed P2PKH output. // It is calculated as: // // - OP_DATA_73 // - 72 bytes DER signature + 1 byte sighash // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_0 // - varint 97 => OP_PUSHDATA1(0x4c) + 0x61 // - 97 bytes contract RefundSigScriptSize = 1 + DERSigLength + 1 + 33 + 1 + 2 + 97 // Overhead for a wire.TxIn. See wire.TxIn.SerializeSize. // hash 32 bytes + index 4 bytes + sequence 4 bytes. TxInOverhead = 32 + 4 + 4 // 40 // TxOutOverhead is the overhead associated with a transaction output. // 8 bytes value + at least 1 byte varint script size TxOutOverhead = 8 + 1 RedeemP2PKSigScriptSize = 1 + DERSigLength // RedeemP2PKHSigScriptSize is the worst case (largest) serialize size // of a transaction input script that redeems a compressed P2PKH output. // It is calculated as: // // - OP_DATA_73 // - 72 bytes DER signature + 1 byte sighash // - OP_DATA_33 // - 33 bytes serialized compressed pubkey RedeemP2PKHSigScriptSize = 1 + DERSigLength + 1 + 33 // 108 // P2PKHPkScriptSize is the size of a transaction output script that // pays to a compressed pubkey hash. It is calculated as: // // - OP_DUP // - OP_HASH160 // - OP_DATA_20 // - 20 bytes pubkey hash // - OP_EQUALVERIFY // - OP_CHECKSIG P2PKHPkScriptSize = 1 + 1 + 1 + 20 + 1 + 1 // 25 // P2PKHOutputSize is the size of the serialized P2PKH output. P2PKHOutputSize = TxOutOverhead + P2PKHPkScriptSize // 9 + 25 = 34 // P2SHPkScriptSize is the size of a transaction output script that // pays to a redeem script. It is calculated as: // // - OP_HASH160 // - OP_DATA_20 // - 20 bytes redeem script hash // - OP_EQUAL P2SHPkScriptSize = 1 + 1 + 20 + 1 // P2SHOutputSize is the size of the serialized P2SH output. P2SHOutputSize = TxOutOverhead + P2SHPkScriptSize // 9 + 23 = 32 // P2WSHPkScriptSize is the size of a segwit transaction output script that // pays to a redeem script. It is calculated as: // // - OP_0 // - OP_DATA_32 // - 32 bytes redeem script hash P2WSHPkScriptSize = 1 + 1 + 32 // P2WSHOutputSize is the size of the serialized P2WSH output. P2WSHOutputSize = TxOutOverhead + P2WSHPkScriptSize // 9 + 34 = 43 // RedeemP2PKHInputSize is the worst case (largest) serialize size of a // transaction input redeeming a compressed P2PKH output. It is // calculated as: // // - 32 bytes previous tx // - 4 bytes output index // - 4 bytes sequence // - 1 byte compact int encoding value 108 // - 108 bytes signature script RedeemP2PKHInputSize = TxInOverhead + 1 + RedeemP2PKHSigScriptSize // 40 + 1 + 108 = 149 // RedeemP2WPKHInputSize is the worst case size of a transaction // input redeeming a P2WPKH output. This does not account for witness data, // which is considered at a lower weight for fee calculations. It is // calculated as // // - 32 bytes previous tx // - 4 bytes output index // - 4 bytes sequence // - 1 byte encoding empty redeem script // - 0 bytes signature script RedeemP2WPKHInputSize = TxInOverhead + 1 // RedeemP2WPKHInputWitnessWeight is the worst case weight of // a witness for spending P2WPKH and nested P2WPKH outputs. It // is calculated as: // // - 1 wu compact int encoding value 2 (number of items) // - 1 wu compact int encoding value 73 // - 72 wu DER signature + 1 wu sighash // - 1 wu compact int encoding value 33 // - 33 wu serialized compressed pubkey // NOTE: witness data is not script. RedeemP2WPKHInputWitnessWeight = 1 + 1 + DERSigLength + 1 + 33 // 109 // RedeemP2WPKHInputTotalSize is the worst case size of a transaction // input redeeming a P2WPKH output and the corresponding witness data. // It is calculated as: // // 41 vbytes base tx input // 109wu witness = 28 vbytes // total = 69 vbytes RedeemP2WPKHInputTotalSize = RedeemP2WPKHInputSize + (RedeemP2WPKHInputWitnessWeight+(witnessWeight-1))/witnessWeight // SigwitMarkerAndFlagWeight is the 2 bytes of overhead witness data // added to every segwit transaction. SegwitMarkerAndFlagWeight = 2 // P2WPKHPkScriptSize is the size of a transaction output script that // pays to a witness pubkey hash. It is calculated as: // // - OP_0 // - OP_DATA_20 // - 20 bytes pubkey hash P2WPKHPkScriptSize = 1 + 1 + 20 // P2WPKHOutputSize is the serialize size of a transaction output with a // P2WPKH output script. It is calculated as: // // - 8 bytes output value // - 1 byte compact int encoding value 22 // - 22 bytes P2PKH output script P2WPKHOutputSize = TxOutOverhead + P2WPKHPkScriptSize // 31 // MinimumTxOverhead is the size of an empty transaction. // 4 bytes version + 4 bytes locktime + 2 bytes of varints for the number of // transaction inputs and outputs MinimumTxOverhead = 4 + 4 + 1 + 1 // 10 // InitTxSizeBase is the size of a standard serialized atomic swap // initialization transaction with one change output and no inputs. This is // MsgTx overhead + 1 P2PKH change output + 1 P2SH contract output. However, // the change output might be P2WPKH, in which case it would be smaller. InitTxSizeBase = MinimumTxOverhead + P2PKHOutputSize + P2SHOutputSize // 10 + 34 + 32 = 76 // InitTxSize is InitTxBaseSize + 1 P2PKH input InitTxSize = InitTxSizeBase + RedeemP2PKHInputSize // 76 + 149 = 225 // InitTxSizeBaseSegwit is the size of a standard serialized atomic swap // initialization transaction with one change output and no inputs. The // change output is assumed to be segwit. 10 + 31 + 43 = 84 InitTxSizeBaseSegwit = MinimumTxOverhead + P2WPKHOutputSize + P2WSHOutputSize // InitTxSizeSegwit is InitTxSizeSegwit + 1 P2WPKH input. // 84 vbytes base tx // 41 vbytes base tx input // 109wu witness + 2wu segwit marker and flag = 28 vbytes // total = 153 vbytes InitTxSizeSegwit = InitTxSizeBaseSegwit + RedeemP2WPKHInputSize + (SegwitMarkerAndFlagWeight+RedeemP2WPKHInputWitnessWeight+(witnessWeight-1))/witnessWeight )
const ( ScriptP2PKH = 1 << iota ScriptP2PK ScriptP2SH ScriptTypeSegwit ScriptMultiSig ScriptUnsupported )
Variables ¶
var ( // ErrMalformedDescriptor is returned when a provided descriptor string does // not match the expected format for a descriptor. ErrMalformedDescriptor = errors.New("malformed descriptor") )
var RPCPorts = NetPorts{
Mainnet: "8332",
Testnet: "18332",
Simnet: "18443",
}
RPCPorts are the default BTC ports.
var UnitInfo = dex.UnitInfo{ AtomicUnit: "Sats", Conventional: dex.Denomination{ Unit: "BTC", ConversionFactor: 1e8, }, Alternatives: []dex.Denomination{ { Unit: "mBTC", ConversionFactor: 1e5, }, { Unit: "µBTC", ConversionFactor: 1e2, }, }, }
Functions ¶
func CheckRPCConfig ¶ added in v0.4.0
func DeepChild ¶ added in v0.5.0
func DeepChild(root *hdkeychain.ExtendedKey, path []uint32) (*hdkeychain.ExtendedKey, error)
DeepChild derives a new extended key from the provided root extended key and derivation path. This is useful given a Descriptor.Key of type KeyExtended that may be decoded into and extended key and path with ParseKeyExtended. Given an address referencing the extended key via its fingerprint (also returned by ParseKeyExtended) and derivation path, the private key for that address may be derived given the child index of the address.
func ExtractContractHash ¶
ExtractContractHash extracts the contract P2SH or P2WSH address from a pkScript. A non-nil error is returned if the hexadecimal encoding of the pkScript is invalid, or if the pkScript is not a P2SH or P2WSH script.
func ExtractPubKeyHash ¶
ExtractPubKeyHash extracts the pubkey hash from the passed script if it is a standard pay-to-pubkey-hash script. It will return nil otherwise.
func ExtractScriptHash ¶
ExtractScriptHash attempts to extract the redeem script hash from a pkScript. If it is not a P2SH or P2WSH pkScript, a nil slice is returned.
func ExtractSwapDetails ¶
func ExtractSwapDetails(pkScript []byte, segwit bool, chainParams *chaincfg.Params) ( sender, receiver btcutil.Address, lockTime uint64, secretHash []byte, err error)
ExtractSwapDetails extacts the sender and receiver addresses from a swap contract. If the provided script is not a swap contract, an error will be returned.
func FindKeyPush ¶
func FindKeyPush(witness [][]byte, sigScript, contractHash []byte, segwit bool, chainParams *chaincfg.Params) ([]byte, error)
FindKeyPush attempts to extract the secret key from the signature script. The contract must be provided for the search algorithm to verify the correct data push. Only contracts of length SwapContractSize that can be validated by ExtractSwapDetails are recognized.
func IsDust ¶
IsDust returns whether or not the passed transaction output amount is considered dust or not based on the passed minimum transaction relay fee. Dust is defined in terms of the minimum transaction relay fee. Based on btcd/policy isDust. See btcd/policy for further documentation.
func MakeContract ¶
func MakeContract(rAddr, sAddr btcutil.Address, secretHash []byte, lockTime int64, segwit bool, chainParams *chaincfg.Params) ([]byte, error)
MakeContract creates a segwit atomic swap contract. The secretHash MUST be computed from a secret of length SecretKeySize bytes or the resulting contract will be invalid.
func MsgTxVBytes ¶
MsgTxVBytes returns the transaction's virtual size, which accounts for the segwit input weighting.
func ParseKeyExtended ¶ added in v0.5.0
func ParseKeyExtended(keyPart string) (key *hdkeychain.ExtendedKey, fingerprint, path string, isRange bool, err error)
ParseKeyExtended is used to decode a Descriptor.Key when KeyFmt is KeyExtended, which is the standard BIP32 extended key encoding with optional derivation steps and a range indicator appended. The fingerprint of the extended key is returned as a convenience. Use ParsePath to get the derivation steps. e.g. "tpubDCo.../0/*" is an extended public key with a ranged path.
func ParsePath ¶ added in v0.5.0
ParsePath splits a common path string such as 84'/0'/0'/0 into it's components, returning the steps in the derivation as integers. Hardened key derivation steps, which are indicated by a ' or h, are offset by 2^31. Certain paths in descriptors may end with /* (or /*' or /*h) to indicate all child keys (or hardened child keys), in which case isRange will be true. As a special case, a prefix of "m/" or just "/" is allowed, but a master key fingerprint is not permitted and must be stripped first.
func ReadCloneParams ¶
func ReadCloneParams(cloneParams *CloneParams) *chaincfg.Params
ReadCloneParams translates a CloneParams into a btcsuite chaincfg.Params.
func RedeemP2SHContract ¶
RedeemP2SHContract returns the signature script to redeem a contract output using the redeemer's signature and the initiator's secret. This function assumes P2SH and appends the contract as the final data push.
func RedeemP2WSHContract ¶
RedeemP2WSHContract returns the witness script to redeem a contract output using the redeemer's signature and the initiator's secret. This function assumes P2WSH and appends the contract as the final data push.
func RefundP2SHContract ¶
RefundP2SHContract returns the signature script to refund a contract output using the contract author's signature after the locktime has been reached. This function assumes P2SH and appends the contract as the final data push.
func RefundP2WSHContract ¶
RefundP2WSHContract returns the witness to refund a contract output using the contract author's signature after the locktime has been reached. This function assumes P2WSH and appends the contract as the final data push.
func SystemConfigPath ¶
SystemConfigPath will return the default config file path for bitcoin-like assets.
Types ¶
type AddressDecoder ¶ added in v0.2.0
AddressDecoder decodes a string address to a btcutil.Address.
type AddressStringer ¶ added in v0.5.0
AddressStringer converts the btcutil.Address into a string address. This may be different from using (btcutil.Address).String when there is a special encoding such as a BCH "Cash Address".
type BTCScriptType ¶
type BTCScriptType uint8
BTCScriptType holds details about a pubkey script and possibly it's redeem script.
func ExtractScriptData ¶ added in v0.4.0
func ExtractScriptData(script []byte, chainParams *chaincfg.Params) (BTCScriptType, []string, int, error)
ExtractScriptData extracts script type, addresses, and required signature count from a pkScript. Non-standard scripts are not necessarily an error; non-nil errors are only returned if the script cannot be parsed. See also InputInfo for additional signature script size data
func ParseScriptType ¶
func ParseScriptType(pkScript, redeemScript []byte) BTCScriptType
ParseScriptType creates a BTCScriptType bitmap for the script type. A script type will be some combination of pay-to-pubkey-hash, pay-to-script-hash, and stake. If a script type is P2SH, it may or may not be mutli-sig.
func (BTCScriptType) IsMultiSig ¶
func (s BTCScriptType) IsMultiSig() bool
IsMultiSig is whether the pkscript references a multi-sig redeem script. Since the DEX will know the redeem script, we can say whether it's multi-sig.
func (BTCScriptType) IsP2PK ¶ added in v0.4.3
func (s BTCScriptType) IsP2PK() bool
IsP2PK will return boolean true if the script is a P2PK script.
func (BTCScriptType) IsP2PKH ¶
func (s BTCScriptType) IsP2PKH() bool
IsP2PKH will return boolean true if the script is a P2PKH script.
func (BTCScriptType) IsP2SH ¶
func (s BTCScriptType) IsP2SH() bool
IsP2SH will return boolean true if the script is a P2SH script.
func (BTCScriptType) IsP2WPKH ¶
func (s BTCScriptType) IsP2WPKH() bool
IsP2WPKH will return boolean true if the script is a P2WPKH script.
func (BTCScriptType) IsP2WSH ¶
func (s BTCScriptType) IsP2WSH() bool
IsP2WSH will return boolean true if the script is a P2WSH script.
func (BTCScriptType) IsSegwit ¶
func (s BTCScriptType) IsSegwit() bool
IsSegwit will return boolean true if the script is a P2WPKH or P2WSH script.
type BtcScriptAddrs ¶
type BtcScriptAddrs struct { PubKeys []btcutil.Address NumPK int PkHashes []btcutil.Address NumPKH int NRequired int // num sigs needed }
BtcScriptAddrs is information about the pubkeys or pubkey hashes present in a scriptPubKey (and the redeem script, for p2sh). This information can be used to estimate the spend script size, e.g. pubkeys in a redeem script don't require pubkeys in the witness/scriptSig, but pubkey hashes do.
func ExtractScriptAddrs ¶
ExtractScriptAddrs extracts the addresses from the pubkey script, or the redeem script if the pubkey script is P2SH. The returned bool indicates if the script is non-standard.
type CloneParams ¶
type CloneParams struct { // Name defines a human-readable identifier for the network. e.g. "mainnet", // "testnet4", or "regtest" Name string // PubKeyHashAddrID: Net ID byte for a pubkey-hash address PubKeyHashAddrID byte // ScriptHashAddrID: Net ID byte for a script-hash address ScriptHashAddrID byte // Bech32HRPSegwit: Human-readable part for Bech32 encoded segwit addresses, // as defined in BIP 173. Bech32HRPSegwit string // CoinbaseMaturity: The number of confirmations before a transaction // spending a coinbase input can be spent. CoinbaseMaturity uint16 // Net is the network identifier, e.g. wire.BitcoinNet. Net uint32 GenesisHash *chainhash.Hash // HDPrivateKeyID and HDPublicKeyID are the BIP32 hierarchical deterministic // extended key magic sequences. They are ONLY required if there is a need // to derive addresses from an extended key, such as if the asset is being // used by the server with the hdkeychain package to create fee addresses. // These are not required by the client. HDPrivateKeyID [4]byte HDPublicKeyID [4]byte }
CloneParams are the parameters needed by BTC-clone-based Backend and ExchangeWallet implementations. Pass a *CloneParams to ReadCloneParams to create a *chaincfg.Params for server/asset/btc.NewBTCClone and client/asset/btc.BTCCloneWallet.
type Descriptor ¶ added in v0.5.0
type Descriptor struct { // Function is the name of the top level function that begins the // descriptor. For example, "pk", "pkh", "wpkh", "sh", "wsh", etc. Function string // Key is set for the KEY type functions and certain SCRIPT functions with // nested KEY functions. May include a suffixed derivation path. Key string // KeyFmt is the type of key encoding. KeyFmt KeyFmt // KeyOrigin is an optional part of a KEY descriptor that describes the // derivation of the key. KeyOrigin *KeyOrigin // Nested will only be set for descriptors with SCRIPT expressions. Nested *Descriptor // Expression is the entirety of the arguments to Function. This may be a // KEY, SCRIPT, TREE, or combination of arguments depending on the function. Expression string // Checksum is an optional 8-character alphanumeric checksum of the // descriptor. It is not validated. Checksum string }
Descriptor models the output description language used by Bitcoin Core descriptor wallets. Descriptors are a textual representation of an output or address that begins with a "function", which may take as an argument a KEY, SCRIPT, or other data specific to the function. This Descriptor type is provided to decode and represent the most common KEY descriptor types and SCRIPT types that commonly wrap other KEY types.
func ParseDescriptor ¶ added in v0.5.0
func ParseDescriptor(desc string) (*Descriptor, error)
ParseDescriptor parses a descriptor string. See https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md If the descriptor string does not match the expected pattern, an ErrMalformedDescriptor error is returned. See the Descriptor type documentation for information.
type KeyFmt ¶ added in v0.5.0
type KeyFmt byte
KeyFmt specifies the encoding of the key within a KEY expression.
Valid KeyFmt values are one of:
- KeyHexPub: hex-encoded public key, compressed or uncompressed
- KeyWIFPriv: WIF-encoded private key
- KeyExtended public or private extended (BIP32) public key, PLUS zero or more /NUM or /NUM' steps, optionally terminated with a /* or /' to indicate all direct children (a range).
type KeyOrigin ¶ added in v0.5.0
type KeyOrigin struct { // Fingerprint is "Exactly 8 hex characters for the fingerprint of the key // where the derivation starts". Fingerprint string // Path is "zero or more /NUM or /NUM' path elements to indicate unhardened // or hardened derivation steps between the fingerprint and the key or // xpub/xprv root that follows". ParseDescriptor will strip a leading "/". Path string // Steps is the parsed path. Hardened keys indexes are offset by 2^31. Steps []uint32 }
KeyOrigin describes the optional part of a KEY expression that contains key origin information in side functions like pkh(KEY). For example, in the descriptor wpkh([b940190e/84'/1'/0'/0/0]0300034...) the key origin is [b940190e/84'/1'/0'/0/0], where the first part must be 8 hexadecimal characters for the fingerprint of the master key, and there are zero or more derivation steps to reach the key. See https://github.com/bitcoin/bitcoin/blob/master/doc/descriptors.md#key-origin-identification Note for Fingerprint: "software must be willing to deal with collisions".
type RPCConfig ¶ added in v0.4.0
type RPCConfig struct { RPCUser string `ini:"rpcuser"` RPCPass string `ini:"rpcpassword"` RPCBind string `ini:"rpcbind"` RPCPort int `ini:"rpcport"` RPCConnect string `ini:"rpcconnect"` // (bitcoin-cli) if set, reflected in RPCBind }
RPCConfig holds the parameters needed to initialize an RPC connection to a btc wallet or backend. Default values are used for RPCBind and/or RPCPort if not set.
type SpendInfo ¶
type SpendInfo struct { SigScriptSize uint32 WitnessSize uint32 ScriptAddrs *BtcScriptAddrs ScriptType BTCScriptType NonStandardScript bool }
SpendInfo is information about an input and it's previous outpoint.