Skip to content
This repository has been archived by the owner on Jul 20, 2021. It is now read-only.

Commit

Permalink
Add IOTA Tangle assesor.
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Bonafide <[email protected]>
  • Loading branch information
jbonafide623 authored and jbdell committed May 29, 2020
1 parent ccf400b commit 8fa5485
Show file tree
Hide file tree
Showing 12 changed files with 879 additions and 0 deletions.
17 changes: 17 additions & 0 deletions cmd/examples/multistage/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/project-alvarium/go-sdk/pkg/annotation/uniqueprovider/ulid"
"github.com/project-alvarium/go-sdk/pkg/annotator"
"github.com/project-alvarium/go-sdk/pkg/annotator/assess"
iotaAssessor "github.com/project-alvarium/go-sdk/pkg/annotator/assess/assessor/iota"
pkiAssessor "github.com/project-alvarium/go-sdk/pkg/annotator/assess/assessor/pki"
"github.com/project-alvarium/go-sdk/pkg/annotator/assess/assessor/pki/factory/verifier"
filterFactory "github.com/project-alvarium/go-sdk/pkg/annotator/filter/matching"
Expand All @@ -42,6 +43,7 @@ import (
"github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/example"
"github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/example/writer/testwriter"
"github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/iota"
iotaPublisherMetadata "github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/iota/metadata"
"github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/ipfs"
ipfsPublisherMetadata "github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/ipfs/metadata"
"github.com/project-alvarium/go-sdk/pkg/hashprovider/sha256"
Expand Down Expand Up @@ -258,6 +260,21 @@ func main() {
},
),
),
assess.New(
p,
uniqueProvider,
idProvider,
persistence,
iotaAssessor.New(newClient(iotaURL)),
filterFactory.New(
func(annotation *annotation.Instance) bool {
t, ok := annotation.Metadata.(*publishMetadata.Instance)
return ok &&
(t.PublisherKind == iotaPublisherMetadata.Kind ||
t.PublisherKind == ipfsPublisherMetadata.Kind)
},
),
),
publish.New(p, uniqueProvider, idProvider, persistence, example.New(w), passthroughFilter),
},
)
Expand Down
51 changes: 51 additions & 0 deletions internal/pkg/test/iota.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@
package test

import (
"encoding/json"
"math/rand"
"testing"

"github.com/project-alvarium/go-sdk/pkg/annotation"
"github.com/project-alvarium/go-sdk/pkg/annotation/metadata"
publishMetadata "github.com/project-alvarium/go-sdk/pkg/annotator/publish/metadata"
"github.com/project-alvarium/go-sdk/pkg/hashprovider/sha256"
identityProvider "github.com/project-alvarium/go-sdk/pkg/identityprovider/hash"
"github.com/project-alvarium/go-sdk/pkg/test"

"github.com/iotaledger/iota.go/bundle"
"github.com/iotaledger/iota.go/converter"
"github.com/iotaledger/iota.go/transaction"
"github.com/iotaledger/iota.go/trinary"
)

const (
Expand All @@ -44,6 +53,11 @@ func FactoryRandomAddressTrytesString() string {
return FactoryRandomFixedLengthTrytesString(addressSize)
}

// FactoryRandomInvalidAddressTrytesString returns Trytes for an Address of a fixed invalid length with a random value.
func FactoryRandomInvalidAddressTrytesString() string {
return FactoryRandomFixedLengthTrytesString(addressSize - 1)
}

// FactoryRandomFixedSizeBundle returns a Bundle of a fixed size with random transaction values with a random length.
func FactoryRandomFixedSizeBundle(size int) bundle.Bundle {
length := rand.Intn(1024)
Expand All @@ -57,3 +71,40 @@ func FactoryRandomFixedSizeBundle(size int) bundle.Bundle {
}
return txs
}

// FactoryAnnotationsTransaction returns a Transaction comprised of a slice of annotation instances.
func FactoryAnnotationsTransaction(t *testing.T, unique string, metadata metadata.Contract) transaction.Transaction {
marshalledAnnotations, _ := json.Marshal([]*annotation.Instance{
factoryPublisherAnnotation(unique, metadata),
})
return bytesToTransaction(marshalledAnnotations)
}

// FactoryAnnotationTransaction returns a Transaction comprised of a single annotation instance.
func FactoryAnnotationTransaction(t *testing.T, unique string, metadata metadata.Contract) transaction.Transaction {
marshalledAnnotations, _ := json.Marshal(factoryPublisherAnnotation(unique, metadata))
return bytesToTransaction(marshalledAnnotations)
}

// factoryPublisherAnnotation is a factory function that returns an initialized instance of an annotation.
func factoryPublisherAnnotation(unique string, metadata metadata.Contract) *annotation.Instance {
return &annotation.Instance{
Unique: unique,
Created: test.FactoryRandomString(),
CurrentIdentityKind: test.FactoryRandomString(),
CurrentIdentity: identityProvider.New(sha256.New()).Derive(test.FactoryRandomByteSlice()),
PreviousIdentityKind: test.FactoryRandomString(),
PreviousIdentity: nil,
MetadataKind: publishMetadata.Kind,
Metadata: metadata,
}
}

// bytesToTransaction returns a Transaction containing a signature made up of the given byte slice.
func bytesToTransaction(data []byte) transaction.Transaction {
trytes, _ := converter.ASCIIToTrytes(string(data))
signatureTrytes, _ := trinary.Pad(trytes, len(trytes)+9)
return transaction.Transaction{
SignatureMessageFragment: signatureTrytes,
}
}
140 changes: 140 additions & 0 deletions internal/pkg/test/iota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@
package test

import (
"encoding/json"
"math/rand"
"strings"
"testing"

"github.com/project-alvarium/go-sdk/pkg/annotation"
ipfsPublisherMetadata "github.com/project-alvarium/go-sdk/pkg/annotator/publish/publisher/ipfs/metadata"
"github.com/project-alvarium/go-sdk/pkg/test"

"github.com/iotaledger/iota.go/converter"
"github.com/iotaledger/iota.go/guards/validators"
"github.com/iotaledger/iota.go/trinary"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -155,6 +161,48 @@ func TestFactoryRandomAddressTrytesString(t *testing.T) {
}
}

// TestFactoryRandomInvalidAddressTrytesString tests FactoryRandomInvalidAddressTrytesString.
func TestFactoryRandomInvalidAddressTrytesString(t *testing.T) {
type testCase struct {
name string
test func(t *testing.T)
}

cases := []testCase{
{
name: "returns fixed size",
test: func(t *testing.T) {
result := FactoryRandomInvalidAddressTrytesString()

assert.Equal(t, addressSize-1, len(result))
},
},
{
name: "returns valid charset",
test: func(t *testing.T) {
result := FactoryRandomInvalidAddressTrytesString()

for i := range result {
assert.True(t, strings.Contains(trytesCharset, string(result[i])))
}
},
},
{
name: "returns varying content",
test: func(t *testing.T) {
result1 := FactoryRandomInvalidAddressTrytesString()
result2 := FactoryRandomInvalidAddressTrytesString()

assert.NotEqual(t, result1, result2)
},
},
}

for i := range cases {
t.Run(cases[i].name, cases[i].test)
}
}

// TestFactoryRandomFixedSizeBundle tests FactoryRandomFixedSizeBundle.
func TestFactoryRandomFixedSizeBundle(t *testing.T) {
type testCase struct {
Expand Down Expand Up @@ -223,3 +271,95 @@ func TestFactoryRandomFixedSizeBundle(t *testing.T) {
t.Run(cases[i].name, cases[i].test)
}
}

// TestFactoryAnnotationsTransaction tests FactoryAnnotationsTransaction.
func TestFactoryAnnotationsTransaction(t *testing.T) {
type testCase struct {
name string
test func(t *testing.T)
}

cases := []testCase{
{
name: "transaction's content varies",
test: func(t *testing.T) {
unique := test.FactoryRandomString()
ipfsMetadata := ipfsPublisherMetadata.NewSuccess(test.FactoryRandomString())
result1 := FactoryAnnotationsTransaction(t, unique, ipfsMetadata)
result2 := FactoryAnnotationsTransaction(t, unique, ipfsMetadata)

assert.NotEqual(t, result1, result2)
},
},
{
name: "transaction content contains an annotation slice",
test: func(t *testing.T) {
unique := test.FactoryRandomString()
ipfsMetadata := ipfsPublisherMetadata.NewSuccess(test.FactoryRandomString())
result := FactoryAnnotationsTransaction(t, unique, ipfsMetadata)

content, err := converter.TrytesToASCII(result.SignatureMessageFragment[:len(result.SignatureMessageFragment)-9])
if err != nil {
assert.FailNow(t, "Unexpected error converting Trtyes to ASCII", err.Error())
}

a := []*annotation.Instance{factoryPublisherAnnotation(unique, ipfsMetadata)}
marshalledAnnotation, err := json.Marshal(a)
if err != nil {
assert.FailNow(t, "Unexpected marshal failure", err.Error())
}
assert.IsType(t, string(marshalledAnnotation), content)
},
},
}

for i := range cases {
t.Run(cases[i].name, cases[i].test)
}
}

// TestFactoryAnnotationTransaction tests FactoryAnnotationTransaction.
func TestFactoryAnnotationTransaction(t *testing.T) {
type testCase struct {
name string
test func(t *testing.T)
}

cases := []testCase{
{
name: "transaction's content varies",
test: func(t *testing.T) {
unique := test.FactoryRandomString()
ipfsMetadata := ipfsPublisherMetadata.NewSuccess(test.FactoryRandomString())
result1 := FactoryAnnotationTransaction(t, unique, ipfsMetadata)
result2 := FactoryAnnotationTransaction(t, unique, ipfsMetadata)

assert.NotEqual(t, result1, result2)
},
},
{
name: "transaction content contains an annotation instance",
test: func(t *testing.T) {
unique := test.FactoryRandomString()
ipfsMetadata := ipfsPublisherMetadata.NewSuccess(test.FactoryRandomString())
result := FactoryAnnotationTransaction(t, unique, ipfsMetadata)

content, err := converter.TrytesToASCII(result.SignatureMessageFragment[:len(result.SignatureMessageFragment)-9])
if err != nil {
assert.FailNow(t, "Unexpected error converting Trtyes to ASCII", err.Error())
}

a := factoryPublisherAnnotation(unique, ipfsMetadata)
marshalledAnnotation, err := json.Marshal(a)
if err != nil {
assert.FailNow(t, "Unexpected marshal failure", err.Error())
}
assert.IsType(t, string(marshalledAnnotation), content)
},
},
}

for i := range cases {
t.Run(cases[i].name, cases[i].test)
}
}
Loading

0 comments on commit 8fa5485

Please sign in to comment.