github.com/hyperledger/aries-framework-go@v0.3.2/pkg/wallet/options.go (about)

     1  /*
     2  Copyright SecureKey Technologies Inc. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package wallet
     8  
     9  import (
    10  	"encoding/json"
    11  	"time"
    12  
    13  	"github.com/hyperledger/aries-framework-go/component/storage/edv"
    14  	"github.com/hyperledger/aries-framework-go/pkg/client/outofband"
    15  	"github.com/hyperledger/aries-framework-go/pkg/doc/verifiable"
    16  	"github.com/hyperledger/aries-framework-go/pkg/kms/webkms"
    17  	"github.com/hyperledger/aries-framework-go/pkg/secretlock"
    18  )
    19  
    20  // profileOpts contains options for creating verifiable credential wallet.
    21  type profileOpts struct {
    22  	// local kms options
    23  	secretLockSvc secretlock.Service
    24  	passphrase    string
    25  
    26  	// remote(web) kms options
    27  	keyServerURL string
    28  
    29  	// EDV options
    30  	edvConf *edvConf
    31  }
    32  
    33  // ProfileOptions is option for verifiable credential wallet key manager.
    34  type ProfileOptions func(opts *profileOpts)
    35  
    36  // WithSecretLockService option, when provided then wallet will use local kms for key operations.
    37  func WithSecretLockService(svc secretlock.Service) ProfileOptions {
    38  	return func(opts *profileOpts) {
    39  		opts.secretLockSvc = svc
    40  	}
    41  }
    42  
    43  // WithPassphrase option to provide passphrase for local kms for key operations.
    44  func WithPassphrase(passphrase string) ProfileOptions {
    45  	return func(opts *profileOpts) {
    46  		opts.passphrase = passphrase
    47  	}
    48  }
    49  
    50  // WithKeyServerURL option, when provided then wallet will use remote kms for key operations.
    51  // This option will be ignore if provided with 'WithSecretLockService' option.
    52  func WithKeyServerURL(url string) ProfileOptions {
    53  	return func(opts *profileOpts) {
    54  		opts.keyServerURL = url
    55  	}
    56  }
    57  
    58  // WithEDVStorage option, for wallet profile to use EDV as storage.
    59  // If provided then all wallet contents will use EDV for storage.
    60  // Note: key manager options supplied for profile creation and management will be reused for EDV operations.
    61  func WithEDVStorage(url, vaultID, encryptionKID, macKID string) ProfileOptions {
    62  	return func(opts *profileOpts) {
    63  		opts.edvConf = &edvConf{
    64  			ServerURL:       url,
    65  			VaultID:         vaultID,
    66  			EncryptionKeyID: encryptionKID,
    67  			MACKeyID:        macKID,
    68  		}
    69  	}
    70  }
    71  
    72  // unlockOpts contains options for unlocking VC wallet client.
    73  type unlockOpts struct {
    74  	// local kms options
    75  	passphrase    string
    76  	secretLockSvc secretlock.Service
    77  
    78  	// remote(web) kms options
    79  	authToken  string
    80  	webkmsOpts []webkms.Opt
    81  
    82  	// expiry
    83  	tokenExpiry time.Duration
    84  
    85  	// edv opts
    86  	edvOpts []edv.RESTProviderOption
    87  }
    88  
    89  // UnlockOptions is option for unlocking verifiable credential wallet key manager.
    90  // Wallet unlocking instantiates KMS instance for wallet operations.
    91  // Type of key manager (local or remote) to be used will be decided based on options passed.
    92  // Note: unlock options should match key manager options set for given wallet profile.
    93  type UnlockOptions func(opts *unlockOpts)
    94  
    95  // WithUnlockByPassphrase option for supplying passphrase to open wallet.
    96  // This option takes precedence when provided along with other options.
    97  func WithUnlockByPassphrase(passphrase string) UnlockOptions {
    98  	return func(opts *unlockOpts) {
    99  		opts.passphrase = passphrase
   100  	}
   101  }
   102  
   103  // WithUnlockBySecretLockService option for supplying secret lock service to open wallet.
   104  // This option will be ignored when supplied with 'WithPassphrase' option.
   105  func WithUnlockBySecretLockService(svc secretlock.Service) UnlockOptions {
   106  	return func(opts *unlockOpts) {
   107  		opts.secretLockSvc = svc
   108  	}
   109  }
   110  
   111  // WithUnlockByAuthorizationToken option for supplying remote kms auth token to open wallet.
   112  // This option will be ignore when supplied with localkms options.
   113  func WithUnlockByAuthorizationToken(url string) UnlockOptions {
   114  	return func(opts *unlockOpts) {
   115  		opts.authToken = url
   116  	}
   117  }
   118  
   119  // WithUnlockExpiry time duration after which wallet key manager will be expired.
   120  // Wallet should be reopened by using 'client.Open()' once expired or a new instance needs to be created.
   121  func WithUnlockExpiry(tokenExpiry time.Duration) UnlockOptions {
   122  	return func(opts *unlockOpts) {
   123  		opts.tokenExpiry = tokenExpiry
   124  	}
   125  }
   126  
   127  // WithUnlockWebKMSOptions can be used to provide custom aries web kms options for unlocking wallet.
   128  // This option can be used to set web kms client http header function instead of using WithUnlockByAuthorizationToken.
   129  func WithUnlockWebKMSOptions(webkmsOpts ...webkms.Opt) UnlockOptions {
   130  	return func(opts *unlockOpts) {
   131  		opts.webkmsOpts = webkmsOpts
   132  	}
   133  }
   134  
   135  // WithUnlockEDVOptions can be used to provide custom aries edv options for unlocking wallet.
   136  // Provided options will be considered only if given wallet profile is using EDV configurations.
   137  func WithUnlockEDVOptions(edvOpts ...edv.RESTProviderOption) UnlockOptions {
   138  	return func(opts *unlockOpts) {
   139  		opts.edvOpts = edvOpts
   140  	}
   141  }
   142  
   143  // proveOpts contains options for proving credentials.
   144  type proveOpts struct {
   145  	// IDs of credentials already saved in wallet.
   146  	storedCredentials []string
   147  	// raw credentials to be supplied to wallet to prove.
   148  	rawCredentials []json.RawMessage
   149  	// verifiable credentials to be supplied to wallet to prove.
   150  	credentials []*verifiable.Credential
   151  	// presentation to be supplied to wallet to prove.
   152  	presentation *verifiable.Presentation
   153  	// rawPresentation to be supplied to wallet to prove.
   154  	rawPresentation json.RawMessage
   155  }
   156  
   157  // ProveOptions options for proving credential to present from wallet.
   158  type ProveOptions func(opts *proveOpts)
   159  
   160  // WithStoredCredentialsToProve option for providing stored credential IDs for wallet to present.
   161  func WithStoredCredentialsToProve(ids ...string) ProveOptions {
   162  	return func(opts *proveOpts) {
   163  		opts.storedCredentials = ids
   164  	}
   165  }
   166  
   167  // WithRawCredentialsToProve option for providing raw credential for wallet to present.
   168  func WithRawCredentialsToProve(raw ...json.RawMessage) ProveOptions {
   169  	return func(opts *proveOpts) {
   170  		opts.rawCredentials = raw
   171  	}
   172  }
   173  
   174  // WithCredentialsToProve option for providing verifiable credential instances for wallet to present.
   175  func WithCredentialsToProve(credentials ...*verifiable.Credential) ProveOptions {
   176  	return func(opts *proveOpts) {
   177  		opts.credentials = credentials
   178  	}
   179  }
   180  
   181  // WithPresentationToProve option for providing presentation for wallet to present.
   182  // If passed along with other credentials options, response verifiable presentation will be normalized
   183  // to include all the credentials.
   184  func WithPresentationToProve(presentation *verifiable.Presentation) ProveOptions {
   185  	return func(opts *proveOpts) {
   186  		opts.presentation = presentation
   187  	}
   188  }
   189  
   190  // WithRawPresentationToProve option for providing raw presentation for wallet to present.
   191  // Ignored if passed along with WithPresentationToProve option.
   192  // If passed along with other credentials options, response verifiable presentation will be normalized
   193  // to include all the credentials.
   194  func WithRawPresentationToProve(presentation json.RawMessage) ProveOptions {
   195  	return func(opts *proveOpts) {
   196  		opts.rawPresentation = presentation
   197  	}
   198  }
   199  
   200  // verifyOpts contains options for verifying credentials.
   201  type verifyOpts struct {
   202  	// ID of the credential to be verified from wallet.
   203  	credentialID string
   204  	// raw credentials to be verified from wallet.
   205  	rawCredential json.RawMessage
   206  	// raw presentation to be verified from wallet.
   207  	rawPresentation json.RawMessage
   208  }
   209  
   210  // VerificationOption options for verifying credential from wallet.
   211  type VerificationOption func(opts *verifyOpts)
   212  
   213  // WithStoredCredentialToVerify option for providing ID of the stored credential to be verified from wallet.
   214  func WithStoredCredentialToVerify(id string) VerificationOption {
   215  	return func(opts *verifyOpts) {
   216  		opts.credentialID = id
   217  	}
   218  }
   219  
   220  // WithRawCredentialToVerify option for providing raw credential to be verified from wallet.
   221  func WithRawCredentialToVerify(raw json.RawMessage) VerificationOption {
   222  	return func(opts *verifyOpts) {
   223  		opts.rawCredential = raw
   224  	}
   225  }
   226  
   227  // WithRawPresentationToVerify option for providing raw presentation to be verified from wallet.
   228  func WithRawPresentationToVerify(raw json.RawMessage) VerificationOption {
   229  	return func(opts *verifyOpts) {
   230  		opts.rawPresentation = raw
   231  	}
   232  }
   233  
   234  // verifyOpts contains options for deriving credentials.
   235  type deriveOpts struct {
   236  	// for deriving credential from stored credential.
   237  	credentialID string
   238  	// for deriving credential from raw credential.
   239  	rawCredential json.RawMessage
   240  	// for deriving credential from credential instance.
   241  	credential *verifiable.Credential
   242  }
   243  
   244  // CredentialToDerive is credential option for deriving a credential from wallet.
   245  type CredentialToDerive func(opts *deriveOpts)
   246  
   247  // FromStoredCredential for deriving credential from stored credential.
   248  func FromStoredCredential(id string) CredentialToDerive {
   249  	return func(opts *deriveOpts) {
   250  		opts.credentialID = id
   251  	}
   252  }
   253  
   254  // FromRawCredential for deriving credential from raw credential bytes.
   255  func FromRawCredential(raw json.RawMessage) CredentialToDerive {
   256  	return func(opts *deriveOpts) {
   257  		opts.rawCredential = raw
   258  	}
   259  }
   260  
   261  // FromCredential option for deriving credential from a credential instance.
   262  func FromCredential(cred *verifiable.Credential) CredentialToDerive {
   263  	return func(opts *deriveOpts) {
   264  		opts.credential = cred
   265  	}
   266  }
   267  
   268  // AddContentOptions is option for adding contents to wallet.
   269  type AddContentOptions func(opts *addContentOpts)
   270  
   271  // addContentOpts contains options for adding contents to wallet.
   272  type addContentOpts struct {
   273  	// ID of the collection to which the content belongs.
   274  	collectionID string
   275  
   276  	// indicated if the model of data saved into the wallet should be validated.
   277  	validateDataModel bool
   278  }
   279  
   280  // AddByCollection option for grouping wallet contents by collection ID.
   281  func AddByCollection(collectionID string) AddContentOptions {
   282  	return func(opts *addContentOpts) {
   283  		opts.collectionID = collectionID
   284  	}
   285  }
   286  
   287  // ValidateContent enables data model validations of adding content.
   288  func ValidateContent() AddContentOptions {
   289  	return func(opts *addContentOpts) {
   290  		opts.validateDataModel = true
   291  	}
   292  }
   293  
   294  // GetAllContentsOptions is option for getting all contents from wallet.
   295  type GetAllContentsOptions func(opts *getAllContentsOpts)
   296  
   297  // getAllContentsOpts contains options for getting all contents from wallet.
   298  type getAllContentsOpts struct {
   299  	// ID of the collection to filter get all results by collection.
   300  	collectionID string
   301  }
   302  
   303  // FilterByCollection option for getting all contents by collection from wallet.
   304  func FilterByCollection(collectionID string) GetAllContentsOptions {
   305  	return func(opts *getAllContentsOpts) {
   306  		opts.collectionID = collectionID
   307  	}
   308  }
   309  
   310  // connectOpts contains options for wallet's DIDComm connect features.
   311  type connectOpts struct {
   312  	outofband.EventOptions
   313  	// timeout duration to wait before waiting for status 'completed'.
   314  	timeout time.Duration
   315  }
   316  
   317  // ConnectOptions options for accepting incoming out-of-band invitation and connecting.
   318  type ConnectOptions func(opts *connectOpts)
   319  
   320  // WithMyLabel option for providing label to be shared with the other agent during the subsequent did-exchange.
   321  func WithMyLabel(label string) ConnectOptions {
   322  	return func(opts *connectOpts) {
   323  		opts.Label = label
   324  	}
   325  }
   326  
   327  // WithReuseAnyConnection option to use any recognized DID in the services array for a reusable connection.
   328  func WithReuseAnyConnection(reuse bool) ConnectOptions {
   329  	return func(opts *connectOpts) {
   330  		opts.ReuseAny = reuse
   331  	}
   332  }
   333  
   334  // WithReuseDID option to provide DID to be used when reusing a connection.
   335  func WithReuseDID(did string) ConnectOptions {
   336  	return func(opts *connectOpts) {
   337  		opts.ReuseDID = did
   338  	}
   339  }
   340  
   341  // WithRouterConnections option to provide for router connections to be used.
   342  func WithRouterConnections(conns ...string) ConnectOptions {
   343  	return func(opts *connectOpts) {
   344  		opts.Connections = conns
   345  	}
   346  }
   347  
   348  // WithConnectTimeout option providing connect timeout, to wait for connection status to be 'completed'.
   349  func WithConnectTimeout(timeout time.Duration) ConnectOptions {
   350  	return func(opts *connectOpts) {
   351  		opts.timeout = timeout
   352  	}
   353  }
   354  
   355  // getOobMessageOptions gets out-of-band message options to accept invitation from connect opts.
   356  func getOobMessageOptions(opts *connectOpts) []outofband.MessageOption {
   357  	var result []outofband.MessageOption
   358  
   359  	if len(opts.Connections) > 0 {
   360  		result = append(result, outofband.WithRouterConnections(opts.Connections...))
   361  	}
   362  
   363  	if opts.ReuseAny {
   364  		result = append(result, outofband.ReuseAnyConnection())
   365  	}
   366  
   367  	return append(result, outofband.ReuseConnection(opts.ReuseDID))
   368  }
   369  
   370  // initiateInteractionOpts contains options for proposing presentation/credential from wallet by
   371  // accepting out-of-band invitation from verifier/issuer.
   372  type initiateInteractionOpts struct {
   373  	// optional from DID option to customize message sender DID.
   374  	from string
   375  	// connect options.
   376  	connectOpts []ConnectOptions
   377  	// timeout duration to wait for response from invitee.
   378  	timeout time.Duration
   379  }
   380  
   381  // InitiateInteractionOption options for initiating credential interaction by proposing presentation/credential
   382  // from wallet.
   383  type InitiateInteractionOption func(opts *initiateInteractionOpts)
   384  
   385  // WithFromDID option for providing customized from DID for sending propose message.
   386  func WithFromDID(from string) InitiateInteractionOption {
   387  	return func(opts *initiateInteractionOpts) {
   388  		opts.from = from
   389  	}
   390  }
   391  
   392  // WithConnectOptions for customizing options for accepting invitation.
   393  func WithConnectOptions(options ...ConnectOptions) InitiateInteractionOption {
   394  	return func(opts *initiateInteractionOpts) {
   395  		opts.connectOpts = options
   396  	}
   397  }
   398  
   399  // WithInitiateTimeout to provide timeout duration to wait for response for propose message.
   400  func WithInitiateTimeout(timeout time.Duration) InitiateInteractionOption {
   401  	return func(opts *initiateInteractionOpts) {
   402  		opts.timeout = timeout
   403  	}
   404  }
   405  
   406  // concludeInteractionOpts contains options to conclude credential interaction by sending
   407  // present proof or request credential message from wallet.
   408  type concludeInteractionOpts struct {
   409  	// presenting proof or requesting credential from raw credential.
   410  	rawPresentation json.RawMessage
   411  	// presenting proof or requesting credential from verifiable presentation instance.
   412  	// this option takes precedence when provided with other options.
   413  	presentation *verifiable.Presentation
   414  	// if provided then wallet will wait till it gets acknowledgement or problem report from other party.
   415  	waitForDone bool
   416  	// time duration to wait for status to be done or abanoned.
   417  	timeout time.Duration
   418  }
   419  
   420  // ConcludeInteractionOptions is option to conclude credential interaction between wallet and verifier/issuer by sending
   421  // present proof or request credential message.
   422  type ConcludeInteractionOptions func(opts *concludeInteractionOpts)
   423  
   424  // FromPresentation for sending aries verifiable presentation as message attachment.
   425  func FromPresentation(presentation *verifiable.Presentation) ConcludeInteractionOptions {
   426  	return func(opts *concludeInteractionOpts) {
   427  		opts.presentation = presentation
   428  	}
   429  }
   430  
   431  // FromRawPresentation for sending raw JSON as presentation as message attachment.
   432  func FromRawPresentation(raw json.RawMessage) ConcludeInteractionOptions {
   433  	return func(opts *concludeInteractionOpts) {
   434  		opts.rawPresentation = raw
   435  	}
   436  }
   437  
   438  // WaitForDone if provided then wallet will wait for credential interaction protocol status to be
   439  // done or abandoned till given timeout. If used then wallet will wait for acknowledgement or problem report
   440  // from other party and also will return web redirect info if found in incoming message.
   441  // If timeout is zero then wallet will use its default timeout.
   442  func WaitForDone(timeout time.Duration) ConcludeInteractionOptions {
   443  	return func(opts *concludeInteractionOpts) {
   444  		opts.waitForDone = true
   445  
   446  		if timeout <= 0 {
   447  			opts.timeout = defaultWaitForPresentProofDone
   448  		} else {
   449  			opts.timeout = timeout
   450  		}
   451  	}
   452  }
   453  
   454  // resolveManifestOpts contains option to resolve credential manifest.
   455  type resolveManifestOpts struct {
   456  	response      *verifiable.Presentation
   457  	rawResponse   json.RawMessage
   458  	descriptorID  string
   459  	credentialID  string
   460  	credential    *verifiable.Credential
   461  	rawCredential json.RawMessage
   462  }
   463  
   464  // ResolveManifestOption is option to resolve credential manifests.
   465  type ResolveManifestOption func(opts *resolveManifestOpts)
   466  
   467  // ResolveResponse options for resolving credential response presentation.
   468  func ResolveResponse(response *verifiable.Presentation) ResolveManifestOption {
   469  	return func(opts *resolveManifestOpts) {
   470  		opts.response = response
   471  	}
   472  }
   473  
   474  // ResolveRawResponse options for resolving raw bytes of credential response presentation.
   475  func ResolveRawResponse(response json.RawMessage) ResolveManifestOption {
   476  	return func(opts *resolveManifestOpts) {
   477  		opts.rawResponse = response
   478  	}
   479  }
   480  
   481  // ResolveCredential options for resolving credential by given descriptor ID.
   482  func ResolveCredential(descriptorID string, credential *verifiable.Credential) ResolveManifestOption {
   483  	return func(opts *resolveManifestOpts) {
   484  		opts.descriptorID = descriptorID
   485  		opts.credential = credential
   486  	}
   487  }
   488  
   489  // ResolveRawCredential options for resolving raw bytes of credential by given descriptor ID.
   490  func ResolveRawCredential(descriptorID string, rawCredential json.RawMessage) ResolveManifestOption {
   491  	return func(opts *resolveManifestOpts) {
   492  		opts.descriptorID = descriptorID
   493  		opts.rawCredential = rawCredential
   494  	}
   495  }
   496  
   497  // ResolveCredentialID options for resolving credential from wallet content store by given descriptor ID.
   498  func ResolveCredentialID(descriptorID, credentialID string) ResolveManifestOption {
   499  	return func(opts *resolveManifestOpts) {
   500  		opts.descriptorID = descriptorID
   501  		opts.credentialID = credentialID
   502  	}
   503  }