From e72e019a6348d95710458f7effe7e87392efe367 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Thu, 5 May 2022 10:35:01 +0000 Subject: [PATCH] Bump github.com/libsv/go-bt/v2 from 2.1.0-beta.2 to 2.1.0-beta.3 Bumps [github.com/libsv/go-bt/v2](https://github.com/libsv/go-bt) from 2.1.0-beta.2 to 2.1.0-beta.3. - [Release notes](https://github.com/libsv/go-bt/releases) - [Changelog](https://github.com/libsv/go-bt/blob/master/.goreleaser.yml) - [Commits](https://github.com/libsv/go-bt/compare/v2.1.0-beta.2...v2.1.0-beta.3) --- updated-dependencies: - dependency-name: github.com/libsv/go-bt/v2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] --- go.mod | 2 +- go.sum | 3 +- .../libsv/go-bt/v2/bscript/script.go | 14 ++-- vendor/github.com/libsv/go-bt/v2/errors.go | 1 + .../libsv/go-bt/v2/localunlocker.go | 60 ---------------- vendor/github.com/libsv/go-bt/v2/txchange.go | 71 +++++++------------ vendor/github.com/libsv/go-bt/v2/txinput.go | 58 +++++++++++++++ vendor/github.com/libsv/go-bt/v2/txunlock.go | 65 ----------------- vendor/github.com/libsv/go-bt/v2/unlocker.go | 10 ++- vendor/github.com/libsv/go-bt/v2/utxojson.go | 8 +-- vendor/modules.txt | 2 +- 11 files changed, 111 insertions(+), 183 deletions(-) delete mode 100644 vendor/github.com/libsv/go-bt/v2/localunlocker.go delete mode 100644 vendor/github.com/libsv/go-bt/v2/txunlock.go diff --git a/go.mod b/go.mod index 8a7252c..37ffb5f 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/labstack/echo/v4 v4.7.2 github.com/libsv/go-bc v0.1.10 github.com/libsv/go-bk v0.1.6 - github.com/libsv/go-bt/v2 v2.1.0-beta.2 + github.com/libsv/go-bt/v2 v2.1.0-beta.3 github.com/libsv/go-dpp v0.1.11 github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.12.1 diff --git a/go.sum b/go.sum index 1c9ff2f..59412a8 100644 --- a/go.sum +++ b/go.sum @@ -304,8 +304,9 @@ github.com/libsv/go-bc v0.1.10/go.mod h1:55OsjWtvaIEXy4w02icUi2lIdThuwqkAiSeF4GP github.com/libsv/go-bk v0.1.5/go.mod h1:xbDkeFFpP0uyFaPLnP6TwaLpAsHaslZ0LftTdWlB6HI= github.com/libsv/go-bk v0.1.6 h1:c9CiT5+64HRDbzxPl1v/oiFmbvWZTuUYqywCf+MBs/c= github.com/libsv/go-bk v0.1.6/go.mod h1:khJboDoH18FPUaZlzRFKzlVN84d4YfdmlDtdX4LAjQA= -github.com/libsv/go-bt/v2 v2.1.0-beta.2 h1:oq6BQQtNeZiG/esfoY/7RyYF+dDj996xqNfvoQfH6n4= github.com/libsv/go-bt/v2 v2.1.0-beta.2/go.mod h1:u5g3GmVLffBV8sWvMqHR3JekC51OR9XYvmQp1h/XoiQ= +github.com/libsv/go-bt/v2 v2.1.0-beta.3 h1:G2aVEd8C+OvOIy7TRzp2KlwF4SZfSEu30g/DEUIpTmY= +github.com/libsv/go-bt/v2 v2.1.0-beta.3/go.mod h1:KbBf6ugGNMtVwtCSUtlSAHoVhbAie4hu2VM97d1ZL8I= github.com/libsv/go-dpp v0.1.11 h1:K3fpykSin7L5s8SghkLSivACuimagdbCdHXHb5ZrrRQ= github.com/libsv/go-dpp v0.1.11/go.mod h1:nc9Lh987LXHc8+pE3k3ThMWvQQft9JSboFQF1elfRFs= github.com/magiconair/properties v1.8.6 h1:5ibWZ6iY0NctNGWo87LalDlEZ6R41TqbbDamhfG/Qzo= diff --git a/vendor/github.com/libsv/go-bt/v2/bscript/script.go b/vendor/github.com/libsv/go-bt/v2/bscript/script.go index b7d3299..42bf762 100644 --- a/vendor/github.com/libsv/go-bt/v2/bscript/script.go +++ b/vendor/github.com/libsv/go-bt/v2/bscript/script.go @@ -227,23 +227,27 @@ func (s *Script) String() string { // ToASM returns the string ASM opcodes of the script. func (s *Script) ToASM() (string, error) { + if s == nil || len(*s) == 0 { + return "", nil + } parts, err := DecodeParts(*s) // if err != nil, we will append [error] to the ASM script below (as done in the node). - var asmScript string + var asm strings.Builder for _, p := range parts { + asm.WriteRune(' ') if len(p) == 1 { - asmScript = asmScript + " " + opCodeValues[p[0]] + asm.WriteString(opCodeValues[p[0]]) } else { - asmScript = asmScript + " " + hex.EncodeToString(p) + asm.WriteString(hex.EncodeToString(p)) } } if err != nil { - asmScript += " [error]" + asm.WriteString(" [error]") } - return strings.TrimSpace(asmScript), nil + return asm.String()[1:], nil } // IsP2PKH returns true if this is a pay to pubkey hash output script. diff --git a/vendor/github.com/libsv/go-bt/v2/errors.go b/vendor/github.com/libsv/go-bt/v2/errors.go index 5319572..86f5100 100644 --- a/vendor/github.com/libsv/go-bt/v2/errors.go +++ b/vendor/github.com/libsv/go-bt/v2/errors.go @@ -11,6 +11,7 @@ var ( ErrEmptyValues = errors.New("empty value or values passed, all arguments are required and cannot be empty") ErrUnsupportedScript = errors.New("non-P2PKH input used in the tx - unsupported") ErrInvalidScriptType = errors.New("invalid script type") + ErrNoUnlocker = errors.New("unlocker not supplied") ) // Sentinal errors reported by inputs. diff --git a/vendor/github.com/libsv/go-bt/v2/localunlocker.go b/vendor/github.com/libsv/go-bt/v2/localunlocker.go deleted file mode 100644 index b10b90f..0000000 --- a/vendor/github.com/libsv/go-bt/v2/localunlocker.go +++ /dev/null @@ -1,60 +0,0 @@ -package bt - -import ( - "context" - "errors" - - "github.com/libsv/go-bk/bec" - "github.com/libsv/go-bt/v2/bscript" - "github.com/libsv/go-bt/v2/sighash" -) - -// LocalUnlockerGetter implements the UnlockerGetter interface. It unlocks a Tx locally, -// using a bec PrivateKey. -type LocalUnlockerGetter struct { - PrivateKey *bec.PrivateKey -} - -// Unlocker builds a new *bt.LocalUnlocker with the same private key -// as the calling *bt.LocalUnlockerGetter. -func (lg *LocalUnlockerGetter) Unlocker(ctx context.Context, lockingScript *bscript.Script) (Unlocker, error) { - return &LocalUnlocker{PrivateKey: lg.PrivateKey}, nil -} - -// LocalUnlocker implements the unlocker interface. It is used to unlock a tx locally using a -// bec Private Key. -type LocalUnlocker struct { - PrivateKey *bec.PrivateKey -} - -// Unlock a transaction at a given input using the PrivateKey passed in through the LocalUnlocker -// struct. -// Unlock generates, applies, and returns an ECDSA signature for the provided hash digest using the private key -// as well as the public key corresponding to the private key used. The produced -// signature is deterministic (same message and same key yield the same signature) and -// canonical in accordance with RFC6979 and BIP0062. -func (lu *LocalUnlocker) Unlock(ctx context.Context, tx *Tx, idx uint32, shf sighash.Flag) error { - if shf == 0 { - shf = sighash.AllForkID - } - - sh, err := tx.CalcInputSignatureHash(idx, shf) - if err != nil { - return err - } - - sig, err := lu.PrivateKey.Sign(sh) - if err != nil { - return err - } - - pubKey := lu.PrivateKey.PubKey().SerialiseCompressed() - signature := sig.Serialise() - - switch tx.Inputs[idx].PreviousTxScript.ScriptType() { - case bscript.ScriptTypePubKeyHash: - return tx.ApplyP2PKHUnlockingScript(idx, pubKey, signature, shf) - } - - return errors.New("currently only p2pkh supported") -} diff --git a/vendor/github.com/libsv/go-bt/v2/txchange.go b/vendor/github.com/libsv/go-bt/v2/txchange.go index 1a7debf..3ee4ebe 100644 --- a/vendor/github.com/libsv/go-bt/v2/txchange.go +++ b/vendor/github.com/libsv/go-bt/v2/txchange.go @@ -6,7 +6,7 @@ import ( const ( // DustLimit is the current minimum txo output accepted by miners. - DustLimit = 136 + DustLimit = 1 ) // ChangeToAddress calculates the amount of fees needed to cover the transaction @@ -63,62 +63,43 @@ func (tx *Tx) change(f *FeeQuote, output *changeOutput) (uint64, bool, error) { } available := inputAmount - outputAmount - standardFees, err := f.Fee(FeeTypeStandard) + size, err := tx.EstimateSizeWithTypes() if err != nil { return 0, false, err } - - var txFees *TxFees - if txFees, err = tx.EstimateFeesPaid(f); err != nil { + stdFee, err := f.Fee(FeeTypeStandard) + if err != nil { return 0, false, err } - changeFee, canAdd := tx.canAddChange(txFees, standardFees) - if !canAdd { + dataFee, err := f.Fee(FeeTypeData) + if err != nil { return 0, false, err } - available -= txFees.TotalFeePaid - // if we want to add to a new output, set - // newOutput to true, this will add the calculated change - // into a new output. + varIntUpper := VarInt(tx.OutputCount()).UpperLimitInc() + if varIntUpper == -1 { + return 0, false, nil + } + changeOutputFee := varIntUpper + changeP2pkhByteLen := uint64(0) if output != nil && output.newOutput { - available -= changeFee - tx.AddOutput(&Output{Satoshis: available, LockingScript: output.lockingScript}) + changeP2pkhByteLen = uint64(8 + 1 + 25) } - return available, true, nil -} + sFees := (size.TotalStdBytes + changeP2pkhByteLen) * uint64(stdFee.MiningFee.Satoshis) / uint64(stdFee.MiningFee.Bytes) + dFees := size.TotalDataBytes * uint64(dataFee.MiningFee.Satoshis) / uint64(dataFee.MiningFee.Bytes) + txFees := sFees + dFees + uint64(changeOutputFee) -// canAddChange will return true / false if the tx can have a change output -// added. -// Reasons this could be false are: -// - hitting max output limit -// - change would be below dust limit -// - not enough funds for change -// We also return the change output fee amount, if we can add change -func (tx *Tx) canAddChange(txFees *TxFees, standardFees *Fee) (uint64, bool) { - varIntUpper := VarInt(uint64(tx.OutputCount())).UpperLimitInc() - if varIntUpper == -1 { - return 0, false // upper limit of Outputs in one tx reached - } - changeOutputFee := uint64(varIntUpper) - // 8 bytes for satoshi value +1 for varint length + 25 bytes for p2pkh script (e.g. 76a914cc...05388ac) - changeP2pkhByteLen := 8 + 1 + 25 - changeOutputFee += uint64(changeP2pkhByteLen * standardFees.MiningFee.Satoshis / standardFees.MiningFee.Bytes) - - inputAmount := tx.TotalInputSatoshis() - outputAmount := tx.TotalOutputSatoshis() - // shouldn't get this far, but if we do, there's no change to add - if inputAmount <= outputAmount { - return 0, false - } - available := inputAmount - outputAmount // not enough to add change, no change to add - if available <= changeOutputFee+txFees.TotalFeePaid { - return 0, false + if available <= txFees || available-txFees <= DustLimit { + return 0, false, nil } - // after fees the change would be lower than dust limit, don't add change - if available-changeOutputFee+txFees.TotalFeePaid <= DustLimit { - return 0, false + + // if we want to add to a new output, set + // newOutput to true, this will add the calculated change + // into a new output + available -= txFees + if output != nil && output.newOutput { + tx.AddOutput(&Output{Satoshis: available, LockingScript: output.lockingScript}) } - return changeOutputFee, true + return available, true, nil } diff --git a/vendor/github.com/libsv/go-bt/v2/txinput.go b/vendor/github.com/libsv/go-bt/v2/txinput.go index 937898d..b12caf3 100644 --- a/vendor/github.com/libsv/go-bt/v2/txinput.go +++ b/vendor/github.com/libsv/go-bt/v2/txinput.go @@ -11,6 +11,7 @@ import ( "github.com/pkg/errors" "github.com/libsv/go-bt/v2/bscript" + "github.com/libsv/go-bt/v2/sighash" ) // UTXOGetterFunc is used for tx.Fund(...). It provides the amount of satoshis required @@ -217,3 +218,60 @@ func (tx *Tx) SequenceHash() []byte { return crypto.Sha256d(buf) } + +// InsertInputUnlockingScript applies a script to the transaction at a specific index in +// unlocking script field. +func (tx *Tx) InsertInputUnlockingScript(index uint32, s *bscript.Script) error { + if tx.Inputs[index] != nil { + tx.Inputs[index].UnlockingScript = s + return nil + } + + return fmt.Errorf("no input at index %d", index) +} + +// FillInput is used to unlock the transaction at a specific input index. +// It takes an Unlocker interface as a parameter so that different +// unlocking implementations can be used to unlock the transaction - +// for example local or external unlocking (hardware wallet), or +// signature/nonsignature based. +func (tx *Tx) FillInput(ctx context.Context, unlocker Unlocker, params UnlockerParams) error { + if unlocker == nil { + return ErrNoUnlocker + } + + if params.SigHashFlags == 0 { + params.SigHashFlags = sighash.AllForkID + } + + uscript, err := unlocker.UnlockingScript(ctx, tx, params) + if err != nil { + return err + } + + return tx.InsertInputUnlockingScript(params.InputIdx, uscript) +} + +// FillAllInputs is used to sign all inputs. It takes an UnlockerGetter interface +// as a parameter so that different unlocking implementations can +// be used to sign the transaction - for example local/external +// signing, or P2PKH/contract signing. +// +// Given this signs inputs and outputs, sighash `ALL|FORKID` is used. +func (tx *Tx) FillAllInputs(ctx context.Context, ug UnlockerGetter) error { + for i, in := range tx.Inputs { + u, err := ug.Unlocker(ctx, in.PreviousTxScript) + if err != nil { + return err + } + + if err = tx.FillInput(ctx, u, UnlockerParams{ + InputIdx: uint32(i), + SigHashFlags: sighash.AllForkID, // use SIGHASHALLFORFORKID to sign automatically + }); err != nil { + return err + } + } + + return nil +} diff --git a/vendor/github.com/libsv/go-bt/v2/txunlock.go b/vendor/github.com/libsv/go-bt/v2/txunlock.go deleted file mode 100644 index 24390d6..0000000 --- a/vendor/github.com/libsv/go-bt/v2/txunlock.go +++ /dev/null @@ -1,65 +0,0 @@ -package bt - -import ( - "context" - "fmt" - - "github.com/libsv/go-bt/v2/bscript" - "github.com/libsv/go-bt/v2/sighash" -) - -// Unlock is used to unlock the transaction at a specific input index. -// It takes an Unlocker interface as a parameter so that different -// unlocking implementations can be used to unlock the transaction - -// for example local or external unlocking (hardware wallet), or -// signature/nonsignature based. -func (tx *Tx) Unlock(ctx context.Context, u Unlocker, idx uint32, shf sighash.Flag) error { - if shf == 0 { - shf = sighash.AllForkID - } - - return u.Unlock(ctx, tx, idx, shf) -} - -// UnlockAll is used to sign all inputs. It takes an UnlockerGetter interface -// as a parameter so that different unlocking implementations can -// be used to sign the transaction - for example local/external -// signing, or P2PKH/contract signing. -func (tx *Tx) UnlockAll(ctx context.Context, ug UnlockerGetter) error { - shf := sighash.AllForkID // use SIGHASHALLFORFORKID to sign automatically - - for i, in := range tx.Inputs { - u, err := ug.Unlocker(ctx, in.PreviousTxScript) - if err != nil { - return err - } - - if err = tx.Unlock(ctx, u, uint32(i), shf); err != nil { - return err - } - } - - return nil -} - -// ApplyP2PKHUnlockingScript applies a script to the transaction at a specific index in -// unlocking script field. -func (tx *Tx) ApplyP2PKHUnlockingScript(index uint32, pubKey []byte, sig []byte, shf sighash.Flag) error { - uls, err := bscript.NewP2PKHUnlockingScript(pubKey, sig, shf) - if err != nil { - return err - } - - return tx.ApplyUnlockingScript(index, uls) -} - -// ApplyUnlockingScript applies a script to the transaction at a specific index in -// unlocking script field. -func (tx *Tx) ApplyUnlockingScript(index uint32, s *bscript.Script) error { - if tx.Inputs[index] != nil { - tx.Inputs[index].UnlockingScript = s - return nil - } - - return fmt.Errorf("no input at index %d", index) -} diff --git a/vendor/github.com/libsv/go-bt/v2/unlocker.go b/vendor/github.com/libsv/go-bt/v2/unlocker.go index 86622f8..8db5984 100644 --- a/vendor/github.com/libsv/go-bt/v2/unlocker.go +++ b/vendor/github.com/libsv/go-bt/v2/unlocker.go @@ -7,10 +7,18 @@ import ( "github.com/libsv/go-bt/v2/sighash" ) +// UnlockerParams params used for unlocking an input with a `bt.Unlocker`. +type UnlockerParams struct { + // InputIdx the input to be unlocked. [DEFAULT 0] + InputIdx uint32 + // SigHashFlags the be applied [DEFAULT ALL|FORKID] + SigHashFlags sighash.Flag +} + // Unlocker interface to allow custom implementations of different unlocking mechanisms. // Implement the Unlocker function as shown in LocalUnlocker, for example. type Unlocker interface { - Unlock(ctx context.Context, tx *Tx, idx uint32, shf sighash.Flag) error + UnlockingScript(ctx context.Context, tx *Tx, up UnlockerParams) (uscript *bscript.Script, err error) } // UnlockerGetter interfaces getting an unlocker for a given output/locking script. diff --git a/vendor/github.com/libsv/go-bt/v2/utxojson.go b/vendor/github.com/libsv/go-bt/v2/utxojson.go index 3161bbd..5906255 100644 --- a/vendor/github.com/libsv/go-bt/v2/utxojson.go +++ b/vendor/github.com/libsv/go-bt/v2/utxojson.go @@ -39,13 +39,13 @@ func (u *UTXO) UnmarshalJSON(body []byte) error { return err } - ls, err := bscript.NewFromHexString(j.LockingScript) + lscript, err := bscript.NewFromHexString(j.LockingScript) if err != nil { return err } u.TxID = txID - u.LockingScript = ls + u.LockingScript = lscript u.Vout = j.Vout u.Satoshis = j.Satoshis @@ -83,14 +83,14 @@ func (n *nodeUTXOWrapper) UnmarshalJSON(b []byte) error { return err } - ls, err := bscript.NewFromHexString(uj.ScriptPubKey) + lscript, err := bscript.NewFromHexString(uj.ScriptPubKey) if err != nil { return err } n.UTXO.Satoshis = uint64(uj.Amount * 100000000) n.UTXO.Vout = uj.Vout - n.UTXO.LockingScript = ls + n.UTXO.LockingScript = lscript n.UTXO.TxID = txID return nil diff --git a/vendor/modules.txt b/vendor/modules.txt index 905c36c..6d5315e 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -84,7 +84,7 @@ github.com/libsv/go-bk/bip32 github.com/libsv/go-bk/chaincfg github.com/libsv/go-bk/crypto github.com/libsv/go-bk/envelope -# github.com/libsv/go-bt/v2 v2.1.0-beta.2 +# github.com/libsv/go-bt/v2 v2.1.0-beta.3 ## explicit; go 1.17 github.com/libsv/go-bt/v2 github.com/libsv/go-bt/v2/bscript