mirror of https://github.com/fiatjaf/nak.git
fix and simplify `nak decode`.
This commit is contained in:
parent
01be954ae6
commit
5a8c7df811
113
decode.go
113
decode.go
|
@ -3,11 +3,12 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
stdjson "encoding/json"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
|
"fiatjaf.com/nostr/nip05"
|
||||||
"fiatjaf.com/nostr/nip19"
|
"fiatjaf.com/nostr/nip19"
|
||||||
"fiatjaf.com/nostr/sdk"
|
|
||||||
"github.com/urfave/cli/v3"
|
"github.com/urfave/cli/v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -39,88 +40,56 @@ var decode = &cli.Command{
|
||||||
input = input[6:]
|
input = input[6:]
|
||||||
}
|
}
|
||||||
|
|
||||||
var decodeResult DecodeResult
|
_, data, err := nip19.Decode(input)
|
||||||
if b, err := hex.DecodeString(input); err == nil {
|
if err == nil {
|
||||||
if len(b) == 64 {
|
switch v := data.(type) {
|
||||||
decodeResult.HexResult.PossibleTypes = []string{"sig"}
|
case nostr.SecretKey:
|
||||||
decodeResult.HexResult.Signature = hex.EncodeToString(b)
|
stdout(v.Hex())
|
||||||
} else if len(b) == 32 {
|
continue
|
||||||
decodeResult.HexResult.PossibleTypes = []string{"pubkey", "private_key", "event_id"}
|
case nostr.PubKey:
|
||||||
decodeResult.HexResult.ID = hex.EncodeToString(b)
|
stdout(v.Hex())
|
||||||
decodeResult.HexResult.PrivateKey = hex.EncodeToString(b)
|
continue
|
||||||
decodeResult.HexResult.PublicKey = hex.EncodeToString(b)
|
case [32]byte:
|
||||||
} else {
|
stdout(hex.EncodeToString(v[:]))
|
||||||
ctx = lineProcessingError(ctx, "hex string with invalid number of bytes: %d", len(b))
|
continue
|
||||||
|
case nostr.EventPointer:
|
||||||
|
if c.Bool("id") {
|
||||||
|
stdout(v.ID.Hex())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out, _ := stdjson.MarshalIndent(v, "", " ")
|
||||||
|
stdout(string(out))
|
||||||
|
continue
|
||||||
|
case nostr.ProfilePointer:
|
||||||
|
if c.Bool("pubkey") {
|
||||||
|
stdout(v.PublicKey.Hex())
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out, _ := stdjson.MarshalIndent(v, "", " ")
|
||||||
|
stdout(string(out))
|
||||||
|
continue
|
||||||
|
case nostr.EntityPointer:
|
||||||
|
out, _ := stdjson.MarshalIndent(v, "", " ")
|
||||||
|
stdout(string(out))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else if evp := sdk.InputToEventPointer(input); evp != nil {
|
}
|
||||||
decodeResult = DecodeResult{EventPointer: evp}
|
|
||||||
if c.Bool("id") {
|
pp, _ := nip05.QueryIdentifier(ctx, input)
|
||||||
stdout(evp.ID)
|
if pp != nil {
|
||||||
continue
|
|
||||||
}
|
|
||||||
} else if pp := sdk.InputToProfile(ctx, input); pp != nil {
|
|
||||||
decodeResult = DecodeResult{ProfilePointer: pp}
|
|
||||||
if c.Bool("pubkey") {
|
if c.Bool("pubkey") {
|
||||||
stdout(pp.PublicKey)
|
stdout(pp.PublicKey.Hex())
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
} else if prefix, value, err := nip19.Decode(input); err == nil && prefix == "naddr" {
|
out, _ := stdjson.MarshalIndent(pp, "", " ")
|
||||||
if ep, ok := value.(nostr.EntityPointer); ok {
|
stdout(string(out))
|
||||||
decodeResult = DecodeResult{EntityPointer: &ep}
|
|
||||||
} else {
|
|
||||||
ctx = lineProcessingError(ctx, "couldn't decode naddr: %s", err)
|
|
||||||
}
|
|
||||||
} else if prefix, value, err := nip19.Decode(input); err == nil && prefix == "nsec" {
|
|
||||||
decodeResult.PrivateKey.PrivateKey = value.(string)
|
|
||||||
decodeResult.PrivateKey.PublicKey = nostr.GetPublicKey(value.(nostr.SecretKey))
|
|
||||||
} else {
|
|
||||||
ctx = lineProcessingError(ctx, "couldn't decode input '%s': %s", input, err)
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.Bool("pubkey") || c.Bool("id") {
|
ctx = lineProcessingError(ctx, "couldn't decode input '%s'", input)
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
stdout(decodeResult.JSON())
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
exitIfLineProcessingError(ctx)
|
exitIfLineProcessingError(ctx)
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
type DecodeResult struct {
|
|
||||||
*nostr.EventPointer
|
|
||||||
*nostr.ProfilePointer
|
|
||||||
*nostr.EntityPointer
|
|
||||||
HexResult struct {
|
|
||||||
PossibleTypes []string `json:"possible_types"`
|
|
||||||
PublicKey string `json:"pubkey,omitempty"`
|
|
||||||
ID string `json:"event_id,omitempty"`
|
|
||||||
PrivateKey string `json:"private_key,omitempty"`
|
|
||||||
Signature string `json:"sig,omitempty"`
|
|
||||||
}
|
|
||||||
PrivateKey struct {
|
|
||||||
nostr.ProfilePointer
|
|
||||||
PrivateKey string `json:"private_key"`
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (d DecodeResult) JSON() string {
|
|
||||||
var j []byte
|
|
||||||
if d.EventPointer != nil {
|
|
||||||
j, _ = json.MarshalIndent(d.EventPointer, "", " ")
|
|
||||||
} else if d.ProfilePointer != nil {
|
|
||||||
j, _ = json.MarshalIndent(d.ProfilePointer, "", " ")
|
|
||||||
} else if d.EntityPointer != nil {
|
|
||||||
j, _ = json.MarshalIndent(d.EntityPointer, "", " ")
|
|
||||||
} else if len(d.HexResult.PossibleTypes) > 0 {
|
|
||||||
j, _ = json.MarshalIndent(d.HexResult, "", " ")
|
|
||||||
} else if d.PrivateKey.PrivateKey != "" {
|
|
||||||
j, _ = json.MarshalIndent(d.PrivateKey, "", " ")
|
|
||||||
}
|
|
||||||
return string(j)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue