btcsuite / btcd

An alternative full node bitcoin implementation written in Go (golang)
https://github.com/btcsuite/btcd/blob/master/README.md
ISC License
6.2k stars 2.36k forks source link

Hi, please help me check the error: "malformed signature: no header magic" #1948

Closed elvis-hp closed 1 year ago

elvis-hp commented 1 year ago

I got an error: "malformed signature: no header magic" signature, err := ecdsa.ParseDERSignature(sigBytes) signatureBase64Hex message is was signed from code: https://github.com/bitcoinjs/bitcoinjs-message with segwit format (p2wpkh).

So, with the bellow code, how to Parse the message and verify it with status true. Thank you!

fullcode:


package main

import (
    "encoding/hex"
    "fmt"

    "github.com/btcsuite/btcd/btcec/v2"
    "github.com/btcsuite/btcd/btcec/v2/ecdsa"
    "github.com/btcsuite/btcd/chaincfg/chainhash"
)

func main() {
        signatureBase64Hex := "2782be48c14bbba7750a81534b590963269d310460c2828787630bfad1b5b5c2384308c27a23e7f7bf571084b45e8ccc81da2e950b9c254015b3afb9d15cd37542"
        publicKeyByHex := ""025a2e2b540b4eb8a362db61f9cc0fc9820bf23dc1b9f65a0b421febbd55199408""
    // Decode hex-encoded serialized public key.
    pubKeyBytes, err := hex.DecodeString(publicKeyByHex)
    if err != nil {
        fmt.Println(err)
        return
    }
    pubKey, err := btcec.ParsePubKey(pubKeyBytes)
    if err != nil {
        fmt.Println(err)
        return
    }

    // Decode hex-encoded serialized signature.
    sigBytes, err := hex.DecodeString(signatureBase64Hex)

    if err != nil {
        fmt.Println(err)
        return
    }
    signature, err := ecdsa.ParseDERSignature(sigBytes)
    if err != nil {
        fmt.Println("can't parse ParseSignature: ", err)
        return
    }

    // Verify the signature for the message using the public key.
    message := "Welcome 7b6536ae-9d7e-1c06-16e1-147f981f3caa to Generative"
    messageHash := chainhash.DoubleHashB([]byte(message))
    verified := signature.Verify(messageHash, pubKey)
    fmt.Println("Signature Verified?", verified)
guggero commented 1 year ago

Looking at the linked example code, it's not a DER signature but a compact one. Try this instead: https://github.com/BitonicNL/verify-signed-message/blob/main/pkg/verify.go#L81

elvis-hp commented 1 year ago

btcd

Hi, thank you for reply. But my project using new btcd lib, they had moved to another link, and some function not found! The link you sent me use btcd old lib.

Roasbeef commented 1 year ago

Is the sig actually base64 encoded, then hex encoded as the variable name mentions? If so you'll need to actually decode it. Closing this for now (feel free to option up a discussion).