forked from iden3/js-iden3-auth
-
Notifications
You must be signed in to change notification settings - Fork 0
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
INTERNAL PR js-iden3-auth: VidosResolver #1
Open
toteto
wants to merge
8
commits into
develop
Choose a base branch
from
vidos-resolver
base: develop
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1055479
implement vidos resolver
toteto 6f75f72
update import
toteto ab87448
fix formatting
toteto a6c0e58
add impl note
toteto 156be16
fix prettier
toteto fa1ed4c
use did-resolver type
toteto cc4047c
add network config
toteto 7165914
throw error on did resolution metadata error
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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,4 +1,5 @@ | ||
export * as auth from '@lib/auth/auth'; | ||
export * as resolver from '@lib/state/resolver'; | ||
export { default as VidosResolver } from '@lib/state/vidosResolver'; | ||
export * as protocol from '@lib/types-sdk'; | ||
export { core } from '@0xpolygonid/js-sdk'; |
This file contains 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,133 @@ | ||
/* eslint-disable prettier/prettier */ | ||
import { Id } from '@iden3/js-iden3-core'; | ||
import { type IStateResolver, type ResolvedState, isGenesisStateId } from './resolver'; | ||
import type { DIDDocument, DIDResolutionResult, VerificationMethod } from 'did-resolver'; | ||
|
||
/** | ||
* Extended DID resolution result that includes additional information about Polygon ID resolution. | ||
*/ | ||
type PolygonDidResolutionResult = DIDResolutionResult & { | ||
didDocument: DIDDocument & { | ||
verificationMethod: (VerificationMethod & { | ||
info: { | ||
id: string; | ||
state: string; | ||
replacedByState: string; | ||
createdAtTimestamp: string; | ||
replacedAtTimestamp: string; | ||
createdAtBlock: string; | ||
replacedAtBlock: string; | ||
}; | ||
global: { | ||
root: string; | ||
replacedByRoot: string; | ||
createdAtTimestamp: string; | ||
replacedAtTimestamp: string; | ||
createdAtBlock: string; | ||
replacedAtBlock: string; | ||
}; | ||
})[]; | ||
}; | ||
}; | ||
|
||
/** | ||
* Implementation of {@link IStateResolver} that uses Vidos resolver service to resolve states. | ||
* It can serve as drop-in replacement for EthStateResolver. | ||
*/ | ||
export default class VidosResolver implements IStateResolver { | ||
constructor(private readonly resolverUrl: string, private readonly apiKey: string, private readonly network: 'main' | 'mumbai' | 'amoy' = 'main') {} | ||
|
||
// Note: implementation closely resembles EthStateResolver because Vidos resolver internally uses the same contract. | ||
// The only difference is the usage of regular HTTP requests instead of web3 calls. | ||
|
||
async rootResolve(state: bigint): Promise<ResolvedState> { | ||
const stateHex = state.toString(16); | ||
|
||
const zeroAddress = '11111111111111111111'; // 1 is 0 in base58 | ||
const did = `did:polygonid:polygon:${this.network}:${zeroAddress}?gist=${stateHex}`; | ||
|
||
const response = await fetch(`${this.resolverUrl}/${encodeURIComponent(did)}`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: `Bearer ${this.apiKey}` | ||
} | ||
}); | ||
const result = (await response.json()) as PolygonDidResolutionResult; | ||
if (result.didResolutionMetadata.error) { | ||
throw new Error(`error resolving DID: ${result.didResolutionMetadata.error}`); | ||
} | ||
|
||
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 | ||
}; | ||
} | ||
|
||
async resolve(id: bigint, state: bigint): Promise<ResolvedState> { | ||
const iden3Id = Id.fromBigInt(id); | ||
const stateHex = state.toString(16); | ||
|
||
const did = `did:polygonid:polygon:${this.network}:${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()) as PolygonDidResolutionResult; | ||
if (result.didResolutionMetadata.error) { | ||
throw new Error(`error resolving DID: ${result.didResolutionMetadata.error}`); | ||
} | ||
|
||
const isGenesis = 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) | ||
}; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
suggest checking for an error in
result.didResolutionMetadata.error
before going too far as otherwise likely will give an incorrect resultThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done 7165914, error is thrown on such resolution error