From 5d19f6d904d17bdcf904d2665f8367def2ebab77 Mon Sep 17 00:00:00 2001 From: Marco Bardelli Date: Fri, 21 Oct 2022 00:36:16 +0200 Subject: [PATCH] Add support for partially signed tx, closes #38 * 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. --- tx_builder.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tx_builder.go b/tx_builder.go index 556db99..7e0792f 100644 --- a/tx_builder.go +++ b/tx_builder.go @@ -15,6 +15,8 @@ type TxBuilder struct { pkeys []crypto.PrvKey changeReceiver *Address + + additionalWitnesses uint } // NewTxBuilder returns a new instance of TxBuilder. @@ -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 @@ -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 }