Skip to content

Commit

Permalink
Add support for partially signed tx, closes echovl#38
Browse files Browse the repository at this point in the history
* allow to set additional witnesses for partially signed tx preparation
* change calculateMinFee to consider additional witnesses that will
  increase the tx length. It is still not clear how to precisely
  calculate the bytes for witnesses, in this implementation
  for each additional witnesses there will be an additional 100 bytes,
  (32 public key, 64 signature, 4 index/key in cbor). Those are not
  actually enough so we are considering 1 additional byte
  for each additional witness after the first.
  • Loading branch information
safanaj committed Nov 15, 2022
1 parent 5d486b0 commit 5d19f6d
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions tx_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ type TxBuilder struct {
pkeys []crypto.PrvKey

changeReceiver *Address

additionalWitnesses uint
}

// NewTxBuilder returns a new instance of TxBuilder.
Expand Down Expand Up @@ -48,6 +50,12 @@ func (tb *TxBuilder) SetFee(fee Coin) {
tb.tx.Body.Fee = fee
}

// SetAdditionalWitnesses sets future witnesses for a partially signed transction.
// This is useful to compute the real length and so fee in advance
func (tb *TxBuilder) SetAdditionalWitnesses(witnesses uint) {
tb.additionalWitnesses = witnesses
}

// AddAuxiliaryData adds auxiliary data to the transaction.
func (tb *TxBuilder) AddAuxiliaryData(data *AuxiliaryData) {
tb.tx.AuxiliaryData = data
Expand Down Expand Up @@ -143,6 +151,14 @@ func (tb *TxBuilder) MinCoinsForTxOut(txOut *TxOutput) Coin {
func (tb *TxBuilder) calculateMinFee() Coin {
txBytes := tb.tx.Bytes()
txLength := uint64(len(txBytes))
// for each additional witnesses there will be an additional 100 bytes,
// (32 public key, 64 signature, 4 index/key in cbor)
txLength += uint64(tb.additionalWitnesses * 100)
// apparently that is not enough, so just consider 1 additional byte
// for each additional witness after the first
if tb.additionalWitnesses > 1 {
txLength += uint64(tb.additionalWitnesses - 1)
}
return tb.protocol.MinFeeA*Coin(txLength) + tb.protocol.MinFeeB
}

Expand Down

0 comments on commit 5d19f6d

Please sign in to comment.