Skip to content

Commit

Permalink
feat: add a method for validating an address for a network version (#115
Browse files Browse the repository at this point in the history
)

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
Stebalien authored Nov 8, 2022
1 parent 0b1be5f commit 276e899
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
26 changes: 26 additions & 0 deletions abi/address.go
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
}
}
19 changes: 19 additions & 0 deletions abi/address_test.go
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))
}
}

0 comments on commit 276e899

Please sign in to comment.