2019-03-06 21:13:50 +01:00
|
|
|
package test
|
|
|
|
|
|
|
|
|
|
import (
|
2019-12-09 16:47:23 +01:00
|
|
|
"bytes"
|
2019-03-06 21:13:50 +01:00
|
|
|
"context"
|
2022-06-09 20:24:57 +02:00
|
|
|
"encoding/hex"
|
2020-02-07 16:16:38 -08:00
|
|
|
"fmt"
|
2024-04-03 13:01:12 +02:00
|
|
|
"time"
|
2019-03-06 21:13:50 +01:00
|
|
|
|
2022-03-14 12:36:02 +00:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
2022-06-09 20:24:57 +02:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
2019-03-06 21:13:50 +01:00
|
|
|
"github.com/btcsuite/btcd/wire"
|
2020-08-13 11:17:03 +02:00
|
|
|
"github.com/lightninglabs/lndclient"
|
2019-03-06 21:13:50 +01:00
|
|
|
"github.com/lightningnetwork/lnd/input"
|
2019-12-09 16:34:02 +01:00
|
|
|
"github.com/lightningnetwork/lnd/keychain"
|
2024-04-03 13:01:12 +02:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
|
2019-03-06 21:13:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type mockSigner struct {
|
2024-09-02 18:49:22 +02:00
|
|
|
lndclient.SignerClient
|
|
|
|
|
|
2019-12-09 16:34:02 +01:00
|
|
|
lnd *LndMockServices
|
2019-03-06 21:13:50 +01:00
|
|
|
}
|
|
|
|
|
|
2024-04-03 13:01:12 +02:00
|
|
|
var _ lndclient.SignerClient = (*mockSigner)(nil)
|
|
|
|
|
|
|
|
|
|
func (s *mockSigner) RawClientWithMacAuth(
|
|
|
|
|
ctx context.Context) (context.Context, time.Duration,
|
|
|
|
|
signrpc.SignerClient) {
|
|
|
|
|
|
|
|
|
|
return ctx, 0, nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-06 21:13:50 +01:00
|
|
|
func (s *mockSigner) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
|
2022-06-02 18:26:17 +02:00
|
|
|
signDescriptors []*lndclient.SignDescriptor,
|
|
|
|
|
_ []*wire.TxOut) ([][]byte, error) {
|
2019-03-06 21:13:50 +01:00
|
|
|
|
2020-01-15 09:55:22 +01:00
|
|
|
s.lnd.SignOutputRawChannel <- SignOutputRawRequest{
|
|
|
|
|
Tx: tx,
|
|
|
|
|
SignDescriptors: signDescriptors,
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-11 23:24:06 -03:00
|
|
|
rawSigs := make([][]byte, len(signDescriptors))
|
|
|
|
|
for i := range signDescriptors {
|
|
|
|
|
sig := make([]byte, 64)
|
|
|
|
|
sig[0] = byte(i + 1)
|
|
|
|
|
rawSigs[i] = sig
|
|
|
|
|
}
|
2019-03-06 21:13:50 +01:00
|
|
|
|
|
|
|
|
return rawSigs, nil
|
|
|
|
|
}
|
2019-12-09 16:34:02 +01:00
|
|
|
|
2020-02-07 16:16:38 -08:00
|
|
|
func (s *mockSigner) ComputeInputScript(ctx context.Context, tx *wire.MsgTx,
|
2022-10-07 12:23:10 +02:00
|
|
|
signDescriptors []*lndclient.SignDescriptor,
|
|
|
|
|
prevOutputs []*wire.TxOut) ([]*input.Script, error) {
|
2020-02-07 16:16:38 -08:00
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("unimplemented")
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-09 16:34:02 +01:00
|
|
|
func (s *mockSigner) SignMessage(ctx context.Context, msg []byte,
|
2023-01-09 16:36:52 +01:00
|
|
|
locator keychain.KeyLocator, opts ...lndclient.SignMessageOption) (
|
|
|
|
|
[]byte, error) {
|
2019-12-09 16:34:02 +01:00
|
|
|
|
|
|
|
|
return s.lnd.Signature, nil
|
|
|
|
|
}
|
2019-12-09 16:47:23 +01:00
|
|
|
|
|
|
|
|
func (s *mockSigner) VerifyMessage(ctx context.Context, msg, sig []byte,
|
2023-01-09 16:36:52 +01:00
|
|
|
pubkey [33]byte, opts ...lndclient.VerifyMessageOption) (bool, error) {
|
2019-12-09 16:47:23 +01:00
|
|
|
|
|
|
|
|
// Make the mock somewhat functional by asserting that the message and
|
|
|
|
|
// signature is what we expect from the mock parameters.
|
|
|
|
|
mockAssertion := bytes.Equal(msg, []byte(s.lnd.SignatureMsg)) &&
|
|
|
|
|
bytes.Equal(sig, s.lnd.Signature)
|
|
|
|
|
|
|
|
|
|
return mockAssertion, nil
|
|
|
|
|
}
|
2020-01-21 15:37:49 -08:00
|
|
|
|
|
|
|
|
func (s *mockSigner) DeriveSharedKey(context.Context, *btcec.PublicKey,
|
|
|
|
|
*keychain.KeyLocator) ([32]byte, error) {
|
|
|
|
|
|
|
|
|
|
return [32]byte{4, 5, 6}, nil
|
|
|
|
|
}
|
2022-06-02 18:26:17 +02:00
|
|
|
|
|
|
|
|
// MuSig2CreateSession creates a new MuSig2 signing session using the local
|
|
|
|
|
// key identified by the key locator. The complete list of all public keys of
|
|
|
|
|
// all signing parties must be provided, including the public key of the local
|
|
|
|
|
// signing key. If nonces of other parties are already known, they can be
|
|
|
|
|
// submitted as well to reduce the number of method calls necessary later on.
|
2023-01-09 16:36:52 +01:00
|
|
|
func (s *mockSigner) MuSig2CreateSession(context.Context, input.MuSig2Version,
|
|
|
|
|
*keychain.KeyLocator, [][]byte, ...lndclient.MuSig2SessionOpts) (
|
|
|
|
|
*input.MuSig2SessionInfo, error) {
|
2022-06-02 18:26:17 +02:00
|
|
|
|
2022-06-09 20:24:57 +02:00
|
|
|
const testPubKey = "F9308A019258C31049344F85F89D5229B531C845836F99B08601F113BCE036F9"
|
|
|
|
|
pubKeyBytes, err := hex.DecodeString(testPubKey)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
combinedKey, err := schnorr.ParsePubKey(pubKeyBytes)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &input.MuSig2SessionInfo{
|
|
|
|
|
CombinedKey: combinedKey,
|
|
|
|
|
HaveAllNonces: true,
|
|
|
|
|
}, nil
|
2022-06-02 18:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MuSig2RegisterNonces registers one or more public nonces of other signing
|
|
|
|
|
// participants for a session identified by its ID. This method returns true
|
|
|
|
|
// once we have all nonces for all other signing participants.
|
|
|
|
|
func (s *mockSigner) MuSig2RegisterNonces(context.Context, [32]byte,
|
|
|
|
|
[][66]byte) (bool, error) {
|
|
|
|
|
|
2022-06-09 20:24:57 +02:00
|
|
|
return true, nil
|
2022-06-02 18:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MuSig2Sign creates a partial signature using the local signing key
|
|
|
|
|
// that was specified when the session was created. This can only be
|
|
|
|
|
// called when all public nonces of all participants are known and have
|
|
|
|
|
// been registered with the session. If this node isn't responsible for
|
|
|
|
|
// combining all the partial signatures, then the cleanup parameter
|
|
|
|
|
// should be set, indicating that the session can be removed from memory
|
|
|
|
|
// once the signature was produced.
|
|
|
|
|
func (s *mockSigner) MuSig2Sign(context.Context, [32]byte, [32]byte,
|
|
|
|
|
bool) ([]byte, error) {
|
|
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MuSig2CombineSig combines the given partial signature(s) with the
|
|
|
|
|
// local one, if it already exists. Once a partial signature of all
|
|
|
|
|
// participants is registered, the final signature will be combined and
|
|
|
|
|
// returned.
|
|
|
|
|
func (s *mockSigner) MuSig2CombineSig(context.Context, [32]byte,
|
|
|
|
|
[][]byte) (bool, []byte, error) {
|
|
|
|
|
|
2024-07-11 23:24:06 -03:00
|
|
|
sig := make([]byte, 64)
|
|
|
|
|
sig[0] = 42
|
|
|
|
|
|
|
|
|
|
return true, sig, nil
|
2022-06-02 18:26:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MuSig2Cleanup removes a session from memory to free up resources.
|
|
|
|
|
func (s *mockSigner) MuSig2Cleanup(context.Context, [32]byte) error {
|
|
|
|
|
return nil
|
|
|
|
|
}
|