Skip to content

Instantly share code, notes, and snippets.

@WietseWind
Created June 20, 2024 10:54
Show Gist options
  • Save WietseWind/7de5487e98ad9cfdf469f78aa7ea8c72 to your computer and use it in GitHub Desktop.
Save WietseWind/7de5487e98ad9cfdf469f78aa7ea8c72 to your computer and use it in GitHub Desktop.
Xahau Voucher state
import { getPubKey, xflHexToString } from "./statelib.mjs";
const main = async () => {
console.log(getPubKey(process.argv[2]));
const options = {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
method: "ledger_entry",
params: [
{
hook_state: {
account: "rvoucheredGC1mB4yd2CBjs7jGMRTLexe",
namespace_id:
"5B85492F93FF99C3D72A121F2BF28BC9858EE7209AC3DA5C1079B529F4A6CAF0",
key: getPubKey(process.argv[2]),
},
},
],
}),
};
const url = "https://xahau.network";
const response = await fetch(url, options);
const { result } = await response.json();
console.log(
JSON.stringify(
{ amount: Number(xflHexToString(result.node.HookStateData)) },
null,
"\t"
)
);
if (result.error) return 0; // Voucher might have been claimed or it's invalid
return { amount: Number(xflHexToString(result.node.HookStateData)) };
};
main();
import { derive } from "xrpl-accountlib";
export function getPubKey(privateKey) {
const { keypair } = derive.privatekey(privateKey);
if (typeof keypair.publicKey === "string") {
const res = keypair.publicKey;
return res.slice(2);
}
}
export function xflHexToString(hex) {
const buffer = Buffer.from(hexToBytes(hex));
const integerValue = BigInt(buffer.readBigInt64LE());
const stringValue = to_string(integerValue);
return stringValue === "<zero>" ? "0" : stringValue;
}
const hexToBytes = (hexString) => {
if (hexString.length % 2 !== 0) {
throw new Error("Hex string length must be even");
}
const byteArray = [];
for (let i = 0; i < hexString.length; i += 2) {
byteArray.push(parseInt(hexString.substr(i, 2), 16));
}
return byteArray;
};
function get_exponent(xfl) {
if (xfl < 0n) throw "Invalid XFL";
if (xfl == 0n) return 0n;
return ((xfl >> 54n) & 0xffn) - 97n;
}
function get_mantissa(xfl) {
if (xfl < 0n) throw "Invalid XFL";
if (xfl == 0n) return 0n;
return xfl - ((xfl >> 54n) << 54n);
}
function is_negative(xfl) {
if (xfl < 0n) throw "Invalid XFL";
if (xfl == 0n) return false;
return ((xfl >> 62n) & 1n) == 0n;
}
const to_string = (xfl) => {
if (xfl < 0n) throw "Invalid XFL";
if (xfl == 0n) return "<zero>";
return `${is_negative(xfl) ? "-" : ""}${get_mantissa(xfl)}e${get_exponent(
xfl
)}`;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment