applesauce-signers 6.2.0 → 6.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/helpers/index.d.ts +1 -0
- package/dist/helpers/index.js +1 -0
- package/dist/helpers/nbunksec.d.ts +11 -0
- package/dist/helpers/nbunksec.js +78 -0
- package/dist/helpers/nostr-connect.d.ts +1 -1
- package/dist/helpers/nostr-connect.js +1 -1
- package/dist/signers/nostr-connect-signer.js +11 -3
- package/package.json +2 -2
package/dist/helpers/index.d.ts
CHANGED
package/dist/helpers/index.js
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** The decoded contents of an nbunksec encoded NIP-46 signer session */
|
|
2
|
+
export interface BunkerInfo {
|
|
3
|
+
pubkey: string;
|
|
4
|
+
local_key: string;
|
|
5
|
+
relays: string[];
|
|
6
|
+
secret?: string;
|
|
7
|
+
}
|
|
8
|
+
/** Encodes a {@link BunkerInfo} object into an nbunksec string */
|
|
9
|
+
export declare function encodeNbunksec(info: BunkerInfo): string;
|
|
10
|
+
/** Decodes an nbunksec string into a {@link BunkerInfo} object */
|
|
11
|
+
export declare function decodeNbunksec(encoded: string): BunkerInfo;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// nbunksec encoding/decoding adapted from @sandwichfarm/encoded-entities
|
|
2
|
+
// https://github.com/sandwichfarm/encoded-entities (src/encoders/nbunksec.ts)
|
|
3
|
+
import { bech32 } from "@scure/base";
|
|
4
|
+
import { bytesToHex, hexToBytes } from "applesauce-core/helpers/event";
|
|
5
|
+
const encoder = new TextEncoder();
|
|
6
|
+
const decoder = new TextDecoder();
|
|
7
|
+
// TLV types used in the nbunksec encoding
|
|
8
|
+
const TYPE_PUBKEY = 0;
|
|
9
|
+
const TYPE_LOCAL_KEY = 1;
|
|
10
|
+
const TYPE_RELAY = 2;
|
|
11
|
+
const TYPE_SECRET = 3;
|
|
12
|
+
/** Encodes a {@link BunkerInfo} object into an nbunksec string */
|
|
13
|
+
export function encodeNbunksec(info) {
|
|
14
|
+
try {
|
|
15
|
+
const parts = [];
|
|
16
|
+
const pubkeyBytes = hexToBytes(info.pubkey);
|
|
17
|
+
parts.push(new Uint8Array([TYPE_PUBKEY, pubkeyBytes.length]), pubkeyBytes);
|
|
18
|
+
const localKeyBytes = hexToBytes(info.local_key);
|
|
19
|
+
parts.push(new Uint8Array([TYPE_LOCAL_KEY, localKeyBytes.length]), localKeyBytes);
|
|
20
|
+
for (const relay of info.relays) {
|
|
21
|
+
const relayBytes = encoder.encode(relay);
|
|
22
|
+
parts.push(new Uint8Array([TYPE_RELAY, relayBytes.length]), relayBytes);
|
|
23
|
+
}
|
|
24
|
+
if (info.secret) {
|
|
25
|
+
const secretBytes = encoder.encode(info.secret);
|
|
26
|
+
parts.push(new Uint8Array([TYPE_SECRET, secretBytes.length]), secretBytes);
|
|
27
|
+
}
|
|
28
|
+
const length = parts.reduce((sum, part) => sum + part.length, 0);
|
|
29
|
+
const data = new Uint8Array(length);
|
|
30
|
+
let offset = 0;
|
|
31
|
+
for (const part of parts) {
|
|
32
|
+
data.set(part, offset);
|
|
33
|
+
offset += part.length;
|
|
34
|
+
}
|
|
35
|
+
return bech32.encode("nbunksec", bech32.toWords(data), false);
|
|
36
|
+
}
|
|
37
|
+
catch (error) {
|
|
38
|
+
throw new Error(`Failed to encode bunker info: ${error}`);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/** Decodes an nbunksec string into a {@link BunkerInfo} object */
|
|
42
|
+
export function decodeNbunksec(encoded) {
|
|
43
|
+
try {
|
|
44
|
+
const { prefix, words } = bech32.decode(encoded, false);
|
|
45
|
+
if (prefix !== "nbunksec")
|
|
46
|
+
throw new Error(`Invalid prefix: ${prefix}`);
|
|
47
|
+
const bytes = bech32.fromWords(words);
|
|
48
|
+
let offset = 0;
|
|
49
|
+
const info = { pubkey: "", local_key: "", relays: [] };
|
|
50
|
+
while (offset < bytes.length) {
|
|
51
|
+
const type = bytes[offset];
|
|
52
|
+
const length = bytes[offset + 1];
|
|
53
|
+
offset += 2;
|
|
54
|
+
const value = bytes.slice(offset, offset + length);
|
|
55
|
+
offset += length;
|
|
56
|
+
switch (type) {
|
|
57
|
+
case TYPE_PUBKEY:
|
|
58
|
+
info.pubkey = bytesToHex(value);
|
|
59
|
+
break;
|
|
60
|
+
case TYPE_LOCAL_KEY:
|
|
61
|
+
info.local_key = bytesToHex(value);
|
|
62
|
+
break;
|
|
63
|
+
case TYPE_RELAY:
|
|
64
|
+
info.relays.push(decoder.decode(value));
|
|
65
|
+
break;
|
|
66
|
+
case TYPE_SECRET:
|
|
67
|
+
info.secret = decoder.decode(value);
|
|
68
|
+
break;
|
|
69
|
+
default:
|
|
70
|
+
throw new Error(`Unknown type: ${type}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return info;
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new Error(`Failed to decode bunker info: ${error}`);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -45,7 +45,7 @@ export type ConnectResponseResults = {
|
|
|
45
45
|
[NostrConnectMethod.Nip04Decrypt]: string;
|
|
46
46
|
[NostrConnectMethod.Nip44Encrypt]: string;
|
|
47
47
|
[NostrConnectMethod.Nip44Decrypt]: string;
|
|
48
|
-
[NostrConnectMethod.SwitchRelays]: string[] | null;
|
|
48
|
+
[NostrConnectMethod.SwitchRelays]: string | string[] | null;
|
|
49
49
|
[NostrConnectMethod.Logout]: "ack";
|
|
50
50
|
};
|
|
51
51
|
export type NostrConnectRequest<N extends NostrConnectMethod> = {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { encodeNbunksec, decodeNbunksec } from "
|
|
1
|
+
import { encodeNbunksec, decodeNbunksec } from "./nbunksec.js";
|
|
2
2
|
import { setHiddenContentEncryptionMethod } from "applesauce-core/helpers/hidden-content";
|
|
3
3
|
import { isHexKey } from "applesauce-core/helpers/string";
|
|
4
4
|
import { kinds } from "applesauce-core/helpers/event";
|
|
@@ -169,7 +169,7 @@ export class NostrConnectSigner {
|
|
|
169
169
|
else
|
|
170
170
|
p.reject(new Error(response.error));
|
|
171
171
|
}
|
|
172
|
-
else if (response.result) {
|
|
172
|
+
else if (response.result !== undefined) {
|
|
173
173
|
this.log("Got Response", response.id, response.result);
|
|
174
174
|
p.resolve(response.result);
|
|
175
175
|
}
|
|
@@ -334,9 +334,17 @@ export class NostrConnectSigner {
|
|
|
334
334
|
*/
|
|
335
335
|
async switchRelays() {
|
|
336
336
|
await this.requireConnection();
|
|
337
|
-
const
|
|
337
|
+
const response = await this.makeRequest(NostrConnectMethod.SwitchRelays, []);
|
|
338
|
+
// NIP-46 results are strings, so the relay list arrives JSON-stringified
|
|
339
|
+
let result = null;
|
|
340
|
+
try {
|
|
341
|
+
const parsed = typeof response === "string" ? JSON.parse(response) : response;
|
|
342
|
+
if (Array.isArray(parsed))
|
|
343
|
+
result = parsed.filter((relay) => typeof relay === "string");
|
|
344
|
+
}
|
|
345
|
+
catch (e) { }
|
|
338
346
|
// Update local relays if the remote signer provided new ones
|
|
339
|
-
if (result
|
|
347
|
+
if (result && result.length > 0) {
|
|
340
348
|
this.log("Switching relays from", this.relays, "to", result);
|
|
341
349
|
this.relays = result;
|
|
342
350
|
// Restart subscription with new relays
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "applesauce-signers",
|
|
3
|
-
"version": "6.2.
|
|
3
|
+
"version": "6.2.3",
|
|
4
4
|
"description": "Signer classes for applesauce",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@noble/secp256k1": "^3.1.0",
|
|
48
|
-
"@
|
|
48
|
+
"@scure/base": "^2.2.0",
|
|
49
49
|
"applesauce-core": "^6.2.0",
|
|
50
50
|
"debug": "^4.4.0",
|
|
51
51
|
"nanoid": "^5.0.9",
|