applesauce-signers 6.2.0 → 6.2.2

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.
@@ -1,2 +1,3 @@
1
1
  export * from "./encryption.js";
2
+ export * from "./nbunksec.js";
2
3
  export * from "./nostr-connect.js";
@@ -1,2 +1,3 @@
1
1
  export * from "./encryption.js";
2
+ export * from "./nbunksec.js";
2
3
  export * from "./nostr-connect.js";
@@ -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
+ }
@@ -1,4 +1,4 @@
1
- import { encodeNbunksec, decodeNbunksec } from "@sandwichfarm/encoded-entities";
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";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-signers",
3
- "version": "6.2.0",
3
+ "version": "6.2.2",
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
- "@sandwichfarm/encoded-entities": "^0.1.1",
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",