/
account_serializer.go
128 lines (105 loc) · 3.3 KB
/
account_serializer.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Copyright 2019 The klaytn Authors
// This file is part of the klaytn library.
//
// The klaytn library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The klaytn library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the klaytn library. If not, see <http://www.gnu.org/licenses/>.
package account
import (
"encoding/json"
"github.com/klaytn/klaytn/ser/rlp"
"io"
)
// AccountSerializer serializes an Account object using RLP/JSON.
type AccountSerializer struct {
accType AccountType
account Account
}
// accountJSON is an internal data structure for JSON serialization.
type accountJSON struct {
AccType AccountType `json:"accType"`
Account json.RawMessage `json:"account"`
}
// NewAccountSerializer creates a new AccountSerializer object with default values.
// This returned object will be used for decoding.
func NewAccountSerializer() *AccountSerializer {
return &AccountSerializer{}
}
// NewAccountSerializerWithAccount creates a new AccountSerializer object with the given account.
func NewAccountSerializerWithAccount(a Account) *AccountSerializer {
return &AccountSerializer{a.Type(), a}
}
func (ser *AccountSerializer) EncodeRLP(w io.Writer) error {
// If it is a LegacyAccount object, do not encode the account type.
if ser.accType == LegacyAccountType {
return rlp.Encode(w, ser.account.(*LegacyAccount))
}
if err := rlp.Encode(w, ser.accType); err != nil {
return err
}
return rlp.Encode(w, ser.account)
}
func (ser *AccountSerializer) GetAccount() Account {
return ser.account
}
func (ser *AccountSerializer) DecodeRLP(s *rlp.Stream) error {
if err := s.Decode(&ser.accType); err != nil {
// fallback to decoding a LegacyAccount object.
acc := newLegacyAccount()
if err := s.Decode(acc); err != nil {
return err
}
ser.accType = LegacyAccountType
ser.account = acc
return nil
}
var err error
ser.account, err = NewAccountWithType(ser.accType)
if err != nil {
return err
}
return s.Decode(ser.account)
}
func (ser *AccountSerializer) MarshalJSON() ([]byte, error) {
// if it is a legacyAccount object, do not marshal the account type.
if ser.accType == LegacyAccountType {
return json.Marshal(ser.account)
}
b, err := json.Marshal(ser.account)
if err != nil {
return nil, err
}
return json.Marshal(&accountJSON{ser.accType, b})
}
func (ser *AccountSerializer) UnmarshalJSON(b []byte) error {
dec := &accountJSON{}
if err := json.Unmarshal(b, dec); err != nil {
return err
}
if len(dec.Account) == 0 {
// fallback to unmarshal a LegacyAccount object.
acc := newLegacyAccount()
if err := json.Unmarshal(b, acc); err != nil {
return err
}
ser.accType = LegacyAccountType
ser.account = acc
return nil
}
ser.accType = dec.AccType
var err error
ser.account, err = NewAccountWithType(ser.accType)
if err != nil {
return err
}
return json.Unmarshal(dec.Account, ser.account)
}