Skip to content

Commit 9577f57

Browse files
committed
Remove custom encoding types from interface objects
1 parent ce5c792 commit 9577f57

File tree

2 files changed

+73
-10
lines changed

2 files changed

+73
-10
lines changed

wallet/interfaces.go

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414

1515
// Certificate represents a basic certificate in the wallet
1616
type Certificate struct {
17-
Type Bytes32Base64 `json:"type"`
17+
Type [32]byte `json:"type"`
1818
SerialNumber Bytes32Base64 `json:"serialNumber"`
1919
Subject *ec.PublicKey `json:"subject"`
2020
Certifier *ec.PublicKey `json:"certifier"`
@@ -37,10 +37,12 @@ func (c *Certificate) MarshalJSON() ([]byte, error) {
3737
}
3838

3939
return json.Marshal(&struct {
40-
Subject *string `json:"subject"`
41-
Certifier *string `json:"certifier"`
40+
Type Bytes32Base64 `json:"type"`
41+
Subject *string `json:"subject"`
42+
Certifier *string `json:"certifier"`
4243
*Alias
4344
}{
45+
Type: c.Type,
4446
Subject: subjectHex,
4547
Certifier: certifierHex,
4648
Alias: (*Alias)(c),
@@ -51,8 +53,9 @@ func (c *Certificate) MarshalJSON() ([]byte, error) {
5153
func (c *Certificate) UnmarshalJSON(data []byte) error {
5254
type Alias Certificate // Use alias to avoid recursion
5355
aux := &struct {
54-
Subject *string `json:"subject"`
55-
Certifier *string `json:"certifier"`
56+
Type Bytes32Base64 `json:"type"`
57+
Subject *string `json:"subject"`
58+
Certifier *string `json:"certifier"`
5659
*Alias
5760
}{
5861
Alias: (*Alias)(c),
@@ -77,6 +80,7 @@ func (c *Certificate) UnmarshalJSON(data []byte) error {
7780
}
7881
c.Certifier = cert
7982
}
83+
c.Type = aux.Type
8084

8185
return nil
8286
}
@@ -627,7 +631,7 @@ func (r *KeyringRevealer) UnmarshalJSON(data []byte) error {
627631
// AcquireCertificateArgs contains parameters for acquiring a new certificate.
628632
// This includes the certificate type, certifier information, and acquisition method.
629633
type AcquireCertificateArgs struct {
630-
Type Bytes32Base64 `json:"type"`
634+
Type [32]byte `json:"type"`
631635
Certifier Bytes33Hex `json:"certifier"`
632636
AcquisitionProtocol AcquisitionProtocol `json:"acquisitionProtocol"` // "direct" | "issuance"
633637
Fields map[string]string `json:"fields,omitempty"`
@@ -641,6 +645,35 @@ type AcquireCertificateArgs struct {
641645
PrivilegedReason string `json:"privilegedReason,omitempty"`
642646
}
643647

648+
func (a *AcquireCertificateArgs) UnmarshalJSON(data []byte) error {
649+
type Alias AcquireCertificateArgs
650+
aux := &struct {
651+
Type Bytes32Base64 `json:"type"`
652+
*Alias
653+
}{
654+
Alias: (*Alias)(a),
655+
}
656+
657+
if err := json.Unmarshal(data, &aux); err != nil {
658+
return fmt.Errorf("error unmarshaling AcquireCertificateArgs: %w", err)
659+
}
660+
661+
a.Type = aux.Type
662+
663+
return nil
664+
}
665+
666+
func (a *AcquireCertificateArgs) MarshalJSON() ([]byte, error) {
667+
type Alias AcquireCertificateArgs
668+
return json.Marshal(&struct {
669+
Type Bytes32Base64 `json:"type"`
670+
*Alias
671+
}{
672+
Type: Bytes32Base64(a.Type[:]),
673+
Alias: (*Alias)(a),
674+
})
675+
}
676+
644677
// ListCertificatesArgs contains parameters for listing certificates with filtering and pagination.
645678
type ListCertificatesArgs struct {
646679
Certifiers []Bytes33Hex `json:"certifiers"`
@@ -724,11 +757,42 @@ type ListCertificatesResult struct {
724757

725758
// RelinquishCertificateArgs contains parameters for relinquishing ownership of a certificate.
726759
type RelinquishCertificateArgs struct {
727-
Type Bytes32Base64 `json:"type"`
760+
Type [32]byte `json:"type"`
728761
SerialNumber Bytes32Base64 `json:"serialNumber"`
729762
Certifier Bytes33Hex `json:"certifier"`
730763
}
731764

765+
func (r *RelinquishCertificateArgs) UnmarshalJSON(data []byte) error {
766+
// Unmarshal into a temporary struct to handle the Bytes32Base64 type
767+
type Alias RelinquishCertificateArgs
768+
aux := &struct {
769+
Type Bytes32Base64 `json:"type"`
770+
*Alias
771+
}{
772+
Alias: (*Alias)(r),
773+
}
774+
775+
if err := json.Unmarshal(data, &aux); err != nil {
776+
return fmt.Errorf("error unmarshaling RelinquishCertificateArgs: %w", err)
777+
}
778+
779+
r.Type = aux.Type
780+
781+
return nil
782+
}
783+
784+
func (r *RelinquishCertificateArgs) MarshalJSON() ([]byte, error) {
785+
// Marshal the Type as Bytes32Base64
786+
type Alias RelinquishCertificateArgs
787+
return json.Marshal(&struct {
788+
Type Bytes32Base64 `json:"type"`
789+
*Alias
790+
}{
791+
Type: Bytes32Base64(r.Type[:]),
792+
Alias: (*Alias)(r),
793+
})
794+
}
795+
732796
// RelinquishOutputArgs contains parameters for relinquishing ownership of an output.
733797
type RelinquishOutputArgs struct {
734798
Basket string `json:"basket"`

wallet/substrates/http_wallet_json_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ func TestHTTPWalletJSON_SignatureOperations(t *testing.T) {
442442
}
443443

444444
func TestHTTPWalletJSON_CertificateOperations(t *testing.T) {
445-
typeTest := wallet.Bytes32Base64(tu.GetByte32FromString("test-type"))
445+
typeTest := tu.GetByte32FromString("test-type")
446446
serialNumber := wallet.Bytes32Base64(tu.GetByte32FromString("12345"))
447447
certifier := wallet.Bytes33Hex(tu.GetByte33FromString("test-certifier"))
448448
verifier := tu.GetByte33FromString("test-verifier")
@@ -551,8 +551,7 @@ func TestHTTPWalletJSON_CertificateOperations(t *testing.T) {
551551
}
552552

553553
func TestHTTPWalletJSON_DiscoveryOperations(t *testing.T) {
554-
var typeDiscovered wallet.Bytes32Base64
555-
copy(typeDiscovered[:], "discovered-type")
554+
typeDiscovered := tu.GetByte32FromString("discovered-type")
556555
testKey := tu.GetByte33FromString("test-key")
557556
// Test DiscoverByIdentityKey
558557
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)