Skip to content

Commit

Permalink
Merge pull request #410 from iotaledger/feat/update-modules
Browse files Browse the repository at this point in the history
Adapt iota.go to latest serializer version
  • Loading branch information
luca-moser authored Aug 29, 2022
2 parents 05bb3a5 + 1d92ff8 commit 2f7b872
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 192 deletions.
26 changes: 4 additions & 22 deletions feat_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ package iotago
import (
"bytes"
"encoding/json"
"errors"
"fmt"

"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/iota.go/v3/util"
)

const (
// MinMetadataLength defines the min length of the data within a MetadataFeature.
MinMetadataLength = 1
// MaxMetadataLength defines the max length of the data within a MetadataFeature.
MaxMetadataLength = 8192
)

var (
// ErrMetadataFeatureEmpty gets returned when a MetadataFeature is empty.
ErrMetadataFeatureEmpty = errors.New("metadata feature is empty")
// ErrMetadataFeatureDataExceedsMaxLength gets returned when a MetadataFeature's data exceeds MaxMetadataLength.
ErrMetadataFeatureDataExceedsMaxLength = errors.New("metadata feature data exceeds max length")
)

// MetadataFeature is a feature which simply holds binary data to be freely
// interpreted by higher layer applications.
type MetadataFeature struct {
Expand Down Expand Up @@ -49,37 +43,25 @@ func (s *MetadataFeature) Type() FeatureType {
return FeatureMetadata
}

func (s *MetadataFeature) ValidDataSize() error {
switch {
case len(s.Data) == 0:
return ErrMetadataFeatureEmpty
case len(s.Data) > MaxMetadataLength:
return ErrMetadataFeatureDataExceedsMaxLength
}
return nil
}

func (s *MetadataFeature) Deserialize(data []byte, deSeriMode serializer.DeSerializationMode, deSeriCtx interface{}) (int, error) {
return serializer.NewDeserializer(data).
CheckTypePrefix(uint32(FeatureMetadata), serializer.TypeDenotationByte, func(err error) error {
return fmt.Errorf("unable to deserialize metadata feature: %w", err)
}).
ReadVariableByteSlice(&s.Data, serializer.SeriLengthPrefixTypeAsUint16, func(err error) error {
return fmt.Errorf("unable to deserialize data for metadata feature: %w", err)
}, MaxMetadataLength).
WithValidation(deSeriMode, func(_ []byte, err error) error { return s.ValidDataSize() }).
}, MinMetadataLength, MaxMetadataLength).
Done()
}

func (s *MetadataFeature) Serialize(deSeriMode serializer.DeSerializationMode, deSeriCtx interface{}) ([]byte, error) {
return serializer.NewSerializer().
WithValidation(deSeriMode, func(_ []byte, err error) error { return s.ValidDataSize() }).
WriteNum(byte(FeatureMetadata), func(err error) error {
return fmt.Errorf("unable to serialize metadata feature type ID: %w", err)
}).
WriteVariableByteSlice(s.Data, serializer.SeriLengthPrefixTypeAsUint16, func(err error) error {
return fmt.Errorf("unable to serialize metadata feature data: %w", err)
}).
}, MinMetadataLength, MaxMetadataLength).
Serialize()
}

Expand Down
28 changes: 5 additions & 23 deletions feat_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,19 @@ package iotago
import (
"bytes"
"encoding/json"
"errors"
"fmt"

"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/iota.go/v3/util"
)

const (
// MaxTagLength defines the max. length of an tag feature tag.
// MinTagLength defines the min. length of a tag feature tag.
MinTagLength = 1
// MaxTagLength defines the max. length of a tag feature tag.
MaxTagLength = 64
)

var (
// ErrTagFeatureEmpty gets returned when an TagFeature is empty.
ErrTagFeatureEmpty = errors.New("tag feature data is empty")
// ErrTagFeatureTagExceedsMaxLength gets returned when an TagFeature tag exceeds MaxTagLength.
ErrTagFeatureTagExceedsMaxLength = errors.New("tag feature tag exceeds max length")
)

// TagFeature is a feature which allows to additionally tag an output by a user defined value.
type TagFeature struct {
Tag []byte
Expand Down Expand Up @@ -51,37 +45,25 @@ func (s *TagFeature) Type() FeatureType {
return FeatureTag
}

func (s *TagFeature) ValidTagSize() error {
switch {
case len(s.Tag) == 0:
return ErrTagFeatureEmpty
case len(s.Tag) > MaxTagLength:
return ErrTagFeatureTagExceedsMaxLength
}
return nil
}

func (s *TagFeature) Deserialize(data []byte, deSeriMode serializer.DeSerializationMode, deSeriCtx interface{}) (int, error) {
return serializer.NewDeserializer(data).
CheckTypePrefix(uint32(FeatureTag), serializer.TypeDenotationByte, func(err error) error {
return fmt.Errorf("unable to deserialize tag feature: %w", err)
}).
ReadVariableByteSlice(&s.Tag, serializer.SeriLengthPrefixTypeAsByte, func(err error) error {
return fmt.Errorf("unable to deserialize tag for tag feature: %w", err)
}, MaxTagLength).
WithValidation(deSeriMode, func(_ []byte, err error) error { return s.ValidTagSize() }).
}, MinTagLength, MaxTagLength).
Done()
}

func (s *TagFeature) Serialize(deSeriMode serializer.DeSerializationMode, deSeriCtx interface{}) ([]byte, error) {
return serializer.NewSerializer().
WithValidation(deSeriMode, func(_ []byte, err error) error { return s.ValidTagSize() }).
WriteNum(byte(FeatureTag), func(err error) error {
return fmt.Errorf("unable to serialize tag feature type ID: %w", err)
}).
WriteVariableByteSlice(s.Tag, serializer.SeriLengthPrefixTypeAsByte, func(err error) error {
return fmt.Errorf("unable to serialize tag feature tag: %w", err)
}).
}, MinTagLength, MaxTagLength).
Serialize()
}

Expand Down
22 changes: 11 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ go 1.18
require (
filippo.io/edwards25519 v1.0.0
github.com/eclipse/paho.mqtt.golang v1.4.1
github.com/ethereum/go-ethereum v1.10.21
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-beta.1
github.com/ethereum/go-ethereum v1.10.23
github.com/iotaledger/hive.go/serializer/v2 v2.0.0-beta.3
github.com/iotaledger/iota.go v1.0.0
github.com/pasztorpisti/qs v0.0.0-20171216220353-8d6c33ee906c
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa
google.golang.org/grpc v1.48.0
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.28.1
gopkg.in/h2non/gock.v1 v1.1.2
)

require (
github.com/btcsuite/btcd/btcec/v2 v2.2.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.8.1 // indirect
golang.org/x/net v0.0.0-20220809012201-f428fae20770 // indirect
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/sync v0.0.0-20220819030929-7fc1605a5dde // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
google.golang.org/genproto v0.0.0-20220808204814-fd01256a5276 // indirect
google.golang.org/genproto v0.0.0-20220822174746-9e6da59bd2fc // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 2f7b872

Please sign in to comment.