contracts/ens: added support for fallback contract
This commit is contained in:
@ -28,6 +28,7 @@ import (
|
|||||||
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
||||||
"github.com/ethereum/go-ethereum/common"
|
"github.com/ethereum/go-ethereum/common"
|
||||||
"github.com/ethereum/go-ethereum/contracts/ens/contract"
|
"github.com/ethereum/go-ethereum/contracts/ens/contract"
|
||||||
|
"github.com/ethereum/go-ethereum/contracts/ens/fallback_contract"
|
||||||
"github.com/ethereum/go-ethereum/core/types"
|
"github.com/ethereum/go-ethereum/core/types"
|
||||||
"github.com/ethereum/go-ethereum/crypto"
|
"github.com/ethereum/go-ethereum/crypto"
|
||||||
)
|
)
|
||||||
@ -119,6 +120,21 @@ func (ens *ENS) getResolver(node [32]byte) (*contract.PublicResolverSession, err
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ens *ENS) getFallbackResolver(node [32]byte) (*fallback_contract.PublicResolverSession, error) {
|
||||||
|
resolverAddr, err := ens.Resolver(node)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
resolver, err := fallback_contract.NewPublicResolver(resolverAddr, ens.contractBackend)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &fallback_contract.PublicResolverSession{
|
||||||
|
Contract: resolver,
|
||||||
|
TransactOpts: ens.TransactOpts,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (ens *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
|
func (ens *ENS) getRegistrar(node [32]byte) (*contract.FIFSRegistrarSession, error) {
|
||||||
registrarAddr, err := ens.Owner(node)
|
registrarAddr, err := ens.Owner(node)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -150,7 +166,15 @@ func (ens *ENS) Resolve(name string) (common.Hash, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !supported {
|
if !supported {
|
||||||
panic("w00t")
|
resolver, err := ens.getFallbackResolver(node)
|
||||||
|
if err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
ret, err := resolver.Content(node)
|
||||||
|
if err != nil {
|
||||||
|
return common.Hash{}, err
|
||||||
|
}
|
||||||
|
return common.BytesToHash(ret[:]), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// END DEPRECATED CODE
|
// END DEPRECATED CODE
|
||||||
@ -212,6 +236,9 @@ func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, er
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
opts := ens.TransactOpts
|
||||||
|
opts.GasLimit = 200000
|
||||||
|
|
||||||
// IMPORTANT: The old contract is deprecated. This code should be removed latest on June 1st 2019
|
// IMPORTANT: The old contract is deprecated. This code should be removed latest on June 1st 2019
|
||||||
supported, err := resolver.SupportsInterface(contentHash_Interface_Id)
|
supported, err := resolver.SupportsInterface(contentHash_Interface_Id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -219,12 +246,17 @@ func (ens *ENS) SetContentHash(name string, hash []byte) (*types.Transaction, er
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !supported {
|
if !supported {
|
||||||
panic("w00t")
|
resolver, err := ens.getFallbackResolver(node)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
opts := ens.TransactOpts
|
||||||
|
opts.GasLimit = 200000
|
||||||
|
var b [32]byte
|
||||||
|
copy(b[:], hash)
|
||||||
|
return resolver.Contract.SetContent(&opts, node, b)
|
||||||
}
|
}
|
||||||
|
|
||||||
// END DEPRECATED CODE
|
// END DEPRECATED CODE
|
||||||
|
|
||||||
opts := ens.TransactOpts
|
|
||||||
opts.GasLimit = 200000
|
|
||||||
return resolver.Contract.SetContenthash(&opts, node, hash)
|
return resolver.Contract.SetContenthash(&opts, node, hash)
|
||||||
}
|
}
|
||||||
|
@ -30,11 +30,12 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
||||||
name = "my name on ENS"
|
name = "my name on ENS"
|
||||||
hash = crypto.Keccak256Hash([]byte("my content"))
|
hash = crypto.Keccak256Hash([]byte("my content"))
|
||||||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
fallbackHash = crypto.Keccak256Hash([]byte("my content hash"))
|
||||||
testAddr = common.HexToAddress("0x1234123412341234123412341234123412341234")
|
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||||
|
testAddr = common.HexToAddress("0x1234123412341234123412341234123412341234")
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestENS(t *testing.T) {
|
func TestENS(t *testing.T) {
|
||||||
@ -94,17 +95,17 @@ func TestENS(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deploy the fallback contract and see that the fallback mechanism works
|
// deploy the fallback contract and see that the fallback mechanism works
|
||||||
newResolverAddr, _, _, err := fallback_contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr)
|
fallbackResolverAddr, _, _, err := fallback_contract.DeployPublicResolver(transactOpts, contractBackend, ensAddr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("can't deploy resolver: %v", err)
|
t.Fatalf("can't deploy resolver: %v", err)
|
||||||
}
|
}
|
||||||
if _, err := ens.SetResolver(EnsNode(name), newResolverAddr); err != nil {
|
if _, err := ens.SetResolver(EnsNode(name), fallbackResolverAddr); err != nil {
|
||||||
t.Fatalf("can't set resolver: %v", err)
|
t.Fatalf("can't set resolver: %v", err)
|
||||||
}
|
}
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
|
|
||||||
// Set the content hash for the name.
|
// Set the content hash for the name.
|
||||||
if _, err = ens.SetContentHash(name, hash.Bytes()); err != nil {
|
if _, err = ens.SetContentHash(name, fallbackHash.Bytes()); err != nil {
|
||||||
t.Fatalf("can't set content hash: %v", err)
|
t.Fatalf("can't set content hash: %v", err)
|
||||||
}
|
}
|
||||||
contractBackend.Commit()
|
contractBackend.Commit()
|
||||||
@ -114,7 +115,7 @@ func TestENS(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("expected no error, got %v", err)
|
t.Fatalf("expected no error, got %v", err)
|
||||||
}
|
}
|
||||||
if vhost != hash {
|
if vhost != fallbackHash {
|
||||||
t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex())
|
t.Fatalf("resolve error, expected %v, got %v", hash.Hex(), vhost.Hex())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user