tapscript

package
v0.3.0-alpha.rc3 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2023 License: MIT Imports: 28 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DummyAmtSats is the default amount of sats we'll use in Bitcoin
	// outputs embedding Taproot Asset commitments. This value just needs to
	// be greater than dust, and we assume that this value is updated to
	// match the input asset bearing UTXOs before finalizing the transfer
	// TX.
	DummyAmtSats = btcutil.Amount(1_000)

	// SendConfTarget is the confirmation target we'll use to query for
	// a fee estimate.
	SendConfTarget = 6
)
View Source
const Subsystem = "SEND"

Subsystem defines the logging code for this subsystem.

Variables

View Source
var (
	// ErrInvalidCollectibleSplit is returned when a collectible is split
	// into more than two outputs.
	ErrInvalidCollectibleSplit = errors.New(
		"fund: invalid collectible split",
	)

	// ErrInvalidChangeOutputLocation is returned when the change output is
	// not at the expected location (index 0).
	ErrInvalidChangeOutputLocation = errors.New(
		"fund: invalid change output location, should be index 0",
	)

	// ErrInvalidSplitAmounts is returned when the split amounts don't add
	// up to the amount of the asset being spent.
	ErrInvalidSplitAmounts = errors.New(
		"fund: invalid split amounts, sum doesn't match input",
	)

	// ErrMissingInputAsset is an error returned when we attempt to spend
	// to a Taproot Asset address from an input that does not contain
	// the matching asset.
	ErrMissingInputAsset = errors.New(
		"send: Input does not contain requested asset",
	)

	// ErrInsufficientInputAssets is an error returned when we attempt
	// to spend to a Taproot Asset address from a set of inputs which
	// contain an insufficient amount of total funds.
	ErrInsufficientInputAssets = errors.New(
		"send: Input assets total funds is insufficient",
	)

	// ErrInvalidOutputIndexes is an error returned when we attempt to spend
	// to Bitcoin output indexes that do not start at 0 or
	// are not continuous.
	ErrInvalidOutputIndexes = errors.New(
		"send: Output indexes not starting at 0 and continuous",
	)

	// ErrMissingSplitAsset is an error returned when we attempt to look up
	// a split asset in a map and the specified asset is not found.
	ErrMissingSplitAsset = errors.New(
		"send: split asset not found",
	)

	// ErrMissingAssetCommitment is an error returned when we attempt to
	// look up an Asset commitment in a map and the specified commitment
	// is not found.
	ErrMissingAssetCommitment = errors.New(
		"send: Asset commitment not found",
	)

	// ErrMissingTapCommitment is an error returned when we attempt to look
	// up a Taproot Asset commitment in a map and the specified commitment
	// is not found.
	ErrMissingTapCommitment = errors.New(
		"send: Taproot Asset commitment not found",
	)

	// ErrInvalidAnchorInfo is an error returned when the anchor output
	// information on a virtual transaction output is invalid.
	ErrInvalidAnchorInfo = errors.New(
		"send: invalid anchor output info",
	)
)
View Source
var (
	// ErrNoInputs represents an error case where an asset undergoing a
	// state transition does not have any or a specific input required.
	ErrNoInputs = errors.New("missing asset input(s)")

	// ErrInputMismatch represents an error case where an asset's set of
	// inputs mismatch the set provided to the virtual machine.
	ErrInputMismatch = errors.New("asset input(s) mismatch")

	// ErrInvalidScriptVersion represents an error case where an asset input
	// commits to an invalid script version.
	ErrInvalidScriptVersion = errors.New("invalid script version")
)
View Source
var (
	// GenesisDummyScript is a dummy script that we'll use to fund the
	// initial PSBT packet that'll create initial set of assets. It's the
	// same size as a encoded P2TR output and has a valid P2TR prefix.
	GenesisDummyScript = append(
		[]byte{txscript.OP_1, 0x20}, bytes.Repeat([]byte{0x00}, 32)...,
	)
)

Functions

func AnchorPassiveAssets

func AnchorPassiveAssets(passiveAssets []*tappsbt.VPacket,
	tapCommitment *commitment.TapCommitment) error

AnchorPassiveAssets anchors the passive assets within the given Taproot Asset commitment.

func AreValidAnchorOutputIndexes

func AreValidAnchorOutputIndexes(outputs []*tappsbt.VOutput) (bool, error)

AreValidAnchorOutputIndexes checks a set of virtual outputs for the minimum number of outputs, and tests if the external indexes could be used for a Taproot Asset only spend, i.e. a TX that does not need other outputs added to be valid.

func AssetFromTapCommitment

func AssetFromTapCommitment(tapCommitment *commitment.TapCommitment,
	desc *FundingDescriptor, inputScriptKey btcec.PublicKey) (*asset.Asset,
	error)

AssetFromTapCommitment uses a script key to extract an asset from a given Taproot Asset commitment.

func BuildGenesisTx added in v0.3.0

func BuildGenesisTx(newAsset *asset.Asset) (*wire.MsgTx,
	*wire.TxOut, error)

BuildGenesisTx constructs a virtual transaction and prevOut that represent the genesis state transition for a grouped asset. This output is used to create a group witness for the grouped asset.

func CreateAnchorTx

func CreateAnchorTx(outputs []*tappsbt.VOutput) (*psbt.Packet, error)

CreateAnchorTx creates a template BTC anchor TX with dummy outputs.

func CreateOutputCommitments

func CreateOutputCommitments(inputTapCommitments tappsbt.InputCommitments,
	vPkt *tappsbt.VPacket,
	passiveAssets []*tappsbt.VPacket) ([]*commitment.TapCommitment,
	error)

CreateOutputCommitments creates the final set of Taproot asset commitments representing the asset send.

func CreateTaprootSignature

func CreateTaprootSignature(vIn *tappsbt.VInput, virtualTx *wire.MsgTx,
	idx int, txSigner Signer) (wire.TxWitness, error)

CreateTaprootSignature creates a Taproot signature for the given asset input. Depending on the fields set in the input, this will either create a key path spend or a script path spend.

func DisableLog added in v0.3.0

func DisableLog()

DisableLog disables all library log output. Logging output is disabled by default until UseLogger is called.

func InputAssetPrevOut

func InputAssetPrevOut(prevAsset asset.Asset) (*wire.TxOut, error)

InputAssetPrevOut returns a TxOut that represents the input asset in a Taproot Asset virtual TX.

func InputKeySpendSigHash

func InputKeySpendSigHash(virtualTx *wire.MsgTx, input *asset.Asset,
	idx uint32, sigHashType txscript.SigHashType) ([]byte, error)

InputKeySpendSigHash returns the signature hash of a virtual transaction for a specific Taproot Asset input that can be spent through the key path. This is the message over which signatures are generated over.

func InputPrevOutFetcher

func InputPrevOutFetcher(prevAsset asset.Asset) (*txscript.CannedPrevOutputFetcher,
	error)

InputPrevOutFetcher returns a Taproot Asset input's `PrevOutFetcher` to be used throughout signing.

func InputScriptSpendSigHash

func InputScriptSpendSigHash(virtualTx *wire.MsgTx, input *asset.Asset,
	idx uint32, sigHashType txscript.SigHashType,
	tapLeaf *txscript.TapLeaf) ([]byte, error)

InputScriptSpendSigHash returns the signature hash of a virtual transaction for a specific Taproot Asset input that can be spent through the script path. This is the message over which signatures are generated over.

func LogCommitment added in v0.3.0

func LogCommitment(prefix string, idx int,
	tapCommitment *commitment.TapCommitment, internalKey *btcec.PublicKey,
	pkScript, trimmedMerkleRoot []byte)

LogCommitment logs the given Taproot Asset commitment to the log as a trace message. This is a no-op if the log level is not set to trace.

func PayToAddrScript

func PayToAddrScript(internalKey btcec.PublicKey, sibling *chainhash.Hash,
	commitment commitment.TapCommitment) ([]byte, error)

PayToAddrScript constructs a P2TR script that embeds a Taproot Asset commitment by tweaking the receiver key by a Tapscript tree that contains the Taproot Asset commitment root. The Taproot Asset commitment must be reconstructed by the receiver, and they also need to Tapscript sibling hash used here if present.

func PayToTaprootScript

func PayToTaprootScript(taprootKey *btcec.PublicKey) ([]byte, error)

PayToTaprootScript creates a pk script for a pay-to-taproot output key.

func PrepareOutputAssets

func PrepareOutputAssets(ctx context.Context, vPkt *tappsbt.VPacket) error

PrepareOutputAssets prepares the assets of the given outputs depending on the amounts set on the transaction. If a split is necessary (non-interactive or partial amount send) it computes a split commitment with the given inputs and spend information. The inputs MUST be checked as valid beforehand and the change output is expected to be declared as such (and be at index 0).

func SignVirtualTransaction

func SignVirtualTransaction(vPkt *tappsbt.VPacket, signer Signer,
	validator TxValidator) error

SignVirtualTransaction updates the new asset (the root asset located at the change output in case of a non-interactive or partial amount send or the full asset in case of an interactive full amount send) by creating a signature over the asset transfer, verifying the transfer with the Taproot Asset VM, and attaching that signature to the new Asset.

func UpdateTaprootOutputKeys

func UpdateTaprootOutputKeys(btcPacket *psbt.Packet, vPkt *tappsbt.VPacket,
	outputCommitments []*commitment.TapCommitment) (
	map[uint32]*commitment.TapCommitment, error)

UpdateTaprootOutputKeys updates a PSBT with outputs embedding TapCommitments involved in an asset send. The sender must attach the Bitcoin input holding the corresponding Taproot Asset input asset to this PSBT before finalizing the TX. Locators MUST be checked beforehand.

func UseLogger added in v0.3.0

func UseLogger(logger btclog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using btclog.

func ValidateInputs

func ValidateInputs(inputCommitments tappsbt.InputCommitments,
	inputsScriptKeys []*btcec.PublicKey, expectedAssetType asset.Type,
	desc *FundingDescriptor) (bool, error)

ValidateInputs validates a set of inputs against a funding request. It returns true if the inputs would be spent fully, otherwise false.

func VirtualTx

func VirtualTx(newAsset *asset.Asset, prevAssets commitment.InputSet) (
	*wire.MsgTx, mssmt.Tree, error)

VirtualTx constructs the virtual transaction that enables the movement of an asset representing an asset state transition.

Types

type AssetGroupQuerier

type AssetGroupQuerier interface {
	// QueryAssetGroup attempts to locate the asset group information
	// (genesis + group key) associated with a given asset.
	QueryAssetGroup(context.Context, asset.ID) (*asset.AssetGroup, error)
}

AssetGroupQuerier is an interface that allows us to query for asset groups by asset ID.

type FundingDescriptor

type FundingDescriptor struct {
	// ID is the asset ID of the asset being transferred.
	ID asset.ID

	// GroupKey is the optional group key of the asset to transfer.
	GroupKey *btcec.PublicKey

	// Amount is the amount of the asset to transfer.
	Amount uint64
}

FundingDescriptor describes the information that is needed to select and verify input assets in order to send to a specific recipient. It is a subset of the information contained in a Taproot Asset address.

func DescribeAddrs

func DescribeAddrs(addrs []*address.Tap) (*FundingDescriptor, error)

DescribeAddrs extracts the recipient descriptors from a list of Taproot Asset addresses.

func DescribeRecipients

func DescribeRecipients(ctx context.Context, vPkt *tappsbt.VPacket,
	groupQuerier AssetGroupQuerier) (*FundingDescriptor, error)

DescribeRecipients extracts the recipient descriptors from a Taproot Asset PSBT.

func (*FundingDescriptor) TapCommitmentKey

func (r *FundingDescriptor) TapCommitmentKey() [32]byte

TapCommitmentKey is the key that maps to the root commitment for the asset group specified by a recipient descriptor.

type GroupTxBuilder added in v0.3.0

type GroupTxBuilder struct{}

GroupTxBuilder is an implementation of the asset.GenesisTxBuilder interface that constructs virtual transactions for grouped asset genesis.

func (*GroupTxBuilder) BuildGenesisTx added in v0.3.0

func (m *GroupTxBuilder) BuildGenesisTx(newAsset *asset.Asset) (*wire.MsgTx,
	*wire.TxOut, error)

BuildGenesisTx constructs a virtual transaction and prevOut that represent the genesis state transition for a grouped asset. This output is used to create a group witness for the grouped asset.

type MockSigner

type MockSigner struct {
	PrivKey *btcec.PrivateKey
}

func NewMockSigner

func NewMockSigner(privKey *btcec.PrivateKey) *MockSigner

func (*MockSigner) SignVirtualTx

func (m *MockSigner) SignVirtualTx(signDesc *lndclient.SignDescriptor,
	tx *wire.MsgTx, prevOut *wire.TxOut) (*schnorr.Signature, error)

type Signer

type Signer interface {
	// SignVirtualTx generates a signature according to the passed signing
	// descriptor and TX.
	SignVirtualTx(signDesc *lndclient.SignDescriptor, tx *wire.MsgTx,
		prevOut *wire.TxOut) (*schnorr.Signature, error)
}

Signer is the interface used to compute the witness for a Taproot Asset virtual TX.

type TxValidator

type TxValidator interface {
	// Execute creates an instance of the Taproot Asset VM and validates
	// an asset transfer, including the attached witnesses.
	Execute(newAsset *asset.Asset, splitAssets []*commitment.SplitAsset,
		prevAssets commitment.InputSet) error
}

TxValidator is the interface used to validate an asset transfer with the Taproot Asset VM.

Jump to

Keyboard shortcuts

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