split b58 checksum validation from address validation

This commit is contained in:
Jack Robison 2017-11-27 10:24:10 -05:00
parent f53da5c3e5
commit 6d169425d1
No known key found for this signature in database
GPG key ID: 284699E7404E3CFF
6 changed files with 34 additions and 22 deletions

View file

@ -1,8 +1,8 @@
package base58
import (
"math/big"
"errors"
"math/big"
)
var b58_characters = [58]byte{0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x41, 0x42, 0x43, 0x44, 0x45,
@ -11,7 +11,6 @@ var b58_characters = [58]byte {0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0
0x6a, 0x6b, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a}
func CharacterIndex(character byte) (*big.Int, error) {
for i := 0; i < 58; i++ {
if b58_characters[i] == character {

View file

@ -0,0 +1,20 @@
package base58
import "crypto/sha256"
const checksum_length = 4
func VerifyBase58Checksum(v []byte) bool {
checksum := [checksum_length]byte{}
for i := range checksum {
checksum[i] = v[len(v)-checksum_length+i]
}
real_checksum := sha256.Sum256(v[:len(v)-checksum_length])
real_checksum = sha256.Sum256(real_checksum[:])
for i, c := range checksum {
if c != real_checksum[i] {
return false
}
}
return true
}

View file

@ -1,8 +1,8 @@
package base58
import (
"math/big"
"errors"
"math/big"
)
func DecodeBase58(value string, size int64) ([]byte, error) {
@ -32,5 +32,8 @@ func DecodeBase58(value string, size int64) ([]byte, error) {
if long_value.Int64() != 0 {
return result, errors.New("cannot decode to the given size")
}
if size != int64(len(result)) {
return result, errors.New("length mismatch")
}
return result, nil
}

View file

@ -4,7 +4,7 @@ import (
"math/big"
)
func EncodeBase58(data []byte) (string) {
func EncodeBase58(data []byte) string {
long_value := big.NewInt(0)
result := ""
for i := 0; i < len(data); i++ {

View file

@ -14,5 +14,6 @@ func DecodeAddress(address string, blockchainName string) ([address_length]byte,
for i, b := range decoded {
buf[i] = b
}
return ValidateAddress(buf, blockchainName)
}

View file

@ -1,7 +1,7 @@
package address
import (
"crypto/sha256"
"./base58"
"errors"
)
@ -48,19 +48,8 @@ func PubKeyIsValid(address [address_length]byte) bool {
return true
}
func ChecksumIsValid(address [address_length]byte) bool {
checksum := [checksum_length]byte{}
for i := range checksum {
checksum[i] = address[prefix_length+pubkey_length+i]
}
real_checksum := sha256.Sum256(address[:prefix_length+pubkey_length])
real_checksum = sha256.Sum256(real_checksum[:])
for i, c := range checksum {
if c != real_checksum[i] {
return false
}
}
return true
func AddressChecksumIsValid(address [address_length]byte) bool {
return base58.VerifyBase58Checksum(address[:])
}
func ValidateAddress(address [address_length]byte, blockchainName string) ([address_length]byte, error) {
@ -73,7 +62,7 @@ func ValidateAddress(address [address_length]byte, blockchainName string) ([addr
if !PubKeyIsValid(address) {
return address, errors.New("invalid pubkey")
}
if !ChecksumIsValid(address) {
if !AddressChecksumIsValid(address) {
return address, errors.New("invalid address checksum")
}
return address, nil