Skip to content

Commit 6112ca1

Browse files
committed
asset: move GroupKeyRevealEncoder and Decoder from proof package
Relocate GroupKeyRevealEncoder and GroupKeyRevealDecoder to the asset package to allow direct interrogation in unit tests.
1 parent 11f9159 commit 6112ca1

File tree

3 files changed

+54
-56
lines changed

3 files changed

+54
-56
lines changed

asset/encoding.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,3 +895,55 @@ func AltLeavesDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
895895
}
896896
return tlv.NewTypeForEncodingErr(val, "[]*AltLeaf")
897897
}
898+
899+
func GroupKeyRevealEncoder(w io.Writer, val any, _ *[8]byte) error {
900+
if t, ok := val.(*GroupKeyReveal); ok {
901+
if err := (*t).Encode(w); err != nil {
902+
return fmt.Errorf("unable to encode group key "+
903+
"reveal: %w", err)
904+
}
905+
906+
return nil
907+
}
908+
909+
return tlv.NewTypeForEncodingErr(val, "GroupKeyReveal")
910+
}
911+
912+
func GroupKeyRevealDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
913+
// Return early if the val is not a pointer to a GroupKeyReveal.
914+
typ, ok := val.(*GroupKeyReveal)
915+
if !ok {
916+
return tlv.NewTypeForEncodingErr(val, "GroupKeyReveal")
917+
}
918+
919+
// If the length is less than or equal to the sum of the lengths of the
920+
// internal key and the tapscript root, then we'll attempt to decode it
921+
// as a GroupKeyRevealV0.
922+
internalKeyLen := uint64(btcec.PubKeyBytesLenCompressed)
923+
tapscriptRootLen := uint64(sha256.Size)
924+
925+
if l <= internalKeyLen+tapscriptRootLen {
926+
// Attempt decoding with GroupKeyRevealV0.
927+
var gkrV0 GroupKeyRevealV0
928+
929+
err := gkrV0.Decode(r, buf, l)
930+
if err != nil {
931+
return fmt.Errorf("group key reveal V0 decode "+
932+
"error: %w", err)
933+
}
934+
935+
*typ = &gkrV0
936+
return nil
937+
}
938+
939+
// Attempt decoding with GroupKeyRevealV1.
940+
var gkrV1 GroupKeyRevealV1
941+
942+
err := gkrV1.Decode(r, buf, l)
943+
if err != nil {
944+
return fmt.Errorf("group key reveal V1 decode error: %w", err)
945+
}
946+
947+
*typ = &gkrV1
948+
return nil
949+
}

proof/encoding.go

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@ package proof
22

33
import (
44
"bytes"
5-
"crypto/sha256"
65
"fmt"
76
"io"
87
"math"
98

109
"github.com/btcsuite/btcd/blockchain"
11-
"github.com/btcsuite/btcd/btcec/v2"
1210
"github.com/btcsuite/btcd/wire"
1311
"github.com/lightninglabs/taproot-assets/asset"
1412
"github.com/lightningnetwork/lnd/tlv"
@@ -465,55 +463,3 @@ func GenesisRevealDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
465463

466464
return tlv.NewTypeForEncodingErr(val, "GenesisReveal")
467465
}
468-
469-
func GroupKeyRevealEncoder(w io.Writer, val any, _ *[8]byte) error {
470-
if t, ok := val.(*asset.GroupKeyReveal); ok {
471-
if err := (*t).Encode(w); err != nil {
472-
return fmt.Errorf("unable to encode group key "+
473-
"reveal: %w", err)
474-
}
475-
476-
return nil
477-
}
478-
479-
return tlv.NewTypeForEncodingErr(val, "GroupKeyReveal")
480-
}
481-
482-
func GroupKeyRevealDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
483-
// Return early if the val is not a pointer to a GroupKeyReveal.
484-
typ, ok := val.(*asset.GroupKeyReveal)
485-
if !ok {
486-
return tlv.NewTypeForEncodingErr(val, "GroupKeyReveal")
487-
}
488-
489-
// If the length is less than or equal to the sum of the lengths of the
490-
// internal key and the tapscript root, then we'll attempt to decode it
491-
// as a GroupKeyRevealV0.
492-
internalKeyLen := uint64(btcec.PubKeyBytesLenCompressed)
493-
tapscriptRootLen := uint64(sha256.Size)
494-
495-
if l <= internalKeyLen+tapscriptRootLen {
496-
// Attempt decoding with GroupKeyRevealV0.
497-
var gkrV0 asset.GroupKeyRevealV0
498-
499-
err := gkrV0.Decode(r, buf, l)
500-
if err != nil {
501-
return fmt.Errorf("group key reveal V0 decode "+
502-
"error: %w", err)
503-
}
504-
505-
*typ = &gkrV0
506-
return nil
507-
}
508-
509-
// Attempt decoding with GroupKeyRevealV1.
510-
var gkrV1 asset.GroupKeyRevealV1
511-
512-
err := gkrV1.Decode(r, buf, l)
513-
if err != nil {
514-
return fmt.Errorf("group key reveal V1 decode error: %w", err)
515-
}
516-
517-
*typ = &gkrV1
518-
return nil
519-
}

proof/records.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ func GroupKeyRevealRecord(reveal *asset.GroupKeyReveal) tlv.Record {
389389
)
390390
}
391391
return tlv.MakeDynamicRecord(
392-
GroupKeyRevealType, reveal, recordSize, GroupKeyRevealEncoder,
393-
GroupKeyRevealDecoder,
392+
GroupKeyRevealType, reveal, recordSize,
393+
asset.GroupKeyRevealEncoder, asset.GroupKeyRevealDecoder,
394394
)
395395
}

0 commit comments

Comments
 (0)