-
Notifications
You must be signed in to change notification settings - Fork 2
/
create_account_item.go
101 lines (83 loc) · 2.73 KB
/
create_account_item.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
package currency
import (
"github.com/ProtoconNet/mitum-currency/v3/common"
"github.com/ProtoconNet/mitum-currency/v3/types"
"github.com/ProtoconNet/mitum2/base"
"github.com/ProtoconNet/mitum2/util"
"github.com/ProtoconNet/mitum2/util/hint"
"github.com/pkg/errors"
)
type BaseCreateAccountItem struct {
hint.BaseHinter
keys types.AccountKeys
amounts []types.Amount
addressType hint.Type
}
func NewBaseCreateAccountItem(ht hint.Hint, keys types.AccountKeys, amounts []types.Amount, addrHint hint.Type) BaseCreateAccountItem {
return BaseCreateAccountItem{
BaseHinter: hint.NewBaseHinter(ht),
keys: keys,
amounts: amounts,
addressType: addrHint,
}
}
func (it BaseCreateAccountItem) Bytes() []byte {
bs := make([][]byte, len(it.amounts)+2)
bs[0] = it.keys.Bytes()
bs[1] = it.addressType.Bytes()
for i := range it.amounts {
bs[i+2] = it.amounts[i].Bytes()
}
return util.ConcatBytesSlice(bs...)
}
func (it BaseCreateAccountItem) IsValid([]byte) error {
if n := len(it.amounts); n == 0 {
return common.ErrItemInvalid.Wrap(common.ErrArrayLen.Wrap(errors.Errorf("Empty amounts")))
}
if err := util.CheckIsValiders(nil, false, it.BaseHinter, it.keys); err != nil {
return common.ErrItemInvalid.Wrap(err)
}
if it.addressType != types.AddressHint.Type() && it.addressType != types.EthAddressHint.Type() {
return common.ErrItemInvalid.Wrap(common.ErrValOOR.Wrap(errors.Errorf("Neither AddressHint nor EteAddressHint")))
}
founds := map[types.CurrencyID]struct{}{}
for i := range it.amounts {
am := it.amounts[i]
if _, found := founds[am.Currency()]; found {
return common.ErrItemInvalid.Wrap(common.ErrDupVal.Wrap(errors.Errorf("Currency id, %v", am.Currency())))
}
founds[am.Currency()] = struct{}{}
if err := am.IsValid(nil); err != nil {
return err
} else if !am.Big().OverZero() {
return common.ErrItemInvalid.Wrap(common.ErrValOOR.Wrap(errors.Errorf("Amount should be over zero")))
}
}
return nil
}
func (it BaseCreateAccountItem) Keys() types.AccountKeys {
return it.keys
}
func (it BaseCreateAccountItem) Address() (base.Address, error) {
if it.addressType == types.AddressHint.Type() {
return types.NewAddressFromKeys(it.keys)
} else if it.addressType == types.EthAddressHint.Type() {
return types.NewEthAddressFromKeys(it.keys)
}
return nil, util.ErrInvalid.Errorf("Invalid address hint")
}
func (it BaseCreateAccountItem) AddressType() hint.Type {
return it.addressType
}
func (it BaseCreateAccountItem) Amounts() []types.Amount {
return it.amounts
}
func (it BaseCreateAccountItem) Rebuild() CreateAccountItem {
ams := make([]types.Amount, len(it.amounts))
for i := range it.amounts {
am := it.amounts[i]
ams[i] = am.WithBig(am.Big())
}
it.amounts = ams
return it
}