Skip to content

Commit ed97469

Browse files
authoredMar 29, 2023
Merge pull request #55 from everFinance/fix/bundle-serialized-tags
feat(): fix tags serialize bug
2 parents 6c7acec + bb0971e commit ed97469

File tree

5 files changed

+48
-20
lines changed

5 files changed

+48
-20
lines changed
 

‎client_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"github.com/everFinance/goar/utils"
55
"github.com/stretchr/testify/assert"
66
"io/ioutil"
7+
"os"
78
"testing"
89
)
910

@@ -329,3 +330,21 @@ func TestClient_ExistTxData(t *testing.T) {
329330
assert.NoError(t, err)
330331
t.Log(exist)
331332
}
333+
334+
func TestNewTempConn2(t *testing.T) {
335+
data, err := os.Open("/Users/sandyzhou/Downloads/zHZIquAcF8eyYb6SbYUtzu1JJ_oeVCMJvqV7Sy-LP4k")
336+
assert.NoError(t, err)
337+
item, err := utils.DecodeBundleItemStream(data)
338+
assert.NoError(t, err)
339+
// 0x03641046696c654e616d6520576563686174494d4738302e6a70656718436f6e74656e742d5479706514696d6167652f6a70656700
340+
341+
// by, err := ioutil.ReadFile("/Users/sandyzhou/Downloads/zHZIquAcF8eyYb6SbYUtzu1JJ_oeVCMJvqV7Sy-LP4k")
342+
// assert.NoError(t, err)
343+
// item, err := utils.DecodeBundleItem(by)
344+
// assert.NoError(t, err)
345+
346+
t.Log(item.Tags) // [{FileName WechatIMG80.jpeg} {Content-Type image/jpeg}]
347+
348+
err = utils.VerifyBundleItem(*item)
349+
assert.NoError(t, err)
350+
}

‎example/bundle_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,10 @@ func TestNewBundleStream(t *testing.T) {
327327
bundle02, err := utils.DecodeBundle(bundleData)
328328
assert.NoError(t, err)
329329
assert.Equal(t, item01.Signature, bundle02.Items[0].Signature)
330+
for _, item := range bundle02.Items {
331+
err = utils.VerifyBundleItem(item)
332+
assert.NoError(t, err)
333+
}
330334
}
331335

332336
func TestAAA(t *testing.T) {

‎types/bundle.go

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ type BundleItem struct {
5555
Tags []Tag `json:"tags"`
5656
Data string `json:"data"`
5757
Id string `json:"id"`
58+
TagsBy string `json:"tagsBy"` // utils.Base64Encode(TagsBytes) for retry assemble item
5859

5960
ItemBinary []byte `json:"-"`
6061
DataReader *os.File `json:"-"`

‎utils/bundle.go

+18-20
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ func DecodeBundleItem(itemBinary []byte) (*types.BundleItem, error) {
283283

284284
var tagsBytesLength int
285285
tags := []types.Tag{}
286+
tagsBytes := make([]byte, 0)
286287
if numOfTags > 0 {
287288
if len(itemBinary) < tagsStart+16 {
288289
return nil, errors.New("itemBinary incorrect")
@@ -291,7 +292,7 @@ func DecodeBundleItem(itemBinary []byte) (*types.BundleItem, error) {
291292
if len(itemBinary) < tagsStart+16+tagsBytesLength || tagsStart+16+tagsBytesLength < 0 {
292293
return nil, errors.New("itemBinary incorrect")
293294
}
294-
tagsBytes := itemBinary[tagsStart+16 : tagsStart+16+tagsBytesLength]
295+
tagsBytes = itemBinary[tagsStart+16 : tagsStart+16+tagsBytesLength]
295296
// parser tags
296297
tgs, err := DeserializeTags(tagsBytes)
297298
if err != nil {
@@ -311,6 +312,7 @@ func DecodeBundleItem(itemBinary []byte) (*types.BundleItem, error) {
311312
Tags: tags,
312313
Data: Base64Encode(data),
313314
Id: id,
315+
TagsBy: Base64Encode(tagsBytes),
314316
ItemBinary: itemBinary,
315317
}, nil
316318
}
@@ -383,14 +385,15 @@ func DecodeBundleItemStream(itemBinary io.Reader) (*types.BundleItem, error) {
383385

384386
var tagsBytesLength int
385387
tags := []types.Tag{}
388+
tagsBytes := make([]byte, 0)
386389
if numOfTags > 0 {
387390
tagsBytesLengthBy := make([]byte, 8, 8)
388391
n, err = itemBinary.Read(tagsBytesLengthBy)
389392
if err != nil || n < 8 {
390393
return nil, errors.New("itemBinary incorrect")
391394
}
392395
tagsBytesLength = ByteArrayToLong(tagsBytesLengthBy)
393-
tagsBytes := make([]byte, tagsBytesLength, tagsBytesLength)
396+
tagsBytes = make([]byte, tagsBytesLength, tagsBytesLength)
394397
n, err = itemBinary.Read(tagsBytes)
395398
if err != nil || n < tagsBytesLength {
396399
return nil, errors.New("itemBinary incorrect")
@@ -425,6 +428,7 @@ func DecodeBundleItemStream(itemBinary io.Reader) (*types.BundleItem, error) {
425428
Tags: tags,
426429
Data: "",
427430
Id: id,
431+
TagsBy: Base64Encode(tagsBytes),
428432
ItemBinary: make([]byte, 0),
429433
DataReader: dataReader,
430434
}, nil
@@ -457,6 +461,10 @@ func newBundleItem(owner string, signatureType int, target, anchor string, data
457461
return nil, errors.New("anchor length must be 32")
458462
}
459463
}
464+
tagsBytes, err := SerializeTags(tags)
465+
if err != nil {
466+
return nil, err
467+
}
460468
item := &types.BundleItem{
461469
SignatureType: signatureType,
462470
Signature: "",
@@ -465,6 +473,7 @@ func newBundleItem(owner string, signatureType int, target, anchor string, data
465473
Anchor: anchor,
466474
Tags: tags,
467475
Id: "",
476+
TagsBy: Base64Encode(tagsBytes),
468477
ItemBinary: make([]byte, 0),
469478
}
470479
if _, ok := data.(*os.File); ok {
@@ -476,20 +485,6 @@ func newBundleItem(owner string, signatureType int, target, anchor string, data
476485
}
477486

478487
func BundleItemSignData(d types.BundleItem) ([]byte, error) {
479-
var err error
480-
tagsBy := make([]byte, 0)
481-
if len(d.ItemBinary) > 0 { // verify logic
482-
tagsBy, err = GetBundleItemTagsBytes(d.ItemBinary)
483-
if err != nil {
484-
return nil, err
485-
}
486-
} else {
487-
tagsBy, err = SerializeTags(d.Tags)
488-
if err != nil {
489-
return nil, err
490-
}
491-
}
492-
493488
// deep hash
494489
dataList := make([]interface{}, 0)
495490
dataList = append(dataList, Base64Encode([]byte("dataitem")))
@@ -498,7 +493,7 @@ func BundleItemSignData(d types.BundleItem) ([]byte, error) {
498493
dataList = append(dataList, d.Owner)
499494
dataList = append(dataList, d.Target)
500495
dataList = append(dataList, d.Anchor)
501-
dataList = append(dataList, Base64Encode(tagsBy))
496+
dataList = append(dataList, d.TagsBy)
502497
if d.DataReader != nil {
503498
dataList = append(dataList, d.DataReader)
504499
} else {
@@ -641,9 +636,12 @@ func generateItemMetaBinary(d *types.BundleItem) ([]byte, error) {
641636
return nil, errors.New("anchorBytes length must 32")
642637
}
643638
}
644-
tagsBytes, err := SerializeTags(d.Tags)
645-
if err != nil {
646-
return nil, err
639+
tagsBytes := make([]byte, 0)
640+
if len(d.Tags) > 0 {
641+
tagsBytes, err = Base64Decode(d.TagsBy)
642+
if err != nil {
643+
return nil, err
644+
}
647645
}
648646

649647
sigMeta, ok := types.SigConfigMap[d.SignatureType]

‎utils/tags_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,9 @@ func TestTags(t *testing.T) {
4242
assert.Equal(t, tagsBase64[1].Name, tagsRes[1].Name)
4343
assert.Equal(t, tagsBase64[1].Value, tagsRes[1].Value)
4444
}
45+
46+
func TestSerializeTags(t *testing.T) {
47+
by, err := SerializeTags([]types.Tag{})
48+
assert.NoError(t, err)
49+
t.Log(len(by))
50+
}

0 commit comments

Comments
 (0)