func (service *TransactionService) GetTransaction(r *http.Request, args *GetTransactionArgs, reply *GetTransactionReply) error {
	hash, err := hex.DecodeString(args.Hash)
	if err != nil {
		return err
	}
	var tx *transaction.Envelope

	tx, err = service.srv.DB.GetTransaction(hash)
	if err != nil {
		return err
	}

	if tx == nil {
		block, err := service.srv.DB.GetTransactionBlock(hash)
		if err != nil {
			return err
		}
		for i := range block.Transactions {
			if block.Transactions[i].Hash().Equals(hash) {
				tx = block.Transactions[i]
				break
			}
		}
	}
	if tx == nil {
		return err
	}
	reply.PreviousTransactionHash = hex.EncodeToString(tx.PreviousEnvelopeHash)
	pk, err := keys.DecodeECDSAPublicKey(tx.PublicKey)
	if err != nil {
		return err
	}
	reply.PublicKey = keys.ECDSAPublicKeyToString(*pk)
	pk, err = keys.DecodeECDSAPublicKey(tx.PublicKey)
	if err != nil {
		return err
	}
	reply.NextPublicKey = keys.ECDSAPublicKeyToString(*pk)
	reply.Valid = tx.Transaction.Valid()
	jsonEncoded, err := json.Marshal(tx.Transaction)
	if err != nil {
		return err
	}
	reply.Content = string(jsonEncoded)
	return nil
}
Beispiel #2
0
func (e *Envelope) Verify() (bool, error) {
	publicKey, err := keys.DecodeECDSAPublicKey(e.PublicKey)
	if err != nil {
		return false, err
	}
	r := new(big.Int)
	s := new(big.Int)
	r.SetBytes(e.SignatureR)
	s.SetBytes(e.SignatureS)
	return ecdsa.Verify(publicKey, e.Hash(), r, s), nil
}
Beispiel #3
0
func (*KeyAuth) Valid(b []byte) bool {
	var data [][]byte
	dec := gob.NewDecoder(bytes.NewBuffer(b))
	dec.Decode(&data)
	publicKey, err := keys.DecodeECDSAPublicKey(data[0])
	if err != nil {
		panic(err) // TODO: better error handling
	}
	r := new(big.Int)
	s := new(big.Int)
	r.SetBytes(data[1])
	s.SetBytes(data[2])
	return ecdsa.Verify(publicKey, util.SHA256(data[0]), r, s)
}
Beispiel #4
0
func TestGetPreviousEnvelopeHashForPublicKey(t *testing.T) {
	transactions, _ := fixtureSampleTransactions(t)

	block, err := block.NewBlock(types.EmptyHash(), block.HIGHEST_TARGET, transactions)
	if err != nil {
		t.Errorf("can't create a block because of %v", err)
	}

	db, err := NewDB("test.db")
	defer os.Remove("test.db")

	if err != nil {
		t.Errorf("error opening database: %v", err)
	}
	err = db.PutBlock(block, false)
	if err != nil {
		t.Errorf("error putting block: %v", err)
	}

	dec, err := keys.DecodeECDSAPublicKey(transactions[2].NextPublicKey)
	if err != nil {
		t.Errorf("error decoding ECDSA pubkey: %v", err)
	}
	tx, err := db.GetPreviousEnvelopeHashForPublicKey(dec)
	if err != nil {
		t.Errorf("error getting previous transaction's for a pubkey: %v", err)
	}
	assert.True(t, bytes.Compare(tx, transactions[2].Hash()) == 0)

	privateKey := generateECDSAKey(t)

	tx, err = db.GetPreviousEnvelopeHashForPublicKey(&privateKey.PublicKey)
	if err != nil {
		t.Errorf("error getting previous transaction's for a pubkey: %v", err)
	}
	assert.Nil(t, tx)

}