Skip to content

Commit 35d590e

Browse files
Merge pull request #63 from everFinance/fix/encode-decode-tags
feat(): fix bundleItem tags encode and decode
2 parents 5a8e321 + 2f9596b commit 35d590e

File tree

5 files changed

+90
-5
lines changed

5 files changed

+90
-5
lines changed

client_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ func TestClient_GetBundleItems3(t *testing.T) {
398398
}
399399

400400
func TestNewClient2(t *testing.T) {
401-
arTx := "b92IF6ZX0owPFcssPcs-qOBN2nuR0d3xLYOSovh1eck"
401+
arTx := "FH95DEmWitTvg07vKUejh9akIPQKgZOp7ZdPa2adnGM"
402402
c := NewClient("https://arweave.net")
403403
data, err := c.GetTransactionDataByGateway(arTx)
404404

@@ -411,6 +411,7 @@ func TestNewClient2(t *testing.T) {
411411
for _, item := range bundleItems {
412412
t.Log(item.Id)
413413
}
414+
t.Log(len(bundleItems))
414415
}
415416

416417
func TestNewWallet2(t *testing.T) {

go.mod

+2
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ require (
2626
github.com/everFinance/ethrpc v1.0.4 // indirect
2727
github.com/getsentry/sentry-go v0.11.0 // indirect
2828
github.com/go-stack/stack v1.8.1 // indirect
29+
github.com/golang/snappy v0.0.4 // indirect
2930
github.com/google/uuid v1.3.0 // indirect
3031
github.com/jinzhu/inflection v1.0.0 // indirect
3132
github.com/jinzhu/now v1.1.4 // indirect
3233
github.com/json-iterator/go v1.1.11 // indirect
3334
github.com/kr/pretty v0.2.1 // indirect
35+
github.com/linkedin/goavro/v2 v2.12.0 // indirect
3436
github.com/mattn/go-colorable v0.1.11 // indirect
3537
github.com/mattn/go-isatty v0.0.14 // indirect
3638
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect

utils/bundle.go

+21
Original file line numberDiff line numberDiff line change
@@ -831,3 +831,24 @@ func SubmitItemToArSeed(item types.BundleItem, currency, arseedUrl string) (*sch
831831
}
832832
return br, nil
833833
}
834+
835+
func SubmitItemToMU(item types.BundleItem, muUrl string) ([]byte, error) {
836+
itemBinary := item.ItemBinary
837+
if len(itemBinary) == 0 {
838+
var err error
839+
itemBinary, err = GenerateItemBinary(&item)
840+
if err != nil {
841+
return nil, err
842+
}
843+
}
844+
845+
resp, err := http.DefaultClient.Post(muUrl, "application/octet-stream", bytes.NewBuffer(itemBinary))
846+
if err != nil {
847+
return nil, err
848+
}
849+
defer resp.Body.Close()
850+
fmt.Printf("resp code:%v\n", resp.StatusCode)
851+
// json unmarshal
852+
body, err := ioutil.ReadAll(resp.Body)
853+
return body, err
854+
}

utils/tags.go

+58-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package utils
33
import (
44
"github.com/everFinance/goar/types"
55
"github.com/hamba/avro"
6+
"github.com/linkedin/goavro/v2"
67
)
78

89
func TagsEncode(tags []types.Tag) []types.Tag {
@@ -42,7 +43,7 @@ func TagsDecode(base64Tags []types.Tag) ([]types.Tag, error) {
4243
}
4344

4445
// using bundle tx, avro serialize
45-
func SerializeTags(tags []types.Tag) ([]byte, error) {
46+
func SerializeTags1(tags []types.Tag) ([]byte, error) {
4647
if len(tags) == 0 {
4748
return make([]byte, 0), nil
4849
}
@@ -55,7 +56,7 @@ func SerializeTags(tags []types.Tag) ([]byte, error) {
5556
return avro.Marshal(tagsParser, tags)
5657
}
5758

58-
func DeserializeTags(data []byte) ([]types.Tag, error) {
59+
func DeserializeTags1(data []byte) ([]types.Tag, error) {
5960
tagsParser, err := avro.Parse(`{"type": "array", "items": {"type": "record", "name": "Tag", "fields": [{"name": "name", "type": "string"}, {"name": "value", "type": "string"}]}}`)
6061
if err != nil {
6162
return nil, err
@@ -64,3 +65,58 @@ func DeserializeTags(data []byte) ([]types.Tag, error) {
6465
err = avro.Unmarshal(tagsParser, data, &tags)
6566
return tags, err
6667
}
68+
69+
const avroTagSchema = `{
70+
"type": "array",
71+
"items": {
72+
"type": "record",
73+
"name": "Tag",
74+
"fields": [
75+
{ "name": "name", "type": "bytes" },
76+
{ "name": "value", "type": "bytes" }
77+
]
78+
}
79+
}`
80+
81+
func SerializeTags(tags []types.Tag) ([]byte, error) {
82+
if len(tags) == 0 {
83+
return make([]byte, 0), nil
84+
}
85+
86+
codec, err := goavro.NewCodec(avroTagSchema)
87+
if err != nil {
88+
return nil, err
89+
}
90+
avroTags := []map[string]interface{}{}
91+
for _, tag := range tags {
92+
m := map[string]interface{}{"name": []byte(tag.Name), "value": []byte(tag.Value)}
93+
avroTags = append(avroTags, m)
94+
}
95+
96+
data, err := codec.BinaryFromNative(nil, avroTags)
97+
if err != nil {
98+
return nil, err
99+
}
100+
101+
return data, err
102+
}
103+
104+
func DeserializeTags(data []byte) ([]types.Tag, error) {
105+
codec, err := goavro.NewCodec(avroTagSchema)
106+
if err != nil {
107+
return nil, err
108+
}
109+
110+
avroTags, _, err := codec.NativeFromBinary(data)
111+
if err != nil {
112+
return nil, err
113+
}
114+
115+
tags := []types.Tag{}
116+
117+
for _, v := range avroTags.([]interface{}) {
118+
tag := v.(map[string]interface{})
119+
tags = append(tags, types.Tag{Name: string(tag["name"].([]byte)), Value: string(tag["value"].([]byte))})
120+
}
121+
return tags, err
122+
}

utils/tags_test.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package utils
22

33
import (
4+
"encoding/hex"
45
"testing"
56

67
"github.com/everFinance/goar/types"
@@ -44,7 +45,11 @@ func TestTags(t *testing.T) {
4445
}
4546

4647
func TestSerializeTags(t *testing.T) {
47-
by, err := SerializeTags([]types.Tag{})
48+
tags := []types.Tag{{Name: "abc", Value: "123"}, {Name: "bbc", Value: "223"}}
49+
50+
by, err := SerializeTags(tags)
4851
assert.NoError(t, err)
49-
t.Log(len(by))
52+
t.Log(hex.EncodeToString(by))
53+
// 040661626306313233066262630632323300
54+
// 03200661626306313233066262630632323300
5055
}

0 commit comments

Comments
 (0)