-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosetta_account.go
59 lines (53 loc) · 2.18 KB
/
rosetta_account.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
package models
import (
"encoding/json"
)
// RosettaAccount represents a RosettaAccount struct.
// The account_identifier uniquely identifies an account within a network. All fields in the account_identifier are utilized to determine this uniqueness (including the metadata field, if populated).
type RosettaAccount struct {
// The address may be a cryptographic public key (or some encoding of it) or a provided username.
Address string `json:"address"`
// An account may have state specific to a contract address (ERC-20 token) and/or a stake (delegated balance). The sub_account_identifier should specify which state (if applicable) an account instantiation refers to.
SubAccount *RosettaSubAccount `json:"sub_account,omitempty"`
// Blockchains that utilize a username model (where the address is not a derivative of a cryptographic public key) should specify the public key(s) owned by the address in metadata.
Metadata *interface{} `json:"metadata,omitempty"`
}
// MarshalJSON implements the json.Marshaler interface for RosettaAccount.
// It customizes the JSON marshaling process for RosettaAccount objects.
func (r *RosettaAccount) MarshalJSON() (
[]byte,
error) {
return json.Marshal(r.toMap())
}
// toMap converts the RosettaAccount object to a map representation for JSON marshaling.
func (r *RosettaAccount) toMap() map[string]any {
structMap := make(map[string]any)
structMap["address"] = r.Address
if r.SubAccount != nil {
structMap["sub_account"] = r.SubAccount
}
if r.Metadata != nil {
structMap["metadata"] = r.Metadata
}
return structMap
}
// UnmarshalJSON implements the json.Unmarshaler interface for RosettaAccount.
// It customizes the JSON unmarshaling process for RosettaAccount objects.
func (r *RosettaAccount) UnmarshalJSON(input []byte) error {
if input[0] == '"' {
return unmarshalResponseString(input)
}
temp := &struct {
Address string `json:"address"`
SubAccount *RosettaSubAccount `json:"sub_account,omitempty"`
Metadata *interface{} `json:"metadata,omitempty"`
}{}
err := json.Unmarshal(input, &temp)
if err != nil {
return err
}
r.Address = temp.Address
r.SubAccount = temp.SubAccount
r.Metadata = temp.Metadata
return nil
}