mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-25 14:16:44 +00:00
swarm/services/ens: expose registrar/resolver deploy in API
This commit is contained in:
parent
03f67527d8
commit
4ddfb2de99
3 changed files with 42 additions and 38 deletions
|
|
@ -116,7 +116,16 @@ const ENS_JS = `
|
|||
web3._extend({
|
||||
property: 'ens',
|
||||
methods:
|
||||
[ new web3._extend.Method({
|
||||
[
|
||||
new web3._extend.Method({
|
||||
name: 'deployRegistrar',
|
||||
call: 'ens_deployRegistrar',
|
||||
}),
|
||||
new web3._extend.Method({
|
||||
name: 'deployResolver',
|
||||
call: 'ens_deployResolver',
|
||||
}),
|
||||
new web3._extend.Method({
|
||||
name: 'register',
|
||||
call: 'ens_register',
|
||||
params: 2,
|
||||
|
|
|
|||
|
|
@ -13,32 +13,48 @@ import (
|
|||
"github.com/ethereum/go-ethereum/swarm/storage"
|
||||
)
|
||||
|
||||
var qtypeChash = [32]byte{ 0x43, 0x48, 0x41, 0x53, 0x48}
|
||||
var rtypeChash = [16]byte{ 0x43, 0x48, 0x41, 0x53, 0x48}
|
||||
var qtypeChash = [32]byte{0x43, 0x48, 0x41, 0x53, 0x48}
|
||||
var rtypeChash = [16]byte{0x43, 0x48, 0x41, 0x53, 0x48}
|
||||
|
||||
// swarm domain name registry and resolver
|
||||
// the ENS instance can be directly wrapped in rpc.Api
|
||||
type ENS struct {
|
||||
transactOpts *bind.TransactOpts;
|
||||
contractBackend bind.ContractBackend;
|
||||
rootAddress common.Address;
|
||||
transactOpts *bind.TransactOpts
|
||||
contractBackend bind.ContractBackend
|
||||
rootAddress common.Address
|
||||
}
|
||||
|
||||
func NewENS(transactOpts *bind.TransactOpts, contractAddr common.Address, contractBackend bind.ContractBackend) *ENS {
|
||||
return &ENS{
|
||||
transactOpts: transactOpts,
|
||||
transactOpts: transactOpts,
|
||||
contractBackend: contractBackend,
|
||||
rootAddress: contractAddr,
|
||||
rootAddress: contractAddr,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *ENS) DeployRegistrar() (common.Address, error) {
|
||||
addr, _, _, err := contract.DeployOpenRegistrar(self.transactOpts, self.contractBackend)
|
||||
if err != nil {
|
||||
return common.Address{}, err
|
||||
}
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
func (self *ENS) DeployResolver() (common.Address, error) {
|
||||
addr, _, _, err := contract.DeployPersonalResolver(self.transactOpts, self.contractBackend)
|
||||
if err != nil {
|
||||
return common.Address{}, err
|
||||
}
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
func (self *ENS) newResolver(contractAddr common.Address) (*contract.ResolverSession, error) {
|
||||
resolver, err := contract.NewResolver(contractAddr, self.contractBackend)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &contract.ResolverSession{
|
||||
Contract: resolver,
|
||||
Contract: resolver,
|
||||
TransactOpts: *self.transactOpts,
|
||||
}, nil
|
||||
}
|
||||
|
|
@ -59,7 +75,7 @@ func (self *ENS) nextResolver(resolver *contract.ResolverSession, nodeId [12]byt
|
|||
err = fmt.Errorf("error resolving label '%v': got response code %v", label, ret.Rcode)
|
||||
return nil, [12]byte{}, err
|
||||
}
|
||||
nodeId = ret.Rnode;
|
||||
nodeId = ret.Rnode
|
||||
resolver, err = self.newResolver(ret.Raddress)
|
||||
if err != nil {
|
||||
return nil, [12]byte{}, err
|
||||
|
|
@ -149,7 +165,7 @@ func (self *ENS) findPersonalResolver(name string) (*contract.ResolverSession, [
|
|||
|
||||
for i := len(labels) - 1; i >= 0; i-- {
|
||||
if personal, _ := resolver.IsPersonalResolver(); personal {
|
||||
return resolver, nodeId, strings.Join(labels[0:i + 1], "."), nil
|
||||
return resolver, nodeId, strings.Join(labels[0:i+1], "."), nil
|
||||
}
|
||||
|
||||
resolver, nodeId, err = self.nextResolver(resolver, nodeId, labels[i])
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@
|
|||
package ens
|
||||
|
||||
import (
|
||||
"crypto/ecdsa"
|
||||
"math/big"
|
||||
"testing"
|
||||
|
||||
|
|
@ -12,7 +11,6 @@ import (
|
|||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/swarm/services/ens/contract"
|
||||
)
|
||||
|
||||
var (
|
||||
|
|
@ -22,42 +20,23 @@ var (
|
|||
addr = crypto.PubkeyToAddress(key.PublicKey)
|
||||
)
|
||||
|
||||
func deployRegistrar(prvKey *ecdsa.PrivateKey, amount *big.Int, backend *backends.SimulatedBackend) (common.Address, error) {
|
||||
deployTransactor := bind.NewKeyedTransactor(prvKey)
|
||||
deployTransactor.Value = amount
|
||||
addr, _, _, err := contract.DeployOpenRegistrar(deployTransactor, backend)
|
||||
if err != nil {
|
||||
return common.Address{}, err
|
||||
}
|
||||
backend.Commit()
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
func deployResolver(prvKey *ecdsa.PrivateKey, amount *big.Int, backend *backends.SimulatedBackend) (common.Address, error) {
|
||||
deployTransactor := bind.NewKeyedTransactor(prvKey)
|
||||
deployTransactor.Value = amount
|
||||
addr, _, _, err := contract.DeployPersonalResolver(deployTransactor, backend)
|
||||
if err != nil {
|
||||
return common.Address{}, err
|
||||
}
|
||||
backend.Commit()
|
||||
return addr, nil
|
||||
}
|
||||
|
||||
func TestENS(t *testing.T) {
|
||||
contractBackend := backends.NewSimulatedBackend(core.GenesisAccount{addr, big.NewInt(1000000000)})
|
||||
transactOpts := bind.NewKeyedTransactor(key)
|
||||
contractAddr, err := deployRegistrar(key, big.NewInt(0), contractBackend)
|
||||
ens := NewENS(transactOpts, common.Address{}, contractBackend)
|
||||
registrarAddr, err := ens.DeployRegistrar()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
contractBackend.Commit()
|
||||
|
||||
resolverAddr, err := deployResolver(key, big.NewInt(0), contractBackend)
|
||||
ens = NewENS(transactOpts, registrarAddr, contractBackend)
|
||||
resolverAddr, err := ens.DeployResolver()
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
}
|
||||
contractBackend.Commit()
|
||||
|
||||
ens := NewENS(transactOpts, contractAddr, contractBackend)
|
||||
_, err = ens.Register(name, resolverAddr)
|
||||
if err != nil {
|
||||
t.Fatalf("expected no error, got %v", err)
|
||||
|
|
|
|||
Loading…
Reference in a new issue