/
account_json.go
44 lines (36 loc) · 1005 Bytes
/
account_json.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
package currency
import (
"encoding/json"
"github.com/spikeekips/mitum/base"
jsonenc "github.com/spikeekips/mitum/util/encoder/json"
"github.com/spikeekips/mitum/util/valuehash"
)
type AccountPackerJSON struct {
jsonenc.HintedHead
H valuehash.Hash `json:"hash"`
AD base.Address `json:"address"`
KS AccountKeys `json:"keys"`
}
func (ac Account) PackerJSON() AccountPackerJSON {
return AccountPackerJSON{
HintedHead: jsonenc.NewHintedHead(ac.Hint()),
H: ac.h,
AD: ac.address,
KS: ac.keys,
}
}
func (ac Account) MarshalJSON() ([]byte, error) {
return jsonenc.Marshal(ac.PackerJSON())
}
type AccountJSONUnpacker struct {
H valuehash.Bytes `json:"hash"`
AD base.AddressDecoder `json:"address"`
KS json.RawMessage `json:"keys"`
}
func (ac *Account) UnpackJSON(b []byte, enc *jsonenc.Encoder) error {
var uac AccountJSONUnpacker
if err := enc.Unmarshal(b, &uac); err != nil {
return err
}
return ac.unpack(enc, uac.H, uac.AD, uac.KS)
}