Skip to content

Commit e0c5b1b

Browse files
chore: refactor and export functions
1 parent 30d8a92 commit e0c5b1b

File tree

23 files changed

+2286
-657
lines changed

23 files changed

+2286
-657
lines changed
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
package store
1+
package self
22

33
import (
44
"context"
5-
"self-sdk-go/internal/types"
5+
66
)
77

8+
// GetActionIdFunc is a function type for custom action ID generation
89
type GetActionIdFunc func(ctx context.Context, userIdentifier string, userDefinedData string) (string, error)
910

11+
// InMemoryConfigStore provides an in-memory implementation of ConfigStore with custom action ID logic
1012
type InMemoryConfigStore struct {
11-
configs map[string]types.VerificationConfig
13+
configs map[string]VerificationConfig
1214
getActionIdFunc GetActionIdFunc
1315
}
1416

@@ -18,28 +20,29 @@ var _ ConfigStore = (*InMemoryConfigStore)(nil)
1820
// NewInMemoryConfigStore creates a new instance of InMemoryConfigStore
1921
func NewInMemoryConfigStore(getActionIdFunc GetActionIdFunc) *InMemoryConfigStore {
2022
return &InMemoryConfigStore{
21-
configs: make(map[string]types.VerificationConfig),
23+
configs: make(map[string]VerificationConfig),
2224
getActionIdFunc: getActionIdFunc,
2325
}
2426
}
2527

28+
// GetActionId uses the custom function to generate action IDs
2629
func (store *InMemoryConfigStore) GetActionId(ctx context.Context, userIdentifier string, userDefinedData string) (string, error) {
2730
return store.getActionIdFunc(ctx, userIdentifier, userDefinedData)
2831
}
2932

3033
// SetConfig stores a configuration with the given ID
3134
// Returns true if the configuration was newly created, false if it was updated
32-
func (store *InMemoryConfigStore) SetConfig(ctx context.Context, id string, config types.VerificationConfig) (bool, error) {
35+
func (store *InMemoryConfigStore) SetConfig(ctx context.Context, id string, config VerificationConfig) (bool, error) {
3336
_, existed := store.configs[id]
3437
store.configs[id] = config
3538
return !existed, nil
3639
}
3740

3841
// GetConfig retrieves a configuration by ID
39-
func (store *InMemoryConfigStore) GetConfig(ctx context.Context, id string) (types.VerificationConfig, error) {
42+
func (store *InMemoryConfigStore) GetConfig(ctx context.Context, id string) (VerificationConfig, error) {
4043
config, exists := store.configs[id]
4144
if !exists {
42-
return types.VerificationConfig{}, nil
45+
return VerificationConfig{}, nil
4346
}
4447
return config, nil
4548
}

sdk/sdk-go/README.md

Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
# Self Protocol Go SDK
2+
3+
A Go SDK for integrating with the Self protocol for privacy-preserving identity verification using zero-knowledge proofs and passport/ID card attestations.
4+
5+
## Installation
6+
7+
```bash
8+
go get github.com/self/sdk
9+
```
10+
11+
## Quick Start
12+
13+
### Basic Usage
14+
15+
```go
16+
package main
17+
18+
import (
19+
"context"
20+
"fmt"
21+
"log"
22+
23+
"github.com/self/sdk"
24+
)
25+
26+
func main() {
27+
// Create a verification configuration
28+
config := self.VerificationConfig{
29+
MinimumAge: &[]int{18}[0], // Require 18+ years
30+
ExcludedCountries: []self.Country3LetterCode{"USA"}, // Exclude USA
31+
Ofac: &[]bool{false}[0], // Allow OFAC flagged individuals
32+
}
33+
34+
// Create a config store
35+
configStore := self.NewDefaultConfigStore(config)
36+
37+
// Define allowed attestation types
38+
allowedIds := map[self.AttestationId]bool{
39+
self.Passport: true,
40+
self.EUCard: true,
41+
}
42+
43+
// Initialize the verifier
44+
verifier, err := self.NewBackendVerifier(
45+
"my-app-scope", // Your application scope
46+
"https://my-app.com", // Your application endpoint
47+
false, // Use mainnet (true for testnet)
48+
allowedIds, // Allowed attestation types
49+
configStore, // Configuration storage
50+
self.UserIDTypeHex, // User identifier type
51+
)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
56+
// Verify a proof (these would come from your frontend)
57+
ctx := context.Background()
58+
result, err := verifier.Verify(
59+
ctx,
60+
"1", // Attestation ID (passport)
61+
proof, // Zero-knowledge proof from frontend
62+
publicSignals, // Public signals from frontend
63+
userContextData, // User context data from frontend
64+
)
65+
if err != nil {
66+
log.Printf("Verification failed: %v", err)
67+
return
68+
}
69+
70+
// Check verification results
71+
if result.IsValidDetails.IsValid {
72+
fmt.Printf("✅ Verification successful!\n")
73+
fmt.Printf("User ID: %s\n", result.UserData.UserIdentifier)
74+
fmt.Printf("Age verification: %v\n", result.IsValidDetails.IsMinimumAgeValid)
75+
fmt.Printf("OFAC verification: %v\n", result.IsValidDetails.IsOfacValid)
76+
fmt.Printf("Nationality: %s\n", result.DiscloseOutput.Nationality)
77+
} else {
78+
fmt.Printf("❌ Verification failed\n")
79+
}
80+
}
81+
```
82+
83+
## Configuration
84+
85+
### Verification Config
86+
87+
The `VerificationConfig` struct allows you to specify verification requirements:
88+
89+
```go
90+
type VerificationConfig struct {
91+
MinimumAge *int // Minimum age requirement (nil to disable)
92+
ExcludedCountries []common.Country3LetterCode // Countries to exclude
93+
Ofac *bool // OFAC compliance (nil to ignore)
94+
}
95+
```
96+
97+
### Config Storage
98+
99+
Implement the `ConfigStore` interface for custom configuration management:
100+
101+
```go
102+
type ConfigStore interface {
103+
GetConfig(ctx context.Context, id string) (VerificationConfig, error)
104+
SetConfig(ctx context.Context, id string, config VerificationConfig) (bool, error)
105+
GetActionId(ctx context.Context, userIdentifier string, actionId string) (string, error)
106+
}
107+
```
108+
109+
For simple use cases, use `DefaultConfigStore`:
110+
111+
```go
112+
configStore := self.NewDefaultConfigStore(config)
113+
```
114+
115+
For more complex scenarios, implement your own:
116+
117+
```go
118+
type DatabaseConfigStore struct {
119+
db *sql.DB
120+
}
121+
122+
func (d *DatabaseConfigStore) GetConfig(ctx context.Context, id string) (self.VerificationConfig, error) {
123+
// Your database logic here
124+
}
125+
126+
func (d *DatabaseConfigStore) SetConfig(ctx context.Context, id string, config self.VerificationConfig) (bool, error) {
127+
// Your database logic here
128+
}
129+
130+
func (d *DatabaseConfigStore) GetActionId(ctx context.Context, userIdentifier string, actionId string) (string, error) {
131+
// Your database logic here
132+
}
133+
```
134+
135+
## Attestation Types
136+
137+
The SDK supports two attestation types:
138+
139+
- `self.Passport`: Traditional passport verification
140+
- `self.EUCard`: European ID card verification
141+
142+
## Network Configuration
143+
144+
### Mainnet (Production)
145+
```go
146+
verifier, err := self.NewBackendVerifier(
147+
scope, endpoint, false, // mockPassport = false for mainnet
148+
allowedIds, configStore, userIdType,
149+
)
150+
```
151+
152+
### Testnet (Development)
153+
```go
154+
verifier, err := self.NewBackendVerifier(
155+
scope, endpoint, true, // mockPassport = true for testnet
156+
allowedIds, configStore, userIdType,
157+
)
158+
```
159+
160+
## User Identifier Types
161+
162+
Choose how user identifiers are formatted:
163+
164+
```go
165+
self.UserIDTypeHex // Hex format: 0x1234...
166+
self.UserIDTypeUUID // UUID format: 12345678-1234-1234-1234-123456789abc
167+
```
168+
169+
## Country Codes
170+
171+
Use 3-letter ISO country codes for exclusions:
172+
173+
```go
174+
import "github.com/self/sdk/common"
175+
176+
excludedCountries := []common.Country3LetterCode{
177+
common.USA, // United States
178+
common.RUS, // Russia
179+
common.CHN, // China
180+
}
181+
```
182+
183+
## Error Handling
184+
185+
The SDK provides detailed error information through `ConfigMismatchError`:
186+
187+
```go
188+
result, err := verifier.Verify(ctx, attestationId, proof, signals, contextData)
189+
if err != nil {
190+
if configErr, ok := err.(*self.ConfigMismatchError); ok {
191+
fmt.Println("Configuration issues:")
192+
for _, issue := range configErr.Issues {
193+
fmt.Printf("- %s: %s\n", issue.Type, issue.Message)
194+
}
195+
} else {
196+
fmt.Printf("Other error: %v\n", err)
197+
}
198+
return
199+
}
200+
```
201+
202+
## Verification Result
203+
204+
The `VerificationResult` contains comprehensive verification information:
205+
206+
```go
207+
type VerificationResult struct {
208+
AttestationId AttestationId // Type of attestation verified
209+
IsValidDetails IsValidDetails // Validation status details
210+
ForbiddenCountriesList []string // List of forbidden countries
211+
DiscloseOutput GenericDiscloseOutput // Disclosed identity information
212+
UserData UserData // User-specific data
213+
}
214+
215+
type IsValidDetails struct {
216+
IsValid bool // Overall proof validity
217+
IsMinimumAgeValid bool // Age requirement met
218+
IsOfacValid bool // OFAC compliance
219+
}
220+
221+
type GenericDiscloseOutput struct {
222+
Nullifier string // Unique nullifier for this proof
223+
IssuingState string // Country that issued the document
224+
Name string // Full name
225+
IdNumber string // Document ID number
226+
Nationality string // Nationality
227+
DateOfBirth string // Date of birth
228+
Gender string // Gender
229+
ExpiryDate string // Document expiry date
230+
MinimumAge string // Minimum age disclosed
231+
Ofac []bool // OFAC check results
232+
}
233+
```
234+
235+
## Examples
236+
237+
### Age Verification (18+)
238+
239+
```go
240+
config := self.VerificationConfig{
241+
MinimumAge: &[]int{18}[0],
242+
}
243+
```
244+
245+
### Country Restrictions
246+
247+
```go
248+
config := self.VerificationConfig{
249+
ExcludedCountries: []common.Country3LetterCode{
250+
common.USA,
251+
common.RUS,
252+
common.IRN,
253+
},
254+
}
255+
```
256+
257+
### OFAC Compliance
258+
259+
```go
260+
config := self.VerificationConfig{
261+
Ofac: &[]bool{true}[0], // Require OFAC compliance
262+
}
263+
```
264+
265+
### Combined Requirements
266+
267+
```go
268+
config := self.VerificationConfig{
269+
MinimumAge: &[]int{21}[0],
270+
ExcludedCountries: []common.Country3LetterCode{common.USA},
271+
Ofac: &[]bool{true}[0],
272+
}
273+
```
274+
275+
## Contributing
276+
277+
1. Fork the repository
278+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
279+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
280+
4. Push to the branch (`git push origin feature/amazing-feature`)
281+
5. Open a Pull Request
282+
283+
## License
284+
285+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
286+
287+
## Support
288+
289+
For support and questions:
290+
291+
- Create an issue in this repository
292+
- Check the [Self Protocol documentation](https://docs.self.id)
293+
- Join our [Discord community](https://discord.gg/worldcoin)

sdk/sdk-go/config.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package self
2+
3+
import (
4+
"context"
5+
)
6+
7+
// ConfigStore interface defines methods for storing and retrieving verification configurations
8+
type ConfigStore interface {
9+
// GetConfig retrieves a verification configuration by ID
10+
GetConfig(ctx context.Context, id string) (VerificationConfig, error)
11+
// SetConfig stores a verification configuration with the given ID
12+
SetConfig(ctx context.Context, id string, config VerificationConfig) (bool, error)
13+
// GetActionId retrieves the action ID for a given user identifier and user-defined data
14+
GetActionId(ctx context.Context, userIdentifier string, actionId string) (string, error)
15+
}
16+
17+
// DefaultConfigStore provides a simple in-memory implementation of ConfigStore
18+
type DefaultConfigStore struct {
19+
config VerificationConfig
20+
}
21+
22+
// NewDefaultConfigStore creates a new DefaultConfigStore with the given configuration
23+
func NewDefaultConfigStore(config VerificationConfig) *DefaultConfigStore {
24+
return &DefaultConfigStore{
25+
config: config,
26+
}
27+
}
28+
29+
// GetConfig returns the stored configuration
30+
func (store *DefaultConfigStore) GetConfig(ctx context.Context, id string) (VerificationConfig, error) {
31+
return store.config, nil
32+
}
33+
34+
// SetConfig updates the stored configuration
35+
func (store *DefaultConfigStore) SetConfig(ctx context.Context, id string, config VerificationConfig) (bool, error) {
36+
store.config = config
37+
return true, nil
38+
}
39+
40+
// GetActionId returns a default action ID
41+
func (store *DefaultConfigStore) GetActionId(ctx context.Context, userIdentifier string, userDefinedData string) (string, error) {
42+
return "random-id", nil
43+
}

0 commit comments

Comments
 (0)