|
| 1 | +package universe |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "crypto/sha256" |
| 6 | + "io" |
| 7 | + |
| 8 | + "github.com/btcsuite/btcd/btcec/v2" |
| 9 | + "github.com/btcsuite/btcd/btcec/v2/schnorr" |
| 10 | + "github.com/lightninglabs/taproot-assets/asset" |
| 11 | + "github.com/lightningnetwork/lnd/tlv" |
| 12 | +) |
| 13 | + |
| 14 | +type ( |
| 15 | + // IgnoreTupleType is the TLV type for the tuple field in a |
| 16 | + // SignedIgnoreTuple. |
| 17 | + IgnoreTupleType = tlv.TlvType0 |
| 18 | + |
| 19 | + // IgnoreSignatureType is the TLV type for the signature field in a |
| 20 | + // SignedIgnoreTuple. |
| 21 | + IgnoreSignatureType = tlv.TlvType2 |
| 22 | +) |
| 23 | + |
| 24 | +// IgnoreTuple represents an asset previous ID that we want to ignore. |
| 25 | +type IgnoreTuple struct { |
| 26 | + asset.PrevID |
| 27 | + |
| 28 | + // Amount is the amount of units that we want to ignore. |
| 29 | + Amount uint64 |
| 30 | +} |
| 31 | + |
| 32 | +func ignoreTupleEncoder(w io.Writer, val any, buf *[8]byte) error { |
| 33 | + if t, ok := val.(*IgnoreTuple); ok { |
| 34 | + err := asset.OutPointEncoder(w, &t.OutPoint, buf) |
| 35 | + if err != nil { |
| 36 | + return err |
| 37 | + } |
| 38 | + if err := asset.IDEncoder(w, &t.ID, buf); err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + err = asset.SerializedKeyEncoder(w, &t.ScriptKey, buf) |
| 42 | + if err != nil { |
| 43 | + return err |
| 44 | + } |
| 45 | + |
| 46 | + return tlv.EUint64(w, &t.Amount, buf) |
| 47 | + } |
| 48 | + return tlv.NewTypeForEncodingErr(val, "*PrevID") |
| 49 | +} |
| 50 | + |
| 51 | +func ignoreTupleDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error { |
| 52 | + if typ, ok := val.(*IgnoreTuple); ok { |
| 53 | + var prevID asset.PrevID |
| 54 | + err := asset.OutPointDecoder(r, &prevID.OutPoint, buf, 0) |
| 55 | + if err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + err = asset.IDDecoder(r, &prevID.ID, buf, sha256.Size) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + if err = asset.SerializedKeyDecoder( |
| 63 | + r, &prevID.ScriptKey, buf, |
| 64 | + btcec.PubKeyBytesLenCompressed, |
| 65 | + ); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + var amt uint64 |
| 70 | + if err = tlv.DUint64(r, &amt, buf, 8); err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + |
| 74 | + *typ = IgnoreTuple{ |
| 75 | + PrevID: prevID, |
| 76 | + Amount: amt, |
| 77 | + } |
| 78 | + return nil |
| 79 | + } |
| 80 | + return tlv.NewTypeForDecodingErr(val, "*PrevID", l, l) |
| 81 | +} |
| 82 | + |
| 83 | +// Record returns the TLV record for the IgnoreTuple. |
| 84 | +func (i *IgnoreTuple) Record() tlv.Record { |
| 85 | + const recordSize = 8 + 36 + sha256.Size + btcec.PubKeyBytesLenCompressed |
| 86 | + |
| 87 | + return tlv.MakeStaticRecord( |
| 88 | + 0, i, recordSize, |
| 89 | + ignoreTupleEncoder, ignoreTupleDecoder, |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +// IgnoreTuples is a slice of IgnoreTuple. |
| 94 | +type IgnoreTuples = []*IgnoreTuple |
| 95 | + |
| 96 | +// IgnoreSignature is a Schnorr signature over an IgnoreTuple. |
| 97 | +// |
| 98 | +// TODO(roasbeef): sig validate methods, sig gen above |
| 99 | +type IgnoreSig struct { |
| 100 | + schnorr.Signature |
| 101 | +} |
| 102 | + |
| 103 | +// Record returns the TLV record for the IgnoreSig. |
| 104 | +func (s *IgnoreSig) Record() tlv.Record { |
| 105 | + // Note that we set the type here as zero, as when used with a |
| 106 | + // tlv.RecordT, the type param will be used as the type. |
| 107 | + return tlv.MakeStaticRecord( |
| 108 | + 0, &s.Signature, schnorr.SignatureSize, |
| 109 | + asset.SchnorrSignatureEncoder, asset.SchnorrSignatureDecoder, |
| 110 | + ) |
| 111 | +} |
| 112 | + |
| 113 | +// Encode serializes the IgnoreSig to the given io.Writer. |
| 114 | +func (s *IgnoreSig) Encode(w io.Writer) error { |
| 115 | + stream, err := tlv.NewStream(s.Record()) |
| 116 | + if err != nil { |
| 117 | + return err |
| 118 | + } |
| 119 | + |
| 120 | + return stream.Encode(w) |
| 121 | +} |
| 122 | + |
| 123 | +// Decode deserializes the IgnoreSig from the given io.Reader. |
| 124 | +func (s *IgnoreSig) Decode(r io.Reader) error { |
| 125 | + stream, err := tlv.NewStream(s.Record()) |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + return stream.Decode(r) |
| 130 | +} |
| 131 | + |
| 132 | +// SignedIgnoreTuple wraps an IgnoreTuple with a signature. |
| 133 | +type SignedIgnoreTuple struct { |
| 134 | + // IgnoreTuple is the tuple that is being signed. |
| 135 | + IgnoreTuple tlv.RecordT[IgnoreTupleType, IgnoreTuple] |
| 136 | + |
| 137 | + // Sig is the signature over the tuple. |
| 138 | + Sig tlv.RecordT[IgnoreSignatureType, IgnoreSig] |
| 139 | +} |
| 140 | + |
| 141 | +// NewSignedIgnoreTuple creates a new SignedIgnoreTuple with the given tuple and |
| 142 | +// sig. |
| 143 | +func NewSignedIgnoreTuple(tuple IgnoreTuple, sig IgnoreSig) SignedIgnoreTuple { |
| 144 | + return SignedIgnoreTuple{ |
| 145 | + IgnoreTuple: tlv.NewRecordT[IgnoreTupleType](tuple), |
| 146 | + Sig: tlv.NewRecordT[IgnoreSignatureType](sig), |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// records returns the records that make up the SignedIgnoreTuple. |
| 151 | +func (s *SignedIgnoreTuple) records() []tlv.Record { |
| 152 | + return []tlv.Record{ |
| 153 | + s.IgnoreTuple.Record(), |
| 154 | + s.Sig.Record(), |
| 155 | + } |
| 156 | +} |
| 157 | + |
| 158 | +// Encode serializes the SignedIgnoreTuple to the given io.Writer. |
| 159 | +func (s *SignedIgnoreTuple) Encode(w io.Writer) error { |
| 160 | + tlvRecords := s.records() |
| 161 | + |
| 162 | + tlvStream, err := tlv.NewStream(tlvRecords...) |
| 163 | + if err != nil { |
| 164 | + return err |
| 165 | + } |
| 166 | + |
| 167 | + return tlvStream.Encode(w) |
| 168 | +} |
| 169 | + |
| 170 | +// Decode deserializes the SignedIgnoreTuple from the given io.Reader. |
| 171 | +func (s *SignedIgnoreTuple) Decode(r io.Reader) error { |
| 172 | + tlvStream, err := tlv.NewStream(s.records()...) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + |
| 177 | + return tlvStream.Decode(r) |
| 178 | +} |
| 179 | + |
| 180 | +// Bytes returns the serialized SignedIgnoreTuple record. |
| 181 | +func (s *SignedIgnoreTuple) Bytes() ([]byte, error) { |
| 182 | + var buf bytes.Buffer |
| 183 | + if err := s.Encode(&buf); err != nil { |
| 184 | + return nil, err |
| 185 | + } |
| 186 | + return buf.Bytes(), nil |
| 187 | +} |
| 188 | + |
| 189 | +// DecodeSignedIgnoreTuple deserializes a SignedIgnoreTuple from the given blob. |
| 190 | +func DecodeSignedIgnoreTuple(blob []byte) (SignedIgnoreTuple, error) { |
| 191 | + var s SignedIgnoreTuple |
| 192 | + err := s.Decode(bytes.NewReader(blob)) |
| 193 | + if err != nil { |
| 194 | + var s SignedIgnoreTuple |
| 195 | + return s, err |
| 196 | + } |
| 197 | + |
| 198 | + return s, nil |
| 199 | +} |
0 commit comments