Skip to content

Commit

Permalink
Merge pull request #149 from renproject/fix/decode-segwit-address
Browse files Browse the repository at this point in the history
Network check when decoding address
  • Loading branch information
tok-kkk authored May 31, 2022
2 parents 0325fe4 + 69c68db commit 81b3048
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions chain/bitcoin/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAd
if err != nil {
return nil, fmt.Errorf("decode address: %v", err)
}
if !decodedAddr.IsForNet(decoder.params) {
return nil, fmt.Errorf("address of different network")
}

switch a := decodedAddr.(type) {
case *btcutil.AddressPubKeyHash, *btcutil.AddressScriptHash:
Expand Down
3 changes: 3 additions & 0 deletions chain/bitcoin/utxo.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func (txBuilder TxBuilder) BuildTx(inputs []utxo.Input, recipients []utxo.Recipi
if err != nil {
return nil, err
}
if !addr.IsForNet(txBuilder.params) {
return nil, fmt.Errorf("addr of a different network")
}
script, err := txscript.PayToAddrScript(addr)
if err != nil {
return nil, err
Expand Down
3 changes: 3 additions & 0 deletions chain/bitcoincash/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ func (encoder AddressEncoder) EncodeAddress(rawAddr address.RawAddress) (address
func (decoder AddressDecoder) DecodeAddress(addr address.Address) (address.RawAddress, error) {
// Legacy address decoding
if legacyAddr, err := btcutil.DecodeAddress(string(addr), decoder.params); err == nil {
if !legacyAddr.IsForNet(decoder.params) {
return nil, fmt.Errorf("address of different network")
}
switch legacyAddr.(type) {
case *btcutil.AddressPubKeyHash, *btcutil.AddressScriptHash, *btcutil.AddressPubKey:
return decodeLegacyAddress(addr, decoder.params)
Expand Down
23 changes: 23 additions & 0 deletions chain/dogecoin/address_test.go
Original file line number Diff line number Diff line change
@@ -1 +1,24 @@
package dogecoin_test

import (
"github.com/renproject/multichain"
"github.com/renproject/multichain/chain/bitcoin"
"github.com/renproject/multichain/chain/dogecoin"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Dogecoin", func() {
Context("when decoding segwit address", func() {
Context("when decoding an address from a different network ", func() {
It("should return an error ", func() {
// A valid bitcoin segwit address which is not a valid doge address
addr := multichain.Address("bc1qk6yk2ctcu2pmtxfzhya692h774562vlv2g7dvl")
decoder := bitcoin.NewAddressDecoder(&dogecoin.MainNetParams)
_, err := decoder.DecodeAddress(addr)
Expect(err).To(HaveOccurred())
})
})
})
})
4 changes: 2 additions & 2 deletions infra/terra/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ RUN tar -C /usr/local -xzf go1.16.8.linux-amd64.tar.gz
ENV PATH=$PATH:/usr/local/go/bin

WORKDIR /app
RUN git clone https://github.com/terra-money/core.git
WORKDIR /app/core
RUN git clone https://github.com/terra-money/classic-core.git
WORKDIR /app/classic-core
RUN git fetch --all -p
RUN git checkout v0.5.5
RUN make install
Expand Down

0 comments on commit 81b3048

Please sign in to comment.