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
8 changes: 6 additions & 2 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,14 +32,15 @@ type VerifiableCredentialBuilder struct {
}

// NewVerifiableCredentialBuilder returns an initialized credential builder with some default fields populated
// Default id is empty
func NewVerifiableCredentialBuilder() VerifiableCredentialBuilder {
contexts := []string{VerifiableCredentialsLinkedDataContext}
types := []string{VerifiableCredentialType}
return VerifiableCredentialBuilder{
contexts: contexts,
types: types,
VerifiableCredential: &VerifiableCredential{
ID: uuid.NewString(),
ID: "",
Copy link
Member

Choose a reason for hiding this comment

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

🤔 given that this is a required property I'm not sure it's a good idea to leave this empty

perhaps it should be added as an optional property to the builder, and if that is empty we generate it automatically?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, it's better to keep an option argument. I'll make the changes, thanks!

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 an optional argument to keep ID empty. Kindly review.

Context: contexts,
Type: types,
IssuanceDate: util.GetRFC3339Timestamp(),
Expand Down Expand Up @@ -85,7 +87,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
6 changes: 3 additions & 3 deletions credential/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,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
assert.NotEmpty(t, builder.ID)
//default id is empty
assert.Empty(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