From dd5377c742ffb39f6c5394c4dec50afa74dd5e32 Mon Sep 17 00:00:00 2001 From: Oliver Gugger Date: Thu, 10 Mar 2022 10:15:08 +0100 Subject: [PATCH] multi: use new btcutil/address module --- blockchain/fullblocktests/generate.go | 5 +- blockchain/indexers/addrindex.go | 21 ++-- config.go | 9 +- go.mod | 29 ++++- go.sum | 21 ++-- integration/rpctest/blockgen.go | 7 +- integration/rpctest/memwallet.go | 19 +-- integration/rpctest/rpc_harness.go | 7 +- mining/cpuminer/cpuminer.go | 5 +- mining/mining.go | 11 +- rpcclient/extensions.go | 10 +- rpcclient/mining.go | 11 +- rpcclient/notify.go | 23 ++-- rpcclient/rawtransactions.go | 25 ++-- rpcclient/wallet.go | 170 +++++++++++++++++--------- rpcserver.go | 39 +++--- rpcwebsocket.go | 33 ++--- 17 files changed, 283 insertions(+), 162 deletions(-) diff --git a/blockchain/fullblocktests/generate.go b/blockchain/fullblocktests/generate.go index 964986dbcf1..a28448888be 100644 --- a/blockchain/fullblocktests/generate.go +++ b/blockchain/fullblocktests/generate.go @@ -20,11 +20,12 @@ import ( "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/btcec/v2" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) const ( @@ -216,7 +217,7 @@ func makeTestGenerator(params *chaincfg.Params) (testGenerator, error) { // payToScriptHashScript returns a standard pay-to-script-hash for the provided // redeem script. func payToScriptHashScript(redeemScript []byte) []byte { - redeemScriptHash := btcutil.Hash160(redeemScript) + redeemScriptHash := address.Hash160(redeemScript) script, err := txscript.NewScriptBuilder(). AddOp(txscript.OP_HASH160).AddData(redeemScriptHash). AddOp(txscript.OP_EQUAL).Script() diff --git a/blockchain/indexers/addrindex.go b/blockchain/indexers/addrindex.go index 2f0e2eeba1c..04e944c2b90 100644 --- a/blockchain/indexers/addrindex.go +++ b/blockchain/indexers/addrindex.go @@ -10,12 +10,13 @@ import ( "sync" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/database" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) const ( @@ -536,27 +537,27 @@ func dbRemoveAddrIndexEntries(bucket internalBucket, addrKey [addrKeySize]byte, // addrToKey converts known address types to an addrindex key. An error is // returned for unsupported types. -func addrToKey(addr btcutil.Address) ([addrKeySize]byte, error) { +func addrToKey(addr address.Address) ([addrKeySize]byte, error) { switch addr := addr.(type) { - case *btcutil.AddressPubKeyHash: + case *address.AddressPubKeyHash: var result [addrKeySize]byte result[0] = addrKeyTypePubKeyHash copy(result[1:], addr.Hash160()[:]) return result, nil - case *btcutil.AddressScriptHash: + case *address.AddressScriptHash: var result [addrKeySize]byte result[0] = addrKeyTypeScriptHash copy(result[1:], addr.Hash160()[:]) return result, nil - case *btcutil.AddressPubKey: + case *address.AddressPubKey: var result [addrKeySize]byte result[0] = addrKeyTypePubKeyHash copy(result[1:], addr.AddressPubKeyHash().Hash160()[:]) return result, nil - case *btcutil.AddressWitnessScriptHash: + case *address.AddressWitnessScriptHash: var result [addrKeySize]byte result[0] = addrKeyTypeWitnessScriptHash @@ -565,10 +566,10 @@ func addrToKey(addr btcutil.Address) ([addrKeySize]byte, error) { // all address entries within the database uniform and compact, // we use a hash160 here to reduce the size of the salient data // push to 20-bytes. - copy(result[1:], btcutil.Hash160(addr.ScriptAddress())) + copy(result[1:], address.Hash160(addr.ScriptAddress())) return result, nil - case *btcutil.AddressWitnessPubKeyHash: + case *address.AddressWitnessPubKeyHash: var result [addrKeySize]byte result[0] = addrKeyTypeWitnessPubKeyHash copy(result[1:], addr.Hash160()[:]) @@ -805,7 +806,7 @@ func (idx *AddrIndex) DisconnectBlock(dbTx database.Tx, block *btcutil.Block, // that involve a given address. // // This function is safe for concurrent access. -func (idx *AddrIndex) TxRegionsForAddress(dbTx database.Tx, addr btcutil.Address, +func (idx *AddrIndex) TxRegionsForAddress(dbTx database.Tx, addr address.Address, numToSkip, numRequested uint32, reverse bool) ([]database.BlockRegion, uint32, error) { addrKey, err := addrToKey(addr) @@ -931,7 +932,7 @@ func (idx *AddrIndex) RemoveUnconfirmedTx(hash *chainhash.Hash) { // Unsupported address types are ignored and will result in no results. // // This function is safe for concurrent access. -func (idx *AddrIndex) UnconfirmedTxnsForAddress(addr btcutil.Address) []*btcutil.Tx { +func (idx *AddrIndex) UnconfirmedTxnsForAddress(addr address.Address) []*btcutil.Tx { // Ignore unsupported address types. addrKey, err := addrToKey(addr) if err != nil { diff --git a/config.go b/config.go index 5eca3f2174f..9e7ec04bd26 100644 --- a/config.go +++ b/config.go @@ -22,6 +22,8 @@ import ( "time" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/connmgr" @@ -30,7 +32,6 @@ import ( "github.com/btcsuite/btcd/mempool" "github.com/btcsuite/btcd/peer" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/go-socks/socks" flags "github.com/jessevdk/go-flags" ) @@ -177,7 +178,7 @@ type config struct { oniondial func(string, string, time.Duration) (net.Conn, error) dial func(string, string, time.Duration) (net.Conn, error) addCheckpoints []chaincfg.Checkpoint - miningAddrs []btcutil.Address + miningAddrs []address.Address minRelayTxFee btcutil.Amount whitelists []*net.IPNet } @@ -925,9 +926,9 @@ func loadConfig() (*config, []string, error) { } // Check mining addresses are valid and saved parsed versions. - cfg.miningAddrs = make([]btcutil.Address, 0, len(cfg.MiningAddrs)) + cfg.miningAddrs = make([]address.Address, 0, len(cfg.MiningAddrs)) for _, strAddr := range cfg.MiningAddrs { - addr, err := btcutil.DecodeAddress(strAddr, activeNetParams.Params) + addr, err := address.DecodeAddress(strAddr, activeNetParams.Params) if err != nil { str := "%s: mining address '%s' failed to decode: %v" err := fmt.Errorf(str, funcName, strAddr, err) diff --git a/go.mod b/go.mod index a006d957dce..c0c93383976 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,12 @@ module github.com/btcsuite/btcd require ( github.com/btcsuite/btcd/btcec/v2 v2.1.1 - github.com/btcsuite/btcd/btcutil v1.1.0 + github.com/btcsuite/btcd/btcutil v1.1.1 + github.com/btcsuite/btcd/btcutil/address v1.0.0 + github.com/btcsuite/btcd/chaincfg v1.0.0 github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0 + github.com/btcsuite/btcd/txscript v1.0.0 + github.com/btcsuite/btcd/wire v1.0.0 github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 @@ -13,18 +17,39 @@ require ( github.com/jessevdk/go-flags v1.4.0 github.com/jrick/logrotate v1.0.0 github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 - golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 + golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 ) require ( github.com/aead/siphash v1.0.1 // indirect + github.com/btcsuite/btcd/btcutil/base58 v1.0.0 // indirect + github.com/btcsuite/btcd/btcutil/bech32 v1.0.0 // indirect github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect github.com/golang/snappy v0.0.4 // indirect github.com/kkdai/bstream v0.0.0-20161212061736-f391b8402d23 // indirect ) +// TODO(guggero): Remove this as soon as we have a tagged version of btcutil. replace github.com/btcsuite/btcd/btcutil => ./btcutil +// TODO(guggero): Remove this as soon as we have a tagged version of chaincfg. +replace github.com/btcsuite/btcd/chaincfg => ./chaincfg + +// TODO(guggero): Remove this as soon as we have a tagged version of address. +replace github.com/btcsuite/btcd/btcutil/address => ./btcutil/address + +// TODO(guggero): Remove this as soon as we have a tagged version of base58. +replace github.com/btcsuite/btcd/btcutil/base58 => ./btcutil/base58 + +// TODO(guggero): Remove this as soon as we have a tagged version of bech32. +replace github.com/btcsuite/btcd/btcutil/bech32 => ./btcutil/bech32 + +// TODO(guggero): Remove this as soon as we have a tagged version of txscript. +replace github.com/btcsuite/btcd/txscript => ./txscript + +// TODO(guggero): Remove this as soon as we have a tagged version of wire. +replace github.com/btcsuite/btcd/wire => ./wire + // The retract statements below fixes an accidental push of the tags of a btcd // fork. retract ( diff --git a/go.sum b/go.sum index 51aa0520607..807f68e2e16 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,5 @@ github.com/aead/siphash v1.0.1 h1:FwHfE/T45KPKYuuSAKyyvE+oPWcaQ+CUmFW0bPlM+kg= github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII= -github.com/btcsuite/btcd v0.22.0-beta.0.20220111032746-97732e52810c/go.mod h1:tjmYdS6MLJ5/s0Fj4DbLgSbDHbEqLJrtnHecBFkdz5M= github.com/btcsuite/btcd/btcec/v2 v2.1.1 h1:xxivBG6pU3wwxx9qPNZP+2K0PXO9VmFLaSrwOFr24Hw= github.com/btcsuite/btcd/btcec/v2 v2.1.1/go.mod h1:ctjw4H1kknNJmRN4iP1R7bTQ+v3GJkZBd6mui8ZsAZE= github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0 h1:MSskdM4/xJYcFzy0altH/C/xHopifpWzHUi1JeVI34Q= @@ -9,8 +8,6 @@ github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f h1:bAs4lUbRJpnnkd9 github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd h1:R/opQEbFEy9JGkIguV40SvRY1uliPX8ifOvi6ICsFCw= github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg= -github.com/btcsuite/goleveldb v1.0.0/go.mod h1:QiK9vBlgftBg6rWQIj6wFzbPfRjiykIEhBH4obrXJ/I= -github.com/btcsuite/snappy-go v1.0.0/go.mod h1:8woku9dyThutzjeg+3xrA5iCpBRH8XEEg3lh6TiUghc= github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792 h1:R8vQdOQdZ9Y3SkEwmHoWBmX1DNXhXZqlTpq6s4tyJGc= github.com/btcsuite/websocket v0.0.0-20150119174127-31079b680792/go.mod h1:ghJtEyQwv5/p4Mg4C0fgbePVuGr935/5ddU9Z3TmDRY= github.com/btcsuite/winsvc v1.0.0 h1:J9B4L7e3oqhXOcm+2IuNApwzQec85lE+QaikUcCs+dk= @@ -51,21 +48,21 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/ginkgo v1.14.0 h1:2mOpI4JVVPBN+WQRa0WKH2eXR+Ey+uK4n7Zj0aYpIQA= github.com/onsi/ginkgo v1.14.0/go.mod h1:iSB4RoI2tjJc9BBv4NKIKWKya62Rps+oPG/Lv9klQyY= -github.com/onsi/gomega v1.4.1/go.mod h1:C1qb7wdrVGGVU+Z6iS04AVkA3Q65CEZX59MT0QO5uiA= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= github.com/onsi/gomega v1.10.1 h1:o0+MgICZLuZ7xjH7Vx6zS/zcu93/BEp1VwkIW1mEXCE= github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 h1:psW17arqaxU48Z5kZ0CQnkZWQJsqcURM6tKiBApRjXI= golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= -golang.org/x/net v0.0.0-20180719180050-a680a1efc54d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2 h1:XdAboW3BNMv9ocSCOk/u1MFioZGzCNkiJZ19v9Oe3Ig= +golang.org/x/crypto v0.0.0-20220210151621-f4118a5b28e2/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= -golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc h1:zK/HqS5bZxDptfPJNq8v7vJfXtkU7r9TLIoSr1bXaP4= golang.org/x/net v0.0.0-20200813134508-3edf25e44fcc/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 h1:CIJ76btIcR3eFI5EgSo6k1qKw9KJexJuRLI9G7Hp5wE= +golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= @@ -75,12 +72,17 @@ golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191120155948-bd437916bb0e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed h1:J22ig1FUekjjkmZUM7pTKixYm8DvrYsvrBZdunYeIuQ= golang.org/x/sys v0.0.0-20200814200057-3d37ad5750ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= +golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= -golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= +golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M= +golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE= @@ -95,7 +97,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8 gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= -gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/integration/rpctest/blockgen.go b/integration/rpctest/blockgen.go index a35c66e6ac5..3f13b81cd0b 100644 --- a/integration/rpctest/blockgen.go +++ b/integration/rpctest/blockgen.go @@ -12,12 +12,13 @@ import ( "time" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/mining" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) // solveBlock attempts to find a nonce which makes the passed block header hash @@ -97,7 +98,7 @@ func standardCoinbaseScript(nextBlockHeight int32, extraNonce uint64) ([]byte, e // createCoinbaseTx returns a coinbase transaction paying an appropriate // subsidy based on the passed block height to the provided address. func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int32, - addr btcutil.Address, mineTo []wire.TxOut, + addr address.Address, mineTo []wire.TxOut, net *chaincfg.Params) (*btcutil.Tx, error) { // Create the script to pay to the provided payment address. @@ -134,7 +135,7 @@ func createCoinbaseTx(coinbaseScript []byte, nextBlockHeight int32, // second is used. Passing nil for the previous block results in a block that // builds off of the genesis block for the specified chain. func CreateBlock(prevBlock *btcutil.Block, inclusionTxs []*btcutil.Tx, - blockVersion int32, blockTime time.Time, miningAddr btcutil.Address, + blockVersion int32, blockTime time.Time, miningAddr address.Address, mineTo []wire.TxOut, net *chaincfg.Params) (*btcutil.Block, error) { var ( diff --git a/integration/rpctest/memwallet.go b/integration/rpctest/memwallet.go index d084e99feb7..7c6ea132e49 100644 --- a/integration/rpctest/memwallet.go +++ b/integration/rpctest/memwallet.go @@ -13,6 +13,7 @@ import ( "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/btcec/v2" "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/btcutil/hdkeychain" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" @@ -72,7 +73,7 @@ type undoEntry struct { // hierarchy which promotes reproducibility between harness test runs. type memWallet struct { coinbaseKey *btcec.PrivateKey - coinbaseAddr btcutil.Address + coinbaseAddr address.Address // hdRoot is the root master private key for the wallet. hdRoot *hdkeychain.ExtendedKey @@ -86,7 +87,7 @@ type memWallet struct { // addrs tracks all addresses belonging to the wallet. The addresses // are indexed by their keypath from the hdRoot. - addrs map[uint32]btcutil.Address + addrs map[uint32]address.Address // utxos is the set of utxos spendable by the wallet. utxos map[wire.OutPoint]*utxo @@ -141,7 +142,7 @@ func newMemWallet(net *chaincfg.Params, harnessID uint32) (*memWallet, error) { // Track the coinbase generation address to ensure we properly track // newly generated bitcoin we can spend. - addrs := make(map[uint32]btcutil.Address) + addrs := make(map[uint32]address.Address) addrs[0] = coinbaseAddr return &memWallet{ @@ -335,7 +336,7 @@ func (m *memWallet) unwindBlock(update *chainUpdate) { // newAddress returns a new address from the wallet's hd key chain. It also // loads the address into the RPC client's transaction filter to ensure any // transactions that involve it are delivered via the notifications. -func (m *memWallet) newAddress() (btcutil.Address, error) { +func (m *memWallet) newAddress() (address.Address, error) { index := m.hdIndex childKey, err := m.hdRoot.Derive(index) @@ -352,7 +353,7 @@ func (m *memWallet) newAddress() (btcutil.Address, error) { return nil, err } - err = m.rpc.LoadTxFilter(false, []btcutil.Address{addr}, nil) + err = m.rpc.LoadTxFilter(false, []address.Address{addr}, nil) if err != nil { return nil, err } @@ -367,7 +368,7 @@ func (m *memWallet) newAddress() (btcutil.Address, error) { // NewAddress returns a fresh address spendable by the wallet. // // This function is safe for concurrent access. -func (m *memWallet) NewAddress() (btcutil.Address, error) { +func (m *memWallet) NewAddress() (address.Address, error) { m.Lock() defer m.Unlock() @@ -584,9 +585,11 @@ func (m *memWallet) ConfirmedBalance() btcutil.Amount { } // keyToAddr maps the passed private to corresponding p2pkh address. -func keyToAddr(key *btcec.PrivateKey, net *chaincfg.Params) (btcutil.Address, error) { +func keyToAddr(key *btcec.PrivateKey, net *chaincfg.Params) (address.Address, + error) { + serializedKey := key.PubKey().SerializeCompressed() - pubKeyAddr, err := btcutil.NewAddressPubKey(serializedKey, net) + pubKeyAddr, err := address.NewAddressPubKey(serializedKey, net) if err != nil { return nil, err } diff --git a/integration/rpctest/rpc_harness.go b/integration/rpctest/rpc_harness.go index cb49e42c264..22b1f71c0cb 100644 --- a/integration/rpctest/rpc_harness.go +++ b/integration/rpctest/rpc_harness.go @@ -16,11 +16,12 @@ import ( "testing" "time" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/rpcclient" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) const ( @@ -245,7 +246,7 @@ func (h *Harness) SetUp(createTestChain bool, numMatureOutputs uint32) error { // Filter transactions that pay to the coinbase associated with the // wallet. - filterAddrs := []btcutil.Address{h.wallet.coinbaseAddr} + filterAddrs := []address.Address{h.wallet.coinbaseAddr} if err := h.Client.LoadTxFilter(true, filterAddrs, nil); err != nil { return err } @@ -369,7 +370,7 @@ func (h *Harness) connectRPCClient() error { // wallet. // // This function is safe for concurrent access. -func (h *Harness) NewAddress() (btcutil.Address, error) { +func (h *Harness) NewAddress() (address.Address, error) { return h.wallet.NewAddress() } diff --git a/mining/cpuminer/cpuminer.go b/mining/cpuminer/cpuminer.go index 038e6645bb7..21eda325f98 100644 --- a/mining/cpuminer/cpuminer.go +++ b/mining/cpuminer/cpuminer.go @@ -13,11 +13,12 @@ import ( "time" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/mining" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) const ( @@ -59,7 +60,7 @@ type Config struct { // MiningAddrs is a list of payment addresses to use for the generated // blocks. Each generated block will randomly choose one of them. - MiningAddrs []btcutil.Address + MiningAddrs []address.Address // ProcessBlock defines the function to call with any solved blocks. // It typically must run the provided block through the same set of diff --git a/mining/mining.go b/mining/mining.go index 4ed61f3f320..054bbdfa347 100644 --- a/mining/mining.go +++ b/mining/mining.go @@ -11,11 +11,12 @@ import ( "time" "github.com/btcsuite/btcd/blockchain" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) const ( @@ -250,7 +251,9 @@ func standardCoinbaseScript(nextBlockHeight int32, extraNonce uint64) ([]byte, e // // See the comment for NewBlockTemplate for more information about why the nil // address handling is useful. -func createCoinbaseTx(params *chaincfg.Params, coinbaseScript []byte, nextBlockHeight int32, addr btcutil.Address) (*btcutil.Tx, error) { +func createCoinbaseTx(params *chaincfg.Params, coinbaseScript []byte, + nextBlockHeight int32, addr address.Address) (*btcutil.Tx, error) { + // Create the script to pay to the provided payment address if one was // specified. Otherwise create a script that allows the coinbase to be // redeemable by anyone. @@ -440,7 +443,9 @@ func NewBlkTmplGenerator(policy *Policy, params *chaincfg.Params, // | transactions (while block size | | // | <= policy.BlockMinSize) | | // ----------------------------------- -- -func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress btcutil.Address) (*BlockTemplate, error) { +func (g *BlkTmplGenerator) NewBlockTemplate( + payToAddress address.Address) (*BlockTemplate, error) { + // Extend the most recently known best block. best := g.chain.BestSnapshot() nextBlockHeight := best.Height + 1 diff --git a/rpcclient/extensions.go b/rpcclient/extensions.go index efb6c1c7104..56a48413b2c 100644 --- a/rpcclient/extensions.go +++ b/rpcclient/extensions.go @@ -13,9 +13,9 @@ import ( "fmt" "github.com/btcsuite/btcd/btcjson" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) // FutureDebugLevelResult is a future promise to deliver the result of a @@ -128,7 +128,9 @@ func (r FutureListAddressTransactionsResult) Receive() ([]btcjson.ListTransactio // See ListAddressTransactions for the blocking version and more details. // // NOTE: This is a btcd extension. -func (c *Client) ListAddressTransactionsAsync(addresses []btcutil.Address, account string) FutureListAddressTransactionsResult { +func (c *Client) ListAddressTransactionsAsync(addresses []address.Address, + account string) FutureListAddressTransactionsResult { + // Convert addresses to strings. addrs := make([]string, 0, len(addresses)) for _, addr := range addresses { @@ -142,7 +144,9 @@ func (c *Client) ListAddressTransactionsAsync(addresses []btcutil.Address, accou // with the provided addresses. // // NOTE: This is a btcwallet extension. -func (c *Client) ListAddressTransactions(addresses []btcutil.Address, account string) ([]btcjson.ListTransactionsResult, error) { +func (c *Client) ListAddressTransactions(addresses []address.Address, + account string) ([]btcjson.ListTransactionsResult, error) { + return c.ListAddressTransactionsAsync(addresses, account).Receive() } diff --git a/rpcclient/mining.go b/rpcclient/mining.go index 680a63b6d52..229c3a9afef 100644 --- a/rpcclient/mining.go +++ b/rpcclient/mining.go @@ -10,8 +10,9 @@ import ( "errors" "github.com/btcsuite/btcd/btcjson" - "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" + "github.com/btcsuite/btcd/chaincfg/chainhash" ) // FutureGenerateResult is a future promise to deliver the result of a @@ -98,13 +99,17 @@ func (f FutureGenerateToAddressResult) Receive() ([]*chainhash.Hash, error) { // the returned instance. // // See GenerateToAddress for the blocking version and more details. -func (c *Client) GenerateToAddressAsync(numBlocks int64, address btcutil.Address, maxTries *int64) FutureGenerateToAddressResult { +func (c *Client) GenerateToAddressAsync(numBlocks int64, + address address.Address, maxTries *int64) FutureGenerateToAddressResult { + cmd := btcjson.NewGenerateToAddressCmd(numBlocks, address.EncodeAddress(), maxTries) return c.SendCmd(cmd) } // GenerateToAddress generates numBlocks blocks to the given address and returns their hashes. -func (c *Client) GenerateToAddress(numBlocks int64, address btcutil.Address, maxTries *int64) ([]*chainhash.Hash, error) { +func (c *Client) GenerateToAddress(numBlocks int64, + address address.Address, maxTries *int64) ([]*chainhash.Hash, error) { + return c.GenerateToAddressAsync(numBlocks, address, maxTries).Receive() } diff --git a/rpcclient/notify.go b/rpcclient/notify.go index 225af281cf4..87f1760613a 100644 --- a/rpcclient/notify.go +++ b/rpcclient/notify.go @@ -14,9 +14,10 @@ import ( "time" "github.com/btcsuite/btcd/btcjson" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) var ( @@ -1079,7 +1080,9 @@ func (c *Client) notifyReceivedInternal(addresses []string) FutureNotifyReceived // NOTE: This is a btcd extension and requires a websocket connection. // // Deprecated: Use LoadTxFilterAsync instead. -func (c *Client) NotifyReceivedAsync(addresses []btcutil.Address) FutureNotifyReceivedResult { +func (c *Client) NotifyReceivedAsync( + addresses []address.Address) FutureNotifyReceivedResult { + // Not supported in HTTP POST mode. if c.config.HTTPPostMode { return newFutureError(ErrWebsocketsRequired) @@ -1119,7 +1122,7 @@ func (c *Client) NotifyReceivedAsync(addresses []btcutil.Address) FutureNotifyRe // NOTE: This is a btcd extension and requires a websocket connection. // // Deprecated: Use LoadTxFilter instead. -func (c *Client) NotifyReceived(addresses []btcutil.Address) error { +func (c *Client) NotifyReceived(addresses []address.Address) error { return c.NotifyReceivedAsync(addresses).Receive() } @@ -1152,7 +1155,7 @@ func (r FutureRescanResult) Receive() error { // // Deprecated: Use RescanBlocksAsync instead. func (c *Client) RescanAsync(startBlock *chainhash.Hash, - addresses []btcutil.Address, + addresses []address.Address, outpoints []*wire.OutPoint) FutureRescanResult { // Not supported in HTTP POST mode. @@ -1217,7 +1220,7 @@ func (c *Client) RescanAsync(startBlock *chainhash.Hash, // // Deprecated: Use RescanBlocks instead. func (c *Client) Rescan(startBlock *chainhash.Hash, - addresses []btcutil.Address, + addresses []address.Address, outpoints []*wire.OutPoint) error { return c.RescanAsync(startBlock, addresses, outpoints).Receive() @@ -1233,7 +1236,7 @@ func (c *Client) Rescan(startBlock *chainhash.Hash, // // Deprecated: Use RescanBlocksAsync instead. func (c *Client) RescanEndBlockAsync(startBlock *chainhash.Hash, - addresses []btcutil.Address, outpoints []*wire.OutPoint, + addresses []address.Address, outpoints []*wire.OutPoint, endBlock *chainhash.Hash) FutureRescanResult { // Not supported in HTTP POST mode. @@ -1295,7 +1298,7 @@ func (c *Client) RescanEndBlockAsync(startBlock *chainhash.Hash, // // Deprecated: Use RescanBlocks instead. func (c *Client) RescanEndHeight(startBlock *chainhash.Hash, - addresses []btcutil.Address, outpoints []*wire.OutPoint, + addresses []address.Address, outpoints []*wire.OutPoint, endBlock *chainhash.Hash) error { return c.RescanEndBlockAsync(startBlock, addresses, outpoints, @@ -1327,7 +1330,7 @@ func (r FutureLoadTxFilterResult) Receive() error { // // NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient // and requires a websocket connection. -func (c *Client) LoadTxFilterAsync(reload bool, addresses []btcutil.Address, +func (c *Client) LoadTxFilterAsync(reload bool, addresses []address.Address, outPoints []wire.OutPoint) FutureLoadTxFilterResult { addrStrs := make([]string, len(addresses)) @@ -1352,6 +1355,8 @@ func (c *Client) LoadTxFilterAsync(reload bool, addresses []btcutil.Address, // // NOTE: This is a btcd extension ported from github.com/decred/dcrrpcclient // and requires a websocket connection. -func (c *Client) LoadTxFilter(reload bool, addresses []btcutil.Address, outPoints []wire.OutPoint) error { +func (c *Client) LoadTxFilter(reload bool, addresses []address.Address, + outPoints []wire.OutPoint) error { + return c.LoadTxFilterAsync(reload, addresses, outPoints).Receive() } diff --git a/rpcclient/rawtransactions.go b/rpcclient/rawtransactions.go index 3643f2b0caf..20b34d1449a 100644 --- a/rpcclient/rawtransactions.go +++ b/rpcclient/rawtransactions.go @@ -10,9 +10,10 @@ import ( "encoding/json" "github.com/btcsuite/btcd/btcjson" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) const ( @@ -291,7 +292,7 @@ func (r FutureCreateRawTransactionResult) Receive() (*wire.MsgTx, error) { // // See CreateRawTransaction for the blocking version and more details. func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput, - amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) FutureCreateRawTransactionResult { + amounts map[address.Address]btcutil.Amount, lockTime *int64) FutureCreateRawTransactionResult { convertedAmts := make(map[string]float64, len(amounts)) for addr, amount := range amounts { @@ -305,7 +306,7 @@ func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput, // and sending to the provided addresses. If the inputs are either nil or an // empty slice, it is interpreted as an empty slice. func (c *Client) CreateRawTransaction(inputs []btcjson.TransactionInput, - amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) { + amounts map[address.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) { return c.CreateRawTransactionAsync(inputs, amounts, lockTime).Receive() } @@ -770,7 +771,9 @@ func (r FutureSearchRawTransactionsResult) Receive() ([]*wire.MsgTx, error) { // function on the returned instance. // // See SearchRawTransactions for the blocking version and more details. -func (c *Client) SearchRawTransactionsAsync(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) FutureSearchRawTransactionsResult { +func (c *Client) SearchRawTransactionsAsync(address address.Address, skip, + count int, reverse bool, filterAddrs []string) FutureSearchRawTransactionsResult { + addr := address.EncodeAddress() verbose := btcjson.Int(0) cmd := btcjson.NewSearchRawTransactionsCmd(addr, verbose, &skip, &count, @@ -785,7 +788,9 @@ func (c *Client) SearchRawTransactionsAsync(address btcutil.Address, skip, count // // See SearchRawTransactionsVerbose to retrieve a list of data structures with // information about the transactions instead of the transactions themselves. -func (c *Client) SearchRawTransactions(address btcutil.Address, skip, count int, reverse bool, filterAddrs []string) ([]*wire.MsgTx, error) { +func (c *Client) SearchRawTransactions(address address.Address, skip, count int, + reverse bool, filterAddrs []string) ([]*wire.MsgTx, error) { + return c.SearchRawTransactionsAsync(address, skip, count, reverse, filterAddrs).Receive() } @@ -817,8 +822,9 @@ func (r FutureSearchRawTransactionsVerboseResult) Receive() ([]*btcjson.SearchRa // function on the returned instance. // // See SearchRawTransactionsVerbose for the blocking version and more details. -func (c *Client) SearchRawTransactionsVerboseAsync(address btcutil.Address, skip, - count int, includePrevOut, reverse bool, filterAddrs *[]string) FutureSearchRawTransactionsVerboseResult { +func (c *Client) SearchRawTransactionsVerboseAsync(address address.Address, skip, + count int, includePrevOut, reverse bool, + filterAddrs *[]string) FutureSearchRawTransactionsVerboseResult { addr := address.EncodeAddress() verbose := btcjson.Int(1) @@ -838,8 +844,9 @@ func (c *Client) SearchRawTransactionsVerboseAsync(address btcutil.Address, skip // specifically been enabled. // // See SearchRawTransactions to retrieve a list of raw transactions instead. -func (c *Client) SearchRawTransactionsVerbose(address btcutil.Address, skip, - count int, includePrevOut, reverse bool, filterAddrs []string) ([]*btcjson.SearchRawTransactionsResult, error) { +func (c *Client) SearchRawTransactionsVerbose(address address.Address, skip, + count int, includePrevOut, reverse bool, + filterAddrs []string) ([]*btcjson.SearchRawTransactionsResult, error) { return c.SearchRawTransactionsVerboseAsync(address, skip, count, includePrevOut, reverse, &filterAddrs).Receive() diff --git a/rpcclient/wallet.go b/rpcclient/wallet.go index 7268ad0a192..e44b4cc8575 100644 --- a/rpcclient/wallet.go +++ b/rpcclient/wallet.go @@ -9,10 +9,11 @@ import ( "strconv" "github.com/btcsuite/btcd/btcjson" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" ) // ***************************** @@ -244,7 +245,9 @@ func (c *Client) ListUnspentMinMaxAsync(minConf, maxConf int) FutureListUnspentR // function on the returned instance. // // See ListUnspentMinMaxAddresses for the blocking version and more details. -func (c *Client) ListUnspentMinMaxAddressesAsync(minConf, maxConf int, addrs []btcutil.Address) FutureListUnspentResult { +func (c *Client) ListUnspentMinMaxAddressesAsync(minConf, maxConf int, + addrs []address.Address) FutureListUnspentResult { + addrStrs := make([]string, 0, len(addrs)) for _, a := range addrs { addrStrs = append(addrStrs, a.EncodeAddress()) @@ -278,7 +281,9 @@ func (c *Client) ListUnspentMinMax(minConf, maxConf int) ([]btcjson.ListUnspentR // ListUnspentMinMaxAddresses returns all unspent transaction outputs that pay // to any of specified addresses in a wallet using the specified number of // minimum and maximum number of confirmations as a filter. -func (c *Client) ListUnspentMinMaxAddresses(minConf, maxConf int, addrs []btcutil.Address) ([]btcjson.ListUnspentResult, error) { +func (c *Client) ListUnspentMinMaxAddresses(minConf, maxConf int, + addrs []address.Address) ([]btcjson.ListUnspentResult, error) { + return c.ListUnspentMinMaxAddressesAsync(minConf, maxConf, addrs).Receive() } @@ -536,7 +541,9 @@ func (r FutureSendToAddressResult) Receive() (*chainhash.Hash, error) { // returned instance. // // See SendToAddress for the blocking version and more details. -func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amount) FutureSendToAddressResult { +func (c *Client) SendToAddressAsync(address address.Address, + amount btcutil.Amount) FutureSendToAddressResult { + addr := address.EncodeAddress() cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), nil, nil) return c.SendCmd(cmd) @@ -550,7 +557,9 @@ func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amou // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error) { +func (c *Client) SendToAddress(address address.Address, + amount btcutil.Amount) (*chainhash.Hash, error) { + return c.SendToAddressAsync(address, amount).Receive() } @@ -559,7 +568,7 @@ func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount) ( // function on the returned instance. // // See SendToAddressComment for the blocking version and more details. -func (c *Client) SendToAddressCommentAsync(address btcutil.Address, +func (c *Client) SendToAddressCommentAsync(address address.Address, amount btcutil.Amount, comment, commentTo string) FutureSendToAddressResult { @@ -581,7 +590,10 @@ func (c *Client) SendToAddressCommentAsync(address btcutil.Address, // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SendToAddressComment(address btcutil.Address, amount btcutil.Amount, comment, commentTo string) (*chainhash.Hash, error) { +func (c *Client) SendToAddressComment(address address.Address, + amount btcutil.Amount, comment, commentTo string) (*chainhash.Hash, + error) { + return c.SendToAddressCommentAsync(address, amount, comment, commentTo).Receive() } @@ -615,7 +627,9 @@ func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) { // returned instance. // // See SendFrom for the blocking version and more details. -func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount) FutureSendFromResult { +func (c *Client) SendFromAsync(fromAccount string, toAddress address.Address, + amount btcutil.Amount) FutureSendFromResult { + addr := toAddress.EncodeAddress() cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), nil, nil, nil) @@ -630,7 +644,9 @@ func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address, am // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error) { +func (c *Client) SendFrom(fromAccount string, toAddress address.Address, + amount btcutil.Amount) (*chainhash.Hash, error) { + return c.SendFromAsync(fromAccount, toAddress, amount).Receive() } @@ -639,7 +655,10 @@ func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount // the returned instance. // // See SendFromMinConf for the blocking version and more details. -func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, minConfirms int) FutureSendFromResult { +func (c *Client) SendFromMinConfAsync(fromAccount string, + toAddress address.Address, amount btcutil.Amount, + minConfirms int) FutureSendFromResult { + addr := toAddress.EncodeAddress() cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), &minConfirms, nil, nil) @@ -655,7 +674,9 @@ func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress btcutil.Addr // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, minConfirms int) (*chainhash.Hash, error) { +func (c *Client) SendFromMinConf(fromAccount string, toAddress address.Address, + amount btcutil.Amount, minConfirms int) (*chainhash.Hash, error) { + return c.SendFromMinConfAsync(fromAccount, toAddress, amount, minConfirms).Receive() } @@ -666,7 +687,7 @@ func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address, // // See SendFromComment for the blocking version and more details. func (c *Client) SendFromCommentAsync(fromAccount string, - toAddress btcutil.Address, amount btcutil.Amount, minConfirms int, + toAddress address.Address, amount btcutil.Amount, minConfirms int, comment, commentTo string) FutureSendFromResult { addr := toAddress.EncodeAddress() @@ -686,7 +707,7 @@ func (c *Client) SendFromCommentAsync(fromAccount string, // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SendFromComment(fromAccount string, toAddress btcutil.Address, +func (c *Client) SendFromComment(fromAccount string, toAddress address.Address, amount btcutil.Amount, minConfirms int, comment, commentTo string) (*chainhash.Hash, error) { @@ -723,7 +744,9 @@ func (r FutureSendManyResult) Receive() (*chainhash.Hash, error) { // returned instance. // // See SendMany for the blocking version and more details. -func (c *Client) SendManyAsync(fromAccount string, amounts map[btcutil.Address]btcutil.Amount) FutureSendManyResult { +func (c *Client) SendManyAsync(fromAccount string, + amounts map[address.Address]btcutil.Amount) FutureSendManyResult { + convertedAmounts := make(map[string]float64, len(amounts)) for addr, amount := range amounts { convertedAmounts[addr.EncodeAddress()] = amount.ToBTC() @@ -740,7 +763,9 @@ func (c *Client) SendManyAsync(fromAccount string, amounts map[btcutil.Address]b // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SendMany(fromAccount string, amounts map[btcutil.Address]btcutil.Amount) (*chainhash.Hash, error) { +func (c *Client) SendMany(fromAccount string, + amounts map[address.Address]btcutil.Amount) (*chainhash.Hash, error) { + return c.SendManyAsync(fromAccount, amounts).Receive() } @@ -750,7 +775,7 @@ func (c *Client) SendMany(fromAccount string, amounts map[btcutil.Address]btcuti // // See SendManyMinConf for the blocking version and more details. func (c *Client) SendManyMinConfAsync(fromAccount string, - amounts map[btcutil.Address]btcutil.Amount, + amounts map[address.Address]btcutil.Amount, minConfirms int) FutureSendManyResult { convertedAmounts := make(map[string]float64, len(amounts)) @@ -772,7 +797,7 @@ func (c *Client) SendManyMinConfAsync(fromAccount string, // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. func (c *Client) SendManyMinConf(fromAccount string, - amounts map[btcutil.Address]btcutil.Amount, + amounts map[address.Address]btcutil.Amount, minConfirms int) (*chainhash.Hash, error) { return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms).Receive() @@ -784,7 +809,7 @@ func (c *Client) SendManyMinConf(fromAccount string, // // See SendManyComment for the blocking version and more details. func (c *Client) SendManyCommentAsync(fromAccount string, - amounts map[btcutil.Address]btcutil.Amount, minConfirms int, + amounts map[address.Address]btcutil.Amount, minConfirms int, comment string) FutureSendManyResult { convertedAmounts := make(map[string]float64, len(amounts)) @@ -807,7 +832,7 @@ func (c *Client) SendManyCommentAsync(fromAccount string, // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. func (c *Client) SendManyComment(fromAccount string, - amounts map[btcutil.Address]btcutil.Amount, minConfirms int, + amounts map[address.Address]btcutil.Amount, minConfirms int, comment string) (*chainhash.Hash, error) { return c.SendManyCommentAsync(fromAccount, amounts, minConfirms, @@ -828,7 +853,7 @@ type FutureAddMultisigAddressResult struct { // Receive waits for the Response promised by the future and returns the // multisignature address that requires the specified number of signatures for // the provided addresses. -func (r FutureAddMultisigAddressResult) Receive() (btcutil.Address, error) { +func (r FutureAddMultisigAddressResult) Receive() (address.Address, error) { res, err := ReceiveFuture(r.responseChannel) if err != nil { return nil, err @@ -841,7 +866,7 @@ func (r FutureAddMultisigAddressResult) Receive() (btcutil.Address, error) { return nil, err } - return btcutil.DecodeAddress(addr, r.network) + return address.DecodeAddress(addr, r.network) } // AddMultisigAddressAsync returns an instance of a type that can be used to get @@ -849,7 +874,10 @@ func (r FutureAddMultisigAddressResult) Receive() (btcutil.Address, error) { // the returned instance. // // See AddMultisigAddress for the blocking version and more details. -func (c *Client) AddMultisigAddressAsync(requiredSigs int, addresses []btcutil.Address, account string) FutureAddMultisigAddressResult { +func (c *Client) AddMultisigAddressAsync(requiredSigs int, + addresses []address.Address, + account string) FutureAddMultisigAddressResult { + addrs := make([]string, 0, len(addresses)) for _, addr := range addresses { addrs = append(addrs, addr.String()) @@ -865,7 +893,9 @@ func (c *Client) AddMultisigAddressAsync(requiredSigs int, addresses []btcutil.A // AddMultisigAddress adds a multisignature address that requires the specified // number of signatures for the provided addresses to the wallet. -func (c *Client) AddMultisigAddress(requiredSigs int, addresses []btcutil.Address, account string) (btcutil.Address, error) { +func (c *Client) AddMultisigAddress(requiredSigs int, + addresses []address.Address, account string) (address.Address, error) { + return c.AddMultisigAddressAsync(requiredSigs, addresses, account).Receive() } @@ -896,7 +926,9 @@ func (r FutureCreateMultisigResult) Receive() (*btcjson.CreateMultiSigResult, er // the returned instance. // // See CreateMultisig for the blocking version and more details. -func (c *Client) CreateMultisigAsync(requiredSigs int, addresses []btcutil.Address) FutureCreateMultisigResult { +func (c *Client) CreateMultisigAsync(requiredSigs int, + addresses []address.Address) FutureCreateMultisigResult { + addrs := make([]string, 0, len(addresses)) for _, addr := range addresses { addrs = append(addrs, addr.String()) @@ -909,7 +941,9 @@ func (c *Client) CreateMultisigAsync(requiredSigs int, addresses []btcutil.Addre // CreateMultisig creates a multisignature address that requires the specified // number of signatures for the provided addresses and returns the // multisignature address and script needed to redeem it. -func (c *Client) CreateMultisig(requiredSigs int, addresses []btcutil.Address) (*btcjson.CreateMultiSigResult, error) { +func (c *Client) CreateMultisig(requiredSigs int, + addresses []address.Address) (*btcjson.CreateMultiSigResult, error) { + return c.CreateMultisigAsync(requiredSigs, addresses).Receive() } @@ -1068,7 +1102,7 @@ type FutureGetNewAddressResult struct { // Receive waits for the Response promised by the future and returns a new // address. -func (r FutureGetNewAddressResult) Receive() (btcutil.Address, error) { +func (r FutureGetNewAddressResult) Receive() (address.Address, error) { res, err := ReceiveFuture(r.responseChannel) if err != nil { return nil, err @@ -1081,7 +1115,7 @@ func (r FutureGetNewAddressResult) Receive() (btcutil.Address, error) { return nil, err } - return btcutil.DecodeAddress(addr, r.network) + return address.DecodeAddress(addr, r.network) } // GetNewAddressAsync returns an instance of a type that can be used to get the @@ -1100,7 +1134,7 @@ func (c *Client) GetNewAddressAsync(account string) FutureGetNewAddressResult { // GetNewAddress returns a new address, and decodes based on the client's // chain params. -func (c *Client) GetNewAddress(account string) (btcutil.Address, error) { +func (c *Client) GetNewAddress(account string) (address.Address, error) { return c.GetNewAddressAsync(account).Receive() } @@ -1114,7 +1148,7 @@ type FutureGetRawChangeAddressResult struct { // Receive waits for the Response promised by the future and returns a new // address for receiving change that will be associated with the provided // account. Note that this is only for raw transactions and NOT for normal use. -func (r FutureGetRawChangeAddressResult) Receive() (btcutil.Address, error) { +func (r FutureGetRawChangeAddressResult) Receive() (address.Address, error) { res, err := ReceiveFuture(r.responseChannel) if err != nil { return nil, err @@ -1127,7 +1161,7 @@ func (r FutureGetRawChangeAddressResult) Receive() (btcutil.Address, error) { return nil, err } - return btcutil.DecodeAddress(addr, r.network) + return address.DecodeAddress(addr, r.network) } // GetRawChangeAddressAsync returns an instance of a type that can be used to @@ -1147,7 +1181,7 @@ func (c *Client) GetRawChangeAddressAsync(account string) FutureGetRawChangeAddr // GetRawChangeAddress returns a new address for receiving change that will be // associated with the provided account. Note that this is only for raw // transactions and NOT for normal use. -func (c *Client) GetRawChangeAddress(account string) (btcutil.Address, error) { +func (c *Client) GetRawChangeAddress(account string) (address.Address, error) { return c.GetRawChangeAddressAsync(account).Receive() } @@ -1160,7 +1194,7 @@ type FutureAddWitnessAddressResult struct { // Receive waits for the Response promised by the future and returns the new // address. -func (r FutureAddWitnessAddressResult) Receive() (btcutil.Address, error) { +func (r FutureAddWitnessAddressResult) Receive() (address.Address, error) { res, err := ReceiveFuture(r.responseChannel) if err != nil { return nil, err @@ -1173,7 +1207,7 @@ func (r FutureAddWitnessAddressResult) Receive() (btcutil.Address, error) { return nil, err } - return btcutil.DecodeAddress(addr, r.network) + return address.DecodeAddress(addr, r.network) } // AddWitnessAddressAsync returns an instance of a type that can be used to get @@ -1192,7 +1226,7 @@ func (c *Client) AddWitnessAddressAsync(address string) FutureAddWitnessAddressR // AddWitnessAddress adds a witness address for a script and returns the new // address (P2SH of the witness script). -func (c *Client) AddWitnessAddress(address string) (btcutil.Address, error) { +func (c *Client) AddWitnessAddress(address string) (address.Address, error) { return c.AddWitnessAddressAsync(address).Receive() } @@ -1205,7 +1239,7 @@ type FutureGetAccountAddressResult struct { // Receive waits for the Response promised by the future and returns the current // Bitcoin address for receiving payments to the specified account. -func (r FutureGetAccountAddressResult) Receive() (btcutil.Address, error) { +func (r FutureGetAccountAddressResult) Receive() (address.Address, error) { res, err := ReceiveFuture(r.responseChannel) if err != nil { return nil, err @@ -1218,7 +1252,7 @@ func (r FutureGetAccountAddressResult) Receive() (btcutil.Address, error) { return nil, err } - return btcutil.DecodeAddress(addr, r.network) + return address.DecodeAddress(addr, r.network) } // GetAccountAddressAsync returns an instance of a type that can be used to get @@ -1237,7 +1271,7 @@ func (c *Client) GetAccountAddressAsync(account string) FutureGetAccountAddressR // GetAccountAddress returns the current Bitcoin address for receiving payments // to the specified account. -func (c *Client) GetAccountAddress(account string) (btcutil.Address, error) { +func (c *Client) GetAccountAddress(account string) (address.Address, error) { return c.GetAccountAddressAsync(account).Receive() } @@ -1268,14 +1302,14 @@ func (r FutureGetAccountResult) Receive() (string, error) { // returned instance. // // See GetAccount for the blocking version and more details. -func (c *Client) GetAccountAsync(address btcutil.Address) FutureGetAccountResult { +func (c *Client) GetAccountAsync(address address.Address) FutureGetAccountResult { addr := address.EncodeAddress() cmd := btcjson.NewGetAccountCmd(addr) return c.SendCmd(cmd) } // GetAccount returns the account associated with the passed address. -func (c *Client) GetAccount(address btcutil.Address) (string, error) { +func (c *Client) GetAccount(address address.Address) (string, error) { return c.GetAccountAsync(address).Receive() } @@ -1295,14 +1329,14 @@ func (r FutureSetAccountResult) Receive() error { // returned instance. // // See SetAccount for the blocking version and more details. -func (c *Client) SetAccountAsync(address btcutil.Address, account string) FutureSetAccountResult { +func (c *Client) SetAccountAsync(address address.Address, account string) FutureSetAccountResult { addr := address.EncodeAddress() cmd := btcjson.NewSetAccountCmd(addr, account) return c.SendCmd(cmd) } // SetAccount sets the account associated with the passed address. -func (c *Client) SetAccount(address btcutil.Address, account string) error { +func (c *Client) SetAccount(address address.Address, account string) error { return c.SetAccountAsync(address, account).Receive() } @@ -1315,7 +1349,7 @@ type FutureGetAddressesByAccountResult struct { // Receive waits for the Response promised by the future and returns the list of // addresses associated with the passed account. -func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error) { +func (r FutureGetAddressesByAccountResult) Receive() ([]address.Address, error) { res, err := ReceiveFuture(r.responseChannel) if err != nil { return nil, err @@ -1328,9 +1362,9 @@ func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error) return nil, err } - addresses := make([]btcutil.Address, len(addrStrings)) + addresses := make([]address.Address, len(addrStrings)) for i, addrString := range addrStrings { - addresses[i], err = btcutil.DecodeAddress(addrString, r.network) + addresses[i], err = address.DecodeAddress(addrString, r.network) if err != nil { return nil, err } @@ -1355,7 +1389,7 @@ func (c *Client) GetAddressesByAccountAsync(account string) FutureGetAddressesBy // GetAddressesByAccount returns the list of addresses associated with the // passed account. -func (c *Client) GetAddressesByAccount(account string) ([]btcutil.Address, error) { +func (c *Client) GetAddressesByAccount(account string) ([]address.Address, error) { return c.GetAddressesByAccountAsync(account).Receive() } @@ -1503,14 +1537,18 @@ func (r FutureValidateAddressResult) Receive() (*btcjson.ValidateAddressWalletRe // the returned instance. // // See ValidateAddress for the blocking version and more details. -func (c *Client) ValidateAddressAsync(address btcutil.Address) FutureValidateAddressResult { +func (c *Client) ValidateAddressAsync( + address address.Address) FutureValidateAddressResult { + addr := address.EncodeAddress() cmd := btcjson.NewValidateAddressCmd(addr) return c.SendCmd(cmd) } // ValidateAddress returns information about the given bitcoin address. -func (c *Client) ValidateAddress(address btcutil.Address) (*btcjson.ValidateAddressWalletResult, error) { +func (c *Client) ValidateAddress( + address address.Address) (*btcjson.ValidateAddressWalletResult, error) { + return c.ValidateAddressAsync(address).Receive() } @@ -1913,7 +1951,9 @@ func (r FutureGetReceivedByAddressResult) Receive() (btcutil.Amount, error) { // function on the returned instance. // // See GetReceivedByAddress for the blocking version and more details. -func (c *Client) GetReceivedByAddressAsync(address btcutil.Address) FutureGetReceivedByAddressResult { +func (c *Client) GetReceivedByAddressAsync( + address address.Address) FutureGetReceivedByAddressResult { + addr := address.EncodeAddress() cmd := btcjson.NewGetReceivedByAddressCmd(addr, nil) return c.SendCmd(cmd) @@ -1925,7 +1965,9 @@ func (c *Client) GetReceivedByAddressAsync(address btcutil.Address) FutureGetRec // // See GetReceivedByAddressMinConf to override the minimum number of // confirmations. -func (c *Client) GetReceivedByAddress(address btcutil.Address) (btcutil.Amount, error) { +func (c *Client) GetReceivedByAddress(address address.Address) (btcutil.Amount, + error) { + return c.GetReceivedByAddressAsync(address).Receive() } @@ -1934,7 +1976,9 @@ func (c *Client) GetReceivedByAddress(address btcutil.Address) (btcutil.Amount, // function on the returned instance. // // See GetReceivedByAddressMinConf for the blocking version and more details. -func (c *Client) GetReceivedByAddressMinConfAsync(address btcutil.Address, minConfirms int) FutureGetReceivedByAddressResult { +func (c *Client) GetReceivedByAddressMinConfAsync(address address.Address, + minConfirms int) FutureGetReceivedByAddressResult { + addr := address.EncodeAddress() cmd := btcjson.NewGetReceivedByAddressCmd(addr, &minConfirms) return c.SendCmd(cmd) @@ -1944,7 +1988,9 @@ func (c *Client) GetReceivedByAddressMinConfAsync(address btcutil.Address, minCo // address with at least the specified number of minimum confirmations. // // See GetReceivedByAddress to use the default minimum number of confirmations. -func (c *Client) GetReceivedByAddressMinConf(address btcutil.Address, minConfirms int) (btcutil.Amount, error) { +func (c *Client) GetReceivedByAddressMinConf(address address.Address, + minConfirms int) (btcutil.Amount, error) { + return c.GetReceivedByAddressMinConfAsync(address, minConfirms).Receive() } @@ -2223,7 +2269,9 @@ func (r FutureSignMessageResult) Receive() (string, error) { // returned instance. // // See SignMessage for the blocking version and more details. -func (c *Client) SignMessageAsync(address btcutil.Address, message string) FutureSignMessageResult { +func (c *Client) SignMessageAsync(address address.Address, + message string) FutureSignMessageResult { + addr := address.EncodeAddress() cmd := btcjson.NewSignMessageCmd(addr, message) return c.SendCmd(cmd) @@ -2233,7 +2281,9 @@ func (c *Client) SignMessageAsync(address btcutil.Address, message string) Futur // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) SignMessage(address btcutil.Address, message string) (string, error) { +func (c *Client) SignMessage(address address.Address, message string) (string, + error) { + return c.SignMessageAsync(address, message).Receive() } @@ -2264,7 +2314,9 @@ func (r FutureVerifyMessageResult) Receive() (bool, error) { // returned instance. // // See VerifyMessage for the blocking version and more details. -func (c *Client) VerifyMessageAsync(address btcutil.Address, signature, message string) FutureVerifyMessageResult { +func (c *Client) VerifyMessageAsync(address address.Address, signature, + message string) FutureVerifyMessageResult { + addr := address.EncodeAddress() cmd := btcjson.NewVerifyMessageCmd(addr, signature, message) return c.SendCmd(cmd) @@ -2274,7 +2326,9 @@ func (c *Client) VerifyMessageAsync(address btcutil.Address, signature, message // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) VerifyMessage(address btcutil.Address, signature, message string) (bool, error) { +func (c *Client) VerifyMessage(address address.Address, signature, + message string) (bool, error) { + return c.VerifyMessageAsync(address, signature, message).Receive() } @@ -2310,7 +2364,9 @@ func (r FutureDumpPrivKeyResult) Receive() (*btcutil.WIF, error) { // returned instance. // // See DumpPrivKey for the blocking version and more details. -func (c *Client) DumpPrivKeyAsync(address btcutil.Address) FutureDumpPrivKeyResult { +func (c *Client) DumpPrivKeyAsync( + address address.Address) FutureDumpPrivKeyResult { + addr := address.EncodeAddress() cmd := btcjson.NewDumpPrivKeyCmd(addr) return c.SendCmd(cmd) @@ -2321,7 +2377,7 @@ func (c *Client) DumpPrivKeyAsync(address btcutil.Address) FutureDumpPrivKeyResu // // NOTE: This function requires to the wallet to be unlocked. See the // WalletPassphrase function for more details. -func (c *Client) DumpPrivKey(address btcutil.Address) (*btcutil.WIF, error) { +func (c *Client) DumpPrivKey(address address.Address) (*btcutil.WIF, error) { return c.DumpPrivKeyAsync(address).Receive() } diff --git a/rpcserver.go b/rpcserver.go index b8012b2b01b..69b5dc24a67 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -32,6 +32,7 @@ import ( "github.com/btcsuite/btcd/btcec/v2/ecdsa" "github.com/btcsuite/btcd/btcjson" "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/database" @@ -556,7 +557,7 @@ func handleCreateRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan } // Decode the provided address. - addr, err := btcutil.DecodeAddress(encodedAddr, params) + addr, err := address.DecodeAddress(encodedAddr, params) if err != nil { return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCInvalidAddressOrKey, @@ -568,8 +569,8 @@ func handleCreateRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan // the network encoded with the address matches the network the // server is currently on. switch addr.(type) { - case *btcutil.AddressPubKeyHash: - case *btcutil.AddressScriptHash: + case *address.AddressPubKeyHash: + case *address.AddressScriptHash: default: return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCInvalidAddressOrKey, @@ -841,7 +842,7 @@ func handleDecodeScript(s *rpcServer, cmd interface{}, closeChan <-chan struct{} } // Convert the script itself to a pay-to-script-hash address. - p2sh, err := btcutil.NewAddressScriptHash(script, s.cfg.ChainParams) + p2sh, err := address.NewAddressScriptHash(script, s.cfg.ChainParams) if err != nil { context := "Failed to convert script to pay-to-script-hash" return nil, internalRPCError(err.Error(), context) @@ -1577,7 +1578,7 @@ func (state *gbtWorkState) updateBlockTemplate(s *rpcServer, useCoinbaseValue bo // Choose a payment address at random if the caller requests a // full coinbase as opposed to only the pertinent details needed // to create their own coinbase. - var payAddr btcutil.Address + var payAddr address.Address if !useCoinbaseValue { payAddr = cfg.miningAddrs[rand.Intn(len(cfg.miningAddrs))] } @@ -3066,7 +3067,9 @@ func createVinListPrevOut(s *rpcServer, mtx *wire.MsgTx, chainParams *chaincfg.P // fetchMempoolTxnsForAddress queries the address index for all unconfirmed // transactions that involve the provided address. The results will be limited // by the number to skip and the number requested. -func fetchMempoolTxnsForAddress(s *rpcServer, addr btcutil.Address, numToSkip, numRequested uint32) ([]*btcutil.Tx, uint32) { +func fetchMempoolTxnsForAddress(s *rpcServer, addr address.Address, numToSkip, + numRequested uint32) ([]*btcutil.Tx, uint32) { + // There are no entries to return when there are less available than the // number being skipped. mpTxns := s.cfg.AddrIndex.UnconfirmedTxnsForAddress(addr) @@ -3116,7 +3119,7 @@ func handleSearchRawTransactions(s *rpcServer, cmd interface{}, closeChan <-chan // Attempt to decode the supplied address. params := s.cfg.ChainParams - addr, err := btcutil.DecodeAddress(c.Address, params) + addr, err := address.DecodeAddress(c.Address, params) if err != nil { return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCInvalidAddressOrKey, @@ -3500,7 +3503,7 @@ func handleSignMessageWithPrivKey(s *rpcServer, cmd interface{}, closeChan <-cha switch err { case btcutil.ErrMalformedPrivateKey: message = "Malformed private key" - case btcutil.ErrChecksumMismatch: + case address.ErrChecksumMismatch: message = "Private key checksum mismatch" } return nil, &btcjson.RPCError{ @@ -3584,32 +3587,32 @@ func handleValidateAddress(s *rpcServer, cmd interface{}, closeChan <-chan struc c := cmd.(*btcjson.ValidateAddressCmd) result := btcjson.ValidateAddressChainResult{} - addr, err := btcutil.DecodeAddress(c.Address, s.cfg.ChainParams) + addr, err := address.DecodeAddress(c.Address, s.cfg.ChainParams) if err != nil { // Return the default value (false) for IsValid. return result, nil } switch addr := addr.(type) { - case *btcutil.AddressPubKeyHash: + case *address.AddressPubKeyHash: result.IsScript = btcjson.Bool(false) result.IsWitness = btcjson.Bool(false) - case *btcutil.AddressScriptHash: + case *address.AddressScriptHash: result.IsScript = btcjson.Bool(true) result.IsWitness = btcjson.Bool(false) - case *btcutil.AddressPubKey: + case *address.AddressPubKey: result.IsScript = btcjson.Bool(false) result.IsWitness = btcjson.Bool(false) - case *btcutil.AddressWitnessPubKeyHash: + case *address.AddressWitnessPubKeyHash: result.IsScript = btcjson.Bool(false) result.IsWitness = btcjson.Bool(true) result.WitnessVersion = btcjson.Int32(int32(addr.WitnessVersion())) result.WitnessProgram = btcjson.String(hex.EncodeToString(addr.WitnessProgram())) - case *btcutil.AddressWitnessScriptHash: + case *address.AddressWitnessScriptHash: result.IsScript = btcjson.Bool(true) result.IsWitness = btcjson.Bool(true) result.WitnessVersion = btcjson.Int32(int32(addr.WitnessVersion())) @@ -3684,7 +3687,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{ // Decode the provided address. params := s.cfg.ChainParams - addr, err := btcutil.DecodeAddress(c.Address, params) + addr, err := address.DecodeAddress(c.Address, params) if err != nil { return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCInvalidAddressOrKey, @@ -3693,7 +3696,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{ } // Only P2PKH addresses are valid for signing. - if _, ok := addr.(*btcutil.AddressPubKeyHash); !ok { + if _, ok := addr.(*address.AddressPubKeyHash); !ok { return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCType, Message: "Address is not a pay-to-pubkey-hash address", @@ -3730,7 +3733,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{ } else { serializedPK = pk.SerializeUncompressed() } - address, err := btcutil.NewAddressPubKey(serializedPK, params) + addr2, err := address.NewAddressPubKey(serializedPK, params) if err != nil { // Again mirror Bitcoin Core behavior, which treats error in public key // reconstruction as invalid signature. @@ -3738,7 +3741,7 @@ func handleVerifyMessage(s *rpcServer, cmd interface{}, closeChan <-chan struct{ } // Return boolean if addresses match. - return address.EncodeAddress() == c.Address, nil + return addr2.EncodeAddress() == c.Address, nil } // handleVersion implements the version command. diff --git a/rpcwebsocket.go b/rpcwebsocket.go index 4d140b4825c..94d82354f51 100644 --- a/rpcwebsocket.go +++ b/rpcwebsocket.go @@ -22,12 +22,13 @@ import ( "github.com/btcsuite/btcd/blockchain" "github.com/btcsuite/btcd/btcjson" + "github.com/btcsuite/btcd/btcutil" + "github.com/btcsuite/btcd/btcutil/address" "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/database" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btcd/btcutil" "github.com/btcsuite/websocket" "golang.org/x/crypto/ripemd160" ) @@ -296,15 +297,15 @@ func newWSClientFilter(addresses []string, unspentOutPoints []wire.OutPoint, par // on the type of address passed as an argument. // // NOTE: This extension was ported from github.com/decred/dcrd -func (f *wsClientFilter) addAddress(a btcutil.Address) { +func (f *wsClientFilter) addAddress(a address.Address) { switch a := a.(type) { - case *btcutil.AddressPubKeyHash: + case *address.AddressPubKeyHash: f.pubKeyHashes[*a.Hash160()] = struct{}{} return - case *btcutil.AddressScriptHash: + case *address.AddressScriptHash: f.scriptHashes[*a.Hash160()] = struct{}{} return - case *btcutil.AddressPubKey: + case *address.AddressPubKey: serializedPubKey := a.ScriptAddress() switch len(serializedPubKey) { case 33: // compressed @@ -331,7 +332,7 @@ func (f *wsClientFilter) addAddressStr(s string, params *chaincfg.Params) { // If address can't be decoded, no point in saving it since it should also // impossible to create the address from an inspected transaction output // script. - a, err := btcutil.DecodeAddress(s, params) + a, err := address.DecodeAddress(s, params) if err != nil { return } @@ -342,15 +343,15 @@ func (f *wsClientFilter) addAddressStr(s string, params *chaincfg.Params) { // wsClientFilter. // // NOTE: This extension was ported from github.com/decred/dcrd -func (f *wsClientFilter) existsAddress(a btcutil.Address) bool { +func (f *wsClientFilter) existsAddress(a address.Address) bool { switch a := a.(type) { - case *btcutil.AddressPubKeyHash: + case *address.AddressPubKeyHash: _, ok := f.pubKeyHashes[*a.Hash160()] return ok - case *btcutil.AddressScriptHash: + case *address.AddressScriptHash: _, ok := f.scriptHashes[*a.Hash160()] return ok - case *btcutil.AddressPubKey: + case *address.AddressPubKey: serializedPubKey := a.ScriptAddress() switch len(serializedPubKey) { case 33: // compressed @@ -380,15 +381,15 @@ func (f *wsClientFilter) existsAddress(a btcutil.Address) bool { // wsClientFilter. // // NOTE: This extension was ported from github.com/decred/dcrd -func (f *wsClientFilter) removeAddress(a btcutil.Address) { +func (f *wsClientFilter) removeAddress(a address.Address) { switch a := a.(type) { - case *btcutil.AddressPubKeyHash: + case *address.AddressPubKeyHash: delete(f.pubKeyHashes, *a.Hash160()) return - case *btcutil.AddressScriptHash: + case *address.AddressScriptHash: delete(f.scriptHashes, *a.Hash160()) return - case *btcutil.AddressPubKey: + case *address.AddressPubKey: serializedPubKey := a.ScriptAddress() switch len(serializedPubKey) { case 33: // compressed @@ -412,7 +413,7 @@ func (f *wsClientFilter) removeAddress(a btcutil.Address) { // // NOTE: This extension was ported from github.com/decred/dcrd func (f *wsClientFilter) removeAddressStr(s string, params *chaincfg.Params) { - a, err := btcutil.DecodeAddress(s, params) + a, err := address.DecodeAddress(s, params) if err == nil { f.removeAddress(a) } else { @@ -2231,7 +2232,7 @@ func handleStopNotifyReceived(wsc *wsClient, icmd interface{}) (interface{}, err // properly, the function returns an error. Otherwise, nil is returned. func checkAddressValidity(addrs []string, params *chaincfg.Params) error { for _, addr := range addrs { - _, err := btcutil.DecodeAddress(addr, params) + _, err := address.DecodeAddress(addr, params) if err != nil { return &btcjson.RPCError{ Code: btcjson.ErrRPCInvalidAddressOrKey,