跳转到主要内容

处理发送/接收加密货币的各种类

项目描述

PyPi Version License Button PyPI - Downloads PyPI - Python Version GitHub last commit

Privex的Python币处理器

工作进展中。币处理器最初是为我们的一项开源项目编写的: privex/cryptotoken-converter

此项目包含独立版本的币处理器,可用于任何Python项目,无论框架如何。币处理器可用于处理接收、发送和发行币。

官方仓库: https://github.com/privex/python-coinhandlers

安装

最低Python版本为 Python 3.6 - 但我们建议使用 Python 3.7 以获得最佳兼容性。

pip3 install privex-coinhandlers

许可协议

+===================================================+
|                 © 2019 Privex Inc.                |
|               https://www.privex.io               |
+===================================================+
|                                                   |
|        Python Cryptocurrency Handlers             |
|        License: X11/MIT                           |
|                                                   |
|        Core Developer(s):                         |
|                                                   |
|          (+)  Chris (@someguy123) [Privex]        |
|                                                   |
+===================================================+

Python Cryptocurrency Handlers - Various classes for handling sending/receiving cryptocurrencies
Copyright (c) 2019    Privex Inc. ( https://www.privex.io )

此项目采用 X11 / MIT 许可协议。有关详细信息,请参阅文件 LICENSE

以下是重要部分

  • 如果您修改/分发/复制此项目的一部分或全部,则必须包含/显示许可协议和版权声明(LICENSE)。
  • 未经我们许可,您不能使用我们的名称来推广/支持您的产品。但是,您可以声明您的产品使用此项目的一部分或全部。

示例用法

关于上下文管理器的说明

自版本1.1.0起 - 一些币处理器已添加上下文管理功能,以及不需要上下文管理的处理器的模拟上下文管理。

建议在使用加载器/管理器时使用上下文管理(with 语句),以确保连接、数据库会话等干净地打开和关闭。

from privex.coin_handlers import MoneroLoader, get_loader, Coin

###
# Context management when using on-the-fly programmatic loading
###

with get_loader('XMR') as m:
    m.load()
    txs = m.list_txs()

###
# Context management when directly using loader/manager classes
###

settings = dict(
    COIND_RPC=dict(XMR=dict(user='monero', password='SomeRPCPassword', port=18100))
)

xmrcoin = Coin(symbol='XMR', symbol_id='XMR')

with MoneroLoader(settings=settings, coins=[xmrcoin]) as m:
    m.load()
    txs = m.list_txs()

直接使用加载器/管理器

from privex.coin_handlers import BitcoinLoader, BitcoinManager, Coin

settings = dict(
    COIND_RPC=dict(
        BTC={}
    )
)

# A Coin object generally only needs ``symbol`` and ``symbol_id``
# Account-based coins such as Steem and EOS will also need ``our_account`` (your sending/receiving account)
# Most coins should work without any ``setting_`` options, unless they depend on authentication with local daemon
# e.g. the Bitcoin handler depends on a user/pass to connect to bitcoind 
c = Coin(
    setting_user='bitcoinrpc', 
    setting_pass='SomeSecureRPCPassword',
    setting_json='{"confirms_needed": 1}',
    symbol='BTC',     # A unique symbol or ID that you use to identify this coin in your app (returned by list_txs())
    symbol_id='BTC'   # The ``symbol_id`` **MUST** match the native coin symbol for the network you use it on.
)


######
#
# Coin Handler Loaders
#
######

# Loaders expect both ``settings`` (a dict containing symbols mapped to dicts containing settings),
# as well as a ``List[Coin]`` containing :class:`privex.coin_handlers.base.objects.Coin` objects they should handle.
# A loader's sole purpose is handling incoming deposit transactions, something which could easily be done through
# a third party service such as a block explorer.
bl = BitcoinLoader(settings=settings, coins=[c])

# list_txs() is a generator which outputs Deposit objects (which can also be converted to dict's)
txs = bl.list_txs()
next(txs)
# <Deposit coin='BTC' amount='0.00177883' address='3xxxxxKYQLH24Aa3xxxxtL3mggxxx'>

x = next(txs)
print(dict(x))
# {
# 'coin': 'BTC', 'txid': '8xxxx5a1xxxxxxxxxxxxxxxxxxxx2dd81xxxxxx5973', 
# 'vout': 0, 'address': '3xxxxxxxxxxxxxxxxxxxxxxxxx', 
# 'amount': Decimal('0.00012434'), 'tx_timestamp': datetime.datetime(2019, 4, 5, 4, 34, 45, tzinfo=<UTC>)
# }

print(x.address)
# 3xxxxxxxxxxxxxxxxxxxxxxxxx

######
#
# Coin Handler Managers
#
######

# Managers expect ``settings`` and ``coin`` (that's ONE coin, not a list).
# A manager handles everything other than deposits, it generally handles functions such as address generation,
# sending/issuing coins, and running health checks on a coin.
bm = BitcoinManager(settings=settings, coin=c)

# health_test() returns True (dependant service e.g. bitcoind is working fine), or False (service is broken)
# This can be used to detect whether there's a problem with the RPC node *before* running any sending code.
bm.health_test()
# True


bm.health()
# (
#   'BitcoinManager', 
#   ('Symbol', 'Status', 'Current Block', 'Version', 'Wallet Balance', 'P2P Connections'), 
#   (
#      'BTC', '<b style="color: green">Online</b>', '586,662 (Headers: 586,662)', 
#      '170100 (/Satoshi:0.17.1/)', '0.1234567', '8'
#   )
# )

bm.get_deposit()

# ('address', '3GnzkkGHZSdBFRYXhhk34xFTuN4cpm1M6W')

程序化加载处理器

使用位于 privex/coin_handlers/__init__.py 中的币处理器辅助函数,可以动态地使用用户指定的处理器名称加载处理器,并仅使用币符号查找正确的处理器。

import privex.coin_handlers as ch
from privex.coin_handlers import Coin

# In the example code, there are three coin handlers entered by default: Steem, Monero and Bitcoin
print(ch.COIN_HANDLERS.keys())
# dict_keys(['Bitcoin', 'Steem', 'Monero'])

# To ensure the coin handlers you want to use, are actually loaded, you should call enable_handler
# It won't hurt if they're already enabled.
ch.enable_handler('Bitcoin', 'Monero')

# Add Dogecoin to the coins handled by the ``Bitcoin`` coin handler.
ch.add_handler_coin('Bitcoin', Coin(symbol='DOGE', symbol_id='DOGE'))

# Add connection settings for DOGE's handler instances
ch.configure_coin('DOGE', user='dogerpc', password='SomeSecret', port=22555)

# Some handlers such as Monero come with their native coin pre-added. You can check if a handler has
# a certain coin with `handler_has_coin`
ch.handler_has_coin('Monero', 'XMR')
# True

# Add connection and wallet configuration settings for Monero (XMR)
# wallet = Monero wallet filename, walletpass = Wallet encryption password, account = Wallet account
ch.configure_coin(
    'XMR',
    user='monero', password='SomeRPCPassword', port=18100, confirms_needed=0,
    wallet='mnrwallet', walletpass='SomeWalletPassword', account='mywalletaccount'
)

# Force reload the handlers
ch.reload_handlers()

# As we can see, if we pass the symbol DOGE to get_loader we get a BitcoinLoader object  
doge = ch.get_loader('DOGE')
# <privex.coin_handlers.Bitcoin.BitcoinLoader.BitcoinLoader object at 0x10ac0f160>

# We cam also see in the loader object that our coin options were injected into the Loader settings, with the password
# 'SecurePass' and username 'dogerpc'.
print(doge.settings['DOGE'])
# {
#   'host': '127.0.0.1', 'port': 8332, 'user': 'dogerpc', 'password': 'SecurePass', 
#   'confirms_needed': 0, 'use_trusted': True, 'string_amt': True
# }

贡献

我们非常愿意接受拉取请求,并处理向我们报告的任何问题。

以下是一些重要信息

报告问题

  • 对于错误报告,您应包括以下信息:
    • 测试的 privex-coinhelpersrequests 版本 - 使用 pip3 freeze
      • 如果不是通过 PyPi 发布版安装,请提供出现问题的 git 修订号 - git log -n1
    • 您的 python3 版本 - python3 -V
    • 您的操作系统和操作系统版本(例如:Ubuntu 18.04,Debian 7)
  • 对于功能请求/更改
    • 请避免提出需要新依赖的建议。此工具旨在轻量级,不包含外部依赖。
    • 清楚地说明您希望添加的功能/更改
    • 解释为什么该功能/更改对我们或工具的其他用户有用
    • 请注意,对于添加复杂或我们认为对工具的使用不必要的功能/更改可能不会被添加(但我们可以接受 PR)

拉取请求

  • 我们乐意接受仅添加代码注释或 README 变更的 PR
  • 在贡献代码时使用 4 个空格,而不是制表符
  • 您可以使用 Python 3.6+ 的功能(我们项目使用 Python 3.7+)
    • 需要尚未发布到 Ubuntu Server LTS 最新稳定版本的 Python 版本的功能将不被接受(目前为 Ubuntu 18.04 Bionic)
  • 在标题和描述中清楚地说明您拉取请求的目的
    • 您做出了哪些更改?
    • 为什么您要做出这些更改?
  • 请确保代码贡献有适当的注释 - 我们不会接受未注释的、高度简洁的单行代码更改

贡献内容的法律免责声明

没有人愿意阅读一个充满法律文本的长文档,因此我们在这里总结了重要内容。

如果您将您创建/拥有的内容贡献给 Privex 创建/拥有的项目,例如代码或文档,那么您可能自动授予我们不受限制的使用权,无论我们的项目适用的开源许可证是什么。

如果您不想授予我们您内容的无限使用权,请确保将您的内容放在一个单独的文件中,确保在文件开头(例如代码注释)或其包含文件夹内(例如命名为 LICENSE 的文件)清楚地显示您内容的许可证。

请在您的拉取请求或问题中告知我们您已包含受单独许可证授权的文件,以便我们可以确保不存在可能阻止我们接受您的贡献的许可证冲突。

如果您想阅读完整的法律文本,它应包含为 privex_contribution_agreement.txt

(替代)从 Git 手动安装

如果您不想使用 PyPi(例如,对于尚未在 PyPi 上发布的开发版本),您可以直接从我们的 Git 仓库安装项目。

除非您有特定的理由手动安装它,否则您应像上面所示使用 pip3 正常安装。

选项 1 - 使用 pip 从 Github 直接安装

pip3 install git+https://github.com/Privex/python-coinhandlers

选项 2 - 克隆并手动安装

# Clone the repository from Github
git clone https://github.com/Privex/python-coinhandlers
cd python-coinhandlers

# RECOMMENDED MANUAL INSTALL METHOD
# Use pip to install the source code
pip3 install .

# ALTERNATIVE INSTALL METHOD
# If you don't have pip, or have issues with installing using it, then you can use setuptools instead.
python3 setup.py install

感谢您阅读!

如果您觉得这个项目对您有帮助,考虑从 Privex 购买 VPS 或专用服务器吧 - 价格低至每月 8 美元起(我们接受加密货币!)

项目详情


下载文件

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

源分布

privex_coinhandlers-1.4.1.tar.gz (52.6 kB 查看哈希值)

上传时间: 源码

构建版本

privex_coinhandlers-1.4.1-py3-none-any.whl (65.9 kB 查看哈希值)

上传时间: Python 3

由以下支持