|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"math/big"
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
"../../cnutil"
|
|
|
|
)
|
|
|
|
|
|
|
|
var Diff1 *big.Int
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Diff1 = new(big.Int)
|
|
|
|
Diff1.SetString("FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", 16)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeTimestamp() int64 {
|
|
|
|
return time.Now().UnixNano() / int64(time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTargetHex(diff int64) string {
|
|
|
|
padded := make([]byte, 32)
|
|
|
|
|
|
|
|
diffBuff := new(big.Int).Div(Diff1, big.NewInt(diff)).Bytes()
|
|
|
|
copy(padded[32-len(diffBuff):], diffBuff)
|
|
|
|
buff := padded[0:4]
|
|
|
|
targetHex := hex.EncodeToString(reverse(buff))
|
|
|
|
return targetHex
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetHashDifficulty(hashBytes []byte) *big.Int {
|
|
|
|
diff := new(big.Int)
|
|
|
|
diff.SetBytes(reverse(hashBytes))
|
|
|
|
return diff.Div(Diff1, diff)
|
|
|
|
}
|
|
|
|
|
|
|
|
func ValidateAddress(addy string, poolAddy string) bool {
|
|
|
|
if len(addy) != len(poolAddy) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
prefix, _ := utf8.DecodeRuneInString(addy)
|
|
|
|
poolPrefix, _ := utf8.DecodeRuneInString(poolAddy)
|
|
|
|
if prefix != poolPrefix {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return cnutil.ValidateAddress(addy)
|
|
|
|
}
|
|
|
|
|
|
|
|
func reverse(src []byte) []byte {
|
|
|
|
dst := make([]byte, len(src))
|
|
|
|
for i := len(src); i > 0; i-- {
|
|
|
|
dst[len(src)-i] = src[i-1]
|
|
|
|
}
|
|
|
|
return dst
|
|
|
|
}
|