Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add custom records to NWC connector #3172

Merged
merged 7 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/components/TransactionsTable/TransactionModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default function TransactionModal({
{transaction.boostagram?.message && (
<TransactionDetailRow
title={t("boostagram.message")}
content={transaction.boostagram?.podcast}
content={transaction.boostagram?.message}
/>
)}
{transaction.boostagram?.podcast && (
Expand Down
10 changes: 10 additions & 0 deletions src/common/lib/string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// https://stackoverflow.com/questions/30106476/using-javascripts-atob-to-decode-base64-doesnt-properly-decode-utf-8-strings
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙀

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("")
);
}
4 changes: 3 additions & 1 deletion src/common/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions src/extension/background-script/connectors/nwc.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -22,6 +23,14 @@ import Connector, {
SignMessageResponse,
} from "./connector.interface";

type TLVRecord = {
type: number;
/**
* hex-encoded value
*/
value: string;
};

interface Config {
nostrWalletConnectUrl: string;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Loading