-
Notifications
You must be signed in to change notification settings - Fork 4
/
account_json.go
138 lines (128 loc) · 3.52 KB
/
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
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
129
130
131
132
133
134
135
136
137
138
// Copyright 2019 - 2023 Weald Technology Trading.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package nd
import (
"encoding/hex"
"encoding/json"
"fmt"
"github.com/google/uuid"
"github.com/pkg/errors"
e2types "github.com/wealdtech/go-eth2-types/v2"
keystorev4 "github.com/wealdtech/go-eth2-wallet-encryptor-keystorev4"
)
// MarshalJSON implements custom JSON marshaller.
func (a *account) MarshalJSON() ([]byte, error) {
data := make(map[string]any)
data["uuid"] = a.id.String()
data["name"] = a.name
data["pubkey"] = fmt.Sprintf("%x", a.publicKey.Marshal())
data["crypto"] = a.crypto
data["encryptor"] = a.encryptor.String()
data["version"] = a.version
return json.Marshal(data)
}
// UnmarshalJSON implements custom JSON unmarshaller.
//
//nolint:cyclop
func (a *account) UnmarshalJSON(data []byte) error {
var v map[string]any
if err := json.Unmarshal(data, &v); err != nil {
return errors.Wrap(err, "failed to unmarshal account")
}
if val, exists := v["uuid"]; exists {
idStr, ok := val.(string)
if !ok {
return errors.New("account ID invalid")
}
id, err := uuid.Parse(idStr)
if err != nil {
return errors.Wrap(err, "failed to parse account ID")
}
a.id = id
} else if val, exists := v["id"]; exists {
idStr, ok := val.(string)
if !ok {
return errors.New("account ID invalid")
}
id, err := uuid.Parse(idStr)
if err != nil {
return errors.Wrap(err, "failed to parse account ID")
}
a.id = id
} else {
return errors.New("account ID missing")
}
if val, exists := v["name"]; exists {
name, ok := val.(string)
if !ok {
return errors.New("account name invalid")
}
a.name = name
} else {
return errors.New("account name missing")
}
if val, exists := v["pubkey"]; exists {
publicKey, ok := val.(string)
if !ok {
return errors.New("account pubkey invalid")
}
bytes, err := hex.DecodeString(publicKey)
if err != nil {
return errors.Wrap(err, "failed to decode public key")
}
a.publicKey, err = e2types.BLSPublicKeyFromBytes(bytes)
if err != nil {
return errors.Wrap(err, "invalid public key")
}
} else {
return errors.New("account pubkey missing")
}
if val, exists := v["crypto"]; exists {
crypto, ok := val.(map[string]any)
if !ok {
return errors.New("account crypto invalid")
}
a.crypto = crypto
} else {
return errors.New("account crypto missing")
}
if val, exists := v["version"]; exists {
version, ok := val.(float64)
if !ok {
return errors.New("account version invalid")
}
a.version = uint(version)
} else {
return errors.New("account version missing")
}
// Only support keystore v4 at current.
if a.version != 4 {
return errors.New("unsupported keystore version")
}
if val, exists := v["encryptor"]; !exists {
// Default.
a.encryptor = keystorev4.New()
} else {
encryptor, ok := val.(string)
if !ok {
return errors.New("encryptor invalid")
}
switch encryptor {
case "keystore", "keystorev4":
a.encryptor = keystorev4.New()
default:
return fmt.Errorf("unsupported encryptor %q", encryptor)
}
}
return nil
}