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

Commit

Permalink
feat: wallet controller - option to prove presentation
Browse files Browse the repository at this point in the history
- Part of #2770

Signed-off-by: sudesh.shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed May 4, 2021
1 parent eea5a2c commit 9edfd24
Show file tree
Hide file tree
Showing 6 changed files with 156 additions and 40 deletions.
12 changes: 6 additions & 6 deletions pkg/client/vcwallet/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1179,8 +1179,8 @@ func TestClient_Prove(t *testing.T) {
require.NoError(t, vcWalletClient.Add(wallet.DIDResolutionResponse, []byte(sampleDIDResolutionResponse)))

result, err := vcWalletClient.Prove(&wallet.ProofOptions{Controller: sampleDIDKey},
wallet.WithStoredCredentialsToPresent("http://example.edu/credentials/1872"),
wallet.WithRawCredentialsToPresent([]byte(sampleUDCVC)),
wallet.WithStoredCredentialsToProve("http://example.edu/credentials/1872"),
wallet.WithRawCredentialsToProve([]byte(sampleUDCVC)),
)
require.NoError(t, err)
require.NotEmpty(t, result)
Expand All @@ -1198,8 +1198,8 @@ func TestClient_Prove(t *testing.T) {
require.NoError(t, vcWalletClient.Add(wallet.Credential, []byte(sampleUDCVC)))

result, err := vcWalletClient.Prove(&wallet.ProofOptions{Controller: sampleDIDKey2},
wallet.WithStoredCredentialsToPresent("http://example.edu/credentials/1872"),
wallet.WithRawCredentialsToPresent([]byte(sampleUDCVC)),
wallet.WithStoredCredentialsToProve("http://example.edu/credentials/1872"),
wallet.WithRawCredentialsToProve([]byte(sampleUDCVC)),
)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to read json keyset from reader")
Expand All @@ -1219,8 +1219,8 @@ func TestClient_Prove(t *testing.T) {
vcWalletClient.Close()

result, err := vcWalletClient.Prove(&wallet.ProofOptions{Controller: sampleDIDKey},
wallet.WithStoredCredentialsToPresent("http://example.edu/credentials/1872"),
wallet.WithRawCredentialsToPresent([]byte(sampleUDCVC)),
wallet.WithStoredCredentialsToProve("http://example.edu/credentials/1872"),
wallet.WithRawCredentialsToProve([]byte(sampleUDCVC)),
)
require.Error(t, err)
require.True(t, errors.Is(err, ErrWalletLocked))
Expand Down
9 changes: 6 additions & 3 deletions pkg/controller/command/vcwallet/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,17 @@ func prepareProveOptions(rqst *ProveRequest) []wallet.ProveOptions {
var options []wallet.ProveOptions

if len(rqst.StoredCredentials) > 0 {
options = append(options, wallet.WithStoredCredentialsToPresent(rqst.StoredCredentials...))
options = append(options, wallet.WithStoredCredentialsToProve(rqst.StoredCredentials...))
}

if len(rqst.RawCredentials) > 0 {
options = append(options, wallet.WithRawCredentialsToPresent(rqst.RawCredentials...))
options = append(options, wallet.WithRawCredentialsToProve(rqst.RawCredentials...))
}

if len(rqst.Presentation) > emptyRawLength {
options = append(options, wallet.WithRawPresentationToProve(rqst.Presentation))
}

// TODO option to pass raw presentation #2433
return options
}

Expand Down
21 changes: 21 additions & 0 deletions pkg/controller/command/vcwallet/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,27 @@ func TestCommand_IssueProveVerify(t *testing.T) {

presentation = parsePresentation(t, b)
require.NotEmpty(t, presentation.Proofs)
require.Len(t, presentation.Credentials(), 2)
require.Len(t, presentation.Proofs, 1)
b.Reset()

// prove using raw presentation
rawPresentation, err := presentation.MarshalJSON()
require.NoError(t, err)

cmdErr = cmd.Prove(&b, getReader(t, &ProveRequest{
WalletAuth: WalletAuth{UserID: sampleUser1, Auth: token},
StoredCredentials: []string{"http://example.edu/credentials/1877"},
Presentation: rawPresentation,
ProofOptions: &wallet.ProofOptions{
Controller: sampleDIDKey,
},
}))
require.NoError(t, cmdErr)
presentation2 := parsePresentation(t, b)
require.NotEmpty(t, presentation2.Proofs)
require.Len(t, presentation2.Credentials(), 3)
require.Len(t, presentation2.Proofs, 2)
})

t.Run("verify a raw presentation", func(t *testing.T) {
Expand Down
28 changes: 20 additions & 8 deletions pkg/wallet/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,41 +149,53 @@ type proveOpts struct {
credentials []*verifiable.Credential
// presentation to be supplied to wallet to prove.
presentation *verifiable.Presentation
// rawPresentation to be supplied to wallet to prove.
rawPresentation json.RawMessage
}

// ProveOptions options for proving credential to present from wallet.
type ProveOptions func(opts *proveOpts)

// WithStoredCredentialsToPresent option for providing stored credential IDs for wallet to present.
func WithStoredCredentialsToPresent(ids ...string) ProveOptions {
// WithStoredCredentialsToProve option for providing stored credential IDs for wallet to present.
func WithStoredCredentialsToProve(ids ...string) ProveOptions {
return func(opts *proveOpts) {
opts.storedCredentials = ids
}
}

// WithRawCredentialsToPresent option for providing raw credential for wallet to present.
func WithRawCredentialsToPresent(raw ...json.RawMessage) ProveOptions {
// WithRawCredentialsToProve option for providing raw credential for wallet to present.
func WithRawCredentialsToProve(raw ...json.RawMessage) ProveOptions {
return func(opts *proveOpts) {
opts.rawCredentials = raw
}
}

// WithCredentialsToPresent option for providing verifiable credential instances for wallet to present.
func WithCredentialsToPresent(credentials ...*verifiable.Credential) ProveOptions {
// WithCredentialsToProve option for providing verifiable credential instances for wallet to present.
func WithCredentialsToProve(credentials ...*verifiable.Credential) ProveOptions {
return func(opts *proveOpts) {
opts.credentials = credentials
}
}

// WithPresentation option for providing presentation for wallet to present.
// WithPresentationToProve option for providing presentation for wallet to present.
// If passed along with other credentials options, response verifiable presentation will be normalized
// to include all the credentials.
func WithPresentation(presentation *verifiable.Presentation) ProveOptions {
func WithPresentationToProve(presentation *verifiable.Presentation) ProveOptions {
return func(opts *proveOpts) {
opts.presentation = presentation
}
}

// WithRawPresentationToProve option for providing raw presentation for wallet to present.
// Ignored if passed along with WithPresentationToProve option.
// If passed along with other credentials options, response verifiable presentation will be normalized
// to include all the credentials.
func WithRawPresentationToProve(presentation json.RawMessage) ProveOptions {
return func(opts *proveOpts) {
opts.rawPresentation = presentation
}
}

// verifyOpts contains options for verifying credentials.
type verifyOpts struct {
// ID of the credential to be verified from wallet.
Expand Down
14 changes: 13 additions & 1 deletion pkg/wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ const (

// miscellaneous constants.
const (
bbsContext = "https://w3id.org/security/bbs/v1"
bbsContext = "https://w3id.org/security/bbs/v1"
emptyRawLength = 4
)

// proof options.
Expand Down Expand Up @@ -504,6 +505,7 @@ func (c *Wallet) Derive(authToken string, credential CredentialToDerive, options
return derived, nil
}

//nolint: funlen,gocyclo
func (c *Wallet) resolveOptionsToPresent(auth string, credentials ...ProveOptions) (*verifiable.Presentation, error) {
var allCredentials []*verifiable.Credential

Expand Down Expand Up @@ -554,6 +556,16 @@ func (c *Wallet) resolveOptionsToPresent(auth string, credentials ...ProveOption
opts.presentation.AddCredentials(allCredentials...)

return opts.presentation, nil
} else if len(opts.rawPresentation) > emptyRawLength {
vp, err := verifiable.ParsePresentation(opts.rawPresentation, verifiable.WithPresDisabledProofCheck(),
verifiable.WithPresJSONLDDocumentLoader(c.jsonldDocumentLoader))
if err != nil {
return nil, err
}

vp.AddCredentials(allCredentials...)

return vp, nil
}

return verifiable.NewPresentation(verifiable.WithCredentials(allCredentials...))
Expand Down
Loading

0 comments on commit 9edfd24

Please sign in to comment.