Skip to content

Commit 2a7c518

Browse files
committed
lndclient: expose SignMessage RPC
1 parent 162b258 commit 2a7c518

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

lndclient/signer_client.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/btcsuite/btcd/wire"
77
"github.com/lightninglabs/loop/swap"
88
"github.com/lightningnetwork/lnd/input"
9+
"github.com/lightningnetwork/lnd/keychain"
910
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
1011
"google.golang.org/grpc"
1112
)
@@ -14,6 +15,11 @@ import (
1415
type SignerClient interface {
1516
SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
1617
signDescriptors []*input.SignDescriptor) ([][]byte, error)
18+
19+
// SignMessage signs a message with the key specified in the key
20+
// locator. The returned signature is fixed-size LN wire format encoded.
21+
SignMessage(ctx context.Context, msg []byte,
22+
locator keychain.KeyLocator) ([]byte, error)
1723
}
1824

1925
type signerClient struct {
@@ -93,3 +99,28 @@ func (s *signerClient) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
9399

94100
return resp.RawSigs, nil
95101
}
102+
103+
// SignMessage signs a message with the key specified in the key locator. The
104+
// returned signature is fixed-size LN wire format encoded.
105+
func (s *signerClient) SignMessage(ctx context.Context, msg []byte,
106+
locator keychain.KeyLocator) ([]byte, error) {
107+
108+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
109+
defer cancel()
110+
111+
rpcIn := &signrpc.SignMessageReq{
112+
Msg: msg,
113+
KeyLoc: &signrpc.KeyLocator{
114+
KeyFamily: int32(locator.Family),
115+
KeyIndex: int32(locator.Index),
116+
},
117+
}
118+
119+
rpcCtx = s.signerMac.WithMacaroonAuth(rpcCtx)
120+
resp, err := s.client.SignMessage(rpcCtx, rpcIn)
121+
if err != nil {
122+
return nil, err
123+
}
124+
125+
return resp.Signature, nil
126+
}

test/lnd_services_mock.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ var (
1818
testStartingHeight = int32(600)
1919
testNodePubkey = "03f5374b16f0b1f1b49101de1b9d89e0b460bc57ce9c2f9" +
2020
"132b73dfc76d3704daa"
21+
testSignature = []byte{55, 66, 77, 88, 99}
2122
)
2223

2324
// NewMockLnd returns a new instance of LndMockServices that can be used in unit
@@ -59,13 +60,15 @@ func NewMockLnd() *LndMockServices {
5960
epochChannel: make(chan int32),
6061
Height: testStartingHeight,
6162
NodePubkey: testNodePubkey,
63+
Signature: testSignature,
6264
}
6365

6466
lightningClient.lnd = &lnd
6567
chainNotifier.lnd = &lnd
6668
walletKit.lnd = &lnd
6769
invoices.lnd = &lnd
6870
router.lnd = &lnd
71+
signer.lnd = &lnd
6972

7073
lnd.WaitForFinished = func() {
7174
chainNotifier.WaitForFinished()
@@ -127,6 +130,7 @@ type LndMockServices struct {
127130

128131
Height int32
129132
NodePubkey string
133+
Signature []byte
130134

131135
WaitForFinished func()
132136

test/signer_mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ import (
55

66
"github.com/btcsuite/btcd/wire"
77
"github.com/lightningnetwork/lnd/input"
8+
"github.com/lightningnetwork/lnd/keychain"
89
)
910

1011
type mockSigner struct {
12+
lnd *LndMockServices
1113
}
1214

1315
func (s *mockSigner) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
@@ -17,3 +19,9 @@ func (s *mockSigner) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
1719

1820
return rawSigs, nil
1921
}
22+
23+
func (s *mockSigner) SignMessage(ctx context.Context, msg []byte,
24+
locator keychain.KeyLocator) ([]byte, error) {
25+
26+
return s.lnd.Signature, nil
27+
}

0 commit comments

Comments
 (0)