Skip to content

Commit

Permalink
Minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mrz1836 committed Oct 30, 2020
1 parent e3ed0ba commit 18d3119
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
20 changes: 14 additions & 6 deletions sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,26 @@ func SignMessage(privateKey string, message string) (string, error) {
}

var buf bytes.Buffer
wire.WriteVarString(&buf, 0, hBSV)
wire.WriteVarString(&buf, 0, message)
var err error
if err = wire.WriteVarString(&buf, 0, hBSV); err != nil {
return "", err
}
if err = wire.WriteVarString(&buf, 0, message); err != nil {
return "", err
}

// Create the hash
messageHash := chainhash.DoubleHashB(buf.Bytes())

ecdsaPrivateKey, err := PrivateKeyFromString(privateKey)
if err != nil {
// Get the private key
var ecdsaPrivateKey *bsvec.PrivateKey
if ecdsaPrivateKey, err = PrivateKeyFromString(privateKey); err != nil {
return "", err
}

// Sign
var sigBytes []byte
sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, messageHash, true)
if err != nil {
if sigBytes, err = bsvec.SignCompact(bsvec.S256(), ecdsaPrivateKey, messageHash, true); err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(sigBytes), nil
Expand Down
23 changes: 15 additions & 8 deletions verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,31 @@ func VerifyMessage(address, sig, data string) error {
// Validate the signature - this just shows that it was valid at all.
// we will compare it with the key next.
var buf bytes.Buffer
wire.WriteVarString(&buf, 0, hBSV)
wire.WriteVarString(&buf, 0, data)
if err = wire.WriteVarString(&buf, 0, hBSV); err != nil {
return err
}
if err = wire.WriteVarString(&buf, 0, data); err != nil {
return err
}

// Create the hash
expectedMessageHash := chainhash.DoubleHashB(buf.Bytes())

pk, wasCompressed, err := bsvec.RecoverCompact(bsvec.S256(), decodedSig, expectedMessageHash)
if err != nil {
var publicKey *bsvec.PublicKey
var wasCompressed bool
if publicKey, wasCompressed, err = bsvec.RecoverCompact(bsvec.S256(), decodedSig, expectedMessageHash); err != nil {
return err
}

// Reconstruct the pubkey hash.
var serializedPK []byte
if wasCompressed {
serializedPK = pk.SerializeCompressed()
serializedPK = publicKey.SerializeCompressed()
} else {
serializedPK = pk.SerializeUncompressed()
serializedPK = publicKey.SerializeUncompressed()
}
bsvecAddress, err := bsvutil.NewAddressPubKey(serializedPK, &chaincfg.MainNetParams)
if err != nil {
var bsvecAddress *bsvutil.AddressPubKey
if bsvecAddress, err = bsvutil.NewAddressPubKey(serializedPK, &chaincfg.MainNetParams); err != nil {
return err
}

Expand Down
5 changes: 1 addition & 4 deletions verify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,7 @@ func TestVerifyMessageSigRecoverFailed(t *testing.T) {
exp.X.SetHex(vs[i][4])
exp.Y.SetHex(vs[i][5])

var success bool
success = secp256k1.RecoverPublicKey(sig.R.Bytes(), sig.S.Bytes(), msg.Bytes(), int(rid), &pubkey)

if success {
if secp256k1.RecoverPublicKey(sig.R.Bytes(), sig.S.Bytes(), msg.Bytes(), int(rid), &pubkey) {
t.Fatalf("sigRecover should have failed")
}
}
Expand Down

0 comments on commit 18d3119

Please sign in to comment.