123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// Credential represents the structure of a Verifiable Credential
|
|
type Credential struct {
|
|
Context []string `json:"@context"`
|
|
Type []string `json:"type"`
|
|
Issuer string `json:"issuer"`
|
|
IssuanceDate string `json:"issuanceDate"`
|
|
CredentialSubject map[string]interface{} `json:"credentialSubject"`
|
|
}
|
|
|
|
// createDID generates a new DID using walt.id's API
|
|
func createDID() (string, error) {
|
|
req, err := http.NewRequest("POST", "http://localhost:7000/did/create", nil)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
req.Header.Set("Authorization", "Bearer YOUR_API_KEY") // Replace with actual API key
|
|
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return "", fmt.Errorf("failed to create DID: status %d", resp.StatusCode)
|
|
}
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
var result map[string]string
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
return "", err
|
|
}
|
|
return result["did"], nil
|
|
}
|
|
|
|
func main() {
|
|
// Create issuer DID
|
|
issuerDID, err := createDID()
|
|
if err != nil {
|
|
fmt.Println("Error creating issuer DID:", err)
|
|
return
|
|
}
|
|
fmt.Println("Issuer DID:", issuerDID)
|
|
|
|
// Create subject DID
|
|
subjectDID, err := createDID()
|
|
if err != nil {
|
|
fmt.Println("Error creating subject DID:", err)
|
|
return
|
|
}
|
|
fmt.Println("Subject DID:", subjectDID)
|
|
|
|
// Define the credential
|
|
credential := Credential{
|
|
Context: []string{"https://www.w3.org/2018/credentials/v1"},
|
|
Type: []string{"VerifiableCredential", "FeedbackEligibility"},
|
|
Issuer: issuerDID,
|
|
IssuanceDate: time.Now().UTC().Format(time.RFC3339),
|
|
CredentialSubject: map[string]interface{}{
|
|
"id": subjectDID,
|
|
"eligible": true,
|
|
},
|
|
}
|
|
|
|
// Marshal the credential to JSON
|
|
jsonData, err := json.Marshal(credential)
|
|
if err != nil {
|
|
fmt.Println("Error marshaling JSON:", err)
|
|
return
|
|
}
|
|
|
|
// Create HTTP request to issue the credential
|
|
req, err := http.NewRequest("POST", "http://localhost:7000/issue", bytes.NewBuffer(jsonData))
|
|
if err != nil {
|
|
fmt.Println("Error creating request:", err)
|
|
return
|
|
}
|
|
|
|
// Set headers
|
|
req.Header.Set("Content-Type", "application/json")
|
|
req.Header.Set("Authorization", "Bearer YOUR_API_KEY") // Replace with actual API key
|
|
|
|
// Send the request
|
|
client := &http.Client{}
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
fmt.Println("Error sending request:", err)
|
|
return
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
// Check the response status code
|
|
if resp.StatusCode != http.StatusOK {
|
|
fmt.Println("Error: received status code", resp.StatusCode)
|
|
return
|
|
}
|
|
|
|
// Read and display the response
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
fmt.Println("Error reading response:", err)
|
|
return
|
|
}
|
|
|
|
fmt.Println("Issued Credential:", string(body))
|
|
}
|