Skip to content

Commit

Permalink
Please work
Browse files Browse the repository at this point in the history
  • Loading branch information
Rigidity committed Aug 28, 2024
1 parent f485912 commit 80f10b1
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 7 deletions.
5 changes: 4 additions & 1 deletion astro.config.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { defineConfig } from "astro/config";

import react from "@astrojs/react";

// https://astro.build/config
export default defineConfig({
site: "https://rigidity.github.io",
base: "chia-encoder-util",
});
integrations: [react()]
});
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
"astro": "astro"
},
"dependencies": {
"@astrojs/react": "^3.6.2",
"@types/react": "^18.3.4",
"@types/react-dom": "^18.3.0",
"astro": "^4.14.6",
"bech32": "^2.0.0",
"chia-bls": "^1.0.2"
"chia-bls": "^1.0.2",
"react": "^18.3.1",
"react-dom": "^18.3.1"
}
}
145 changes: 145 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions src/components/Main.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { bech32m } from "bech32";
import bls from "chia-bls";
import { useState } from "react";

const { AugSchemeMPL, JacobianPoint } = bls;

export default function Main() {
const [m, setM] = useState("");

const trimmed = m.trim();
const result = trimmed.length > 0 ? calc(trimmed) : null;

return (
<>
<h1 style={{ fontSize: 40 }}>BLS Public Key Encoder</h1>
<input value={m} onChange={(e) => setM(e.target.value)}></input>
<p style={{ width: 700, wordWrap: "break-word" }}>
{m.trim().length > 0
? result ?? "Invalid master public key"
: "Enter your master public key"}
</p>
<p style={{ fontSize: 20 }}>
This converts a master public key to the first derivation encoded with
the bls12381 prefix.
</p>
</>
);
}

function calc(hex) {
try {
const key = JacobianPoint.fromHexG1(hex.replace("0x", ""));
const address = toAddress(
AugSchemeMPL.deriveChildPkUnhardened(key, 0).toBytes(),
"bls1238"
);
return address;
} catch (e) {
console.error(e);
return null;
}
}

function toAddress(hash, prefix) {
return bech32m.encode(prefix, convertBits(hash, 8, 5, true), Infinity);
}

function addressInfo(address) {
const { words, prefix } = bech32m.decode(address);
return {
hash: convertBits(Uint8Array.from(words), 5, 8, false),
prefix,
};
}

function convertBits(bytes, from, to, pad) {
let accumulate = 0;
let bits = 0;
let maxv = (1 << to) - 1;
let result = [];
for (const value of bytes) {
const b = value & 0xff;
if (b < 0 || b >> from > 0) throw new Error("Could not convert bits.");
accumulate = (accumulate << from) | b;
bits += from;
while (bits >= to) {
bits -= to;
result.push((accumulate >> bits) & maxv);
}
}
if (pad && bits > 0) {
result.push((accumulate << (to - bits)) & maxv);
} else if (bits >= from || ((accumulate << (to - bits)) & maxv) !== 0) {
throw new Error("Could not convert bits.");
}
return Uint8Array.from(result);
}
Loading

0 comments on commit 80f10b1

Please sign in to comment.