From ccc0b1c1feb3c3cfeed517712a8d7dd575812314 Mon Sep 17 00:00:00 2001 From: Rohit Narurkar Date: Mon, 28 Jun 2021 15:31:54 +0400 Subject: [PATCH] fix(filecoin): catch and return error on undefined address --- chain/filecoin/address.go | 8 ++++++++ chain/filecoin/address_test.go | 14 ++++++++++++++ go.sum | 1 + 3 files changed, 23 insertions(+) diff --git a/chain/filecoin/address.go b/chain/filecoin/address.go index cb34c5a6..a20c8459 100644 --- a/chain/filecoin/address.go +++ b/chain/filecoin/address.go @@ -1,6 +1,8 @@ package filecoin import ( + "fmt" + filaddress "github.com/filecoin-project/go-address" "github.com/renproject/multichain/api/address" ) @@ -32,6 +34,9 @@ func (encoder AddressEncoder) EncodeAddress(raw address.RawAddress) (address.Add if err != nil { return address.Address(""), err } + if addr == filaddress.Undef { + return address.Address(""), fmt.Errorf("encoding address: undefined address=%v", raw) + } return address.Address(addr.String()), nil } @@ -42,5 +47,8 @@ func (addrDecoder AddressDecoder) DecodeAddress(addr address.Address) (address.R if err != nil { return nil, err } + if rawAddr == filaddress.Undef { + return nil, fmt.Errorf("decoding address: undefined address=%v", addr) + } return address.RawAddress(rawAddr.Bytes()), nil } diff --git a/chain/filecoin/address_test.go b/chain/filecoin/address_test.go index 670419ef..33c1b2b3 100644 --- a/chain/filecoin/address_test.go +++ b/chain/filecoin/address_test.go @@ -94,4 +94,18 @@ var _ = Describe("Address", func() { }) }) }) + + Context("when encoding empty address", func() { + It("should fail", func() { + _, err := encoderDecoder.EncodeAddress(address.RawAddress(pack.Bytes([]byte{}))) + Expect(err).To(HaveOccurred()) + }) + }) + + Context("when decoding empty address", func() { + It("should fail", func() { + _, err := encoderDecoder.DecodeAddress(address.Address("")) + Expect(err).To(HaveOccurred()) + }) + }) }) diff --git a/go.sum b/go.sum index f49524f0..58a5995a 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,7 @@ dmitri.shuralyov.com/html/belt v0.0.0-20180602232347-f7d459c86be0/go.mod h1:JLBr dmitri.shuralyov.com/service/change v0.0.0-20181023043359-a85b471d5412/go.mod h1:a1inKt/atXimZ4Mv927x+r7UpyzRUf4emIoiiSC2TN4= dmitri.shuralyov.com/state v0.0.0-20180228185332-28bcc343414c/go.mod h1:0PRwlb0D6DFvNNtx+9ybjezNCa8XF0xaYcETyp6rHWU= git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg= +github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4 h1:/vQbFIOMbk2FiG/kXiLl8BRyzTWDw7gX/Hz7Dd5eDMs= github.com/99designs/go-keychain v0.0.0-20191008050251-8e49817e8af4/go.mod h1:hN7oaIRCjzsZ2dE+yG5k+rsdt3qcwykqK6HVGcKwsw4= github.com/99designs/keyring v1.1.3 h1:mEV3iyZWjkxQ7R8ia8GcG97vCX5zQQ7n4o8R2BylwQY= github.com/99designs/keyring v1.1.3/go.mod h1:657DQuMrBZRtuL/voxVyiyb6zpMehlm5vLB9Qwrv904=