|
| 1 | +package tapchannel |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/hex" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/btcsuite/btcd/wire" |
| 13 | + "github.com/lightninglabs/taproot-assets/address" |
| 14 | + "github.com/lightninglabs/taproot-assets/asset" |
| 15 | + "github.com/lightninglabs/taproot-assets/internal/test" |
| 16 | + "github.com/lightninglabs/taproot-assets/proof" |
| 17 | + cmsg "github.com/lightninglabs/taproot-assets/tapchannelmsg" |
| 18 | + "github.com/lightninglabs/taproot-assets/tapfreighter" |
| 19 | + "github.com/lightninglabs/taproot-assets/tappsbt" |
| 20 | + "github.com/lightningnetwork/lnd/channeldb" |
| 21 | + lfn "github.com/lightningnetwork/lnd/fn" |
| 22 | + "github.com/lightningnetwork/lnd/input" |
| 23 | + "github.com/lightningnetwork/lnd/lnwallet" |
| 24 | + "github.com/lightningnetwork/lnd/lnwire" |
| 25 | + "github.com/lightningnetwork/lnd/tlv" |
| 26 | + "github.com/stretchr/testify/require" |
| 27 | +) |
| 28 | + |
| 29 | +const ( |
| 30 | + // testDataFileName is the name of the directory with the test data. |
| 31 | + testDataFileName = "testdata" |
| 32 | +) |
| 33 | + |
| 34 | +var ( |
| 35 | + testChainParams = &address.RegressionNetTap |
| 36 | + |
| 37 | + // Block 100002 with 9 transactions on bitcoin mainnet. |
| 38 | + oddTxBlockHexFileName = filepath.Join( |
| 39 | + testDataFileName, "odd-block.hex", |
| 40 | + ) |
| 41 | + |
| 42 | + testTimeout = time.Second |
| 43 | +) |
| 44 | + |
| 45 | +// TestAuxLeafSigner tests the AuxLeafSigner implementation. |
| 46 | +func TestAuxLeafSigner(t *testing.T) { |
| 47 | + cfg := &LeafSignerConfig{ |
| 48 | + ChainParams: testChainParams, |
| 49 | + Signer: &mockVirtualSigner{}, |
| 50 | + } |
| 51 | + |
| 52 | + signer := NewAuxLeafSigner(cfg) |
| 53 | + require.NoError(t, signer.Start()) |
| 54 | + |
| 55 | + defer func() { |
| 56 | + require.NoError(t, signer.Stop()) |
| 57 | + }() |
| 58 | + |
| 59 | + chanState := &channeldb.OpenChannel{ |
| 60 | + ChanType: channeldb.AnchorOutputsBit | |
| 61 | + channeldb.ScidAliasChanBit | channeldb.SingleFunderBit | |
| 62 | + channeldb.SimpleTaprootFeatureBit | |
| 63 | + channeldb.TapscriptRootBit, |
| 64 | + IsInitiator: true, |
| 65 | + } |
| 66 | + randInputProof := randProof(t) |
| 67 | + commitTx := &randInputProof.AnchorTx |
| 68 | + keyRing := lnwallet.CommitmentKeyRing{ |
| 69 | + CommitPoint: test.RandPubKey(t), |
| 70 | + LocalCommitKeyTweak: test.RandBytes(32), |
| 71 | + LocalHtlcKeyTweak: test.RandBytes(32), |
| 72 | + LocalHtlcKey: test.RandPubKey(t), |
| 73 | + RemoteHtlcKey: test.RandPubKey(t), |
| 74 | + ToLocalKey: test.RandPubKey(t), |
| 75 | + ToRemoteKey: test.RandPubKey(t), |
| 76 | + RevocationKey: test.RandPubKey(t), |
| 77 | + } |
| 78 | + |
| 79 | + outgoingHtlcs := make(map[input.HtlcIndex][]*cmsg.AssetOutput) |
| 80 | + outgoingHtlcs[0] = []*cmsg.AssetOutput{ |
| 81 | + cmsg.NewAssetOutput( |
| 82 | + randInputProof.Asset.ID(), randInputProof.Asset.Amount, |
| 83 | + randInputProof, |
| 84 | + ), |
| 85 | + } |
| 86 | + |
| 87 | + com := cmsg.NewCommitment( |
| 88 | + nil, nil, outgoingHtlcs, nil, lnwallet.CommitAuxLeaves{}, |
| 89 | + ) |
| 90 | + |
| 91 | + randKeyDesc, _ := test.RandKeyDesc(t) |
| 92 | + |
| 93 | + jobs := []lnwallet.AuxSigJob{ |
| 94 | + { |
| 95 | + SignDesc: input.SignDescriptor{ |
| 96 | + KeyDesc: randKeyDesc, |
| 97 | + }, |
| 98 | + BaseAuxJob: lnwallet.BaseAuxJob{ |
| 99 | + OutputIndex: 0, |
| 100 | + KeyRing: keyRing, |
| 101 | + HTLC: lnwallet.PaymentDescriptor{ |
| 102 | + HtlcIndex: 0, |
| 103 | + Amount: lnwire.NewMSatFromSatoshis( |
| 104 | + 354, |
| 105 | + ), |
| 106 | + EntryType: lnwallet.Add, |
| 107 | + }, |
| 108 | + Incoming: false, |
| 109 | + CommitBlob: lfn.Some[tlv.Blob](com.Bytes()), |
| 110 | + HtlcLeaf: input.AuxTapLeaf{}, |
| 111 | + }, |
| 112 | + Resp: make(chan lnwallet.AuxSigJobResp), |
| 113 | + Cancel: make(chan struct{}), |
| 114 | + }, |
| 115 | + } |
| 116 | + |
| 117 | + err := signer.SubmitSecondLevelSigBatch(chanState, commitTx, jobs) |
| 118 | + require.NoError(t, err) |
| 119 | + |
| 120 | + select { |
| 121 | + case resp := <-jobs[0].Resp: |
| 122 | + require.NoError(t, resp.Err) |
| 123 | + require.True(t, resp.SigBlob.IsSome()) |
| 124 | + require.True(t, bytes.Contains( |
| 125 | + resp.SigBlob.UnwrapOr(nil), |
| 126 | + []byte("this is a signature"), |
| 127 | + )) |
| 128 | + |
| 129 | + case <-time.After(testTimeout): |
| 130 | + t.Fatalf("timeout waiting for response") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +// mockVirtualSigner is a mock implementation of the VirtualSigner interface. |
| 135 | +type mockVirtualSigner struct { |
| 136 | +} |
| 137 | + |
| 138 | +// SignVirtualPacket signs the virtual transaction of the given packet and |
| 139 | +// returns the input indexes that were signed. |
| 140 | +// |
| 141 | +// NOTE: This is part of the VirtualPacketSigner interface. |
| 142 | +func (m *mockVirtualSigner) SignVirtualPacket(vPkt *tappsbt.VPacket, |
| 143 | + _ ...tapfreighter.SignVirtualPacketOption) ([]uint32, |
| 144 | + error) { |
| 145 | + |
| 146 | + // A second-level HTLC transaction is always a one-in-one-out virtual |
| 147 | + // transaction, so there's always just one (non-split) output. |
| 148 | + vPkt.Outputs[0].Asset.PrevWitnesses[0].TxWitness = [][]byte{ |
| 149 | + []byte("this is a signature"), |
| 150 | + } |
| 151 | + |
| 152 | + return []uint32{0}, nil |
| 153 | +} |
| 154 | + |
| 155 | +// A compile time check to ensure mockVirtualSigner implements the |
| 156 | +// VirtualPacketSigner interface. |
| 157 | +var _ VirtualPacketSigner = (*mockVirtualSigner)(nil) |
| 158 | + |
| 159 | +// randProof returns a random proof that contains all information required for |
| 160 | +// it to be successfully serialized. |
| 161 | +func randProof(t *testing.T) proof.Proof { |
| 162 | + oddTxBlockHex, err := os.ReadFile(oddTxBlockHexFileName) |
| 163 | + require.NoError(t, err) |
| 164 | + |
| 165 | + oddTxBlockBytes, err := hex.DecodeString( |
| 166 | + strings.Trim(string(oddTxBlockHex), "\n"), |
| 167 | + ) |
| 168 | + require.NoError(t, err) |
| 169 | + |
| 170 | + var oddTxBlock wire.MsgBlock |
| 171 | + err = oddTxBlock.Deserialize(bytes.NewReader(oddTxBlockBytes)) |
| 172 | + require.NoError(t, err) |
| 173 | + |
| 174 | + randGen := asset.RandGenesis(t, asset.Normal) |
| 175 | + scriptKey1 := test.RandPubKey(t) |
| 176 | + originalRandProof := proof.RandProof( |
| 177 | + t, randGen, scriptKey1, oddTxBlock, 0, 0, |
| 178 | + ) |
| 179 | + |
| 180 | + // Proofs don't Encode everything, so we need to do a quick Encode/ |
| 181 | + // Decode cycle to make sure we can compare it afterward. |
| 182 | + proofBytes, err := proof.Encode(&originalRandProof) |
| 183 | + require.NoError(t, err) |
| 184 | + p, err := proof.Decode(proofBytes) |
| 185 | + require.NoError(t, err) |
| 186 | + |
| 187 | + return *p |
| 188 | +} |
0 commit comments