Esempio n. 1
0
func (b *Block) Mine(c chan *Block) {
	target := targetFromBits(b.Bits)
	i := big.NewInt(int64(0))
	var n uint32
	buf := bytes.NewBuffer([]byte{})
	buf.Grow(192)
	buf1 := bytes.NewBuffer([]byte{})
	buf1.Grow(32)
loop:
	for n = 0; n < 4294967295; n++ {
		binary.Write(buf, binary.LittleEndian, b.PreviousBlockHash)
		binary.Write(buf, binary.LittleEndian, b.MerkleRootHash)
		binary.Write(buf, binary.LittleEndian, b.Version)
		binary.Write(buf, binary.LittleEndian, b.Timestamp)
		binary.Write(buf, binary.LittleEndian, b.Bits)
		binary.Write(buf, binary.LittleEndian, n) // current nonce
		hash := fastsha256.Sum256(buf.Bytes())
		binary.Write(buf1, binary.BigEndian, hash)
		hash = fastsha256.Sum256(buf1.Bytes())
		buf1.Reset()
		binary.Write(buf1, binary.BigEndian, hash)
		i.SetBytes(buf1.Bytes())
		buf1.Reset()
		buf.Reset()
		if i.Cmp(target) == -1 {
			b.Nonce = n
			c <- b
			return
		}
	}
	// Update timestamp and restart the process
	b.Timestamp = time.Now().UTC().Unix()
	goto loop
}
Esempio n. 2
0
func Miner(blockChan chan *Block, minedBlockChan chan *Block) {
	var b *Block
init:
	b = <-blockChan
	if b == nil {
		goto init
	}
mine:
	target := targetFromBits(b.Bits)
	i := big.NewInt(int64(0))
	var n uint32
	buf := bytes.NewBuffer([]byte{})
	buf.Grow(192)
	buf1 := bytes.NewBuffer([]byte{})
	buf1.Grow(32)
loop:
	for n = 0; n < 4294967295; n++ {
		select {
		case b = <-blockChan:
			if b == nil { // cancel mining
				goto init
			} else {
				goto mine
			}
		default:
			binary.Write(buf, binary.LittleEndian, b.PreviousBlockHash)
			binary.Write(buf, binary.LittleEndian, b.MerkleRootHash)
			binary.Write(buf, binary.LittleEndian, b.Version)
			binary.Write(buf, binary.LittleEndian, b.Timestamp)
			binary.Write(buf, binary.LittleEndian, b.Bits)
			binary.Write(buf, binary.LittleEndian, n) // current nonce
			hash := fastsha256.Sum256(buf.Bytes())
			binary.Write(buf1, binary.BigEndian, hash)
			hash = fastsha256.Sum256(buf1.Bytes())
			buf1.Reset()
			binary.Write(buf1, binary.BigEndian, hash)
			i.SetBytes(buf1.Bytes())
			buf1.Reset()
			buf.Reset()
			if i.Cmp(target) == -1 {
				b.Nonce = n
				minedBlockChan <- b
				goto init
			}
			runtime.Gosched()
		}
	}
	// Update timestamp and restart the process
	b.Timestamp = time.Now().UTC().Unix()
	goto loop
}
Esempio n. 3
0
func ECDSAPublicKeyToString(key ecdsa.PublicKey) string {
	x := key.X.Bytes()
	y := key.Y.Bytes()
	sha := fastsha256.Sum256(append(append([]byte{0x04}, x...), y...)) // should it be 0x04?
	ripe := ripemd160.New().Sum(sha[:])
	ripesha := fastsha256.Sum256(ripe)
	ripedoublesha := fastsha256.Sum256(ripesha[:])
	head := ripedoublesha[0:3]
	final := append(ripe, head...)
	i := new(big.Int)
	i.SetBytes(final)
	var b []byte
	return string(base58.EncodeBig(b, i))
}
Esempio n. 4
0
func hash(t T) []byte {
	buf := new(bytes.Buffer)
	gob.NewEncoder(buf).Encode(t)
	encoded := buf.Bytes()
	buf.Reset()
	binary.Write(buf, binary.BigEndian, fastsha256.Sum256(encoded))
	return buf.Bytes()
}
Esempio n. 5
0
func (b *Block) Hash() types.Hash {
	buf := bytes.NewBuffer([]byte{})
	buf.Grow(192)
	buf1 := bytes.NewBuffer([]byte{})
	buf1.Grow(32)
	binary.Write(buf, binary.LittleEndian, b.PreviousBlockHash)
	binary.Write(buf, binary.LittleEndian, b.MerkleRootHash)
	binary.Write(buf, binary.LittleEndian, b.Version)
	binary.Write(buf, binary.LittleEndian, b.Timestamp)
	binary.Write(buf, binary.LittleEndian, b.Bits)
	binary.Write(buf, binary.LittleEndian, b.Nonce)
	hash := fastsha256.Sum256(buf.Bytes())
	binary.Write(buf1, binary.BigEndian, hash)
	hash = fastsha256.Sum256(buf1.Bytes())
	buf1.Reset()
	binary.Write(buf1, binary.BigEndian, hash)
	return buf1.Bytes()
}
Esempio n. 6
0
func NewNameReservation(name string, publicKey *ecdsa.PublicKey) (txn *NameReservation, random []byte) {
	buf := make([]byte, 4)
	rand.Read(buf)
	return &NameReservation{
			Version:   NAME_RESERVATION_VERSION,
			Hashed:    fastsha256.Sum256(append([]byte(name), buf...)),
			PublicKey: []byte(keys.ECDSAPublicKeyToString(*publicKey))},
		buf
}
Esempio n. 7
0
// checkAuth checks the HTTP Basic authentication supplied by a wallet
// or RPC client in the HTTP request r.  If the supplied authentication
// does not match the username and password expected, a non-nil error is
// returned.
//
// This check is time-constant.
func (s *rpcServer) checkAuth(r *http.Request, require bool) (bool, error) {
	authhdr := r.Header["Authorization"]
	if len(authhdr) <= 0 {
		if require {
			rpcsLog.Warnf("RPC authentication failure from %s",
				r.RemoteAddr)
			return false, errors.New("auth failure")
		}

		return false, nil
	}

	authsha := fastsha256.Sum256([]byte(authhdr[0]))
	cmp := subtle.ConstantTimeCompare(authsha[:], s.authsha[:])
	if cmp != 1 {
		rpcsLog.Warnf("RPC authentication failure from %s", r.RemoteAddr)
		return false, errors.New("auth failure")
	}
	return true, nil
}
Esempio n. 8
0
func SHA256(buf []byte) []byte {
	result := fastsha256.Sum256(buf)
	return result[:]
}
Esempio n. 9
0
// newRPCServer returns a new instance of the rpcServer struct.
func newRPCServer(listenAddrs []string, s *server) (*rpcServer, error) {
	login := cfg.RPCUser + ":" + cfg.RPCPass
	auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(login))
	rpc := rpcServer{
		authsha: fastsha256.Sum256([]byte(auth)),
		server:  s,
		quit:    make(chan int),
	}
	rpc.ntfnMgr = newWsNotificationManager(&rpc)

	// check for existence of cert file and key file
	if !fileExists(cfg.RPCKey) && !fileExists(cfg.RPCCert) {
		// if both files do not exist, we generate them.
		err := genCertPair(cfg.RPCCert, cfg.RPCKey)
		if err != nil {
			return nil, err
		}
	}
	keypair, err := tls.LoadX509KeyPair(cfg.RPCCert, cfg.RPCKey)
	if err != nil {
		return nil, err
	}

	tlsConfig := tls.Config{
		Certificates: []tls.Certificate{keypair},
	}

	// TODO(oga) this code is similar to that in server, should be
	// factored into something shared.
	ipv4ListenAddrs, ipv6ListenAddrs, _, err := parseListeners(listenAddrs)
	if err != nil {
		return nil, err
	}
	listeners := make([]net.Listener, 0,
		len(ipv6ListenAddrs)+len(ipv4ListenAddrs))
	for _, addr := range ipv4ListenAddrs {
		listener, err := tls.Listen("tcp4", addr, &tlsConfig)
		if err != nil {
			rpcsLog.Warnf("Can't listen on %s: %v", addr,
				err)
			continue
		}
		listeners = append(listeners, listener)
	}

	for _, addr := range ipv6ListenAddrs {
		listener, err := tls.Listen("tcp6", addr, &tlsConfig)
		if err != nil {
			rpcsLog.Warnf("Can't listen on %s: %v", addr,
				err)
			continue
		}
		listeners = append(listeners, listener)
	}
	if len(listeners) == 0 {
		return nil, errors.New("RPCS: No valid listen address")
	}

	rpc.listeners = listeners

	return &rpc, nil
}