/
keyupdater.go
102 lines (84 loc) · 2.37 KB
/
keyupdater.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package currency
import (
"github.com/spikeekips/mitum/base"
"github.com/spikeekips/mitum/util"
"github.com/spikeekips/mitum/util/hint"
"github.com/spikeekips/mitum/util/isvalid"
"github.com/spikeekips/mitum/util/valuehash"
)
var (
KeyUpdaterFactType = hint.Type("mitum-currency-keyupdater-operation-fact")
KeyUpdaterFactHint = hint.NewHint(KeyUpdaterFactType, "v0.0.1")
KeyUpdaterFactHinter = KeyUpdaterFact{BaseHinter: hint.NewBaseHinter(KeyUpdaterFactHint)}
KeyUpdaterType = hint.Type("mitum-currency-keyupdater-operation")
KeyUpdaterHint = hint.NewHint(KeyUpdaterType, "v0.0.1")
KeyUpdaterHinter = KeyUpdater{BaseOperation: operationHinter(KeyUpdaterHint)}
)
type KeyUpdaterFact struct {
hint.BaseHinter
h valuehash.Hash
token []byte
target base.Address
keys AccountKeys
currency CurrencyID
}
func NewKeyUpdaterFact(token []byte, target base.Address, keys AccountKeys, currency CurrencyID) KeyUpdaterFact {
fact := KeyUpdaterFact{
BaseHinter: hint.NewBaseHinter(KeyUpdaterFactHint),
token: token,
target: target,
keys: keys,
currency: currency,
}
fact.h = fact.GenerateHash()
return fact
}
func (fact KeyUpdaterFact) Hash() valuehash.Hash {
return fact.h
}
func (fact KeyUpdaterFact) GenerateHash() valuehash.Hash {
return valuehash.NewSHA256(fact.Bytes())
}
func (fact KeyUpdaterFact) Bytes() []byte {
return util.ConcatBytesSlice(
fact.token,
fact.target.Bytes(),
fact.keys.Bytes(),
fact.currency.Bytes(),
)
}
func (fact KeyUpdaterFact) IsValid(b []byte) error {
if err := IsValidOperationFact(fact, b); err != nil {
return err
}
return isvalid.Check(nil, false,
fact.target,
fact.keys,
fact.currency,
)
}
func (fact KeyUpdaterFact) Token() []byte {
return fact.token
}
func (fact KeyUpdaterFact) Target() base.Address {
return fact.target
}
func (fact KeyUpdaterFact) Keys() AccountKeys {
return fact.keys
}
func (fact KeyUpdaterFact) Currency() CurrencyID {
return fact.currency
}
func (fact KeyUpdaterFact) Addresses() ([]base.Address, error) {
return []base.Address{fact.target}, nil
}
type KeyUpdater struct {
BaseOperation
}
func NewKeyUpdater(fact KeyUpdaterFact, fs []base.FactSign, memo string) (KeyUpdater, error) {
bo, err := NewBaseOperationFromFact(KeyUpdaterHint, fact, fs, memo)
if err != nil {
return KeyUpdater{}, err
}
return KeyUpdater{BaseOperation: bo}, nil
}