JSFiddle - React, Tailwind, and code Playground
HTML
<h3>
DER Signature Deconstructor
</h3>
<p>
Use this to deconstruct DER signatures to make them easier to read. <br><br><a id="example">Click for an example</a>
</p>
<input id="sig">
<button id="sigsubmit">
Deconstruct
</button>
<pre><code id="der"></code><div id="err"></div></pre>
CSS
body {
font-family: "Courier New", Courier, monospace;
background:#121212;
color:#fff;
text-align: center;
}
input {
width:200px;
height:30px;
padding:0px 10px;
margin:0px;
border: 0px none;
}
button {
width:120px;
height: 30px;
margin:0px;
border:0px none;
background:#999;
}
button:hover {
cursor: pointer;
background:#ccc;
}
p {
text-align:center;
color:#999;
}
a {
color:#fff;
text-decoration: underline;
}
pre {
background:#000;
color:#21e800;
border-radius:10px;
margin:50px auto 0px auto;
width:720px;
padding:50px;
text-align: left;
}
#err {
text-align: center;
color:red;
}
JavaScript
const der = document.getElementById("der");
const err = document.getElementById("err");
const siginput = document.getElementById("sig");
document.getElementById("sigsubmit").addEventListener("click", deconstruct,false);
document.getElementById("example").addEventListener("click", example,false);
function example(){
siginput.value = '3044022038febfa206b2d1d03b91f780228cf652d166892ccdc29cf250546fd407969eb9022029b2f33c11da681cbe425fc623e8a5d6fa48f0e6155f0ce0b837edeb24f0755641';
deconstruct();
}
function deconstruct(){
clearErrors();
let sig = siginput.value;
sig = sig.replace('[ALL|FORKID]', '41');
sig = sig.replace('[SINGLE|FORKID]', '43');
sig = sig.replace('[NONE|FORKID]', '42');
sig = sig.replace('[ANYONECANPAY|FORKID]', 'C0');
console.log(sig)
if(sig.length%2!==0){
return showError("Invalid signature length");
}
if(!sig.match(/[0-9a-fA-F]/gm)){
return showError("Not a valid hex string");
}
bytes = sig.match(/[0-9a-fA-F]{2}/gm);
let sigobj = {}
sigobj.seq = bytes.shift(),
sigobj.length = bytes.shift(),
sigobj.int = bytes.shift(),
sigobj.r_length = bytes.shift(),
sigobj.r = bytes.splice(0,parseInt('0x'+sigobj.r_length)).join('');
sigobj.int2 = bytes.shift(),
sigobj.s_length = bytes.shift(),
sigobj.s = bytes.splice(0,parseInt('0x'+sigobj.s_length)).join('');
if(bytes.length){
sigobj.sighash = bytes.join('');
}
der.innerHTML = JSON.stringify(sigobj, undefined, 2);
}
function showError(e){
der.innerHTML = '';
err.innerHTML = e;
}
function clearErrors(){
err.innerHTML = "";
}