Skip to content

Commit

Permalink
Merge pull request #3172 from getAlby/feat/nwc-custom-records
Browse files Browse the repository at this point in the history
feat: add custom records to NWC connector
  • Loading branch information
rolznz authored Jul 4, 2024
2 parents eb3e895 + 8f1b157 commit 4c892bb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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
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;

0 comments on commit 4c892bb

Please sign in to comment.