Skip to content

Commit bbaad0c

Browse files
committed
expose a pubkey from sig method
1 parent a0c12e9 commit bbaad0c

File tree

1 file changed

+16
-18
lines changed

1 file changed

+16
-18
lines changed

verify.go

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,37 +18,35 @@ const (
1818
hBSV string = "Bitcoin Signed Message:\n"
1919
)
2020

21-
// VerifyMessage verifies a string and address against the provided
22-
// signature and assumes Bitcoin Signed Message encoding
23-
//
24-
// Error will occur if verify fails or verification is not successful (no bool)
25-
// Spec: https://docs.moneybutton.com/docs/bsv-message.html
26-
func VerifyMessage(address, sig, data string) error {
27-
21+
// PublicKeyFromSignature gets a publickey for a signature and tells you whether is was compressed
22+
func PublicKeyFromSignature(sig, data string) (pubKey *bsvec.PublicKey, wasCompressed bool, err error) {
2823
decodedSig, err := base64.StdEncoding.DecodeString(sig)
2924
if err != nil {
30-
return err
25+
return nil, false, err
3126
}
3227

3328
// Validate the signature - this just shows that it was valid at all.
3429
// we will compare it with the key next.
3530
var buf bytes.Buffer
36-
if err = wire.WriteVarString(&buf, 0, hBSV); err != nil {
37-
return err
38-
}
39-
if err = wire.WriteVarString(&buf, 0, data); err != nil {
40-
return err
41-
}
31+
wire.WriteVarString(&buf, 0, hBSV)
32+
wire.WriteVarString(&buf, 0, data)
4233

4334
// Create the hash
4435
expectedMessageHash := chainhash.DoubleHashB(buf.Bytes())
36+
return bsvec.RecoverCompact(bsvec.S256(), decodedSig, expectedMessageHash)
37+
}
4538

46-
var publicKey *bsvec.PublicKey
47-
var wasCompressed bool
48-
if publicKey, wasCompressed, err = bsvec.RecoverCompact(bsvec.S256(), decodedSig, expectedMessageHash); err != nil {
39+
// VerifyMessage verifies a string and address against the provided
40+
// signature and assumes Bitcoin Signed Message encoding
41+
//
42+
// Error will occur if verify fails or verification is not successful (no bool)
43+
// Spec: https://docs.moneybutton.com/docs/bsv-message.html
44+
func VerifyMessage(address, sig, data string) error {
45+
46+
publicKey, wasCompressed, err := PublicKeyFromSignature(sig, data)
47+
if err != nil {
4948
return err
5049
}
51-
5250
// Reconstruct the pubkey hash.
5351
var serializedPK []byte
5452
if wasCompressed {

0 commit comments

Comments
 (0)