Skip to content

Commit

Permalink
upgrade btcd go-ethereum
Browse files Browse the repository at this point in the history
  • Loading branch information
lizhenchun committed Oct 11, 2022
1 parent f885da4 commit b0dce20
Show file tree
Hide file tree
Showing 19 changed files with 158 additions and 581 deletions.
2 changes: 1 addition & 1 deletion btc/msgtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"encoding/hex"
"github.com/btcsuite/btcd/blockchain"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
)

func DecodeMsgTx(mtx *wire.MsgTx, chainParams *chaincfg.Params) *btcjson.TxRawDecodeResult {
Expand Down
50 changes: 32 additions & 18 deletions btc/transact.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"errors"
"fmt"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcd/rpcclient"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/lizc2003/hdwallet/wallet"
)
Expand Down Expand Up @@ -86,7 +86,7 @@ func (t *BtcTransaction) SignWithSecretsSource(secretsSource txauthor.SecretsSou
if err != nil {
return err
}
err = t.validate()
err = validateMsgTx(t.Tx, t.PrevScripts, t.PrevInputValues)
if err != nil {
return err
}
Expand Down Expand Up @@ -132,22 +132,6 @@ func (t *BtcTransaction) Send(client *rpcclient.Client, allowHighFees bool) (*ch
return hash, nil
}

func (t *BtcTransaction) validate() error {
hashCache := txscript.NewTxSigHashes(t.Tx)
for i, prevScript := range t.PrevScripts {
vm, err := txscript.NewEngine(prevScript, t.Tx, i,
txscript.StandardVerifyFlags, nil, hashCache, int64(t.PrevInputValues[i]))
if err != nil {
return fmt.Errorf("cannot create script engine: %s", err)
}
err = vm.Execute()
if err != nil {
return fmt.Errorf("cannot validate transaction: %s", err)
}
}
return nil
}

func makeTxOutputs(outputs []BtcOutput, relayFeePerKb btcutil.Amount, chainCfg *chaincfg.Params) ([]*wire.TxOut, error) {
outLen := len(outputs)
if outLen == 0 {
Expand Down Expand Up @@ -212,3 +196,33 @@ func makeInputSource(unspents []BtcUnspent) txauthor.InputSource {
return currentTotal, currentInputs, currentInputValues, currentScripts, nil
}
}

// validateMsgTx verifies transaction input scripts for tx. All previous output
// scripts from outputs redeemed by the transaction, in the same order they are
// spent, must be passed in the prevScripts slice.
func validateMsgTx(tx *wire.MsgTx, prevScripts [][]byte,
inputValues []btcutil.Amount) error {

inputFetcher, err := txauthor.TXPrevOutFetcher(
tx, prevScripts, inputValues,
)
if err != nil {
return err
}

hashCache := txscript.NewTxSigHashes(tx, inputFetcher)
for i, prevScript := range prevScripts {
vm, err := txscript.NewEngine(
prevScript, tx, i, txscript.StandardVerifyFlags, nil,
hashCache, int64(inputValues[i]), inputFetcher,
)
if err != nil {
return fmt.Errorf("cannot create script engine: %s", err)
}
err = vm.Execute()
if err != nil {
return fmt.Errorf("cannot validate transaction: %s", err)
}
}
return nil
}
4 changes: 2 additions & 2 deletions btc/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package btc

import (
"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/chaincfg/chainhash"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/btcsuite/btcwallet/wallet/txrules"
"github.com/btcsuite/btcwallet/wallet/txsizes"
Expand Down Expand Up @@ -41,7 +41,7 @@ func EstimateFee(numP2PKHIns, numP2WPKHIns, numNestedP2WPKHIns int,
return 0, 0, err
}

maxSignedSize := txsizes.EstimateVirtualSize(numP2PKHIns, numP2WPKHIns,
maxSignedSize := txsizes.EstimateVirtualSize(numP2PKHIns, 0, numP2WPKHIns,
numNestedP2WPKHIns, txOuts, changeScriptSize)

targetFee := txrules.FeeForSerializeSize(feeRatePerKb, maxSignedSize)
Expand Down
38 changes: 21 additions & 17 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
module github.com/lizc2003/hdwallet

go 1.17
go 1.19

require (
github.com/btcsuite/btcd v0.22.0-beta.0.20211026140004-31791ba4dc6e
github.com/btcsuite/btcutil v1.0.3-0.20210929233259-9cdf59f60c51
github.com/btcsuite/btcwallet/wallet/txauthor v1.1.0
github.com/btcsuite/btcwallet/wallet/txrules v1.1.0
github.com/btcsuite/btcwallet/wallet/txsizes v1.1.0
github.com/ethereum/go-ethereum v1.10.13
github.com/fbsobreira/gotron-sdk v0.0.0-20211102183839-58a64f4da5f4
github.com/stretchr/testify v1.7.0
github.com/btcsuite/btcd v0.23.2
github.com/btcsuite/btcd/btcec/v2 v2.2.1
github.com/btcsuite/btcd/btcutil v1.1.2
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1
github.com/btcsuite/btcwallet/wallet/txauthor v1.3.2
github.com/btcsuite/btcwallet/wallet/txrules v1.2.0
github.com/btcsuite/btcwallet/wallet/txsizes v1.2.3
github.com/ethereum/go-ethereum v1.10.25
github.com/lizc2003/gotron-sdk v0.0.0-20221010131620-2fa8f18bda85
github.com/stretchr/testify v1.7.2
github.com/tyler-smith/go-bip39 v1.1.0
google.golang.org/grpc v1.37.0
google.golang.org/protobuf v1.25.0
google.golang.org/protobuf v1.26.0
)

require (
Expand All @@ -23,11 +25,13 @@ require (
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd // indirect
github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deckarep/golang-set v1.7.1 // indirect
github.com/deckarep/golang-set v1.8.0 // indirect
github.com/decred/dcrd/crypto/blake256 v1.0.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/go-ole/go-ole v1.2.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/uuid v1.1.5 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/websocket v1.4.2 // indirect
github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand All @@ -43,10 +47,10 @@ require (
go.uber.org/multierr v1.5.0 // indirect
go.uber.org/zap v1.15.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/net v0.0.0-20210805182204-aaa1db679c0d // indirect
golang.org/x/sys v0.0.0-20211102061401-a2f17f7b995c // indirect
golang.org/x/text v0.3.6 // indirect
golang.org/x/net v0.0.0-20220607020251-c690dde0001d // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20200825200019-8632dd797987 // indirect
gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit b0dce20

Please sign in to comment.