Skip to content

Commit

Permalink
Fixed ValidateChecksum in wif module
Browse files Browse the repository at this point in the history
  • Loading branch information
Dag Robøle committed Aug 8, 2013
1 parent a5abbcb commit bd1fd9d
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 deletions.
4 changes: 2 additions & 2 deletions address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func New(addressVersion, stream uint64, eighteenByteRipe bool) (*address, error)
return addr, nil
}

func Validate(address string) (bool, error) {
func ValidateChecksum(address string) (bool, error) {

b, err := base58.Decode(address[3:])
if err != nil {
Expand All @@ -116,7 +116,7 @@ func Validate(address string) (bool, error) {

func GetStream(address string) (uint64, error) {

valid, err := Validate(address)
valid, err := ValidateChecksum(address)
if err != nil {
return 0, err
}
Expand Down
2 changes: 1 addition & 1 deletion address/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestAddress(t *testing.T) {
t.Error("Address does not start with correct prefix. Want BM-2D, got %s\n", addr.Identifier[:5])
}

valid, err := Validate(addr.Identifier)
valid, err := ValidateChecksum(addr.Identifier)
if err != nil {
t.Error(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion pow/pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func Nonce(payload []byte) uint64 {
return nonce
}

func Validate(payload []byte) bool {
func ValidateNonce(payload []byte) bool {

if len(payload) < 2 {
return false
Expand Down
2 changes: 1 addition & 1 deletion pow/pow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestPOW(t *testing.T) {
new_payload := varint.Encode(nonce)
new_payload = append(new_payload, payload...)

if !Validate(new_payload) {
if !ValidateNonce(new_payload) {
t.Error("Nonce %d is not valid for payload", nonce)
}
}
5 changes: 2 additions & 3 deletions wif/wif.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func Decode(wif string) (*bitecdsa.PrivateKey, error) {
return keys, nil
}

func Validate(wif string) (bool, error) {
func ValidateChecksum(wif string) (bool, error) {

if len(wif) < 6 {
return false, errors.New("wif.Validate: wif is too short")
Expand All @@ -75,10 +75,9 @@ func Validate(wif string) (bool, error) {
if err != nil {
return false, err
}
raw := extended[1 : len(extended)-4]
cs1 := extended[len(extended)-4:]
sha1, sha2 := sha256.New(), sha256.New()
sha1.Write(raw)
sha1.Write(extended[:len(extended)-4])
sha2.Write(sha1.Sum(nil))
cs2 := sha2.Sum(nil)[:4]
return bytes.Compare(cs1, cs2) == 0, nil
Expand Down
11 changes: 10 additions & 1 deletion wif/wif_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"bitmessage-go/bitelliptic"
)

func TestKeys(t *testing.T) {
func TestWIF(t *testing.T) {

if testing.Short() {
t.Skip("skipping test in short mode.")
Expand Down Expand Up @@ -47,6 +47,15 @@ func TestKeys(t *testing.T) {
t.Error("Public point is not on curve\n")
}

ok, err := ValidateChecksum(wif)
if err != nil {
t.Error(err.Error())
}

if !ok {
t.Error("Invalid checksum")
}

// 0437a3191fe90d9b483324c28ecd019479e708cfcff96800131c113ec30a0646ee95c31b4c5656b1e7122f071ae4471a97511f372179147277ea2a2087147f9486

keys3, err := Decode("5HtKNfWZH4QQZPUGRadud7wfyPGEKLhQJfnYPGvpiivgwfrHfpX")
Expand Down

0 comments on commit bd1fd9d

Please sign in to comment.