zec

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: BlueOak-1.0.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MinimumTxOverhead
	// 4 header + 4 nVersionGroup + 1 varint input count + 1 varint output count
	// + 4 lockTime + 4 nExpiryHeight + 8 valueBalanceSapling + 1 varint nSpendsSapling
	// + 1 varint nOutputsSapling + 1 varint nJoinSplit
	MinimumTxOverhead = 29

	InitTxSizeBase = MinimumTxOverhead + btc.P2PKHOutputSize + btc.P2SHOutputSize // 29 + 34 + 32 = 95
	InitTxSize     = InitTxSizeBase + btc.RedeemP2PKHInputSize                    // 95 + 149 = 244

	// LegacyFeeRate returns a standard 10 zats / byte. Prior to ZIP-0317, Zcash
	// used a standard tx fee of 1000 zats, regardless of tx size. However,
	// zcashd v5.5 begins making stricter fee requirements for both relay and
	// block inclusion. The release notes state that relay by default still
	// works with 1000 zat fee txns, but it may be adjusted by operators, and it
	// may need to be set to the higher ZIP 317 rate. For mining, there is a
	// small allowance on the number of "unpaid actions" allowed in a block, so
	// we should take care to pay the ZIP 317 "conventional" rate, which is
	// multiples of 5000 zats, and a minimum of 10000. To ensure we have no
	// unpaid actions in our (transparent) transactions, we need a higher rate.
	// For a 242 byte transaction, like a swap init, we can emulate this with
	// about 42 zats/byte. Even with 100 zats/byte, our typical redeem of ~342
	// bytes would pay 34200 zats, which is only about a penny, so to ensure our
	// transactions are relayed and mined, we go with a high rate.
	LegacyFeeRate = 84
)
View Source
const (
	VersionPreOverwinter = 2
	VersionOverwinter    = 3
	VersionSapling       = 4
	VersionNU5           = 5
	MaxExpiryHeight      = 499999999 // https://zips.z.cash/zip-0203

)

Variables

View Source
var (
	UnitInfo = dex.UnitInfo{
		AtomicUnit: "zats",
		Conventional: dex.Denomination{
			Unit:             "ZEC",
			ConversionFactor: 1e8,
		},
		Alternatives: []dex.Denomination{
			{
				Unit:             "mZEC",
				ConversionFactor: 1e5,
			},
			{
				Unit:             "µZEC",
				ConversionFactor: 1e2,
			},
		},
		FeeRateDenom: "action",
	}

	// MainNetParams are the clone parameters for mainnet. Zcash,
	// like Decred, uses two bytes for their address IDs. We will convert
	// between address types on the fly and use these spoof parameters
	// internally.
	MainNetParams = btc.ReadCloneParams(&btc.CloneParams{
		Name:             "mainnet",
		ScriptHashAddrID: 0xBD,
		PubKeyHashAddrID: 0xB8,
		CoinbaseMaturity: 100,
		Net:              0x24e92764,
	})
	// TestNet4Params are the clone parameters for testnet.
	TestNet4Params = btc.ReadCloneParams(&btc.CloneParams{
		Name:             "testnet4",
		PubKeyHashAddrID: 0x25,
		ScriptHashAddrID: 0xBA,
		CoinbaseMaturity: 100,
		Net:              0xfa1af9bf,
	})
	// RegressionNetParams are the clone parameters for simnet.
	RegressionNetParams = btc.ReadCloneParams(&btc.CloneParams{
		Name:             "regtest",
		PubKeyHashAddrID: 0x25,
		ScriptHashAddrID: 0xBA,
		CoinbaseMaturity: 100,
		Net:              0xaae83f5f,
	})

	// MainNetAddressParams are used for string address parsing. We use a
	// spoofed address internally, since Zcash uses a two-byte address ID
	// instead of a 1-byte ID.
	MainNetAddressParams = &AddressParams{
		ScriptHashAddrID: [2]byte{0x1C, 0xBD},
		PubKeyHashAddrID: [2]byte{0x1C, 0xB8},
	}

	// TestNet4AddressParams are used for string address parsing.
	TestNet4AddressParams = &AddressParams{
		ScriptHashAddrID: [2]byte{0x1C, 0xBA},
		PubKeyHashAddrID: [2]byte{0x1D, 0x25},
	}

	// RegressionNetAddressParams are used for string address parsing.
	RegressionNetAddressParams = &AddressParams{
		ScriptHashAddrID: [2]byte{0x1C, 0xBA},
		PubKeyHashAddrID: [2]byte{0x1D, 0x25},
	}
)
View Source
var (
	// Little-endian encoded CONSENSUS_BRANCH_IDs.
	// https://zcash.readthedocs.io/en/latest/rtd_pages/nu_dev_guide.html#canopy
	ConsensusBranchNU5     = [4]byte{0xB4, 0xD0, 0xD6, 0xC2} // 1687104, testnet: 1842420
	ConsensusBranchSapling = [4]byte{0xBB, 0x09, 0xB8, 0x76}

	// Zclassic only
	ConsensusBranchButtercup = [4]byte{0x0d, 0x54, 0x0b, 0x93}
)

Functions

func CalcTxSize

func CalcTxSize(tx *wire.MsgTx) uint64

CalcTxSize calculates the size of a Zcash transparent transaction. CalcTxSize won't return accurate results for shielded or blended transactions.

func DecodeAddress

func DecodeAddress(a string, addrParams *AddressParams, btcParams *chaincfg.Params) (btcutil.Address, error)

DecodeAddress decodes an address string into an internal btc address. Zcash uses a double SHA-256 checksum but with a 2-byte address ID, so a little customization is needed. TODO: There also appears to be a bech32 encoding and something called a "unified payment address", but for our use of this function client-side, we will never generate those addresses. Do we need to revisit before NU5?

func DeserializeBlockHeader added in v1.0.0

func DeserializeBlockHeader(b []byte) (*wire.BlockHeader, error)

func EncodeAddress

func EncodeAddress(btcAddr btcutil.Address, addrParams *AddressParams) (string, error)

EncodeAddress converts a btcutil.Address from the BTC backend into a Zcash address string.

func MinHTLCSize added in v1.0.0

func MinHTLCSize(redeemTxFees uint64) uint64

MinHTLCSize calculates the minimum value for the output of a chained P2SH -> P2WPKH transaction pair where the spending tx size is known.

func MinLotSize added in v1.0.0

func MinLotSize(maxFeeRate uint64) uint64

MinLotSize is the minimum lot size that avoids dust for a given max network fee rate.

func RecodeAddress

func RecodeAddress(addr string, addrParams *AddressParams, btcParams *chaincfg.Params) (string, error)

RecodeAddress converts an internal btc address to a Zcash address string.

func RequiredOrderFunds added in v1.0.0

func RequiredOrderFunds(swapVal, inputCount, inputsSize, maxSwaps uint64) uint64

RequiredOrderFunds is the ZIP-0317 compliant version of calc.RequiredOrderFunds.

func TransparentTxFeesZIP317 added in v1.0.0

func TransparentTxFeesZIP317(txInSize, txOutSize uint64) uint64

TransparentTxFeesZIP317 calculates the ZIP-0317 fees for a fully transparent Zcash transaction, which only depends on the size of the tx_in and tx_out fields.

func TxFeesZIP317 added in v1.0.0

func TxFeesZIP317(transparentTxInsSize, transparentTxOutsSize uint64, nSpendsSapling, nOutputsSapling, nJoinSplit, nActionsOrchard uint64) uint64

TxFeexZIP317 calculates fees for a transaction. The caller must sum up the txin and txout, which is the entire serialization size associated with the respective field, including the size of the count varint.

Types

type AddressParams

type AddressParams struct {
	PubKeyHashAddrID [2]byte
	ScriptHashAddrID [2]byte
}

type Block

type Block struct {
	wire.MsgBlock
	// Transactions and MsgBlock.Transactions should both be populated. Each
	// *Tx.MsgTx will be the same as the *MsgTx in MsgBlock.Transactions.
	Transactions         []*Tx
	HashBlockCommitments [32]byte // Using NU5 name
	Nonce                [32]byte // Bitcoin uses uint32
	Solution             []byte   // length 1344 on main and testnet, 36 on regtest
}

Block extends a wire.MsgBlock to specify Zcash specific fields, or in the case of the Nonce, a type-variant.

func DeserializeBlock

func DeserializeBlock(b []byte) (*Block, error)

DeserializeBlock deserializes the Zcash-encoded block.

type JoinSplit

type JoinSplit struct {
	Old, New uint64
}

JoinSplit is only the new and old fields of a vJoinSplit.

type Tx

type Tx struct {
	*wire.MsgTx
	ExpiryHeight        uint32
	NSpendsSapling      uint64
	NOutputsSapling     uint64
	ValueBalanceSapling int64
	NActionsOrchard     uint64
	SizeProofsOrchard   uint64
	NJoinSplit          uint64
	VJoinSplit          []*JoinSplit
	ValueBalanceOrchard int64
}

Tx is a Zcash-adapted MsgTx. Tx will decode any version transaction, but will not save most data for shielded transactions. Tx can only produce tx hashes for unshielded transactions. Tx can only create signature hashes for unshielded version 5 transactions.

func DeserializeTx

func DeserializeTx(b []byte) (*Tx, error)

see https://zips.z.cash/protocol/protocol.pdf section 7.1

func NewTxFromMsgTx

func NewTxFromMsgTx(tx *wire.MsgTx, expiryHeight uint32) *Tx

NewTxFromMsgTx creates a Tx embedding the MsgTx, and adding Zcash-specific fields.

func (*Tx) Bytes

func (tx *Tx) Bytes() (_ []byte, err error)

Bytes encodes the receiver to w using the bitcoin protocol encoding. This is part of the Message interface implementation. See Serialize for encoding transactions to be stored to disk, such as in a database, as opposed to encoding transactions for the wire. msg.Version must be 4 or 5.

func (*Tx) RequiredTxFeesZIP317 added in v1.0.0

func (tx *Tx) RequiredTxFeesZIP317() uint64

RequiredTxFeesZIP317 calculates the minimum tx fees according to ZIP-0317.

func (*Tx) SerializeSize

func (tx *Tx) SerializeSize() uint64

SerializeSize is the size of the transaction when serialized.

func (*Tx) SignatureDigest

func (tx *Tx) SignatureDigest(
	vin int, hashType txscript.SigHashType, script []byte, vals []int64, prevScripts [][]byte,
) (_ [32]byte, err error)

SignatureDigest produces a hash of tx data suitable for signing. SignatureDigest only works correctly for unshielded version 5 transactions.

func (*Tx) TxHash

func (tx *Tx) TxHash() chainhash.Hash

TxHash generates the Hash for the transaction.

func (*Tx) ZecDecode

func (tx *Tx) ZecDecode(r io.Reader) (err error)

ZecDecode reads the serialized transaction from the reader and populates the *Tx's fields.

Jump to

Keyboard shortcuts

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