Skip to content

Commit

Permalink
More work on tests etc
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Sep 30, 2020
1 parent 3fe1af1 commit 4fbb8e3
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 49 deletions.
3 changes: 2 additions & 1 deletion .make/Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ ifdef HAS_GIT
ifdef HAS_REPO
## Automatically detect the repo owner and repo name (for local use with Git)
REPO_NAME=$(shell basename "$(shell git rev-parse --show-toplevel 2> /dev/null)")
REPO_OWNER=$(shell git config --get remote.origin.url | sed 's/git@$(GIT_DOMAIN)://g' | sed 's/\/$(REPO_NAME).git//g')
OWNER=$(shell git config --get remote.origin.url | sed 's/git@$(GIT_DOMAIN)://g' | sed 's/\/$(REPO_NAME).git//g')
REPO_OWNER=$(shell echo $(OWNER) | tr A-Z a-z)
VERSION_SHORT=$(shell git describe --tags --always --abbrev=0)
export REPO_NAME, REPO_OWNER, VERSION_SHORT
endif
Expand Down
7 changes: 5 additions & 2 deletions address.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,14 @@ func (a *A25) ComputeChecksum() (c [4]byte) {

// AddressFromPrivateKey takes a private key string and returns a Bitcoin address
func AddressFromPrivateKey(privateKey string) (string, error) {
pubKey := PrivateKey(privateKey).PubKey()
address, err := Address(pubKey)
pubKey, err := PrivateKey(privateKey)
if err != nil {
return "", err
}
var address *bsvutil.LegacyAddressPubKeyHash
if address, err = Address(pubKey.PubKey()); err != nil {
return "", err
}
return address.EncodeAddress(), nil
}

Expand Down
16 changes: 8 additions & 8 deletions private_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ package bitcoin

import (
"crypto/ecdsa"
"encoding/hex"
"math/big"

"github.com/bitcoinsv/bsvd/bsvec"
)

// PrivateKey turns a private key string into an bsvec.PrivateKey
func PrivateKey(privKey string) (ecdsaPrivKey *bsvec.PrivateKey) {
privKeyBytes := HexDecode(privKey)
x, y := bsvec.S256().ScalarBaseMult(privKeyBytes)
func PrivateKey(privateKey string) (*bsvec.PrivateKey, error) {
privateKeyBytes, err := hex.DecodeString(privateKey)
if err != nil {
return nil, err
}
x, y := bsvec.S256().ScalarBaseMult(privateKeyBytes)
ecdsaPubKey := ecdsa.PublicKey{
Curve: bsvec.S256(),
X: x,
Y: y,
}
ecdsaPrivKey = &bsvec.PrivateKey{
PublicKey: ecdsaPubKey,
D: new(big.Int).SetBytes(privKeyBytes),
}
return
return &bsvec.PrivateKey{PublicKey: ecdsaPubKey, D: new(big.Int).SetBytes(privateKeyBytes)}, nil
}
17 changes: 10 additions & 7 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,22 @@ import (
)

// SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding
func SignMessage(privKey string, message string) string {
prefixBytes := []byte("Bitcoin Signed Message:\n")
func SignMessage(privateKey string, message string) (string, error) {
prefixBytes := []byte(hBSV)
messageBytes := []byte(message)
var bytes []byte
bytes = append(bytes, byte(len(prefixBytes)))
bytes = append(bytes, prefixBytes...)
bytes = append(bytes, byte(len(messageBytes)))
bytes = append(bytes, messageBytes...)
ecdsaPrivKey := PrivateKey(privKey)

sigbytes, err := bsvec.SignCompact(bsvec.S256(), ecdsaPrivKey, chainhash.DoubleHashB(bytes), true)
ecdsaPrivateKey, err := PrivateKey(privateKey)
if err != nil {
return "", err
}
var sigBytes []byte
sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, chainhash.DoubleHashB(bytes), true)
if err != nil {
panic(err)
return "", err
}
return base64.StdEncoding.EncodeToString(sigbytes)
return base64.StdEncoding.EncodeToString(sigBytes), nil
}
21 changes: 6 additions & 15 deletions sign_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,18 @@ import (
)

// Identity Private Key
const privKey = "E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75"
const privateKey = "E83385AF76B2B1997326B567461FB73DD9C27EAB9E1E86D26779F4650C5F2B75"

// TestSignMessage will test the method SignMessage()
func TestSignMessage(t *testing.T) {

// privKey string, message string, compress bool
sig := SignMessage(privKey, "Testing!")
// privateKey string, message string, compress bool
sig, err := SignMessage(privateKey, "Testing!")
if err != nil {
t.Fatalf("error occurred: %s", err.Error())
}

if sig != "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8=" {
t.Error("Failed to generate expected signature", sig)
}
}

// TestVerifyMessage will test the method VerifyMessage()
func TestVerifyMessage(t *testing.T) {

var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"

if err := VerifyMessage(address, sig, data); err != nil {
t.Fatalf("failed to verify message: %s", err.Error())
}
}
10 changes: 0 additions & 10 deletions util.go

This file was deleted.

13 changes: 7 additions & 6 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ import (
"golang.org/x/crypto/ripemd160"
)

func sha256d(body []byte) []byte {
msgHash1 := sha256.Sum256(body)
msgHash2 := sha256.Sum256(msgHash1[:])
return msgHash2[:]
}

const (
// H_BSV is the magic header string required fore Bitcoin Signed Messages
hBSV string = "Bitcoin Signed Message:\n"
Expand All @@ -36,6 +30,13 @@ func VerifyMessage(address, signature, data string) error {
return fmt.Errorf("address: %s not found", address)
}

// sha256d is a double sha256
func sha256d(body []byte) []byte {
msgHash1 := sha256.Sum256(body)
msgHash2 := sha256.Sum256(msgHash1[:])
return msgHash2[:]
}

// messageHash will compute a hash for the given message & header
func messageHash(message, header string) ([]byte, error) {
headerLength := len(header)
Expand Down
43 changes: 43 additions & 0 deletions verify_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package bitcoin

import (
"fmt"
"testing"
)

// TestVerifyMessage will test the method VerifyMessage()
func TestVerifyMessage(t *testing.T) {

var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"

if err := VerifyMessage(address, sig, data); err != nil {
t.Fatalf("failed to verify message: %s", err.Error())
}
}

// ExampleVerifyMessage example using VerifyMessage()
func ExampleVerifyMessage() {
var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"

if err := VerifyMessage(address, sig, data); err != nil {
fmt.Printf("error occurred: %s", err.Error())
return
}
fmt.Printf("verification passed")
// Output:verification passed
}

// BenchmarkVerifyMessage benchmarks the method VerifyMessage()
func BenchmarkVerifyMessage(b *testing.B) {
var sig = "IBDscOd/Ov4yrd/YXantqajSAnW4fudpfr2KQy5GNo9pZybF12uNaal4KI822UpQLS/UJD+UK2SnNMn6Z3E4na8="
var address = "1FiyJnrgwBc3Ff83V1yRWAkmXBdGrDQnXQ"
var data = "Testing!"

for i := 0; i < b.N; i++ {
_ = VerifyMessage(address, sig, data)
}
}

0 comments on commit 4fbb8e3

Please sign in to comment.