Skip to content

Commit

Permalink
fix: SpendTx Payload should actually be a bytearray
Browse files Browse the repository at this point in the history
  • Loading branch information
randomshinichi committed Jul 24, 2019
1 parent 41fb8d4 commit 361aea5
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 23 deletions.
4 changes: 2 additions & 2 deletions aeternity/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,14 @@ func NewContext(client *Node, address string) Context {
}

// SpendTx creates a spend transaction
func (c *Context) SpendTx(senderID string, recipientID string, amount, fee big.Int, payload string) (tx SpendTx, err error) {
func (c *Context) SpendTx(senderID string, recipientID string, amount, fee big.Int, payload []byte) (tx SpendTx, err error) {
txTTL, accountNonce, err := GetTTLNonce(c.Client, c.Address, Config.Client.TTL)
if err != nil {
return
}

// create the transaction
return NewSpendTx(senderID, recipientID, amount, fee, payload, txTTL, accountNonce)
return NewSpendTx(senderID, recipientID, amount, fee, payload, txTTL, accountNonce), err
}

// NamePreclaimTx creates a name preclaim transaction and salt (required for claiming)
Expand Down
2 changes: 2 additions & 0 deletions aeternity/identifiers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const (
PrefixTransactionHash = HashPrefix("th_")

// Base 64 encoded transactions
PrefixByteArray = HashPrefix("ba_")
PrefixContractByteArray = HashPrefix("cb_")
PrefixOracleResponse = HashPrefix("or_")
PrefixOracleQuery = HashPrefix("ov_")
Expand All @@ -50,6 +51,7 @@ const (

// store the encoding
var objectEncoding = map[HashPrefix]ObjectEncoding{
PrefixByteArray: Base64c,
PrefixContractByteArray: Base64c,
PrefixOracleResponse: Base64c,
PrefixOracleQuery: Base64c,
Expand Down
15 changes: 5 additions & 10 deletions aeternity/transactions.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package aeternity

import (
"errors"
"fmt"
"io"
"math/big"
"unicode/utf8"

"github.com/aeternity/aepp-sdk-go/swagguard/node/models"
"github.com/aeternity/aepp-sdk-go/utils"
Expand Down Expand Up @@ -147,7 +145,7 @@ type SpendTx struct {
RecipientID string
Amount big.Int
Fee big.Int
Payload string
Payload []byte
TTL uint64
Nonce uint64
}
Expand Down Expand Up @@ -180,11 +178,12 @@ func (tx *SpendTx) RLP() (rlpRawMsg []byte, err error) {

// JSON representation of a Tx is useful for querying the node's debug endpoint
func (tx *SpendTx) JSON() (string, error) {
baseEncodedPayload := Encode(PrefixByteArray, tx.Payload)
swaggerT := models.SpendTx{
Amount: utils.BigInt(tx.Amount),
Fee: utils.BigInt(tx.Fee),
Nonce: tx.Nonce,
Payload: &tx.Payload,
Payload: &baseEncodedPayload,
RecipientID: &tx.RecipientID,
SenderID: &tx.SenderID,
TTL: tx.TTL,
Expand All @@ -209,12 +208,8 @@ func (tx *SpendTx) FeeEstimate() (*big.Int, error) {
}

// NewSpendTx is a constructor for a SpendTx struct
func NewSpendTx(senderID, recipientID string, amount, fee big.Int, payload string, ttl, nonce uint64) (SpendTx, error) {
payloadValid := utf8.ValidString(payload)
if !payloadValid {
return SpendTx{}, errors.New("Error: SpendTx payload must be valid UTF-8")
}
return SpendTx{senderID, recipientID, amount, fee, payload, ttl, nonce}, nil
func NewSpendTx(senderID, recipientID string, amount, fee big.Int, payload []byte, ttl, nonce uint64) SpendTx {
return SpendTx{senderID, recipientID, amount, fee, payload, ttl, nonce}
}

// NamePreclaimTx represents a transaction where one reserves a name on AENS without revealing it yet
Expand Down
8 changes: 4 additions & 4 deletions aeternity/transactions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestSpendTx_RLP(t *testing.T) {
recipientID string
amount big.Int
fee big.Int
payload string
payload []byte
ttl uint64
nonce uint64
}
Expand All @@ -41,7 +41,7 @@ func TestSpendTx_RLP(t *testing.T) {
recipientID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
amount: *utils.NewIntFromUint64(10),
fee: *utils.NewIntFromUint64(10),
payload: "Hello World",
payload: []byte("Hello World"),
ttl: uint64(10),
nonce: uint64(1),
},
Expand All @@ -55,7 +55,7 @@ func TestSpendTx_RLP(t *testing.T) {
recipientID: "ak_Egp9yVdpxmvAfQ7vsXGvpnyfNq71msbdUpkMNYGTeTe8kPL3v",
amount: *utils.NewIntFromUint64(0),
fee: *utils.NewIntFromUint64(10),
payload: "Hello World",
payload: []byte("Hello World"),
ttl: uint64(10),
nonce: uint64(1),
},
Expand All @@ -65,7 +65,7 @@ func TestSpendTx_RLP(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tx, _ := NewSpendTx(tt.fields.senderID, tt.fields.recipientID,
tx := NewSpendTx(tt.fields.senderID, tt.fields.recipientID,
tt.fields.amount,
tt.fields.fee,
tt.fields.payload,
Expand Down
7 changes: 2 additions & 5 deletions cmd/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,7 @@ func txSpendFunc(cmd *cobra.Command, args []string) (err error) {
}
}

tx, err := aeternity.NewSpendTx(sender, recipient, *amount, *feeBigInt, spendTxPayload, ttl, nonce)
if err != nil {
return err
}
tx := aeternity.NewSpendTx(sender, recipient, *amount, *feeBigInt, []byte(spendTxPayload), ttl, nonce)
base64Tx, err := aeternity.BaseEncodeTx(&tx)
if err != nil {
return err
Expand Down Expand Up @@ -224,5 +221,5 @@ func init() {
txSpendCmd.Flags().StringVar(&fee, "fee", aeternity.Config.Client.Fee.String(), fmt.Sprintf("Set the transaction fee (default=%s)", aeternity.Config.Client.Fee.String()))
txSpendCmd.Flags().Uint64Var(&ttl, "ttl", aeternity.Config.Client.TTL, fmt.Sprintf("Set the TTL in keyblocks (default=%d)", aeternity.Config.Client.TTL))
txSpendCmd.Flags().Uint64Var(&nonce, "nonce", 0, fmt.Sprint("Set the sender account nonce, if not the chain will be queried for its value"))
txSpendCmd.Flags().StringVar(&spendTxPayload, "payload", "", fmt.Sprint("Optional text payload for Spend Transactions"))
txSpendCmd.Flags().StringVar(&spendTxPayload, "payload", "", fmt.Sprint("Optional text payload for Spend Transactions, which will be turned into a bytearray"))
}
2 changes: 1 addition & 1 deletion integration_test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestAPI(t *testing.T) {
if err != nil {
t.Fatalf("Error in GetTTLNonce(): %v", err)
}
spendTx, _ := aeternity.NewSpendTx(sender, bob.Address, *big.NewInt(1000), aeternity.Config.Client.Fee, "", ttl, nonce)
spendTx := aeternity.NewSpendTx(sender, bob.Address, *big.NewInt(1000), aeternity.Config.Client.Fee, []byte(""), ttl, nonce)
_, _, _ = signBroadcastWaitForTransaction(t, &spendTx, alice, privateNet)

name := randomName(6)
Expand Down
2 changes: 1 addition & 1 deletion integration_test/spend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestSpendTx(t *testing.T) {
ctx := aeternity.NewContext(node, alice.Address)

// create the SpendTransaction
tx, err := ctx.SpendTx(alice.Address, bob.Address, *amount, *fee, msg)
tx, err := ctx.SpendTx(alice.Address, bob.Address, *amount, *fee, []byte(msg))
if err != nil {
t.Error(err)
}
Expand Down

0 comments on commit 361aea5

Please sign in to comment.