summaryrefslogtreecommitdiff
path: root/lib/urbit/wallet.ts
diff options
context:
space:
mode:
Diffstat (limited to 'lib/urbit/wallet.ts')
-rw-r--r--lib/urbit/wallet.ts79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/urbit/wallet.ts b/lib/urbit/wallet.ts
new file mode 100644
index 0000000..c29da48
--- /dev/null
+++ b/lib/urbit/wallet.ts
@@ -0,0 +1,79 @@
+import { generateWallet } from "urbit-key-generation";
+import * as ob from "urbit-ob";
+
+import {
+ GALAXY_ENTROPY_BITS,
+ MIN_PLANET,
+ MIN_STAR,
+ PLANET_ENTROPY_BITS,
+ STAR_ENTROPY_BITS,
+ SEED_ENTROPY_BITS,
+} from "./constants";
+import { shas } from "@/lib/utils/bit";
+
+const SEED_LENGTH_BYTES = SEED_ENTROPY_BITS / 8;
+const getTicketBitSize = (point: number) =>
+ point < MIN_STAR
+ ? GALAXY_ENTROPY_BITS
+ : point < MIN_PLANET
+ ? STAR_ENTROPY_BITS
+ : PLANET_ENTROPY_BITS;
+
+export const stripHexPrefix = (hex: string) => {
+ return hex.startsWith("0x") ? hex.slice(2) : hex;
+};
+export const makeDeterministicTicket = (point: number, seed: string) => {
+ const bits = getTicketBitSize(point);
+
+ const bytes = bits / 8;
+
+ const pointSalt = Buffer.concat([
+ Buffer.from(point.toString()),
+ Buffer.from("invites"),
+ ]);
+ const normalizedSeed = stripHexPrefix(seed);
+ const entropy = shas(Buffer.from(normalizedSeed, "hex"), pointSalt);
+
+ const buf = entropy.slice(0, bytes);
+ const patq = ob.hex2patq(buf.toString("hex"));
+ return patq;
+};
+
+// return a wallet object
+export const makeWalletFromP = async (patp: string, ticket: string) => {
+ const point = Number(ob.patp2dec(patp));
+ return await makeWallet({ point, ticket });
+};
+export const makeWallet = async (data: {
+ point: number;
+ ticket: string;
+ boot?: boolean;
+ revision?: number;
+}) => {
+ const config = {
+ ticket: data.ticket,
+ seedSize: SEED_LENGTH_BYTES,
+ point: data.point,
+ password: "",
+ revision: data.revision || 1,
+ boot: data.boot || false,
+ };
+
+ // This is here to notify anyone who opens console because the thread
+ // hangs, blocking UI updates so this cannot be done in the UI
+ console.log("Generating Wallet for point address: ", data.point);
+
+ const wallet = await generateWallet(config);
+ console.log({ wallet });
+
+ return wallet;
+ // return new Promise(async (resolve, reject) => {
+ // // Use a web worker to process the data
+ // try {
+ // const processed = await walletgenWorker.generate(JSON.stringify(config));
+ // resolve(processed);
+ // } catch (error) {
+ // reject(error);
+ // }
+ // });
+};