Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<div align="center">
<img style="border-radius:5px" src="https://w3s.link/ipfs/bafybeibfuicu45rj2gw6a362gxys2jdzmsobnwavj45ny5lt74yd6bmlvq/libracha.webp" alt="libracha logo" width="180" />
<h1>libforge</h1>
<p>Common libraries and tools for Forge Network UCAN 1.0 projects.</p>
</div>
# libforge

Common libraries and tools for Forge Network UCAN 1.0 projects.
80 changes: 80 additions & 0 deletions bytemap/bytemap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package bytemap

import (
"iter"
"maps"
)

type byteMap[K ~[]byte, T any] struct {
data map[string]T
}

// ByteMap is a generic for mapping byte array like types to arbitrary data types
type ByteMap[K ~[]byte, T any] interface {
Get(K) T
Has(K) bool
Set(K, T)
Delete(K) bool
Size() int
Iterator() iter.Seq2[K, T]
Keys() iter.Seq[K]
Values() iter.Seq[T]
}

// NewByteMap returns a new map of multihash to a data type
func NewByteMap[K ~[]byte, T any](sizeHint int) ByteMap[K, T] {
var stringMap map[string]T
if sizeHint == -1 {
stringMap = make(map[string]T)
} else {
stringMap = make(map[string]T, sizeHint)
}
return &byteMap[K, T]{stringMap}
}

func (bm *byteMap[K, T]) Get(b K) T {
return bm.data[string(b)]
}

func (bm *byteMap[K, T]) Has(b K) bool {
_, ok := bm.data[string(b)]
return ok
}

func (bm *byteMap[K, T]) Set(b K, t T) {
bm.data[string(b)] = t
}

func (bm *byteMap[K, T]) Delete(b K) bool {
_, ok := bm.data[string(b)]
delete(bm.data, string(b))
return ok
}

func (bm *byteMap[K, T]) Size() int {
return len(bm.data)
}

func (bm *byteMap[K, T]) Iterator() iter.Seq2[K, T] {
return func(yield func(K, T) bool) {
for k, v := range bm.data {
if !yield(K(k), v) {
return
}
}
}
}

func (bm *byteMap[K, T]) Keys() iter.Seq[K] {
return func(yield func(K) bool) {
for k := range bm.data {
if !yield(K(k)) {
return
}
}
}
}

func (bm *byteMap[K, T]) Values() iter.Seq[T] {
return maps.Values(bm.data)
}
48 changes: 48 additions & 0 deletions bytemap/bytemap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package bytemap_test

import (
"testing"

"github.com/fil-forge/libforge/bytemap"
"github.com/multiformats/go-multihash"
"github.com/stretchr/testify/require"
)

func TestKeys(t *testing.T) {
bm := bytemap.NewByteMap[multihash.Multihash, struct{}](-1)

d0, err := multihash.Sum([]byte{1, 2, 3}, multihash.SHA2_256, -1)
require.NoError(t, err)
d1, err := multihash.Sum([]byte{4, 5, 6}, multihash.SHA2_256, -1)
require.NoError(t, err)
d2, err := multihash.Sum([]byte{7, 8, 9}, multihash.SHA2_256, -1)
require.NoError(t, err)

digests := []multihash.Multihash{d0, d1, d2}
for _, d := range digests {
bm.Set(d, struct{}{})
}

var keys []multihash.Multihash
for d := range bm.Keys() {
keys = append(keys, d)
}

require.ElementsMatch(t, digests, keys)
}

func TestValues(t *testing.T) {
bm := bytemap.NewByteMap[multihash.Multihash, string](-1)

strings := []string{"this", "is", "fine"}
for _, v := range strings {
bm.Set([]byte("key"+v), v)
}

var values []string
for d := range bm.Values() {
values = append(values, d)
}

require.ElementsMatch(t, strings, values)
}
11 changes: 11 additions & 0 deletions capabilities/access/confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package access

import (
adm "github.com/fil-forge/libforge/capabilities/access/datamodel"
"github.com/fil-forge/ucantone/errors"
"github.com/fil-forge/ucantone/validator/bindcap"
)

Expand All @@ -19,3 +20,13 @@ const ConfirmCommand = "/access/confirm"

// Confirm can be invoked by an agent to confirm an access request.
var Confirm, _ = bindcap.New[*ConfirmArguments](ConfirmCommand)

const (
InvalidAccessConfirmSubjectErrorName = "InvalidAccessConfirmSubject"
InvalidAccessConfirmIssuerErrorName = "InvalidAccessConfirmIssuer"
)

var (
ErrInvalidAccessConfirmSubject = errors.New(InvalidAccessConfirmSubjectErrorName, "the subject of an access confirm invocation must be the service itself")
ErrInvalidAccessConfirmIssuer = errors.New(InvalidAccessConfirmIssuerErrorName, "the issuer of an access confirm invocation must be a valid mailto DID")
)
Loading