575505cc83cab76550c9ece7af65dd8ac0f725e947b300ab526b89953d1262c5.json raw
1 {"ast":null,"code":"import _asyncToGenerator from \"/home/mleku/src/orly.dev/next/signer/node_modules/@babel/runtime/helpers/esm/asyncToGenerator.js\";\n/* eslint-disable @typescript-eslint/no-unused-vars */\nimport { bech32 } from '@scure/base';\nimport * as utils from '@noble/curves/abstract/utils';\nimport { getPublicKey } from 'nostr-tools';\nimport { encrypt as nip49Encrypt } from 'nostr-tools/nip49';\nexport class NostrHelper {\n static getNostrPrivkeyObject(nsec_OR_hex) {\n // 1. Assume we got an nsec.\n // Try to generate hex value.\n try {\n const hexObject = this.#nSomething2hexObject(nsec_OR_hex);\n if (hexObject.represents !== 'nsec') {\n throw new Error('The provided string is NOT an nsec.');\n }\n // Everything is fine. The provided string IS an nsec.\n return {\n hex: hexObject.hex,\n nsec: nsec_OR_hex\n };\n } catch (error) {\n // Continue.\n }\n // 2. Assume we got an hex.\n // Try to generate the nsec.\n try {\n const nsec = NostrHelper.privkey2nsec(nsec_OR_hex);\n return {\n hex: nsec_OR_hex,\n nsec\n };\n } catch (error) {\n // Continue;\n }\n throw new Error('Could not convert the provided string into nsec/hex.');\n }\n static getNostrPubkeyObject(npub_OR_hex) {\n // 1. Assume we got an npub.\n // Try to generate hex value.\n try {\n const hexObject = this.#nSomething2hexObject(npub_OR_hex);\n if (hexObject.represents !== 'npub') {\n throw new Error('The provided string is NOT an npub.');\n }\n // Everything is fine. The provided string IS an npub.\n return {\n hex: hexObject.hex,\n npub: npub_OR_hex\n };\n } catch (error) {\n // Continue.\n }\n // 2. Assume we got an hex.\n // Try to generate the npub.\n try {\n const npub = NostrHelper.pubkey2npub(npub_OR_hex);\n return {\n hex: npub_OR_hex,\n npub\n };\n } catch (error) {\n // Continue;\n }\n throw new Error('Could not convert the provided string into npub/hex.');\n }\n static pubkey2npub(hex) {\n const data = utils.hexToBytes(hex);\n const words = bech32.toWords(data);\n return bech32.encode('npub', words, 5000);\n }\n static privkey2nsec(hex) {\n const data = utils.hexToBytes(hex);\n const words = bech32.toWords(data);\n return bech32.encode('nsec', words, 5000);\n }\n static pubkeyFromPrivkey(hex) {\n const privkeyBytes = utils.hexToBytes(hex);\n return getPublicKey(privkeyBytes);\n }\n static hex2bytes(hex) {\n return utils.hexToBytes(hex);\n }\n static splitKey(text, first, last) {\n const part1 = text.slice(0, first);\n const part2 = '...';\n const part3 = text.slice(-last);\n return `${part1}${part2}${part3}`;\n }\n static #nSomething2hexObject(nSomething) {\n const {\n prefix,\n words\n } = bech32.decode(nSomething, 5000);\n const data = new Uint8Array(bech32.fromWords(words));\n return {\n represents: prefix,\n hex: utils.bytesToHex(data)\n };\n }\n /**\n * Encrypts a private key (hex) with a password using NIP-49.\n * Returns an ncryptsec bech32 string.\n * @param privkeyHex - The private key in hex format\n * @param password - The password to encrypt with\n * @param logN - Optional log2(N) parameter for scrypt (default: 16)\n * @returns Promise<string> - The ncryptsec bech32 encoded encrypted key\n */\n static privkeyToNcryptsec(_x, _x2) {\n return _asyncToGenerator(function* (privkeyHex, password, logN = 16) {\n const privkeyBytes = utils.hexToBytes(privkeyHex);\n return nip49Encrypt(privkeyBytes, password, logN);\n }).apply(this, arguments);\n }\n}","map":null,"metadata":{},"sourceType":"module","externalDependencies":[]}