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

btcutil: Add library functions for BIP-0352 silent payment _send_ support #2244

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions btcutil/psbt/bip32.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"encoding/binary"
)

const (
// uint32Size is the size of a uint32 in bytes.
uint32Size = 4
)

// Bip32Derivation encapsulates the data for the input and output
// Bip32Derivation key-value fields.
//
Expand Down
23 changes: 23 additions & 0 deletions btcutil/psbt/partial_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type POutput struct {
TaprootInternalKey []byte
TaprootTapTree []byte
TaprootBip32Derivation []*TaprootBip32Derivation
SilentPaymentInfo *SilentPaymentInfo
Unknowns []*Unknown
}

Expand Down Expand Up @@ -144,6 +145,18 @@ func (po *POutput) deserialize(r io.Reader) error {
po.TaprootBip32Derivation, taprootDerivation,
)

case SilentPaymentV0InfoOutputType:
if po.SilentPaymentInfo != nil {
return ErrDuplicateKey
}

info, err := ReadSilentPaymentInfo(value)
if err != nil {
return err
}

po.SilentPaymentInfo = info

default:
// A fall through case for any proprietary types.
keyCodeAndData := append(
Expand Down Expand Up @@ -246,6 +259,16 @@ func (po *POutput) serialize(w io.Writer) error {
}
}

if po.SilentPaymentInfo != nil {
err := serializeKVPairWithType(
w, uint8(SilentPaymentV0InfoOutputType), nil,
SerializeSilentPaymentInfo(po.SilentPaymentInfo),
)
if err != nil {
return err
}
}

// Unknown is a special case; we don't have a key type, only a key and
// a value field
for _, kv := range po.Unknowns {
Expand Down
105 changes: 89 additions & 16 deletions btcutil/psbt/psbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,14 @@ type Packet struct {
// produced by this PSBT.
Outputs []POutput

// SilentPaymentShares is a list of ECDH shares that are used to derive
// the shared secret for a silent payment.
SilentPaymentShares []SilentPaymentShare

// SilentPaymentDLEQs is a list of DLEQ proofs that are used to prove
// the validity of the shares for a silent payment.
SilentPaymentDLEQs []SilentPaymentDLEQ

// Unknowns are the set of custom types (global only) within this PSBT.
Unknowns []*Unknown
}
Expand All @@ -161,13 +169,17 @@ func NewFromUnsignedTx(tx *wire.MsgTx) (*Packet, error) {

inSlice := make([]PInput, len(tx.TxIn))
outSlice := make([]POutput, len(tx.TxOut))
spsSlice := make([]SilentPaymentShare, 0)
spdSlice := make([]SilentPaymentDLEQ, 0)
unknownSlice := make([]*Unknown, 0)

return &Packet{
UnsignedTx: tx,
Inputs: inSlice,
Outputs: outSlice,
Unknowns: unknownSlice,
UnsignedTx: tx,
Inputs: inSlice,
Outputs: outSlice,
SilentPaymentShares: spsSlice,
SilentPaymentDLEQs: spdSlice,
Unknowns: unknownSlice,
}, nil
}

Expand Down Expand Up @@ -230,7 +242,11 @@ func NewFromRawBytes(r io.Reader, b64 bool) (*Packet, error) {

// Next we parse any unknowns that may be present, making sure that we
// break at the separator.
var unknownSlice []*Unknown
var (
spsSlice []SilentPaymentShare
spdSlice []SilentPaymentDLEQ
unknownSlice []*Unknown
)
for {
keyint, keydata, err := getKey(r)
if err != nil {
Expand All @@ -247,14 +263,47 @@ func NewFromRawBytes(r io.Reader, b64 bool) (*Packet, error) {
return nil, err
}

keyintanddata := []byte{byte(keyint)}
keyintanddata = append(keyintanddata, keydata...)

newUnknown := &Unknown{
Key: keyintanddata,
Value: value,
switch GlobalType(keyint) {
case SilentPaymentShareType:
share, err := ReadSilentPaymentShare(keydata, value)
if err != nil {
return nil, err
}

// Duplicate keys are not allowed.
for _, x := range spsSlice {
if x.EqualKey(share) {
return nil, ErrDuplicateKey
}
}

spsSlice = append(spsSlice, *share)

case SilentPaymentDLEQType:
proof, err := ReadSilentPaymentDLEQ(keydata, value)
if err != nil {
return nil, err
}

// Duplicate keys are not allowed.
for _, x := range spdSlice {
if x.EqualKey(proof) {
return nil, ErrDuplicateKey
}
}

spdSlice = append(spdSlice, *proof)

default:
keyintanddata := []byte{byte(keyint)}
keyintanddata = append(keyintanddata, keydata...)

newUnknown := &Unknown{
Key: keyintanddata,
Value: value,
}
unknownSlice = append(unknownSlice, newUnknown)
}
unknownSlice = append(unknownSlice, newUnknown)
}

// Next we parse the INPUT section.
Expand Down Expand Up @@ -283,10 +332,12 @@ func NewFromRawBytes(r io.Reader, b64 bool) (*Packet, error) {

// Populate the new Packet object.
newPsbt := Packet{
UnsignedTx: msgTx,
Inputs: inSlice,
Outputs: outSlice,
Unknowns: unknownSlice,
UnsignedTx: msgTx,
Inputs: inSlice,
Outputs: outSlice,
SilentPaymentShares: spsSlice,
SilentPaymentDLEQs: spdSlice,
Unknowns: unknownSlice,
}

// Extended sanity checking is applied here to make sure the
Expand Down Expand Up @@ -325,6 +376,28 @@ func (p *Packet) Serialize(w io.Writer) error {
return err
}

// Serialize the global silent payment shares.
for _, share := range p.SilentPaymentShares {
keyBytes, valueBytes := SerializeSilentPaymentShare(&share)
err := serializeKVPairWithType(
w, uint8(SilentPaymentShareType), keyBytes, valueBytes,
)
if err != nil {
return err
}
}

// Serialize the global silent payment DLEQ proofs.
for _, dleq := range p.SilentPaymentDLEQs {
keyBytes, valueBytes := SerializeSilentPaymentDLEQ(&dleq)
err := serializeKVPairWithType(
w, uint8(SilentPaymentDLEQType), keyBytes, valueBytes,
)
if err != nil {
return err
}
}

// Unknown is a special case; we don't have a key type, only a key and
// a value field
for _, kv := range p.Unknowns {
Expand Down
Loading
Loading