我的工作是恒星区块链,需要解码恒星XDR,这是在GO语言。我知道如何使用JavaScript进行解码,但无法在GO中找到一种方法。
//JS code
const {Transaction} = require('stellar-base')
const parsedTx = new Transaction('tx_envelope_encoded_as_XDR')
console.log(parsedTx)这个很好用。我试过但不工作的东西..。
//GO code
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/stellar/go/xdr"
"github.com/gorilla/mux"
)
func DecodeXDR(w http.ResponseWriter, r *http.Request) {
var OBJ model.TransactionCollectionBody
err := json.NewDecoder(r.Body).Decode(&OBJ)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode("Error while Decoding the body")
fmt.Println(err)
return
}
// fmt.Println(OBJ)
// lol:=xdr.Value(OBJ.XDR)
var txe xdr.Transaction
err = xdr.SafeUnmarshalBase64(XDRB64, &txe)
if err != nil {
fmt.Println(err)
}
fmt.Println(txe)
}
//Output
{{PublicKeyTypePublicKeyTypeEd25519 0xc042055d20} 200 2800572080062465 <nil> {MemoTypeMemoNone <nil> <nil> <nil> <nil>} [{<nil> {OperationTypeManageData <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> 0xc042174040 <nil>}} {<nil> {OperationTypeManageData <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> <nil> 0xc042174080 <nil>}}] {0}}//预期输出
'GCKUXI3JRJANYOF3AM35Z22FGUGYYUIEBPE5TTZ7P3G6XAEFGYZC2POM',{ {类型:‘支付’,目的地:资产:资产{代码:'Blog',发行者:'GDOPTRADBVWJR6BMB6H5ACQTAVUS6XMT53CDNAJZLOSTIUICIW57ISMF‘},金额:'10’}
'GCKUXI3JRJANYOF3AM35Z22FGUGYYUIEBPE5TTZ7P3G6XAEFGYZC2POM',{ {类型:‘支付’,目的地:资产:资产{代码:'Blog',发行者:'GDOPTRADBVWJR6BMB6H5ACQTAVUS6XMT53CDNAJZLOSTIUICIW57ISMF‘},金额:'10’}
'GCKUXI3JRJANYOF3AM35Z22FGUGYYUIEBPE5TTZ7P3G6XAEFGYZC2POM',{ {类型:‘支付’,目的地:资产:资产{代码:'Blog',发行者:'GDOPTRADBVWJR6BMB6H5ACQTAVUS6XMT53CDNAJZLOSTIUICIW57ISMF‘},金额:'10’}
有人能帮我解决这个问题吗?
发布于 2022-10-31 18:56:51
我想出了一个迟来的解决办法。但是为了得到你需要的东西,你可以使用这个包:
Github.com/stellar/go-xdr/xdr3
并使用以下函数。
// DecodeFrom decodes this value using the Decoder.
func (s *Value) DecodeFrom(d *xdr.Decoder) (int, error) {
var err error
var n, nTmp int
(*s), nTmp, err = d.DecodeOpaque(0)
n += nTmp
if err != nil {
return n, fmt.Errorf("decoding Value: %s", err)
}
return n, nil
}https://stackoverflow.com/questions/53275299
复制相似问题