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
12 changes: 9 additions & 3 deletions credential/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,25 @@ type VerifiableCredentialBuilder struct {

// NewVerifiableCredentialBuilder returns an initialized credential builder with some default fields populated
// Default id is empty
func NewVerifiableCredentialBuilder() VerifiableCredentialBuilder {
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: "",
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
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,9 +97,9 @@ func TestCredentialBuilder(t *testing.T) {
err = builder.AddContext("https://www.w3.org/2018/credentials/examples/v1")
assert.NoError(t, err)

//default id is empty
assert.Empty(t, builder.ID)

//default id is not empty
assert.NotEmpty(t, builder.ID)
// set id
id := "p"
err = builder.SetID(id)
Expand Down