mirror of
https://github.com/aljazceru/mutiny-web.git
synced 2025-12-24 01:24:28 +01:00
add lnurlauth scanning
This commit is contained in:
@@ -22,7 +22,6 @@ const settingsRoutes = [
|
|||||||
"/emergencykit",
|
"/emergencykit",
|
||||||
"/encrypt",
|
"/encrypt",
|
||||||
"/gift",
|
"/gift",
|
||||||
"/lnurlauth",
|
|
||||||
"/plus",
|
"/plus",
|
||||||
"/restore",
|
"/restore",
|
||||||
"/servers",
|
"/servers",
|
||||||
@@ -116,10 +115,6 @@ test("visit each route", async ({ page }) => {
|
|||||||
);
|
);
|
||||||
await page.goBack();
|
await page.goBack();
|
||||||
|
|
||||||
// LNURL Auth
|
|
||||||
await checkRoute(page, "/settings/lnurlauth", "LNURL Auth", checklist);
|
|
||||||
await page.goBack();
|
|
||||||
|
|
||||||
// Sync Nostr Contacts
|
// Sync Nostr Contacts
|
||||||
await checkRoute(
|
await checkRoute(
|
||||||
page,
|
page,
|
||||||
|
|||||||
147
src/components/HomePrompt.tsx
Normal file
147
src/components/HomePrompt.tsx
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
import { useSearchParams } from "@solidjs/router";
|
||||||
|
import { createEffect, createSignal, Show } from "solid-js";
|
||||||
|
|
||||||
|
import { Button, InfoBox, NiceP, SimpleDialog, VStack } from "~/components";
|
||||||
|
import { useI18n } from "~/i18n/context";
|
||||||
|
import { useMegaStore } from "~/state/megaStore";
|
||||||
|
import { bech32, bech32WordsToUrl, eify } from "~/utils";
|
||||||
|
|
||||||
|
const ImageWithFallback = (props: { src: string; alt: string }) => {
|
||||||
|
const [hasError, setHasError] = createSignal(false);
|
||||||
|
|
||||||
|
const handleError = () => {
|
||||||
|
setHasError(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<img
|
||||||
|
src={props.src}
|
||||||
|
alt={props.alt}
|
||||||
|
onError={handleError}
|
||||||
|
class="h-4 w-4 rounded-sm"
|
||||||
|
classList={{
|
||||||
|
hidden: hasError()
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export function HomePrompt() {
|
||||||
|
const [state, _actions] = useMegaStore();
|
||||||
|
const i18n = useI18n();
|
||||||
|
|
||||||
|
const [params, setParams] = useSearchParams();
|
||||||
|
|
||||||
|
// When we have a nice result we can head over to the send screen
|
||||||
|
createEffect(() => {
|
||||||
|
if (params.lnurlauth) {
|
||||||
|
const lnurlauth = params.lnurlauth;
|
||||||
|
setParams({ lnurlauth: undefined });
|
||||||
|
setLnurlauthResult(lnurlauth);
|
||||||
|
try {
|
||||||
|
const decodedLnurl = bech32.decode(lnurlauth, 1023);
|
||||||
|
const url = bech32WordsToUrl(decodedLnurl.words);
|
||||||
|
const domain = new URL(url).hostname;
|
||||||
|
setLnurlDomain(domain);
|
||||||
|
} catch (e) {
|
||||||
|
// We care about this error the domain is just to make it pretty
|
||||||
|
console.error(e);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (params.fedimint_invite) {
|
||||||
|
const fedimint_invite = params.fedimint_invite;
|
||||||
|
setParams({ fedimint_invite: undefined });
|
||||||
|
setFedi(fedimint_invite);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fedi stuff
|
||||||
|
// fed11qgqzc2nhwden5te0vejkg6tdd9h8gepwvejkg6tdd9h8garhduhx6at5d9h8jmn9wshxxmmd9uqqzgxg6s3evnr6m9zdxr6hxkdkukexpcs3mn7mj3g5pc5dfh63l4tj6g9zk4er
|
||||||
|
const [fedi, setFedi] = createSignal<string>();
|
||||||
|
|
||||||
|
// Lnurl Auth stuff
|
||||||
|
const [lnurlauthResult, setLnurlauthResult] = createSignal<string>();
|
||||||
|
const [authLoading, setAuthLoading] = createSignal<boolean>(false);
|
||||||
|
const [isAuthenticated, setIsAuthenticated] = createSignal<boolean>(false);
|
||||||
|
const [authError, setAuthError] = createSignal<Error | undefined>(
|
||||||
|
undefined
|
||||||
|
);
|
||||||
|
const [lnurlDomain, setLnurlDomain] = createSignal<string>("");
|
||||||
|
|
||||||
|
async function handleLnurlAuth() {
|
||||||
|
setAuthLoading(true);
|
||||||
|
try {
|
||||||
|
await state.mutiny_wallet?.lnurl_auth(lnurlauthResult()!);
|
||||||
|
setIsAuthenticated(true);
|
||||||
|
} catch (e) {
|
||||||
|
// lnurl1dp68gurn8ghj7um5v93kketj9ehx2amn9ashq6f0d3hxzat5dqlhgct884kx7emfdcnxkvfavvurwdtrvgmkyd3489skgcfexqckxd3svg6xgwr98q6nsd3c893kzcfkvc6nsdr9xpjxvc3jvejrxwpevyurqvfev3nxvvnxx5ergdc8g6gzl
|
||||||
|
console.error(e);
|
||||||
|
setAuthError(eify(e));
|
||||||
|
} finally {
|
||||||
|
setAuthLoading(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<SimpleDialog
|
||||||
|
title={i18n.t("modals.fedi_invite.title")}
|
||||||
|
open={!!fedi()}
|
||||||
|
setOpen={() => setFedi(undefined)}
|
||||||
|
>
|
||||||
|
<NiceP>{i18n.t("modals.fedi_invite.description")}</NiceP>
|
||||||
|
</SimpleDialog>
|
||||||
|
<SimpleDialog
|
||||||
|
title={i18n.t("modals.lnurl_auth.auth_request")}
|
||||||
|
open={!!lnurlauthResult()}
|
||||||
|
setOpen={(open) => {
|
||||||
|
if (!open) setLnurlauthResult(undefined);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<Show when={lnurlDomain()}>
|
||||||
|
<div class="flex w-full items-center justify-center gap-2 p-2">
|
||||||
|
<ImageWithFallback
|
||||||
|
src={`https://${lnurlDomain()}/favicon.ico`}
|
||||||
|
alt="Favicon"
|
||||||
|
/>
|
||||||
|
<pre class="text-center">{lnurlDomain()}</pre>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
<Show when={!isAuthenticated()}>
|
||||||
|
<VStack>
|
||||||
|
<Button
|
||||||
|
loading={authLoading()}
|
||||||
|
intent="blue"
|
||||||
|
onClick={handleLnurlAuth}
|
||||||
|
>
|
||||||
|
{i18n.t("modals.lnurl_auth.login")}
|
||||||
|
</Button>
|
||||||
|
<Button
|
||||||
|
intent="red"
|
||||||
|
onClick={() => setLnurlauthResult(undefined)}
|
||||||
|
>
|
||||||
|
{i18n.t("modals.lnurl_auth.decline")}
|
||||||
|
</Button>
|
||||||
|
</VStack>
|
||||||
|
</Show>
|
||||||
|
<Show when={isAuthenticated()}>
|
||||||
|
<div class="flex flex-col items-center">
|
||||||
|
<div class="rounded bg-m-grey-950 px-2 py-1 text-center">
|
||||||
|
<NiceP>
|
||||||
|
{i18n.t("modals.lnurl_auth.authenticated")}
|
||||||
|
</NiceP>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</Show>
|
||||||
|
<Show when={authError()}>
|
||||||
|
<InfoBox accent="red">
|
||||||
|
{i18n.t("modals.lnurl_auth.error")}
|
||||||
|
</InfoBox>
|
||||||
|
</Show>
|
||||||
|
</SimpleDialog>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -46,3 +46,4 @@ export * from "./GiftLink";
|
|||||||
export * from "./MutinyPlusCta";
|
export * from "./MutinyPlusCta";
|
||||||
export * from "./ToggleHodl";
|
export * from "./ToggleHodl";
|
||||||
export * from "./IOSbanner";
|
export * from "./IOSbanner";
|
||||||
|
export * from "./HomePrompt";
|
||||||
|
|||||||
@@ -703,6 +703,17 @@ export default {
|
|||||||
are_you_sure: "Are you sure?",
|
are_you_sure: "Are you sure?",
|
||||||
cancel: "Cancel",
|
cancel: "Cancel",
|
||||||
confirm: "Confirm"
|
confirm: "Confirm"
|
||||||
|
},
|
||||||
|
lnurl_auth: {
|
||||||
|
auth_request: "Authentication Request",
|
||||||
|
login: "Login",
|
||||||
|
decline: "Decline",
|
||||||
|
error: "That didn't work for some reason.",
|
||||||
|
authenticated: "Authenticated!"
|
||||||
|
},
|
||||||
|
fedi_invite: {
|
||||||
|
title: "Fedimint Invite",
|
||||||
|
description: "Fedimint support coming soon!"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ export type ParsedParams = {
|
|||||||
lnurl?: string;
|
lnurl?: string;
|
||||||
lightning_address?: string;
|
lightning_address?: string;
|
||||||
nostr_wallet_auth?: string;
|
nostr_wallet_auth?: string;
|
||||||
|
fedimint_invite?: string;
|
||||||
|
is_lnurl_auth?: boolean;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function toParsedParams(
|
export function toParsedParams(
|
||||||
@@ -58,7 +60,9 @@ export function toParsedParams(
|
|||||||
node_pubkey: params.node_pubkey,
|
node_pubkey: params.node_pubkey,
|
||||||
lnurl: params.lnurl,
|
lnurl: params.lnurl,
|
||||||
lightning_address: params.lightning_address,
|
lightning_address: params.lightning_address,
|
||||||
nostr_wallet_auth: params.nostr_wallet_auth
|
nostr_wallet_auth: params.nostr_wallet_auth,
|
||||||
|
is_lnurl_auth: params.is_lnurl_auth,
|
||||||
|
fedimint_invite: params.fedimint_invite_code
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import {
|
|||||||
EmergencyKit,
|
EmergencyKit,
|
||||||
Encrypt,
|
Encrypt,
|
||||||
Gift,
|
Gift,
|
||||||
LnUrlAuth,
|
|
||||||
Plus,
|
Plus,
|
||||||
Restore,
|
Restore,
|
||||||
Servers,
|
Servers,
|
||||||
@@ -112,7 +111,6 @@ export function Router() {
|
|||||||
<Route path="/emergencykit" component={EmergencyKit} />
|
<Route path="/emergencykit" component={EmergencyKit} />
|
||||||
<Route path="/encrypt" component={Encrypt} />
|
<Route path="/encrypt" component={Encrypt} />
|
||||||
<Route path="/gift" component={Gift} />
|
<Route path="/gift" component={Gift} />
|
||||||
<Route path="/lnurlauth" component={LnUrlAuth} />
|
|
||||||
<Route path="/plus" component={Plus} />
|
<Route path="/plus" component={Plus} />
|
||||||
<Route path="/restore" component={Restore} />
|
<Route path="/restore" component={Restore} />
|
||||||
<Route path="/servers" component={Servers} />
|
<Route path="/servers" component={Servers} />
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
CombinedActivity,
|
CombinedActivity,
|
||||||
DecryptDialog,
|
DecryptDialog,
|
||||||
DefaultMain,
|
DefaultMain,
|
||||||
|
HomePrompt,
|
||||||
IOSbanner,
|
IOSbanner,
|
||||||
LoadingIndicator,
|
LoadingIndicator,
|
||||||
LoadingShimmer,
|
LoadingShimmer,
|
||||||
@@ -106,6 +107,7 @@ export function Main() {
|
|||||||
</DefaultMain>
|
</DefaultMain>
|
||||||
<DecryptDialog />
|
<DecryptDialog />
|
||||||
<BetaWarningModal />
|
<BetaWarningModal />
|
||||||
|
<HomePrompt />
|
||||||
<NavBar activeTab="home" />
|
<NavBar activeTab="home" />
|
||||||
</SafeArea>
|
</SafeArea>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,77 +0,0 @@
|
|||||||
import { TextField } from "@kobalte/core";
|
|
||||||
import { createSignal } from "solid-js";
|
|
||||||
|
|
||||||
import {
|
|
||||||
BackLink,
|
|
||||||
Button,
|
|
||||||
DefaultMain,
|
|
||||||
InnerCard,
|
|
||||||
LargeHeader,
|
|
||||||
MutinyWalletGuard,
|
|
||||||
NavBar,
|
|
||||||
SafeArea
|
|
||||||
} from "~/components";
|
|
||||||
import { useI18n } from "~/i18n/context";
|
|
||||||
import { useMegaStore } from "~/state/megaStore";
|
|
||||||
|
|
||||||
export function LnUrlAuth() {
|
|
||||||
const i18n = useI18n();
|
|
||||||
const [state, _] = useMegaStore();
|
|
||||||
|
|
||||||
const [value, setValue] = createSignal("");
|
|
||||||
|
|
||||||
const onSubmit = async (e: SubmitEvent) => {
|
|
||||||
e.preventDefault();
|
|
||||||
|
|
||||||
const lnurl = value().trim();
|
|
||||||
await state.mutiny_wallet?.lnurl_auth(lnurl);
|
|
||||||
|
|
||||||
setValue("");
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
|
||||||
<MutinyWalletGuard>
|
|
||||||
<SafeArea>
|
|
||||||
<DefaultMain>
|
|
||||||
<BackLink
|
|
||||||
href="/settings"
|
|
||||||
title={i18n.t("settings.header")}
|
|
||||||
/>
|
|
||||||
<LargeHeader>
|
|
||||||
{i18n.t("settings.lnurl_auth.title")}
|
|
||||||
</LargeHeader>
|
|
||||||
<InnerCard>
|
|
||||||
<form class="flex flex-col gap-4" onSubmit={onSubmit}>
|
|
||||||
<TextField.Root
|
|
||||||
value={value()}
|
|
||||||
onChange={setValue}
|
|
||||||
validationState={
|
|
||||||
value() == "" ||
|
|
||||||
value().toLowerCase().startsWith("lnurl")
|
|
||||||
? "valid"
|
|
||||||
: "invalid"
|
|
||||||
}
|
|
||||||
class="flex flex-col gap-4"
|
|
||||||
>
|
|
||||||
<TextField.Label class="text-sm font-semibold uppercase">
|
|
||||||
{i18n.t("settings.lnurl_auth.title")}
|
|
||||||
</TextField.Label>
|
|
||||||
<TextField.Input
|
|
||||||
class="w-full rounded-lg p-2 text-black"
|
|
||||||
placeholder="LNURL..."
|
|
||||||
/>
|
|
||||||
<TextField.ErrorMessage class="text-red-500">
|
|
||||||
{i18n.t("settings.lnurl_auth.expected")}
|
|
||||||
</TextField.ErrorMessage>
|
|
||||||
</TextField.Root>
|
|
||||||
<Button layout="small" type="submit">
|
|
||||||
{i18n.t("settings.lnurl_auth.auth")}
|
|
||||||
</Button>
|
|
||||||
</form>
|
|
||||||
</InnerCard>
|
|
||||||
</DefaultMain>
|
|
||||||
<NavBar activeTab="settings" />
|
|
||||||
</SafeArea>
|
|
||||||
</MutinyWalletGuard>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
@@ -148,10 +148,6 @@ export function Settings() {
|
|||||||
? i18n.t("settings.gift.no_plus_caption")
|
? i18n.t("settings.gift.no_plus_caption")
|
||||||
: undefined
|
: undefined
|
||||||
},
|
},
|
||||||
{
|
|
||||||
href: "/settings/lnurlauth",
|
|
||||||
text: i18n.t("settings.lnurl_auth.title")
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
href: "/settings/syncnostrcontacts",
|
href: "/settings/syncnostrcontacts",
|
||||||
text: "Sync Nostr Contacts"
|
text: "Sync Nostr Contacts"
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ export * from "./Currency";
|
|||||||
export * from "./EmergencyKit";
|
export * from "./EmergencyKit";
|
||||||
export * from "./Encrypt";
|
export * from "./Encrypt";
|
||||||
export * from "./Gift";
|
export * from "./Gift";
|
||||||
export * from "./LnUrlAuth";
|
|
||||||
export * from "./Plus";
|
export * from "./Plus";
|
||||||
export * from "./Restore";
|
export * from "./Restore";
|
||||||
export * from "./Servers";
|
export * from "./Servers";
|
||||||
|
|||||||
@@ -299,7 +299,7 @@ export const Provider: ParentComponent = (props) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
setScanResult(scan_result: ParsedParams) {
|
setScanResult(scan_result: ParsedParams | undefined) {
|
||||||
setState({ scan_result });
|
setState({ scan_result });
|
||||||
},
|
},
|
||||||
setHasBackedUp() {
|
setHasBackedUp() {
|
||||||
@@ -361,12 +361,25 @@ export const Provider: ParentComponent = (props) => {
|
|||||||
result.value?.address ||
|
result.value?.address ||
|
||||||
result.value?.invoice ||
|
result.value?.invoice ||
|
||||||
result.value?.node_pubkey ||
|
result.value?.node_pubkey ||
|
||||||
result.value?.lnurl
|
(result.value?.lnurl && !result.value.is_lnurl_auth)
|
||||||
) {
|
) {
|
||||||
if (onSuccess) {
|
if (onSuccess) {
|
||||||
onSuccess(result.value);
|
onSuccess(result.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (result.value?.lnurl && result.value?.is_lnurl_auth) {
|
||||||
|
navigate(
|
||||||
|
"/?lnurlauth=" + encodeURIComponent(result.value?.lnurl)
|
||||||
|
);
|
||||||
|
actions.setScanResult(undefined);
|
||||||
|
}
|
||||||
|
if (result.value?.fedimint_invite) {
|
||||||
|
navigate(
|
||||||
|
"/?fedimint_invite=" +
|
||||||
|
encodeURIComponent(result.value?.fedimint_invite)
|
||||||
|
);
|
||||||
|
actions.setScanResult(undefined);
|
||||||
|
}
|
||||||
if (result.value?.nostr_wallet_auth) {
|
if (result.value?.nostr_wallet_auth) {
|
||||||
console.log(
|
console.log(
|
||||||
"nostr_wallet_auth",
|
"nostr_wallet_auth",
|
||||||
|
|||||||
243
src/utils/bech32.ts
Normal file
243
src/utils/bech32.ts
Normal file
@@ -0,0 +1,243 @@
|
|||||||
|
// pasted from https://github.com/bitcoinjs/bech32/
|
||||||
|
const ALPHABET = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
|
||||||
|
|
||||||
|
const ALPHABET_MAP: { [key: string]: number } = {};
|
||||||
|
for (let z = 0; z < ALPHABET.length; z++) {
|
||||||
|
const x = ALPHABET.charAt(z);
|
||||||
|
ALPHABET_MAP[x] = z;
|
||||||
|
}
|
||||||
|
|
||||||
|
function polymodStep(pre: number): number {
|
||||||
|
const b = pre >> 25;
|
||||||
|
return (
|
||||||
|
((pre & 0x1ffffff) << 5) ^
|
||||||
|
(-((b >> 0) & 1) & 0x3b6a57b2) ^
|
||||||
|
(-((b >> 1) & 1) & 0x26508e6d) ^
|
||||||
|
(-((b >> 2) & 1) & 0x1ea119fa) ^
|
||||||
|
(-((b >> 3) & 1) & 0x3d4233dd) ^
|
||||||
|
(-((b >> 4) & 1) & 0x2a1462b3)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function prefixChk(prefix: string): number | string {
|
||||||
|
let chk = 1;
|
||||||
|
for (let i = 0; i < prefix.length; ++i) {
|
||||||
|
const c = prefix.charCodeAt(i);
|
||||||
|
if (c < 33 || c > 126) return "Invalid prefix (" + prefix + ")";
|
||||||
|
|
||||||
|
chk = polymodStep(chk) ^ (c >> 5);
|
||||||
|
}
|
||||||
|
chk = polymodStep(chk);
|
||||||
|
|
||||||
|
for (let i = 0; i < prefix.length; ++i) {
|
||||||
|
const v = prefix.charCodeAt(i);
|
||||||
|
chk = polymodStep(chk) ^ (v & 0x1f);
|
||||||
|
}
|
||||||
|
return chk;
|
||||||
|
}
|
||||||
|
|
||||||
|
function convert(
|
||||||
|
data: ArrayLike<number>,
|
||||||
|
inBits: number,
|
||||||
|
outBits: number,
|
||||||
|
pad: true
|
||||||
|
): number[];
|
||||||
|
function convert(
|
||||||
|
data: ArrayLike<number>,
|
||||||
|
inBits: number,
|
||||||
|
outBits: number,
|
||||||
|
pad: false
|
||||||
|
): number[] | string;
|
||||||
|
function convert(
|
||||||
|
data: ArrayLike<number>,
|
||||||
|
inBits: number,
|
||||||
|
outBits: number,
|
||||||
|
pad: boolean
|
||||||
|
): number[] | string {
|
||||||
|
let value = 0;
|
||||||
|
let bits = 0;
|
||||||
|
const maxV = (1 << outBits) - 1;
|
||||||
|
|
||||||
|
const result: number[] = [];
|
||||||
|
for (let i = 0; i < data.length; ++i) {
|
||||||
|
value = (value << inBits) | data[i];
|
||||||
|
bits += inBits;
|
||||||
|
|
||||||
|
while (bits >= outBits) {
|
||||||
|
bits -= outBits;
|
||||||
|
result.push((value >> bits) & maxV);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pad) {
|
||||||
|
if (bits > 0) {
|
||||||
|
result.push((value << (outBits - bits)) & maxV);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (bits >= inBits) return "Excess padding";
|
||||||
|
if ((value << (outBits - bits)) & maxV) return "Non-zero padding";
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function toWords(bytes: ArrayLike<number>): number[] {
|
||||||
|
return convert(bytes, 8, 5, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function fromWordsUnsafe(words: ArrayLike<number>): number[] | undefined {
|
||||||
|
const res = convert(words, 5, 8, false);
|
||||||
|
if (Array.isArray(res)) return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function fromWords(words: ArrayLike<number>): number[] {
|
||||||
|
const res = convert(words, 5, 8, false);
|
||||||
|
if (Array.isArray(res)) return res;
|
||||||
|
|
||||||
|
throw new Error(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLibraryFromEncoding(encoding: "bech32" | "bech32m"): BechLib {
|
||||||
|
let ENCODING_CONST: number;
|
||||||
|
if (encoding === "bech32") {
|
||||||
|
ENCODING_CONST = 1;
|
||||||
|
} else {
|
||||||
|
ENCODING_CONST = 0x2bc830a3;
|
||||||
|
}
|
||||||
|
|
||||||
|
function encode(
|
||||||
|
prefix: string,
|
||||||
|
words: ArrayLike<number>,
|
||||||
|
LIMIT?: number
|
||||||
|
): string {
|
||||||
|
LIMIT = LIMIT || 90;
|
||||||
|
if (prefix.length + 7 + words.length > LIMIT)
|
||||||
|
throw new TypeError("Exceeds length limit");
|
||||||
|
|
||||||
|
prefix = prefix.toLowerCase();
|
||||||
|
|
||||||
|
// determine chk mod
|
||||||
|
let chk = prefixChk(prefix);
|
||||||
|
if (typeof chk === "string") throw new Error(chk);
|
||||||
|
|
||||||
|
let result = prefix + "1";
|
||||||
|
for (let i = 0; i < words.length; ++i) {
|
||||||
|
const x = words[i];
|
||||||
|
if (x >> 5 !== 0) throw new Error("Non 5-bit word");
|
||||||
|
|
||||||
|
chk = polymodStep(chk) ^ x;
|
||||||
|
result += ALPHABET.charAt(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < 6; ++i) {
|
||||||
|
chk = polymodStep(chk);
|
||||||
|
}
|
||||||
|
chk ^= ENCODING_CONST;
|
||||||
|
|
||||||
|
for (let i = 0; i < 6; ++i) {
|
||||||
|
const v = (chk >> ((5 - i) * 5)) & 0x1f;
|
||||||
|
result += ALPHABET.charAt(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function __decode(str: string, LIMIT?: number): Decoded | string {
|
||||||
|
LIMIT = LIMIT || 90;
|
||||||
|
if (str.length < 8) return str + " too short";
|
||||||
|
if (str.length > LIMIT) return "Exceeds length limit";
|
||||||
|
|
||||||
|
// don't allow mixed case
|
||||||
|
const lowered = str.toLowerCase();
|
||||||
|
const uppered = str.toUpperCase();
|
||||||
|
if (str !== lowered && str !== uppered)
|
||||||
|
return "Mixed-case string " + str;
|
||||||
|
str = lowered;
|
||||||
|
|
||||||
|
const split = str.lastIndexOf("1");
|
||||||
|
if (split === -1) return "No separator character for " + str;
|
||||||
|
if (split === 0) return "Missing prefix for " + str;
|
||||||
|
|
||||||
|
const prefix = str.slice(0, split);
|
||||||
|
const wordChars = str.slice(split + 1);
|
||||||
|
if (wordChars.length < 6) return "Data too short";
|
||||||
|
|
||||||
|
let chk = prefixChk(prefix);
|
||||||
|
if (typeof chk === "string") return chk;
|
||||||
|
|
||||||
|
const words = [];
|
||||||
|
for (let i = 0; i < wordChars.length; ++i) {
|
||||||
|
const c = wordChars.charAt(i);
|
||||||
|
const v = ALPHABET_MAP[c];
|
||||||
|
if (v === undefined) return "Unknown character " + c;
|
||||||
|
chk = polymodStep(chk) ^ v;
|
||||||
|
|
||||||
|
// not in the checksum?
|
||||||
|
if (i + 6 >= wordChars.length) continue;
|
||||||
|
words.push(v);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (chk !== ENCODING_CONST) return "Invalid checksum for " + str;
|
||||||
|
return { prefix, words };
|
||||||
|
}
|
||||||
|
|
||||||
|
function decodeUnsafe(str: string, LIMIT?: number): Decoded | undefined {
|
||||||
|
const res = __decode(str, LIMIT);
|
||||||
|
if (typeof res === "object") return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
function decode(str: string, LIMIT?: number): Decoded {
|
||||||
|
const res = __decode(str, LIMIT);
|
||||||
|
if (typeof res === "object") return res;
|
||||||
|
|
||||||
|
throw new Error(res);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
decodeUnsafe,
|
||||||
|
decode,
|
||||||
|
encode,
|
||||||
|
toWords,
|
||||||
|
fromWordsUnsafe,
|
||||||
|
fromWords
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export function bech32WordsToUrl(words: number[]) {
|
||||||
|
// Step 1: Convert words to bytes
|
||||||
|
const bits = words
|
||||||
|
.map((word) => word.toString(2).padStart(5, "0"))
|
||||||
|
.join("");
|
||||||
|
const bytes = [];
|
||||||
|
for (let i = 0; i < bits.length; i += 8) {
|
||||||
|
bytes.push(parseInt(bits.substring(i, i + 8), 2));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 2: Convert bytes to characters
|
||||||
|
const urlChars = bytes.map((byte) => String.fromCharCode(byte));
|
||||||
|
|
||||||
|
// Step 3: Concatenate characters to form URL string
|
||||||
|
return urlChars.join("");
|
||||||
|
}
|
||||||
|
|
||||||
|
export const bech32 = getLibraryFromEncoding("bech32");
|
||||||
|
export const bech32m = getLibraryFromEncoding("bech32m");
|
||||||
|
export interface Decoded {
|
||||||
|
prefix: string;
|
||||||
|
words: number[];
|
||||||
|
}
|
||||||
|
export interface BechLib {
|
||||||
|
decodeUnsafe: (
|
||||||
|
str: string,
|
||||||
|
LIMIT?: number | undefined
|
||||||
|
) => Decoded | undefined;
|
||||||
|
decode: (str: string, LIMIT?: number | undefined) => Decoded;
|
||||||
|
encode: (
|
||||||
|
prefix: string,
|
||||||
|
words: ArrayLike<number>,
|
||||||
|
LIMIT?: number | undefined
|
||||||
|
) => string;
|
||||||
|
toWords: typeof toWords;
|
||||||
|
fromWordsUnsafe: typeof fromWordsUnsafe;
|
||||||
|
fromWords: typeof fromWords;
|
||||||
|
}
|
||||||
@@ -19,3 +19,4 @@ export * from "./vibrate";
|
|||||||
export * from "./openLinkProgrammatically";
|
export * from "./openLinkProgrammatically";
|
||||||
export * from "./nostr";
|
export * from "./nostr";
|
||||||
export * from "./currencies";
|
export * from "./currencies";
|
||||||
|
export * from "./bech32";
|
||||||
|
|||||||
Reference in New Issue
Block a user