Skip to content

Commit

Permalink
Add support for psetv2 (#189)
Browse files Browse the repository at this point in the history
* added factory method FromPublicKeys for creating multiscript payment

* define psetv2 format

* pset desr - untested version

* fixes added - not verified

* validation

* first version passing test case

* psetv2 serialization

* refactor

* creator and constructor role

* blinder role

* updater role

* signer

* fix

* add nigiri gh action

* separate signer role from updater

* finalizer role added

* extractor role added

* fix timelock validation

* signer validation added

* CreateAssetBlindProof

* decouple psetv2 from confidential

* decouple blindProofsValid from confidential pkg

* verify blinding, pset to unsigned tx, bugs

* bug fixing

* fix bugs

* fix bug

* fix ComputeAndAddToScalarOffset bug

* Rename methods of elementsutils & bufferutil

* Remove useless fils

* Refactor psetv2

* Add checks && Polish errors

* Drop travis

* Split Blind into BlindLast|BlindNonLast

* Fix serialization

* Fixes after review

* Fix method name

* Fix global (de)serialization

* Add tests for creator role

* Fix swap test

* Add NewBlinderHandlerFromOwnedInputs factory

* Fix usage of ecdh nonce for last value rangeproof

* Return unconf output if it's not confidential instead of nil

* Remove too much strict validation check

* Allow to add dummy outputs to partial tx

* Fix blinding dummy outputs + dedicated test

* Update CI badge in readme

* Fix unsigned tx version

* Fix issuance blindingi nonce deserialization

* Fix signing psetv2 test txs

* Fix signer

* Split BlinderHandler into validator and generator & validate all blinding proofs

* Fixes

* Do not use bitset fields for now

* Fixes

* Revert sighash change

* Fixes

* Fixes

* Set TxModifiable flag by creator

* Add and make mandatory usage of updater.AddInUtxoRangeProof

* Fix out.NeedsBlinding

Co-authored-by: sekulicd <sekula87@gmail.com>
  • Loading branch information
altafan and sekulicd authored Sep 16, 2022
1 parent bd72b8d commit 1fe2fd4
Show file tree
Hide file tree
Showing 48 changed files with 8,558 additions and 386 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

# go-elements

[![Build Status](https://travis-ci.com/vulpemventures/go-elements.svg?branch=master)](https://travis-ci.com/vulpemventures/go-elements)
[![Go](https://github.com/vulpemventures/go-elements/actions/workflows/ci.yml/badge.svg)](https://github.com/vulpemventures/go-elements/actions/workflows/ci.yml)
[![PkgGoDev](https://pkg.go.dev/badge/github.com/vulpemventures/go-elements)](https://pkg.go.dev/github.com/vulpemventures/go-elements)
[![Release](https://img.shields.io/github/release/vulpemventures/go-elements.svg?style=flat-square)](https://github.com/vulpemventures/go-elements/releases/latest)
[![Go Report Card](https://goreportcard.com/badge/github.com/vulpemventures/go-elements)](https://goreportcard.com/report/github.com/vulpemventures/go-elements)
Expand Down
2 changes: 1 addition & 1 deletion block/deserialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func TestBlockDeserializationIntegration(t *testing.T) {
if len(v.Outputs) == 3 {
for _, o := range v.Outputs {
if len(o.Asset) == 9 && len(o.Script) > 0 {
value, err := elementsutil.ElementsToSatoshiValue(
value, err := elementsutil.ValueFromBytes(
block.TransactionsData.Transactions[1].Outputs[1].Value,
)
if err != nil {
Expand Down
119 changes: 45 additions & 74 deletions block/serialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,20 @@ import (
)

func (b *Block) SerializeBlock() ([]byte, error) {
s, err := bufferutil.NewSerializer(nil)
if err != nil {
return nil, err
}
s := bufferutil.NewSerializer(nil)

err = b.Header.serializeHeader(s, false)
if err != nil {
if err := b.Header.serializeHeader(s, false); err != nil {
return nil, err
}

err = b.TransactionsData.SerializeTransactions(s)
if err != nil {
if err := b.TransactionsData.Serialize(s); err != nil {
return nil, err
}

return s.Bytes(), nil
}

func (t *Transactions) SerializeTransactions(
s *bufferutil.Serializer,
) error {
func (t *Transactions) Serialize(s *bufferutil.Serializer) error {
err := s.WriteVarInt(uint64(len(t.Transactions)))
if err != nil {
return err
Expand All @@ -46,9 +39,42 @@ func (t *Transactions) SerializeTransactions(
return nil
}

// SerializeForHash returns the block bytes for block hash
// it does not include some data of the block (like witness or solution in case of signed blocks)
func (h *Header) SerializeForHash() ([]byte, error) {
s := bufferutil.NewSerializer(nil)

if err := h.serializeHeader(s, true); err != nil {
return nil, err
}

return s.Bytes(), nil
}

// Serialize returns the block bytes
// includes all the data of the block
func (h *Header) Serialize() ([]byte, error) {
s := bufferutil.NewSerializer(nil)

if err := h.serializeHeader(s, false); err != nil {
return nil, err
}

return s.Bytes(), nil
}

// Hash gets the bytes with SerializeForHash and DoubleHash the bytes
func (h *Header) Hash() (chainhash.Hash, error) {
bytes, err := h.SerializeForHash()
if err != nil {
return chainhash.Hash{}, err
}

return chainhash.DoubleHashH(bytes), nil
}

func (h *Header) serializeHeader(
s *bufferutil.Serializer,
forHash bool,
s *bufferutil.Serializer, forHash bool,
) error {
version := h.Version
if h.ExtData.IsDyna {
Expand Down Expand Up @@ -83,52 +109,7 @@ func (h *Header) serializeHeader(
return nil
}

// SerializeForHash returns the block bytes for block hash
// it does not include some data of the block (like witness or solution in case of signed blocks)
func (h *Header) SerializeForHash() ([]byte, error) {
s, err := bufferutil.NewSerializer(nil)
if err != nil {
return nil, err
}

err = h.serializeHeader(s, true)
if err != nil {
return nil, err
}

return s.Bytes(), nil
}

// Serialize returns the block bytes
// includes all the data of the block
func (h *Header) Serialize() ([]byte, error) {
s, err := bufferutil.NewSerializer(nil)
if err != nil {
return nil, err
}

err = h.serializeHeader(s, false)
if err != nil {
return nil, err
}

return s.Bytes(), nil
}

// Hash gets the bytes with SerializeForHash and DoubleHash the bytes
func (h *Header) Hash() (chainhash.Hash, error) {
bytes, err := h.SerializeForHash()
if err != nil {
return chainhash.Hash{}, err
}

return chainhash.DoubleHashH(bytes), nil
}

func (e *ExtData) serialize(
s *bufferutil.Serializer,
forHash bool,
) error {
func (e *ExtData) serialize(s *bufferutil.Serializer, forHash bool) error {
if e.IsDyna {
err := e.DynamicFederation.serialize(s, forHash)
if err != nil {
Expand All @@ -144,10 +125,7 @@ func (e *ExtData) serialize(
return nil
}

func (p *Proof) serialize(
s *bufferutil.Serializer,
forHash bool,
) error {
func (p *Proof) serialize(s *bufferutil.Serializer, forHash bool) error {
if err := s.WriteVarInt(uint64(len(p.Challenge))); err != nil {
return err
}
Expand All @@ -170,8 +148,7 @@ func (p *Proof) serialize(
}

func (d *DynamicFederation) serialize(
s *bufferutil.Serializer,
forHash bool,
s *bufferutil.Serializer, forHash bool,
) error {
if d.Current == nil {
if err := s.WriteUint8(null); err != nil {
Expand Down Expand Up @@ -212,9 +189,7 @@ func (d *DynamicFederation) serialize(
return nil
}

func (d *DynamicFederationParams) serialize(
s *bufferutil.Serializer,
) error {
func (d *DynamicFederationParams) serialize(s *bufferutil.Serializer) error {
if d.CompactParams == nil && d.FullParams == nil {
if err := s.WriteUint8(null); err != nil {
return err
Expand Down Expand Up @@ -244,9 +219,7 @@ func (d *DynamicFederationParams) serialize(
return nil
}

func (c *CompactParams) serialize(
s *bufferutil.Serializer,
) error {
func (c *CompactParams) serialize(s *bufferutil.Serializer) error {
if err := s.WriteVarInt(uint64(len(c.SignBlockScript))); err != nil {
return err
}
Expand All @@ -266,9 +239,7 @@ func (c *CompactParams) serialize(
return nil
}

func (f *FullParams) serialize(
s *bufferutil.Serializer,
) error {
func (f *FullParams) serialize(s *bufferutil.Serializer) error {
if err := s.WriteVarInt(uint64(len(f.SignBlockScript))); err != nil {
return err
}
Expand Down
Loading

0 comments on commit 1fe2fd4

Please sign in to comment.