Merge ff408351a2
into 084b64f96d
This commit is contained in:
commit
d5672f065f
27
base58.go
27
base58.go
|
@ -22,38 +22,31 @@ func Base58Encode(input []byte) []byte {
|
||||||
result = append(result, b58Alphabet[mod.Int64()])
|
result = append(result, b58Alphabet[mod.Int64()])
|
||||||
}
|
}
|
||||||
|
|
||||||
ReverseBytes(result)
|
// https://en.bitcoin.it/wiki/Base58Check_encoding#Version_bytes
|
||||||
for b := range input {
|
if input[0] == 0x00 {
|
||||||
if b == 0x00 {
|
result = append(result, b58Alphabet[0])
|
||||||
result = append([]byte{b58Alphabet[0]}, result...)
|
|
||||||
} else {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ReverseBytes(result)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// Base58Decode decodes Base58-encoded data
|
// Base58Decode decodes Base58-encoded data
|
||||||
func Base58Decode(input []byte) []byte {
|
func Base58Decode(input []byte) []byte {
|
||||||
result := big.NewInt(0)
|
result := big.NewInt(0)
|
||||||
zeroBytes := 0
|
|
||||||
|
|
||||||
for b := range input {
|
for _, b := range input {
|
||||||
if b == 0x00 {
|
|
||||||
zeroBytes++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
payload := input[zeroBytes:]
|
|
||||||
for _, b := range payload {
|
|
||||||
charIndex := bytes.IndexByte(b58Alphabet, b)
|
charIndex := bytes.IndexByte(b58Alphabet, b)
|
||||||
result.Mul(result, big.NewInt(58))
|
result.Mul(result, big.NewInt(58))
|
||||||
result.Add(result, big.NewInt(int64(charIndex)))
|
result.Add(result, big.NewInt(int64(charIndex)))
|
||||||
}
|
}
|
||||||
|
|
||||||
decoded := result.Bytes()
|
decoded := result.Bytes()
|
||||||
decoded = append(bytes.Repeat([]byte{byte(0x00)}, zeroBytes), decoded...)
|
|
||||||
|
if input[0] == b58Alphabet[0] {
|
||||||
|
decoded = append([]byte{0x00}, decoded...)
|
||||||
|
}
|
||||||
|
|
||||||
return decoded
|
return decoded
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,24 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/hex"
|
||||||
|
"log"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBase58(t *testing.T) {
|
||||||
|
rawHash := "00010966776006953D5567439E5E39F86A0D273BEED61967F6"
|
||||||
|
hash, err := hex.DecodeString(rawHash)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
encoded := Base58Encode(hash)
|
||||||
|
assert.Equal(t, "16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM", string(encoded))
|
||||||
|
|
||||||
|
decoded := Base58Decode([]byte("16UwLL9Risc3QfPqBUvKofHmBQ7wMtjvM"))
|
||||||
|
assert.Equal(t, strings.ToLower("00010966776006953D5567439E5E39F86A0D273BEED61967F6"), hex.EncodeToString(decoded))
|
||||||
|
}
|
|
@ -56,7 +56,9 @@ func (pow *ProofOfWork) Run() (int, []byte) {
|
||||||
data := pow.prepareData(nonce)
|
data := pow.prepareData(nonce)
|
||||||
|
|
||||||
hash = sha256.Sum256(data)
|
hash = sha256.Sum256(data)
|
||||||
fmt.Printf("\r%x", hash)
|
if math.Remainder(float64(nonce), 100000) == 0 {
|
||||||
|
fmt.Printf("\r%x", hash)
|
||||||
|
}
|
||||||
hashInt.SetBytes(hash[:])
|
hashInt.SetBytes(hash[:])
|
||||||
|
|
||||||
if hashInt.Cmp(pow.target) == -1 {
|
if hashInt.Cmp(pow.target) == -1 {
|
||||||
|
|
|
@ -162,7 +162,7 @@ func (tx *Transaction) Verify(prevTXs map[string]Transaction) bool {
|
||||||
|
|
||||||
dataToVerify := fmt.Sprintf("%x\n", txCopy)
|
dataToVerify := fmt.Sprintf("%x\n", txCopy)
|
||||||
|
|
||||||
rawPubKey := ecdsa.PublicKey{curve, &x, &y}
|
rawPubKey := ecdsa.PublicKey{Curve: curve, X: &x, Y: &y}
|
||||||
if ecdsa.Verify(&rawPubKey, []byte(dataToVerify), &r, &s) == false {
|
if ecdsa.Verify(&rawPubKey, []byte(dataToVerify), &r, &s) == false {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue