forked from celo-org/celo-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from celo-org/master
master
- Loading branch information
Showing
63 changed files
with
643 additions
and
817 deletions.
There are no files selected for viewing
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
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
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
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
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
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
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
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
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,67 @@ | ||
import { isValidUrl } from '@celo/utils/lib/io' | ||
import { Address } from '../../base' | ||
import { IdentityMetadataWrapper } from '../metadata' | ||
import { AccountClaim } from './account' | ||
import { Claim } from './claim' | ||
import { verifyKeybaseClaim } from './keybase' | ||
import { ClaimTypes } from './types' | ||
|
||
/** | ||
* Verifies a claim made by an account, i.e. whether a claim can be verified to be correct | ||
* @param claim The claim to verify | ||
* @param address The address that is making the claim | ||
* @param metadataURLGetter A function that can retrieve the metadata URL for a given account address, | ||
* should be Accounts.getMetadataURL() | ||
* @returns If valid, returns undefined. If invalid or unable to verify, returns a string with the error | ||
*/ | ||
export async function verifyClaim( | ||
claim: Claim, | ||
address: string, | ||
metadataURLGetter: MetadataURLGetter | ||
) { | ||
switch (claim.type) { | ||
case ClaimTypes.KEYBASE: | ||
return verifyKeybaseClaim(claim, address) | ||
case ClaimTypes.ACCOUNT: | ||
return verifyAccountClaim(claim, address, metadataURLGetter) | ||
default: | ||
break | ||
} | ||
return | ||
} | ||
|
||
/** | ||
* A function that can asynchronously fetch the metadata URL for an account address | ||
* Should virtually always be Accounts#getMetadataURL | ||
*/ | ||
export type MetadataURLGetter = (address: Address) => Promise<string> | ||
|
||
export const verifyAccountClaim = async ( | ||
claim: AccountClaim, | ||
address: string, | ||
metadataURLGetter: MetadataURLGetter | ||
) => { | ||
const metadataURL = await metadataURLGetter(claim.address) | ||
|
||
console.info(JSON.stringify(metadataURL)) | ||
if (!isValidUrl(metadataURL)) { | ||
return `Metadata URL of ${claim.address} could not be retrieved` | ||
} | ||
|
||
let metadata: IdentityMetadataWrapper | ||
try { | ||
metadata = await IdentityMetadataWrapper.fetchFromURL(metadataURL) | ||
} catch (error) { | ||
return `Metadata could not be fetched for ${ | ||
claim.address | ||
} at ${metadataURL}: ${error.toString()}` | ||
} | ||
|
||
const accountClaims = metadata.filterClaims(ClaimTypes.ACCOUNT) | ||
|
||
if (accountClaims.find((x) => x.address === address) === undefined) { | ||
return `${claim.address} did not claim ${address}` | ||
} | ||
|
||
return | ||
} |
Oops, something went wrong.