Skip to content

Commit

Permalink
data inclusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Sun Hyuk Ahn committed Dec 19, 2023
1 parent ba01a18 commit daa2c65
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
9 changes: 8 additions & 1 deletion cmd/subcommands/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ var (
transferFileFlags []transferFlags
timeout uint32
timeFormat = "2006-01-02 15:04:05.000000"
data string
)

type transactionLog struct {
Expand Down Expand Up @@ -169,6 +170,11 @@ func handlerForTransaction(txLog *transactionLog) error {
gLimit = uint64(tempLimit)
}

dataByte, err := transaction.StringToByte(data)
if err != nil {
return handlerForError(txLog, err)
}

addr := toAddress.String()

txLog.TimeSigned = time.Now().UTC().Format(timeFormat) // Approximate time of signature
Expand All @@ -177,7 +183,7 @@ func handlerForTransaction(txLog *transactionLog) error {
&addr,
fromShardID, toShardID,
amt, gPrice,
[]byte{},
dataByte,
)

if dryRun {
Expand Down Expand Up @@ -418,6 +424,7 @@ Create a transaction, sign it, and send off to the Harmony blockchain
cmdTransfer.Flags().Uint32Var(&timeout, "timeout", defaultTimeout, "set timeout in seconds. Set to 0 to not wait for confirm")
cmdTransfer.Flags().BoolVar(&userProvidesPassphrase, "passphrase", false, ppPrompt)
cmdTransfer.Flags().StringVar(&passphraseFilePath, "passphrase-file", "", "path to a file containing the passphrase")
cmdTransfer.Flags().StringVar(&data, "data", "", "transaction data")

RootCmd.AddCommand(cmdTransfer)

Expand Down
37 changes: 23 additions & 14 deletions pkg/console/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,6 @@ import (
"encoding/hex"
"errors"
"fmt"
ethereum_rpc "github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/go-sdk/pkg/account"
"github.com/harmony-one/go-sdk/pkg/address"
"github.com/harmony-one/go-sdk/pkg/common"
"github.com/harmony-one/go-sdk/pkg/console/jsre"
"github.com/harmony-one/go-sdk/pkg/console/jsre/deps"
"github.com/harmony-one/go-sdk/pkg/console/prompt"
"github.com/harmony-one/go-sdk/pkg/console/web3ext"
"github.com/harmony-one/go-sdk/pkg/rpc"
"github.com/harmony-one/go-sdk/pkg/store"
"github.com/harmony-one/go-sdk/pkg/transaction"
"github.com/harmony-one/harmony/accounts"
"io"
"io/ioutil"
"math/big"
Expand All @@ -29,6 +17,19 @@ import (
"syscall"
"time"

ethereum_rpc "github.com/ethereum/go-ethereum/rpc"
"github.com/harmony-one/go-sdk/pkg/account"
"github.com/harmony-one/go-sdk/pkg/address"
"github.com/harmony-one/go-sdk/pkg/common"
"github.com/harmony-one/go-sdk/pkg/console/jsre"
"github.com/harmony-one/go-sdk/pkg/console/jsre/deps"
"github.com/harmony-one/go-sdk/pkg/console/prompt"
"github.com/harmony-one/go-sdk/pkg/console/web3ext"
"github.com/harmony-one/go-sdk/pkg/rpc"
"github.com/harmony-one/go-sdk/pkg/store"
"github.com/harmony-one/go-sdk/pkg/transaction"
"github.com/harmony-one/harmony/accounts"

"github.com/dop251/goja"
"github.com/mattn/go-colorable"
"github.com/peterh/liner"
Expand Down Expand Up @@ -553,6 +554,10 @@ func (b *bridge) HmySignTransaction(call jsre.Call) (goja.Value, error) {
gasLimit := getStringFromJsObjWithDefault(txObj, "gas", "1000000")
amount := getStringFromJsObjWithDefault(txObj, "value", "0")
gasPrice := getStringFromJsObjWithDefault(txObj, "gasPrice", "1")
input, err := transaction.StringToByte(getStringFromJsObjWithDefault(txObj, "data", ""))
if err != nil {
return nil, err
}

networkHandler := rpc.NewHTTPHandler(b.console.nodeUrl)
chanId, err := common.StringToChainID(b.console.net)
Expand Down Expand Up @@ -598,7 +603,7 @@ func (b *bridge) HmySignTransaction(call jsre.Call) (goja.Value, error) {
toP,
uint32(b.console.shardId), uint32(b.console.shardId),
amt, gPrice,
[]byte{},
input,
)
if err != nil {
return nil, err
Expand Down Expand Up @@ -635,6 +640,10 @@ func (b *bridge) HmySendTransaction(call jsre.Call) (goja.Value, error) {
gasLimit := getStringFromJsObjWithDefault(txObj, "gas", "1000000")
amount := getStringFromJsObjWithDefault(txObj, "value", "0")
gasPrice := getStringFromJsObjWithDefault(txObj, "gasPrice", "1")
input, err := transaction.StringToByte(getStringFromJsObjWithDefault(txObj, "data", ""))
if err != nil {
return nil, err
}

networkHandler := rpc.NewHTTPHandler(b.console.nodeUrl)
chanId, err := common.StringToChainID(b.console.net)
Expand Down Expand Up @@ -680,7 +689,7 @@ func (b *bridge) HmySendTransaction(call jsre.Call) (goja.Value, error) {
toP,
uint32(b.console.shardId), uint32(b.console.shardId),
amt, gPrice,
[]byte{},
input,
)
if err != nil {
return nil, err
Expand Down
17 changes: 17 additions & 0 deletions pkg/transaction/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package transaction

import (
"encoding/hex"
"fmt"
"strings"
)

func StringToByte(dataStr string) ([]byte, error) {
if len(dataStr) == 0 {
return []byte{}, nil
}
if !strings.HasPrefix(dataStr, "0x") {
return nil, fmt.Errorf("invalid data literal: %q", dataStr)
}
return hex.DecodeString(dataStr[2:])
}

0 comments on commit daa2c65

Please sign in to comment.