Merge pull request #4423 from breez/enable-encode-invoice-via-rpc
Let invoice.Encode receive a function which hashes itself the message
This commit is contained in:
commit
ef8a61db99
@ -10,6 +10,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/btcsuite/btcd/chaincfg"
|
"github.com/btcsuite/btcd/chaincfg"
|
||||||
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
||||||
"github.com/btcsuite/btcd/wire"
|
"github.com/btcsuite/btcd/wire"
|
||||||
"github.com/btcsuite/btcutil"
|
"github.com/btcsuite/btcutil"
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
@ -329,7 +330,10 @@ func AddInvoice(ctx context.Context, cfg *AddInvoiceConfig,
|
|||||||
|
|
||||||
payReqString, err := payReq.Encode(
|
payReqString, err := payReq.Encode(
|
||||||
zpay32.MessageSigner{
|
zpay32.MessageSigner{
|
||||||
SignCompact: cfg.NodeSigner.SignDigestCompact,
|
SignCompact: func(msg []byte) ([]byte, error) {
|
||||||
|
hash := chainhash.HashB(msg)
|
||||||
|
return cfg.NodeSigner.SignDigestCompact(hash)
|
||||||
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -70,12 +70,11 @@ func (invoice *Invoice) Encode(signer MessageSigner) (string, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
toSign := append([]byte(hrp), taggedFieldsBytes...)
|
toSign := append([]byte(hrp), taggedFieldsBytes...)
|
||||||
hash := chainhash.HashB(toSign)
|
|
||||||
|
|
||||||
// We use compact signature format, and also encoded the recovery ID
|
// We use compact signature format, and also encoded the recovery ID
|
||||||
// such that a reader of the invoice can recover our pubkey from the
|
// such that a reader of the invoice can recover our pubkey from the
|
||||||
// signature.
|
// signature.
|
||||||
sign, err := signer.SignCompact(hash)
|
sign, err := signer.SignCompact(toSign)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
@ -95,6 +94,7 @@ func (invoice *Invoice) Encode(signer MessageSigner) (string, error) {
|
|||||||
"signature: %v", err)
|
"signature: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
hash := chainhash.HashB(toSign)
|
||||||
valid := signature.Verify(hash, invoice.Destination)
|
valid := signature.Verify(hash, invoice.Destination)
|
||||||
if !valid {
|
if !valid {
|
||||||
return "", fmt.Errorf("signature does not match " +
|
return "", fmt.Errorf("signature does not match " +
|
||||||
|
@ -100,11 +100,11 @@ var (
|
|||||||
// MessageSigner is passed to the Encode method to provide a signature
|
// MessageSigner is passed to the Encode method to provide a signature
|
||||||
// corresponding to the node's pubkey.
|
// corresponding to the node's pubkey.
|
||||||
type MessageSigner struct {
|
type MessageSigner struct {
|
||||||
// SignCompact signs the passed hash with the node's privkey. The
|
// SignCompact signs the hash of the passed msg with the node's privkey.
|
||||||
// returned signature should be 65 bytes, where the last 64 are the
|
// The returned signature should be 65 bytes, where the last 64 are the
|
||||||
// compact signature, and the first one is a header byte. This is the
|
// compact signature, and the first one is a header byte. This is the
|
||||||
// format returned by btcec.SignCompact.
|
// format returned by btcec.SignCompact.
|
||||||
SignCompact func(hash []byte) ([]byte, error)
|
SignCompact func(msg []byte) ([]byte, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Invoice represents a decoded invoice, or to-be-encoded invoice. Some of the
|
// Invoice represents a decoded invoice, or to-be-encoded invoice. Some of the
|
||||||
|
@ -101,9 +101,9 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
testMessageSigner = MessageSigner{
|
testMessageSigner = MessageSigner{
|
||||||
SignCompact: func(hash []byte) ([]byte, error) {
|
SignCompact: func(msg []byte) ([]byte, error) {
|
||||||
sig, err := btcec.SignCompact(btcec.S256(),
|
sig, err := btcec.SignCompact(btcec.S256(),
|
||||||
testPrivKey, hash, true)
|
testPrivKey, chainhash.HashB(msg), true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't sign the "+
|
return nil, fmt.Errorf("can't sign the "+
|
||||||
"message: %v", err)
|
"message: %v", err)
|
||||||
@ -915,7 +915,8 @@ func TestInvoiceChecksumMalleability(t *testing.T) {
|
|||||||
|
|
||||||
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes)
|
privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), privKeyBytes)
|
||||||
msgSigner := MessageSigner{
|
msgSigner := MessageSigner{
|
||||||
SignCompact: func(hash []byte) ([]byte, error) {
|
SignCompact: func(msg []byte) ([]byte, error) {
|
||||||
|
hash := chainhash.HashB(msg)
|
||||||
return btcec.SignCompact(btcec.S256(), privKey, hash, true)
|
return btcec.SignCompact(btcec.S256(), privKey, hash, true)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user