|
| 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) |
0 commit comments