lnwire: update Sig to support both ECDSA and schnorr sigs

In this commit, we update the Sig type to support ECDSA and schnorr
signatures. We need to do this as the HTLC signatures will become
schnorr sigs for taproot channels. The current spec draft opts to
overload this field since both the sigs are actually 64 bytes in length.
The only consideration with this move is that callers need to "coerce" a
sig to the proper type if they need schnorr signatures.
This commit is contained in:
Olaoluwa Osuntokun
2023-01-16 19:33:21 -08:00
parent eccc77315b
commit b368e476c5
29 changed files with 296 additions and 128 deletions

View File

@@ -91,8 +91,10 @@ func Decode(invoice string, net *chaincfg.Params) (*Invoice, error) {
if err != nil {
return nil, err
}
var sig lnwire.Sig
copy(sig[:], sigBase256[:64])
sig, err := lnwire.NewSigFromWireECDSA(sigBase256[:64])
if err != nil {
return nil, err
}
recoveryID := sigBase256[64]
// The signature is over the hrp + the data the invoice, encoded in
@@ -121,7 +123,7 @@ func Decode(invoice string, net *chaincfg.Params) (*Invoice, error) {
}
} else {
headerByte := recoveryID + 27 + 4
compactSign := append([]byte{headerByte}, sig[:]...)
compactSign := append([]byte{headerByte}, sig.RawBytes()...)
pubkey, _, err := ecdsa.RecoverCompact(compactSign, hash)
if err != nil {
return nil, err

View File

@@ -91,8 +91,10 @@ func (invoice *Invoice) Encode(signer MessageSigner) (string, error) {
// From the header byte we can extract the recovery ID, and the last 64
// bytes encode the signature.
recoveryID := sign[0] - 27 - 4
var sig lnwire.Sig
copy(sig[:], sign[1:])
sig, err := lnwire.NewSigFromWireECDSA(sign[1:])
if err != nil {
return "", err
}
// If the pubkey field was explicitly set, it must be set to the pubkey
// used to create the signature.
@@ -112,7 +114,10 @@ func (invoice *Invoice) Encode(signer MessageSigner) (string, error) {
}
// Convert the signature to base32 before writing it to the buffer.
signBase32, err := bech32.ConvertBits(append(sig[:], recoveryID), 8, 5, true)
signBase32, err := bech32.ConvertBits(
append(sig.RawBytes(), recoveryID),
8, 5, true,
)
if err != nil {
return "", err
}