Skip to content

Commit

Permalink
feat: expose MagicVerify to abstract verification process
Browse files Browse the repository at this point in the history
  • Loading branch information
AJ ONeal committed Mar 14, 2022
1 parent d40394c commit 1587b82
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 25 deletions.
28 changes: 3 additions & 25 deletions cmd/dashmsg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,39 +257,17 @@ func verify(args []string) {
addr := string(addrBytes)

msg := readFileOrString(msgname)
magichash := dashmsg.MagicHash(msg)

sigBytes := readFileOrString(signame)
sig := string(sigBytes)

sigBytes, err := base64.StdEncoding.DecodeString(sig)
if nil != err {
fmt.Fprintf(os.Stderr, "error: could not decode signature: %v\n", err)
os.Exit(1)
return
}

pub, err := dashmsg.SigToPub(magichash, sigBytes)
if nil != err {
fmt.Fprintf(os.Stderr, "error: could not verify message: %v\n", err)
if err := dashmsg.MagicVerify(addr, msg, sig); nil != err {
fmt.Fprintf(os.Stderr, "error: %v", err)
os.Exit(1)
return
}

cointype, err := dashmsg.AddressToCointype(addr)
if nil != err {
// Neither a valid file nor string. Blast!
fmt.Printf("can't detect coin type of %q: %v\n", addr, err)
os.Exit(1)
return
}

if dashmsg.PublicKeyToAddress(cointype, *pub) == addr {
fmt.Println("Verified: true")
return
}

fmt.Println("Invalid Signature")
fmt.Println("Verified: true")
}

func readFileOrString(str string) []byte {
Expand Down
34 changes: 34 additions & 0 deletions dashmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/base64"
"encoding/binary"
"encoding/hex"
"fmt"
"io"
"math/big"

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

// Base64 indicates that the given string should be Base64 encoded (std, with padding)
type Base64 = string

// Base58Check indicates that the given string should be in Base58Check encoded (coint type prefix on double hash of public key, BaseX-style Base58 encoding)
type Base58Check = string

// MagicVerify checks that the given public key hash payment address can be used to verify the given base64 signature and arbitrary message
func MagicVerify(addr Base58Check, msg []byte, sig Base64) error {
sigBytes, err := base64.StdEncoding.DecodeString(sig)
if nil != err {
return fmt.Errorf("could not decode signature: %w", err)
}

magichash := MagicHash(msg)
pub, err := SigToPub(magichash, sigBytes)
if nil != err {
return fmt.Errorf("could not verify message: %w", err)
}

cointype, err := AddressToCointype(addr)
if nil != err {
// Neither a valid file nor string. Blast!
return fmt.Errorf("can't detect coin type of %q: %v", addr, err)
}

guess := PublicKeyToAddress(cointype, *pub)
if guess == addr {
return nil
}

return fmt.Errorf("signature's public key hash payment address %q does not match given address %q", guess, addr)
}

// 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)
func SigToPub(magichash, dsig []byte) (*ecdsa.PublicKey, error) {
rsig := make([]byte, 0, 65)
Expand Down

0 comments on commit 1587b82

Please sign in to comment.