Skip to content

Commit 1587b82

Browse files
author
AJ ONeal
committed
feat: expose MagicVerify to abstract verification process
1 parent d40394c commit 1587b82

File tree

2 files changed

+37
-25
lines changed

2 files changed

+37
-25
lines changed

cmd/dashmsg/main.go

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -257,39 +257,17 @@ func verify(args []string) {
257257
addr := string(addrBytes)
258258

259259
msg := readFileOrString(msgname)
260-
magichash := dashmsg.MagicHash(msg)
261260

262261
sigBytes := readFileOrString(signame)
263262
sig := string(sigBytes)
264263

265-
sigBytes, err := base64.StdEncoding.DecodeString(sig)
266-
if nil != err {
267-
fmt.Fprintf(os.Stderr, "error: could not decode signature: %v\n", err)
268-
os.Exit(1)
269-
return
270-
}
271-
272-
pub, err := dashmsg.SigToPub(magichash, sigBytes)
273-
if nil != err {
274-
fmt.Fprintf(os.Stderr, "error: could not verify message: %v\n", err)
264+
if err := dashmsg.MagicVerify(addr, msg, sig); nil != err {
265+
fmt.Fprintf(os.Stderr, "error: %v", err)
275266
os.Exit(1)
276267
return
277268
}
278269

279-
cointype, err := dashmsg.AddressToCointype(addr)
280-
if nil != err {
281-
// Neither a valid file nor string. Blast!
282-
fmt.Printf("can't detect coin type of %q: %v\n", addr, err)
283-
os.Exit(1)
284-
return
285-
}
286-
287-
if dashmsg.PublicKeyToAddress(cointype, *pub) == addr {
288-
fmt.Println("Verified: true")
289-
return
290-
}
291-
292-
fmt.Println("Invalid Signature")
270+
fmt.Println("Verified: true")
293271
}
294272

295273
func readFileOrString(str string) []byte {

dashmsg.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"encoding/base64"
99
"encoding/binary"
1010
"encoding/hex"
11+
"fmt"
1112
"io"
1213
"math/big"
1314

@@ -124,6 +125,39 @@ func MagicSign(priv *ecdsa.PrivateKey, msg []byte) ([]byte, error) {
124125
return sig, nil
125126
}
126127

128+
// Base64 indicates that the given string should be Base64 encoded (std, with padding)
129+
type Base64 = string
130+
131+
// Base58Check indicates that the given string should be in Base58Check encoded (coint type prefix on double hash of public key, BaseX-style Base58 encoding)
132+
type Base58Check = string
133+
134+
// MagicVerify checks that the given public key hash payment address can be used to verify the given base64 signature and arbitrary message
135+
func MagicVerify(addr Base58Check, msg []byte, sig Base64) error {
136+
sigBytes, err := base64.StdEncoding.DecodeString(sig)
137+
if nil != err {
138+
return fmt.Errorf("could not decode signature: %w", err)
139+
}
140+
141+
magichash := MagicHash(msg)
142+
pub, err := SigToPub(magichash, sigBytes)
143+
if nil != err {
144+
return fmt.Errorf("could not verify message: %w", err)
145+
}
146+
147+
cointype, err := AddressToCointype(addr)
148+
if nil != err {
149+
// Neither a valid file nor string. Blast!
150+
return fmt.Errorf("can't detect coin type of %q: %v", addr, err)
151+
}
152+
153+
guess := PublicKeyToAddress(cointype, *pub)
154+
if guess == addr {
155+
return nil
156+
}
157+
158+
return fmt.Errorf("signature's public key hash payment address %q does not match given address %q", guess, addr)
159+
}
160+
127161
// SigToPub computes the public key from the message's magichash and the recovery signature (has the magic byte, a.k.a. "i" at the front of it)
128162
func SigToPub(magichash, dsig []byte) (*ecdsa.PublicKey, error) {
129163
rsig := make([]byte, 0, 65)

0 commit comments

Comments
 (0)