forked from 0xPolygonID/tutorial-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
INTERNAL PR VidosResolver example #1
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
Open
antonio-ivanovski
wants to merge
14
commits into
main
Choose a base branch
from
vidos-resolver-example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5160408
vidos resolver
ToTeTo 904b68c
intercept state resolver
ToTeTo c9c61ba
integrate vidos resolver
ToTeTo 6f7aa96
fix exports
ToTeTo d8d695e
add dotenv
ToTeTo 0d9c0e3
and .env to gitignore
ToTeTo 3d0926e
make dotenv regular dep
ToTeTo adc7579
Update verifier-integration/js/.env.example
robdefeo 322a92f
Update verifier-integration/js/.env.example
robdefeo bb56da0
remove old vidos resolver and intercept resolver
ToTeTo cd637f3
use VidosResolver from auth package
ToTeTo 00952ba
Update verifier-integration/js/.env.example
robdefeo 623f551
add vidos resolver network
ToTeTo b69d9a0
Merge remote-tracking branch 'original-origin/main' into vidos-resolv…
ToTeTo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
.DS_Store | ||
**/**/node_modules/ | ||
**/.env |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VIDOS_RESOLVER_URL= | ||
VIDOS_API_KEY= |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
VIDOS_RESOLVER_URL=https://ugly-aqua-hummingbird-120.resolver.service.eu.vidos.dev | ||
VIDOS_API_KEY=24b500499e7c2f0a8634831d1b9894b74d656d3e7cd6de238d706c20a5db6d41 | ||
robdefeo marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
const { Id } = require("@iden3/js-iden3-core"); | ||
const { resolver } = require("@iden3/js-iden3-auth"); | ||
|
||
/** | ||
* Implementation of {@link resolver.IStateResolver} that uses Vidos resolver service to resolve states. | ||
* It can serve as drop-in replacement for {@link resolver.EthStateResolver}. | ||
* | ||
* @implements {resolver.IStateResolver} | ||
*/ | ||
class VidosResolver { | ||
/** | ||
* @param {string} resolverUrl | ||
* @param {string} apiKey | ||
*/ | ||
constructor(resolverUrl, apiKey) { | ||
this.resolverUrl = resolverUrl; | ||
this.apiKey = apiKey; | ||
} | ||
|
||
/** | ||
* @param {bigint} state | ||
* @returns {Promise<resolver.ResolvedState>} | ||
*/ | ||
async rootResolve(state) { | ||
const stateHex = state.toString("16"); | ||
|
||
const zeroAddress = "11111111111111111111"; // 1 is 0 in base58 | ||
const did = `did:polygonid:polygon:amoy:${zeroAddress}?gist=${stateHex}`; | ||
|
||
const response = await fetch(`${this.resolverUrl}/${encodeURIComponent(did)}`, { | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${this.apiKey}`, | ||
}, | ||
}); | ||
const result = await response.json(); | ||
|
||
const globalInfo = result.didDocument.verificationMethod[0].global; | ||
if (globalInfo == null) throw new Error('gist info not found') | ||
|
||
if (globalInfo.root !== stateHex) { | ||
throw new Error('gist info contains invalid state'); | ||
} | ||
|
||
if (globalInfo.replacedByRoot !== "0") { | ||
if (globalInfo.replacedAtTimestamp === "0") { | ||
throw new Error('state was replaced, but replaced time unknown'); | ||
} | ||
return { | ||
latest: false, | ||
state: state, | ||
transitionTimestamp: globalInfo.replacedAtTimestamp, | ||
genesis: false, | ||
}; | ||
} | ||
|
||
return { | ||
latest: true, | ||
state: state, | ||
transitionTimestamp: 0, | ||
genesis: false, | ||
}; | ||
} | ||
|
||
/** | ||
* @param {bigint} id | ||
* @param {bigint} state | ||
* @returns {Promise<resolver.ResolvedState>} | ||
*/ | ||
async resolve(id, state) { | ||
const iden3Id = Id.fromBigInt(id); | ||
const stateHex = state.toString("16"); | ||
|
||
const did = `did:polygonid:polygon:amoy:${iden3Id.string()}`; | ||
|
||
const didWithState = `${did}?state=${stateHex}`; | ||
const response = await fetch( | ||
`${this.resolverUrl}/${encodeURIComponent(didWithState)}`, | ||
{ | ||
method: "GET", | ||
headers: { | ||
Authorization: `Bearer ${this.apiKey}`, | ||
}, | ||
} | ||
); | ||
const result = await response.json(); | ||
const isGenesis = resolver.isGenesisStateId(id, state); | ||
|
||
const stateInfo = result.didDocument.verificationMethod[0].info; | ||
if (stateInfo == null) throw new Error('state info not found'); | ||
|
||
if (stateInfo.id !== did) { | ||
throw new Error(`state was recorded for another identity`); | ||
} | ||
|
||
if (stateInfo.state !== stateHex) { | ||
if (stateInfo.replacedAtTimestamp === "0") { | ||
throw new Error(`no information about state transition`); | ||
} | ||
return { | ||
latest: false, | ||
genesis: false, | ||
state: state, | ||
transitionTimestamp: Number.parseInt(stateInfo.replacedAtTimestamp), | ||
}; | ||
} | ||
|
||
return { | ||
latest: stateInfo.replacedAtTimestamp === "0", | ||
genesis: isGenesis, | ||
state, | ||
transitionTimestamp: Number.parseInt(stateInfo.replacedAtTimestamp), | ||
}; | ||
} | ||
} | ||
|
||
module.exports = VidosResolver; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { resolver } = require("@iden3/js-iden3-auth"); | ||
|
||
/** | ||
* Wrapper around a state resolver that logs the state resolution process. Useful for debugging and learning how state resolution works. | ||
* | ||
* @param {resolver.IStateResolver} resolver | ||
* @returns {resolver.IStateResolver} | ||
*/ | ||
function interceptStateResolver(resolver) { | ||
return { | ||
async resolve(id, state) { | ||
try { | ||
console.log("Resolving state", id, state); | ||
const result = await resolver.resolve(id, state); | ||
console.log("Resolved state", id, state, result); | ||
return result; | ||
} catch (e) { | ||
console.error("Error resolving state", id, state, e); | ||
throw e; | ||
} | ||
}, | ||
async rootResolve(state) { | ||
try { | ||
console.log("Resolving root state", state); | ||
const result = await resolver.rootResolve(state); | ||
console.log("Resolved root state", state, result); | ||
return result; | ||
} catch (e) { | ||
console.error("Error resolving root state", state, e); | ||
throw e; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
module.exports = interceptStateResolver; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{ | ||
"dependencies": { | ||
"@iden3/js-iden3-auth": "1.3.2", | ||
"express": "^4.18.2" | ||
"@iden3/js-iden3-core": "^1.3.1", | ||
"express": "^4.18.2", | ||
"dotenv": "^16.4.5" | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.