Skip to content

Commit 7bf40e7

Browse files
committed
Move custom certificate encoding types into marshaler only
1 parent 9577f57 commit 7bf40e7

File tree

3 files changed

+116
-97
lines changed

3 files changed

+116
-97
lines changed

wallet/encoding_json.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package wallet
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
ec "github.com/bsv-blockchain/go-sdk/primitives/ec"
8+
)
9+
10+
// aliasCertificate uses an alias to avoid recursion
11+
type aliasCertificate Certificate
12+
13+
type jsonCertificate struct {
14+
Type Bytes32Base64 `json:"type"`
15+
SerialNumber Bytes32Base64 `json:"serialNumber"`
16+
Subject *string `json:"subject"`
17+
Certifier *string `json:"certifier"`
18+
Signature BytesHex `json:"signature"`
19+
*aliasCertificate
20+
}
21+
22+
// MarshalJSON implements json.Marshaler interface for Certificate
23+
func (c Certificate) MarshalJSON() ([]byte, error) {
24+
var subjectHex, certifierHex *string
25+
if c.Subject != nil {
26+
s := c.Subject.ToDERHex()
27+
subjectHex = &s
28+
}
29+
if c.Certifier != nil {
30+
cs := c.Certifier.ToDERHex()
31+
certifierHex = &cs
32+
}
33+
34+
return json.Marshal(&jsonCertificate{
35+
Type: c.Type,
36+
SerialNumber: c.SerialNumber,
37+
Signature: c.Signature,
38+
Subject: subjectHex,
39+
Certifier: certifierHex,
40+
aliasCertificate: (*aliasCertificate)(&c),
41+
})
42+
}
43+
44+
// UnmarshalJSON implements json.Unmarshaler interface for Certificate
45+
func (c *Certificate) UnmarshalJSON(data []byte) error {
46+
aux := &jsonCertificate{
47+
aliasCertificate: (*aliasCertificate)(c),
48+
}
49+
50+
if err := json.Unmarshal(data, &aux); err != nil {
51+
return fmt.Errorf("error unmarshaling certificate: %w", err)
52+
}
53+
54+
// Decode public key hex strings
55+
if aux.Subject != nil {
56+
sub, err := ec.PublicKeyFromString(*aux.Subject)
57+
if err != nil {
58+
return fmt.Errorf("error decoding subject public key hex: %w", err)
59+
}
60+
c.Subject = sub
61+
}
62+
if aux.Certifier != nil {
63+
cert, err := ec.PublicKeyFromString(*aux.Certifier)
64+
if err != nil {
65+
return fmt.Errorf("error decoding certifier public key hex: %w", err)
66+
}
67+
c.Certifier = cert
68+
}
69+
70+
c.Type = aux.Type
71+
c.SerialNumber = aux.SerialNumber
72+
c.Signature = aux.Signature
73+
74+
return nil
75+
}
76+
77+
type aliasAcquireCertificateArgs AcquireCertificateArgs
78+
79+
type jsonAcquireCertificateArgs struct {
80+
Type Bytes32Base64 `json:"type"`
81+
SerialNumber Bytes32Base64 `json:"serialNumber"`
82+
Certifier Bytes33Hex `json:"certifier"`
83+
*aliasAcquireCertificateArgs
84+
}
85+
86+
func (a AcquireCertificateArgs) MarshalJSON() ([]byte, error) {
87+
return json.Marshal(&jsonAcquireCertificateArgs{
88+
Type: a.Type,
89+
SerialNumber: a.SerialNumber,
90+
Certifier: a.Certifier,
91+
aliasAcquireCertificateArgs: (*aliasAcquireCertificateArgs)(&a),
92+
})
93+
}
94+
95+
func (a *AcquireCertificateArgs) UnmarshalJSON(data []byte) error {
96+
type Alias AcquireCertificateArgs
97+
aux := &jsonAcquireCertificateArgs{
98+
aliasAcquireCertificateArgs: (*aliasAcquireCertificateArgs)(a),
99+
}
100+
101+
if err := json.Unmarshal(data, &aux); err != nil {
102+
return fmt.Errorf("error unmarshaling AcquireCertificateArgs: %w", err)
103+
}
104+
105+
a.Type = aux.Type
106+
a.SerialNumber = aux.SerialNumber
107+
a.Certifier = aux.Certifier
108+
109+
return nil
110+
}

wallet/interfaces.go

Lines changed: 4 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -15,74 +15,12 @@ import (
1515
// Certificate represents a basic certificate in the wallet
1616
type Certificate struct {
1717
Type [32]byte `json:"type"`
18-
SerialNumber Bytes32Base64 `json:"serialNumber"`
18+
SerialNumber [32]byte `json:"serialNumber"`
1919
Subject *ec.PublicKey `json:"subject"`
2020
Certifier *ec.PublicKey `json:"certifier"`
2121
RevocationOutpoint *Outpoint `json:"revocationOutpoint,omitempty"`
2222
Fields map[string]string `json:"fields,omitempty"` // Field name -> field value (encrypted)
23-
Signature BytesHex `json:"signature,omitempty"`
24-
}
25-
26-
// MarshalJSON implements json.Marshaler interface for Certificate
27-
func (c *Certificate) MarshalJSON() ([]byte, error) {
28-
type Alias Certificate // Use alias to avoid recursion
29-
var subjectHex, certifierHex *string
30-
if c.Subject != nil {
31-
s := c.Subject.ToDERHex()
32-
subjectHex = &s
33-
}
34-
if c.Certifier != nil {
35-
cs := c.Certifier.ToDERHex()
36-
certifierHex = &cs
37-
}
38-
39-
return json.Marshal(&struct {
40-
Type Bytes32Base64 `json:"type"`
41-
Subject *string `json:"subject"`
42-
Certifier *string `json:"certifier"`
43-
*Alias
44-
}{
45-
Type: c.Type,
46-
Subject: subjectHex,
47-
Certifier: certifierHex,
48-
Alias: (*Alias)(c),
49-
})
50-
}
51-
52-
// UnmarshalJSON implements json.Unmarshaler interface for Certificate
53-
func (c *Certificate) UnmarshalJSON(data []byte) error {
54-
type Alias Certificate // Use alias to avoid recursion
55-
aux := &struct {
56-
Type Bytes32Base64 `json:"type"`
57-
Subject *string `json:"subject"`
58-
Certifier *string `json:"certifier"`
59-
*Alias
60-
}{
61-
Alias: (*Alias)(c),
62-
}
63-
64-
if err := json.Unmarshal(data, &aux); err != nil {
65-
return fmt.Errorf("error unmarshaling certificate: %w", err)
66-
}
67-
68-
// Decode public key hex strings
69-
if aux.Subject != nil {
70-
sub, err := ec.PublicKeyFromString(*aux.Subject)
71-
if err != nil {
72-
return fmt.Errorf("error decoding subject public key hex: %w", err)
73-
}
74-
c.Subject = sub
75-
}
76-
if aux.Certifier != nil {
77-
cert, err := ec.PublicKeyFromString(*aux.Certifier)
78-
if err != nil {
79-
return fmt.Errorf("error decoding certifier public key hex: %w", err)
80-
}
81-
c.Certifier = cert
82-
}
83-
c.Type = aux.Type
84-
85-
return nil
23+
Signature []byte `json:"signature,omitempty"`
8624
}
8725

8826
func (c *Certificate) RevocationOutpointString() string {
@@ -632,10 +570,10 @@ func (r *KeyringRevealer) UnmarshalJSON(data []byte) error {
632570
// This includes the certificate type, certifier information, and acquisition method.
633571
type AcquireCertificateArgs struct {
634572
Type [32]byte `json:"type"`
635-
Certifier Bytes33Hex `json:"certifier"`
573+
Certifier [33]byte `json:"certifier"`
636574
AcquisitionProtocol AcquisitionProtocol `json:"acquisitionProtocol"` // "direct" | "issuance"
637575
Fields map[string]string `json:"fields,omitempty"`
638-
SerialNumber Bytes32Base64 `json:"serialNumber"`
576+
SerialNumber [32]byte `json:"serialNumber"`
639577
RevocationOutpoint *Outpoint `json:"revocationOutpoint,omitempty"`
640578
Signature BytesHex `json:"signature,omitempty"`
641579
CertifierUrl string `json:"certifierUrl,omitempty"`
@@ -645,35 +583,6 @@ type AcquireCertificateArgs struct {
645583
PrivilegedReason string `json:"privilegedReason,omitempty"`
646584
}
647585

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-
677586
// ListCertificatesArgs contains parameters for listing certificates with filtering and pagination.
678587
type ListCertificatesArgs struct {
679588
Certifiers []Bytes33Hex `json:"certifiers"`

wallet/substrates/http_wallet_json_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,8 +443,8 @@ func TestHTTPWalletJSON_SignatureOperations(t *testing.T) {
443443

444444
func TestHTTPWalletJSON_CertificateOperations(t *testing.T) {
445445
typeTest := tu.GetByte32FromString("test-type")
446-
serialNumber := wallet.Bytes32Base64(tu.GetByte32FromString("12345"))
447-
certifier := wallet.Bytes33Hex(tu.GetByte33FromString("test-certifier"))
446+
serialNumber := tu.GetByte32FromString("12345")
447+
certifier := tu.GetByte33FromString("test-certifier")
448448
verifier := tu.GetByte33FromString("test-verifier")
449449
// Test AcquireCertificate
450450
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)