asset

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2024 License: MIT Imports: 29 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MaxAssetNameLength is the maximum byte length of an asset's name.
	// This byte length is equivalent to character count for single-byte
	// UTF-8 characters.
	MaxAssetNameLength = 64

	// MaxAssetEncodeSizeBytes is the size we expect an asset to not exceed
	// in its encoded form. This is used to prevent OOMs when decoding
	// assets. The main contributing factor to this size are the previous
	// witnesses which we currently allow to number up to 65k witnesses.
	MaxAssetEncodeSizeBytes = blockchain.MaxBlockWeight
)
View Source
const (
	// MetaHashLen is the length of the metadata hash.
	MetaHashLen = 32
)
View Source
const (
	// TaprootAssetsKeyFamily is the key family used to generate internal
	// keys that tapd will use creating internal taproot keys and also any
	// other keys used for asset script keys.
	// This was derived via: sum(map(lambda y: ord(y), 'tapd')).
	// In order words: take the word tapd and return the integer
	// representation of each character and sum those. We get 425, then
	// divide that by 2, to allow us to fit this into just a 2-byte integer
	// and to ensure compatibility with the remote signer.
	TaprootAssetsKeyFamily = 212
)

Variables

View Source
var (
	// ZeroPrevID is the blank prev ID used for genesis assets and also
	// asset split leaves.
	ZeroPrevID PrevID

	// NUMSBytes is the NUMs point we'll use for un-spendable script keys.
	// It was generated via a try-and-increment approach using the phrase
	// "taproot-assets" with SHA2-256. The code for the try-and-increment
	// approach can be seen here:
	// https://github.com/lightninglabs/lightning-node-connect/tree/master/mailbox/numsgen
	NUMSBytes, _ = hex.DecodeString(
		"027c79b9b26e463895eef5679d8558942c86c4ad2233adef01bc3e6d540b" +
			"3653fe",
	)
	NUMSPubKey, _     = btcec.ParsePubKey(NUMSBytes)
	NUMSCompressedKey = ToSerialized(NUMSPubKey)
	NUMSScriptKey     = ScriptKey{
		PubKey: NUMSPubKey,
	}

	// ErrUnknownVersion is returned when an asset with an unknown asset
	// version is being used.
	ErrUnknownVersion = errors.New("asset: unknown asset version")
)
View Source
var (
	// ErrTooManyInputs is returned when an asset TLV atempts to reference
	// too many inputs.
	ErrTooManyInputs = errors.New("witnesses: witness elements")

	// ErrByteSliceTooLarge is returned when an encoded byte slice is too
	// large.
	ErrByteSliceTooLarge = errors.New("bytes: too large")
)

Functions

func AssetCommitmentKey

func AssetCommitmentKey(assetID ID, scriptKey *btcec.PublicKey,
	issuanceDisabled bool) [32]byte

AssetCommitmentKey returns a key which can be used to locate an asset within an AssetCommitment that is specific to a particular owner (script key).

NOTE: This function is also used outside the asset package.

func CompressedPubKeyDecoder

func CompressedPubKeyDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func CompressedPubKeyEncoder

func CompressedPubKeyEncoder(w io.Writer, val any, buf *[8]byte) error

func ComputeTaprootScript added in v0.3.0

func ComputeTaprootScript(witnessProgram []byte) ([]byte, error)

ComputeTaprootScript computes the on-chain SegWit v1 script, known as Taproot, based on the given `witnessProgram`.

func DVarBytesWithLimit added in v0.3.0

func DVarBytesWithLimit(limit uint64) tlv.Decoder

func DeriveBurnKey added in v0.3.0

func DeriveBurnKey(firstPrevID PrevID) *btcec.PublicKey

DeriveBurnKey derives a provably un-spendable but unique key by tweaking the public NUMS key with a tap tweak:

burnTweak = h_tapTweak(NUMSKey || outPoint || assetID || scriptKey)
burnKey = NUMSKey + burnTweak*G

The firstPrevID must be the PrevID from the first input that is being spent by the virtual transaction that contains the burn.

func EqualKeyDescriptors

func EqualKeyDescriptors(a, o keychain.KeyDescriptor) bool

EqualKeyDescriptors returns true if the two key descriptors are equal.

func GenesisDecoder

func GenesisDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error

func GenesisEncoder

func GenesisEncoder(w io.Writer, val any, buf *[8]byte) error

func GenesisPrevOutFetcher added in v0.3.0

func GenesisPrevOutFetcher(prevAsset Asset) (*txscript.CannedPrevOutputFetcher,
	error)

GenesisPrevOutFetcher returns a Taproot Asset input's `PrevOutFetcher` to be used throughout signing when the input asset is a genesis grouped asset.

func GroupKeyDecoder

func GroupKeyDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func GroupKeyEncoder

func GroupKeyEncoder(w io.Writer, val any, buf *[8]byte) error

func GroupPubKey added in v0.3.0

func GroupPubKey(rawKey *btcec.PublicKey, singleTweak, tapTweak []byte) (
	*btcec.PublicKey, error)

GroupPubKey derives a tweaked group key from a public key and two tweaks; the single tweak is the asset ID of the group anchor asset, and the tapTweak is the root of a tapscript tree that commits to script-based conditions for reissuing assets as part of this asset group. The tweaked key is defined by:

internalKey = rawKey + singleTweak * G
tweakedGroupKey = TapTweak(internalKey, tapTweak)

func IDDecoder

func IDDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func IDEncoder

func IDEncoder(w io.Writer, val any, buf *[8]byte) error

func InlineVarBytesDecoder added in v0.3.0

func InlineVarBytesDecoder(r io.Reader, val any, buf *[8]byte,
	maxLen uint64) error

func InlineVarBytesEncoder added in v0.3.0

func InlineVarBytesEncoder(w io.Writer, val any, buf *[8]byte) error

func InputGenesisAssetPrevOut added in v0.3.0

func InputGenesisAssetPrevOut(prevAsset Asset) (*wire.TxOut, error)

InputGenesisAssetPrevOut returns a TxOut that represents the input asset in a Taproot Asset virtual TX, but uses tweaked group key of the input asset to enable group witness validation.

func IsBurnKey added in v0.3.0

func IsBurnKey(scriptKey *btcec.PublicKey, witness Witness) bool

IsBurnKey returns true if the given script key is a valid burn key for the given witness.

func IsGroupSig added in v0.3.0

func IsGroupSig(witness wire.TxWitness) (*schnorr.Signature, bool)

IsGroupSig checks if the given witness represents a key path spend of the tweaked group key. Such a witness must include one Schnorr signature, and can include an optional annex (matching the rules specified in BIP-341). If the signature is valid, IsGroupSig returns true and the parsed signature.

func IsSplitCommitWitness added in v0.3.0

func IsSplitCommitWitness(witness Witness) bool

IsSplitCommitWitness returns true if the witness is a split-commitment witness.

func LeafDecoder

func LeafDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func LeafEncoder

func LeafEncoder(w io.Writer, val any, buf *[8]byte) error

func NewLeafAmountRecord

func NewLeafAmountRecord(amount *uint64) tlv.Record

func NewLeafGenesisRecord

func NewLeafGenesisRecord(genesis *Genesis) tlv.Record

func NewLeafGroupKeyRecord

func NewLeafGroupKeyRecord(groupKey **GroupKey) tlv.Record

func NewLeafIDRecord

func NewLeafIDRecord(id *ID) tlv.Record

func NewLeafLockTimeRecord

func NewLeafLockTimeRecord(lockTime *uint64) tlv.Record

func NewLeafPrevWitnessRecord

func NewLeafPrevWitnessRecord(prevWitnesses *[]Witness,
	encodeType EncodeType) tlv.Record

func NewLeafRelativeLockTimeRecord

func NewLeafRelativeLockTimeRecord(relativeLockTime *uint64) tlv.Record

func NewLeafScriptKeyRecord

func NewLeafScriptKeyRecord(scriptKey **btcec.PublicKey) tlv.Record

func NewLeafScriptVersionRecord

func NewLeafScriptVersionRecord(version *ScriptVersion) tlv.Record

func NewLeafSplitCommitmentRootRecord

func NewLeafSplitCommitmentRootRecord(root *mssmt.Node) tlv.Record

func NewLeafTypeRecord

func NewLeafTypeRecord(assetType *Type) tlv.Record

func NewLeafVersionRecord

func NewLeafVersionRecord(version *Version) tlv.Record

func NewWitnessPrevIDRecord

func NewWitnessPrevIDRecord(prevID **PrevID) tlv.Record

func NewWitnessSplitCommitmentRecord

func NewWitnessSplitCommitmentRecord(commitment **SplitCommitment) tlv.Record

func NewWitnessTxWitnessRecord

func NewWitnessTxWitnessRecord(witness *wire.TxWitness) tlv.Record

func OutPointDecoder

func OutPointDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error

func OutPointEncoder

func OutPointEncoder(w io.Writer, val any, buf *[8]byte) error

func ParseGroupSig added in v0.3.0

func ParseGroupSig(witness []byte) (*schnorr.Signature, error)

ParseGroupSig parses a group signature that was stored as a group witness in the DB. It returns an error if the witness is not a single Schnorr signature.

func ParseGroupWitness added in v0.3.0

func ParseGroupWitness(witness []byte) (wire.TxWitness, error)

ParseGroupWitness parses a group witness that was stored as a TLV stream in the DB.

func PrevIDDecoder

func PrevIDDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func PrevIDEncoder

func PrevIDEncoder(w io.Writer, val any, buf *[8]byte) error

func SchnorrSignatureDecoder

func SchnorrSignatureDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func SchnorrSignatureEncoder

func SchnorrSignatureEncoder(w io.Writer, val any, buf *[8]byte) error

func ScriptVersionDecoder

func ScriptVersionDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func ScriptVersionEncoder

func ScriptVersionEncoder(w io.Writer, val any, buf *[8]byte) error

func SerializeGroupWitness added in v0.3.0

func SerializeGroupWitness(witness wire.TxWitness) ([]byte, error)

SerializeGroupWitness serializes a group witness into a TLV stream suitable for storing in the DB.

func SerializedKeyDecoder

func SerializedKeyDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func SerializedKeyEncoder

func SerializedKeyEncoder(w io.Writer, val any, buf *[8]byte) error

func SignOutputRaw added in v0.3.0

func SignOutputRaw(priv *btcec.PrivateKey, tx *wire.MsgTx,
	signDesc *input.SignDescriptor) (*schnorr.Signature, error)

SignOutputRaw creates a signature for a single input. Taken from lnd/lnwallet/btcwallet/signer:L344, SignOutputRaw

func SignVirtualTx added in v0.3.0

func SignVirtualTx(priv *btcec.PrivateKey, signDesc *lndclient.SignDescriptor,
	tx *wire.MsgTx, prevOut *wire.TxOut) (*schnorr.Signature, error)

func SplitCommitmentDecoder

func SplitCommitmentDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func SplitCommitmentEncoder

func SplitCommitmentEncoder(w io.Writer, val any, buf *[8]byte) error

func SplitCommitmentRootDecoder

func SplitCommitmentRootDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func SplitCommitmentRootEncoder

func SplitCommitmentRootEncoder(w io.Writer, val any, buf *[8]byte) error

func TapCommitmentKey

func TapCommitmentKey(assetID ID, groupKey *btcec.PublicKey) [32]byte

TapCommitmentKey is the key that maps to the root commitment for a specific asset group within a TapCommitment.

NOTE: This function is also used outside the asset package.

func TxWitnessDecoder

func TxWitnessDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error

func TxWitnessEncoder

func TxWitnessEncoder(w io.Writer, val any, buf *[8]byte) error

func TypeDecoder

func TypeDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func TypeEncoder

func TypeEncoder(w io.Writer, val any, buf *[8]byte) error

func ValidateAssetName added in v0.3.0

func ValidateAssetName(name string) error

ValidateAssetName validates an asset name (the asset's genesis tag).

func VarIntDecoder

func VarIntDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func VarIntEncoder

func VarIntEncoder(w io.Writer, val any, buf *[8]byte) error

func VersionDecoder

func VersionDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error

func VersionEncoder

func VersionEncoder(w io.Writer, val any, buf *[8]byte) error

func VirtualGenesisTxIn added in v0.3.0

func VirtualGenesisTxIn(newAsset *Asset) (*wire.TxIn, mssmt.Tree, error)

VirtualGenesisTxIn computes the single input of a Taproot Asset virtual transaction that represents a grouped asset genesis. The input prevout's hash is the root of a MS-SMT committing to only the genesis asset.

func VirtualTxInPrevOut added in v0.3.0

func VirtualTxInPrevOut(root mssmt.Node) *wire.OutPoint

VirtualTxInPrevOut returns the prevout of the Taproot Asset virtual transaction's single input as a hash of the root node's key concatenated by its sum.

func VirtualTxWithInput added in v0.3.0

func VirtualTxWithInput(virtualTx *wire.MsgTx, input *Asset,
	idx uint32, witness wire.TxWitness) *wire.MsgTx

VirtualTxWithInput returns a copy of the `virtualTx` amended to include all input-specific details.

This is used to further bind a given witness to the "true" input it spends. We'll use the index of the serialized input to bind the prev index, which represents the "leaf index" of the virtual input MS-SMT.

func WitnessDecoder

func WitnessDecoder(r io.Reader, val any, buf *[8]byte, _ uint64) error

func WitnessEncoder

func WitnessEncoder(w io.Writer, val any, buf *[8]byte) error

func WitnessEncoderWithType added in v0.3.0

func WitnessEncoderWithType(encodeType EncodeType) tlv.Encoder

WitnessEncoderWithType is a wrapper around WitnessEncoder that allows the caller to specify th witness type. It's a higher order function that returns an encoder function.

Types

type Asset

type Asset struct {
	// Version is the Taproot Asset version of the asset.
	Version Version

	// Genesis encodes an asset's genesis metadata which directly maps to
	// its unique ID within the Taproot Asset protocol.
	Genesis

	// Amount is the number of units represented by the asset.
	Amount uint64

	// LockTime, if non-zero, restricts an asset from being moved prior to
	// the represented block height in the chain.
	LockTime uint64

	// RelativeLockTime, if non-zero, restricts an asset from being moved
	// until a number of blocks after the confirmation height of the latest
	// transaction for the asset is reached.
	RelativeLockTime uint64

	// PrevWitnesses contains the witness(es) of an asset's previous
	// transfer.
	PrevWitnesses []Witness

	// SplitCommitmentRoot is the root node of the MS-SMT storing split
	// commitments.
	//
	// NOTE: This should only be set when the previous transfer of an asset
	// resulted in a value split.
	SplitCommitmentRoot mssmt.Node

	// ScriptVersion denotes how an asset's ScriptKey should be validated.
	ScriptVersion ScriptVersion

	// ScriptKey represents a tweaked Taproot output key encumbering the
	// different ways an asset can be spent.
	ScriptKey ScriptKey

	// GroupKey is the tweaked public key that is used to associate assets
	// together across distinct asset IDs, allowing further issuance of the
	// asset to be made possible.
	GroupKey *GroupKey
}

Asset represents a Taproot asset.

func New

func New(genesis Genesis, amount, locktime, relativeLocktime uint64,
	scriptKey ScriptKey, groupKey *GroupKey,
	opts ...NewAssetOpt) (*Asset, error)

New instantiates a new asset with a genesis asset witness.

func NewAssetNoErr added in v0.3.0

func NewAssetNoErr(t testing.TB, gen Genesis, amt, locktime, relocktime uint64,
	scriptKey ScriptKey, groupKey *GroupKey, opts ...NewAssetOpt) *Asset

NewAssetNoErr creates an asset and fails the test if asset creation fails.

func RandAsset

func RandAsset(t testing.TB, assetType Type) *Asset

RandAsset creates a random asset of the given type for testing.

func RandAssetWithValues

func RandAssetWithValues(t testing.TB, genesis Genesis, groupKey *GroupKey,
	scriptKey ScriptKey) *Asset

RandAssetWithValues creates a random asset with the given genesis and keys for testing.

func (*Asset) AssetCommitmentKey

func (a *Asset) AssetCommitmentKey() [32]byte

AssetCommitmentKey is the key that maps to a specific owner of an asset within a Taproot AssetCommitment.

func (*Asset) Copy

func (a *Asset) Copy() *Asset

Copy returns a deep copy of an Asset.

func (*Asset) Decode

func (a *Asset) Decode(r io.Reader) error

Decode decodes an asset from a TLV stream.

func (*Asset) DecodeRecords

func (a *Asset) DecodeRecords() []tlv.Record

DecodeRecords provides all records known for an asset witness for proper decoding.

func (*Asset) DeepEqual

func (a *Asset) DeepEqual(o *Asset) bool

DeepEqual returns true if this asset is equal with the given asset.

func (*Asset) Encode

func (a *Asset) Encode(w io.Writer) error

Encode encodes an asset into a TLV stream. This is used for encoding proof files and state transitions.

func (*Asset) EncodeNoWitness added in v0.3.0

func (a *Asset) EncodeNoWitness(w io.Writer) error

EncodeNoWitness encodes the asset without the witness into a TLV stream. This is used for serializing on an asset as a leaf within a TAP MS-SMT tree. This only applies when the asset version is v1.

func (*Asset) EncodeRecords

func (a *Asset) EncodeRecords() []tlv.Record

EncodeRecords determines the non-nil records to include when encoding an asset at runtime.

func (*Asset) HasGenesisWitness

func (a *Asset) HasGenesisWitness() bool

HasGenesisWitness determines whether an asset has a valid genesis witness, which should only have one input with a zero PrevID and empty witness and split commitment proof.

func (*Asset) HasGenesisWitnessForGroup added in v0.3.0

func (a *Asset) HasGenesisWitnessForGroup() bool

HasGenesisWitnessForGroup determines whether an asset has a witness for a genesis asset in an asset group. This asset must have a non-empty group key and a single prevWitness with a zero PrevID, empty split commitment proof, and non-empty witness.

func (*Asset) HasSplitCommitmentWitness

func (a *Asset) HasSplitCommitmentWitness() bool

HasSplitCommitmentWitness returns true if an asset has a split commitment witness.

func (*Asset) IsBurn added in v0.3.0

func (a *Asset) IsBurn() bool

IsBurn returns true if an asset uses an un-spendable script key that was constructed using the proof-of-burn scheme.

func (*Asset) IsGenesisAsset added in v0.3.0

func (a *Asset) IsGenesisAsset() bool

IsGenesisAsset returns true if an asset is a genesis asset.

func (*Asset) IsUnSpendable

func (a *Asset) IsUnSpendable() bool

IsUnSpendable returns true if an asset uses the un-spendable script key and has zero value.

func (*Asset) IsUnknownVersion added in v0.3.0

func (a *Asset) IsUnknownVersion() bool

IsUnknownVersion returns true if an asset has a version that is not recognized by this implementation of tap.

func (*Asset) Leaf

func (a *Asset) Leaf() (*mssmt.LeafNode, error)

Leaf returns the asset encoded as a MS-SMT leaf node.

func (*Asset) NeedsGenesisWitnessForGroup added in v0.3.0

func (a *Asset) NeedsGenesisWitnessForGroup() bool

NeedsGenesisWitnessForGroup determines whether an asset is a genesis grouped asset, which does not yet have a group witness.

func (*Asset) PrimaryPrevID added in v0.3.0

func (a *Asset) PrimaryPrevID() (*PrevID, error)

PrimaryPrevID returns the primary prev ID of an asset. This is the prev ID of the first witness, unless the first witness is a split-commitment witness, in which case it is the prev ID of the first witness of the root asset. The first witness effectively corresponds to the asset's direct lineage.

func (*Asset) TapCommitmentKey

func (a *Asset) TapCommitmentKey() [32]byte

TapCommitmentKey is the key that maps to the root commitment for a specific asset group within a TapCommitment.

func (*Asset) Validate added in v0.3.0

func (a *Asset) Validate() error

Validate ensures that an asset is valid.

type AssetGroup

type AssetGroup struct {
	*Genesis

	*GroupKey
}

AssetGroup holds information about an asset group, including the genesis information needed re-tweak the raw key.

type BurnTestVectors added in v0.3.0

type BurnTestVectors struct {
	ValidTestCases []*ValidBurnTestCase `json:"valid_test_cases"`
}

type ChainAsset added in v0.3.3

type ChainAsset struct {
	*Asset

	// IsSpent indicates whether the above asset was previously spent.
	IsSpent bool

	// AnchorTx is the transaction that anchors this chain asset.
	AnchorTx *wire.MsgTx

	// AnchorBlockHash is the blockhash that mined the anchor tx.
	AnchorBlockHash chainhash.Hash

	// AnchorBlockHeight is the height of the block that mined the anchor
	// tx.
	AnchorBlockHeight uint32

	// AnchorOutpoint is the outpoint that commits to the asset.
	AnchorOutpoint wire.OutPoint

	// AnchorInternalKey is the raw internal key that was used to create the
	// anchor Taproot output key.
	AnchorInternalKey *btcec.PublicKey

	// AnchorMerkleRoot is the Taproot merkle root hash of the anchor output
	// the asset was committed to. If there is no Tapscript sibling, this is
	// equal to the Taproot Asset root commitment hash.
	AnchorMerkleRoot []byte

	// AnchorTapscriptSibling is the serialized preimage of a Tapscript
	// sibling, if there was one. If this is empty, then the
	// AnchorTapscriptSibling hash is equal to the Taproot root hash of the
	// anchor output.
	AnchorTapscriptSibling []byte

	// AnchorLeaseOwner is the identity of the application that currently
	// has a lease on this UTXO. If empty/nil, then the UTXO is not
	// currently leased. A lease means that the UTXO is being
	// reserved/locked to be spent in an upcoming transaction and that it
	// should not be available for coin selection through any of the wallet
	// RPCs.
	AnchorLeaseOwner [32]byte

	// AnchorLeaseExpiry is the expiry of the lease. If the expiry is nil or
	// the time is in the past, then the lease is not valid and the UTXO is
	// available for coin selection.
	AnchorLeaseExpiry *time.Time
}

ChainAsset is a wrapper around the base asset struct that includes information detailing where in the chain the asset is currently anchored.

type EncodeType added in v0.3.0

type EncodeType uint8

EncodeType is used to denote the type of encoding used for an asset.

const (
	// Encode normal is the normal encoding type for an asset.
	EncodeNormal EncodeType = iota

	// EncodeSegwit denotes that the witness vector field is not be be
	// encoded.
	EncodeSegwit
)

type ErrorTestCase added in v0.3.0

type ErrorTestCase struct {
	Asset   *TestAsset `json:"asset"`
	Error   string     `json:"error"`
	Comment string     `json:"comment"`
}

type Genesis

type Genesis struct {
	// FirstPrevOut represents the outpoint of the transaction's first
	// input that resulted in the creation of the asset.
	//
	// NOTE: This is immutable for the lifetime of the asset.
	FirstPrevOut wire.OutPoint

	// Tag is a human-readable identifier for the asset. This does not need
	// to be unique, but asset issuers should attempt for it to be unique if
	// possible. Users usually recognise this field as the asset's name.
	//
	// NOTE: This is immutable for the lifetime of the asset.
	Tag string

	// MetaHash is the hash of the set of encoded meta data. This value is
	// carried along for all assets transferred in the "light cone" of the
	// genesis asset. The preimage for this field may optionally be
	// revealed within the genesis asset proof for this asset.
	//
	// NOTE: This is immutable for the lifetime of the asset.
	MetaHash [MetaHashLen]byte

	// OutputIndex is the index of the output that carries the unique
	// Taproot Asset commitment in the genesis transaction.
	OutputIndex uint32

	// Type uniquely identifies the type of Taproot asset.
	Type Type
}

Genesis encodes an asset's genesis metadata which directly maps to its unique ID within the Taproot Asset protocol.

func DecodeGenesis

func DecodeGenesis(r io.Reader) (Genesis, error)

DecodeGenesis decodes an asset genesis.

func RandGenesis

func RandGenesis(t testing.TB, assetType Type) Genesis

RandGenesis creates a random genesis for testing.

func (Genesis) Encode

func (g Genesis) Encode(w io.Writer) error

Encode encodes an asset genesis.

func (Genesis) GroupKeyTweak

func (g Genesis) GroupKeyTweak() []byte

GroupKeyTweak returns the tweak bytes that commit to the previous outpoint, output index and type of the genesis.

func (Genesis) ID

func (g Genesis) ID() ID

ID computes an asset's unique identifier from its metadata.

func (Genesis) TagHash

func (g Genesis) TagHash() [sha256.Size]byte

TagHash computes the SHA-256 hash of the asset's tag.

type GenesisSigner

type GenesisSigner 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)
}

GenesisSigner is used to sign the assetID using the group key public key for a given asset.

type GenesisTxBuilder added in v0.3.0

type GenesisTxBuilder interface {
	// BuildGenesisTx constructs a virtual transaction and prevOut that
	// represent the genesis state transition for a grouped asset. This
	// ouput is used to create a group witness for the grouped asset.
	BuildGenesisTx(newAsset *Asset) (*wire.MsgTx, *wire.TxOut, error)
}

GenesisTxBuilder is used to construct the virtual transaction that represents asset minting for grouped assets. This transaction is used to generate a group witness that authorizes the minting of an asset into the asset group.

type GroupKey

type GroupKey struct {
	// RawKey is the raw group key before the tweak with the genesis point
	// has been applied.
	RawKey keychain.KeyDescriptor

	// GroupPubKey is the tweaked public key that is used to associate assets
	// together across distinct asset IDs, allowing further issuance of the
	// asset to be made possible. The tweaked public key is the result of:
	//
	// 	internalKey = rawKey + singleTweak * G
	// 	tweakedGroupKey = TapTweak(internalKey, tapTweak)
	GroupPubKey btcec.PublicKey

	// TapscriptRoot is the root of the Tapscript tree that commits to all
	// script spend conditions for the group key. Instead of spending an
	// asset, these scripts are used to define witnesses more complex than
	// a Schnorr signature for reissuing assets. A group key with an empty
	// Tapscript root can only authorize reissuance with a signature.
	TapscriptRoot []byte

	// Witness is a stack of witness elements that authorizes the membership
	// of an asset in a particular asset group. The witness can be a single
	// signature or a script from the tapscript tree committed to with the
	// TapscriptRoot, and follows the witness rules in BIP-341.
	Witness wire.TxWitness
}

GroupKey is the tweaked public key that is used to associate assets together across distinct asset IDs, allowing further issuance of the asset to be made possible.

func DeriveGroupKey

func DeriveGroupKey(genSigner GenesisSigner, genBuilder GenesisTxBuilder,
	rawKey keychain.KeyDescriptor, initialGen Genesis,
	newAsset *Asset) (*GroupKey, error)

DeriveGroupKey derives an asset's group key based on an internal public key descriptor, the original group asset genesis, and the asset's genesis.

func RandGroupKey

func RandGroupKey(t testing.TB, genesis Genesis, newAsset *Asset) *GroupKey

RandGroupKey creates a random group key for testing.

func RandGroupKeyWithSigner

func RandGroupKeyWithSigner(t testing.TB, genesis Genesis,
	newAsset *Asset) (*GroupKey, []byte)

RandGroupKeyWithSigner creates a random group key for testing, and provides the signer for reissuing assets into the same group.

func (*GroupKey) IsEqual

func (g *GroupKey) IsEqual(otherGroupKey *GroupKey) bool

IsEqual returns true if this group key and signature are exactly equivalent to the passed other group key.

func (*GroupKey) IsEqualGroup

func (g *GroupKey) IsEqualGroup(otherGroupKey *GroupKey) bool

IsEqualGroup returns true if this group key describes the same asset group as the passed other group key.

func (*GroupKey) IsLocal

func (g *GroupKey) IsLocal() bool

IsLocal returns true if the private key that corresponds to this group key is held by this daemon. A non-local group key is stored with the internal key family and index set to their default values, 0.

type GroupKeyReveal added in v0.3.0

type GroupKeyReveal struct {
	// RawKey is the public key that is tweaked twice to derive the final
	// tweaked group key. The final tweaked key is the result of:
	// internalKey = rawKey + singleTweak * G
	// tweakedGroupKey = TapTweak(internalKey, tapTweak)
	RawKey SerializedKey

	// TapscriptRoot is the root of the Tapscript tree that commits to all
	// script spend conditions for the group key. Instead of spending an
	// asset, these scripts are used to define witnesses more complex than
	// a Schnorr signature for reissuing assets. This is either empty/nil or
	// a 32-byte hash.
	TapscriptRoot []byte
}

GroupKeyReveal is a type for representing the data used to derive the tweaked key used to identify an asset group. The final tweaked key is the result of: TapTweak(groupInternalKey, tapscriptRoot)

func (*GroupKeyReveal) GroupPubKey added in v0.3.0

func (g *GroupKeyReveal) GroupPubKey(assetID ID) (*btcec.PublicKey, error)

GroupPubKey returns the group public key derived from the group key reveal.

type ID

type ID [sha256.Size]byte

ID serves as a unique identifier of an asset, resulting from:

sha256(genesisOutPoint || sha256(tag) || sha256(metadata) ||
  outputIndex || assetType)

func RandID

func RandID(t testing.TB) ID

RandID creates a random asset ID.

func (ID) String

func (i ID) String() string

String returns the hex-encoded string representation of the ID.

type LeafTlvType

type LeafTlvType = tlv.Type

LeafTlvType represents the different TLV types for Asset Leaf TLV records.

const (
	LeafVersion             LeafTlvType = 0
	LeafGenesis             LeafTlvType = 2
	LeafType                LeafTlvType = 4
	LeafAmount              LeafTlvType = 6
	LeafLockTime            LeafTlvType = 7
	LeafRelativeLockTime    LeafTlvType = 9
	LeafPrevWitness         LeafTlvType = 11
	LeafSplitCommitmentRoot LeafTlvType = 13
	LeafScriptVersion       LeafTlvType = 14
	LeafScriptKey           LeafTlvType = 16
	LeafGroupKey            LeafTlvType = 17

	// Types for future asset format.
	LeafAssetID LeafTlvType = 11
)

type MockGenesisSigner added in v0.3.0

type MockGenesisSigner struct {
	// contains filtered or unexported fields
}

MockGenesisSigner implements the GenesisSigner interface using a raw private key.

func NewMockGenesisSigner added in v0.3.0

func NewMockGenesisSigner(priv *btcec.PrivateKey) *MockGenesisSigner

NewMockGenesisSigner creates a new MockGenesisSigner instance given the passed public key.

func (*MockGenesisSigner) SignVirtualTx added in v0.3.0

func (r *MockGenesisSigner) SignVirtualTx(signDesc *lndclient.SignDescriptor,
	virtualTx *wire.MsgTx, prevOut *wire.TxOut) (*schnorr.Signature,
	error)

SignVirtualTx generates a signature according to the passed signing descriptor and virtual TX.

type MockGroupTxBuilder added in v0.3.0

type MockGroupTxBuilder struct{}

func (*MockGroupTxBuilder) BuildGenesisTx added in v0.3.0

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

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

type NewAssetOpt added in v0.3.0

type NewAssetOpt func(*newAssetOptions)

NewAssetOpt is used to modify how a new asset is to be created.

func WithAssetVersion added in v0.3.0

func WithAssetVersion(v Version) NewAssetOpt

WithAssetVersion can be used to create an asset with a custom version.

type PrevID

type PrevID struct {
	// OutPoint refers to the asset's previous output position within a
	// transaction.
	OutPoint wire.OutPoint

	// ID is the asset ID of the previous asset tree.
	ID ID

	// ScriptKey is the previously tweaked Taproot output key committing to
	// the possible spending conditions of the asset. PrevID is being used
	// as map keys, so we want to only use data types with fixed and
	// comparable content, which a btcec.PublicKey might not be.
	ScriptKey SerializedKey
}

PrevID serves as a reference to an asset's previous input.

func (PrevID) Hash

func (id PrevID) Hash() [sha256.Size]byte

Hash returns the SHA-256 hash of all items encapsulated by PrevID.

type ScriptKey

type ScriptKey struct {
	// PubKey is the script key that'll be encoded in the final TLV format.
	// All signatures are checked against this script key.
	PubKey *btcec.PublicKey

	*TweakedScriptKey
}

ScriptKey represents a tweaked Taproot output key encumbering the different ways an asset can be spent.

func NewScriptKey

func NewScriptKey(key *btcec.PublicKey) ScriptKey

NewScriptKey constructs a ScriptKey with only the publicly available information. This resulting key may or may not have a tweak applied to it.

func NewScriptKeyBip86

func NewScriptKeyBip86(rawKey keychain.KeyDescriptor) ScriptKey

NewScriptKeyBip86 constructs a ScriptKey tweaked BIP-0086 style. The resulting script key will include the specified BIP-0086 tweak (no real tweak), and also apply that to the final external PubKey.

func RandScriptKey

func RandScriptKey(t testing.TB) ScriptKey

RandScriptKey creates a random script key for testing.

func (ScriptKey) IsUnSpendable

func (s ScriptKey) IsUnSpendable() (bool, error)

IsUnSpendable returns true if this script key is equal to the un-spendable NUMS point.

type ScriptVersion

type ScriptVersion uint16

ScriptVersion denotes the asset script versioning scheme.

const (
	// ScriptV0 represents the initial asset script version of the Taproot
	// Asset protocol. In this version, assets commit to a tweaked Taproot
	// output key, allowing the ability for an asset to indirectly commit to
	// multiple spending conditions.
	ScriptV0 ScriptVersion = 0
)

type SerializedKey

type SerializedKey [33]byte

SerializedKey is a type for representing a public key, serialized in the compressed, 33-byte form.

func RandSerializedKey

func RandSerializedKey(t testing.TB) SerializedKey

RandSerializedKey creates a random serialized key for testing.

func ToSerialized

func ToSerialized(pubKey *btcec.PublicKey) SerializedKey

ToSerialized serializes a public key in its 33-byte compressed form.

func (SerializedKey) CopyBytes

func (s SerializedKey) CopyBytes() []byte

CopyBytes returns a copy of the underlying array as a byte slice.

func (SerializedKey) SchnorrSerialized

func (s SerializedKey) SchnorrSerialized() []byte

SchnorrSerialized returns the Schnorr serialized, x-only 32-byte representation of the serialized key.

func (SerializedKey) ToPubKey added in v0.3.0

func (s SerializedKey) ToPubKey() (*btcec.PublicKey, error)

ToPubKey returns the public key parsed from the serialized key.

type SplitCommitment

type SplitCommitment struct {
	// Proof is the proof for a particular asset split resulting from a
	// split commitment.
	Proof mssmt.Proof

	// RootAsset is the asset containing the root of the split commitment
	// tree from which the `Proof` above was computed from.
	RootAsset Asset
}

SplitCommitment represents the asset witness for an asset split.

func (*SplitCommitment) DeepEqual

func (s *SplitCommitment) DeepEqual(o *SplitCommitment) bool

DeepEqual returns true if this split commitment is equal with the given split commitment.

type TestAsset added in v0.3.0

type TestAsset struct {
	Version             uint8           `json:"version"`
	GenesisFirstPrevOut string          `json:"genesis_first_prev_out"`
	GenesisTag          string          `json:"genesis_tag"`
	GenesisMetaHash     string          `json:"genesis_meta_hash"`
	GenesisOutputIndex  uint32          `json:"genesis_output_index"`
	GenesisType         uint8           `json:"genesis_type"`
	Amount              uint64          `json:"amount"`
	LockTime            uint64          `json:"lock_time"`
	RelativeLockTime    uint64          `json:"relative_lock_time"`
	PrevWitnesses       []*TestWitness  `json:"prev_witnesses"`
	SplitCommitmentRoot *mssmt.TestNode `json:"split_commitment_root"`
	ScriptVersion       uint16          `json:"script_version"`
	ScriptKey           string          `json:"script_key"`
	GroupKey            *TestGroupKey   `json:"group_key"`
}

func NewTestFromAsset added in v0.3.0

func NewTestFromAsset(t testing.TB, a *Asset) *TestAsset

func (*TestAsset) ToAsset added in v0.3.0

func (ta *TestAsset) ToAsset(t testing.TB) *Asset

type TestGenesisReveal added in v0.3.0

type TestGenesisReveal struct {
	FirstPrevOut string `json:"first_prev_out"`
	Tag          string `json:"tag"`
	MetaHash     string `json:"meta_hash"`
	OutputIndex  uint32 `json:"output_index"`
	Type         uint8  `json:"type"`
}

func NewTestFromGenesisReveal added in v0.3.0

func NewTestFromGenesisReveal(t testing.TB, g *Genesis) *TestGenesisReveal

func (*TestGenesisReveal) ToGenesisReveal added in v0.3.0

func (tgr *TestGenesisReveal) ToGenesisReveal(t testing.TB) *Genesis

type TestGroupKey added in v0.3.0

type TestGroupKey struct {
	GroupKey string `json:"group_key"`
}

func NewTestFromGroupKey added in v0.3.0

func NewTestFromGroupKey(t testing.TB, gk *GroupKey) *TestGroupKey

func (*TestGroupKey) ToGroupKey added in v0.3.0

func (tgk *TestGroupKey) ToGroupKey(t testing.TB) *GroupKey

type TestGroupKeyReveal added in v0.3.0

type TestGroupKeyReveal struct {
	RawKey        string `json:"raw_key"`
	TapscriptRoot string `json:"tapscript_root"`
}

func NewTestFromGroupKeyReveal added in v0.3.0

func NewTestFromGroupKeyReveal(t testing.TB,
	gkr *GroupKeyReveal) *TestGroupKeyReveal

func (*TestGroupKeyReveal) ToGroupKeyReveal added in v0.3.0

func (tgkr *TestGroupKeyReveal) ToGroupKeyReveal(t testing.TB) *GroupKeyReveal

type TestPrevID added in v0.3.0

type TestPrevID struct {
	OutPoint  string `json:"out_point"`
	AssetID   string `json:"asset_id"`
	ScriptKey string `json:"script_key"`
}

func NewTestFromPrevID added in v0.3.0

func NewTestFromPrevID(prevID *PrevID) *TestPrevID

func (*TestPrevID) ToPrevID added in v0.3.0

func (tpv *TestPrevID) ToPrevID(t testing.TB) *PrevID

type TestScriptKey added in v0.3.0

type TestScriptKey struct {
}

type TestSplitCommitment added in v0.3.0

type TestSplitCommitment struct {
	Proof     string     `json:"proof"`
	RootAsset *TestAsset `json:"root_asset"`
}

func NewTestFromSplitCommitment added in v0.3.0

func NewTestFromSplitCommitment(t testing.TB,
	sc *SplitCommitment) *TestSplitCommitment

func (*TestSplitCommitment) ToSplitCommitment added in v0.3.0

func (tsc *TestSplitCommitment) ToSplitCommitment(
	t testing.TB) *SplitCommitment

type TestVectors added in v0.3.0

type TestVectors struct {
	ValidTestCases []*ValidTestCase `json:"valid_test_cases"`
	ErrorTestCases []*ErrorTestCase `json:"error_test_cases"`
}

type TestWitness added in v0.3.0

type TestWitness struct {
	PrevID          *TestPrevID          `json:"prev_id"`
	TxWitness       []string             `json:"tx_witness"`
	SplitCommitment *TestSplitCommitment `json:"split_commitment"`
}

func NewTestFromWitness added in v0.3.0

func NewTestFromWitness(t testing.TB, w Witness) *TestWitness

func (*TestWitness) ToWitness added in v0.3.0

func (tw *TestWitness) ToWitness(t testing.TB) Witness

type TweakedScriptKey

type TweakedScriptKey struct {
	// RawKey is the raw script key before the script key tweak is applied.
	// We store a full key descriptor here for wallet purposes, but will
	// only encode the pubkey above for the normal script leaf TLV
	// encoding.
	RawKey keychain.KeyDescriptor

	// Tweak is the tweak that is applied on the raw script key to get the
	// public key. If this is nil, then a BIP-0086 tweak is assumed.
	Tweak []byte
}

TweakedScriptKey is an embedded struct which is primarily used by wallets to be able to keep track of the tweak of a script key along side the raw key derivation information.

type Type

type Type uint8

Type denotes the asset types supported by the Taproot Asset protocol.

const (
	// Normal is an asset that can be represented in multiple units,
	// resembling a divisible asset.
	Normal Type = 0

	// Collectible is a unique asset, one that cannot be represented in
	// multiple units.
	Collectible Type = 1
)

func (Type) String

func (t Type) String() string

String returns a human-readable description of the type.

type ValidBurnTestCase added in v0.3.0

type ValidBurnTestCase struct {
	PrevID   *TestPrevID `json:"prev_id"`
	Expected string      `json:"expected"`
	Comment  string      `json:"comment"`
}

type ValidTestCase added in v0.3.0

type ValidTestCase struct {
	Asset    *TestAsset `json:"asset"`
	Expected string     `json:"expected"`
	Comment  string     `json:"comment"`
}

type Version

type Version uint8

Version denotes the version of the Taproot Asset protocol in effect for an asset.

const (
	// V0 is the initial Taproot Asset protocol version.
	V0 Version = 0

	// V1 is the version of asset serialization that doesn't include the
	// witness field when creating a TAP commitment. This is similar to
	// segwit as found in Bitcoin land.
	V1 Version = 1
)

type Witness

type Witness struct {
	// PrevID is a reference to an asset's previous input.
	//
	// NOTE: This should only be nil upon the creation of an asset.
	PrevID *PrevID

	// TxWitness is a witness that satisfies the asset's previous ScriptKey.
	//
	// NOTE: This field and `SplitCommitmentProof` are mutually exclusive,
	// except upon the creation of an asset, where both should be nil.
	TxWitness wire.TxWitness

	// SplitCommitmentProof is used to permit the spending of an asset UTXO
	// created as a result of an asset split. When an asset is split, the
	// non-change UTXO commits to the location of all other splits within an
	// MS-SMT tree. When spending a change UTXO resulting from a
	// `SplitCommitment`, a normal `Witness` isn't required, instead the
	// owner of the change asset UTXO must prove that it holds a valid split
	// which was authorized by the main transfer transaction.
	//
	// Outputs with the same `SplitCommitment` are said to share a single
	// `Witness` as such outputs are the result of a new asset split.
	// Therefore, we only need a single witness and the resulting merkle-sum
	// asset tree to verify a transfer.
	//
	// NOTE: This field and `TxWitness` are mutually exclusive,
	// except upon the creation of an asset, where both should be nil.
	//
	// TODO: This still needs to be specified further in the BIPs, see
	// https://github.com/lightninglabs/taproot-assets/issues/3.
	SplitCommitment *SplitCommitment
}

Witness is a nested TLV stream within the main Asset TLV stream that contains the necessary data to verify the movement of an asset. All fields should be nil to represent the creation of an asset, `TxWitness` and `SplitCommitmentProof` are mutually exclusive otherwise.

func (*Witness) Decode

func (w *Witness) Decode(r io.Reader) error

Decode decodes an asset witness from a TLV stream.

func (*Witness) DecodeRecords

func (w *Witness) DecodeRecords() []tlv.Record

DecodeRecords provides all records known for an asset witness for proper decoding.

func (*Witness) DeepEqual

func (w *Witness) DeepEqual(o *Witness) bool

DeepEqual returns true if this witness is equal with the given witness.

func (*Witness) Encode

func (w *Witness) Encode(writer io.Writer) error

Encode encodes an asset witness into a TLV stream.

func (*Witness) EncodeNoWitness added in v0.3.0

func (w *Witness) EncodeNoWitness(writer io.Writer) error

EncodeNoWitness encodes an asset witness into a TLV stream, but does not include the raw witness field. The prevID and the split commitment are still included.

func (*Witness) EncodeRecords

func (w *Witness) EncodeRecords() []tlv.Record

EncodeRecords determines the non-nil records to include when encoding an asset witness at runtime.

type WitnessTlvType

type WitnessTlvType = tlv.Type

WitnessTlvType represents the different TLV types for Asset Witness TLV records.

const (
	WitnessPrevID          WitnessTlvType = 1
	WitnessTxWitness       WitnessTlvType = 3
	WitnessSplitCommitment WitnessTlvType = 5
)

Jump to

Keyboard shortcuts

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