最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《如何解码恒星 XDR》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~
我正在研究恒星区块链,需要解码go语言的恒星xdr。我知道如何使用 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}}
//预期输出
{ type: '付款', 目的地:'gckuxi3jrjanyof3am35z22fgugyyuiebpe5ttz7p3g6xaefgyzc2pom', 资产: 资产 { 代码:'博客', 发行人: 'gdoptradbvwjr6bmb6h5acqtavus6xmt53cdnajzlostiuiciw57ismf' }, 金额:'10'}
{ type: '付款', 目的地:'gckuxi3jrjanyof3am35z22fgugyyuiebpe5ttz7p3g6xaefgyzc2pom', 资产: 资产 { 代码:'博客', 发行人: 'gdoptradbvwjr6bmb6h5acqtavus6xmt53cdnajzlostiuiciw57ismf' }, 金额:'10'}
{ type: '付款', 目的地:'gckuxi3jrjanyof3am35z22fgugyyuiebpe5ttz7p3g6xaefgyzc2pom', 资产: 资产 { 代码:'博客', 发行人: 'gdoptradbvwjr6bmb6h5acqtavus6xmt53cdnajzlostiuiciw57ismf' }, 金额:'10'}
谁能帮我解决这个问题吗?
解决方案
我提出了一个可能较晚的解决方案。但要获得您需要的内容,您可以使用该包:
并使用以下函数。
// 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
}本篇关于《如何解码恒星 XDR》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
