Browse Source

lndclient: expose ComputeInputScript in SignerClient

pull/155/head
Wilmer Paulino 1 year ago
parent
commit
0b053e6868
No known key found for this signature in database GPG Key ID: 6DF57B9F9514972F
2 changed files with 66 additions and 7 deletions
  1. +59
    -7
      lndclient/signer_client.go
  2. +7
    -0
      test/signer_mock.go

+ 59
- 7
lndclient/signer_client.go View File

@ -17,6 +17,13 @@ type SignerClient interface {
SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([][]byte, error)
// ComputeInputScript generates the proper input script for P2WPKH
// output and NP2WPKH outputs. This method only requires that the
// `Output`, `HashType`, `SigHashes` and `InputIndex` fields are
// populated within the sign descriptors.
ComputeInputScript(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([]*input.Script, error)
// SignMessage signs a message with the key specified in the key
// locator. The returned signature is fixed-size LN wire format encoded.
SignMessage(ctx context.Context, msg []byte,
@ -56,13 +63,8 @@ func newSignerClient(conn *grpc.ClientConn,
}
}
func (s *signerClient) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([][]byte, error) {
txRaw, err := swap.EncodeTx(tx)
if err != nil {
return nil, err
}
func marshallSignDescriptors(signDescriptors []*input.SignDescriptor,
) []*signrpc.SignDescriptor {
rpcSignDescs := make([]*signrpc.SignDescriptor, len(signDescriptors))
for i, signDesc := range signDescriptors {
@ -103,6 +105,18 @@ func (s *signerClient) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
}
}
return rpcSignDescs
}
func (s *signerClient) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([][]byte, error) {
txRaw, err := swap.EncodeTx(tx)
if err != nil {
return nil, err
}
rpcSignDescs := marshallSignDescriptors(signDescriptors)
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
defer cancel()
@ -120,6 +134,44 @@ func (s *signerClient) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
return resp.RawSigs, nil
}
// ComputeInputScript generates the proper input script for P2WPKH output and
// NP2WPKH outputs. This method only requires that the `Output`, `HashType`,
// `SigHashes` and `InputIndex` fields are populated within the sign
// descriptors.
func (s *signerClient) ComputeInputScript(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([]*input.Script, error) {
txRaw, err := swap.EncodeTx(tx)
if err != nil {
return nil, err
}
rpcSignDescs := marshallSignDescriptors(signDescriptors)
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
defer cancel()
rpcCtx = s.signerMac.WithMacaroonAuth(rpcCtx)
resp, err := s.client.ComputeInputScript(
rpcCtx, &signrpc.SignReq{
RawTxBytes: txRaw,
SignDescs: rpcSignDescs,
},
)
if err != nil {
return nil, err
}
inputScripts := make([]*input.Script, 0, len(resp.InputScripts))
for _, inputScript := range resp.InputScripts {
inputScripts = append(inputScripts, &input.Script{
SigScript: inputScript.SigScript,
Witness: inputScript.Witness,
})
}
return inputScripts, nil
}
// SignMessage signs a message with the key specified in the key locator. The
// returned signature is fixed-size LN wire format encoded.
func (s *signerClient) SignMessage(ctx context.Context, msg []byte,

+ 7
- 0
test/signer_mock.go View File

@ -3,6 +3,7 @@ package test
import (
"bytes"
"context"
"fmt"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire"
@ -27,6 +28,12 @@ func (s *mockSigner) SignOutputRaw(ctx context.Context, tx *wire.MsgTx,
return rawSigs, nil
}
func (s *mockSigner) ComputeInputScript(ctx context.Context, tx *wire.MsgTx,
signDescriptors []*input.SignDescriptor) ([]*input.Script, error) {
return nil, fmt.Errorf("unimplemented")
}
func (s *mockSigner) SignMessage(ctx context.Context, msg []byte,
locator keychain.KeyLocator) ([]byte, error) {

Loading…
Cancel
Save