@@ -2,6 +2,7 @@ package tappsbt
22
33import (
44 "bytes"
5+ "errors"
56 "fmt"
67 "net/url"
78
7980
8081 // ErrInvalidVPacketVersion is an error returned when a VPacket version
8182 // is invalid.
82- ErrInvalidVPacketVersion = fmt .Errorf ("tappsbt: invalid version" )
83+ ErrInvalidVPacketVersion = errors .New ("tappsbt: invalid version" )
84+
85+ // ErrAltLeavesAlreadySet is returned when a VInput or VOutput already
86+ // has some AltLeaves set, but a call was made to set the AltLeaves
87+ // again.
88+ ErrAltLeavesAlreadySet = errors .New ("tappsbt: alt leaves already set" )
8389)
8490
8591// VPacketVersion is the version of the virtual transaction. This can signal
@@ -419,6 +425,27 @@ func (i *VInput) Asset() *asset.Asset {
419425 return i .asset
420426}
421427
428+ // SetAltLeaves asserts that a set of AltLeaves are valid, and updates a VInput
429+ // to set the AltLeaves. Setting the input's AltLeaves twice is disallowed.
430+ func (i * VInput ) SetAltLeaves (altLeafAssets []* asset.Asset ) error {
431+ // AltLeaves can be set exactly once on a VInput.
432+ if len (i .AltLeaves ) != 0 {
433+ return fmt .Errorf ("%w: input" , ErrAltLeavesAlreadySet )
434+ }
435+
436+ // Each asset must be a valid AltLeaf, and the set of AltLeaves must be
437+ // valid, by not having overlapping keys in the AltCommitment.
438+ altLeaves := asset .ToAltLeaves (altLeafAssets )
439+ err := asset .ValidAltLeaves (altLeaves )
440+ if err != nil {
441+ return err
442+ }
443+
444+ i .AltLeaves = asset .CopyAltLeaves (altLeaves )
445+
446+ return nil
447+ }
448+
422449// serializeScriptKey serializes the input asset's script key as the PSBT
423450// derivation information on the virtual input.
424451func (i * VInput ) serializeScriptKey (key asset.ScriptKey , coinType uint32 ) {
@@ -657,6 +684,27 @@ func (o *VOutput) SetAnchorInternalKey(keyDesc keychain.KeyDescriptor,
657684 )
658685}
659686
687+ // SetAltLeaves asserts that a set of AltLeaves are valid, and updates a VOutput
688+ // to set the AltLeaves. Setting the output's AltLeaves twice is disallowed.
689+ func (o * VOutput ) SetAltLeaves (altLeafAssets []* asset.Asset ) error {
690+ // AltLeaves can be set exactly once on a VOutput.
691+ if len (o .AltLeaves ) != 0 {
692+ return fmt .Errorf ("%w: output" , ErrAltLeavesAlreadySet )
693+ }
694+
695+ // Each asset must be a valid AltLeaf, and the set of AltLeaves must be
696+ // valid, by not having overlapping keys in the AltCommitment.
697+ altLeaves := asset .ToAltLeaves (altLeafAssets )
698+ err := asset .ValidAltLeaves (altLeaves )
699+ if err != nil {
700+ return err
701+ }
702+
703+ o .AltLeaves = asset .CopyAltLeaves (altLeaves )
704+
705+ return nil
706+ }
707+
660708// AnchorKeyToDesc attempts to extract the key descriptor of the anchor output
661709// from the anchor output BIP-0032 derivation information.
662710func (o * VOutput ) AnchorKeyToDesc () (keychain.KeyDescriptor , error ) {
0 commit comments