go-blockchain/wallet/ring.go
Claude 7e31e706c5
feat(wallet): RPCRingSelector for decoy output selection
RingSelector interface with RPCRingSelector that fetches random outputs
from the daemon, excludes the real output and duplicates, and returns
the requested ring size.

Co-Authored-By: Charon <charon@lethean.io>
2026-02-20 23:20:15 +00:00

75 lines
1.9 KiB
Go

// Copyright (c) 2017-2026 Lethean (https://lt.hn)
//
// Licensed under the European Union Public Licence (EUPL) version 1.2.
// You may obtain a copy of the licence at:
//
// https://joinup.ec.europa.eu/software/page/eupl/licence-eupl
//
// SPDX-License-Identifier: EUPL-1.2
package wallet
import (
"fmt"
"forge.lthn.ai/core/go-blockchain/rpc"
"forge.lthn.ai/core/go-blockchain/types"
)
// RingMember is a public key and global index used in ring construction.
type RingMember struct {
PublicKey types.PublicKey
GlobalIndex uint64
}
// RingSelector picks decoy outputs for ring signatures.
type RingSelector interface {
SelectRing(amount uint64, realGlobalIndex uint64, ringSize int) ([]RingMember, error)
}
// RPCRingSelector fetches decoys from the daemon via RPC.
type RPCRingSelector struct {
client *rpc.Client
}
// NewRPCRingSelector returns a RingSelector backed by the given RPC client.
func NewRPCRingSelector(client *rpc.Client) *RPCRingSelector {
return &RPCRingSelector{client: client}
}
// SelectRing fetches random outputs from the daemon and returns ringSize
// decoy members, excluding the real output and any duplicates.
func (s *RPCRingSelector) SelectRing(amount uint64, realGlobalIndex uint64, ringSize int) ([]RingMember, error) {
outs, err := s.client.GetRandomOutputs(amount, ringSize+5)
if err != nil {
return nil, fmt.Errorf("wallet: get random outputs: %w", err)
}
var members []RingMember
seen := map[uint64]bool{realGlobalIndex: true}
for _, out := range outs {
if seen[out.GlobalIndex] {
continue
}
seen[out.GlobalIndex] = true
pk, err := types.PublicKeyFromHex(out.PublicKey)
if err != nil {
continue
}
members = append(members, RingMember{
PublicKey: pk,
GlobalIndex: out.GlobalIndex,
})
if len(members) >= ringSize {
break
}
}
if len(members) < ringSize {
return nil, fmt.Errorf("wallet: insufficient decoys: got %d, need %d",
len(members), ringSize)
}
return members, nil
}