Skip to content
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

Gateway requestor: support for sending digest #404

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 71 additions & 12 deletions core/src.ts/web/examples/gateway_requestor.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,19 @@ <h2>Using gateway</h2>
<p class="text-muted">Only for debugging. It's recommended to keep this as it is.</p>
</div>
<div class="mb-3">
<label class="form-label">Message to be signed with ECDSA/Keccak (hex-encoded)</label>
<input type="text" class="form-control" id="message" value="">
<label class="form-label">How would you like to sign the message</label>
<select class="form-select" id="signMethod" onchange="onChangeSelect(this);">
<option value="message" selected>EIP191</option>
<option value="digest">ECDSA</option>
</select>
</div>
<div class="mb-3" id="message-container">
<label class="form-label">Hex-encoded message to be signed with ECDSA/Keccak</label>
<input type="text" class="form-control" id="message">
</div>
<div class="mb-3" id="digest-container" style="display:none">
<label class="form-label">Hex-encoded 32-byte digest to be signed using plain ECDSA</label>
<input type="text" class="form-control" id="digest">
</div>
<button class="btn btn-primary" id="pairSignBtn" onclick="btnSignMessageClicked();">Pair and request to sign with key #1</button>
<button class="btn btn-secondary" id="signBtn" onclick="btnInitiateSessClicked();">Request another signature with key #1</button>
Expand All @@ -63,23 +74,69 @@ <h2>Using gateway</h2>
<div id="server-ver"></div>

<script type="text/javascript">
// generate random message on load
let gate = null;
// generate random messages on load
generateRandomMessage();
generateRandomDigest();

let rnd = new Uint8Array(6);
crypto.getRandomValues(rnd);
document.getElementById('message').value = arr2hex(rnd);
document.getElementById('signBtn').disabled = true;
document.getElementById('signBtn').style.display = 'none';

let cmd = {"name": "sign", "message": document.getElementById('message').value, "keyNo": 1};
let gate = null;

function log(data) {
console.log(data);
document.getElementById('statusText').innerText += '\n' + data;
}

function onChangeSelect(selectElement) {
const val = selectElement.value;
if (val === "message") {
document.getElementById('message-container').style.display = 'block';
document.getElementById('digest-container').style.display = 'none';
} else {
document.getElementById('message-container').style.display = 'none';
document.getElementById('digest-container').style.display = 'block';
}
}

function generateRandomMessage() {
const rnd = new Uint8Array(6);
crypto.getRandomValues(rnd);
document.getElementById('message').value = arr2hex(rnd);
document.getElementById('signBtn').disabled = true;
document.getElementById('signBtn').style.display = 'none';
}

async function generateRandomDigest() {
const rnd = new Uint8Array(6);
crypto.getRandomValues(rnd);

const msgBuffer = new TextEncoder().encode(rnd);
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
const hashArray = Array.from(new Uint8Array(hashBuffer));

const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join('');

document.getElementById('digest').value = hashHex;
document.getElementById('signBtn').disabled = true;
document.getElementById('signBtn').style.display = 'none';
}
Comment on lines +106 to +119
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a note, any random 32 byte hex string would be a valid SHA-256 hash. Of course then we wouldn't know what was the original input that was hashed, but it's also not necessary for this demo.

Nevertheless, I like the approach of generating a random message and then hashing it on the client side, it's a bit more descriptive. LGTM 👍


function generateCmd() {
const name = "sign";
const signMethod = document.getElementById('signMethod').value;
const message = document.getElementById('message').value;
const digest = document.getElementById('digest').value;

let cmd = {};
if (signMethod === "message") {
cmd = {name, message, "keyNo": 1};
} else {
cmd = {name, digest, "keyNo": 1};
}

return cmd;
}

async function btnSignMessageClicked() {
const cmd = generateCmd();

document.getElementById('pairSignBtn').disabled = true;
gate = new HaloGateway(document.getElementById('gatewayURL').value);

Expand Down Expand Up @@ -124,6 +181,8 @@ <h2>Using gateway</h2>
document.getElementById('signBtn').disabled = true;
log('Requested to execute a command. Please click [Confirm] on your smartphone and tap your HaLo tag.');

const cmd = generateCmd();

try {
let res = await gate.execHaloCmd(cmd);
log('Command completed. Result: ' + JSON.stringify(res));
Expand Down