refactor(types): centralise to-key target extraction
Some checks are pending
Security Scan / security (push) Waiting to run
Test / Test (push) Waiting to run

Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
Virgil 2026-04-04 20:50:54 +00:00
parent 92cb5a8fbb
commit 92628cec35
5 changed files with 33 additions and 5 deletions

View file

@ -55,9 +55,11 @@ func (c *Chain) GetRingOutputs(height, amount uint64, offsets []uint64) ([]types
// TxOutHTLC selects redeem vs refund based on whether the spending height is
// before or after the contract expiration.
func ringOutputSpendKey(height uint64, target types.TxOutTarget) (types.PublicKey, error) {
switch t := target.(type) {
case types.TxOutToKey:
if t, ok := types.AsTxOutToKey(target); ok {
return t.Key, nil
}
switch t := target.(type) {
case types.TxOutMultisig:
if len(t.Keys) == 0 {
return types.PublicKey{}, fmt.Errorf("unsupported multisig target with no keys")

View file

@ -325,7 +325,7 @@ func (m *ExplorerModel) viewTxDetail() string {
for i, output := range tx.Vout {
switch v := output.(type) {
case types.TxOutputBare:
if targetKey, ok := v.Target.(types.TxOutToKey); ok {
if targetKey, ok := types.AsTxOutToKey(v.Target); ok {
b.WriteString(fmt.Sprintf(" [%d] bare amount=%d key=%x\n", i, v.Amount, targetKey.Key[:4]))
} else {
b.WriteString(fmt.Sprintf(" [%d] bare amount=%d target=%T\n", i, v.Amount, v.Target))

View file

@ -121,6 +121,13 @@ type TxOutTarget interface {
TargetType() uint8
}
// AsTxOutToKey returns target as a TxOutToKey when it is a standard
// transparent output target.
func AsTxOutToKey(target TxOutTarget) (TxOutToKey, bool) {
v, ok := target.(TxOutToKey)
return v, ok
}
// TxOutToKey is the txout_to_key target variant. On the wire it is
// serialised as a 33-byte packed blob: 32-byte public key + 1-byte mix_attr.
type TxOutToKey struct {
@ -219,7 +226,7 @@ func (t TxInputHTLC) InputType() uint8 { return InputTypeHTLC }
// TxInputMultisig spends from a multisig output (HF1+).
type TxInputMultisig struct {
Amount uint64
MultisigOutID Hash // 32-byte hash identifying the multisig output
MultisigOutID Hash // 32-byte hash identifying the multisig output
SigsCount uint64
EtcDetails []byte // opaque variant vector
}

View file

@ -14,6 +14,25 @@ func TestTxOutToKey_TargetType_Good(t *testing.T) {
}
}
func TestAsTxOutToKey_Good(t *testing.T) {
target, ok := AsTxOutToKey(TxOutToKey{Key: PublicKey{1}, MixAttr: 7})
if !ok {
t.Fatal("AsTxOutToKey: expected true for TxOutToKey target")
}
if target.Key != (PublicKey{1}) {
t.Errorf("Key: got %x, want %x", target.Key, PublicKey{1})
}
if target.MixAttr != 7 {
t.Errorf("MixAttr: got %d, want %d", target.MixAttr, 7)
}
}
func TestAsTxOutToKey_Bad(t *testing.T) {
if _, ok := AsTxOutToKey(TxOutHTLC{}); ok {
t.Fatal("AsTxOutToKey: expected false for non-to-key target")
}
}
func TestTxOutMultisig_TargetType_Good(t *testing.T) {
var target TxOutTarget = TxOutMultisig{MinimumSigs: 2, Keys: []PublicKey{{1}, {2}}}
if target.TargetType() != TargetTypeMultisig {

View file

@ -64,7 +64,7 @@ func (s *V1Scanner) ScanTransaction(tx *types.Transaction, txHash types.Hash,
continue
}
targetKey, ok := bare.Target.(types.TxOutToKey)
targetKey, ok := types.AsTxOutToKey(bare.Target)
if !ok {
continue
}