Skip to content

Commit

Permalink
Upgrade github.com/iden3/go-iden3-core to v2 and bump self version to…
Browse files Browse the repository at this point in the history
… v2 (#42)

* Upgrade github.com/iden3/go-iden3-core to v2 and bump self version to v2

* Upgrade go-iden3-core, go-circuits, go-jwz, iden3comm and go-schema-processor to latest versions

* add supported operations for xsd double type (#55)

* support non-merklized credentials (#60)

---------

Co-authored-by: vmidyllic <[email protected]>
Co-authored-by: Ilya <[email protected]>
  • Loading branch information
3 people authored Aug 29, 2023
1 parent 8513869 commit 51cbe86
Show file tree
Hide file tree
Showing 26 changed files with 1,133 additions and 711 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/ci-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ on:
- main
- develop
pull_request:
branches:
- main
- develop
jobs:
test:
strategy:
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
>

`go get github.com/iden3/go-iden3-auth`
`go get github.com/iden3/go-iden3-auth/v2`

### General description:

Expand Down Expand Up @@ -62,7 +62,7 @@ The blockchain verification algorithm is used
1. A non-empty GIST is returned, equal to the GIST is provided by the user, it means the user is using the latest state.
2. The non-empty GIST is returned and it’s not equal to the GIST is provided by a user. Gets the transition time of the GIST. The verification party can make a decision if it can accept this state based on that time frame.
## How to use:
1. `go get https://github.com/iden3/go-iden3-auth`
1. `go get https://github.com/iden3/go-iden3-auth/v2`
2. Request generation:

basic auth:
Expand Down
138 changes: 97 additions & 41 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,33 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
"github.com/google/uuid"
"github.com/iden3/contracts-abi/state/go/abi"
"github.com/iden3/go-circuits"
"github.com/iden3/go-iden3-auth/loaders"
"github.com/iden3/go-iden3-auth/proofs"
"github.com/iden3/go-iden3-auth/pubsignals"
"github.com/iden3/go-iden3-auth/state"
"github.com/iden3/go-jwz"
"github.com/iden3/go-schema-processor/merklize"
"github.com/iden3/go-schema-processor/verifiable"
"github.com/iden3/iden3comm"
"github.com/iden3/iden3comm/packers"
"github.com/iden3/iden3comm/protocol"
"github.com/iden3/go-circuits/v2"
"github.com/iden3/go-iden3-auth/v2/loaders"
"github.com/iden3/go-iden3-auth/v2/proofs"
"github.com/iden3/go-iden3-auth/v2/pubsignals"
"github.com/iden3/go-iden3-auth/v2/state"
"github.com/iden3/go-jwz/v2"
schemaloaders "github.com/iden3/go-schema-processor/v2/loaders"
"github.com/iden3/go-schema-processor/v2/merklize"
"github.com/iden3/go-schema-processor/v2/verifiable"
"github.com/iden3/iden3comm/v2"
"github.com/iden3/iden3comm/v2/packers"
"github.com/iden3/iden3comm/v2/protocol"
shell "github.com/ipfs/go-ipfs-api"
"github.com/piprate/json-gold/ld"
"github.com/pkg/errors"
)

var defaultSchemaLoader ld.DocumentLoader

// SetDocumentLoader sets the default schema loader that would be used if
// other is not set with WithDocumentLoader option. Also, this document loader
// is set for go-schema-processor library to use it for merklize.
func SetDocumentLoader(schemaLoader ld.DocumentLoader) {
defaultSchemaLoader = schemaLoader
merklize.SetDocumentLoader(schemaLoader)
}

// UniversalResolverURL is a url for universal resolver
const UniversalResolverURL = "https://dev.uniresolver.io/1.0/identifiers"

Expand Down Expand Up @@ -86,56 +98,86 @@ var UniversalDIDResolver = packers.DIDResolverHandlerFunc(func(did string) (*ver
// Verifier is a struct for auth instance
type Verifier struct {
verificationKeyLoader loaders.VerificationKeyLoader
claimSchemaLoader loaders.SchemaLoader
documentLoader ld.DocumentLoader
stateResolver map[string]pubsignals.StateResolver
packageManager iden3comm.PackageManager
}

// NewVerifier returns setup instance of auth library
// Deprecated: NewVerifier now return nil it can't set up default package manager for verifier,
// in future major release it will return error
func NewVerifier(
keyLoader loaders.VerificationKeyLoader,
claimSchemaLoader loaders.SchemaLoader,
resolver map[string]pubsignals.StateResolver,
) *Verifier {
v, err := NewVerifierWithExplicitError(keyLoader, claimSchemaLoader,
resolver)
if err != nil {
return nil
// VerifierOption is a function to set options for Verifier instance
type VerifierOption func(opts *verifierOpts)

// WithDocumentLoader sets the document loader for Verifier instance
func WithDocumentLoader(docLoader ld.DocumentLoader) VerifierOption {
return func(opts *verifierOpts) {
opts.docLoader = docLoader
}
return v
}

// NewVerifierWithExplicitError returns verifier instance with default package manager and explicit error if it couldn't register default packers
// in future major release it will be renamed to NewVerifier
func NewVerifierWithExplicitError(
// WithIPFSClient sets the IPFS client for document loader of Verifier instance.
// If document loader is set with WithDocumentLoader function, this option is
// ignored.
func WithIPFSClient(ipfsCli *shell.Shell) VerifierOption {
return func(opts *verifierOpts) {
opts.ipfsCli = ipfsCli
}
}

// WithIPFSGateway sets the IPFS gateway for document loader of Verifier
// instance. If document loader is set with WithDocumentLoader function, this
// option is ignored. If WithIPFSClient is set, this option is ignored also.
func WithIPFSGateway(ipfsGW string) VerifierOption {
return func(opts *verifierOpts) {
opts.ipfsGW = ipfsGW
}
}

// WithDIDResolver sets the DID resolver for Verifier instance. The default
// value is UniversalDIDResolver.
func WithDIDResolver(resolver packers.DIDResolverHandlerFunc) VerifierOption {
return func(opts *verifierOpts) {
opts.didResolver = resolver
}
}

type verifierOpts struct {
docLoader ld.DocumentLoader
ipfsCli *shell.Shell
ipfsGW string
didResolver packers.DIDResolverHandlerFunc
}

func newOpts() verifierOpts {
return verifierOpts{
didResolver: UniversalDIDResolver,
}
}

// NewVerifier returns setup instance of auth library
func NewVerifier(
keyLoader loaders.VerificationKeyLoader,
claimSchemaLoader loaders.SchemaLoader,
resolver map[string]pubsignals.StateResolver,
opts ...VerifierOption,
) (*Verifier, error) {
vOpts := newOpts()
for _, optFn := range opts {
optFn(&vOpts)
}

docLoader := getDocumentLoader(vOpts.docLoader, vOpts.ipfsCli,
vOpts.ipfsGW)
v := &Verifier{
verificationKeyLoader: keyLoader,
claimSchemaLoader: claimSchemaLoader,
documentLoader: docLoader,
stateResolver: resolver,
packageManager: *iden3comm.NewPackageManager(),
}

// try to extract IPFS_URL if the schema loader is the default one
if impl, ok := claimSchemaLoader.(loaders.DefaultSchemaLoader); ok &&
impl.IpfsURL != "" {

ipfsCli := shell.NewShell(impl.IpfsURL)
documentLoader := merklize.NewDocumentLoader(ipfsCli, "")
merklize.SetDocumentLoader(documentLoader)
}

err := v.SetupAuthV2ZKPPacker()
if err != nil {
return nil, err
}

err = v.SetupJWSPacker(UniversalDIDResolver)
err = v.SetupJWSPacker(vOpts.didResolver)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -328,7 +370,7 @@ func (v *Verifier) VerifyAuthResponse(
rawMessage = nil
}

err = cv.VerifyQuery(ctx, query, v.claimSchemaLoader, rawMessage, opts...)
err = cv.VerifyQuery(ctx, query, v.documentLoader, rawMessage, opts...)
if err != nil {
return err
}
Expand Down Expand Up @@ -463,3 +505,17 @@ func findProofByRequestID(arr []protocol.ZeroKnowledgeProofResponse, id uint32)
}
return nil
}

func getDocumentLoader(docLoader ld.DocumentLoader, ipfsCli *shell.Shell,
ipfsGW string) ld.DocumentLoader {

if docLoader != nil {
return docLoader
}

if ipfsCli == nil && ipfsGW == "" && defaultSchemaLoader != nil {
return defaultSchemaLoader
}

return schemaloaders.NewDocumentLoader(ipfsCli, ipfsGW)
}
Loading

0 comments on commit 51cbe86

Please sign in to comment.