提交 6aa390ab 编写于 作者: O obscuren

Merge branch 'conversion' of github.com-obscure:ethereum/go-ethereum into conversion

package types
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
......@@ -156,9 +155,9 @@ func (self *Block) Transactions() Transactions {
return self.transactions
}
func (self *Block) Transaction(hash []byte) *Transaction {
func (self *Block) Transaction(hash common.Hash) *Transaction {
for _, transaction := range self.transactions {
if bytes.Equal(hash, transaction.Hash()) {
if transaction.Hash() == hash {
return transaction
}
}
......
......@@ -2,13 +2,13 @@ package types
import (
"bytes"
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/rlp"
)
......@@ -20,7 +20,7 @@ type Transaction struct {
AccountNonce uint64
Price *big.Int
GasLimit *big.Int
Recipient []byte
Recipient common.Address
Amount *big.Int
Payload []byte
V byte
......@@ -28,31 +28,24 @@ type Transaction struct {
}
func NewContractCreationTx(Amount, gasAmount, price *big.Int, data []byte) *Transaction {
return NewTransactionMessage(nil, Amount, gasAmount, price, data)
return NewTransactionMessage(common.Address{}, Amount, gasAmount, price, data)
}
func NewTransactionMessage(to []byte, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
func NewTransactionMessage(to common.Address, Amount, gasAmount, price *big.Int, data []byte) *Transaction {
return &Transaction{Recipient: to, Amount: Amount, Price: price, GasLimit: gasAmount, Payload: data}
}
func NewTransactionFromBytes(data []byte) *Transaction {
tx := &Transaction{}
tx.RlpDecode(data)
tx := new(Transaction)
rlp.Decode(bytes.NewReader(data), tx)
return tx
}
func NewTransactionFromAmount(val *common.Value) *Transaction {
tx := &Transaction{}
tx.RlpValueDecode(val)
return tx
}
func (tx *Transaction) Hash() []byte {
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
return crypto.Sha3(common.Encode(data))
func (tx *Transaction) Hash() (a common.Hash) {
h := sha3.NewKeccak256()
rlp.Encode(h, []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload})
h.Sum(a[:])
return a
}
func (self *Transaction) Data() []byte {
......@@ -79,11 +72,11 @@ func (self *Transaction) SetNonce(AccountNonce uint64) {
self.AccountNonce = AccountNonce
}
func (self *Transaction) From() []byte {
func (self *Transaction) From() common.Address {
return self.sender()
}
func (self *Transaction) To() []byte {
func (self *Transaction) To() common.Address {
return self.Recipient
}
......@@ -97,48 +90,31 @@ func (tx *Transaction) Curve() (v byte, r []byte, s []byte) {
func (tx *Transaction) Signature(key []byte) []byte {
hash := tx.Hash()
sig, _ := secp256k1.Sign(hash, key)
sig, _ := secp256k1.Sign(hash[:], key)
return sig
}
func (tx *Transaction) PublicKey() []byte {
hash := tx.Hash()
v, r, s := tx.Curve()
sig := append(r, s...)
sig = append(sig, v-27)
//pubkey := crypto.Ecrecover(append(hash, sig...))
pubkey, _ := secp256k1.RecoverPubkey(hash, sig)
pubkey, _ := secp256k1.RecoverPubkey(hash[:], sig)
return pubkey
}
func (tx *Transaction) sender() []byte {
func (tx *Transaction) sender() (a common.Address) {
pubkey := tx.PublicKey()
// Validate the returned key.
// Return nil if public key isn't in full format
if len(pubkey) == 0 || pubkey[0] != 4 {
return nil
return a
}
return crypto.Sha3(pubkey[1:])[12:]
}
// TODO: deprecate after new accounts & key stores are integrated
func (tx *Transaction) Sign(privk []byte) error {
sig := tx.Signature(privk)
tx.R = sig[:32]
tx.S = sig[32:64]
tx.V = sig[64] + 27
return nil
copy(a[:], crypto.Sha3(pubkey[1:]))
return a
}
func (tx *Transaction) SetSignatureValues(sig []byte) error {
......@@ -148,36 +124,17 @@ func (tx *Transaction) SetSignatureValues(sig []byte) error {
return nil
}
func (tx *Transaction) SignECDSA(key *ecdsa.PrivateKey) error {
return tx.Sign(crypto.FromECDSA(key))
}
// TODO: remove
func (tx *Transaction) RlpData() interface{} {
data := []interface{}{tx.AccountNonce, tx.Price, tx.GasLimit, tx.Recipient, tx.Amount, tx.Payload}
return append(data, tx.V, new(big.Int).SetBytes(tx.R).Bytes(), new(big.Int).SetBytes(tx.S).Bytes())
}
// TODO: remove
func (tx *Transaction) RlpEncode() []byte {
return common.Encode(tx)
}
func (tx *Transaction) RlpDecode(data []byte) {
rlp.Decode(bytes.NewReader(data), tx)
}
func (tx *Transaction) RlpValueDecode(decoder *common.Value) {
tx.AccountNonce = decoder.Get(0).Uint()
tx.Price = decoder.Get(1).BigInt()
tx.GasLimit = decoder.Get(2).BigInt()
tx.Recipient = decoder.Get(3).Bytes()
tx.Amount = decoder.Get(4).BigInt()
tx.Payload = decoder.Get(5).Bytes()
tx.V = decoder.Get(6).Byte()
tx.R = decoder.Get(7).Bytes()
tx.S = decoder.Get(8).Bytes()
}
func (tx *Transaction) String() string {
return fmt.Sprintf(`
TX(%x)
......@@ -213,6 +170,7 @@ func (tx *Transaction) String() string {
// Transaction slice type for basic sorting
type Transactions []*Transaction
// TODO: remove
func (self Transactions) RlpData() interface{} {
// Marshal the transactions of this block
enc := make([]interface{}, len(self))
......@@ -223,6 +181,7 @@ func (self Transactions) RlpData() interface{} {
return enc
}
func (s Transactions) Len() int { return len(s) }
func (s Transactions) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s Transactions) GetRlp(i int) []byte { return common.Rlp(s[i]) }
......
......@@ -16,10 +16,10 @@ import (
"errors"
"code.google.com/p/go-uuid/uuid"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/crypto/secp256k1"
"github.com/ethereum/go-ethereum/crypto/sha3"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/pbkdf2"
"golang.org/x/crypto/ripemd160"
)
......@@ -37,6 +37,15 @@ func Sha3(data ...[]byte) []byte {
return d.Sum(nil)
}
func Sha3Hash(data ...[]byte) (h common.Hash) {
d := sha3.NewKeccak256()
for _, b := range data {
d.Write(b)
}
d.Sum(h[:])
return h
}
// Creates an ethereum address given the bytes and the nonce
func CreateAddress(b []byte, nonce uint64) []byte {
return Sha3(common.NewValue([]interface{}{b, nonce}).Encode())[12:]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册