2024-01-25 06:19:55 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-06-25 21:18:26 -04:00
|
|
|
"context"
|
2024-05-14 14:23:08 -04:00
|
|
|
"encoding/hex"
|
2024-05-16 18:45:30 -04:00
|
|
|
"encoding/json"
|
2024-01-25 06:19:55 -05:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2024-05-14 14:23:08 -04:00
|
|
|
"github.com/btcsuite/btcd/btcec/v2"
|
|
|
|
"github.com/btcsuite/btcd/btcec/v2/schnorr/musig2"
|
2024-05-16 18:45:30 -04:00
|
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
2024-07-30 10:43:14 -04:00
|
|
|
"github.com/fiatjaf/cli/v3"
|
2024-01-25 06:19:55 -05:00
|
|
|
"github.com/nbd-wtf/go-nostr"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip19"
|
|
|
|
"github.com/nbd-wtf/go-nostr/nip49"
|
|
|
|
)
|
|
|
|
|
|
|
|
var key = &cli.Command{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "key",
|
|
|
|
Usage: "operations on secret keys: generate, derive, encrypt, decrypt.",
|
|
|
|
Description: ``,
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Commands: []*cli.Command{
|
2024-01-25 06:19:55 -05:00
|
|
|
generate,
|
|
|
|
public,
|
2024-09-17 10:28:26 -04:00
|
|
|
encryptKey,
|
|
|
|
decryptKey,
|
2024-05-14 14:23:08 -04:00
|
|
|
combine,
|
2024-01-25 06:19:55 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var generate = &cli.Command{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "generate",
|
|
|
|
Usage: "generates a secret key",
|
|
|
|
Description: ``,
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-01-25 06:19:55 -05:00
|
|
|
sec := nostr.GeneratePrivateKey()
|
|
|
|
stdout(sec)
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var public = &cli.Command{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "public",
|
|
|
|
Usage: "computes a public key from a secret key",
|
|
|
|
Description: ``,
|
|
|
|
ArgsUsage: "[secret]",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
|
|
|
for sec := range getSecretKeysFromStdinLinesOrSlice(ctx, c, c.Args().Slice()) {
|
2024-01-25 06:19:55 -05:00
|
|
|
pubkey, err := nostr.GetPublicKey(sec)
|
|
|
|
if err != nil {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "failed to derive public key: %s", err)
|
2024-01-25 06:19:55 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stdout(pubkey)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-09-17 10:28:26 -04:00
|
|
|
var encryptKey = &cli.Command{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "encrypt",
|
|
|
|
Usage: "encrypts a secret key and prints an ncryptsec code",
|
|
|
|
Description: `uses the NIP-49 standard.`,
|
|
|
|
ArgsUsage: "<secret> <password>",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-01-25 06:19:55 -05:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "logn",
|
|
|
|
Usage: "the bigger the number the harder it will be to bruteforce the password",
|
|
|
|
Value: 16,
|
|
|
|
DefaultText: "16",
|
|
|
|
},
|
|
|
|
},
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-06-12 07:54:00 -04:00
|
|
|
keys := make([]string, 0, 1)
|
2024-02-03 08:03:32 -05:00
|
|
|
var password string
|
|
|
|
switch c.Args().Len() {
|
|
|
|
case 1:
|
|
|
|
password = c.Args().Get(0)
|
|
|
|
case 2:
|
2024-06-12 07:54:00 -04:00
|
|
|
keys = append(keys, c.Args().Get(0))
|
2024-02-03 08:03:32 -05:00
|
|
|
password = c.Args().Get(1)
|
|
|
|
}
|
2024-01-25 06:19:55 -05:00
|
|
|
if password == "" {
|
|
|
|
return fmt.Errorf("no password given")
|
|
|
|
}
|
2024-06-25 21:18:26 -04:00
|
|
|
for sec := range getSecretKeysFromStdinLinesOrSlice(ctx, c, keys) {
|
2024-01-25 06:19:55 -05:00
|
|
|
ncryptsec, err := nip49.Encrypt(sec, password, uint8(c.Int("logn")), 0x02)
|
|
|
|
if err != nil {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "failed to encrypt: %s", err)
|
2024-01-25 06:19:55 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
stdout(ncryptsec)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-09-17 10:28:26 -04:00
|
|
|
var decryptKey = &cli.Command{
|
2024-08-07 10:46:07 -04:00
|
|
|
Name: "decrypt",
|
|
|
|
Usage: "takes an ncrypsec and a password and decrypts it into an nsec",
|
|
|
|
Description: `uses the NIP-49 standard.`,
|
|
|
|
ArgsUsage: "<ncryptsec-code> <password>",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-06-25 12:46:15 -04:00
|
|
|
var ncryptsec string
|
2024-02-03 08:03:32 -05:00
|
|
|
var password string
|
|
|
|
switch c.Args().Len() {
|
|
|
|
case 2:
|
2024-06-25 12:46:15 -04:00
|
|
|
ncryptsec = c.Args().Get(0)
|
2024-02-03 08:03:32 -05:00
|
|
|
password = c.Args().Get(1)
|
2024-06-25 12:46:15 -04:00
|
|
|
if password == "" {
|
|
|
|
return fmt.Errorf("no password given")
|
|
|
|
}
|
2024-01-25 06:19:55 -05:00
|
|
|
sec, err := nip49.Decrypt(ncryptsec, password)
|
|
|
|
if err != nil {
|
2024-06-25 12:46:15 -04:00
|
|
|
return fmt.Errorf("failed to decrypt: %s", err)
|
2024-01-25 06:19:55 -05:00
|
|
|
}
|
2024-07-12 18:15:13 -04:00
|
|
|
stdout(sec)
|
2024-06-25 12:46:15 -04:00
|
|
|
return nil
|
|
|
|
case 1:
|
|
|
|
if arg := c.Args().Get(0); strings.HasPrefix(arg, "ncryptsec1") {
|
|
|
|
ncryptsec = arg
|
|
|
|
if res, err := promptDecrypt(ncryptsec); err != nil {
|
|
|
|
return err
|
|
|
|
} else {
|
|
|
|
stdout(res)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
password = c.Args().Get(0)
|
|
|
|
for ncryptsec := range getStdinLinesOrArgumentsFromSlice([]string{ncryptsec}) {
|
|
|
|
sec, err := nip49.Decrypt(ncryptsec, password)
|
|
|
|
if err != nil {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "failed to decrypt: %s", err)
|
2024-06-25 12:46:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2024-07-12 18:15:13 -04:00
|
|
|
stdout(sec)
|
2024-06-25 12:46:15 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid number of arguments")
|
2024-01-25 06:19:55 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-05-14 14:23:08 -04:00
|
|
|
var combine = &cli.Command{
|
2024-05-16 18:45:30 -04:00
|
|
|
Name: "combine",
|
|
|
|
Usage: "combines two or more pubkeys using musig2",
|
|
|
|
Description: `The public keys must have 33 bytes (66 characters hex), with the 02 or 03 prefix. It is common in Nostr to drop that first byte, so you'll have to derive the public keys again from the private keys in order to get it back.
|
|
|
|
|
|
|
|
However, if the intent is to check if two existing Nostr pubkeys match a given combined pubkey, then it might be sufficient to calculate the combined key for all the possible combinations of pubkeys in the input.`,
|
2024-08-07 10:46:07 -04:00
|
|
|
ArgsUsage: "[pubkey...]",
|
|
|
|
DisableSliceFlagSeparator: true,
|
2024-06-25 21:18:26 -04:00
|
|
|
Action: func(ctx context.Context, c *cli.Command) error {
|
2024-05-16 18:45:30 -04:00
|
|
|
type Combination struct {
|
|
|
|
Variants []string `json:"input_variants"`
|
|
|
|
Output struct {
|
|
|
|
XOnly string `json:"x_only"`
|
|
|
|
Variant string `json:"variant"`
|
|
|
|
} `json:"combined_key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Result struct {
|
|
|
|
Keys []string `json:"keys"`
|
|
|
|
Combinations []Combination `json:"combinations"`
|
|
|
|
}
|
|
|
|
|
|
|
|
result := Result{}
|
|
|
|
|
|
|
|
result.Keys = c.Args().Slice()
|
|
|
|
keyGroups := make([][]*btcec.PublicKey, 0, len(result.Keys))
|
|
|
|
|
|
|
|
for i, keyhex := range result.Keys {
|
|
|
|
keyb, err := hex.DecodeString(keyhex)
|
2024-05-14 14:23:08 -04:00
|
|
|
if err != nil {
|
2024-05-16 18:45:30 -04:00
|
|
|
return fmt.Errorf("error parsing key %s: %w", keyhex, err)
|
2024-05-14 14:23:08 -04:00
|
|
|
}
|
|
|
|
|
2024-05-16 18:45:30 -04:00
|
|
|
if len(keyb) == 32 /* we'll use both the 02 and the 03 prefix versions */ {
|
|
|
|
group := make([]*btcec.PublicKey, 2)
|
|
|
|
for i, prefix := range []byte{0x02, 0x03} {
|
|
|
|
pubk, err := btcec.ParsePubKey(append([]byte{prefix}, keyb...))
|
|
|
|
if err != nil {
|
2024-09-22 18:04:21 -04:00
|
|
|
log("error parsing key %s: %s", keyhex, err)
|
2024-05-16 18:45:30 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
group[i] = pubk
|
|
|
|
}
|
|
|
|
keyGroups = append(keyGroups, group)
|
|
|
|
} else /* assume it's 33 */ {
|
|
|
|
pubk, err := btcec.ParsePubKey(keyb)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error parsing key %s: %w", keyhex, err)
|
|
|
|
}
|
|
|
|
keyGroups = append(keyGroups, []*btcec.PublicKey{pubk})
|
|
|
|
|
|
|
|
// remove the leading byte from the output just so it is all uniform
|
|
|
|
result.Keys[i] = result.Keys[i][2:]
|
2024-05-14 14:23:08 -04:00
|
|
|
}
|
2024-05-16 18:45:30 -04:00
|
|
|
}
|
2024-05-14 14:23:08 -04:00
|
|
|
|
2024-05-16 18:45:30 -04:00
|
|
|
result.Combinations = make([]Combination, 0, 16)
|
|
|
|
|
|
|
|
var fn func(prepend int, curr []int)
|
|
|
|
fn = func(prepend int, curr []int) {
|
|
|
|
curr = append([]int{prepend}, curr...)
|
|
|
|
if len(curr) == len(keyGroups) {
|
|
|
|
combi := Combination{
|
|
|
|
Variants: make([]string, len(keyGroups)),
|
|
|
|
}
|
|
|
|
|
|
|
|
combining := make([]*btcec.PublicKey, len(keyGroups))
|
|
|
|
for g, altKeys := range keyGroups {
|
|
|
|
altKey := altKeys[curr[g]]
|
|
|
|
variant := secp256k1.PubKeyFormatCompressedEven
|
|
|
|
if altKey.Y().Bit(0) == 1 {
|
|
|
|
variant = secp256k1.PubKeyFormatCompressedOdd
|
|
|
|
}
|
|
|
|
combi.Variants[g] = hex.EncodeToString([]byte{variant})
|
|
|
|
combining[g] = altKey
|
|
|
|
}
|
|
|
|
|
|
|
|
agg, _, _, err := musig2.AggregateKeys(combining, true)
|
|
|
|
if err != nil {
|
2024-09-22 18:04:21 -04:00
|
|
|
log("error aggregating: %s", err)
|
2024-05-16 18:45:30 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
serialized := agg.FinalKey.SerializeCompressed()
|
|
|
|
combi.Output.XOnly = hex.EncodeToString(serialized[1:])
|
|
|
|
combi.Output.Variant = hex.EncodeToString(serialized[0:1])
|
|
|
|
result.Combinations = append(result.Combinations, combi)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fn(0, curr)
|
|
|
|
if len(keyGroups[len(keyGroups)-len(curr)-1]) > 1 {
|
|
|
|
fn(1, curr)
|
|
|
|
}
|
2024-05-14 14:23:08 -04:00
|
|
|
}
|
|
|
|
|
2024-05-16 18:45:30 -04:00
|
|
|
fn(0, nil)
|
|
|
|
if len(keyGroups[len(keyGroups)-1]) > 1 {
|
|
|
|
fn(1, nil)
|
2024-05-14 14:23:08 -04:00
|
|
|
}
|
|
|
|
|
2024-05-16 18:45:30 -04:00
|
|
|
res, _ := json.MarshalIndent(result, "", " ")
|
2024-09-21 11:02:09 -04:00
|
|
|
stdout(string(res))
|
2024-05-16 18:45:30 -04:00
|
|
|
|
2024-05-14 14:23:08 -04:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2024-09-21 11:02:09 -04:00
|
|
|
func getSecretKeysFromStdinLinesOrSlice(ctx context.Context, _ *cli.Command, keys []string) chan string {
|
2024-01-25 06:19:55 -05:00
|
|
|
ch := make(chan string)
|
|
|
|
go func() {
|
2024-03-19 10:34:59 -04:00
|
|
|
for sec := range getStdinLinesOrArgumentsFromSlice(keys) {
|
2024-01-25 06:19:55 -05:00
|
|
|
if sec == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(sec, "nsec1") {
|
|
|
|
_, data, err := nip19.Decode(sec)
|
|
|
|
if err != nil {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "invalid nsec code: %s", err)
|
2024-01-25 06:19:55 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
sec = data.(string)
|
|
|
|
}
|
2024-07-12 13:04:14 -04:00
|
|
|
sec = leftPadKey(sec)
|
2024-01-25 06:19:55 -05:00
|
|
|
if !nostr.IsValid32ByteHex(sec) {
|
2024-07-11 14:33:19 -04:00
|
|
|
ctx = lineProcessingError(ctx, "invalid hex key")
|
2024-01-25 06:19:55 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
ch <- sec
|
|
|
|
}
|
|
|
|
close(ch)
|
|
|
|
}()
|
|
|
|
return ch
|
|
|
|
}
|