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/iden3 did for e2e #12

Merged
merged 4 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 13 additions & 1 deletion .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ jobs:

- name: Build file with supported networks
run: |
echo -e "iden3:" > resolvers.settings.yaml
echo -e "polygon:" > resolvers.settings.yaml
echo -e " amoy:" >> resolvers.settings.yaml
echo -e " contractAddress: ${{ env.STATE_CONTRACT_ADDRESS_AMOY }}" >> resolvers.settings.yaml
echo -e " networkURL: ${{ secrets.POLYGON_AMOY_NODE_URL }}" >> resolvers.settings.yaml
Expand Down Expand Up @@ -60,7 +60,19 @@ jobs:
--global-var issuer_server=${{ secrets.ISSUER_URL }} \
--bail --verbose

- name: Run state tests
id: e2e_state
continue-on-error: true
run: |
npm install -g newman && \
newman run tests/e2e/state_resolve_tests.json \
-g ./tests/e2e/dev_env.json \
--bail --verbose

- name: e2e tests finished
if: steps.e2e.outcome != 'success'
run: exit 1
- name: e2e test state finished
if: steps.e2e_state.outcome != 'success'
run: exit 1
# / TESTS
2 changes: 1 addition & 1 deletion .github/workflows/push-container.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- name: Build file with supported networks
run: |
cd driver-did-iden3
echo -e "iden3:" > resolvers.settings.yaml
echo -e "polygon:" > resolvers.settings.yaml
echo -e " amoy:" >> resolvers.settings.yaml
echo -e " contractAddress: ${{ env.STATE_CONTRACT_ADDRESS_AMOY }}" >> resolvers.settings.yaml
echo -e " networkURL: ${{ secrets.POLYGON_AMOY_NODE_URL }}" >> resolvers.settings.yaml
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

# Code editors
.idea
.vscode

# Dependency directories (remove the comment below to include it)
# vendor/
Expand Down
15 changes: 4 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ go 1.18
require (
github.com/ethereum/go-ethereum v1.11.5
github.com/golang/mock v1.6.0
github.com/iden3/go-iden3-core v1.0.0
github.com/iden3/contracts-abi/state/go/abi v1.0.1
github.com/iden3/go-iden3-core/v2 v2.1.1
github.com/kelseyhightower/envconfig v1.4.0
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.1
github.com/stretchr/testify v1.8.2
github.com/wealdtech/go-ens/v3 v3.5.5
golang.org/x/crypto v0.7.0
golang.org/x/net v0.8.0
Expand All @@ -19,27 +20,23 @@ require (
github.com/deckarep/golang-set/v2 v2.2.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/holiman/uint256 v1.2.0 // indirect
github.com/iden3/contracts-abi/state/go/abi v1.0.1 // indirect
)

require (
github.com/btcsuite/btcd v0.22.0-beta // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.2 // indirect
github.com/cespare/cp v1.1.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/gballet/go-libpcsclite v0.0.0-20191108122812-4678299bea08 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/iden3/go-iden3-crypto v0.0.13 // indirect
github.com/iden3/go-iden3-crypto v0.0.15 // indirect
github.com/iden3/go-merkletree-sql/v2 v2.0.0
github.com/ipfs/go-cid v0.3.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/pretty v0.3.1 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
Expand All @@ -50,14 +47,10 @@ require (
github.com/multiformats/go-multihash v0.2.1 // indirect
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/tsdb v0.10.0 // indirect
github.com/rjeczalik/notify v0.9.3 // indirect
github.com/shirou/gopsutil v3.21.11+incompatible // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/status-im/keycard-go v0.2.0 // indirect
github.com/tklauser/go-sysconf v0.3.11 // indirect
github.com/tklauser/numcpus v0.6.0 // indirect
github.com/tyler-smith/go-bip39 v1.1.0 // indirect
github.com/wealdtech/go-multicodec v1.4.0 // indirect
github.com/yusufpapurcu/wmi v1.2.2 // indirect
golang.org/x/sys v0.6.0 // indirect
Expand Down
144 changes: 34 additions & 110 deletions go.sum

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/app/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strings"

"github.com/iden3/driver-did-iden3/pkg/services"
core "github.com/iden3/go-iden3-core"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)
Expand All @@ -31,7 +31,7 @@ func (d *DidDocumentHandler) Get(w http.ResponseWriter, r *http.Request) {
}

state, err := d.DidDocumentService.GetDidDocument(r.Context(), rawURL[len(rawURL)-1], &opts)
if errors.Is(err, core.ErrInvalidDID) {
if errors.Is(err, core.ErrIncorrectDID) {
log.Println("invalid did:", err)

} else if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions pkg/document/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ func NewDidResolution() *DidResolution {
return &DidResolution{
Context: defaultContext,
DidDocument: &DidDocument{
Context: []string{defaultDidDocContext, iden3Context},
Authentication: []Authentication{},
Context: []string{defaultDidDocContext, iden3Context},
VerificationMethod: []VerificationMethod{},
},
DidResolutionMetadata: &DidResolutionMetadata{
ContentType: defaultContentType,
Expand Down Expand Up @@ -75,17 +75,17 @@ func NewDidErrorResolution(errCode ErrorCode, errMsg string) *DidResolution {
}
}

type Authentication struct {
type VerificationMethod struct {
ID string `json:"id"`
Type string `json:"type"`
IdentityState
}

// DidDocument representation of did document.
type DidDocument struct {
Context []string `json:"@context"`
ID string `json:"id"`
Authentication []Authentication `json:"authentication"`
Context []string `json:"@context"`
ID string `json:"id"`
VerificationMethod []VerificationMethod `json:"verificationMethod"`
}

// DidResolutionMetadata representation of resolution metadata.
Expand Down
17 changes: 12 additions & 5 deletions pkg/services/blockchain/eth/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"github.com/ethereum/go-ethereum/ethclient"
contract "github.com/iden3/contracts-abi/state/go/abi"
"github.com/iden3/driver-did-iden3/pkg/services"
core "github.com/iden3/go-iden3-core"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
)

//go:generate mockgen -destination=contract/mock/contract.go . StateContract
Expand Down Expand Up @@ -95,7 +96,7 @@ func (r *Resolver) ResolveGist(

func (r *Resolver) Resolve(
ctx context.Context,
did core.DID,
did w3c.DID,
opts *services.ResolverOpts,
) (services.IdentityState, error) {
if opts.GistRoot != nil && opts.State != nil {
Expand All @@ -109,13 +110,19 @@ func (r *Resolver) Resolve(
err error
)

userID, err := core.IDFromDID(did)
if err != nil {
return services.IdentityState{},
fmt.Errorf("invalid did format for did '%s': %v", did, err)
}

switch {
case opts.GistRoot != nil:
stateInfo, gistInfo, err = r.resolveStateByGistRoot(ctx, did.ID, opts.GistRoot)
stateInfo, gistInfo, err = r.resolveStateByGistRoot(ctx, userID, opts.GistRoot)
case opts.State != nil:
stateInfo, err = r.resolveState(ctx, did.ID, opts.State)
stateInfo, err = r.resolveState(ctx, userID, opts.State)
default:
stateInfo, gistInfo, err = r.resolveLatest(ctx, did.ID)
stateInfo, gistInfo, err = r.resolveLatest(ctx, userID)
}

identityState := services.IdentityState{}
Expand Down
24 changes: 14 additions & 10 deletions pkg/services/blockchain/eth/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
contract "github.com/iden3/contracts-abi/state/go/abi"
"github.com/iden3/driver-did-iden3/pkg/services"
cm "github.com/iden3/driver-did-iden3/pkg/services/blockchain/eth/contract/mock"
core "github.com/iden3/go-iden3-core"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
)

var userDID, _ = core.ParseDID("did:polygonid:polygon:mumbai:2qJEaVmT5jBrtgBQ4m7b7bRYzWmvMyDjBZGP24QwvD")
var userDID, _ = w3c.ParseDID("did:polygonid:polygon:mumbai:2qJEaVmT5jBrtgBQ4m7b7bRYzWmvMyDjBZGP24QwvD")

func TestResolveGist_Success(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -72,7 +73,7 @@ func TestResolve_Success(t *testing.T) {
tests := []struct {
name string
opts *services.ResolverOpts
userDID *core.DID
userDID *w3c.DID
contractMock func(c *cm.MockStateContract)
expectedIdentityState services.IdentityState
}{
Expand All @@ -88,11 +89,12 @@ func TestResolve_Success(t *testing.T) {
Existence: true,
Value: big.NewInt(5),
}
c.EXPECT().GetGISTProofByRoot(gomock.Any(), userDID.ID.BigInt(), big.NewInt(1)).Return(proof, nil)
userID, _ := core.IDFromDID(*userDID)
c.EXPECT().GetGISTProofByRoot(gomock.Any(), userID.BigInt(), big.NewInt(1)).Return(proof, nil)
gistInfo := contract.IStateGistRootInfo{Root: big.NewInt(555)}
c.EXPECT().GetGISTRootInfo(gomock.Any(), big.NewInt(4)).Return(gistInfo, nil)
stateInfo := contract.IStateStateInfo{Id: userDID.ID.BigInt(), State: big.NewInt(444)}
c.EXPECT().GetStateInfoByIdAndState(gomock.Any(), userDID.ID.BigInt(), big.NewInt(5)).Return(stateInfo, nil)
stateInfo := contract.IStateStateInfo{Id: userID.BigInt(), State: big.NewInt(444)}
c.EXPECT().GetStateInfoByIdAndState(gomock.Any(), userID.BigInt(), big.NewInt(5)).Return(stateInfo, nil)
},
expectedIdentityState: services.IdentityState{
StateInfo: &services.StateInfo{
Expand All @@ -111,8 +113,9 @@ func TestResolve_Success(t *testing.T) {
},
userDID: userDID,
contractMock: func(c *cm.MockStateContract) {
res := contract.IStateStateInfo{Id: userDID.ID.BigInt(), State: big.NewInt(555)}
c.EXPECT().GetStateInfoByIdAndState(gomock.Any(), userDID.ID.BigInt(), big.NewInt((1))).Return(res, nil)
userID, _ := core.IDFromDID(*userDID)
res := contract.IStateStateInfo{Id: userID.BigInt(), State: big.NewInt(555)}
c.EXPECT().GetStateInfoByIdAndState(gomock.Any(), userID.BigInt(), big.NewInt((1))).Return(res, nil)
},
expectedIdentityState: services.IdentityState{
StateInfo: &services.StateInfo{
Expand All @@ -127,12 +130,13 @@ func TestResolve_Success(t *testing.T) {
opts: &services.ResolverOpts{},
userDID: userDID,
contractMock: func(c *cm.MockStateContract) {
userID, _ := core.IDFromDID(*userDID)
latestGist := big.NewInt(100)
c.EXPECT().GetGISTRoot(gomock.Any()).Return(latestGist, nil)
latestGistInfo := contract.IStateGistRootInfo{Root: big.NewInt(400)}
c.EXPECT().GetGISTRootInfo(gomock.Any(), latestGist).Return(latestGistInfo, nil)
stateInfo := contract.IStateStateInfo{Id: userDID.ID.BigInt(), State: big.NewInt(555)}
c.EXPECT().GetStateInfoById(gomock.Any(), userDID.ID.BigInt()).Return(stateInfo, nil)
stateInfo := contract.IStateStateInfo{Id: userID.BigInt(), State: big.NewInt(555)}
c.EXPECT().GetStateInfoById(gomock.Any(), userID.BigInt()).Return(stateInfo, nil)
},
expectedIdentityState: services.IdentityState{
StateInfo: &services.StateInfo{
Expand Down
66 changes: 37 additions & 29 deletions pkg/services/did.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (

"github.com/iden3/driver-did-iden3/pkg/document"
"github.com/iden3/driver-did-iden3/pkg/services/ens"
core "github.com/iden3/go-iden3-core"
core "github.com/iden3/go-iden3-core/v2"
"github.com/iden3/go-iden3-core/v2/w3c"
"github.com/iden3/go-merkletree-sql/v2"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -37,21 +38,39 @@ func (d *DidDocumentServices) GetDidDocument(ctx context.Context, did string, op
opts = &ResolverOpts{}
}

userDID, err := core.ParseDID(did)
userDID, err := w3c.ParseDID(did)
errResolution, err := expectedError(err)
if err != nil {
return errResolution, err
}

resolver, err := d.resolvers.GetResolverByNetwork(string(userDID.Blockchain), string(userDID.NetworkID))
userID, err := core.IDFromDID(*userDID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

b, err := core.BlockchainFromID(userID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

n, err := core.NetworkIDFromID(userID)
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

resolver, err := d.resolvers.GetResolverByNetwork(string(b), string(n))
errResolution, err = expectedError(err)
if err != nil {
return errResolution, err
}

identityState, err := resolver.Resolve(ctx, *userDID, opts)
if errors.Is(err, ErrNotFound) && (opts.State != nil || opts.GistRoot != nil) {
gen, errr := isGenesis(userDID.ID.BigInt(), opts.State)
gen, errr := isGenesis(userID.BigInt(), opts.State)
if errr != nil {
return nil, fmt.Errorf("invalid state: %v", errr)
}
Expand All @@ -72,9 +91,9 @@ func (d *DidDocumentServices) GetDidDocument(ctx context.Context, did string, op

didResolution := document.NewDidResolution()
didResolution.DidDocument.ID = did
didResolution.DidDocument.Authentication = append(
didResolution.DidDocument.Authentication,
document.Authentication{
didResolution.DidDocument.VerificationMethod = append(
didResolution.DidDocument.VerificationMethod,
document.VerificationMethod{
ID: getRepresentaionID(did, identityState),
Type: document.StateType,
IdentityState: document.IdentityState{
Expand Down Expand Up @@ -102,12 +121,12 @@ func (d *DidDocumentServices) ResolveDNSDomain(ctx context.Context, domain strin
}

var (
did *core.DID
did *w3c.DID
v string
)
// try to find correct did.
for _, v = range records {
did, err = core.ParseDID(v)
did, err = w3c.ParseDID(v)
if did != nil && err == nil {
break
}
Expand Down Expand Up @@ -167,39 +186,28 @@ func isGenesis(id, state *big.Int) (bool, error) {
return false, nil
}

userID, err := core.IDFromInt(id)
if err != nil {
return false, err
}
userDID, err := core.ParseDIDFromID(userID)
if err != nil {
return false, err
}

didType, err := core.BuildDIDType(userDID.Method, userDID.Blockchain, userDID.NetworkID)
if err != nil {
return false, err
}
identifier, err := core.IdGenesisFromIdenState(didType, state)
isGenesis, err := core.CheckGenesisStateID(id, state)
if err != nil {
return false, err
}

return id.Cmp(identifier.BigInt()) == 0, nil
return isGenesis, nil
}

func expectedError(err error) (*document.DidResolution, error) {
if err == nil {
return nil, nil
}

if errors.Is(err, core.ErrInvalidDID) {
switch {
case errors.Is(err, core.ErrIncorrectDID):
return document.NewDidInvalidResolution(err.Error()), err
}
if errors.Is(err, core.ErrNetworkNotSupportedForDID) {
case
errors.Is(err, core.ErrBlockchainNotSupportedForDID),
errors.Is(err, core.ErrNetworkNotSupportedForDID):

return document.NewNetworkNotSupportedForDID(err.Error()), err
}
if errors.Is(err, core.ErrDIDMethodNotSupported) {
case errors.Is(err, core.ErrDIDMethodNotSupported):
return document.NewDidMethodNotSupportedResolution(err.Error()), err
}

Expand Down
Loading
Loading