mirror of
https://github.com/nbd-wtf/go-nostr.git
synced 2025-09-28 04:49:33 +02:00
nip60: small fixes.
This commit is contained in:
@@ -7,8 +7,6 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"slices"
|
||||
|
||||
"github.com/btcsuite/btcd/btcec/v2"
|
||||
"github.com/btcsuite/btcd/btcec/v2/schnorr"
|
||||
@@ -82,65 +80,9 @@ func createBlindedMessages(
|
||||
return blindedMessages, secrets, rs, nil
|
||||
}
|
||||
|
||||
func splitWalletTarget(proofs cashu.Proofs, amountToSplit uint64, mint string) []uint64 {
|
||||
target := 3
|
||||
|
||||
// amounts that are in wallet
|
||||
amountsInWallet := make([]uint64, len(proofs))
|
||||
for i, proof := range proofs {
|
||||
amountsInWallet[i] = proof.Amount
|
||||
}
|
||||
slices.Sort(amountsInWallet)
|
||||
|
||||
allPossibleAmounts := make([]uint64, crypto.MAX_ORDER)
|
||||
for i := 0; i < crypto.MAX_ORDER; i++ {
|
||||
amount := uint64(math.Pow(2, float64(i)))
|
||||
allPossibleAmounts[i] = amount
|
||||
}
|
||||
|
||||
// based on amounts that are already in the wallet
|
||||
// define what amounts wanted to reach target
|
||||
var neededAmounts []uint64
|
||||
for _, amount := range allPossibleAmounts {
|
||||
count := cashu.Count(amountsInWallet, amount)
|
||||
timesToAdd := cashu.Max(0, uint64(target)-uint64(count))
|
||||
for i := 0; i < int(timesToAdd); i++ {
|
||||
neededAmounts = append(neededAmounts, amount)
|
||||
}
|
||||
}
|
||||
slices.Sort(neededAmounts)
|
||||
|
||||
// fill in based on the needed amounts
|
||||
// that are below the amount passed (amountToSplit)
|
||||
var amounts []uint64
|
||||
var amountsSum uint64 = 0
|
||||
for amountsSum < amountToSplit {
|
||||
if len(neededAmounts) > 0 {
|
||||
if amountsSum+neededAmounts[0] > amountToSplit {
|
||||
break
|
||||
}
|
||||
amounts = append(amounts, neededAmounts[0])
|
||||
amountsSum += neededAmounts[0]
|
||||
neededAmounts = slices.Delete(neededAmounts, 0, 1)
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
remainingAmount := amountToSplit - amountsSum
|
||||
if remainingAmount > 0 {
|
||||
amounts = append(amounts, cashu.AmountSplit(remainingAmount)...)
|
||||
}
|
||||
slices.Sort(amounts)
|
||||
|
||||
return amounts
|
||||
}
|
||||
|
||||
func signInput(
|
||||
privateKey *btcec.PrivateKey,
|
||||
publicKey *btcec.PublicKey,
|
||||
proof cashu.Proof,
|
||||
nut10Secret nut10.WellKnownSecret,
|
||||
) (string, error) {
|
||||
hash := sha256.Sum256([]byte(proof.Secret))
|
||||
signature, err := schnorr.Sign(privateKey, hash[:])
|
||||
|
@@ -29,7 +29,7 @@ func (w *Wallet) ReceiveToken(ctx context.Context, serializedToken string) error
|
||||
switch nut10Secret.Kind {
|
||||
case nut10.P2PK:
|
||||
swapOpts = append(swapOpts, WithSignedOutputs())
|
||||
proofs[i].Witness, err = signInput(w.PrivateKey, w.PublicKey, proof, nut10Secret)
|
||||
proofs[i].Witness, err = signInput(w.PrivateKey, proof)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to sign locked proof %d: %w", i, err)
|
||||
}
|
||||
|
@@ -64,10 +64,12 @@ func (w *Wallet) SwapProofs(
|
||||
fee := calculateFee(proofs, keysets)
|
||||
if targetAmount < proofsAmount {
|
||||
// we'll get the exact target, then a change, and fee will be taken from the change
|
||||
principalAmount = targetAmount
|
||||
changeAmount = proofsAmount - targetAmount - fee
|
||||
} else if targetAmount == proofsAmount {
|
||||
// we're swapping everything, so take the fee from the principal
|
||||
principalAmount = targetAmount - fee
|
||||
changeAmount = 0
|
||||
} else {
|
||||
return nil, nil, fmt.Errorf("can't swap for more than we are sending: %d > %d",
|
||||
targetAmount, proofsAmount)
|
||||
@@ -99,7 +101,7 @@ func (w *Wallet) SwapProofs(
|
||||
|
||||
res, err := client.PostSwap(ctx, mint, req)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("failed to claim received tokens at %s: %w", mint, err)
|
||||
return nil, nil, fmt.Errorf("failed to swap tokens at %s: %w", mint, err)
|
||||
}
|
||||
|
||||
// build the proofs locally from mint's response
|
||||
|
Reference in New Issue
Block a user