diff --git a/src/app/components/TransactionsTable/TransactionModal.tsx b/src/app/components/TransactionsTable/TransactionModal.tsx index be441fe310..38b7621ca6 100644 --- a/src/app/components/TransactionsTable/TransactionModal.tsx +++ b/src/app/components/TransactionsTable/TransactionModal.tsx @@ -131,7 +131,7 @@ export default function TransactionModal({ {transaction.boostagram?.message && ( )} {transaction.boostagram?.podcast && ( diff --git a/src/common/lib/string.ts b/src/common/lib/string.ts new file mode 100644 index 0000000000..d36bf5e7a8 --- /dev/null +++ b/src/common/lib/string.ts @@ -0,0 +1,10 @@ +// https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings +export function base64DecodeUnicode(str: string) { + return decodeURIComponent( + Array.prototype.map + .call(atob(str), function (c) { + return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2); + }) + .join("") + ); +} diff --git a/src/common/lib/utils.ts b/src/common/lib/utils.ts index 1a7a3af23e..a78489cba8 100644 --- a/src/common/lib/utils.ts +++ b/src/common/lib/utils.ts @@ -1,5 +1,6 @@ import browser, { Runtime } from "webextension-polyfill"; import { ABORT_PROMPT_ERROR } from "~/common/constants"; +import { base64DecodeUnicode } from "~/common/lib/string"; import { ConnectorTransaction } from "~/extension/background-script/connectors/connector.interface"; import type { DeferredPromise, OriginData, OriginDataInternal } from "~/types"; import { createPromptTab, createPromptWindow } from "../utils/window"; @@ -147,8 +148,9 @@ const utils = { let boostagramDecoded: string | undefined; const boostagram = custom_records?.[7629169]; if (boostagram) { - boostagramDecoded = atob(boostagram); + boostagramDecoded = base64DecodeUnicode(boostagram); } + return boostagramDecoded ? JSON.parse(boostagramDecoded) : undefined; } catch (e) { console.error(e); diff --git a/src/extension/background-script/connectors/nwc.ts b/src/extension/background-script/connectors/nwc.ts index be67fe1024..df246bc1be 100644 --- a/src/extension/background-script/connectors/nwc.ts +++ b/src/extension/background-script/connectors/nwc.ts @@ -1,6 +1,7 @@ import { webln } from "@getalby/sdk"; import { NostrWebLNProvider } from "@getalby/sdk/dist/webln"; import lightningPayReq from "bolt11-signet"; +import Base64 from "crypto-js/enc-base64"; import Hex from "crypto-js/enc-hex"; import SHA256 from "crypto-js/sha256"; import { Account } from "~/types"; @@ -22,6 +23,14 @@ import Connector, { SignMessageResponse, } from "./connector.interface"; +type TLVRecord = { + type: number; + /** + * hex-encoded value + */ + value: string; +}; + interface Config { nostrWalletConnectUrl: string; } @@ -89,6 +98,9 @@ class NWCConnector implements Connector { settleDate: transaction.settled_at * 1000, totalAmount: transaction.amount, type: transaction.type == "incoming" ? "received" : "sent", + custom_records: mapTLVRecords( + transaction.metadata?.["tlv_records"] as TLVRecord[] | undefined + ), }) ); return { @@ -205,4 +217,19 @@ class NWCConnector implements Connector { } } +function mapTLVRecords( + tlvRecords: TLVRecord[] | undefined +): ConnectorTransaction["custom_records"] | undefined { + if (!tlvRecords) { + return undefined; + } + const customRecords: ConnectorTransaction["custom_records"] = {}; + for (const tlv of tlvRecords) { + // TODO: ConnectorTransaction["custom_records"] should not be in base64 format + // as this requires unnecessary re-encoding + customRecords[tlv.type.toString()] = Hex.parse(tlv.value).toString(Base64); + } + return customRecords; +} + export default NWCConnector;