Last active
August 8, 2025 15:21
-
-
Save koteitan/f218b8028adab50877379339d7b5d63c to your computer and use it in GitHub Desktop.
Genrates a pair of secret keys that share the same public key
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { nip19, getPublicKey } from 'nostr-tools' | |
| import crypto from 'crypto' | |
| // secp256k1の位数 | |
| const n = 115792089237316195423570985008687907852837564279074904382605163141518161494337n | |
| // シードから秘密鍵ペアを生成 | |
| function generatePair(seed) { | |
| // シードをSHA256でハッシュ化して32バイトの値を作る | |
| const hash = crypto.createHash('sha256').update(seed).digest() | |
| // ハッシュ値を大きな整数に変換 | |
| let sk1 = BigInt('0x' + hash.toString('hex')) | |
| // 有効範囲[1, n-1]に収める | |
| if (sk1 >= n) sk1 = sk1 % (n - 1n) + 1n | |
| if (sk1 === 0n) sk1 = 1n | |
| // ペアとなる秘密鍵を計算(n - sk1) | |
| const sk2 = n - sk1 | |
| // 16進数文字列に変換(64文字にパディング) | |
| const sk1_hex = sk1.toString(16).padStart(64, '0') | |
| const sk2_hex = sk2.toString(16).padStart(64, '0') | |
| // nsec形式にエンコード | |
| const nsec1 = nip19.nsecEncode(Buffer.from(sk1_hex, 'hex')) | |
| const nsec2 = nip19.nsecEncode(Buffer.from(sk2_hex, 'hex')) | |
| // 公開鍵を生成(両方同じになるはず) | |
| const pubkey1 = getPublicKey(sk1_hex) | |
| const pubkey2 = getPublicKey(sk2_hex) | |
| const npub1 = nip19.npubEncode(pubkey1) | |
| const npub2 = nip19.npubEncode(pubkey2) | |
| return { nsec1, nsec2, npub1, npub2 } | |
| } | |
| // コマンドライン引数からシード値を取得 | |
| const seed = process.argv[2] | |
| // シードが指定されていない場合はヘルプを表示 | |
| if (!seed) { | |
| console.log('使い方: nsec-pair-gen.js <シード値>') | |
| console.log('例: node nsec-pair-gen.js "my secret seed"') | |
| process.exit(1) | |
| } | |
| // ペアを生成 | |
| const result = generatePair(seed) | |
| console.log(`シード: "${seed}"`) | |
| console.log(`秘密鍵1: ${result.nsec1}`) | |
| console.log(`秘密鍵2: ${result.nsec2}`) | |
| console.log(`公開鍵1: ${result.npub1}`) | |
| console.log(`公開鍵2: ${result.npub2}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment