Skip to content

Instantly share code, notes, and snippets.

@Davidson-Souza
Last active August 16, 2023 15:16
Show Gist options
  • Save Davidson-Souza/23c54da5af82a21473219a3f380a1eed to your computer and use it in GitHub Desktop.
Save Davidson-Souza/23c54da5af82a21473219a3f380a1eed to your computer and use it in GitHub Desktop.
This is a super simple example of verifying the validity of a utreexo on the web.
<html>
<head>
<meta content="text/html;charset=utf-8" http-equiv="Content-Type" />
</head>
<body>
<!-- Note the usage of `type=module` here as this is an ES6 module -->
<script src="mock_data.js"></script>
<script type="module" src="main.js"></script>
<p> Don't try to foo me sending and invalid UTXO, I have the Utreexo powers!</p>
</body>
</html>
/**
* Utreexo allows efficient proving of arbitrary UTXOs. This **super simple** example show a basic workflow doing this.
* Building the acc from scratch by going over each block is likely intractable for browsers. So an application may hand
* the acc, the user may fetch them from a trusted source (e.g their own node) or you can use a zerosync like scheme to get it
* going. Once you have the roots, you can download all blocks and update your acc by yourself. Shouldn't be too much work.
*
* If someone claims to have a UTXO, you can ask for a utreexo proof, and validate it against your own accumulator.
*/
import init, { Stump, LeafData, Proof } from './pkg/rustreexo.js';
async function run() {
await init();
const stump = new Stump();
stump.leaves = BigInt(acc.data.leaves);
stump.roots = acc.data.roots.map(hex_decode);
const hashes = proof_data.data.proof.hashes.map(hex_decode);
const targets = proof_data.data.proof.targets.map(BigInt);
const proof = new Proof([targets], [hashes]);
const leaf = new LeafData(
hex_decode("0000014aa19ec158e642f2e4a97ee781bf820f4b03ea1765a8887b47c0178910"),
hex_decode("f18671b7ba1a07ed0dc22559a41f988297cbc090248102762ce0142823b2c743"),
1,
10000,
hex_decode("a914ab5ea27696874f5e0ebb5832f12eafb8af47ca5187"),
)
console.log("Proof", stump.verify(proof, [leaf]));
}
// TODO: Update the accumulator and proof as new blocks come in
function hex_decode(string) {
if (!string || !string.replace) return new Uint8Array(0);
let bytes = [];
string.replace(/../g, function (pair) {
bytes.push(parseInt(pair, 16));
});
return new Uint8Array(bytes);
}
run().catch(console.error);
// Mock data for signet tx f18671b7ba1a07ed0dc22559a41f988297cbc090248102762ce0142823b2c743
// you can get this data from http://api.dlsouza.lol/acc and
// http://api.dlsouza.lol/tx/{txid}/outputs
const proof_data = {
"data": {
"proof": {
"hashes": [
"e2cb153ad034af6a8efc9f887aa4a08ec8337152d63d851668b1812ce9dd58ab",
"eeca1cb00d945671ada489f97e6c896df45b11be6ea8a09ec2557d116a6d0432",
"6eb02e27694cdf38ef411cc3b673aa9f3e79cf49f7440999b849e12c895ed4f7"
],
"targets": [
3581285
]
},
"tx": {
"input": [
{
"previous_output": "194c920fda2a879a6a130b04b1d3fa0546f06f0372df6ab1f420b2418eb30be7:0",
"script_sig": "16001401eba32ab659fb3d304d280ba7cde437ffe25ccb",
"sequence": 4294967294,
"witness": [
"30440220790271cf298d72cd7491b2418bb2d78d8a26a8d028af03793d8a0b4d8c49e80702203c0bb047498865c56fbd9a41f75b2b4d6f98fa524f2a7820f52822fb5eed444b01",
"02b221cd9de92b4aecfa21ac69c97c989f0dded15b99e665b27efbcbeea4e561f4"
]
}
],
"lock_time": 156378,
"output": [
{
"script_pubkey": "a914c1c5e56aa9048144d8d2fb2fa99d7c3fcbf4fc0987",
"value": 966027
},
{
"script_pubkey": "a914ab5ea27696874f5e0ebb5832f12eafb8af47ca5187",
"value": 10000
}
],
"version": 2
}
},
"error": null
}
const acc = {
"data":
{
"leaves": 3581290,
"roots": [
"c3e31033dba3957ffee968e8baa9976078b32e944d014e012f89aaa76826208e",
"0143ee36351e8264b8d3fd64e45a1376bbd459f0add231f53bf817e165b7c5cd",
"8ca430dd51625ccced5ddf1fcee9a5eb445096c3c2f9664bd7f14f7b5097e298",
"871f8ced3d93a83f5b6946e1643ff40eb6a7c3b95cb5997d4207378b3a0a82cb",
"91442587124af8a125289691205b0b4ccf7daadde5babd07bdf70ba2234a76f5",
"5e9d00d2e02320c45efba237b7eac582b8f6e0453554de0ded67767db1e6f71b",
"1ed22d41978e3028e007a946f6f76764d32a61dfd7049eb85f17450ae733cd17",
"edbe758d5213333887a804a56f4c2c2592e42f7a7ead709229e645fdb45eb19a",
"e81e7113b78041bac6451b80e6a9fc9f2392339ece0bff88a96f9529848c68c8",
"1479f229567abedd600a32e2135647af331742be7e24057a56e4a87e814540ae",
"cab54f83c3051482b162b823bbe01f54c73d1f9460dc07b073f7f3c86a12b97e",
"33a2d2f654e02d41f063df26ad344230623348b6acce3ec0f6f3f30bea0e1720"
]
},
"error": null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment