Implement ValidateAddress

This commit is contained in:
Ivan Kuznetsov 2017-09-10 13:53:14 +07:00
parent 7290aaac64
commit a436da6c19
4 changed files with 50 additions and 15 deletions

View File

@ -1,8 +1,14 @@
package main
import "fmt"
import (
"fmt"
"log"
)
func (cli *CLI) createBlockchain(address string) {
if !ValidateAddress(address) {
log.Panic("ERROR: Address is not valid")
}
bc := CreateBlockchain(address)
bc.db.Close()
fmt.Println("Done!")

View File

@ -1,8 +1,14 @@
package main
import "fmt"
import (
"fmt"
"log"
)
func (cli *CLI) getBalance(address string) {
if !ValidateAddress(address) {
log.Panic("ERROR: Address is not valid")
}
bc := NewBlockchain(address)
defer bc.db.Close()

View File

@ -1,8 +1,18 @@
package main
import "fmt"
import (
"fmt"
"log"
)
func (cli *CLI) send(from, to string, amount int) {
if !ValidateAddress(from) {
log.Panic("ERROR: Sender address is not valid")
}
if !ValidateAddress(to) {
log.Panic("ERROR: Recipient address is not valid")
}
bc := NewBlockchain(from)
defer bc.db.Close()

View File

@ -1,6 +1,7 @@
package main
import (
"bytes"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
@ -12,6 +13,7 @@ import (
const version = byte(0x00)
const walletFile = "wallet.dat"
const addressChecksumLen = 4
// Wallet stores private and public keys
type Wallet struct {
@ -40,17 +42,6 @@ func (w Wallet) GetAddress() []byte {
return address
}
func newKeyPair() (ecdsa.PrivateKey, []byte) {
curve := elliptic.P256()
private, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
log.Panic(err)
}
pubKey := append(private.PublicKey.X.Bytes(), private.PublicKey.Y.Bytes()...)
return *private, pubKey
}
// HashPubKey hashes public key
func HashPubKey(pubKey []byte) []byte {
publicSHA256 := sha256.Sum256(pubKey)
@ -65,10 +56,32 @@ func HashPubKey(pubKey []byte) []byte {
return publicRIPEMD160
}
// ValidateAddress check if address if valid
func ValidateAddress(address string) bool {
pubKeyHash := Base58Decode([]byte(address))
actualChecksum := pubKeyHash[len(pubKeyHash)-addressChecksumLen:]
version := pubKeyHash[0]
pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-addressChecksumLen]
targetChecksum := checksum(append([]byte{version}, pubKeyHash...))
return bytes.Compare(actualChecksum, targetChecksum) == 0
}
// Checksum generates a checksum for a public key
func checksum(payload []byte) []byte {
firstSHA := sha256.Sum256(payload)
secondSHA := sha256.Sum256(firstSHA[:])
return secondSHA[:4]
return secondSHA[:addressChecksumLen]
}
func newKeyPair() (ecdsa.PrivateKey, []byte) {
curve := elliptic.P256()
private, err := ecdsa.GenerateKey(curve, rand.Reader)
if err != nil {
log.Panic(err)
}
pubKey := append(private.PublicKey.X.Bytes(), private.PublicKey.Y.Bytes()...)
return *private, pubKey
}