From f62fbb3ef10d3d608ea4ffa6c6badd3b1e44d81a Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 14 Jun 2024 12:02:56 +0700 Subject: [PATCH 1/5] feat: add custom records to NWC connector --- src/extension/background-script/connectors/nwc.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/extension/background-script/connectors/nwc.ts b/src/extension/background-script/connectors/nwc.ts index be67fe1024..53851957a1 100644 --- a/src/extension/background-script/connectors/nwc.ts +++ b/src/extension/background-script/connectors/nwc.ts @@ -89,6 +89,9 @@ class NWCConnector implements Connector { settleDate: transaction.settled_at * 1000, totalAmount: transaction.amount, type: transaction.type == "incoming" ? "received" : "sent", + custom_records: transaction.metadata?.[ + "custom_records" + ] as ConnectorTransaction["custom_records"], }) ); return { From 84b6c8520db1514c9711dfbcafc06e2a6adf4ac2 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Fri, 14 Jun 2024 13:34:18 +0700 Subject: [PATCH 2/5] fix: boostagram message field --- src/app/components/TransactionsTable/TransactionModal.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 && ( From 1134dcca8beef8edb2df850c96eeec9803a9065b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Aaron?= Date: Fri, 14 Jun 2024 11:12:53 +0200 Subject: [PATCH 3/5] fix: unicode decoding boostagrams --- src/common/lib/string.ts | 10 ++++++++++ src/common/lib/utils.ts | 4 +++- 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 src/common/lib/string.ts 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); From 06d5377a73572fbe78ec6fc9ef8d0a4e1f5086d6 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Mon, 17 Jun 2024 16:19:10 +0700 Subject: [PATCH 4/5] fix: use new NWC tlv_records format --- .../background-script/connectors/nwc.ts | 32 +++++++++++++++++-- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/src/extension/background-script/connectors/nwc.ts b/src/extension/background-script/connectors/nwc.ts index 53851957a1..0e81210da2 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,9 +98,9 @@ class NWCConnector implements Connector { settleDate: transaction.settled_at * 1000, totalAmount: transaction.amount, type: transaction.type == "incoming" ? "received" : "sent", - custom_records: transaction.metadata?.[ - "custom_records" - ] as ConnectorTransaction["custom_records"], + custom_records: mapTLVRecords( + transaction.metadata?.["tlv_records"] as TLVRecord[] | undefined + ), }) ); return { @@ -208,4 +217,21 @@ class NWCConnector implements Connector { } } +function mapTLVRecords( + tlvRecords: TLVRecord[] | undefined +): ConnectorTransaction["custom_records"] | undefined { + if (tlvRecords) { + 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; + } + return undefined; +} + export default NWCConnector; From d853821a4ca8d4ec67b15f8399ac185819abbd51 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Thu, 4 Jul 2024 10:24:40 +0700 Subject: [PATCH 5/5] chore: simplify mapTLVRecords in nwc connector --- .../background-script/connectors/nwc.ts | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/extension/background-script/connectors/nwc.ts b/src/extension/background-script/connectors/nwc.ts index 0e81210da2..df246bc1be 100644 --- a/src/extension/background-script/connectors/nwc.ts +++ b/src/extension/background-script/connectors/nwc.ts @@ -220,18 +220,16 @@ class NWCConnector implements Connector { function mapTLVRecords( tlvRecords: TLVRecord[] | undefined ): ConnectorTransaction["custom_records"] | undefined { - if (tlvRecords) { - 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; + 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 undefined; + return customRecords; } export default NWCConnector;