diff options
author | polwex <polwex@sortug.com> | 2025-07-16 11:59:08 +0700 |
---|---|---|
committer | polwex <polwex@sortug.com> | 2025-07-16 11:59:08 +0700 |
commit | 9c2fba56e0f68f976c1abe486f9fd3c6e93b437e (patch) | |
tree | 22470efb92a9f1b673136d7c0bdadc06abb3ba35 /lib/urbit/wallet.ts | |
parent | 8ae0d2779c26f74e64a1db2b028bd2ac2f599cb4 (diff) |
'm'
Diffstat (limited to 'lib/urbit/wallet.ts')
-rw-r--r-- | lib/urbit/wallet.ts | 79 |
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); + // } + // }); +}; |