Skip to content

Commit

Permalink
Setters and tests for evidence (#151)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomdaffurn authored Jun 17, 2024
1 parent 78aa51c commit 26cec7b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
13 changes: 11 additions & 2 deletions vc/vc.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type DataModel[T CredentialSubject] struct {

// Evidence represents the evidence property of a Verifiable Credential.
type Evidence struct {
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
ID string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
// todo is `AdditionalFields` the right name?
AdditionalFields map[string]interface{}
}
Expand Down Expand Up @@ -93,6 +93,7 @@ type createOptions struct {
issuanceDate time.Time
expirationDate time.Time
schemas []CredentialSchema
evidence []Evidence
}

// CreateOption is the return type of all Option functions that can be passed to [Create]
Expand Down Expand Up @@ -157,6 +158,13 @@ func ExpirationDate(expirationDate time.Time) CreateOption {
}
}

// Evidences can be used to set the evidence array of the Verifiable Credential created by [Create]
func Evidences(evidence ...Evidence) CreateOption {
return func(o *createOptions) {
o.evidence = evidence
}
}

// Create returns a new Verifiable Credential with the provided claims and options.
// if no options are provided, the following defaults will be used:
// - ID: urn:vc:uuid:<uuid>
Expand Down Expand Up @@ -186,6 +194,7 @@ func Create[T CredentialSubject](claims T, opts ...CreateOption) DataModel[T] {
ID: o.id,
IssuanceDate: o.issuanceDate.UTC().Format(time.RFC3339),
CredentialSubject: claims,
Evidence: o.evidence,
}

if len(o.schemas) > 0 {
Expand Down
13 changes: 13 additions & 0 deletions vc/vc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func TestCreate_Options(t *testing.T) {
vc.IssuanceDate(issuanceDate),
vc.ExpirationDate(expirationDate),
vc.Schemas("https://example.org/examples/degree.json"),
vc.Evidences(vc.Evidence{
ID: "evidenceID",
Type: "Insufficient",
AdditionalFields: map[string]interface{}{
"kind": "circumstantial",
"checks": []string{"motive", "cell_tower_logs"},
},
}),
)

assert.Equal(t, 2, len(cred.Context))
Expand All @@ -60,6 +68,11 @@ func TestCreate_Options(t *testing.T) {
assert.Equal(t, "https://example.org/examples/degree.json", cred.CredentialSchema[0].ID)
assert.Equal(t, "JsonSchema", cred.CredentialSchema[0].Type)

assert.Equal(t, 1, len(cred.Evidence))
assert.Equal(t, "evidenceID", cred.Evidence[0].ID)
assert.Equal(t, "Insufficient", cred.Evidence[0].Type)
assert.Equal(t, "circumstantial", cred.Evidence[0].AdditionalFields["kind"])
assert.Equal(t, []string{"motive", "cell_tower_logs"}, cred.Evidence[0].AdditionalFields["checks"].([]string)) // nolint:forcetypeassert
}

func TestSign(t *testing.T) {
Expand Down

0 comments on commit 26cec7b

Please sign in to comment.