Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add a method for validating an address for a network version #115

Merged
merged 1 commit into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}