-
Notifications
You must be signed in to change notification settings - Fork 46
/
model.go
85 lines (70 loc) · 1.95 KB
/
model.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package nft
import (
"github.com/iov-one/weave"
"github.com/iov-one/weave/errors"
"github.com/iov-one/weave/migration"
"github.com/iov-one/weave/orm"
)
func init() {
migration.MustRegister(1, &NonFungibleToken{}, migration.NoModification)
}
var _ orm.CloneableData = (*NonFungibleToken)(nil)
func (m *NonFungibleToken) Validate() error {
if err := m.Metadata.Validate(); err != nil {
return errors.Wrap(err, "metadata")
}
if !isValidTokenID(m.ID) {
return errors.Wrapf(errors.ErrInput, "id: %s", PrintableID(m.ID))
}
if err := m.Owner.Validate(); err != nil {
return err
}
return nil
}
func (m *NonFungibleToken) Copy() orm.CloneableData {
return m.Clone()
}
func (m *NonFungibleToken) Clone() *NonFungibleToken {
actionApprovals := make([]ActionApprovals, len(m.ActionApprovals))
for i, v := range m.ActionApprovals {
actionApprovals[i] = v.Clone()
}
return &NonFungibleToken{
Metadata: m.Metadata.Copy(),
ID: m.ID,
Owner: m.Owner,
ActionApprovals: actionApprovals,
}
}
func NewNonFungibleToken(key []byte, owner weave.Address, approvals []ActionApprovals) *NonFungibleToken {
return &NonFungibleToken{
Metadata: &weave.Metadata{Schema: 1},
ID: key,
Owner: owner,
ActionApprovals: approvals,
}
}
func (u *NonFungibleToken) OwnerAddress() weave.Address {
return weave.Address(u.Owner)
}
func (m *NonFungibleToken) Approvals() *ApprovalOps {
return NewApprovalOps(m.Owner, &m.ActionApprovals)
}
func (m *NonFungibleToken) SetApprovals(a Approvals) {
m.ActionApprovals = a.AsPersistable()
}
func (m *NonFungibleToken) HasApproval(actor weave.Address, action Action) bool {
return !NewApprovalOps(m.Owner, &m.ActionApprovals).
List().ForAction(action).ForAddress(actor).IsEmpty()
}
type BaseNFT interface {
Owned
//GetId() []byte
Approvals() *ApprovalOps
//Set new approvals
SetApprovals(Approvals)
}
//TODO: Better name
type Identified interface {
GetID() []byte
}