use xhex everywhere.

This commit is contained in:
fiatjaf
2025-11-21 21:16:34 -03:00
parent 61b9717c5c
commit 55a43e46b7
46 changed files with 185 additions and 177 deletions

View File

@@ -3,13 +3,13 @@ package nip60
import (
"crypto/rand"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
"fiatjaf.com/nostr"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/decred/dcrd/dcrec/secp256k1/v4"
@@ -66,7 +66,7 @@ func createBlindedMessages(
if _, err := rand.Read(secretBytes); err != nil {
return nil, nil, nil, err
}
secret = hex.EncodeToString(secretBytes)
secret = nostr.HexEncodeToString(secretBytes)
}
B_, r, err := crypto.BlindMessage(secret, r)
@@ -92,7 +92,7 @@ func signInput(
return "", fmt.Errorf("failed to sign: %w", err)
}
witness, _ := json.Marshal(nut11.P2PKWitness{
Signatures: []string{hex.EncodeToString(signature.Serialize())},
Signatures: []string{nostr.HexEncodeToString(signature.Serialize())},
})
return string(witness), nil
}
@@ -101,14 +101,14 @@ func signOutput(
privateKey *btcec.PrivateKey,
output cashu.BlindedMessage,
) (string, error) {
msg, _ := hex.DecodeString(output.B_)
msg, _ := nostr.HexDecodeString(output.B_)
hash := sha256.Sum256(msg)
signature, err := schnorr.Sign(privateKey, hash[:])
if err != nil {
return "", fmt.Errorf("failed to sign: %w", err)
}
witness, _ := json.Marshal(nut11.P2PKWitness{
Signatures: []string{hex.EncodeToString(signature.Serialize())},
Signatures: []string{nostr.HexEncodeToString(signature.Serialize())},
})
return string(witness), nil
}
@@ -143,7 +143,7 @@ func constructProofs(
dleq = &cashu.DLEQProof{
E: blindedSignature.DLEQ.E,
S: blindedSignature.DLEQ.S,
R: hex.EncodeToString(prep.rs[i].Serialize()),
R: nostr.HexEncodeToString(prep.rs[i].Serialize()),
}
}
}
@@ -170,7 +170,7 @@ func unblindSignature(C_str string, r *secp256k1.PrivateKey, key *secp256k1.Publ
string,
error,
) {
C_bytes, err := hex.DecodeString(C_str)
C_bytes, err := nostr.HexDecodeString(C_str)
if err != nil {
return "", err
}
@@ -180,14 +180,14 @@ func unblindSignature(C_str string, r *secp256k1.PrivateKey, key *secp256k1.Publ
}
C := crypto.UnblindSignature(C_, r, key)
Cstr := hex.EncodeToString(C.SerializeCompressed())
Cstr := nostr.HexEncodeToString(C.SerializeCompressed())
return Cstr, nil
}
func ParseKeysetKeys(keys nut01.KeysMap) (map[uint64]*btcec.PublicKey, error) {
parsedKeys := make(map[uint64]*btcec.PublicKey)
for amount, pkh := range keys {
pkb, err := hex.DecodeString(pkh)
pkb, err := nostr.HexDecodeString(pkh)
if err != nil {
return nil, err
}

View File

@@ -2,7 +2,6 @@ package nip60
import (
"context"
"encoding/hex"
"fmt"
"slices"
@@ -42,7 +41,7 @@ func (opts SendOptions) asSpendingCondition(refund *btcec.PublicKey) *nut10.Spen
return &nut10.SpendingCondition{
Kind: nut10.HTLC,
Data: hex.EncodeToString(opts.Hashlock[:]),
Data: nostr.HexEncodeToString(opts.Hashlock[:]),
Tags: nut11.SerializeP2PKTags(tags),
}
} else if opts.P2PK != nil {
@@ -62,7 +61,7 @@ func (opts SendOptions) asSpendingCondition(refund *btcec.PublicKey) *nut10.Spen
return &nut10.SpendingCondition{
Kind: nut10.P2PK,
Data: hex.EncodeToString(opts.P2PK.SerializeCompressed()),
Data: nostr.HexEncodeToString(opts.P2PK.SerializeCompressed()),
Tags: nut11.SerializeP2PKTags(tags),
}
} else {

View File

@@ -2,7 +2,6 @@ package nip60
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"slices"
@@ -344,7 +343,7 @@ func (w *Wallet) SetPrivateKey(ctx context.Context, privateKey string) error {
return fmt.Errorf("can't do write operations: missing PublishUpdate function")
}
skb, err := hex.DecodeString(privateKey)
skb, err := nostr.HexDecodeString(privateKey)
if err != nil {
return err
}
@@ -378,7 +377,7 @@ func (w *Wallet) toEvent(ctx context.Context, kr nostr.Keyer, evt *nostr.Event)
encryptedTags := make(nostr.Tags, 0, 1+len(w.Mints))
if w.PrivateKey != nil {
encryptedTags = append(encryptedTags, nostr.Tag{"privkey", hex.EncodeToString(w.PrivateKey.Serialize())})
encryptedTags = append(encryptedTags, nostr.Tag{"privkey", nostr.HexEncodeToString(w.PrivateKey.Serialize())})
}
for _, mint := range w.Mints {
@@ -433,7 +432,7 @@ func (w *Wallet) parse(ctx context.Context, kr nostr.Keyer, evt *nostr.Event) er
case "mint":
mints = append(mints, tag[1])
case "privkey":
skb, err := hex.DecodeString(tag[1])
skb, err := nostr.HexDecodeString(tag[1])
if err != nil {
return fmt.Errorf("failed to parse private key: %w", err)
}