Esempio n. 1
0
func TestBinaryDecode(t *testing.T) {

	privAccount := GenPrivAccount()
	pubKey := privAccount.PubKey
	privKey := privAccount.PrivKey

	msg := CRandBytes(128)
	sig := privKey.Sign(msg)
	t.Logf("msg: %X, sig: %X", msg, sig)

	buf, n, err := new(bytes.Buffer), new(int64), new(error)
	wire.WriteBinary(sig, buf, n, err)
	if *err != nil {
		t.Fatalf("Failed to write Signature: %v", err)
	}

	if len(buf.Bytes()) != ed25519.SignatureSize+1 {
		// 1 byte TypeByte, 64 bytes signature bytes
		t.Fatalf("Unexpected signature write size: %v", len(buf.Bytes()))
	}
	if buf.Bytes()[0] != SignatureTypeEd25519 {
		t.Fatalf("Unexpected signature type byte")
	}

	sig2, ok := wire.ReadBinary(SignatureEd25519{}, buf, n, err).(SignatureEd25519)
	if !ok || *err != nil {
		t.Fatalf("Failed to read Signature: %v", err)
	}

	// Test the signature
	if !pubKey.VerifyBytes(msg, sig2) {
		t.Errorf("Account message signature verification failed")
	}
}
Esempio n. 2
0
// General Convenience
func SimpleHashFromBinary(item interface{}) []byte {
	hasher, n, err := ripemd160.New(), new(int64), new(error)
	wire.WriteBinary(item, hasher, n, err)
	if *err != nil {
		PanicCrisis(err)
	}
	return hasher.Sum(nil)
}
Esempio n. 3
0
func UpdateNameReg(fileName, hash string) error {
	nodeAddr := os.Getenv("MINTX_NODE_ADDR")
	signAddr := os.Getenv("MINTX_SIGN_ADDR")
	chainID := os.Getenv("MINTX_CHAINID")
	pubkey := strings.TrimSpace(os.Getenv("MINTX_PUBKEY")) //because bash
	addr := ""
	amtS := "10000"
	nonceS := ""
	feeS := "0"
	name := fileName
	data := hash

	log.WithFields(log.Fields{
		"MINTX_NODE_ADDR": nodeAddr,
		"MINTX_CHAINID":   chainID,
		"MINTX_PUBKEY":    pubkey,
		"MINTX_SIGN_ADDR": signAddr,
		"name":            fileName,
		"data":            data,
		"amount":          amtS,
	}).Warn("Building a nameTx:")

	nTx, err := core.Name(nodeAddr, signAddr, pubkey, addr, amtS, nonceS, feeS, name, data)
	if err != nil {
		return fmt.Errorf("corename error: %v\n", err)
	}
	log.WithField("=>", fmt.Sprintf("%v", nTx)).Warn("Success, nameTx created:")

	//sign but don't broadcast

	log.Warn("Signing transaction...")
	_, err = core.SignAndBroadcast(chainID, nodeAddr, signAddr, nTx, true, false, false)
	if err != nil {
		return fmt.Errorf("sign error: %v\n", err)
	}

	n := new(int64)
	w := new(bytes.Buffer)
	wire.WriteBinary(nTx, w, n, &err)

	// post needs to be to toadserver endpoint, which'll
	// eventually route the TX to the nodes using mindy
	txD := bytes.NewReader(w.Bytes())
	//it can also query for the name reg to ensure things are good
	// TODO => modularize
	endpoint := "http://0.0.0.0:11113/" + "receiveNameTx?hash=" + hash
	_, err = http.Post(endpoint, "", txD)
	if err != nil {
		return fmt.Errorf("post error: %v\n", err)
	}
	if err := os.Remove(fileName); err != nil {
		return fmt.Errorf("remove file error: %v\n", err)
	}
	return nil

}
Esempio n. 4
0
func (s *State) Save() {
	s.accounts.Save()
	s.validatorInfos.Save()
	s.nameReg.Save()
	buf, n, err := new(bytes.Buffer), new(int64), new(error)
	wire.WriteString(s.ChainID, buf, n, err)
	wire.WriteVarint(s.LastBlockHeight, buf, n, err)
	wire.WriteByteSlice(s.LastBlockHash, buf, n, err)
	wire.WriteBinary(s.LastBlockParts, buf, n, err)
	wire.WriteTime(s.LastBlockTime, buf, n, err)
	wire.WriteBinary(s.BondedValidators, buf, n, err)
	wire.WriteBinary(s.LastBondedValidators, buf, n, err)
	wire.WriteBinary(s.UnbondingValidators, buf, n, err)
	wire.WriteByteSlice(s.accounts.Hash(), buf, n, err)
	wire.WriteByteSlice(s.validatorInfos.Hash(), buf, n, err)
	wire.WriteByteSlice(s.nameReg.Hash(), buf, n, err)
	if *err != nil {
		PanicCrisis(*err)
	}
	s.DB.Set(stateKey, buf.Bytes())
}
Esempio n. 5
0
// TODO: Slicing the array gives us length prefixing but loses the type byte.
// Revisit if we add more pubkey types.
// For now, we artificially append the type byte in front to give us backwards
// compatibility for when the pubkey wasn't fixed length array
func (pubKey PubKeyEd25519) Address() []byte {
	w, n, err := new(bytes.Buffer), new(int64), new(error)
	wire.WriteBinary(pubKey[:], w, n, err)
	if *err != nil {
		PanicCrisis(*err)
	}
	// append type byte
	encodedPubkey := append([]byte{1}, w.Bytes()...)
	hasher := ripemd160.New()
	hasher.Write(encodedPubkey) // does not error
	return hasher.Sum(nil)
}
Esempio n. 6
0
func (kv KVPair) Hash() []byte {
	hasher, n, err := ripemd160.New(), new(int64), new(error)
	wire.WriteString(kv.Key, hasher, n, err)
	if kvH, ok := kv.Value.(Hashable); ok {
		wire.WriteByteSlice(kvH.Hash(), hasher, n, err)
	} else {
		wire.WriteBinary(kv.Value, hasher, n, err)
	}
	if *err != nil {
		PanicSanity(*err)
	}
	return hasher.Sum(nil)
}
Esempio n. 7
0
// NOTE: blocking
// Before creating a peer with newPeer(), perform a handshake on connection.
func peerHandshake(conn net.Conn, ourNodeInfo *types.NodeInfo) (*types.NodeInfo, error) {
	var peerNodeInfo = new(types.NodeInfo)
	var err1 error
	var err2 error
	Parallel(
		func() {
			var n int64
			wire.WriteBinary(ourNodeInfo, conn, &n, &err1)
		},
		func() {
			var n int64
			wire.ReadBinary(peerNodeInfo, conn, &n, &err2)
			log.Notice("Peer handshake", "peerNodeInfo", peerNodeInfo)
		})
	if err1 != nil {
		return nil, err1
	}
	if err2 != nil {
		return nil, err2
	}
	return peerNodeInfo, nil
}
Esempio n. 8
0
func AccountEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	wire.WriteBinary(o.(*Account), w, n, err)
}
Esempio n. 9
0
func NameRegEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	wire.WriteBinary(o.(*types.NameRegEntry), w, n, err)
}
Esempio n. 10
0
func ValidatorInfoEncoder(o interface{}, w io.Writer, n *int64, err *error) {
	wire.WriteBinary(o.(*ValidatorInfo), w, n, err)
}
Esempio n. 11
0
func (vc validatorCodec) Encode(o interface{}, w io.Writer, n *int64, err *error) {
	wire.WriteBinary(o.(*Validator), w, n, err)
}