Skip to content

Commit 3e2a724

Browse files
committed
lndclient: expose DeriveSharedKey for SignerClient
1 parent fcff783 commit 3e2a724

File tree

4 files changed

+60
-5
lines changed

4 files changed

+60
-5
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/google/go-cmp v0.3.1 // indirect
1111
github.com/grpc-ecosystem/grpc-gateway v1.10.0
1212
github.com/jessevdk/go-flags v1.4.0
13-
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20200103000305-22e1f006b194
13+
github.com/lightningnetwork/lnd v0.9.0-beta-rc3.0.20200121213302-a2977c4438b5
1414
github.com/lightningnetwork/lnd/queue v1.0.2
1515
github.com/urfave/cli v1.20.0
1616
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ github.com/lightninglabs/gozmq v0.0.0-20191113021534-d20a764486bf/go.mod h1:vxmQ
137137
github.com/lightninglabs/neutrino v0.11.0 h1:lPpYFCtsfJX2W5zI4pWycPmbbBdr7zU+BafYdLoD6k0=
138138
github.com/lightninglabs/neutrino v0.11.0/go.mod h1:CuhF0iuzg9Sp2HO6ZgXgayviFTn1QHdSTJlMncK80wg=
139139
github.com/lightninglabs/protobuf-hex-display v1.3.3-0.20191212020323-b444784ce75d/go.mod h1:KDb67YMzoh4eudnzClmvs2FbiLG9vxISmLApUkCa4uI=
140-
github.com/lightningnetwork/lightning-onion v0.0.0-20191214001659-f34e9dc1651d h1:U50MHOOeL6gR3Ee/l0eMvZMpmRo+ydzmlQuIruCyCsA=
141-
github.com/lightningnetwork/lightning-onion v0.0.0-20191214001659-f34e9dc1651d/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
142-
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20200103000305-22e1f006b194 h1:PCzjJcVWcMbkiQvzFNc3ta0JmiMprFDqzMZsSpd/km8=
143-
github.com/lightningnetwork/lnd v0.8.0-beta-rc3.0.20200103000305-22e1f006b194/go.mod h1:WHK90FD3m2n6OyWzondS7ho0Uhtgfp30Nxvj24lQYX4=
140+
github.com/lightningnetwork/lightning-onion v1.0.1 h1:qChGgS5+aPxFeR6JiUsGvanei1bn6WJpYbvosw/1604=
141+
github.com/lightningnetwork/lightning-onion v1.0.1/go.mod h1:rigfi6Af/KqsF7Za0hOgcyq2PNH4AN70AaMRxcJkff4=
142+
github.com/lightningnetwork/lnd v0.9.0-beta-rc3.0.20200121213302-a2977c4438b5 h1:qLO+I/7EggqegY8uu6k9TuE/5Tc5zV2L8gQGfgEV9LY=
143+
github.com/lightningnetwork/lnd v0.9.0-beta-rc3.0.20200121213302-a2977c4438b5/go.mod h1:sxMH8WLTqgERzBCrTrBCuDkT6SqAjZhnOWiAQSNzJ8A=
144144
github.com/lightningnetwork/lnd/cert v1.0.0 h1:J0gtf2UNQX2U+/j5cXnX2wIMSTuJuwrXv7m9qJr2wtw=
145145
github.com/lightningnetwork/lnd/cert v1.0.0/go.mod h1:fmtemlSMf5t4hsQmcprSoOykypAPp+9c+0d0iqTScMo=
146146
github.com/lightningnetwork/lnd/queue v1.0.1 h1:jzJKcTy3Nj5lQrooJ3aaw9Lau3I0IwvQR5sqtjdv2R0=

lndclient/signer_client.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package lndclient
33
import (
44
"context"
55

6+
"github.com/btcsuite/btcd/btcec"
67
"github.com/btcsuite/btcd/wire"
78
"github.com/lightninglabs/loop/swap"
89
"github.com/lightningnetwork/lnd/input"
@@ -26,6 +27,19 @@ type SignerClient interface {
2627
// encoded.
2728
VerifyMessage(ctx context.Context, msg, sig []byte, pubkey [33]byte) (
2829
bool, error)
30+
31+
// DeriveSharedKey returns a shared secret key by performing
32+
// Diffie-Hellman key derivation between the ephemeral public key and
33+
// the key specified by the key locator (or the node's identity private
34+
// key if no key locator is specified):
35+
//
36+
// P_shared = privKeyNode * ephemeralPubkey
37+
//
38+
// The resulting shared public key is serialized in the compressed
39+
// format and hashed with SHA256, resulting in a final key length of 256
40+
// bits.
41+
DeriveSharedKey(ctx context.Context, ephemeralPubKey *btcec.PublicKey,
42+
keyLocator *keychain.KeyLocator) ([32]byte, error)
2943
}
3044

3145
type signerClient struct {
@@ -152,3 +166,37 @@ func (s *signerClient) VerifyMessage(ctx context.Context, msg, sig []byte,
152166
}
153167
return resp.Valid, nil
154168
}
169+
170+
// DeriveSharedKey returns a shared secret key by performing Diffie-Hellman key
171+
// derivation between the ephemeral public key and the key specified by the key
172+
// locator (or the node's identity private key if no key locator is specified):
173+
//
174+
// P_shared = privKeyNode * ephemeralPubkey
175+
//
176+
// The resulting shared public key is serialized in the compressed format and
177+
// hashed with SHA256, resulting in a final key length of 256 bits.
178+
func (s *signerClient) DeriveSharedKey(ctx context.Context,
179+
ephemeralPubKey *btcec.PublicKey,
180+
keyLocator *keychain.KeyLocator) ([32]byte, error) {
181+
182+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
183+
defer cancel()
184+
185+
rpcIn := &signrpc.SharedKeyRequest{
186+
EphemeralPubkey: ephemeralPubKey.SerializeCompressed(),
187+
KeyLoc: &signrpc.KeyLocator{
188+
KeyFamily: int32(keyLocator.Family),
189+
KeyIndex: int32(keyLocator.Index),
190+
},
191+
}
192+
193+
rpcCtx = s.signerMac.WithMacaroonAuth(rpcCtx)
194+
resp, err := s.client.DeriveSharedKey(rpcCtx, rpcIn)
195+
if err != nil {
196+
return [32]byte{}, err
197+
}
198+
199+
var sharedKey [32]byte
200+
copy(sharedKey[:], resp.SharedKey)
201+
return sharedKey, nil
202+
}

test/signer_mock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66

7+
"github.com/btcsuite/btcd/btcec"
78
"github.com/btcsuite/btcd/wire"
89
"github.com/lightningnetwork/lnd/input"
910
"github.com/lightningnetwork/lnd/keychain"
@@ -42,3 +43,9 @@ func (s *mockSigner) VerifyMessage(ctx context.Context, msg, sig []byte,
4243

4344
return mockAssertion, nil
4445
}
46+
47+
func (s *mockSigner) DeriveSharedKey(context.Context, *btcec.PublicKey,
48+
*keychain.KeyLocator) ([32]byte, error) {
49+
50+
return [32]byte{4, 5, 6}, nil
51+
}

0 commit comments

Comments
 (0)