|
1 | 1 | package wallet |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/hex" |
4 | 5 | "encoding/json" |
5 | 6 | "fmt" |
6 | 7 |
|
@@ -32,6 +33,150 @@ func (s *PubKey) UnmarshalJSON(data []byte) error { |
32 | 33 | return (*Bytes33Hex)(s).UnmarshalJSON(data) |
33 | 34 | } |
34 | 35 |
|
| 36 | +// MarshalJSON implements the json.Marshaler interface for Protocol. |
| 37 | +// It serializes the Protocol as a JSON array containing [SecurityLevel, Protocol]. |
| 38 | +func (p *Protocol) MarshalJSON() ([]byte, error) { |
| 39 | + return json.Marshal([]interface{}{p.SecurityLevel, p.Protocol}) |
| 40 | +} |
| 41 | + |
| 42 | +// UnmarshalJSON implements the json.Unmarshaler interface for Protocol. |
| 43 | +// It deserializes a JSON array [SecurityLevel, Protocol] into the Protocol struct. |
| 44 | +func (p *Protocol) UnmarshalJSON(data []byte) error { |
| 45 | + var temp []interface{} |
| 46 | + if err := json.Unmarshal(data, &temp); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + if len(temp) != 2 { |
| 50 | + return fmt.Errorf("expected array of length 2, but got %d", len(temp)) |
| 51 | + } |
| 52 | + |
| 53 | + securityLevel, ok := temp[0].(float64) |
| 54 | + if !ok { |
| 55 | + return fmt.Errorf("expected SecurityLevel to be a number, but got %T", temp[0]) |
| 56 | + } |
| 57 | + p.SecurityLevel = SecurityLevel(securityLevel) |
| 58 | + |
| 59 | + protocol, ok := temp[1].(string) |
| 60 | + if !ok { |
| 61 | + return fmt.Errorf("expected Protocol to be a string, but got %T", temp[1]) |
| 62 | + } |
| 63 | + p.Protocol = protocol |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +// MarshalJSON implements the json.Marshaler interface for Counterparty. |
| 69 | +// It serializes special counterparty types as strings ("anyone", "self") and |
| 70 | +// specific counterparties as their DER-encoded hex public key. |
| 71 | +func (c *Counterparty) MarshalJSON() ([]byte, error) { |
| 72 | + switch c.Type { |
| 73 | + case CounterpartyTypeAnyone: |
| 74 | + return json.Marshal("anyone") |
| 75 | + case CounterpartyTypeSelf: |
| 76 | + return json.Marshal("self") |
| 77 | + case CounterpartyTypeOther: |
| 78 | + if c.Counterparty == nil { |
| 79 | + return json.Marshal(nil) // Or handle this as an error if it should never happen |
| 80 | + } |
| 81 | + return json.Marshal(c.Counterparty.ToDERHex()) |
| 82 | + default: |
| 83 | + return json.Marshal(nil) // Or handle this as an error if it should never happen |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// UnmarshalJSON implements the json.Unmarshaler interface for Counterparty. |
| 88 | +// It deserializes "anyone", "self", or a DER-encoded hex public key string |
| 89 | +// into the appropriate Counterparty struct. |
| 90 | +func (c *Counterparty) UnmarshalJSON(data []byte) error { |
| 91 | + var s string |
| 92 | + if err := json.Unmarshal(data, &s); err != nil { |
| 93 | + return fmt.Errorf("could not unmarshal Counterparty from JSON: %s", string(data)) |
| 94 | + } |
| 95 | + switch s { |
| 96 | + case "anyone": |
| 97 | + c.Type = CounterpartyTypeAnyone |
| 98 | + case "self": |
| 99 | + c.Type = CounterpartyTypeSelf |
| 100 | + case "": |
| 101 | + c.Type = CounterpartyUninitialized |
| 102 | + default: |
| 103 | + // Attempt to parse as a public key string |
| 104 | + pubKey, err := ec.PublicKeyFromString(s) |
| 105 | + if err != nil { |
| 106 | + return fmt.Errorf("error unmarshaling counterparty: %w", err) |
| 107 | + } |
| 108 | + c.Type = CounterpartyTypeOther |
| 109 | + c.Counterparty = pubKey |
| 110 | + } |
| 111 | + return nil |
| 112 | +} |
| 113 | + |
| 114 | +// MarshalJSON implements the json.Marshaler interface for CreateSignatureResult. |
| 115 | +func (c CreateSignatureResult) MarshalJSON() ([]byte, error) { |
| 116 | + // Use an alias struct with Signature for marshaling |
| 117 | + type Alias CreateSignatureResult |
| 118 | + return json.Marshal(&struct { |
| 119 | + *Alias |
| 120 | + Signature Signature `json:"signature"` // Override Signature field |
| 121 | + }{ |
| 122 | + Alias: (*Alias)(&c), |
| 123 | + Signature: Signature{Signature: c.Signature}, |
| 124 | + }) |
| 125 | +} |
| 126 | + |
| 127 | +// UnmarshalJSON implements the json.Unmarshaler interface for CreateSignatureResult. |
| 128 | +func (c *CreateSignatureResult) UnmarshalJSON(data []byte) error { |
| 129 | + // Use an alias struct with Signature for unmarshaling |
| 130 | + type Alias CreateSignatureResult |
| 131 | + aux := &struct { |
| 132 | + *Alias |
| 133 | + Signature Signature `json:"signature"` // Override Signature field |
| 134 | + }{ |
| 135 | + Alias: (*Alias)(c), |
| 136 | + } |
| 137 | + |
| 138 | + if err := json.Unmarshal(data, &aux); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + // Assign the unmarshaled signature back |
| 143 | + c.Signature = aux.Signature.Signature |
| 144 | + return nil |
| 145 | +} |
| 146 | + |
| 147 | +// MarshalJSON implements the json.Marshaler interface for VerifySignatureArgs. |
| 148 | +func (v VerifySignatureArgs) MarshalJSON() ([]byte, error) { |
| 149 | + // Use an alias struct with Signature for marshaling |
| 150 | + type Alias VerifySignatureArgs |
| 151 | + return json.Marshal(&struct { |
| 152 | + *Alias |
| 153 | + Signature Signature `json:"signature"` // Override Signature field |
| 154 | + }{ |
| 155 | + Alias: (*Alias)(&v), |
| 156 | + Signature: Signature{Signature: v.Signature}, |
| 157 | + }) |
| 158 | +} |
| 159 | + |
| 160 | +// UnmarshalJSON implements the json.Unmarshaler interface for VerifySignatureArgs. |
| 161 | +func (v *VerifySignatureArgs) UnmarshalJSON(data []byte) error { |
| 162 | + // Use an alias struct with Signature for unmarshaling |
| 163 | + type Alias VerifySignatureArgs |
| 164 | + aux := &struct { |
| 165 | + *Alias |
| 166 | + Signature Signature `json:"signature"` // Override Signature field |
| 167 | + }{ |
| 168 | + Alias: (*Alias)(v), |
| 169 | + } |
| 170 | + |
| 171 | + if err := json.Unmarshal(data, &aux); err != nil { |
| 172 | + return err |
| 173 | + } |
| 174 | + |
| 175 | + // Assign the unmarshaled signature back |
| 176 | + v.Signature = aux.Signature.Signature |
| 177 | + return nil |
| 178 | +} |
| 179 | + |
35 | 180 | // aliasCertificate uses an alias to avoid recursion |
36 | 181 | type aliasCertificate Certificate |
37 | 182 | type jsonCertificate struct { |
@@ -451,6 +596,100 @@ func (r *RevealSpecificKeyLinkageResult) UnmarshalJSON(data []byte) error { |
451 | 596 | return nil |
452 | 597 | } |
453 | 598 |
|
| 599 | +// MarshalJSON implements the json.Marshaler interface for IdentityCertificate. |
| 600 | +// It handles the flattening of the embedded Certificate fields. |
| 601 | +func (ic *IdentityCertificate) MarshalJSON() ([]byte, error) { |
| 602 | + // Start with marshaling the embedded Certificate |
| 603 | + certData, err := json.Marshal(&ic.Certificate) |
| 604 | + if err != nil { |
| 605 | + return nil, fmt.Errorf("error marshaling embedded Certificate: %w", err) |
| 606 | + } |
| 607 | + |
| 608 | + // Unmarshal certData into a map |
| 609 | + var certMap map[string]interface{} |
| 610 | + if err := json.Unmarshal(certData, &certMap); err != nil { |
| 611 | + return nil, fmt.Errorf("error unmarshaling cert data into map: %w", err) |
| 612 | + } |
| 613 | + |
| 614 | + // Add IdentityCertificate specific fields to the map |
| 615 | + certMap["certifierInfo"] = ic.CertifierInfo |
| 616 | + if ic.PubliclyRevealedKeyring != nil { |
| 617 | + certMap["publiclyRevealedKeyring"] = ic.PubliclyRevealedKeyring |
| 618 | + } |
| 619 | + if ic.DecryptedFields != nil { |
| 620 | + certMap["decryptedFields"] = ic.DecryptedFields |
| 621 | + } |
| 622 | + |
| 623 | + // Marshal the final map |
| 624 | + return json.Marshal(certMap) |
| 625 | +} |
| 626 | + |
| 627 | +// UnmarshalJSON implements the json.Unmarshaler interface for IdentityCertificate. |
| 628 | +// It handles the flattening of the embedded Certificate fields. |
| 629 | +func (ic *IdentityCertificate) UnmarshalJSON(data []byte) error { |
| 630 | + // Unmarshal into the embedded Certificate first |
| 631 | + if err := json.Unmarshal(data, &ic.Certificate); err != nil { |
| 632 | + return fmt.Errorf("error unmarshaling embedded Certificate: %w", err) |
| 633 | + } |
| 634 | + |
| 635 | + // Unmarshal into a temporary map to get the other fields |
| 636 | + var temp map[string]json.RawMessage |
| 637 | + if err := json.Unmarshal(data, &temp); err != nil { |
| 638 | + return fmt.Errorf("error unmarshaling into temp map: %w", err) |
| 639 | + } |
| 640 | + |
| 641 | + // Unmarshal CertifierInfo |
| 642 | + if certInfoData, ok := temp["certifierInfo"]; ok { |
| 643 | + if err := json.Unmarshal(certInfoData, &ic.CertifierInfo); err != nil { |
| 644 | + return fmt.Errorf("error unmarshaling certifierInfo: %w", err) |
| 645 | + } |
| 646 | + } |
| 647 | + |
| 648 | + // Unmarshal PubliclyRevealedKeyring |
| 649 | + if pubKeyringData, ok := temp["publiclyRevealedKeyring"]; ok { |
| 650 | + if err := json.Unmarshal(pubKeyringData, &ic.PubliclyRevealedKeyring); err != nil { |
| 651 | + return fmt.Errorf("error unmarshaling publiclyRevealedKeyring: %w", err) |
| 652 | + } |
| 653 | + } |
| 654 | + |
| 655 | + // Unmarshal DecryptedFields |
| 656 | + if decryptedData, ok := temp["decryptedFields"]; ok { |
| 657 | + if err := json.Unmarshal(decryptedData, &ic.DecryptedFields); err != nil { |
| 658 | + return fmt.Errorf("error unmarshaling decryptedFields: %w", err) |
| 659 | + } |
| 660 | + } |
| 661 | + |
| 662 | + return nil |
| 663 | +} |
| 664 | + |
| 665 | +func (r KeyringRevealer) MarshalJSON() ([]byte, error) { |
| 666 | + if r.Certifier { |
| 667 | + return json.Marshal(KeyringRevealerCertifier) |
| 668 | + } |
| 669 | + return json.Marshal(hex.EncodeToString(r.PubKey[:])) |
| 670 | +} |
| 671 | + |
| 672 | +func (r *KeyringRevealer) UnmarshalJSON(data []byte) error { |
| 673 | + var str string |
| 674 | + if err := json.Unmarshal(data, &str); err != nil { |
| 675 | + return fmt.Errorf("error unmarshaling revealer: %w", err) |
| 676 | + } |
| 677 | + |
| 678 | + if str == KeyringRevealerCertifier { |
| 679 | + r.Certifier = true |
| 680 | + return nil |
| 681 | + } |
| 682 | + data, err := hex.DecodeString(str) |
| 683 | + if err != nil { |
| 684 | + return fmt.Errorf("error decoding revealer hex: %w", err) |
| 685 | + } |
| 686 | + if len(data) != 33 { |
| 687 | + return fmt.Errorf("revealer hex must be 33 bytes, got %d", len(data)) |
| 688 | + } |
| 689 | + copy(r.PubKey[:], data) |
| 690 | + return nil |
| 691 | +} |
| 692 | + |
454 | 693 | // Custom marshalling for AcquireCertificateArgs |
455 | 694 | type aliasAcquireCertificateArgs AcquireCertificateArgs |
456 | 695 | type jsonAcquireCertificateArgs struct { |
|
0 commit comments