Skip to content

Commit

Permalink
Merge pull request #79 from cryptogarageinc/fix/add-verify-example
Browse files Browse the repository at this point in the history
feat: add verify example
  • Loading branch information
k-matsuzawa authored Jan 18, 2023
2 parents bb61ee4 + d3c6a2d commit bf20a3a
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 2 deletions.
3 changes: 2 additions & 1 deletion example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<li><a href="signwithprivkey_ecdsa.html">Sign with privkey</a> (Before SegWit v0)
<li><a href="signwithmnemonic_ecdsa.html">Sign with mnemonic</a> (Before SegWit v0)
<li><a href="verifysign.html">Verify Sign</a> (Before SegWit v0)
<li><a href="verifysignature.html">Verify ECDSA Signature</a>
<li><a href="verifysignature.html">Verify ECDSA Signature with transaction</a>
<li><a href="add_tx_sign.html">Add Tx Sign</a>
<li><a href="add_tx_multisig_sign.html">Add Tx Multisig Sign</a> (Before SegWit v0)
</ul>
Expand All @@ -54,6 +54,7 @@
<li><a href="tweak_privkey.html">Tweak Privkey</a>
<li><a href="tweak_pubkey.html">Tweak Pubkey</a>
<li><a href="sign_ecdsa.html">Sign ECDSA</a>
<li><a href="verifyecdsasignature.html">Verify ECDSA Signature</a>
<li><a href="verifyschnorrsignature.html">Verify Schnorr Signature</a>
</ul>
<li> for Message
Expand Down
77 changes: 77 additions & 0 deletions example/verifyecdsasignature.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Verify Signature</title>
<style>
#main {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 120vh;
font-family: sans-serif;
}
.decoderawtxInput {
width: 99%;
}
.input {
width: 99%;
}
.decodeOutput {
width: 99%;
}
.decode-output {
vertical-align: top;
margin-top: 13px;
}
h1 {
font-size: 32px;
}
h2 {
font-size: 20px;
font-weight: bold;
}
h3 {
font-size: 16px;
margin-bottom: 0px;
padding-bottom: 0px;
}
.xpubkey-label {
font-size: 12px;
display: inline-block;
width: 8em;
}
</style>
</head>
<body>
<div class="decode">
<h3 class="decode-title">Verify Signature</h3>
<div class="decode-input">
<div>
<!-- signature -->
Input signature hex:<br>
<textarea rows="2" name="signature" id="signature" class="input"></textarea><br>
<!-- key -->
PublicKey:<br>
<textarea rows="2" name="key" id="key" class="input"></textarea><br>
<!-- message -->
message:<br>
<textarea rows="2" name="message" id="message" class="input"></textarea><br>

<input type="button" name="decode" id="execDecode" value="Verify" />
</div>
<hr>
<div class="decode-output">
<div class="decode-label">Result:</div>
<textarea rows="4" id="decoded" class="decodeOutput" readonly></textarea>
</div>
</div>
</div>

<script src="/cfd-js-wasm/dist/cfdjs_wasm.js"></script>
<script src="/cfd-js-wasm/cfdjs_wasm_jsonapi.js"></script>
<script src="verifyecdsasignature.js"></script>
<p><a href="index.html">back</a></p>
</body>
</html>
67 changes: 67 additions & 0 deletions example/verifyecdsasignature.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const updateField = async function(event) {
const signature = document.getElementById("signature").value;
const pubkey = document.getElementById("key").value;
const message = document.getElementById("message").value;

try {
const req = {
message,
isHashed: true,
signature,
pubkey,
};
const resp = await callJsonApi(Module, 'VerifySignatureWithPubkey', req);
if (resp.success) {
decoded.value = 'verify success!';
return;
} else if (pubkey.length !== 130) {
decoded.value = 'verify fail. invalid signature.';
return;
}
} catch (e) {
decoded.value = 'Invalid format';
return;
}

// Retry with compressed public key
try {
const resp1 = await callJsonApi(Module, 'GetCompressedPubkey', {pubkey});
const req = {
message,
isHashed: true,
signature,
pubkey: resp1.pubkey,
};
const resp = await callJsonApi(Module, 'VerifySignatureWithPubkey', req);
if (resp.success) {
decoded.value = 'verify success! (Retry with compressed public key)';
} else {
decoded.value = 'verify fail. invalid signature.';
}
} catch (e) {
decoded.value = 'Invalid format';
}
}

const decodeBtnField = document.getElementById("execDecode");
decodeBtnField.addEventListener('click', updateField);

Module['onRuntimeInitialized'] = async function(){
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
console.log("exist cfdjsJsonApi.");
decoded.value = "";
} else {
console.log("cfdjsJsonApi not found!");
decoded.value = "WebAssembly load fail.";
}
}

window.onload = function() {
const decoded = document.getElementById("decoded");
if (Module['_cfdjsJsonApi']) {
decoded.value = "";
} else {
decoded.value = "WebAssembly loading...";
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit bf20a3a

Please sign in to comment.