-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add a method for validating an address for a network version (#115
) Currently, all addresses are valid (up to nv17). When we land the nv18 patch, we'll allow "delegated" addresses in nv18 only.
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package abi | ||
|
||
import ( | ||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/network" | ||
) | ||
|
||
// AddressValidForNetworkVersion returns true if the address is supported by the given network | ||
// version. | ||
// | ||
// NOTE: It will _also_ return true if the address is "empty", because all versions support empty | ||
// addresses in some places. I.e., it's not a version specific check. | ||
func AddressValidForNetworkVersion(addr address.Address, nv network.Version) bool { | ||
// We define "undefined" addresses as "supported". The user should check for those | ||
// separately. | ||
if addr == address.Undef { | ||
return true | ||
} | ||
|
||
switch addr.Protocol() { | ||
case address.ID, address.SECP256K1, address.Actor, address.BLS: | ||
return true | ||
default: | ||
return false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package abi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/filecoin-project/go-address" | ||
"github.com/filecoin-project/go-state-types/network" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestAddressValidForNetworkVersion(t *testing.T) { | ||
id, _ := address.NewIDAddress(1) | ||
bls, _ := address.NewBLSAddress(make([]byte, address.BlsPublicKeyBytes)) | ||
secp, _ := address.NewSecp256k1Address(make([]byte, address.PayloadHashLength)) | ||
actor, _ := address.NewActorAddress(make([]byte, address.PayloadHashLength)) | ||
for _, addr := range []address.Address{id, bls, secp, actor} { | ||
require.True(t, AddressValidForNetworkVersion(addr, network.Version17)) | ||
} | ||
} |