Skip to content

Commit

Permalink
refactor: TransactionTypes hashmap between ObjectTag identifiers and …
Browse files Browse the repository at this point in the history
…Tx structs; DeserializeTx(), GetTransactionType(), Transaction interface
  • Loading branch information
randomshinichi committed Aug 13, 2019
1 parent c4dfeba commit 6d720ca
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
21 changes: 21 additions & 0 deletions aeternity/identifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,24 @@ const (
ObjectTagMicroBody uint = 101
ObjectTagLightMicroBlock uint = 102
)

// TransactionTypes is a map between the ObjectTags defined above and the
// corresponding Tx struct
// TODO why can't this be a const?
var TransactionTypes = map[uint]Transaction{
ObjectTagSignedTransaction: &SignedTx{},
ObjectTagSpendTransaction: &SpendTx{},
ObjectTagNameServiceClaimTransaction: &NameClaimTx{},
ObjectTagNameServicePreclaimTransaction: &NamePreclaimTx{},
ObjectTagNameServiceUpdateTransaction: &NameUpdateTx{},
ObjectTagNameServiceRevokeTransaction: &NameRevokeTx{},
ObjectTagNameServiceTransferTransaction: &NameTransferTx{},
ObjectTagOracleRegisterTransaction: &OracleRegisterTx{},
ObjectTagOracleQueryTransaction: &OracleQueryTx{},
ObjectTagOracleResponseTransaction: &OracleRespondTx{},
ObjectTagOracleExtendTransaction: &OracleExtendTx{},
ObjectTagContractCreateTransaction: &ContractCreateTx{},
ObjectTagContractCallTransaction: &ContractCallTx{},
ObjectTagGeneralizedAccountAttachTransaction: &GAAttachTx{},
ObjectTagGeneralizedAccountMetaTransaction: &GAMetaTx{},
}
27 changes: 27 additions & 0 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ import (
rlp "github.com/randomshinichi/rlpae"
)

// Transaction describes what every transaction struct should be able to do
type Transaction interface {
rlp.Encoder
}

// Sign calculates the signature of the SignedTx.Tx. Although it does not use
// the SignedTx itself, it takes a SignedTx as an argument because if it took a
// rlp.Encoder as an interface, one might expect the signature to be of the
Expand Down Expand Up @@ -155,6 +160,28 @@ func SerializeTx(tx rlp.Encoder) (string, error) {
return txStr, nil
}

// DeserializeTx takes a tx_ string and returns the corresponding Tx struct
func DeserializeTx(txRLP string) (Transaction, error) {
rawRLP, err := Decode(txRLP)
if err != nil {
return nil, err
}

tx, err := GetTransactionType(rawRLP)
if err != nil {
return nil, err
}
err = rlp.DecodeBytes(rawRLP, tx)
return tx, err
}

// GetTransactionType reads the RLP input and returns a blank Tx struct of the correct type
func GetTransactionType(rawRLP []byte) (tx Transaction, err error) {
f := DecodeRLPMessage(rawRLP)[0] // [33] interface, needs to be cast to []uint8
objTag := uint(f.([]uint8)[0]) // [33] cast to []uint8, get rid of the slice, cast to uint
return TransactionTypes[objTag], nil
}

// SignedTx wraps around other Tx structs to hold the signature.
type SignedTx struct {
Signatures [][]byte
Expand Down

0 comments on commit 6d720ca

Please sign in to comment.