Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add npub2hex.ts. #2

Merged
merged 1 commit into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,45 @@ npm run build

### Optional: Generate sample config for testing

> **Note**: I have prepared `bin/generateConfig.ts` to generate a template config with new `privateKey` and `publicKey` for testing. You should test it first. Then use your own keys.

- I have prepared `bin/generateConfig.ts' to generate a template configuration for testing.
- To generate a template config, run the following command.

```sh
npm run generateConfig
npx ts-node-esm bin/generateConfig.ts
```

### Optional: Convert Bech32 keys (`nsec...` or `npub...`) to hex format

- If you have keys in Bech32 format (`nsec...` or `npub...`), you can convert them to hex format with `bin/npub2hex.ts`.

```sh
npx ts-node-esm bin/npub2hex.ts "nsec..."
npx ts-node-esm bin/npub2hex.ts "npub..."
```

> **Note**: I recommend using environment variables to store keys to avoid leaking them to the shell history.

- Add keys to `~/.bashrc` and restart your terminal.

```sh
export NOSTR_NSEC="nsec..."
export NOSTR_NPUB="npub..."
```

- Convert to hex format using environment variables.

```sh
npx ts-node-esm bin/npub2hex.ts $NOSTR_NSEC
npx ts-node-esm bin/npub2hex.ts $NOSTR_NPUB
```

- `data` field is the hex encoded key.

```javascript
{
type: 'npub',
data: '...'
}
```

### Set your private and public keys
Expand Down Expand Up @@ -108,9 +141,8 @@ npm run build

### Test it on Iris or Snort

> **Note**: First, disable similar NIP-07 extensions, e.g. nos2x, Alby, etc.

- Go to extension page and click `Service Worker` to pen dev console of the extension.
- First, disable similar NIP-07 extensions, e.g. nos2x, Alby, etc.
- Go to extension page and click `Service Worker` to open dev console of the extension.
- Enable log level `Verbose` to show debug logs.
- Go to [Iris](https://iris.to/) or [Snort](https://snort.social/).
- Logout if you already logged in.
Expand All @@ -131,9 +163,9 @@ I have tested this extension on Iris and Snort.
## TODO

- [x] Prepare a zip file for easy installation.
- [ ] Find a way to store the private key securely. Is it possible to use the macOS Keychain (or similar one) from the Chrome extension?
- [ ] GitHub Actions to build and publish the zip file.
- [ ] Test `relays`.
- [ ] Find a way to store the private key securely. Is it possible to use the macOS Keychain (or similar one) from the Chrome extension?
- [ ] Add profiles to switch multiple accounts.
- [ ] Minimal UI.
- [ ] Chrome Web Store?
Expand Down
30 changes: 30 additions & 0 deletions bin/npub2hex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2023 Susumu OTA <[email protected]>
// SPDX-License-Identifier: MIT

import * as secp from '@noble/secp256k1';
import { bech32 } from '@scure/base';

// https://github.com/nostr-protocol/nips/blob/master/19.md
// https://github.com/nbd-wtf/nostr-tools/blob/master/nip19.ts

const BECH32_MAX_SIZE = 5000;

// const hexToBech32 = (prefix: string, hexstr: string) => (
// bech32.encode(prefix, bech32.toWords(secp.utils.hexToBytes(hexstr)), BECH32_MAX_SIZE)
// );

const bech32ToHex = (bech32str: string) => {
const { prefix, words } = bech32.decode(bech32str, BECH32_MAX_SIZE);
return { type: prefix, data: secp.utils.bytesToHex(bech32.fromWords(words)) };
};

if (process.argv[2]) {
console.log(bech32ToHex(process.argv[2] as string));
} else {
for await (const line: Buffer of process.stdin) {
const bech32str = line.toString().trim();
if (bech32str) {
console.log(bech32ToHex(bech32str));
}
}
}