Commit d4884694 authored by taco@waag.org's avatar taco@waag.org
Browse files

moved some stuff to library function

parent 18dbba93
......@@ -6,6 +6,7 @@
<title>IRMA web frontend example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="node_modules/@privacybydesign/irma-frontend/dist/irma.js"></script>
<script type="text/javascript" src="pop.js"></script>
<style>
html, body {
......@@ -28,9 +29,7 @@
</head>
<body>
<!-- section class="irma-web-form" id="irma-web-form"></section-- >
<!-- button id="abort-web">Abort web element</button -->
<textarea id="message" rows="5" cols="80">Groetjes van <b>Taco!</b></textarea>
<textarea id="message" rows="5" cols="80">Replace this with your <b>own</b> text!</textarea>
<button id="start-popup">Sign message</button>
<textarea id="result" rows="5" cols="80"></textarea>
</body>
......@@ -38,66 +37,24 @@
<script type="text/javascript">
document.getElementById('start-popup').onclick = () => {
//normalize content
var div = document.createElement("div");
div.innerHTML = document.getElementById("message").value;
let text = div.textContent || div.innerText || "";
document.getElementById("message").value = text;
let options = {
// Developer options
debugging: true,
// Front-end options
language: 'en',
translations: {
header: 'POP <i class="irma-web-logo">IRMA</i> signing example',
loading: 'Just one second please!'
},
// Back-end options
session: {
// Point this to your IRMA server:
url: 'https://irma.snotolf.waag.org',
// Define your disclosure request:
start: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"@context": "https://irma.app/ld/request/signature/v2",
"message" : document.getElementById("message").value,
"disclose": [
[
[ "pbdf.pbdf.email.email" ],
[ "pbdf.sidn-pbdf.email.email" ],
]
]
})
}
}
};
let irmaPopup = irma.newPopup(options);
irmaPopup.start()
.then(result => {
console.log("Successful disclosure! 🎉", result)
var signature = result.signature;
//strip message from signature
signature.message = undefined;
document.getElementById("result").value = btoa(JSON.stringify(signature));
})
.catch(error => {
if (error === 'Aborted') {
console.log('We closed it ourselves, so no problem 😅');
return;
//1. normalize using pop library function
let normalized_text = pop_normalize(document.getElementById("message").value);
document.getElementById("message").value = normalized_text;
//2. sign the normalized text
pop_sign("https://irma.snotolf.waag.org",
"pbdf.sidn-pbdf.email.email",
normalized_text)
.then( result =>{
//3. show the result
document.getElementById("result").value = result;
}).catch(error => {
if (error === 'Aborted') {
console.log('We closed it ourselves, so no problem 😅');
return;
}
console.error("Couldn't do what you asked 😢", error);
})
.finally(() => irmaPopup = irma.newPopup(options));
};
</script>
......
//normalize a text by stripping all tags
//using the browser textContent function
function pop_normalize(raw){
var div = document.createElement("div");
div.innerHTML = raw;
let text = div.textContent || div.innerText || "";
return text;
}
//calls irma server specified in url
//to sign the message, using the (disclosed) attribute name
function pop_sign(url, attribute, normalized_message){
let options = {
// Developer options
debugging: true,
language: 'nl',
translations: {
header: 'POP <i class="irma-web-logo">IRMA</i> signing example',
loading: 'Just one second please!'
},
session: {
url: url,
start: {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"@context": "https://irma.app/ld/request/signature/v2",
"message" : normalized_message,
"disclose": [
[
[ attribute ]
]
]
})
}
}
};
let irmaPopup = irma.newPopup(options);
//return the promise
return irmaPopup.start().then(result => {
//strip and encode the signature
var signature = result.signature;
signature.message = undefined;
return btoa(JSON.stringify(signature));
})
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment