Skip to content

Commit 1621456

Browse files
authored
fix: pass TLV value as hex from nwc webln provider to nwc client keysend (#229)
* fix: pass TLV value as hex from nwc webln provider to nwc client keysend * fix: update nwc client keysend examples to pass TLV values as hex * fix: typo
1 parent 377d0a8 commit 1621456

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

examples/nwc/client/multi-pay-keysend.js

+26-6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,26 @@ const client = new nwc.NWCClient({
1717
nostrWalletConnectUrl: nwcUrl,
1818
});
1919

20+
// Data from https://podcastindex.org/podcast/920666
21+
const boost = {
22+
action: "boost",
23+
value_msat: 1000,
24+
value_msat_total: 1000,
25+
app_name: "⚡ WebLN Demo",
26+
app_version: "1.0",
27+
feedID: "https://feeds.podcastindex.org/pc20.xml",
28+
podcast: "Podcasting 2.0",
29+
episode: "Episode 104: A New Dump",
30+
ts: 21,
31+
name: "⚡ WebLN Demo",
32+
sender_name: "Satoshi Nakamoto",
33+
message: "Go podcasting!",
34+
};
35+
36+
// from https://stackoverflow.com/a/50868276
37+
const toHexString = (bytes) =>
38+
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
39+
2040
const keysends = [
2141
{
2242
pubkey:
@@ -25,11 +45,11 @@ const keysends = [
2545
tlv_records: [
2646
{
2747
type: 696969,
28-
value: "017rsl75kNnSke4mMHYE", // hello@getalby.com
48+
value: toHexString(new TextEncoder().encode("017rsl75kNnSke4mMHYE")), // hello@getalby.com
2949
},
3050
{
31-
type: 34349334,
32-
value: "first keysend message",
51+
type: 7629169,
52+
value: toHexString(new TextEncoder().encode(JSON.stringify(boost))),
3353
},
3454
],
3555
},
@@ -40,11 +60,11 @@ const keysends = [
4060
tlv_records: [
4161
{
4262
type: 696969,
43-
value: "1KOZHzhLs2U7JIx3BmEY", // another Alby account
63+
value: toHexString(new TextEncoder().encode("1KOZHzhLs2U7JIx3BmEY")), // another Alby account
4464
},
4565
{
46-
type: 34349334,
47-
value: "second keysend message",
66+
type: 7629169,
67+
value: toHexString(new TextEncoder().encode(JSON.stringify(boost))),
4868
},
4969
],
5070
},

examples/nwc/client/pay-keysend.js

+24-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,38 @@ rl.close();
1717
const client = new nwc.NWCClient({
1818
nostrWalletConnectUrl: nwcUrl,
1919
});
20+
21+
// from https://stackoverflow.com/a/50868276
22+
const toHexString = (bytes) =>
23+
bytes.reduce((str, byte) => str + byte.toString(16).padStart(2, "0"), "");
24+
25+
// Data from https://podcastindex.org/podcast/920666
26+
const boost = {
27+
action: "boost",
28+
value_msat: 1000,
29+
value_msat_total: 1000,
30+
app_name: "⚡ WebLN Demo",
31+
app_version: "1.0",
32+
feedID: "https://feeds.podcastindex.org/pc20.xml",
33+
podcast: "Podcasting 2.0",
34+
episode: "Episode 104: A New Dump",
35+
ts: 21,
36+
name: "⚡ WebLN Demo",
37+
sender_name: "Satoshi Nakamoto",
38+
message: "Go podcasting!",
39+
};
40+
2041
const response = await client.payKeysend({
2142
amount: 1000, // millisats
2243
pubkey: "030a58b8653d32b99200a2334cfe913e51dc7d155aa0116c176657a4f1722677a3",
2344
tlv_records: [
2445
{
2546
type: 696969,
26-
value: "017rsl75kNnSke4mMHYE", // hello@getalby.com
47+
value: toHexString(new TextEncoder().encode("017rsl75kNnSke4mMHYE")), // hello@getalby.com
2748
},
2849
{
29-
type: 34349334,
30-
value: "example keysend message",
50+
type: 7629169,
51+
value: toHexString(new TextEncoder().encode(JSON.stringify(boost))),
3152
},
3253
],
3354
});

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@getalby/sdk",
3-
"version": "3.6.0",
3+
"version": "3.6.1",
44
"description": "The SDK to integrate with Nostr Wallet Connect and the Alby API",
55
"repository": "https://github.com/getAlby/js-sdk.git",
66
"bugs": "https://github.com/getAlby/js-sdk/issues",

src/webln/NostrWeblnProvider.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
Nip47PayKeysendRequest,
2424
Nip47Transaction,
2525
} from "../NWCClient";
26+
import { toHexString } from "../utils";
2627

2728
// TODO: review fields (replace with camelCase)
2829
// TODO: consider move to webln-types package
@@ -509,7 +510,7 @@ function mapKeysendToNip47Keysend(args: KeysendArgs): Nip47PayKeysendRequest {
509510
tlv_records: args.customRecords
510511
? Object.entries(args.customRecords).map((v) => ({
511512
type: parseInt(v[0]),
512-
value: v[1],
513+
value: toHexString(new TextEncoder().encode(v[1])),
513514
}))
514515
: [],
515516
// TODO: support optional preimage

0 commit comments

Comments
 (0)