Skip to content

Commit d37b486

Browse files
authored
Merge pull request #799 from lightninglabs/prepare-refactor
[preparation 1/2]: refactor to simplify send logic, prepare for new RPCs
2 parents c35215a + a2a95e7 commit d37b486

32 files changed

+1289
-796
lines changed

address/encoding.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ func compressedPubKeyDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error
4242
)
4343
}
4444

45-
// urlEncoder encodes a url.URL as a variable length byte slice.
46-
func urlEncoder(w io.Writer, val any, buf *[8]byte) error {
45+
// UrlEncoder encodes a url.URL as a variable length byte slice.
46+
func UrlEncoder(w io.Writer, val any, buf *[8]byte) error {
4747
if t, ok := val.(*url.URL); ok {
4848
addrBytes := []byte((*t).String())
4949
return tlv.EVarBytes(w, &addrBytes, buf)
5050
}
5151
return tlv.NewTypeForEncodingErr(val, "*url.URL")
5252
}
5353

54-
// urlDecoder decodes a variable length byte slice as an url.URL.
55-
func urlDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
54+
// UrlDecoder decodes a variable length byte slice as an url.URL.
55+
func UrlDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
5656
if t, ok := val.(*url.URL); ok {
5757
var addrBytes []byte
5858
err := tlv.DVarBytes(r, &addrBytes, buf, l)
@@ -68,9 +68,7 @@ func urlDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
6868

6969
return nil
7070
}
71-
return tlv.NewTypeForDecodingErr(
72-
val, "*url.URL", l, l,
73-
)
71+
return tlv.NewTypeForDecodingErr(val, "*url.URL", l, l)
7472
}
7573

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

address/records.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,6 @@ func newProofCourierAddrRecord(addr *url.URL) tlv.Record {
115115

116116
return tlv.MakeDynamicRecord(
117117
addrProofCourierAddrType, addr, recordSize,
118-
urlEncoder, urlDecoder,
118+
UrlEncoder, UrlDecoder,
119119
)
120120
}

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

itest/tapd_harness.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/hex"
77
"flag"
88
"fmt"
9+
"net"
910
"os"
1011
"path/filepath"
1112
"sync"
@@ -305,10 +306,18 @@ func (hs *tapdHarness) start(expectErrExit bool) error {
305306
}
306307
}()
307308

308-
time.Sleep(1 * time.Second)
309+
// Let's wait until the RPC server is actually listening before we
310+
// connect our client to it.
311+
listenerAddr := hs.clientCfg.RpcConf.RawRPCListeners[0]
312+
err = wait.NoError(func() error {
313+
_, err := net.Dial("tcp", listenerAddr)
314+
return err
315+
}, defaultTimeout)
316+
if err != nil {
317+
return fmt.Errorf("error waiting for server to start: %v", err)
318+
}
309319

310320
// Create our client to interact with the tapd RPC server directly.
311-
listenerAddr := hs.clientCfg.RpcConf.RawRPCListeners[0]
312321
rpcConn, err := dialServer(
313322
listenerAddr, hs.clientCfg.RpcConf.TLSCertPath,
314323
hs.clientCfg.RpcConf.MacaroonPath,

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,

proof/archive_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,17 +292,17 @@ func TestMigrateOldFileNames(t *testing.T) {
292292
scriptKey2 := test.RandPubKey(t)
293293

294294
// We create 4 different proofs with the old naming scheme.
295-
proof1 := randomProof(t, genesis1, scriptKey1, oddTxBlock, 0, 1)
295+
proof1 := RandProof(t, genesis1, scriptKey1, oddTxBlock, 0, 1)
296296
storeProofOldName(proof1)
297-
proof2 := randomProof(t, genesis1, scriptKey2, oddTxBlock, 0, 1)
297+
proof2 := RandProof(t, genesis1, scriptKey2, oddTxBlock, 0, 1)
298298
storeProofOldName(proof2)
299-
proof3 := randomProof(t, genesis2, scriptKey1, oddTxBlock, 1, 1)
299+
proof3 := RandProof(t, genesis2, scriptKey1, oddTxBlock, 1, 1)
300300
storeProofOldName(proof3)
301-
proof4 := randomProof(t, genesis2, scriptKey2, oddTxBlock, 1, 1)
301+
proof4 := RandProof(t, genesis2, scriptKey2, oddTxBlock, 1, 1)
302302
storeProofOldName(proof4)
303303

304304
// We also create a proof with the new naming scheme.
305-
proof5 := randomProof(t, genesis1, scriptKey1, oddTxBlock, 1, 1)
305+
proof5 := RandProof(t, genesis1, scriptKey1, oddTxBlock, 1, 1)
306306
storeProofNewName(proof5)
307307

308308
// We now create the file archive and expect the 4 proofs to be renamed.
@@ -322,7 +322,7 @@ func TestMigrateOldFileNames(t *testing.T) {
322322

323323
// We should be able to import a new proof, and it should be stored
324324
// under the new naming scheme.
325-
proof6 := randomProof(t, genesis2, scriptKey2, oddTxBlock, 2, 1)
325+
proof6 := RandProof(t, genesis2, scriptKey2, oddTxBlock, 2, 1)
326326
err = fileArchive.ImportProofs(nil, nil, nil, false, &AnnotatedProof{
327327
Locator: Locator{
328328
AssetID: fn.Ptr(proof6.Asset.ID()),

proof/courier_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestUniverseRpcCourierLocalArchiveShortCut(t *testing.T) {
6464

6565
genesis := asset.RandGenesis(t, asset.Collectible)
6666
scriptKey := test.RandPubKey(t)
67-
proof := randomProof(t, genesis, scriptKey, oddTxBlock, 0, 1)
67+
proof := RandProof(t, genesis, scriptKey, oddTxBlock, 0, 1)
6868

6969
file, err := NewFile(V0, proof, proof)
7070
require.NoError(t, err)

proof/mint.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ func (p *BaseProofParams) HaveExclusionProof(anchorOutputIndex uint32) bool {
152152
return false
153153
}
154154

155+
// HaveInclusionProof returns true if the inclusion proof is for the given
156+
// anchor output index.
157+
func (p *BaseProofParams) HaveInclusionProof(anchorOutputIndex uint32) bool {
158+
return p.OutputIndex == int(anchorOutputIndex)
159+
}
160+
155161
// MintParams holds the set of chain level information needed to make a proof
156162
// file for the set of assets minted in a batch.
157163
type MintParams struct {

proof/mock.go

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,139 @@ import (
1111
"time"
1212

1313
"github.com/btcsuite/btcd/btcec/v2"
14+
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1415
"github.com/btcsuite/btcd/chaincfg/chainhash"
16+
"github.com/btcsuite/btcd/txscript"
1517
"github.com/btcsuite/btcd/wire"
1618
"github.com/lightninglabs/taproot-assets/asset"
1719
"github.com/lightninglabs/taproot-assets/commitment"
1820
"github.com/lightninglabs/taproot-assets/fn"
1921
"github.com/lightninglabs/taproot-assets/internal/test"
22+
"github.com/lightningnetwork/lnd/keychain"
2023
"github.com/stretchr/testify/require"
2124
)
2225

26+
func RandProof(t testing.TB, genesis asset.Genesis,
27+
scriptKey *btcec.PublicKey, block wire.MsgBlock, txIndex int,
28+
outputIndex uint32) Proof {
29+
30+
txMerkleProof, err := NewTxMerkleProof(block.Transactions, txIndex)
31+
require.NoError(t, err)
32+
33+
tweakedScriptKey := asset.NewScriptKey(scriptKey)
34+
protoAsset := asset.NewAssetNoErr(
35+
t, genesis, 1, 0, 0, tweakedScriptKey, nil,
36+
)
37+
groupKey := asset.RandGroupKey(t, genesis, protoAsset)
38+
groupReveal := asset.GroupKeyReveal{
39+
RawKey: asset.ToSerialized(&groupKey.GroupPubKey),
40+
TapscriptRoot: test.RandBytes(32),
41+
}
42+
43+
amount := uint64(1)
44+
mintCommitment, assets, err := commitment.Mint(
45+
genesis, groupKey, &commitment.AssetDetails{
46+
Type: genesis.Type,
47+
ScriptKey: test.PubToKeyDesc(scriptKey),
48+
Amount: &amount,
49+
LockTime: 1337,
50+
RelativeLockTime: 6,
51+
},
52+
)
53+
require.NoError(t, err)
54+
proofAsset := assets[0]
55+
proofAsset.GroupKey.RawKey = keychain.KeyDescriptor{}
56+
57+
// Empty the group witness, since it will eventually be stored as the
58+
// asset's witness within the proof.
59+
// TODO(guggero): Actually store the witness in the proof.
60+
proofAsset.GroupKey.Witness = nil
61+
62+
// Empty the raw script key, since we only serialize the tweaked
63+
// pubkey. We'll also force the main script key to be an x-only key as
64+
// well.
65+
proofAsset.ScriptKey.PubKey, err = schnorr.ParsePubKey(
66+
schnorr.SerializePubKey(proofAsset.ScriptKey.PubKey),
67+
)
68+
require.NoError(t, err)
69+
70+
proofAsset.ScriptKey.TweakedScriptKey = nil
71+
72+
_, commitmentProof, err := mintCommitment.Proof(
73+
proofAsset.TapCommitmentKey(), proofAsset.AssetCommitmentKey(),
74+
)
75+
require.NoError(t, err)
76+
77+
leaf1 := txscript.NewBaseTapLeaf([]byte{1})
78+
leaf2 := txscript.NewBaseTapLeaf([]byte{2})
79+
testLeafPreimage := commitment.NewPreimageFromLeaf(leaf1)
80+
testLeafPreimage2 := commitment.NewPreimageFromLeaf(leaf2)
81+
testBranchPreimage := commitment.NewPreimageFromBranch(
82+
txscript.NewTapBranch(leaf1, leaf2),
83+
)
84+
return Proof{
85+
PrevOut: genesis.FirstPrevOut,
86+
BlockHeader: block.Header,
87+
BlockHeight: 42,
88+
AnchorTx: *block.Transactions[txIndex],
89+
TxMerkleProof: *txMerkleProof,
90+
Asset: *proofAsset,
91+
InclusionProof: TaprootProof{
92+
OutputIndex: outputIndex,
93+
InternalKey: test.RandPubKey(t),
94+
CommitmentProof: &CommitmentProof{
95+
Proof: *commitmentProof,
96+
TapSiblingPreimage: testLeafPreimage,
97+
},
98+
TapscriptProof: nil,
99+
},
100+
ExclusionProofs: []TaprootProof{
101+
{
102+
OutputIndex: 2,
103+
InternalKey: test.RandPubKey(t),
104+
CommitmentProof: &CommitmentProof{
105+
Proof: *commitmentProof,
106+
TapSiblingPreimage: testLeafPreimage,
107+
},
108+
TapscriptProof: nil,
109+
},
110+
{
111+
OutputIndex: 3,
112+
InternalKey: test.RandPubKey(t),
113+
CommitmentProof: nil,
114+
TapscriptProof: &TapscriptProof{
115+
TapPreimage1: testBranchPreimage,
116+
TapPreimage2: testLeafPreimage2,
117+
Bip86: true,
118+
},
119+
},
120+
{
121+
OutputIndex: 4,
122+
InternalKey: test.RandPubKey(t),
123+
CommitmentProof: nil,
124+
TapscriptProof: &TapscriptProof{
125+
Bip86: true,
126+
},
127+
},
128+
},
129+
SplitRootProof: &TaprootProof{
130+
OutputIndex: 4,
131+
InternalKey: test.RandPubKey(t),
132+
CommitmentProof: &CommitmentProof{
133+
Proof: *commitmentProof,
134+
TapSiblingPreimage: nil,
135+
},
136+
},
137+
MetaReveal: &MetaReveal{
138+
Data: []byte("quoth the raven nevermore"),
139+
Type: MetaOpaque,
140+
},
141+
ChallengeWitness: wire.TxWitness{[]byte("foo"), []byte("bar")},
142+
GenesisReveal: &genesis,
143+
GroupKeyReveal: &groupReveal,
144+
}
145+
}
146+
23147
type MockVerifier struct {
24148
t *testing.T
25149
}

0 commit comments

Comments
 (0)