Skip to content

Commit a299e14

Browse files
committed
Create wallet types for fixed size byte arrays. Update interface json marshalling.
1 parent 7bf40e7 commit a299e14

29 files changed

+623
-331
lines changed

auth/clients/authhttp/authhttp_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func TestNew(t *testing.T) {
5353
mockWallet := wallet.NewMockWallet(t)
5454
mockSessionManager := NewMockSessionManager()
5555
requestedCerts := &utils.RequestedCertificateSet{
56-
Certifiers: []wallet.Bytes33Hex{},
56+
Certifiers: []wallet.PubKey{},
5757
CertificateTypes: make(utils.RequestedCertificateTypeIDAndFieldList),
5858
}
5959

@@ -74,7 +74,7 @@ func TestNewWithNilSessionManager(t *testing.T) {
7474
// Set up dependencies
7575
mockWallet := wallet.NewMockWallet(t)
7676
requestedCerts := &utils.RequestedCertificateSet{
77-
Certifiers: []wallet.Bytes33Hex{},
77+
Certifiers: []wallet.PubKey{},
7878
CertificateTypes: make(utils.RequestedCertificateTypeIDAndFieldList),
7979
}
8080

auth/peer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func NewPeer(cfg *PeerOptions) *Peer {
103103
peer.CertificatesToRequest = cfg.CertificatesToRequest
104104
} else {
105105
peer.CertificatesToRequest = &utils.RequestedCertificateSet{
106-
Certifiers: []wallet.Bytes33Hex{},
106+
Certifiers: []wallet.PubKey{},
107107
CertificateTypes: make(utils.RequestedCertificateTypeIDAndFieldList),
108108
}
109109
}

auth/peer_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -621,14 +621,14 @@ func TestPeerCertificateExchange(t *testing.T) {
621621

622622
// Set certificate requirements - We need to use the RAW type string here, not base64 encoded
623623
aliceCertReqs := &utils.RequestedCertificateSet{
624-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")}, // "any" is special value that accepts any certifier
624+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")}, // "any" is special value that accepts any certifier
625625
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
626626
certType: []string{requiredField},
627627
},
628628
}
629629

630630
bobCertReqs := &utils.RequestedCertificateSet{
631-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")}, // "any" is special value that accepts any certifier
631+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")}, // "any" is special value that accepts any certifier
632632
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
633633
certType: []string{requiredField},
634634
},
@@ -1152,7 +1152,7 @@ func TestPartialCertificateAcceptance(t *testing.T) {
11521152

11531153
// Setup certificate requirements - requesting two fields but accepting partial matches
11541154
requestedCertificates := &utils.RequestedCertificateSet{
1155-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
1155+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")},
11561156
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
11571157
certType: []string{"name", "email"},
11581158
},
@@ -1420,7 +1420,7 @@ func TestLibraryCardVerification(t *testing.T) {
14201420

14211421
// Setup certificate requirements - Alice requires Bob's library card number
14221422
alice.CertificatesToRequest = &utils.RequestedCertificateSet{
1423-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
1423+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")},
14241424
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
14251425
certType: []string{"cardNumber"},
14261426
},
@@ -1441,7 +1441,7 @@ func TestLibraryCardVerification(t *testing.T) {
14411441

14421442
// Alice explicitly requests Bob's certificate
14431443
err = alice.RequestCertificates(ctx, bobPubKey.PublicKey, utils.RequestedCertificateSet{
1444-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
1444+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")},
14451445
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
14461446
certType: []string{"cardNumber"},
14471447
},
@@ -1714,14 +1714,14 @@ func TestNonmatchingCertificateRejection(t *testing.T) {
17141714

17151715
// Create peers with different certificate requirements
17161716
aliceRequiredCerts := utils.RequestedCertificateSet{
1717-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
1717+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")},
17181718
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
17191719
certTypeA: []string{"name"}, // Alice only accepts partnerA certs
17201720
},
17211721
}
17221722

17231723
bobRequiredCerts := utils.RequestedCertificateSet{
1724-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
1724+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("any")},
17251725
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
17261726
certTypeB: []string{"name"}, // Bob only accepts partnerB certs
17271727
},

auth/types.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/base64"
77
"encoding/json"
88
"fmt"
9+
"slices"
910
"sync"
1011

1112
"github.com/bsv-blockchain/go-sdk/auth/certificates"
@@ -108,8 +109,9 @@ func ValidateCertificates(
108109
types := certificatesRequested.CertificateTypes
109110

110111
// Check certifier matches
111-
certifierKey := cert.Certifier.ToDER()
112-
if !wallet.BytesInHex33Slice(certifiers, certifierKey) {
112+
var certifierKey wallet.PubKey
113+
copy(certifierKey[:], cert.Certifier.ToDER())
114+
if !slices.Contains(certifiers, certifierKey) {
113115
errCh <- fmt.Errorf(
114116
"certificate with serial number %s has an unrequested certifier: %x",
115117
cert.SerialNumber,

auth/utils/get_verifiable_certificates.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func GetVerifiableCertificates(ctx context.Context, options *GetVerifiableCertif
3939
var result []*certificates.VerifiableCertificate
4040

4141
// Get all certificate types
42-
var certificateTypes []wallet.Bytes32Base64
42+
var certificateTypes []wallet.CertificateType
4343
for certType := range options.RequestedCertificates.CertificateTypes {
4444
certificateTypes = append(certificateTypes, certType)
4545
}

auth/utils/get_verifiable_certificates_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestGetVerifiableCertificates(t *testing.T) {
4444
keyring2Base64 := base64.StdEncoding.EncodeToString([]byte("key2"))
4545

4646
requestedCerts := &RequestedCertificateSet{
47-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1"), tu.GetByte33FromString("certifier2")},
47+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1"), tu.GetByte33FromString("certifier2")},
4848
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
4949
certType1: {"field1", "field2"},
5050
certType2: {"field3"},
@@ -113,7 +113,7 @@ func TestGetVerifiableCertificates(t *testing.T) {
113113
mockWallet := wallet.NewMockWallet(t)
114114

115115
requestedCerts := &RequestedCertificateSet{
116-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1")},
116+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1")},
117117
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
118118
certType1: {"field1"},
119119
},
@@ -143,7 +143,7 @@ func TestGetVerifiableCertificates(t *testing.T) {
143143
mockWallet := wallet.NewMockWallet(t)
144144

145145
requestedCerts := &RequestedCertificateSet{
146-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1")},
146+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1")},
147147
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
148148
certType1: {"field1"},
149149
},

auth/utils/validate_certificates.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"encoding/json"
77
"errors"
88
"fmt"
9+
"slices"
910

1011
"github.com/bsv-blockchain/go-sdk/auth/certificates"
1112
ec "github.com/bsv-blockchain/go-sdk/primitives/ec"
@@ -19,7 +20,7 @@ var (
1920
)
2021

2122
// RequestedCertificateTypeIDAndFieldList maps certificate type IDs to required fields
22-
type RequestedCertificateTypeIDAndFieldList map[wallet.Bytes32Base64][]string
23+
type RequestedCertificateTypeIDAndFieldList map[wallet.CertificateType][]string
2324

2425
func (m RequestedCertificateTypeIDAndFieldList) MarshalJSON() ([]byte, error) {
2526
tmp := make(map[string][]string)
@@ -44,7 +45,7 @@ func (m *RequestedCertificateTypeIDAndFieldList) UnmarshalJSON(data []byte) erro
4445
if len(decoded) != 32 {
4546
return fmt.Errorf("expected 32 bytes, got %d", len(decoded))
4647
}
47-
var key wallet.Bytes32Base64
48+
var key wallet.CertificateType
4849
copy(key[:], decoded)
4950
result[key] = v
5051
}
@@ -55,7 +56,7 @@ func (m *RequestedCertificateTypeIDAndFieldList) UnmarshalJSON(data []byte) erro
5556
// RequestedCertificateSet represents a set of requested certificates
5657
type RequestedCertificateSet struct {
5758
// Array of public keys that must have signed the certificates
58-
Certifiers []wallet.Bytes33Hex
59+
Certifiers []wallet.PubKey
5960

6061
// Map of certificate type IDs to field names that must be included
6162
CertificateTypes RequestedCertificateTypeIDAndFieldList
@@ -113,8 +114,9 @@ func ValidateCertificates(
113114
if certificatesRequested != nil {
114115
// Check certifier matches
115116
if !isEmptyPublicKey(cert.Certifier) {
116-
certifierKey := cert.Certifier.ToDER()
117-
if !wallet.BytesInHex33Slice(certificatesRequested.Certifiers, certifierKey) {
117+
var certifierKey wallet.PubKey
118+
copy(certifierKey[:], cert.Certifier.ToDER())
119+
if !slices.Contains(certificatesRequested.Certifiers, certifierKey) {
118120
errCh <- fmt.Errorf("certificate with serial number %s has an unrequested certifier: %x",
119121
cert.SerialNumber, certifierKey)
120122
return

auth/utils/validate_certificates_test.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package utils
22

33
import (
44
"context"
5+
"slices"
56
"testing"
67

78
"github.com/bsv-blockchain/go-sdk/auth/certificates"
@@ -37,6 +38,8 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
3738
require.NoError(t, err)
3839
differentSubjectKey := differentSubject.PubKey()
3940

41+
anyCertifier := wallet.PubKey(tu.GetByte33FromString("any"))
42+
4043
var requestedType [32]byte
4144
copy(requestedType[:], "requested_type")
4245
var anotherType [32]byte
@@ -92,34 +95,36 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
9295
t.Run("throws an error for unrequested certifier", func(t *testing.T) {
9396
// Create certificate request with different certifier
9497
certificatesRequested := &RequestedCertificateSet{
95-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("another_certifier")}, // Different from certifierHex
98+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("another_certifier")}, // Different from certifierHex
9699
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
97100
requestedType: []string{"field1"},
98101
},
99102
}
100103

101104
// Check certifier match logic
102-
assert.False(t, wallet.BytesInHex33Slice(certificatesRequested.Certifiers, validCertifierKey.ToDER()))
105+
var certifierKey wallet.PubKey
106+
copy(certifierKey[:], validCertifierKey.ToDER())
107+
assert.False(t, slices.Contains(certificatesRequested.Certifiers, certifierKey))
103108
// The logic in ValidateCertificates would have raised an error here
104109
})
105110

106111
t.Run("accepts 'any' as a certifier match", func(t *testing.T) {
107112
// Create certificate request with "any" certifier
108113
certificatesRequested := &RequestedCertificateSet{
109-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
114+
Certifiers: []wallet.PubKey{anyCertifier},
110115
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
111116
requestedType: []string{"field1"},
112117
},
113118
}
114119

115120
// "any" should match any certifier value
116-
assert.True(t, wallet.BytesInHex33Slice(certificatesRequested.Certifiers, []byte("any")))
121+
assert.True(t, slices.Contains(certificatesRequested.Certifiers, anyCertifier))
117122
})
118123

119124
t.Run("throws an error for unrequested certificate type", func(t *testing.T) {
120125
// Create certificate request with different type
121126
certificatesRequested := &RequestedCertificateSet{
122-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("any")},
127+
Certifiers: []wallet.PubKey{anyCertifier},
123128
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
124129
anotherType: []string{"field1"}, // Different from "requested_type"
125130
},
@@ -133,7 +138,7 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
133138
t.Run("validate certificates request set validation", func(t *testing.T) {
134139
// Test empty certifiers
135140
req := &RequestedCertificateSet{
136-
Certifiers: []wallet.Bytes33Hex{},
141+
Certifiers: []wallet.PubKey{},
137142
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
138143
type1: []string{"field1"},
139144
},
@@ -144,7 +149,7 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
144149

145150
// Test empty types
146151
req = &RequestedCertificateSet{
147-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1")},
152+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1")},
148153
CertificateTypes: RequestedCertificateTypeIDAndFieldList{},
149154
}
150155
err = ValidateRequestedCertificateSet(req)
@@ -153,7 +158,7 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
153158

154159
// Test empty type name
155160
req = &RequestedCertificateSet{
156-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1")},
161+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1")},
157162
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
158163
[32]byte{}: []string{"field1"},
159164
},
@@ -164,7 +169,7 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
164169

165170
// Test empty fields
166171
req = &RequestedCertificateSet{
167-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1")},
172+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1")},
168173
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
169174
type1: []string{},
170175
},
@@ -175,7 +180,7 @@ func TestValidateCertificatesFunctionality(t *testing.T) {
175180

176181
// Test valid request
177182
req = &RequestedCertificateSet{
178-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("certifier1")},
183+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("certifier1")},
179184
CertificateTypes: RequestedCertificateTypeIDAndFieldList{
180185
type1: []string{"field1"},
181186
},

auth/validate_certificates_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func TestValidateCertificates(t *testing.T) {
3030
})
3131

3232
t.Run("Validates certificate requirements structure", func(t *testing.T) {
33-
var certType wallet.Bytes32Base64
33+
var certType wallet.CertificateType
3434
copy(certType[:], "requested_type")
3535
// Test validate certificate requirements struct
3636
reqs := &utils.RequestedCertificateSet{
37-
Certifiers: []wallet.Bytes33Hex{tu.GetByte33FromString("valid_certifier")},
37+
Certifiers: []wallet.PubKey{tu.GetByte33FromString("valid_certifier")},
3838
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
3939
certType: {"field1"},
4040
},
@@ -142,12 +142,12 @@ func TestValidateCertificates(t *testing.T) {
142142
copy(certifierHex[:], certifierIdentityKey.PublicKey.Compressed())
143143

144144
// Convert masterCert.Type from StringBase64 to Base64Bytes32
145-
var certType32 wallet.Bytes32Base64
145+
var certType32 wallet.CertificateType
146146
typeBytes, _ := base64.StdEncoding.DecodeString(string(masterCert.Type))
147147
copy(certType32[:], typeBytes)
148148

149149
certReqs := &utils.RequestedCertificateSet{
150-
Certifiers: []wallet.Bytes33Hex{certifierHex},
150+
Certifiers: []wallet.PubKey{certifierHex},
151151
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
152152
certType32: []string{"name", "email"},
153153
},
@@ -236,12 +236,12 @@ func TestValidateCertificates(t *testing.T) {
236236
copy(subjectHex[:], subjectIdentityKey.PublicKey.Compressed())
237237

238238
// Convert certTypeBase64 from string to Base64Bytes32
239-
var certType32 wallet.Bytes32Base64
239+
var certType32 wallet.CertificateType
240240
typeBytes, _ := base64.StdEncoding.DecodeString(certTypeBase64)
241241
copy(certType32[:], typeBytes)
242242

243243
certReqs := &utils.RequestedCertificateSet{
244-
Certifiers: []wallet.Bytes33Hex{subjectHex},
244+
Certifiers: []wallet.PubKey{subjectHex},
245245
CertificateTypes: utils.RequestedCertificateTypeIDAndFieldList{
246246
certType32: []string{"owner"},
247247
},

identity/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,18 @@ func (c *Client) PubliclyRevealAttributes(
124124
return nil, nil, fmt.Errorf("failed to prove certificate: %w", err)
125125
}
126126

127+
var revocationOutpointString string
128+
if certificate.RevocationOutpoint != nil {
129+
revocationOutpointString = certificate.RevocationOutpoint.String()
130+
}
131+
127132
// Create a JSON object with certificate and keyring
128133
certWithKeyring := map[string]interface{}{
129134
"type": certificate.Type,
130135
"serialNumber": certificate.SerialNumber,
131136
"subject": certificate.Subject.Compressed(),
132137
"certifier": certificate.Certifier.Compressed(),
133-
"revocationOutpoint": certificate.RevocationOutpointString(),
138+
"revocationOutpoint": revocationOutpointString,
134139
"fields": certificate.Fields,
135140
"signature": hex.EncodeToString(certificate.Signature),
136141
"keyring": proveResult.KeyringForVerifier,
@@ -353,7 +358,7 @@ func (c *Client) parseIdentity(identity *wallet.IdentityCertificate) Displayable
353358
badgeClickURL = DefaultIdentity.BadgeClickURL
354359
}
355360

356-
var typeUnknown wallet.Bytes32Base64
361+
var typeUnknown wallet.CertificateType
357362
copy(typeUnknown[:], "unknownType")
358363

359364
// Create abbreviated key for display

0 commit comments

Comments
 (0)