/
tx.go
57 lines (48 loc) · 1.61 KB
/
tx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package types
import (
"bytes"
"errors"
abci "github.com/tendermint/abci/types"
common "github.com/tendermint/go-common"
"github.com/tendermint/go-crypto"
"github.com/tendermint/go-wire"
)
// Tx (Transaction) is an atomic operation on the ledger state.
type Tx interface {
TxType() byte
SignBytes(chainID string) []byte
}
// SignedTx extends Tx with a method to generate signatures.
type SignedTx interface {
TxType() byte
SignBytes(chainID string) []byte
SignTx(privateKey crypto.PrivKey, chainID string) error
}
// TxExecutor validates Tx execution permission
type TxExecutor interface {
CanExecTx(byte) bool
}
// TxBasicValidator implements basic validation rules.
type TxBasicValidator interface {
ValidateBasic() abci.Result
}
// CanExecTx is a convenience function that validates
// caller's execution permission on a Tx.
func CanExecTx(executor TxExecutor, tx Tx) bool {
return executor.CanExecTx(tx.TxType())
}
var _ = wire.RegisterInterface(
struct{ Tx }{},
wire.ConcreteType{O: &TransferTx{}, Byte: TxTypeTransfer},
wire.ConcreteType{O: &CreateAccountTx{}, Byte: TxTypeCreateAccount},
wire.ConcreteType{O: &CreateLegalEntityTx{}, Byte: TxTypeCreateLegalEntity},
wire.ConcreteType{O: &CreateUserTx{}, Byte: TxTypeCreateUser},
)
// SignTx signs the transaction if its address and the privateKey's one match.
func SignTx(signedBytes []byte, addr []byte, privKey crypto.PrivKey) (crypto.Signature, error) {
if !bytes.Equal(privKey.PubKey().Address(), addr) {
return nil, errors.New(common.Fmt("SignTx: addresses mismatch: %x != %x",
privKey.PubKey().Address(), addr))
}
return privKey.Sign(signedBytes), nil
}