forked from jeasonstudio/runestone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecipher.html
62 lines (57 loc) · 1.58 KB
/
decipher.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Decipher Runes</title>
<style>
#input {
width: 500px;
}
</style>
</head>
<body>
<h1>Please input the transaction hash</h1>
<input id="input" />
<button onclick="decipher()">decipher message</button>
<h1>Results</h1>
<div id="content"></div>
</body>
<script type="module">
import { Runestone } from "https://esm.sh/@ordjs/runestone/bundle";
async function getTx(txid) {
const tx = await fetch(`https://mempool.space/api/tx/${txid}`).then(
(res) => res.json()
);
return tx;
}
function getOutputTx(tx) {
const output = tx.vout.map((o) =>
replacePropertyName(o, "scriptpubkey", "script_pubkey")
);
return {
output,
};
}
function replacePropertyName(obj, oldName, newName) {
if (!obj.hasOwnProperty(oldName)) return;
let newObj = { ...obj };
newObj[newName] = newObj[oldName];
delete newObj[oldName];
return newObj;
}
async function decipher() {
document.getElementById("content").innerText = "waiting...";
const txid = document.getElementById("input").value;
const tx = await getTx(txid);
const outputTx = getOutputTx(tx);
try {
const runestone = Runestone.decipher(outputTx);
document.getElementById("content").innerText =
JSON.stringify(runestone);
} catch {
document.getElementById("content").innerText = "error";
}
}
window.decipher = decipher;
</script>
</html>