Skip to content

Commit e5d2aa4

Browse files
committed
multi: refactor to split off tapsend package from tapscript
We have the following weird package dependency chain: proof -> vm -> tapscript -> tappsbt This makes things weird and cumbersome. The only parts in tapscript that actually use the tappsbt stuff is send related. So to break that dependency chain between the vm and tappsbt, we split off all the send functionality into a new tapsend package.
1 parent c35215a commit e5d2aa4

File tree

12 files changed

+284
-281
lines changed

12 files changed

+284
-281
lines changed

itest/psbt_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
2121
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
2222
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
23-
"github.com/lightninglabs/taproot-assets/tapscript"
23+
"github.com/lightninglabs/taproot-assets/tapsend"
2424
"github.com/lightningnetwork/lnd/input"
2525
"github.com/lightningnetwork/lnd/keychain"
2626
"github.com/lightningnetwork/lnd/lntest/wait"
@@ -1391,7 +1391,7 @@ func testPsbtSighashNone(t *harnessTest) {
13911391
witnessBackup := signedPacket.Outputs[0].Asset.PrevWitnesses
13921392

13931393
// Bob now creates the output assets.
1394-
err = tapscript.PrepareOutputAssets(context.Background(), signedPacket)
1394+
err = tapsend.PrepareOutputAssets(context.Background(), signedPacket)
13951395
require.NoError(t.t, err)
13961396

13971397
// We attach the backed-up Previous Witnesses to the newly created
@@ -1564,7 +1564,7 @@ func testPsbtSighashNoneInvalid(t *harnessTest) {
15641564
witnessBackup := signedPacket.Outputs[0].Asset.PrevWitnesses
15651565

15661566
// Bob now creates the output assets.
1567-
err = tapscript.PrepareOutputAssets(context.Background(), signedPacket)
1567+
err = tapsend.PrepareOutputAssets(context.Background(), signedPacket)
15681568
require.NoError(t.t, err)
15691569

15701570
// We attach the backed-up Previous Witnesses to the newly created

log.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/lightninglabs/taproot-assets/tapdb"
1010
"github.com/lightninglabs/taproot-assets/tapfreighter"
1111
"github.com/lightninglabs/taproot-assets/tapgarden"
12-
"github.com/lightninglabs/taproot-assets/tapscript"
12+
"github.com/lightninglabs/taproot-assets/tapsend"
1313
"github.com/lightninglabs/taproot-assets/universe"
1414
"github.com/lightningnetwork/lnd/build"
1515
"github.com/lightningnetwork/lnd/signal"
@@ -99,9 +99,7 @@ func SetupLoggers(root *build.RotatingLogWriter, interceptor signal.Interceptor)
9999
AddSubLogger(root, proof.Subsystem, interceptor, proof.UseLogger)
100100
AddSubLogger(root, tapdb.Subsystem, interceptor, tapdb.UseLogger)
101101
AddSubLogger(root, address.Subsystem, interceptor, address.UseLogger)
102-
AddSubLogger(
103-
root, tapscript.Subsystem, interceptor, tapscript.UseLogger,
104-
)
102+
AddSubLogger(root, tapsend.Subsystem, interceptor, tapsend.UseLogger)
105103
AddSubLogger(root, universe.Subsystem, interceptor, universe.UseLogger)
106104
AddSubLogger(
107105
root, commitment.Subsystem, interceptor, commitment.UseLogger,

rpcserver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ import (
3838
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
3939
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
4040
"github.com/lightninglabs/taproot-assets/tapscript"
41+
"github.com/lightninglabs/taproot-assets/tapsend"
4142
"github.com/lightninglabs/taproot-assets/universe"
4243
"github.com/lightningnetwork/lnd/build"
4344
"github.com/lightningnetwork/lnd/keychain"
@@ -1675,7 +1676,7 @@ func (r *rpcServer) FundVirtualPsbt(ctx context.Context,
16751676
// Extract the recipient information from the packet. This
16761677
// basically assembles the asset ID we want to send to and the
16771678
// sum of all output amounts.
1678-
desc, err := tapscript.DescribeRecipients(
1679+
desc, err := tapsend.DescribeRecipients(
16791680
ctx, vPkt, r.cfg.TapAddrBook,
16801681
)
16811682
if err != nil {
@@ -2349,7 +2350,7 @@ func (r *rpcServer) BurnAsset(ctx context.Context,
23492350
in.AmountToBurn)
23502351

23512352
fundResp, err := r.cfg.AssetWallet.FundBurn(
2352-
ctx, &tapscript.FundingDescriptor{
2353+
ctx, &tapsend.FundingDescriptor{
23532354
ID: assetID,
23542355
GroupKey: groupKey,
23552356
Amount: in.AmountToBurn,

tapfreighter/chain_porter.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/lightninglabs/taproot-assets/tapgarden"
2020
"github.com/lightninglabs/taproot-assets/tappsbt"
2121
"github.com/lightninglabs/taproot-assets/tapscript"
22+
"github.com/lightninglabs/taproot-assets/tapsend"
2223
"github.com/lightningnetwork/lnd/chainntnfs"
2324
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
2425
)
@@ -830,7 +831,7 @@ func (p *ChainPorter) importLocalAddresses(ctx context.Context,
830831
func createDummyOutput() *wire.TxOut {
831832
// The dummy PkScript is the same size as an encoded P2TR output.
832833
newOutput := wire.TxOut{
833-
Value: int64(tapscript.DummyAmtSats),
834+
Value: int64(tapsend.DummyAmtSats),
834835
PkScript: make([]byte, 34),
835836
}
836837
return &newOutput
@@ -923,7 +924,7 @@ func (p *ChainPorter) stateStep(currentPkg sendPackage) (*sendPackage, error) {
923924

924925
default:
925926
feeRate, err = p.cfg.ChainBridge.EstimateFee(
926-
ctx, tapscript.SendConfTarget,
927+
ctx, tapsend.SendConfTarget,
927928
)
928929
if err != nil {
929930
return nil, fmt.Errorf("unable to estimate "+

tapfreighter/wallet.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/lightninglabs/taproot-assets/tapgarden"
3030
"github.com/lightninglabs/taproot-assets/tappsbt"
3131
"github.com/lightninglabs/taproot-assets/tapscript"
32+
"github.com/lightninglabs/taproot-assets/tapsend"
3233
"github.com/lightningnetwork/lnd/keychain"
3334
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
3435
)
@@ -106,13 +107,13 @@ type Wallet interface {
106107
// FundPacket funds a virtual transaction, selecting assets to spend
107108
// in order to pay the given recipient. The selected input is then added
108109
// to the given virtual transaction.
109-
FundPacket(ctx context.Context, fundDesc *tapscript.FundingDescriptor,
110+
FundPacket(ctx context.Context, fundDesc *tapsend.FundingDescriptor,
110111
vPkt *tappsbt.VPacket) (*FundedVPacket, error)
111112

112113
// FundBurn funds a virtual transaction for burning the given amount of
113114
// units of the given asset.
114115
FundBurn(ctx context.Context,
115-
fundDesc *tapscript.FundingDescriptor) (*FundedVPacket, error)
116+
fundDesc *tapsend.FundingDescriptor) (*FundedVPacket, error)
116117

117118
// SignVirtualPacket signs the virtual transaction of the given packet
118119
// and returns the input indexes that were signed.
@@ -441,7 +442,7 @@ func (f *AssetWallet) FundAddressSend(ctx context.Context,
441442
"transaction from addresses: %w", err)
442443
}
443444

444-
fundDesc, err := tapscript.DescribeAddrs(receiverAddrs)
445+
fundDesc, err := tapsend.DescribeAddrs(receiverAddrs)
445446
if err != nil {
446447
return nil, nil, fmt.Errorf("unable to describe recipients: "+
447448
"%w", err)
@@ -522,7 +523,7 @@ func (f *AssetWallet) passiveAssetVPacket(passiveAsset *asset.Asset,
522523
// pay the given recipient. The selected input is then added to the given
523524
// virtual transaction.
524525
func (f *AssetWallet) FundPacket(ctx context.Context,
525-
fundDesc *tapscript.FundingDescriptor,
526+
fundDesc *tapsend.FundingDescriptor,
526527
vPkt *tappsbt.VPacket) (*FundedVPacket, error) {
527528

528529
// The input and address networks must match.
@@ -551,7 +552,7 @@ func (f *AssetWallet) FundPacket(ctx context.Context,
551552
// FundBurn funds a virtual transaction for burning the given amount of units of
552553
// the given asset.
553554
func (f *AssetWallet) FundBurn(ctx context.Context,
554-
fundDesc *tapscript.FundingDescriptor) (*FundedVPacket, error) {
555+
fundDesc *tapsend.FundingDescriptor) (*FundedVPacket, error) {
555556

556557
// We need to find a commitment that has enough assets to satisfy this
557558
// send request. We'll map the address to a set of constraints, so we
@@ -702,7 +703,7 @@ func (f *AssetWallet) FundBurn(ctx context.Context,
702703

703704
// fundPacketWithInputs funds a virtual transaction with the given inputs.
704705
func (f *AssetWallet) fundPacketWithInputs(ctx context.Context,
705-
fundDesc *tapscript.FundingDescriptor, vPkt *tappsbt.VPacket,
706+
fundDesc *tapsend.FundingDescriptor, vPkt *tappsbt.VPacket,
706707
selectedCommitments []*AnchoredCommitment) (*FundedVPacket, error) {
707708

708709
log.Infof("Selected %v asset inputs for send of %d to %x",
@@ -746,7 +747,7 @@ func (f *AssetWallet) fundPacketWithInputs(ctx context.Context,
746747
},
747748
)
748749

749-
fullValue, err := tapscript.ValidateInputs(
750+
fullValue, err := tapsend.ValidateInputs(
750751
inputCommitments, inputsScriptKeys, assetType, fundDesc,
751752
)
752753
if err != nil {
@@ -914,7 +915,7 @@ func (f *AssetWallet) fundPacketWithInputs(ctx context.Context,
914915
)
915916
}
916917

917-
if err := tapscript.PrepareOutputAssets(ctx, vPkt); err != nil {
918+
if err := tapsend.PrepareOutputAssets(ctx, vPkt); err != nil {
918919
return nil, fmt.Errorf("unable to create split commit: %w", err)
919920
}
920921

@@ -968,7 +969,7 @@ func (f *AssetWallet) setVPacketInputs(ctx context.Context,
968969
// to be in the commitment we spend (we did the same when
969970
// creating the output, so differences should be apparent when
970971
// debugging).
971-
tapscript.LogCommitment(
972+
tapsend.LogCommitment(
972973
"Input", idx, assetInput.Commitment, internalKey.PubKey,
973974
anchorPkScript, anchorMerkleRoot[:],
974975
)
@@ -1106,7 +1107,7 @@ func (f *AssetWallet) SignVirtualPacket(vPkt *tappsbt.VPacket,
11061107
// Now we'll use the signer to sign all the inputs for the new Taproot
11071108
// Asset leaves. The witness data for each input will be assigned for
11081109
// us.
1109-
err := tapscript.SignVirtualTransaction(
1110+
err := tapsend.SignVirtualTransaction(
11101111
vPkt, f.cfg.Signer, f.cfg.WitnessValidator,
11111112
)
11121113
if err != nil {
@@ -1382,7 +1383,7 @@ func (f *AssetWallet) AnchorVirtualTransactions(ctx context.Context,
13821383
}
13831384
vPacket := params.VPkts[0]
13841385

1385-
outputCommitments, err := tapscript.CreateOutputCommitments(
1386+
outputCommitments, err := tapsend.CreateOutputCommitments(
13861387
params.InputCommitments, vPacket, params.PassiveAssetsVPkts,
13871388
)
13881389
if err != nil {
@@ -1392,7 +1393,7 @@ func (f *AssetWallet) AnchorVirtualTransactions(ctx context.Context,
13921393

13931394
// Construct our template PSBT to commits to the set of dummy locators
13941395
// we use to make fee estimation work.
1395-
sendPacket, err := tapscript.CreateAnchorTx(vPacket.Outputs)
1396+
sendPacket, err := tapsend.CreateAnchorTx(vPacket.Outputs)
13961397
if err != nil {
13971398
return nil, fmt.Errorf("error creating anchor TX: %w", err)
13981399
}
@@ -1428,7 +1429,7 @@ func (f *AssetWallet) AnchorVirtualTransactions(ctx context.Context,
14281429

14291430
// First, we'll update the PSBT packets to insert the _real_ outputs we
14301431
// need to commit to the asset transfer.
1431-
mergedCommitments, err := tapscript.UpdateTaprootOutputKeys(
1432+
mergedCommitments, err := tapsend.UpdateTaprootOutputKeys(
14321433
signAnchorPkt, vPacket, outputCommitments,
14331434
)
14341435
if err != nil {
@@ -1508,7 +1509,7 @@ func (f *AssetWallet) SignOwnershipProof(
15081509
vPkt := tappsbt.OwnershipProofPacket(
15091510
ownedAsset.Copy(), f.cfg.ChainParams,
15101511
)
1511-
err := tapscript.SignVirtualTransaction(
1512+
err := tapsend.SignVirtualTransaction(
15121513
vPkt, f.cfg.Signer, f.cfg.WitnessValidator,
15131514
)
15141515
if err != nil {

tapgarden/caretaker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"github.com/lightninglabs/taproot-assets/commitment"
2222
"github.com/lightninglabs/taproot-assets/fn"
2323
"github.com/lightninglabs/taproot-assets/proof"
24-
"github.com/lightninglabs/taproot-assets/tapscript"
24+
"github.com/lightninglabs/taproot-assets/tapsend"
2525
"github.com/lightninglabs/taproot-assets/universe"
2626
"github.com/lightningnetwork/lnd/chainntnfs"
2727
"github.com/lightningnetwork/lnd/lnwallet/chainfee"
@@ -394,7 +394,7 @@ func (b *BatchCaretaker) fundGenesisPsbt(ctx context.Context) (*FundedPsbt, erro
394394
b.batchKey[:])
395395

396396
txTemplate := wire.NewMsgTx(2)
397-
txTemplate.AddTxOut(tapscript.CreateDummyOutput())
397+
txTemplate.AddTxOut(tapsend.CreateDummyOutput())
398398
genesisPkt, err := psbt.NewFromUnsignedTx(txTemplate)
399399
if err != nil {
400400
return nil, fmt.Errorf("unable to make psbt packet: %w", err)

tapgarden/mock.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"encoding/hex"
77
"fmt"
8+
"github.com/lightninglabs/taproot-assets/tapsend"
89
"math/rand"
910
"testing"
1011
"time"
@@ -22,7 +23,6 @@ import (
2223
"github.com/lightninglabs/taproot-assets/asset"
2324
"github.com/lightninglabs/taproot-assets/internal/test"
2425
"github.com/lightninglabs/taproot-assets/proof"
25-
"github.com/lightninglabs/taproot-assets/tapscript"
2626
"github.com/lightningnetwork/lnd/chainntnfs"
2727
"github.com/lightningnetwork/lnd/keychain"
2828
"github.com/lightningnetwork/lnd/lnwallet"
@@ -108,7 +108,7 @@ func (m *MockWalletAnchor) FundPsbt(_ context.Context, packet *psbt.Packet,
108108
anchorInput := psbt.PInput{
109109
WitnessUtxo: &wire.TxOut{
110110
Value: 100000,
111-
PkScript: bytes.Clone(tapscript.GenesisDummyScript),
111+
PkScript: bytes.Clone(tapsend.GenesisDummyScript),
112112
},
113113
SighashType: txscript.SigHashDefault,
114114
}
@@ -118,7 +118,7 @@ func (m *MockWalletAnchor) FundPsbt(_ context.Context, packet *psbt.Packet,
118118
// exclusion proofs.
119119
changeOutput := wire.TxOut{
120120
Value: 50000,
121-
PkScript: bytes.Clone(tapscript.GenesisDummyScript),
121+
PkScript: bytes.Clone(tapsend.GenesisDummyScript),
122122
}
123123
changeOutput.PkScript[0] = txscript.OP_0
124124
packet.UnsignedTx.AddTxOut(&changeOutput)

tapgarden/planter_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"database/sql"
77
"encoding/hex"
88
"fmt"
9+
"github.com/lightninglabs/taproot-assets/tapsend"
910
"math/rand"
1011
"sync"
1112
"testing"
@@ -457,7 +458,7 @@ func (t *mintingTestHarness) assertGenesisTxFunded() *tapgarden.FundedPsbt {
457458
if txOut.Value == int64(tapgarden.GenesisAmtSats) {
458459
isP2TR := txscript.IsPayToTaproot(txOut.PkScript)
459460
isDummyScript := bytes.Equal(
460-
txOut.PkScript, tapscript.GenesisDummyScript[:],
461+
txOut.PkScript, tapsend.GenesisDummyScript[:],
461462
)
462463

463464
if isP2TR || isDummyScript {

0 commit comments

Comments
 (0)