-
Notifications
You must be signed in to change notification settings - Fork 38
/
storage.go
51 lines (41 loc) · 965 Bytes
/
storage.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package storage
import (
"errors"
"sync"
"github.com/mr-tron/base58"
"github.com/nspcc-dev/neofs-sdk-go/owner"
)
type key struct {
tokenID string
ownerID string
}
type TokenStore struct {
mtx *sync.RWMutex
tokens map[key]*PrivateToken
}
var ErrNotFound = errors.New("private token not found")
// New creates, initializes and returns a new TokenStore instance.
//
// The elements of the instance are stored in the map.
func New() *TokenStore {
return &TokenStore{
mtx: new(sync.RWMutex),
tokens: make(map[key]*PrivateToken),
}
}
// Get returns private token corresponding to the given identifiers.
//
// Returns nil is there is no element in storage.
func (s *TokenStore) Get(ownerID *owner.ID, tokenID []byte) *PrivateToken {
ownerBytes, err := ownerID.Marshal()
if err != nil {
panic(err)
}
s.mtx.RLock()
t := s.tokens[key{
tokenID: base58.Encode(tokenID),
ownerID: base58.Encode(ownerBytes),
}]
s.mtx.RUnlock()
return t
}