Skip to content

Commit

Permalink
add explicit result of slot parsing for nonmerklized credential. (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
vmidyllic authored Aug 30, 2023
1 parent 72eb16c commit f30880b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
48 changes: 18 additions & 30 deletions json/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ func (s Parser) ParseClaim(ctx context.Context,

subjectID := credential.CredentialSubject["id"]

slots, err := s.parseSlots(mz, credential, credentialType)
slots, nonMerklized, err := s.parseSlots(mz, credential, credentialType)
if err != nil {
return nil, err
}

if slots.isZero() {
// if schema is for non merklized credential, root position must be set to none ('')
// otherwise default position for merklized position is index.
if !nonMerklized {
if opts.MerklizedRootPosition == verifiable.CredentialMerklizedRootPositionNone {
opts.MerklizedRootPosition = verifiable.CredentialMerklizedRootPositionIndex
}
Expand All @@ -81,13 +83,13 @@ func (s Parser) ParseClaim(ctx context.Context,
core.WithValueDataBytes(slots.ValueA, slots.ValueB),
core.WithRevocationNonce(opts.RevNonce),
core.WithVersion(opts.Version))

if opts.Updatable {
claim.SetFlagUpdatable(opts.Updatable)
}
if err != nil {
return nil, err
}
if opts.Updatable {
claim.SetFlagUpdatable(opts.Updatable)
}

if credential.Expiration != nil {
claim.SetExpirationDate(*credential.Expiration)
}
Expand Down Expand Up @@ -322,24 +324,10 @@ type parsedSlots struct {
ValueA, ValueB []byte
}

func (s parsedSlots) isZero() bool {
return isZero(s.IndexA) && isZero(s.IndexB) &&
isZero(s.ValueA) && isZero(s.ValueB)
}

func isZero[T ~byte](in []T) bool {
for _, v := range in {
if v != 0 {
return false
}
}
return true
}

// parseSlots converts payload to claim slots using provided schema
func (s Parser) parseSlots(mz *merklize.Merklizer,
credential verifiable.W3CCredential,
credentialType string) (parsedSlots, error) {
credentialType string) (parsedSlots, bool, error) {

slots := parsedSlots{
IndexA: make([]byte, 32),
Expand All @@ -352,40 +340,40 @@ func (s Parser) parseSlots(mz *merklize.Merklizer,
serAttr, err := getSerializationAttr(credential, jsonLDOpts,
credentialType)
if err != nil {
return slots, err
return slots, false, err
}

if serAttr == "" {
return slots, nil
return slots, false, nil
}

sPaths, err := parseSerializationAttr(serAttr)
if err != nil {
return slots, err
return slots, true, err
}

if sPaths.isEmpty() {
return slots, nil
return slots, true, nil
}

err = fillSlot(slots.IndexA, mz, sPaths.indexAPath)
if err != nil {
return slots, err
return slots, true, err
}
err = fillSlot(slots.IndexB, mz, sPaths.indexBPath)
if err != nil {
return slots, err
return slots, true, err
}
err = fillSlot(slots.ValueA, mz, sPaths.valueAPath)
if err != nil {
return slots, err
return slots, true, err
}
err = fillSlot(slots.ValueB, mz, sPaths.valueBPath)
if err != nil {
return slots, err
return slots, true, err
}

return slots, nil
return slots, true, nil
}

// convert from the slice of concrete type to the slice of interface{}
Expand Down
3 changes: 2 additions & 1 deletion json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ func TestParser_parseSlots(t *testing.T) {
require.NoError(t, err)

parser := Parser{}
slots, err := parser.parseSlots(mz, credential, credentialType)
slots, nonMerklized, err := parser.parseSlots(mz, credential, credentialType)
require.True(t, nonMerklized)
require.NoError(t, err)
require.NotEqual(t, nullSlot, slots.IndexA)
require.Equal(t, nullSlot, slots.IndexB)
Expand Down

0 comments on commit f30880b

Please sign in to comment.