151 lines
4.0 KiB
Go
151 lines
4.0 KiB
Go
package types
|
|
|
|
import (
|
|
"encoding/json"
|
|
|
|
. "github.com/tendermint/go-common"
|
|
"github.com/tendermint/go-crypto"
|
|
"github.com/tendermint/go-wire"
|
|
tmsp "github.com/tendermint/tmsp/types"
|
|
)
|
|
|
|
/*
|
|
Tx (Transaction) is an atomic operation on the ledger state.
|
|
|
|
Account Types:
|
|
- SendTx Send coins to address
|
|
- AppTx Send a msg to a contract that runs in the vm
|
|
*/
|
|
|
|
type Tx interface {
|
|
AssertIsTx()
|
|
SignBytes(chainID string) []byte
|
|
}
|
|
|
|
// Types of Tx implementations
|
|
const (
|
|
// Account transactions
|
|
TxTypeSend = byte(0x01)
|
|
TxTypeApp = byte(0x02)
|
|
)
|
|
|
|
func (_ *SendTx) AssertIsTx() {}
|
|
func (_ *AppTx) AssertIsTx() {}
|
|
|
|
var _ = wire.RegisterInterface(
|
|
struct{ Tx }{},
|
|
wire.ConcreteType{&SendTx{}, TxTypeSend},
|
|
wire.ConcreteType{&AppTx{}, TxTypeApp},
|
|
)
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
type TxInput struct {
|
|
Address []byte `json:"address"` // Hash of the PubKey
|
|
Amount int64 `json:"amount"` // Must not exceed account balance
|
|
Sequence int `json:"sequence"` // Must be 1 greater than the last committed TxInput
|
|
Signature crypto.Signature `json:"signature"` // Depends on the PubKey type and the whole Tx
|
|
PubKey crypto.PubKey `json:"pub_key"` // May be nil
|
|
}
|
|
|
|
func (txIn TxInput) ValidateBasic() tmsp.Result {
|
|
if len(txIn.Address) != 20 {
|
|
return tmsp.ErrBaseInvalidAddress.AppendLog("(in TxInput)")
|
|
}
|
|
if txIn.Amount == 0 {
|
|
return tmsp.ErrBaseInvalidAmount.AppendLog("(in TxInput)")
|
|
}
|
|
return tmsp.OK
|
|
}
|
|
|
|
func (txIn TxInput) String() string {
|
|
return Fmt("TxInput{%X,%v,%v,%v,%v}", txIn.Address, txIn.Amount, txIn.Sequence, txIn.Signature, txIn.PubKey)
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
type TxOutput struct {
|
|
Address []byte `json:"address"` // Hash of the PubKey
|
|
Amount int64 `json:"amount"` // The sum of all outputs must not exceed the inputs.
|
|
}
|
|
|
|
func (txOut TxOutput) ValidateBasic() tmsp.Result {
|
|
if len(txOut.Address) != 20 {
|
|
return tmsp.ErrBaseInvalidAddress.AppendLog("(in TxOutput)")
|
|
}
|
|
if txOut.Amount == 0 {
|
|
return tmsp.ErrBaseInvalidAmount.AppendLog("(in TxOutput)")
|
|
}
|
|
return tmsp.OK
|
|
}
|
|
|
|
func (txOut TxOutput) String() string {
|
|
return Fmt("TxOutput{%X,%v}", txOut.Address, txOut.Amount)
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
type SendTx struct {
|
|
Inputs []TxInput `json:"inputs"`
|
|
Outputs []TxOutput `json:"outputs"`
|
|
}
|
|
|
|
func (tx *SendTx) SignBytes(chainID string) []byte {
|
|
signBytes := wire.BinaryBytes(chainID)
|
|
sigz := make([]crypto.Signature, len(tx.Inputs))
|
|
for i, input := range tx.Inputs {
|
|
sigz[i] = input.Signature
|
|
tx.Inputs[i].Signature = nil
|
|
}
|
|
signBytes = append(signBytes, wire.BinaryBytes(tx)...)
|
|
for i := range tx.Inputs {
|
|
tx.Inputs[i].Signature = sigz[i]
|
|
}
|
|
return signBytes
|
|
}
|
|
|
|
func (tx *SendTx) String() string {
|
|
return Fmt("SendTx{%v -> %v}", tx.Inputs, tx.Outputs)
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
type AppTx struct {
|
|
Type byte `json:"type"` // Which app
|
|
Gas int64 `json:"gas"`
|
|
Fee int64 `json:"fee"`
|
|
Input TxInput `json:"input"`
|
|
Data []byte `json:"data"`
|
|
}
|
|
|
|
func (tx *AppTx) SignBytes(chainID string) []byte {
|
|
signBytes := wire.BinaryBytes(chainID)
|
|
sig := tx.Input.Signature
|
|
tx.Input.Signature = nil
|
|
signBytes = append(signBytes, wire.BinaryBytes(tx)...)
|
|
tx.Input.Signature = sig
|
|
return signBytes
|
|
}
|
|
|
|
func (tx *AppTx) String() string {
|
|
return Fmt("AppTx{%v %v %v %v -> %X}", tx.Type, tx.Gas, tx.Fee, tx.Input, tx.Data)
|
|
}
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
func TxID(chainID string, tx Tx) []byte {
|
|
signBytes := tx.SignBytes(chainID)
|
|
return wire.BinaryRipemd160(signBytes)
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------
|
|
|
|
// Contract: This function is deterministic and completely reversible.
|
|
func jsonEscape(str string) string {
|
|
escapedBytes, err := json.Marshal(str)
|
|
if err != nil {
|
|
PanicSanity(Fmt("Error json-escaping a string", str))
|
|
}
|
|
return string(escapedBytes)
|
|
}
|