dcr

package
v0.2.0-rc1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2021 License: BlueOak-1.0.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
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

	// 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

	// All pubkey scripts are assumed to be version 0.
	CurrentScriptVersion = 0

	// Overhead for a wire.TxIn with a scriptSig length < 254.
	// prefix (41 bytes) + ValueIn (8 bytes) + BlockHeight (4 bytes)
	// + BlockIndex (4 bytes) + sig script var int (at least 1 byte)
	TxInOverhead = 41 + 8 + 4 + 4 // 57 + at least 1 more

	P2PKHSigScriptSize = txsizes.RedeemP2PKHSigScriptSize
	P2PKHInputSize     = TxInOverhead + 1 + P2PKHSigScriptSize // 57 + 1 + 108 = 166

	// TxOutOverhead is the overhead associated with a transaction output.
	// 8 bytes value + 2 bytes version + at least 1 byte varint script size
	TxOutOverhead = 8 + 2 + 1

	P2PKHOutputSize = TxOutOverhead + txsizes.P2PKHPkScriptSize // 36
	P2SHOutputSize  = TxOutOverhead + txsizes.P2SHPkScriptSize  // 34

	// MsgTx overhead is 4 bytes version (lower 2 bytes for the real transaction
	// version and upper 2 bytes for the serialization type) + 4 bytes locktime
	// + 4 bytes expiry + 3 bytes of varints for the number of transaction
	// inputs (x2 for witness and prefix) and outputs
	MsgTxOverhead = 4 + 4 + 4 + 3 // 15

	// InitTxSizeBase is the size of a standard serialized atomic swap
	// initialization transaction with one change output and no inputs. MsgTx
	// overhead is 4 bytes version + 4 bytes locktime + 4 bytes expiry + 3 bytes
	// of varints for the number of transaction inputs (x2 for witness and
	// prefix) and outputs. There is one P2SH output with a 23 byte pkScript,
	// and one P2PKH change output with a 25 byte pkScript.
	InitTxSizeBase = MsgTxOverhead + P2PKHOutputSize + P2SHOutputSize // 15 + 36 + 34 = 85

	// InitTxSize is InitTxBaseSize + 1 P2PKH input
	InitTxSize = InitTxSizeBase + P2PKHInputSize // 85(83) + 166 = 251

	// DERSigLength is the maximum length of a DER encoded signature.
	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 type
	//   - OP_DATA_33
	//   - 33 bytes serialized compressed pubkey
	//   - OP_DATA_32
	//   - 32 bytes secret key
	//   - OP_1
	//   - varint 97 => OP_PUSHDATA1(0x4c) + 0x61
	//   - 97 bytes contract script
	RedeemSwapSigScriptSize = 1 + DERSigLength + 1 + 33 + 1 + 32 + 1 + 2 + SwapContractSize // 241

	// 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 type
	//   - OP_DATA_33
	//   - 33 bytes serialized compressed pubkey
	//   - OP_0
	//   - varint 97 => OP_PUSHDATA1(0x4c) + 0x61
	//   - 97 bytes contract script
	RefundSigScriptSize = 1 + DERSigLength + 1 + 33 + 1 + 2 + SwapContractSize // 208
)

Variables

This section is empty.

Functions

func DataPrefixSize

func DataPrefixSize(data []byte) uint8

DataPrefixSize returns the size of the size opcodes that would precede the data in a script. Certain data of length 0 and 1 is represented with OP_# or OP_1NEGATE codes directly without preceding OP_DATA_# OR OP_PUSHDATA# codes.

func ExtractContractHash

func ExtractContractHash(scriptHex string) ([]byte, error)

ExtractContractHash extracts the contract P2SH 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 script.

func ExtractPubKeyHash

func ExtractPubKeyHash(script []byte) []byte

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 ExtractPubKeyHashAltDetails

func ExtractPubKeyHashAltDetails(script []byte) ([]byte, dcrec.SignatureType)

ExtractPubKeyHashAltDetails extracts the public key hash and signature type from the passed script if it is a standard pay-to-alt-pubkey-hash script. It will return nil otherwise.

func ExtractScriptHash

func ExtractScriptHash(script []byte) []byte

ExtractScriptHash extracts the script hash from the passed script if it is a standard pay-to-script-hash script. It will return nil otherwise. See ExtractStakeScriptHash for stake transaction p2sh outputs.

NOTE: This function is only valid for version 0 opcodes. Since the function does not accept a script version, the results are undefined for other script versions.

func ExtractScriptHashByType

func ExtractScriptHashByType(scriptType DCRScriptType, pkScript []byte) ([]byte, error)

Grab the script hash based on the dcrScriptType.

func ExtractStakePubKeyHash

func ExtractStakePubKeyHash(script []byte, stakeOpcode byte) []byte

ExtractStakePubKeyHash extracts a pubkey hash from the passed public key script if it is a standard pay-to-pubkey-hash script tagged with the provided stake opcode. It will return nil otherwise.

func ExtractStakeScriptHash

func ExtractStakeScriptHash(script []byte, stakeOpcode byte) []byte

ExtractStakeScriptHash extracts a script hash from the passed public key script if it is a standard pay-to-script-hash script tagged with the provided stake opcode. It will return nil otherwise.

func ExtractSwapDetails

func ExtractSwapDetails(pkScript []byte, chainParams *chaincfg.Params) (
	sender, receiver dcrutil.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(sigScript, contractHash []byte, 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

func IsDust(txOut *wire.TxOut, minRelayTxFee uint64) bool

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. See dcrd/mempool/policy isDust for further documentation, though this version accepts atoms/byte rather than atoms/kB.

func IsDustVal

func IsDustVal(sz, amt, minRelayTxFee uint64) bool

IsDustVal is like IsDust but it only needs the size of the serialized output and its amount.

func IsPubKeyHashAltScript

func IsPubKeyHashAltScript(script []byte) bool

IsPubKeyHashAltScript returns whether or not the passed script is a standard pay-to-alt-pubkey-hash script.

func IsPubKeyHashScript

func IsPubKeyHashScript(script []byte) bool

IsPubKeyHashScript returns whether or not the passed script is a standard pay-to-pubkey-hash script.

func IsScriptHashScript

func IsScriptHashScript(script []byte) bool

IsScriptHashScript returns whether or not the passed script is a standard pay-to-script-hash script.

func IsStakePubkeyHashScript

func IsStakePubkeyHashScript(script []byte) bool

isStakePubkeyHashScript returns whether or not the passed script is a stake-related P2PKH script. Script is assumed to be version 0.

func IsStakeScriptHashScript

func IsStakeScriptHashScript(script []byte) bool

IsStakePubkeyHashScript returns whether or not the passed script is a stake-related P2SH script. Script is assumed to be version 0.

func MakeContract

func MakeContract(recipient, sender string, secretHash []byte, lockTime int64, chainParams *chaincfg.Params) ([]byte, error)

MakeContract creates an atomic swap contract. The secretHash MUST be computed from a secret of length SecretKeySize bytes or the resulting contract will be invalid.

func RedeemP2SHContract

func RedeemP2SHContract(contract, sig, pubkey, secret []byte) ([]byte, error)

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 RefundP2SHContract

func RefundP2SHContract(contract, sig, pubkey []byte) ([]byte, error)

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.

Types

type DCRScriptAddrs

type DCRScriptAddrs struct {
	PubKeys   []dcrutil.Address
	NumPK     int
	PkHashes  []dcrutil.Address
	NumPKH    int
	NRequired int
}

DCRScriptAddrs 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 scriptSig, but pubkey hashes do.

func ExtractScriptAddrs

func ExtractScriptAddrs(script []byte, chainParams *chaincfg.Params) (*DCRScriptAddrs, bool, error)

ExtractScriptAddrs extracts the addresses from script. Addresses are separated into pubkey and pubkey hash, where the pkh addresses are actually a catch all for non-P2PK addresses. As such, this function is not intended for use on P2SH pkScripts. Rather, the corresponding redeem script should be processed with ExtractScriptAddrs. The returned bool indicates if the script is non-standard.

type DCRScriptType

type DCRScriptType uint8

DCRScriptType is a bitmask with information about a pubkey script and possibly its redeem script.

const (
	ScriptP2PKH DCRScriptType = 1 << iota
	ScriptP2SH
	ScriptStake
	ScriptMultiSig
	ScriptSigEdwards
	ScriptSigSchnorr
	ScriptUnsupported
)

func ExtractScriptData

func ExtractScriptData(script []byte, chainParams *chaincfg.Params) (DCRScriptType, []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(scriptVersion uint16, pkScript, redeemScript []byte) DCRScriptType

ParseScriptType creates a dcrScriptType bitmask 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 (DCRScriptType) IsMultiSig

func (s DCRScriptType) 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 (DCRScriptType) IsP2PKH

func (s DCRScriptType) IsP2PKH() bool

IsP2PKH will return boolean true if the script is a P2PKH script.

func (DCRScriptType) IsP2SH

func (s DCRScriptType) IsP2SH() bool

IsP2SH will return boolean true if the script is a P2SH script.

func (DCRScriptType) IsStake

func (s DCRScriptType) IsStake() bool

IsStake will return boolean true if the pubkey script it tagged with a stake opcode.

type SpendInfo

type SpendInfo struct {
	SigScriptSize     uint32
	ScriptAddrs       *DCRScriptAddrs
	ScriptType        DCRScriptType
	NonStandardScript bool
}

SpendInfo is information about an input and it's previous outpoint.

func InputInfo

func InputInfo(pkScript, redeemScript []byte, chainParams *chaincfg.Params) (*SpendInfo, error)

InputInfo is some basic information about the input required to spend an output. The pubkey script of the output is provided. If the pubkey script parses as P2SH or P2WSH, the redeem script must be provided.

func (*SpendInfo) Size

func (nfo *SpendInfo) Size() uint32

Size is the serialized size of the input.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL