Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional memo to TAP addresses #757

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
address: add address encoding and decoding
This commit adds address encoding and decoding to the TAP address.
  • Loading branch information
sputn1ck committed Jan 5, 2024
commit 9de140660a1cde8adfcf39808315532d17605df9
7 changes: 6 additions & 1 deletion address/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (a *Tap) TaprootOutputKey() (*btcec.PublicKey, error) {
// EncodeRecords determines the non-nil records to include when encoding an
// address at runtime.
func (a *Tap) EncodeRecords() []tlv.Record {
records := make([]tlv.Record, 0, 9)
records := make([]tlv.Record, 0, 10)
records = append(records, newAddressVersionRecord(&a.Version))
records = append(records, newAddressAssetVersionRecord(&a.AssetVersion))
records = append(records, newAddressAssetID(&a.AssetID))
Expand All @@ -349,6 +349,10 @@ func (a *Tap) EncodeRecords() []tlv.Record {
records, newProofCourierAddrRecord(&a.ProofCourierAddr),
)

if a.Memo != "" {
records = append(records, newMemoRecord(&a.Memo))
}

return records
}

Expand All @@ -365,6 +369,7 @@ func (a *Tap) DecodeRecords() []tlv.Record {
newAddressTapscriptSiblingRecord(&a.TapscriptSibling),
newAddressAmountRecord(&a.Amount),
newProofCourierAddrRecord(&a.ProofCourierAddr),
newMemoRecord(&a.Memo),
}
}

Expand Down
21 changes: 21 additions & 0 deletions address/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,24 @@ func VersionDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
}
return tlv.NewTypeForDecodingErr(val, "Version", l, 1)
}

func memoEncoder(w io.Writer, val any, buf *[8]byte) error {
if t, ok := val.(*string); ok {
memoBytes := []byte(*t)
return tlv.EVarBytes(w, &memoBytes, buf)
}
return tlv.NewTypeForEncodingErr(val, "*string")
}

func memoDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
if t, ok := val.(*string); ok {
var memoBytes []byte
err := tlv.DVarBytes(r, &memoBytes, buf, l)
if err != nil {
return err
}
*t = string(memoBytes)
return nil
}
return tlv.NewTypeForDecodingErr(val, "*string", l, l)
}