跳转到主要内容

以太坊名称服务,Python轻松实现

项目描述

Join the chat at https://gitter.im/ens-py/Lobby

使用此Python库访问以太坊名称服务。注意:这是一个正在进行中的项目

使用此库并不是跳过学习如何使用ENS的方式。如果您正在注册名称,一个小错误可能会导致您损失所有的存款。请先了解ENS。您的资金由您负责。

测试版警告

这是一个为开发者提供的预览,并邀请贡献。请在此警告移除之前不要在生产环境中使用,特别是在涉及资金风险的情况下。资金风险示例包括:向解析的地址发送以太币/代币和参与名称拍卖。

如果您提供一个类型为bytes的域名,它将被假设为UTF-8编码,例如以太坊合约中的编码。

设置

pip install ens

有任何问题?请参阅设置详情

用法

所有示例均使用Python 3

名称信息

从名称获取地址

默认为{name}.eth

from ens import ens


# look up the hex representation of the address for a name

eth_address = ens.address('jasoncarver.eth')

assert eth_address == '0x5b2063246f2191f18f2675cedb8b28102e957458'


# ens.py will assume you want a .eth name if you don't specify a full name

assert ens.address('jasoncarver') == eth_address

从地址获取名称

domain = ens.name('0x5b2063246f2191f18f2675cedb8b28102e957458')


# name() also accepts the bytes version of the address

assert ens.name(b'[ c$o!\x91\xf1\x8f&u\xce\xdb\x8b(\x10.\x95tX') == domain


# confirm that the name resolves back to the address that you looked up:

assert ens.address(domain) == '0x5b2063246f2191f18f2675cedb8b28102e957458'

获取名称的所有者

eth_address = ens.owner('exchange.eth')

设置您的名称

将您的名称指向您的地址

您想设置您的名称,以便ens.address()显示它指向的地址吗?

ens.setup_address('jasoncarver.eth', '0x5b2063246f2191f18f2675cedb8b28102e957458')

您必须是域(或其父域)的所有者。

在通常情况下,您想将名称指向拥有地址时,可以跳过地址

ens.setup_address('jasoncarver.eth')

您可以任意深度地声明子域。 气体成本会随着子域数量的增加而增加!

ens.setup_address('supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth')

等待交易被挖矿,然后

assert ens.address('supreme.executive.power.derives.from.a.mandate.from.the.masses.jasoncarver.eth') == \
    '0x5b2063246f2191f18f2675cedb8b28102e957458'

将您的地址指向您的名称

您想设置您的地址,以便ens.name()显示指向它的名称吗?

这就像来电显示。它使您和其他人能够取一个账户并确定指向它的名称。有时这被称为“反向”解析。

ens.setup_name('jasoncarver.eth', '0x5b2063246f2191f18f2675cedb8b28102e957458')

如果您不提供地址,setup_name将假设您想要ens.address(name)返回的地址。

ens.setup_name('jasoncarver.eth')

如果名称尚未指向地址,ens.setup_name将为您调用ens.setup_address

等待交易被挖矿,然后

assert ens.name('0x5b2063246f2191f18f2675cedb8b28102e957458') == 'jasoncarver.eth'

.eth结尾名称的拍卖

获取拍卖状态

以域名‘payment.eth’为例

from ens.registrar import Status


status = ens.registrar.status('payment')


# if you forget to strip out .eth, ens.py will do it for you

assert ens.registrar.status('payment.eth') == status


# these are the possible statuses

assert status in (
  Status.Open,
  Status.Auctioning,
  Status.Owned,
  Status.Forbidden,
  Status.Revealing,
  Status.NotYetAvailable
  )


# if you get the integer status from another source, you can compare it directly

assert Status.Owned == 2

开始拍卖

# start one auction (which tips people off that you're interested)

ens.registrar.start('you_saw_him_repressin_me_didnt_ya')


# start many auctions (which provides a bit of cover)

ens.registrar.start(['exchange', 'tickets', 'payment', 'trading', 'registry'])

对拍卖出价

以5211 ETH的价格出价“trading.eth”,并秘密“我保证我不会忘记我的秘密”

from web3utils import web3

ens.registrar.bid(
      'trading',
      web3.toWei('5211', 'ether'),
      "I promise I will not forget my secret",
      transact={'from': web3.eth.accounts[0]}
      )

(如果您想“隐藏”您的出价,请在transact字典中设置更高的值)

揭示您的出价

您必须始终揭示您的出价,无论您是赢是输。否则您将损失全部押金。

在‘registry.eth’上以0.01 ETH揭示您的出价,并秘密“说实话,虽然:失去你的秘密意味着失去以太币”

ens.registrar.reveal(
      'registry',
      web3.toWei('0.01', 'ether'),
      "For real, though: losing your secret means losing ether",
      transact={'from': web3.eth.accounts[0]}
      )

领取您赢得的名称

也称为“最终化”拍卖,使您在ENS中成为所有者。

ens.registrar.finalize('gambling')

获取拍卖的详细信息

找出拍卖契据的所有者 - 查看关于拥有名称和契据之间差异的文档

deed = ens.registrar.deed('ethfinex')

assert deed.owner() == '0x9a02ed4ca9ad55b75ff9a05debb36d5eb382e184'

拍卖何时完成?(时区感知的日期时间对象)

close_datetime = ens.registrar.close_at('ethfinex')

assert str(close_datetime) == '2017-06-05 08:10:03+00:00'

存入多少押金?

from decimal import Decimal

deposit = ens.registrar.deposit('ethfinex')

assert web3.fromWei(deposit, 'ether') == Decimal('0.01')

最高出价是多少?

top_bid = ens.registrar.top_bid('ethfinex')

assert web3.fromWei(top_bid, 'ether') == Decimal('201709.02')

设置细节

如果Python 2是您的默认设置,或者您不确定

在您的shell中

if pip --version | grep "python 2"; then
  python3 -m venv ~/.py3venv
  source ~/.py3venv/bin/activate
fi

现在,使用Python 3

在您的shell中: pip install ens

ens.py需要一个最新的以太坊区块链,最好是本地的。如果您的设置不起作用,请尝试运行geth --fast直到完全同步。我强烈建议使用默认的IPC通信方法,以获得速度和安全。

“未找到匹配的发行版”

如果您看到类似的内容

Collecting ens
  Could not find a version that satisfies the requirement ens (from versions: )
No matching distribution found for ens

那么请重试第一个设置部分,以确保您在Python 3中

可选,自定义web3提供者

在Python中

from ens import ENS
from web3 import IPCProvider

ens = ENS(IPCProvider('/your/custom/ipc/path'))

开发者设置

git clone git@github.com:carver/ens.py.git
cd ens.py/

python3 -m venv venv
. venv/bin/activate

pip install -e .
pip install -r requirements-dev.txt

测试设置

重新运行flake文件更改

$ when-changed -s -1 -r ens/ tests/ -c "clear; echo; echo \"running flake - $(date)\"; warn()
{
notify-send -t 5000 'Flake8 failure ⚠⚠⚠⚠⚠' 'flake8 on ens.py failed'
}
if ! git diff | flake8 --diff | grep "\.py"; then if ! flake8 ens/ tests/; then warn; fi else warn; fi; echo done"

为什么ens.py需要python 3?

简短版本

结果证明,strbytes之间的区别很重要。如果您想为未来(以太坊)编写代码,请不要使用来自过去的语言。

长版本

与EVM交互需要明确你所使用的位。例如,sha3散列期望接收一系列字节进行处理。计算字符串的sha3散列是(或应该是)类型错误;散列算法不知道如何处理一系列字符,即Unicode码点。作为调用者,你需要知道你正在计算哪个东西的散列:1.一系列字节:b'[ c$o!\x91\xf1\x8f&u\xce\xdb\x8b(\x10.\x95tX' 2.由字符串表示的十六进制格式的字节:'0x5b2063246f2191f18f2675cedb8b28102e957458' 3.使用utf-8编码字符串生成的字节:哎呀,#1的字节无法使用utf-8读取! 4.使用utf-16编码字符串生成的字节:'⁛④Ⅿ\uf191⚏칵诛ဨ键塴'

Python 3不允许你忽略很多这些细节。这是好事,因为处理EVM的精确性至关重要。以太币就在危险之中。

如果你对此感到抗拒——我理解,我也经历过。这对大多数人来说并不直观。但如果你打算在以太坊之上开发,了解编码是非常值得的。学习有关编码的知识!你的ETH取决于它!

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多有关安装包的信息。

源代码分发

此版本没有提供源代码分发文件。请参阅生成分发存档的教程。

构建分发

ens-0.6.1-py3-none-any.whl (18.2 kB 查看散列)

上传时间 Python 3

支持者

AWSAWS云计算和安全赞助商DatadogDatadog监控FastlyFastlyCDNGoogleGoogle下载分析MicrosoftMicrosoftPSF赞助商PingdomPingdom监控SentrySentry错误记录StatusPageStatusPage状态页面