forked from p2pderivatives/cfd-js-wasm
-
Notifications
You must be signed in to change notification settings - Fork 1
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 #79 from cryptogarageinc/fix/add-verify-example
feat: add verify example
- Loading branch information
Showing
4 changed files
with
147 additions
and
2 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
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> |
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 @@ | ||
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..."; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.