Skip to content

Commit

Permalink
Merge pull request lightninglabs#799 from lightninglabs/prepare-refactor
Browse files Browse the repository at this point in the history
[preparation 1/2]: refactor to simplify send logic, prepare for new RPCs
  • Loading branch information
guggero authored Feb 21, 2024
2 parents c35215a + a2a95e7 commit d37b486
Show file tree
Hide file tree
Showing 32 changed files with 1,289 additions and 796 deletions.
12 changes: 5 additions & 7 deletions address/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ func compressedPubKeyDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error
)
}

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

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

return nil
}
return tlv.NewTypeForDecodingErr(
val, "*url.URL", l, l,
)
return tlv.NewTypeForDecodingErr(val, "*url.URL", l, l)
}

func VersionEncoder(w io.Writer, val any, buf *[8]byte) error {
Expand Down
2 changes: 1 addition & 1 deletion address/records.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,6 @@ func newProofCourierAddrRecord(addr *url.URL) tlv.Record {

return tlv.MakeDynamicRecord(
addrProofCourierAddrType, addr, recordSize,
urlEncoder, urlDecoder,
UrlEncoder, UrlDecoder,
)
}
6 changes: 3 additions & 3 deletions itest/psbt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
wrpc "github.com/lightninglabs/taproot-assets/taprpc/assetwalletrpc"
"github.com/lightninglabs/taproot-assets/taprpc/mintrpc"
"github.com/lightninglabs/taproot-assets/taprpc/tapdevrpc"
"github.com/lightninglabs/taproot-assets/tapscript"
"github.com/lightninglabs/taproot-assets/tapsend"
"github.com/lightningnetwork/lnd/input"
"github.com/lightningnetwork/lnd/keychain"
"github.com/lightningnetwork/lnd/lntest/wait"
Expand Down Expand Up @@ -1391,7 +1391,7 @@ func testPsbtSighashNone(t *harnessTest) {
witnessBackup := signedPacket.Outputs[0].Asset.PrevWitnesses

// Bob now creates the output assets.
err = tapscript.PrepareOutputAssets(context.Background(), signedPacket)
err = tapsend.PrepareOutputAssets(context.Background(), signedPacket)
require.NoError(t.t, err)

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

// Bob now creates the output assets.
err = tapscript.PrepareOutputAssets(context.Background(), signedPacket)
err = tapsend.PrepareOutputAssets(context.Background(), signedPacket)
require.NoError(t.t, err)

// We attach the backed-up Previous Witnesses to the newly created
Expand Down
13 changes: 11 additions & 2 deletions itest/tapd_harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/hex"
"flag"
"fmt"
"net"
"os"
"path/filepath"
"sync"
Expand Down Expand Up @@ -305,10 +306,18 @@ func (hs *tapdHarness) start(expectErrExit bool) error {
}
}()

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

// Create our client to interact with the tapd RPC server directly.
listenerAddr := hs.clientCfg.RpcConf.RawRPCListeners[0]
rpcConn, err := dialServer(
listenerAddr, hs.clientCfg.RpcConf.TLSCertPath,
hs.clientCfg.RpcConf.MacaroonPath,
Expand Down
6 changes: 2 additions & 4 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/lightninglabs/taproot-assets/tapdb"
"github.com/lightninglabs/taproot-assets/tapfreighter"
"github.com/lightninglabs/taproot-assets/tapgarden"
"github.com/lightninglabs/taproot-assets/tapscript"
"github.com/lightninglabs/taproot-assets/tapsend"
"github.com/lightninglabs/taproot-assets/universe"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/signal"
Expand Down Expand Up @@ -99,9 +99,7 @@ func SetupLoggers(root *build.RotatingLogWriter, interceptor signal.Interceptor)
AddSubLogger(root, proof.Subsystem, interceptor, proof.UseLogger)
AddSubLogger(root, tapdb.Subsystem, interceptor, tapdb.UseLogger)
AddSubLogger(root, address.Subsystem, interceptor, address.UseLogger)
AddSubLogger(
root, tapscript.Subsystem, interceptor, tapscript.UseLogger,
)
AddSubLogger(root, tapsend.Subsystem, interceptor, tapsend.UseLogger)
AddSubLogger(root, universe.Subsystem, interceptor, universe.UseLogger)
AddSubLogger(
root, commitment.Subsystem, interceptor, commitment.UseLogger,
Expand Down
12 changes: 6 additions & 6 deletions proof/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,17 +292,17 @@ func TestMigrateOldFileNames(t *testing.T) {
scriptKey2 := test.RandPubKey(t)

// We create 4 different proofs with the old naming scheme.
proof1 := randomProof(t, genesis1, scriptKey1, oddTxBlock, 0, 1)
proof1 := RandProof(t, genesis1, scriptKey1, oddTxBlock, 0, 1)
storeProofOldName(proof1)
proof2 := randomProof(t, genesis1, scriptKey2, oddTxBlock, 0, 1)
proof2 := RandProof(t, genesis1, scriptKey2, oddTxBlock, 0, 1)
storeProofOldName(proof2)
proof3 := randomProof(t, genesis2, scriptKey1, oddTxBlock, 1, 1)
proof3 := RandProof(t, genesis2, scriptKey1, oddTxBlock, 1, 1)
storeProofOldName(proof3)
proof4 := randomProof(t, genesis2, scriptKey2, oddTxBlock, 1, 1)
proof4 := RandProof(t, genesis2, scriptKey2, oddTxBlock, 1, 1)
storeProofOldName(proof4)

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

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

// We should be able to import a new proof, and it should be stored
// under the new naming scheme.
proof6 := randomProof(t, genesis2, scriptKey2, oddTxBlock, 2, 1)
proof6 := RandProof(t, genesis2, scriptKey2, oddTxBlock, 2, 1)
err = fileArchive.ImportProofs(nil, nil, nil, false, &AnnotatedProof{
Locator: Locator{
AssetID: fn.Ptr(proof6.Asset.ID()),
Expand Down
2 changes: 1 addition & 1 deletion proof/courier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestUniverseRpcCourierLocalArchiveShortCut(t *testing.T) {

genesis := asset.RandGenesis(t, asset.Collectible)
scriptKey := test.RandPubKey(t)
proof := randomProof(t, genesis, scriptKey, oddTxBlock, 0, 1)
proof := RandProof(t, genesis, scriptKey, oddTxBlock, 0, 1)

file, err := NewFile(V0, proof, proof)
require.NoError(t, err)
Expand Down
6 changes: 6 additions & 0 deletions proof/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ func (p *BaseProofParams) HaveExclusionProof(anchorOutputIndex uint32) bool {
return false
}

// HaveInclusionProof returns true if the inclusion proof is for the given
// anchor output index.
func (p *BaseProofParams) HaveInclusionProof(anchorOutputIndex uint32) bool {
return p.OutputIndex == int(anchorOutputIndex)
}

// MintParams holds the set of chain level information needed to make a proof
// file for the set of assets minted in a batch.
type MintParams struct {
Expand Down
124 changes: 124 additions & 0 deletions proof/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,139 @@ import (
"time"

"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/lightninglabs/taproot-assets/asset"
"github.com/lightninglabs/taproot-assets/commitment"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/internal/test"
"github.com/lightningnetwork/lnd/keychain"
"github.com/stretchr/testify/require"
)

func RandProof(t testing.TB, genesis asset.Genesis,
scriptKey *btcec.PublicKey, block wire.MsgBlock, txIndex int,
outputIndex uint32) Proof {

txMerkleProof, err := NewTxMerkleProof(block.Transactions, txIndex)
require.NoError(t, err)

tweakedScriptKey := asset.NewScriptKey(scriptKey)
protoAsset := asset.NewAssetNoErr(
t, genesis, 1, 0, 0, tweakedScriptKey, nil,
)
groupKey := asset.RandGroupKey(t, genesis, protoAsset)
groupReveal := asset.GroupKeyReveal{
RawKey: asset.ToSerialized(&groupKey.GroupPubKey),
TapscriptRoot: test.RandBytes(32),
}

amount := uint64(1)
mintCommitment, assets, err := commitment.Mint(
genesis, groupKey, &commitment.AssetDetails{
Type: genesis.Type,
ScriptKey: test.PubToKeyDesc(scriptKey),
Amount: &amount,
LockTime: 1337,
RelativeLockTime: 6,
},
)
require.NoError(t, err)
proofAsset := assets[0]
proofAsset.GroupKey.RawKey = keychain.KeyDescriptor{}

// Empty the group witness, since it will eventually be stored as the
// asset's witness within the proof.
// TODO(guggero): Actually store the witness in the proof.
proofAsset.GroupKey.Witness = nil

// Empty the raw script key, since we only serialize the tweaked
// pubkey. We'll also force the main script key to be an x-only key as
// well.
proofAsset.ScriptKey.PubKey, err = schnorr.ParsePubKey(
schnorr.SerializePubKey(proofAsset.ScriptKey.PubKey),
)
require.NoError(t, err)

proofAsset.ScriptKey.TweakedScriptKey = nil

_, commitmentProof, err := mintCommitment.Proof(
proofAsset.TapCommitmentKey(), proofAsset.AssetCommitmentKey(),
)
require.NoError(t, err)

leaf1 := txscript.NewBaseTapLeaf([]byte{1})
leaf2 := txscript.NewBaseTapLeaf([]byte{2})
testLeafPreimage := commitment.NewPreimageFromLeaf(leaf1)
testLeafPreimage2 := commitment.NewPreimageFromLeaf(leaf2)
testBranchPreimage := commitment.NewPreimageFromBranch(
txscript.NewTapBranch(leaf1, leaf2),
)
return Proof{
PrevOut: genesis.FirstPrevOut,
BlockHeader: block.Header,
BlockHeight: 42,
AnchorTx: *block.Transactions[txIndex],
TxMerkleProof: *txMerkleProof,
Asset: *proofAsset,
InclusionProof: TaprootProof{
OutputIndex: outputIndex,
InternalKey: test.RandPubKey(t),
CommitmentProof: &CommitmentProof{
Proof: *commitmentProof,
TapSiblingPreimage: testLeafPreimage,
},
TapscriptProof: nil,
},
ExclusionProofs: []TaprootProof{
{
OutputIndex: 2,
InternalKey: test.RandPubKey(t),
CommitmentProof: &CommitmentProof{
Proof: *commitmentProof,
TapSiblingPreimage: testLeafPreimage,
},
TapscriptProof: nil,
},
{
OutputIndex: 3,
InternalKey: test.RandPubKey(t),
CommitmentProof: nil,
TapscriptProof: &TapscriptProof{
TapPreimage1: testBranchPreimage,
TapPreimage2: testLeafPreimage2,
Bip86: true,
},
},
{
OutputIndex: 4,
InternalKey: test.RandPubKey(t),
CommitmentProof: nil,
TapscriptProof: &TapscriptProof{
Bip86: true,
},
},
},
SplitRootProof: &TaprootProof{
OutputIndex: 4,
InternalKey: test.RandPubKey(t),
CommitmentProof: &CommitmentProof{
Proof: *commitmentProof,
TapSiblingPreimage: nil,
},
},
MetaReveal: &MetaReveal{
Data: []byte("quoth the raven nevermore"),
Type: MetaOpaque,
},
ChallengeWitness: wire.TxWitness{[]byte("foo"), []byte("bar")},
GenesisReveal: &genesis,
GroupKeyReveal: &groupReveal,
}
}

type MockVerifier struct {
t *testing.T
}
Expand Down
Loading

0 comments on commit d37b486

Please sign in to comment.