|
| 1 | +package address |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/hex" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/btcsuite/btcd/btcec/v2" |
| 9 | + "github.com/btcsuite/btcd/btcec/v2/schnorr" |
| 10 | + "github.com/btcsuite/btcd/btcutil" |
| 11 | + "github.com/lightninglabs/loop/loopdb" |
| 12 | + "github.com/lightninglabs/loop/staticaddr/script" |
| 13 | + "github.com/lightninglabs/loop/swap" |
| 14 | + "github.com/lightninglabs/loop/swapserverrpc" |
| 15 | + "github.com/lightninglabs/loop/test" |
| 16 | + "github.com/lightningnetwork/lnd/input" |
| 17 | + "github.com/lightningnetwork/lnd/keychain" |
| 18 | + "github.com/stretchr/testify/mock" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | + "google.golang.org/grpc" |
| 21 | +) |
| 22 | + |
| 23 | +var ( |
| 24 | + defaultServerPubkeyBytes, _ = hex.DecodeString("021c97a90a411ff2b10dc2a8e32de2f29d2fa49d41bfbb52bd416e460db0747d0d") |
| 25 | + |
| 26 | + defaultServerPubkey, _ = btcec.ParsePubKey(defaultServerPubkeyBytes) |
| 27 | + |
| 28 | + defaultExpiry = uint32(100) |
| 29 | +) |
| 30 | + |
| 31 | +type mockStaticAddressClient struct { |
| 32 | + mock.Mock |
| 33 | +} |
| 34 | + |
| 35 | +func (m *mockStaticAddressClient) ServerNewAddress(ctx context.Context, |
| 36 | + in *swapserverrpc.ServerNewAddressRequest, opts ...grpc.CallOption) ( |
| 37 | + *swapserverrpc.ServerNewAddressResponse, error) { |
| 38 | + |
| 39 | + args := m.Called(ctx, in, opts) |
| 40 | + |
| 41 | + return args.Get(0).(*swapserverrpc.ServerNewAddressResponse), |
| 42 | + args.Error(1) |
| 43 | +} |
| 44 | + |
| 45 | +func TestManager(t *testing.T) { |
| 46 | + ctxb, cancel := context.WithCancel(context.Background()) |
| 47 | + defer cancel() |
| 48 | + |
| 49 | + testContext := NewAddressManagerTestContext(t) |
| 50 | + |
| 51 | + // Start the manager. |
| 52 | + go func() { |
| 53 | + err := testContext.manager.Run(ctxb) |
| 54 | + require.NoError(t, err) |
| 55 | + }() |
| 56 | + |
| 57 | + // Create the expected static address. |
| 58 | + expectedAddress, err := GenerateExpectedTaprootAddress(testContext) |
| 59 | + require.NoError(t, err) |
| 60 | + |
| 61 | + // Create a new static address. |
| 62 | + taprootAddress, err := testContext.manager.NewAddress(ctxb) |
| 63 | + require.NoError(t, err) |
| 64 | + |
| 65 | + // The addresses have to match. |
| 66 | + require.Equal(t, expectedAddress.String(), taprootAddress.String()) |
| 67 | +} |
| 68 | + |
| 69 | +// GenerateExpectedTaprootAddress generates the expected taproot address that |
| 70 | +// the predefined parameters are supposed to generate. |
| 71 | +func GenerateExpectedTaprootAddress(t *ManagerTestContext) ( |
| 72 | + *btcutil.AddressTaproot, error) { |
| 73 | + |
| 74 | + keyIndex := int32(0) |
| 75 | + _, pubKey := test.CreateKey(keyIndex) |
| 76 | + |
| 77 | + keyDescriptor := &keychain.KeyDescriptor{ |
| 78 | + KeyLocator: keychain.KeyLocator{ |
| 79 | + Family: keychain.KeyFamily(swap.StaticAddressKeyFamily), |
| 80 | + Index: uint32(keyIndex), |
| 81 | + }, |
| 82 | + PubKey: pubKey, |
| 83 | + } |
| 84 | + |
| 85 | + staticAddress, err := script.NewStaticAddress( |
| 86 | + input.MuSig2Version100RC2, int64(defaultExpiry), |
| 87 | + keyDescriptor.PubKey, defaultServerPubkey, |
| 88 | + ) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + return btcutil.NewAddressTaproot( |
| 94 | + schnorr.SerializePubKey(staticAddress.TaprootKey), |
| 95 | + t.manager.cfg.ChainParams, |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +// ManagerTestContext is a helper struct that contains all the necessary |
| 100 | +// components to test the static address manager. |
| 101 | +type ManagerTestContext struct { |
| 102 | + manager *Manager |
| 103 | + context test.Context |
| 104 | + mockLnd *test.LndMockServices |
| 105 | + mockStaticAddressClient *mockStaticAddressClient |
| 106 | +} |
| 107 | + |
| 108 | +// NewAddressManagerTestContext creates a new test context for the static |
| 109 | +// address manager. |
| 110 | +func NewAddressManagerTestContext(t *testing.T) *ManagerTestContext { |
| 111 | + mockLnd := test.NewMockLnd() |
| 112 | + lndContext := test.NewContext(t, mockLnd) |
| 113 | + |
| 114 | + dbFixture := loopdb.NewTestDB(t) |
| 115 | + |
| 116 | + store := NewSqlStore(dbFixture.BaseDB) |
| 117 | + |
| 118 | + mockStaticAddressClient := new(mockStaticAddressClient) |
| 119 | + |
| 120 | + mockStaticAddressClient.On( |
| 121 | + "ServerNewAddress", mock.Anything, mock.Anything, mock.Anything, |
| 122 | + ).Return( |
| 123 | + &swapserverrpc.ServerNewAddressResponse{ |
| 124 | + Params: &swapserverrpc.ServerAddressParameters{ |
| 125 | + ServerKey: defaultServerPubkeyBytes, |
| 126 | + Expiry: defaultExpiry, |
| 127 | + }, |
| 128 | + }, nil, |
| 129 | + ) |
| 130 | + |
| 131 | + cfg := &ManagerConfig{ |
| 132 | + Store: store, |
| 133 | + WalletKit: mockLnd.WalletKit, |
| 134 | + ChainParams: mockLnd.ChainParams, |
| 135 | + AddressClient: mockStaticAddressClient, |
| 136 | + FetchL402: func(context.Context) error { return nil }, |
| 137 | + } |
| 138 | + |
| 139 | + manager := NewManager(cfg) |
| 140 | + |
| 141 | + return &ManagerTestContext{ |
| 142 | + manager: manager, |
| 143 | + context: lndContext, |
| 144 | + mockLnd: mockLnd, |
| 145 | + mockStaticAddressClient: mockStaticAddressClient, |
| 146 | + } |
| 147 | +} |
0 commit comments