dcr

package
v0.6.3 Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2023 License: BlueOak-1.0.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxCLTVScriptNum is the largest usable value for a CLTV lockTime. This
	// will actually be stored in a 5-byte ScriptNum since they have a sign bit,
	// however, it is not 2^39-1 since the spending transaction's nLocktime is
	// an unsigned 32-bit integer and it must be at least the CLTV value. This
	// establishes a maximum lock time of February 7, 2106. Any later requires
	// using a block height instead of a unix epoch time stamp.
	MaxCLTVScriptNum = 1<<32 - 1 // 0xffff_ffff a.k.a. 2^32-1

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

	// TxInOverhead is the 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

	// MsgTxOverhead 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 + pubkeyLength + 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 + pubkeyLength + 1 + 2 + SwapContractSize // 208

	// BondScriptSize is the maximum size of a DEX time-locked fidelity bond
	// output script to which a bond P2SH pays:
	//   OP_DATA_4/5 (4/5 bytes lockTime) OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 OP_DATA_20 (20-byte pubkey hash160) OP_EQUALVERIFY OP_CHECKSIG
	BondScriptSize = 1 + 5 + 1 + 1 + 1 + 1 + 1 + 20 + 1 + 1 // 33

	// RedeemBondSigScriptSize is the worst case size of a fidelity bond
	// signature script that spends a bond output. It includes a signature, a
	// compressed pubkey, and the bond script. Each of said data pushes use an
	// OP_DATA_ code.
	RedeemBondSigScriptSize = 1 + DERSigLength + 1 + pubkeyLength + 1 + BondScriptSize // 142

	// BondPushDataSize is the size of the nulldata in a bond commitment output:
	//  OP_RETURN <pushData: ver[2] | account_id[32] | lockTime[4] | pkh[20]>
	BondPushDataSize = 2 + account.HashSize + 4 + 20
)

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 ExtractBondCommitDataV0 added in v0.6.0

func ExtractBondCommitDataV0(scriptVer uint16, pkScript []byte) (acct account.AccountID, lockTime uint32, pubkeyHash [20]byte, err error)

ExtractBondCommitDataV0 parses a v0 bond commitment output script. This is the OP_RETURN output, not the P2SH bond output. Use ExtractBondDetailsV0 to parse the P2SH bond output's redeem script.

If the decoded commitment data indicates a version other than 0, an error is returned.

func ExtractBondDetailsV0 added in v0.6.0

func ExtractBondDetailsV0(scriptVersion uint16, bondScript []byte) (lockTime uint32, pkh []byte, err error)

ExtractBondDetailsV0 validates the provided bond redeem script, extracting the lock time and pubkey. The V0 format of the script must be as follows:

<lockTime> OP_CHECKLOCKTIMEVERIFY OP_DROP OP_DUP OP_HASH160 <pubkeyhash[20]> OP_EQUALVERIFY OP_CHECKSIG

The script version refers to the pkScript version, not bond version, which pertains to DEX's version of the bond script.

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 IsRefundScript added in v0.6.0

func IsRefundScript(scriptVersion uint16, sigScript, contract []byte) bool

IsRefundScript checks if the signature script is of the expected format for the standard swap contract refund. The signature and pubkey data pushes are not validated other than ensuring they are data pushes. The provided contract must correspond to the final data push in the sigScript, but it is otherwise not validated either.

func MakeBondScript added in v0.6.0

func MakeBondScript(ver uint16, lockTime uint32, pubkeyHash []byte) ([]byte, error)

MakeBondScript constructs a versioned bond output script for the provided lock time and pubkey hash. Only version 0 is supported at present. The lock time must be less than 2^32-1 so that it uses at most 5 bytes. The lockTime is also required to use at least 4 bytes (time stamp, not block time).

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 RefundBondScript added in v0.6.0

func RefundBondScript(bondScript, sig, pubkey []byte) ([]byte, error)

RefundBondScript builds the signature script to refund a time-locked fidelity bond in a P2SH output paying to the provided P2PKH bondScript.

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