dcr

package
v0.5.8 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: BlueOak-1.0.0 Imports: 13 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

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

	P2PKSigScriptSize = txsizes.RedeemP2PKSigScriptSize

	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

View Source
var UnitInfo = dex.UnitInfo{
	AtomicUnit: "atoms",
	Conventional: dex.Denomination{
		Unit:             "DCR",
		ConversionFactor: 1e8,
	},
}

Functions

func AddressScript added in v0.4.0

func AddressScript(addr stdaddr.Address) ([]byte, error)

AddressScript returns the raw bytes of the address to be used when inserting the address into a txout's script. This is currently the address' pubkey or script hash160. Other address types are unsupported.

func AddressSigType added in v0.4.0

func AddressSigType(addr stdaddr.Address) (sigType dcrec.SignatureType, err error)

AddressSigType returns the digital signature algorithm for the specified address.

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 ExtractScriptAddrs

func ExtractScriptAddrs(version uint16, script []byte, chainParams *chaincfg.Params) (ScriptType, *ScriptAddrs)

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.

func ExtractScriptHash

func ExtractScriptHash(version uint16, script []byte) []byte

ExtractScriptHash extracts the script hash from a P2SH pkScript of a particular script version.

func ExtractScriptHashV0 added in v0.4.0

func ExtractScriptHashV0(script []byte) []byte

ExtractScriptHashV0 extracts the script hash from a P2SH pkScript. This is valid only for version 0 scripts.

func ExtractSwapDetails

func ExtractSwapDetails(pkScript []byte, chainParams *chaincfg.Params) (
	sender, receiver stdaddr.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(scriptVersion uint16, 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 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 ScriptAddrs added in v0.4.0

type ScriptAddrs struct {
	PubKeys   []stdaddr.Address
	PkHashes  []stdaddr.Address
	NRequired int
}

ScriptAddrs 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.

type ScriptType added in v0.4.0

type ScriptType uint8

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

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

func ExtractScriptData

func ExtractScriptData(version uint16, script []byte, chainParams *chaincfg.Params) (ScriptType, []string, int)

ExtractScriptData extracts script type, addresses, and required signature count from a pkScript. Non-standard scripts are not 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 []byte) ScriptType

ParseScriptType creates a ScriptType bitmask for a pkScript.

func (ScriptType) IsMultiSig added in v0.4.0

func (s ScriptType) 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 (ScriptType) IsP2PK added in v0.4.3

func (s ScriptType) IsP2PK() bool

IsP2PK will return boolean true if the script is a P2PK script.

func (ScriptType) IsP2PKH added in v0.4.0

func (s ScriptType) IsP2PKH() bool

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

func (ScriptType) IsP2SH added in v0.4.0

func (s ScriptType) IsP2SH() bool

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

func (ScriptType) IsStake added in v0.4.0

func (s ScriptType) 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       *ScriptAddrs
	ScriptType        ScriptType
	NonStandardScript bool // refers to redeem script for P2SH ScriptType
}

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

func InputInfo

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

InputInfo is some basic information about the input required to spend an output. Only P2PKH and P2SH pkScripts are supported. If the pubkey script parses as P2SH, 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