forked from BitonicNL/verify-signed-message
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
118 lines (98 loc) · 4.17 KB
/
verify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package verifier
import (
"encoding/base64"
"errors"
"fmt"
"github.com/hlee4real/verify-signed-message/internal"
"github.com/hlee4real/verify-signed-message/internal/flags"
"github.com/hlee4real/verify-signed-message/internal/signature"
"reflect"
"strings"
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/samber/lo"
)
// ExpectedSignatureLength contains the fixed signature length all signed messages are expected to have.
const ExpectedSignatureLength = 65
// Verify will verify a SignedMessage based on the recovery flag on Bitcoin main network.
func Verify(sig SignedMessage) (bool, error) {
return VerifyWithChain(sig, &chaincfg.MainNetParams)
}
// VerifyWithChain will verify a SignedMessage based on the recovery flag on the passed network.
// Supported address types are P2PKH, P2WKH, NP2WKH (P2WPKH), P2TR.
func VerifyWithChain(signedMessage SignedMessage, net *chaincfg.Params) (bool, error) {
// Check if message contains spaces that can be trimmed, if so run the verification with the trimmed message
// This is required because Electrum trims messages before signing
if trimmedMessage := strings.TrimSpace(signedMessage.Message); len(signedMessage.Message) != len(trimmedMessage) {
// We only care about this return if it's valid
if verified, err := Verify(SignedMessage{Message: trimmedMessage, Address: signedMessage.Address, Signature: signedMessage.Signature}); err == nil && verified {
return true, nil
}
}
// Decode the address
address, err := btcutil.DecodeAddress(signedMessage.Address, net)
if err != nil {
return false, fmt.Errorf("could not decode address: %w", err)
}
// Decode the signature
signatureEncoded, err := base64.StdEncoding.DecodeString(signedMessage.Signature)
if err != nil {
return false, err
}
// Ensure signature has proper length
if len(signatureEncoded) != ExpectedSignatureLength {
return false, fmt.Errorf("wrong signature length: %d instead of 65", len(signatureEncoded))
}
signatureEncoded[0] = 31
// Ensure signature has proper recovery flag
recoveryFlag := int(signatureEncoded[0])
if !lo.Contains[int](flags.All(), recoveryFlag) {
return false, fmt.Errorf("invalid recovery flag: %d", recoveryFlag)
}
// Retrieve KeyID
keyID := flags.GetKeyID(recoveryFlag)
// Should address be compressed (for checking later)
compressed := flags.ShouldBeCompressed(recoveryFlag)
// Reset recovery flag after obtaining keyID for Trezor
if lo.Contains[int](flags.Trezor(), recoveryFlag) {
signatureEncoded[0] = byte(27 + keyID)
}
// Make the magic message
magicMessage := internal.CreateMagicMessage(signedMessage.Message)
// Hash the message
messageHash := chainhash.DoubleHashB([]byte(magicMessage))
// Recover the public key from signature and message hash
publicKey, comp, err := ecdsa.RecoverCompact(signatureEncoded, messageHash)
if err != nil {
return false, fmt.Errorf("could not recover pubkey: %w", err)
}
// Ensure our initial assumption was correct, except for Trezor as they do something different
if compressed != comp && !lo.Contains[int](flags.Trezor(), recoveryFlag) {
return false, errors.New("we expected the key to be compressed, it wasn't")
}
// Verify that the signature is valid
if err := signature.Verify(signatureEncoded, publicKey, messageHash); err != nil {
return false, err
}
// Get the hash from the public key, so we can check that address matches
publicKeyHash := internal.GeneratePublicKeyHash(recoveryFlag, publicKey)
switch address.(type) {
// Validate P2PKH
case *btcutil.AddressPubKeyHash:
return internal.ValidateP2PKH(recoveryFlag, publicKeyHash, address, net)
// Validate P2SH
case *btcutil.AddressScriptHash:
return internal.ValidateP2SH(recoveryFlag, publicKeyHash, address, net)
// Validate P2WPKH
case *btcutil.AddressWitnessPubKeyHash:
return internal.ValidateP2WPKH(recoveryFlag, publicKeyHash, address, net)
// Validate P2TR
case *btcutil.AddressTaproot:
return internal.ValidateP2TR(recoveryFlag, publicKey, address, net)
// Unsupported address
default:
return false, fmt.Errorf("unsupported address type '%s'", reflect.TypeOf(address))
}
}