2021-05-02 04:49:52 -06:00
class Claim extends HTMLElement {
// Specify the attributes to observe
static get observedAttributes ( ) {
return [ 'data-claim' ] ;
}
constructor ( ) {
// Call super
super ( ) ;
// Shadow root
this . attachShadow ( { mode : 'open' } ) ;
// Details element
const details = document . createElement ( 'details' ) ;
details . setAttribute ( 'class' , 'kx-item' ) ;
// Summary element
const summary = details . appendChild ( document . createElement ( 'summary' ) ) ;
// Info
const info = summary . appendChild ( document . createElement ( 'div' ) ) ;
info . setAttribute ( 'class' , 'info' ) ;
// Info > Service provider
const serviceProvider = info . appendChild ( document . createElement ( 'p' ) ) ;
serviceProvider . setAttribute ( 'class' , 'subtitle' ) ;
// Info > Profile
const profile = info . appendChild ( document . createElement ( 'p' ) ) ;
profile . setAttribute ( 'class' , 'title' ) ;
// Icons
const icons = summary . appendChild ( document . createElement ( 'div' ) ) ;
icons . setAttribute ( 'class' , 'icons' ) ;
const icons _ _verificationStatus = icons . appendChild ( document . createElement ( 'div' ) ) ;
icons _ _verificationStatus . setAttribute ( 'class' , 'verificationStatus' ) ;
const icons _ _verificationStatus _ _inProgress = icons _ _verificationStatus . appendChild ( document . createElement ( 'div' ) ) ;
icons _ _verificationStatus _ _inProgress . setAttribute ( 'class' , 'inProgress' ) ;
// Details content
const content = details . appendChild ( document . createElement ( 'div' ) ) ;
content . setAttribute ( 'class' , 'content' ) ;
// Load CSS stylesheet
const linkCSS = document . createElement ( 'link' ) ;
linkCSS . setAttribute ( 'rel' , 'stylesheet' ) ;
linkCSS . setAttribute ( 'href' , '/static/kx-styles.css' ) ;
// Attach the elements to the shadow DOM
this . shadowRoot . append ( linkCSS , details ) ;
}
attributeChangedCallback ( name , oldValue , newValue ) {
this . updateContent ( newValue ) ;
}
async verify ( ) {
const claim = new doip . Claim ( JSON . parse ( this . getAttribute ( 'data-claim' ) ) ) ;
await claim . verify ( {
proxy : {
policy : 'adaptive' ,
hostname : 'PLACEHOLDER__PROXY_HOSTNAME'
}
} ) ;
this . setAttribute ( 'data-claim' , JSON . stringify ( claim ) ) ;
}
updateContent ( value ) {
const shadow = this . shadowRoot ;
const claim = new doip . Claim ( JSON . parse ( value ) ) ;
switch ( claim . matches [ 0 ] . serviceprovider . name ) {
case 'dns' :
case 'xmpp' :
case 'irc' :
shadow . querySelector ( '.info .subtitle' ) . innerText = claim . matches [ 0 ] . serviceprovider . name . toUpperCase ( ) ;
break ;
default :
shadow . querySelector ( '.info .subtitle' ) . innerText = claim . matches [ 0 ] . serviceprovider . name ;
break ;
}
shadow . querySelector ( '.info .title' ) . innerText = claim . matches [ 0 ] . profile . display ;
try {
if ( claim . status === 'verified' ) {
shadow . querySelector ( '.icons .verificationStatus' ) . setAttribute ( 'data-value' , claim . verification . result ? 'success' : 'failed' ) ;
} else {
shadow . querySelector ( '.icons .verificationStatus' ) . setAttribute ( 'data-value' , 'running' ) ;
}
} catch ( error ) {
shadow . querySelector ( '.icons .verificationStatus' ) . setAttribute ( 'data-value' , 'failed' ) ;
}
const elContent = shadow . querySelector ( '.content' ) ;
elContent . innerHTML = ` ` ;
// Handle failed ambiguous claim
if ( claim . status === 'verified' && ! claim . verification . result && claim . isAmbiguous ( ) ) {
shadow . querySelector ( '.info .subtitle' ) . innerText = '---' ;
const subsection0 = elContent . appendChild ( document . createElement ( 'div' ) ) ;
subsection0 . setAttribute ( 'class' , 'subsection' ) ;
const subsection0 _icon = subsection0 . appendChild ( document . createElement ( 'img' ) ) ;
subsection0 _icon . setAttribute ( 'src' , '/static/img/alert-decagram.png' ) ;
const subsection0 _text = subsection0 . appendChild ( document . createElement ( 'div' ) ) ;
const message = subsection0 _text . appendChild ( document . createElement ( 'p' ) ) ;
2021-05-02 05:06:18 -06:00
message . innerHTML = ` None of the matched service providers could be verified. Keyoxide was not able to determine which was the correct service provider or why the verification process failed. ` ;
2021-05-02 04:49:52 -06:00
return ;
}
// Links to profile and proof
const subsection1 = elContent . appendChild ( document . createElement ( 'div' ) ) ;
subsection1 . setAttribute ( 'class' , 'subsection' ) ;
const subsection1 _icon = subsection1 . appendChild ( document . createElement ( 'img' ) ) ;
subsection1 _icon . setAttribute ( 'src' , '/static/img/link.png' ) ;
const subsection1 _text = subsection1 . appendChild ( document . createElement ( 'div' ) ) ;
const profile _link = subsection1 _text . appendChild ( document . createElement ( 'p' ) ) ;
if ( claim . matches [ 0 ] . profile . uri ) {
profile _link . innerHTML = ` Profile link: <a href=" ${ claim . matches [ 0 ] . profile . uri } "> ${ claim . matches [ 0 ] . profile . uri } </a> ` ;
} else {
profile _link . innerHTML = ` Profile link: not accessible from browser ` ;
}
const proof _link = subsection1 _text . appendChild ( document . createElement ( 'p' ) ) ;
if ( claim . matches [ 0 ] . proof . uri ) {
proof _link . innerHTML = ` Proof link: <a href=" ${ claim . matches [ 0 ] . proof . uri } "> ${ claim . matches [ 0 ] . proof . uri } </a> ` ;
} else {
proof _link . innerHTML = ` Proof link: not accessible from browser ` ;
}
elContent . appendChild ( document . createElement ( 'hr' ) ) ;
// Claim verification status
const subsection2 = elContent . appendChild ( document . createElement ( 'div' ) ) ;
subsection2 . setAttribute ( 'class' , 'subsection' ) ;
const subsection2 _icon = subsection2 . appendChild ( document . createElement ( 'img' ) ) ;
subsection2 _icon . setAttribute ( 'src' , '/static/img/decagram.png' ) ;
const subsection2 _text = subsection2 . appendChild ( document . createElement ( 'div' ) ) ;
const verification = subsection2 _text . appendChild ( document . createElement ( 'p' ) ) ;
if ( claim . status === 'verified' ) {
verification . innerHTML = ` Claim verification has completed. ` ;
subsection2 _icon . setAttribute ( 'src' , '/static/img/check-decagram.png' ) ;
} else {
verification . innerHTML = ` Claim verification is in progress… ` ;
return ;
}
elContent . appendChild ( document . createElement ( 'hr' ) ) ;
// Result of claim verification
const subsection3 = elContent . appendChild ( document . createElement ( 'div' ) ) ;
subsection3 . setAttribute ( 'class' , 'subsection' ) ;
const subsection3 _icon = subsection3 . appendChild ( document . createElement ( 'img' ) ) ;
subsection3 _icon . setAttribute ( 'src' , '/static/img/shield-search.png' ) ;
const subsection3 _text = subsection3 . appendChild ( document . createElement ( 'div' ) ) ;
const result = subsection3 _text . appendChild ( document . createElement ( 'p' ) ) ;
result . innerHTML = ` The claim <strong> ${ claim . verification . result ? 'HAS BEEN' : 'COULD NOT BE' } </strong> verified by the proof. ` ;
// Additional info
if ( claim . verification . proof . viaProxy ) {
elContent . appendChild ( document . createElement ( 'hr' ) ) ;
const subsection4 = elContent . appendChild ( document . createElement ( 'div' ) ) ;
subsection4 . setAttribute ( 'class' , 'subsection' ) ;
const subsection4 _icon = subsection4 . appendChild ( document . createElement ( 'img' ) ) ;
subsection4 _icon . setAttribute ( 'src' , '/static/img/information.png' ) ;
const subsection4 _text = subsection4 . appendChild ( document . createElement ( 'div' ) ) ;
const result _proxyUsed = subsection4 _text . appendChild ( document . createElement ( 'p' ) ) ;
result _proxyUsed . innerHTML = ` A proxy was used to fetch the proof: <a href="https://PLACEHOLDER__PROXY_HOSTNAME">PLACEHOLDER__PROXY_HOSTNAME</a> ` ;
}
// TODO Display errors
// if (claim.verification.errors.length > 0) {
// console.log(claim.verification);
// elContent.appendChild(document.createElement('hr'));
// const subsection5 = elContent.appendChild(document.createElement('div'));
// subsection5.setAttribute('class', 'subsection');
// const subsection5_icon = subsection5.appendChild(document.createElement('img'));
// subsection5_icon.setAttribute('src', '/static/img/alert-circle.png');
// const subsection5_text = subsection5.appendChild(document.createElement('div'));
// claim.verification.errors.forEach(message => {
// const error = subsection5_text.appendChild(document.createElement('p'));
// if (message instanceof Error) {
// error.innerText = message.message;
// } else {
// error.innerText = message;
// }
// });
// }
}
}
customElements . define ( 'kx-claim' , Claim ) ;