Skip to content
This repository has been archived by the owner on Dec 12, 2024. It is now read-only.

Update ID rules in NewVerifiableCredentialBuilder #532

Merged
merged 9 commits into from
Jun 20, 2024
18 changes: 14 additions & 4 deletions credential/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package credential

import (
"fmt"
"net/url"
"reflect"

"github.com/google/uuid"
Expand Down Expand Up @@ -31,19 +32,26 @@ type VerifiableCredentialBuilder struct {
}

// NewVerifiableCredentialBuilder returns an initialized credential builder with some default fields populated
func NewVerifiableCredentialBuilder() VerifiableCredentialBuilder {
// Default id is empty
func NewVerifiableCredentialBuilder(emptyID ...bool) VerifiableCredentialBuilder {
Copy link
Member

@decentralgabe decentralgabe Jun 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what do you think about making this a bit more readabale?

we could change the build to accept a new type like IDvalue for example

type IDValue string

const EmptyIDValue IDValue = ""

func NewVerifiableCredentialBuilder(id IDValue) VerifiableCredentialBuilder {
    if id == EmptyIDValue {
        // generate ID
    }
    ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @decentralgabe, thanks for your inputs.
Please confirm if the behaviour is as follows:

NewVerifiableCredentialBuilder (EmptyIDValue)  -> creates vc with id as uuid
NewVerifiableCredentialBuilder  ("xxx") -> generates vc with id as "xxx"
vcb.SetID("") ->  sets ID as empty

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, good consideration.

Perhaps we should add....

EmptyIDValue -> set ID value to empty
GenerateIDValue -> generate a uuid
CustomIDValue("<your-id-here>") -> use custom ID

to be unambiguous?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @decentralgabe , I have added IDValue argument here to all three cases (empty ID/ generate ID/ custom ID)

contexts := []string{VerifiableCredentialsLinkedDataContext}
types := []string{VerifiableCredentialType}
return VerifiableCredentialBuilder{
id := uuid.NewString()
if len(emptyID)>0 && emptyID[0]{
id = ""
}
vcb := VerifiableCredentialBuilder{
contexts: contexts,
types: types,
VerifiableCredential: &VerifiableCredential{
ID: uuid.NewString(),
ID: id,
Context: contexts,
Type: types,
IssuanceDate: util.GetRFC3339Timestamp(),
},
}
return vcb

}

// Build attempts to turn a builder into a valid verifiable credential, doing some object model validation.
Expand Down Expand Up @@ -85,7 +93,9 @@ func (vcb *VerifiableCredentialBuilder) SetID(id string) error {
if vcb.IsEmpty() {
return errors.New(BuilderEmptyError)
}

if _, err := url.Parse(id); err != nil {
return errors.Wrap(err, "malformed id")
}
vcb.ID = id
return nil
}
Expand Down
12 changes: 8 additions & 4 deletions credential/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func TestCredential(t *testing.T) {

// Exercise all builder methods
func TestCredentialBuilder(t *testing.T) {
builder := NewVerifiableCredentialBuilder()

builder := NewVerifiableCredentialBuilder(true)
assert.Empty(t, builder.ID)

builder = NewVerifiableCredentialBuilder()
_, err := builder.Build()
assert.Error(t, err)
notReadyErr := "credential not ready to be built"
Expand All @@ -93,11 +97,11 @@ func TestCredentialBuilder(t *testing.T) {
err = builder.AddContext("https://www.w3.org/2018/credentials/examples/v1")
assert.NoError(t, err)

// there is a default id
//default id is not empty
assert.NotEmpty(t, builder.ID)

// set id
id := "test-id"
id := "p"
err = builder.SetID(id)
assert.NoError(t, err)

Expand Down
3 changes: 3 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opencensus.io v0.23.0 h1:gqCw0LfLxScz8irSi8exQc7fyQ0fKQU/qnC/X8+V/1M=
go.opencensus.io v0.23.0/go.mod h1:XItmlyltB5F7CS4xOC1DcqMoFqwtC6OG2xF7mCv7P7E=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
Expand Down Expand Up @@ -259,12 +260,14 @@ golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E=
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down