Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Verify Token V2 #78

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 80 additions & 10 deletions auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"github.com/ethereum/go-ethereum/ethclient"
"github.com/google/uuid"
"github.com/iden3/contracts-abi/state/go/abi"
"github.com/iden3/driver-did-iden3/pkg/services/blockchain/eth"
"github.com/iden3/go-circuits/v2"
"github.com/iden3/go-iden3-auth/v2/loaders"
"github.com/iden3/go-iden3-auth/v2/proofs"
Expand Down Expand Up @@ -100,7 +101,8 @@
verificationKeyLoader loaders.VerificationKeyLoader
documentLoader ld.DocumentLoader
stateResolver map[string]pubsignals.StateResolver
ethResolvers map[int]eth.Resolver
packageManager iden3comm.PackageManager

Check failure on line 105 in auth.go

View workflow job for this annotation

GitHub Actions / lint

undefined: iden3comm (typecheck)
}

// VerifierOption is a function to set options for Verifier instance
Expand Down Expand Up @@ -139,17 +141,46 @@
}
}

// WithEthereumResolvers set the ethereum resolvers to use. It will overwrite the
// default ones.
func WithEthereumResolvers(resolvers map[int]eth.Resolver) VerifierOption {
return func(opts *verifierOpts) {
opts.ethResolvers = resolvers
}
}

type verifierOpts struct {
docLoader ld.DocumentLoader
ipfsCli schemaloaders.IPFSClient
ipfsGW string
didResolver packers.DIDResolverHandlerFunc
docLoader ld.DocumentLoader
ipfsCli schemaloaders.IPFSClient
ipfsGW string
didResolver packers.DIDResolverHandlerFunc
ethResolvers map[int]eth.Resolver
}

func newOpts() verifierOpts {
return verifierOpts{
didResolver: UniversalDIDResolver,
func defaultEthResolver() (map[int]eth.Resolver, error) {
const (
chainID = 21000
rpc = "https://rpc-mainnet.privado.id"
contractAddr = "0x58485809CfAc875B7E6F54E3fCb5f24614f202e9"
)
r, err := eth.NewResolver(rpc, contractAddr)
if err != nil {
return nil, err
}
return map[int]eth.Resolver{
chainID: *r,
}, nil
}

func newOpts() (verifierOpts, error) {
ethRes, err := defaultEthResolver()
if err != nil {
return verifierOpts{}, err
}
return verifierOpts{
didResolver: UniversalDIDResolver,
ethResolvers: ethRes,
}, nil
}

// NewVerifier returns setup instance of auth library
Expand All @@ -158,7 +189,10 @@
resolver map[string]pubsignals.StateResolver,
opts ...VerifierOption,
) (*Verifier, error) {
vOpts := newOpts()
vOpts, err := newOpts()
if err != nil {
return nil, err
}
for _, optFn := range opts {
optFn(&vOpts)
}
Expand All @@ -172,7 +206,7 @@
packageManager: *iden3comm.NewPackageManager(),
}

err := v.SetupAuthV2ZKPPacker()
err = v.SetupAuthV2ZKPPacker()
if err != nil {
return nil, err
}
Expand All @@ -186,12 +220,12 @@
}

// SetPackageManager sets the package manager for the VerifierBuilder.
func (v *Verifier) SetPackageManager(manager iden3comm.PackageManager) {

Check failure on line 223 in auth.go

View workflow job for this annotation

GitHub Actions / lint

undefined: iden3comm (typecheck)
v.packageManager = manager
}

// SetPacker sets the custom packer manager for the VerifierBuilder.
func (v *Verifier) SetPacker(packer iden3comm.Packer) error {

Check failure on line 228 in auth.go

View workflow job for this annotation

GitHub Actions / lint

undefined: iden3comm (typecheck)
return v.packageManager.RegisterPackers(packer)
}

Expand Down Expand Up @@ -302,7 +336,6 @@
From: sender,
Body: protocol.ContractInvokeRequestMessageBody{
Reason: reason,
Message: message,
TransactionData: transactionData,
Scope: zkRequests,
},
Expand Down Expand Up @@ -516,7 +549,44 @@
return nil
}

// VerifyToken performs verification of jws/jwz token using the registered packers in package manager
func (v *Verifier) VerifyToken(
token string,
opts ...pubsignals.VerifyOpt,
) (*protocol.AuthorizationResponseMessage, error) {

var unpackOpts []iden3comm.PackerParams
cfg := pubsignals.VerifyConfig{}
for _, o := range opts {
o(&cfg)
}
if cfg.AcceptedProofGenerationDelay > 0 {
authV2Set, err := v.verificationKeyLoader.Load(circuits.AuthV2CircuitID)
if err != nil {
return nil, fmt.Errorf("failed upload circuits files: %w", err)
}
unpackOpts = append(unpackOpts, packers.NewZKPPUnpackerParams(authV2Set, v.ethResolvers, cfg.AcceptedProofGenerationDelay))
}

msg, _, err := v.packageManager.Unpack([]byte(token), unpackOpts...)
if err != nil {
return nil, err
}
msgBytes, err := json.Marshal(msg)
if err != nil {
return nil, err
}

var authMsgResponse protocol.AuthorizationResponseMessage
err = json.Unmarshal(msgBytes, &authMsgResponse)
if err != nil {
return nil, err
}
return &authMsgResponse, err
}

// VerifyJWZ performs verification of jwz token
// Deprecated: Use VerifyToken instead
func (v *Verifier) VerifyJWZ(
ctx context.Context,
token string,
Expand Down
Loading
Loading