Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

How to decode the data in transaction? #53

Closed
freddieee opened this issue Jun 10, 2019 · 13 comments
Closed

How to decode the data in transaction? #53

freddieee opened this issue Jun 10, 2019 · 13 comments

Comments

@freddieee
Copy link

Hi, I noticed in the docs that the data in tx is encoded in Amino logic. Is there any handy tool or library to decode these content? Appreciate for any insights : )

@ruslansalikhov
Copy link

example in golang

package main

import (
    "encoding/json"
    "fmt"
    "github.com/binance-chain/go-sdk/client/rpc"
    ctypes "github.com/binance-chain/go-sdk/common/types"
    "github.com/binance-chain/go-sdk/types"
    "github.com/binance-chain/go-sdk/types/tx"
)

func main() {
    nodeAddr := "tcp://127.0.0.1:27147"
    client := rpc.NewRPCClient(nodeAddr, ctypes.ProdNetwork)

    codec := types.NewCodec()

    number := int64(8095579)
    //number := int64(8643229)
    res, _ := client.Block(&number)
    txs := res.Block.Data.Txs

    parsedTxs := make([]tx.StdTx, len(txs))
    for i := range txs {
        err := codec.UnmarshalBinaryLengthPrefixed(txs[i], &parsedTxs[i])
        if err != nil {
            fmt.Println("Error - codec unmarshal")
            return
        }
    }

    bz, err := json.Marshal(parsedTxs)

    if err != nil {
       fmt.Println("Error - json marshal")
       return
    }

    fmt.Println(string(bz))
}

@freddieee
Copy link
Author

thanks @huangsuyu , @ruslansalikhov ! Both of yours are very helpful to me!

@amjadDev
Copy link

amjadDev commented Jun 22, 2019

hello @ruslansalikhov i am done with this issue using go language except transaction hash can you guide me what method i need to use for transaction id like
17E6A0811811B75BB94FA8955295D7E2B5D9DA05E6A092685EA946D42E03614C
refrence
https://explorer.binance.org/tx/17E6A0811811B75BB94FA8955295D7E2B5D9DA05E6A092685EA946D42E03614C
Code:

var ParseAminoTx = func(w http.ResponseWriter, r * http.Request) {

    codec: = types.NewCodec()
    decodetx64,
    err: = base64.StdEncoding.DecodeString("yAHwYl3uClAqLIf6CiQKFP6aa5bCfSjIPBZs4zrEml5FE+ISEgwKA0JOQhCem7a3ggESJAoU/imIh6AV6Ub6fhN01DL9AChX0cISDAoDQk5CEJ6btreCARJwCibrWumHIQOlVsK2sHBzehAuYoMcln0SdKFG6Ud5NZpkiv/jcUJRSxJAP66nAeKx/671oBzc37WRnxxXCOxmIJOdCCZEEeSxiv1ualjMoI7sKDiZoTHzS6e1mpjqIa2MHYEM/Tq99+BfCRjC1wEgTQ==")

    if err != nil {
            fmt.Println("error:", err)
    }

    txs: = [] string {
            string(decodetx64)
    }

            parsedTxs: = make([] tx.StdTx, len(txs))
    for i: = range txs {
            err: = codec.UnmarshalBinaryLengthPrefixed([] byte(txs[i]), & parsedTxs[i])

            if err != nil {
                    fmt.Println("Error - codec unmarshal")
            }
    }

            bz,
    err: = json.Marshal(parsedTxs)

    if err != nil {
            fmt.Println("Error - json marshal")

    }
    u.Respond(w, u.Message(true, string(bz)))

}

output

{"message":"[{"msg":[{"inputs":[{"address":"bnb1l6dxh9kz055vs0qkdn3n43y6tez38csj4q7x5d","coins":[{"denom":"BNB","amount":35012840862}]}],"outputs":[{"address":"bnb1lc5c3paqzh55d7n7zd6dgvhaqq5905wzr849tj","coins":[{"denom":"BNB","amount":35012840862}]}]}],"signatures":[{"pub_key":[3,165,86,194,182,176,112,115,122,16,46,98,131,28,150,125,18,116,161,70,233,71,121,53,154,100,138,255,227,113,66,81,75],"signature":"P66nAeKx/671oBzc37WRnxxXCOxmIJOdCCZEEeSxiv1ualjMoI7sKDiZoTHzS6e1mpjqIa2MHYEM/Tq99+BfCQ==","account_number":27586,"sequence":77}],"memo":"","source":0,"data":null}]","status":true}

@chainwhisper
Copy link
Contributor

chainwhisper commented Jun 25, 2019

Hello, @amjadDev
you can use parsedTxs[i].Hash()

@amjadDev
Copy link

Hello, @amjadDev
you can use parsedTxs[i].Hash()

i tired got this error
parsedTxs[i].Hash undefined (type tx.StdTx has no field or method Hash)
m missing any function or package?

@amjadDev
Copy link

Hello, @amjadDev
you can use parsedTxs[i].Hash()

i tired got this error
parsedTxs[i].Hash undefined (type tx.StdTx has no field or method Hash)
m missing any function or package?

thanks !!

sorted

decodetx64, err := base64.StdEncoding.DecodeString("ygHwYl3uClAqLIf6CiQKFJGTdSD0BFj1tBTSZ5YbRsGXid1wEgwKA0JOQhCAmpGy9gISJAoUNZyodQE2lvxtwuhMFhar4FgLeWESDAoDQk5CEICakbL2AhJwCibrWumHIQNW4KWAOJpv0syRzVJcbVpNgFSvcN8XSE5YZ4+fV0oLTRJAmEC1SzRLFtH+xCE/0ajwJGYaLw4AqdoMqZHVQlxc1rQHCsnkC8/qYTKKd+MZvPmhRfvp12fSRf93olACfFnLwxgzIPmDBSAC")
sha256 := sha256.Sum256([]byte(decodetx64))
fmt.Printf("%x\n", sha256)

this is working for me.

@blockmonk
Copy link

Hello, @amjadDev
you can use parsedTxs[i].Hash()

@huangsuyu I am trying to get transaction data from data_hash

"data_hash": "A7044A2430D5F4F461A59E1634F4739B1128912946424A207A73F27B7FCA1C29",

what decryption should i need to use. here is my block result

"block_id": {
"hash": "09F845024BD5313129374DE2ED8F187A1F21373580178A7E18BF8D6352E133F3",
"parts": {
"total": "1",
"hash": "E6995EC7B04C8D5F686AE0C0AD92C189445B9A975600B3166B243573D059D548"
}
},
"header": {
"version": {
"block": "10",
"app": "0"
},
"chain_id": "Binance-Chain-Tigris",
"height": "21993735",
"time": "2019-07-22T15:48:10.041358386Z",
"num_txs": "2",
"total_txs": "6132889",
"last_block_id": {
"hash": "2EF6A9B6BA8AD1CBF5E7C382906C9CBECD5958143CB9C185EA0235541015482E",
"parts": {
"total": "1",
"hash": "4C9D00EC8E5EABD25482BD49CBE89A0AA8D435514C8202F119087A9E471EF0B0"
}
},
"last_commit_hash": "5BBAB5A23DC5E69360EBF8697A011A68CD277061ECB66216DD5CC54B7E26C75D",
"data_hash": "A7044A2430D5F4F461A59E1634F4739B1128912946424A207A73F27B7FCA1C29",
"validators_hash": "43C53A50D8653EF8CF1E5716DA68120FB51B636DC6D111EC3277B098ECD42D49",
"next_validators_hash": "43C53A50D8653EF8CF1E5716DA68120FB51B636DC6D111EC3277B098ECD42D49",
"consensus_hash": "294D8FBD0B94B767A7EBA9840F299A3586DA7FE6B5DEAD3B7EECBA193C400F93",
"app_hash": "B30FE7AACEADCB5AAD748CB3088AD3530BC21FB5205C06A242A14ACDB62ECEFC",
"last_results_hash": "",
"evidence_hash": "",
"proposer_address": "3CD4AABABDDEB7ABFEA9618732E331077A861D2B"
}

@oom-tesla
Copy link

example in golang

package main

import (
    "encoding/json"
    "fmt"
    "github.com/binance-chain/go-sdk/client/rpc"
    ctypes "github.com/binance-chain/go-sdk/common/types"
    "github.com/binance-chain/go-sdk/types"
    "github.com/binance-chain/go-sdk/types/tx"
)

func main() {
    nodeAddr := "tcp://127.0.0.1:27147"
    client := rpc.NewRPCClient(nodeAddr, ctypes.ProdNetwork)

    codec := types.NewCodec()

    number := int64(8095579)
    //number := int64(8643229)
    res, _ := client.Block(&number)
    txs := res.Block.Data.Txs

    parsedTxs := make([]tx.StdTx, len(txs))
    for i := range txs {
        err := codec.UnmarshalBinaryLengthPrefixed(txs[i], &parsedTxs[i])
        if err != nil {
            fmt.Println("Error - codec unmarshal")
            return
        }
    }

    bz, err := json.Marshal(parsedTxs)

    if err != nil {
       fmt.Println("Error - json marshal")
       return
    }

    fmt.Println(string(bz))
}

Hi @ruslansalikhov Do you have example in Node Js?

@oom-tesla
Copy link

Hello, @amjadDev
you can use parsedTxs[i].Hash()

i tired got this error
parsedTxs[i].Hash undefined (type tx.StdTx has no field or method Hash)
m missing any function or package?

thanks !!

sorted

decodetx64, err := base64.StdEncoding.DecodeString("ygHwYl3uClAqLIf6CiQKFJGTdSD0BFj1tBTSZ5YbRsGXid1wEgwKA0JOQhCAmpGy9gISJAoUNZyodQE2lvxtwuhMFhar4FgLeWESDAoDQk5CEICakbL2AhJwCibrWumHIQNW4KWAOJpv0syRzVJcbVpNgFSvcN8XSE5YZ4+fV0oLTRJAmEC1SzRLFtH+xCE/0ajwJGYaLw4AqdoMqZHVQlxc1rQHCsnkC8/qYTKKd+MZvPmhRfvp12fSRf93olACfFnLwxgzIPmDBSAC")
sha256 := sha256.Sum256([]byte(decodetx64))
fmt.Printf("%x\n", sha256)

this is working for me.

Hi @amjadDev I need to get transaction hash as well using Node JS If you can guide me how to do it would be very helpful.

@alexspark21
Copy link

Any news? I have at the same trouble with JS sdk

@blockmonk
Copy link

please check binance javascript sdk.
https://github.com/binance-chain/javascript-sdk

@blockmonk
Copy link

@alexspark21 if u needed I will provide Golang code in form of REST API where you can pass the hash from your js application and will receive txid.

@alexspark21
Copy link

@blockmonk JS SDK for now not supported Transfer type, and I can't decode encoded response from RPC to human response which doing perfectly HTTP API endpoint https://dex.binance.org/api/v1/transactions-in-block/61989125

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants