web3.eth.ens

12 篇文章 0 订阅
7 篇文章 0 订阅

web3.eth.ens

这些web3.eth.ens功能可让您与ENS进行交互。

注册表

web3.eth.ens.registry;
返回特定于网络的ENS注册表。

返回

Registry - 当前的ENS注册表。

示例

web3.eth.ens.registry;

{
ens: ENS,
contract: Contract,
owner: Function(name),
resolve: Function(name)
}

解析器

web3.eth.ens.resolver(name);
将解析器合约返回到以太坊地址。

返回

Reslver - 此名称的ENS解析器。

示例

web3.eth.ens.resolver(‘ethereum.eth’).then(function (contract) {
console.log(contract);
});

Contract

getAddress

web3.eth.ens.getAddress(ENSName);
将ENS名称解析为以太坊地址。

参数

ENSName- String:要解析的ENS名称。

返回

String - 给定名称的以太坊地址。

示例

web3.eth.ens.getAddress(‘ethereum.eth’).then(function (address) {
console.log(address);
})

0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359

setAddress

web3.eth.ens.setAddress(ENSName, address, options);
在其解析器中设置ENS名称的地址。

参数

ENSName- String:ENS名称。
address- String:要设置的地址。
options- Object:用于发送的选项。
from- String:应从中发送事务的地址。
gasPrice- String(可选):用于此交易的wei的汽油价格。
gas- Number(可选):为此交易提供的最大气体(气体限制)。
发出一个AddrChanged事件。

示例

 web3.eth.ens.setAddress(
        'ethereum.eth',
        '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
        {
            from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
        }
    ).then(function (result) {
             console.log(result.events);
    });
    > AddrChanged(...)

//或者使用事件发射器

web3.eth.ens.setAddress(
    'ethereum.eth',
    '0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
)
.on('transactionHash', function(hash){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('error', console.error);

//或者听一下解析器上的AddrChanged事件

web3.eth.ens.resolver('ethereum.eth').then(function (resolver) {
    resolver.events.AddrChanged({fromBlock: 0}, function(error, event) {
        console.log(event);
    })
    .on('data', function(event){
        console.log(event);
    })
    .on('changed', function(event){
        //从本地数据库中删除事件
    })
    .on('error', console.error);
});

getPubkey

web3.eth.ens.getPubkey(ENSName);
返回公钥的曲线点的X和Y坐标。

参数

ENSName- String:ENS名称。

返回

Object<String, String> - X和Y坐标。

示例

web3.eth.ens.getPubkey('ethereum.eth').then(function (result) {
    console.log(result)
});
> {
    "0": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "1": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "x": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "y": "0x0000000000000000000000000000000000000000000000000000000000000000"
}

setPubkey

web3.eth.ens.setPubkey(ENSName, x, y, options);
设置与ENS节点关联的SECP256k1公钥

参数

ENSName- String:ENS名称。
x- String:公钥的X坐标。
y- String:公钥的Y坐标。
options- Object:用于发送的选项。

from- String:应从中发送事务的地址。
gasPrice- String(可选):用于此交易的wei的汽油价格。
gas- Number(可选):为此交易提供的最大气体(气体限制)。
发出一个PubkeyChanged事件。

示例

web3.eth.ens.setPubkey(
    'ethereum.eth',
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
).then(function (result) {
    console.log(result.events);
});
> PubkeyChanged(...)

//或者使用事件发射器
web3.eth.ens.setPubkey(
    'ethereum.eth',
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
)
.on('transactionHash', function(hash){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('error', console.error);

//或者听一下解析器上的PubkeyChanged事件
web3.eth.ens.resolver('ethereum.eth').then(function (resolver) {
    resolver.events.PubkeyChanged({fromBlock: 0}, function(error, event) {
        console.log(event);
    })
    .on('data', function(event){
        console.log(event);
    })
    .on('changed', function(event){
        // remove event from local database
    })
    .on('error', console.error);
});

getContent

web3.eth.ens.getContent(ENSName);
返回与ENS节点关联的内容哈希。

参数

  1. ENSName- String:ENS名称。

返回

String - 与ENS节点关联的内容哈希。

示例

web3.eth.ens.getContent(‘ethereum.eth’).then(function (result) {
console.log(result);
});

“0x0000000000000000000000000000000000000000000000000000000000000000”

setContent

web3.eth.ens.setContent(ENSName, hash, options);
设置与ENS节点关联的内容哈希。

参数

  1. ENSName- String:ENS名称。
  2. hash- String:要设置的内容哈希。
  3. options- Object:用于发送的选项。
    • from- String:应从中发送事务的地址。
    • gasPrice- String(可选):用于此交易的wei的汽油价格。
    • gas- Number(可选):为此交易提供的最大气体(气体限制)。
    发出一个ContentChanged事件。

示例

web3.eth.ens.setContent(
    'ethereum.eth',
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
).then(function (result) {
         console.log(result.events);
 });
> ContentChanged(...)

//或者使用事件发射器

web3.eth.ens.setContent(
    'ethereum.eth',
    '0x0000000000000000000000000000000000000000000000000000000000000000',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
)
.on('transactionHash', function(hash){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('error', console.error);

//或者听一下解析器上的ContentChanged事件
web3.eth.ens.resolver('ethereum.eth').then(function (resolver) {
    resolver.events.ContentChanged({fromBlock: 0}, function(error, event) {
        console.log(event);
    })
    .on('data', function(event){
        console.log(event);
    })
    .on('changed', function(event){
        //从本地数据库中删除事件
    })
    .on('error', console.error);
});

getMultihash

web3.eth.ens.getMultihash(ENSName);
返回与ENS节点关联的multihash。

参数

  1. ENSName- String:ENS名称。

返回

String - 相关的multihash。

示例

web3.eth.ens.getMultihash(‘ethereum.eth’).then(function (result) {
console.log(result);
});

‘QmXpSwxdmgWaYrgMUzuDWCnjsZo5RxphE3oW7VhTMSCoKK’

setMultihash

web3.eth.ens.setMultihash(ENSName, hash, options);
设置与ENS节点关联的multihash。

参数

  1. ENSName- String:ENS名称。
  2. hash- String:要设置的multihash。
  3. options- Object:用于发送的选项。
    • from- String:应从中发送事务的地址。
    • gasPrice- String(可选):用于此交易的wei的汽油价格。
    • gas- Number(可选):为此交易提供的最大气体(气体限制)。
    发出一个MultihashChangedevent。

示例

web3.eth.ens.setMultihash(
    'ethereum.eth',
    'QmXpSwxdmgWaYrgMUzuDWCnjsZo5RxphE3oW7VhTMSCoKK',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
).then(function (result) {
    console.log(result.events);
});
> MultihashChanged(...)

//或者使用事件发射器
web3.eth.ens.setMultihash(
    'ethereum.eth',
    'QmXpSwxdmgWaYrgMUzuDWCnjsZo5RxphE3oW7VhTMSCoKK',
    {
        from: '0x9CC9a2c777605Af16872E0997b3Aeb91d96D5D8c'
    }
)
.on('transactionHash', function(hash){
    ...
})
.on('confirmation', function(confirmationNumber, receipt){
    ...
})
.on('receipt', function(receipt){
    ...
})
.on('error', console.error);

ENS事件

ENS API提供了收听所有ENS相关事件的可能性。
已知的解析器事件

  1. AddrChanged(节点bytes32,一个地址)
  2. ContentChanged(node bytes32,hash bytes32)4。NameChanged(node bytes32,name string)5。ABIChanged(node bytes32,contentType uint256)6。PubkeyChanged(node bytes32,x bytes32,y bytes32)

示例

web3.eth.ens.resolver('ethereum.eth').then(function (resolver) {
    resolver.events.AddrChanged({fromBlock: 0}, function(error, event) {
        console.log(event);
    })
    .on('data', function(event){
        console.log(event);
    })
    .on('changed', function(event){
        //从本地数据库中删除事件
    })
    .on('error', console.error);
});
> {
    returnValues: {
        node: '0x123456789...',
        a: '0x123456789...',
    },
    raw: {
        data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
        topics: [
            '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
            '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385'
        ]
    },
    event: 'AddrChanged',
    signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    logIndex: 0,
    transactionIndex: 0,
    transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
    blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    blockNumber: 1234,
    address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
}

已知的注册表事件

  1. 传输(节点bytes32,所有者地址)
  2. NewOwner(node bytes32,label bytes32,owner address)4。NewResolver(node bytes32,resolver address)5。NewTTL(node bytes32,ttl uint64)

示例

web3.eth.ens.resistry.then(function (registry) {
    registry.events.Transfer({fromBlock: 0}, , function(error, event) {
          console.log(event);
      })
      .on('data', function(event){
          console.log(event);
      })
      .on('changed', function(event){
           //从本地数据库中删除事件
      })
      .on('error', console.error);
});
> {
    returnValues: {
        node: '0x123456789...',
        owner: '0x123456789...',
    },
    raw: {
        data: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
        topics: [
            '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
            '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385'
        ]
    },
    event: 'Transfer',
    signature: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    logIndex: 0,
    transactionIndex: 0,
    transactionHash: '0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385',
    blockHash: '0xfd43ade1c09fade1c0d57a7af66ab4ead7c2c2eb7b11a91ffdd57a7af66ab4ead7',
    blockNumber: 1234,
    address: '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值