博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
小额免密_如何在您的应用中进行小额付款
阅读量:2514 次
发布时间:2019-05-11

本文共 16278 字,大约阅读时间需要 54 分钟。

小额免密

This week I spent coding my very first public pet-app based on Telegram chat bot which acts as a Bitcoin wallet and allows to send and receive tips between Telegram users and other so-called “Lightning Apps”. I assume that you are familiar with Bitcoin & Telegram in general, i’ll try to post short and without deep jump into details. More and Telegram is simply an instant messenger that allows you to create your custom apps (chat-bots) using their platform.

本周,我花了我的第一笔基于Telegram聊天机器人的公共宠物应用程序的编码,该机器人充当了比特币钱包,并允许在Telegram用户和其他所谓的“ Lightning Apps”之间发送和接收提示。 我认为您通常对比特币和电报很熟悉,我会尽量简短而又不深入地介绍细节。 更多 ,Telegram只是一个即时通讯工具,使您可以使用其平台创建自定义应用程序(聊天机器人)。

这种应用程序的重点是什么? (What are the key points of such app?)

  • Allows to rate other users ideas and answers with real value instead of

    允许给其他用户的想法和答案打分,而不是实际价值

    ‘virtual likes’. This brings online conversation to completely new level

    “虚拟喜欢”。 这使在线对话达到了全新的水平

  • Real example of working micro-payment app which can act with other entities

    可以与其他实体合作的小额支付应用程序的实际示例

    over internet using open protocol

    通过互联网使用开放协议

  • All the modules are open-source projects and can be easy re-used and adjusted

    所有模块都是开源项目,可以轻松重用和调整

    for your own project. App does not relay on third-party commercial services.

    为您自己的项目。 应用程序不会中继第三方商业服务。

    Even it falls under e-commerce field, which is currently almost closed, the app

    即使它属于电子商务领域(目前几乎已关闭),该应用

    is based on open solutions.

    基于开放式解决方案。

用例是什么? (What are the use-cases?)

something like this…

像这样的东西

image
or like that..
或那样
image

What do numbers mean? These are bitcoin units — satoshis. As for mid-2019 1000 satoshis has value of $0.06. You can spend it online or exchange to your local currency using many available services. So this is not just another "number in a database" or a "coupons you can spend in our partner store", but a real internet money.

数字是什么意思? 这些是比特币单位-satoshis 。 至于2019年中期,1000个聪的价值为0.06美元。 您可以在线使用它,也可以使用许多可用服务将其兑换成当地货币。 因此,这不仅是另一个“数据库中的号码”“您可以在我们的合作伙伴商店中使用的优惠券” ,而且是一笔真正的互联网资金

You may ask what to do, if you don’t have any bitcoin to use in the app. You simply can receive a tip from existing app user or buy small amount of bitcoin on various exchanges or bitcoin wallets for mobile phone or pc.

如果您在应用程序中没有要使用的比特币,您可能会问该怎么办。 您只需从现有应用程序用户处获得小费,或者在各种交易所或手机或PC上的比特币钱包中购买少量的比特币。

image
image
image

When you got bitcoin, you can start and make a /deposit.

收到比特币后,您可以启动并进行一个/ deposit。

You'll see an invoice, you can select preferred payment method and send money just scanning the QR code or with button press ("open in wallet") on invoice.

您会看到一张发票​​,可以选择首选的付款方式,只需扫描QR码或按发票上的按钮( “在钱包中打开” )即可汇款。

image
image
image

Confirm payment in your mobile wallet, and you will see a confirmation screen almost instantly in Telegram.

在您的手机钱包中确认付款,您几乎会在Telegram中看到确认屏幕。

So far we touched two separate parts of the app: telegram bot back-end and payment processing system (actually more like it's front-end).

到目前为止,我们接触了该应用程序的两个独立部分:电报bot后端和付款处理系统(实际上更像是前端)。

To create this Telegram bot i’ve used . And of course, the .

为了创建这个Telegram机器人,我使用了 。 当然,该 。

As a payment processing system to generate invoices, check status, send notification and take care of UX is used which .

作为用于生成发票,检查状态,发送通知和处理UX的支付处理系统,使用了 , 。

If you are looking on how to process payment events directly, you can look at for on-chain payments and check out solutions for off-chain (Lightning) payments like or .

如果您正在查看如何直接处理付款事件,则可以查看上的链上付款,并查看链下(Lightning)付款(如或 )的解决方案。

您说“没有第三方”和“没有云服务”!? (You said ‘no 3rd party’ and ‘no cloud services’!?)

Yes, that’s true. In my app i am using free shared hosting of BTCPayServer instance — , but it is possible to spin up even on your own dedicated server. Just remember that you need then about ~500GB of space to store Bitcoin and Litecoin blockchain. BTCPayServer does not hold any private keys or sensitive information that can allow hackers to control funds on you wallet. For on-chain (standart) payments you need to provide xpub key from you wallet to BTCPayServer so it will be able to generate public addresses. In this way, BTCPay architecture makes possible to process payments in trustless manner even on shared environment. It is very simple to configure if you use proven wallets such as .

是的,这是真的。 在我的应用程序中,我正在使用BTCPayServer实例的免费共享托管— ,但是甚至可以在自己的专用服务器上启动。 请记住,您当时需要大约500GB的空间来存储比特币和Litecoin区块链。 BTCPayServer不持有任何可能使黑客控制您钱包中资金的私钥或敏感信息。 对于链上(标准)付款,您需要提供从钱包到BTCPayServer的xpub密钥,以便它能够生成公共地址。 这样,BTCPay体系结构甚至可以在共享环境上以不信任的方式处理付款。 如果使用经过验证的电子钱包(例如 ,则配置非常简单。

什么是闪电网络? (What is Lightning Network?)

It is something like an add-on to Bitcoin which adds additional abstraction layer and allows to make instant and almost free bitcoin transactions. All this stuff is possible thanks to cryptographic multi-signature magic, which is often called smart-contract. There are multiple implementations of LN protocol. I am using C implementation.

这就像是比特币的附件,它增加了额外的抽象层,并允许进行即时和几乎免费的比特币交易。 所有这些东西都可以归功于密码多重签名魔术,这通常被称为智能合约 。 LN协议有多种实现。 我正在使用 C实现。

The major difference between on-chain and off-chain (Lightning) payments are in how transactions are stored. All on-chain transactions are broadcasted to the whole network so each running bitcoin node will record this transaction into blockchain, if block containing this transaction will be . Hey, , am i right? ;) That's why we call such transactions on-chain. Such transactions will be stored in blockchain till the end of its existence. It is possible publicly to track, analyse and monitor any such transaction over the network.

链上和链外(闪电)付款之间的主要区别在于交易的存储方式。 所有链上交易都会广播到整个网络,因此,如果将包含该交易的区块,则每个正在运行的比特币节点会将该交易记录到区块链中。 嘿, ,对吗? ;)这就是为什么我们称此类交易为链式交易的原因。 此类交易将存储在区块链中,直至其存亡。 可以通过网络公开跟踪,分析和监视任何此类交易。

In case of off-chain (Lightning) transaction things happens in other way. Each lightning node has (may be many) on-chain address, from which one all starts. When funds are received on such address, using cryptography techniques node may open so-called lightning channel to another node, what means that these two nodes made agreement about funds availability between them. Later, all the transactions that are made trough channel (own transactions, or it may be routed transaction from another node) are stored only on those two nodes (and intermediate node if there was such). The only transactions which are stored in networks blockchain are channel opening and closing transactions. This is why Lightning transactions we call off-chain. Mostly they exist only in node database-file.

下交易(闪电)的情况下,事情会以其他方式发生。 每个闪电节点都有(可能很多)链上地址,所有地址都从此开始。 当在这样的地址上收到资金时,使用加密技术的节点可以打开通往另一个节点的所谓的闪电通道 ,这意味着这两个节点就它们之间的资金可用性达成了协议。 后来,所有通过通道进行的事务(自己的事务,或者可能是来自另一个节点的路由事务)都仅存储在这两个节点上(如果存在,则存储在中间节点上)。 存储在网络区块链中的唯一交易是渠道开放和关闭交易。 这就是为什么我们将闪电交易称为脱链交易的原因。 通常,它们仅存在于节点数据库文件中。

整个图都可视化了 (The whole thing is visualized on diagram)

image

Short story:

短篇故事:

1) User sends message and requests deposit to bot on Telegram

1)用户发送消息并要求在电报上向机器人存入资金

2) Telegram triggers bot's python app

2)Telegram触发bot的python应用

3) python app sends request to BTCPayServer

3) python应用程序将请求发送到BTCPayServer

4) BTCPayServer generates incoming addresses for BTC and LTC, as well contacting lightning-charge, which one is getting data from c-lightning daemon. In a result we get well-handled HTML frame for payment processing

4)BTCPayServer生成BTC和LTC的传入地址,并与lightning-charge联系 ,后者从c-lightning守护程序获取数据。 结果,我们得到了处理良好HTML框架用于付款处理

5) User pays invoice and will see confirmation

5)用户支付发票并看到确认

6) BTCPayServer sends invoice payment notification (IPN) to specified callback url, in our case it will go to callbacks.py (another python app to listen for callbacks)

6) BTCPayServer发票付款通知 (IPN)发送到指定的回调URL,在本例中,它将转到callbacks.py (另一个用于监听回调的python应用)

7) According to complete payment detail user balance is changed and user gets notification

7)根据完整的付款明细,更改用户余额并获得通知

… if user decides to withdraw funds (tips he received) …

……如果用户决定提取资金(他收到的小费)……

8) Bot's python app listens to text or image of QR, when it gets invoice data (long string of characters) then it sends request to pay that invoice to another python app paylightning.py*, which initializes pay-outs using c-lightning RPC.

8) Bot的python应用程序侦听QR的文本或图像,当它获取发票数据(长字符串)时,它向另一个python应用程序paylightning.py *发送付款请求,该应用程序使用c-lightning初始化付款RPC。

image

*To be fair, I need to mention that my pay-wrapper paylightning.py is incomplete and may return false-negative in some specific cases. You should use in case of production-like use.

*为了公平起见,我需要提及的是我的工资包装器paylightning.py不完整,在某些特定情况下可能返回假负数。 如果生产用途,则应使用 。

Users are able to send tips to each other on group chats and send/receive value without any limits. But that’s not all. Thanks to Lightning Network protocol, which is in some kind of unified and simplified payment network you are able to send same balance coins to completely different apps. There is another bitcoin tip-bot on telegram — @lntxbot. User can request a deposit to this bot

用户可以在群聊中相互发送提示,并可以无限制地发送/接收 。 但这还不是全部。 借助Lightning Network协议,该协议在某种统一和简化的支付网络中可以将相同的余额硬币发送到完全不同的应用程序。 电报上还有另一个比特币小费机器人-@lntxbot。 用户可以向该机器人请求存款

image
image

Simply forward data from @lntxbot to @atomic_tipbot and voul’a!

只需将数据从@lntxbot转发到@atomic_tipbot即可!

This is not anymore ‘number swap in database’ but payment between two separate, non-related ‘databases’. Bot already has few 'LApps' integrated: and .

这不再是“数据库中的号码交换”,而是两个单独的,不相关的“数据库”之间的付款。 Bot已经集成了很少的“ LApps”: 和 。

You may ask, what exactly means LApp integration?

您可能会问, LApp集成到底意味着什么?

Each Lightning Network node acts as a bitcoin wallet. It has both on-chain and off-chain balance. The on-chain balance usually is used to open channels and store funds from closed channels, in short — nodes maintenance tasks. The off-chain balance is what exactly is used to exchange funds between nodes. The core functions of each node is ability to send or receive funds, so node as a software provides pay and invoice methods. I'll show in short how it happens:

每个闪电网络节点都充当比特币钱包。 它具有链上和链下的平衡。 简而言之,链上余额通常用于打开渠道并从关闭的渠道中存储资金,简称为节点维护任务。 链外余额恰好用于在节点之间交换资金。 每个节点的核心功能是发送或接收资金的能力,因此节点作为一种软件提供了付款发票方式。 我将简要说明它是如何发生的:

user@ln-node:/ % lightning-cli invoice 1000000000 internal_description external_description{   "payment_hash" : "64c0c8f5f8f708b08487ad1376f3d256f92ccb9606987ba395c2b4193efde5a0",   "expires_at" : 1558709834,   "bolt11" : "lnbc10m1pwdaj72pp5vnqv3a0c7uytppy845fhdu7j2mujejukq6v8hgu4c26pj0hauksqdpqv4u8getjdeskchmyv4ekxunfwp6xjmmwxqyjw5qcqp2rzjqw3qcrp2u3ggke56wrjlstcg76drmfw680cvjum88sl7ja7mpas7xzxz8sqqfecqqyqqqqlgqqqqqqgqjqn8e3ml733dkms2txxldnuwsllwhhkldss73268hdka3e7f083vjhjqurwqrndqd2rcd85pw7vkywnr4dq7yfd59r0g2mw4wzztwr6dcprxzej6"}

The invoice is generated in this case, which is unified format for payment requests in Bitcoin (as well as Litecoin) network. This string includes data about invoice recipient, amount, timestamp, expiration etc. To decode this string c-lightning provides decodepay method:

在这种情况下,将生成发票,这是针对比特币(以及Litecoin)网络中的付款请求的统一格式。 此字符串包括有关发票接收者,金额,时间戳记,到期时间等的数据。要对此字符串进行解码,c-lightning提供了definepay方法:

user@ln-node:/ % lightning-cli decodepay lnbc10m1pwdaj72pp5vnqv3a0c7uytppy845fhdu7j2mujejukq6v8hgu4c26pj0hauksqdpqv4u8getjdeskchmyv4ekxunfwp6xjmmwxqyjw5qcqp2rzjqw3qcrp2u3ggke56wrjlstcg76drmfw680cvjum88sl7ja7mpas7xzxz8sqqfecqqyqqqqlgqqqqqqgqjqn8e3ml733dkms2txxldnuwsllwhhkldss73268hdka3e7f083vjhjqurwqrndqd2rcd85pw7vkywnr4dq7yfd59r0g2mw4wzztwr6dcprxzej6{   "currency" : "bc",   "created_at" : 1558105034,   "expiry" : 604800,   "payee" : "025a14b8ed40583d67aec92da19453e0b2d1fbbf75f96f85d3dd0ff61a51ee0490",   "msatoshi" : 1000000000,   "amount_msat" : "1000000000msat",   "description" : "external_description",   "min_final_cltv_expiry" : 10,   "routes" : [      [         {            "pubkey" : "03a20c0c2ae4508b669a70e5f82f08f69a3da5da3bf0c973673c3fe977db0f61e3",            "short_channel_id" : "574012x1255x1",            "fee_base_msat" : 1000,            "fee_proportional_millionths" : 1,            "cltv_expiry_delta" : 144         }      ]   ],   "payment_hash" : "64c0c8f5f8f708b08487ad1376f3d256f92ccb9606987ba395c2b4193efde5a0",   "signature" : "304502210099f31dffd18b6db8296637db3e3a1ffbaf7b7db087a2ad1eedb7639f25e78b25022079038370073681aa1e1a7a05de6588e98ead078896d0a37a15b755c212dc3d37"}

In case we need to pay some invoice, we should use pay method of c-lightning:

如果需要支付一些发票,则应使用c-lightning的支付方式:

user@ln-node:/ % lightning-cli pay lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j{   "id" : 163,   "payment_hash" : "c3e644a005f56e33df89facbce2c9dfcabbef5d105285a98272cacc77ab2de63",   "destination" : "03021c5f5f57322740e4ee6936452add19dc7ea7ccf90635f95119ab82a62ae268",   "msatoshi" : 200000,   "amount_msat" : "200000msat",   "msatoshi_sent" : 200003,   "amount_sent_msat" : "200003msat",   "created_at" : 1558106072,   "status" : "complete",   "payment_preimage" : "1a9552b9f4e9199e26839353f870b12cc85b7674a2fb134e78aa370032611019",   "bolt11" : "lnbc2u1pwdana3pp5c0nyfgq974hr8huflt9uutyalj4maaw3q5594xp89jkvw74jme3sdql2pshjgr5dus8q6r0dejjqampd3kx2aqcqzpgwjgn45gy80jwjhgm3tpsxg33j6h6pehdus0mnjerrad943cz3vs83g30lyhlhfjxtqvtl76vttkuhs5jekuxpsqmf98l8265pwmm76gp4e7z6j"}

All the statuses of payments are handled by the whole network and your own node. You don't have a need (but have a possibility to do so) to communicate at all with receiving-side app developers or their API, some card processing provider or any bank to get confirmation about your payment or to release funds from your own account. There are no charge-backs or any other kind of external influence on you accounts balance. All you need is a software which acts by decentralized network rules. This is a solution to exchange a value over internet, without a need of any kind of arbiter in the middle who decides about your payment compatibility with some rules, sometimes even previously unknown to all parties. There are no "internal" rules in bitcoin, all the rules your app needs to complete with are open and known.

所有付款状态均由整个网络和您自己的节点处理。 您根本不需要(但有可能这样做)与接收方应用程序开发人员或其API,某些卡处理提供商或任何银行进行通信,以获取有关您的付款的确认信息或从您自己的资金中释放资金帐户。 您的帐户余额没有任何退款或其他任何外部影响。 您需要的只是一款根据分散的网络规则运行的软件。 这是一种通过Internet交换价值的解决方案,而无需中间任何仲裁者来决定您的付款是否符合某些规则,有时甚至是各方以前都不知道的。 比特币中没有“内部”规则,您的应用程序需要完成的所有规则都是公开的。

This makes possible direct, almost free and instant value exchange over internet between people or even software itself in truly autonomous way.

这使得人们之间甚至软件本身之间以真正自治的方式直接几乎免费即时地通过互联网交换价值成为可能。

If there is a way to get payment data from other app then you are able to act with it in financial sense. As in our example, lnsms.world is a web-app that allows you to send sms to any number and pay for it with bitcoin. To integrate such option in our app we simply need to pass required data from web-form to lnsms.world:

如果有一种方法可以从其他应用程序获取付款数据,那么您就可以在财务上采取行动。 在我们的示例中,lnsms.world是一个网络应用程序,可让您向任何号码发送短信并使用比特币付款。 要将此类选项集成到我们的应用中,我们只需要将所需数据从Web表单传递到lnsms.world:

payload = {                                                                                                                                   'number': phone_number,        'text': text,        'force_unicode': 0    }    send_req = requests.post('https://lnsms.world/invoice', data=payload)    if send_req.status_code == 201:        plain_invoice = str(send_req.text)

I don't know for what reason, but lnsms responds with 201 HTTP status code and returns BOLT11 invoice. Well, that's enough to pay this invoice and our users will send sms shortly after payment is received. We can charge some fee on top or not, that is our free choice.

我不知道是出于什么原因,但是lnsms会以201 HTTP状态代码进行响应并返回BOLT11发票。 好了,足够支付这张发票,我们的用户将在收到付款后不久发送短信。 我们可以免费收取一些费用,这是我们的免费选择。

Now, imagine, that your bot is actually some more useful app and pays not another bot, but some other entity, connected to the Internet. Or, those may be payments between software, machines, stores, service providers on regular basis repeating each few seconds? Do you feel the power of micro-payments? That is how the Internet of Things will look like. Already now there are such projects as and in near future we will face great changes in field what we call e-commerce.

现在,想象一下,您的机器人实际上是一个更有用的应用程序,并且无需支付给另一个机器人,而可以支付连接到Internet的其他实体。 或者,这些可能是软件,机器,商店,服务提供商之间定期每隔几秒钟重复一次的付款? 您觉得小额付款的力量吗? 这就是物联网的样子。 现在已经有了这样的项目,并且在不久的将来,我们将在所谓的电子商务领域面临巨大的变化。

If you like this post and would try bitcoin payments yourself you can .

如果您喜欢这篇文章,并且自己尝试使用比特币付款,可以 。

翻译自:

小额免密

转载地址:http://brbwd.baihongyu.com/

你可能感兴趣的文章
python tkinter text 清空_销毁标签的python tkinter
查看>>
python安装tensorflow gpu_[tensorflow] tensorflow-cpu/gpu 安装过程
查看>>
java二维数组矩阵_获取从二维数组矩阵的行和列在Java中
查看>>
scala mysql连接池_Scala 操作Redis使用连接池工具类RedisUtil
查看>>
css背景图片高斯模糊_CSS3 filter(滤镜) 制作图片高斯模糊无需JS
查看>>
java获取微信支付回调_java相关:SpringMvc微信支付回调示例代码
查看>>
java语言程序设计全国考试题_全国月高等教育自学考试Java语言程序设计一考试试题...
查看>>
python什么是入口文件_小问题大隐患:如何正确设置 Python 项目的入口文件?
查看>>
java 中两个类关联不了_java – JPA-在非实体类中连接两个表
查看>>
java web sessionid_JavaWeb根据sessionId获取session对象
查看>>
php 控制骰子概率,掷色子猜大小游戏(可控制概率)
查看>>
微信 开发 实例 php,PHP实例
查看>>
显示天气 php代码,一个显示天气预报的程序_php实例
查看>>
php取微信名字和头像,获取微信昵称、头像等信息方法
查看>>
apache 2.4 配置php,apache 2.4 +php7安装手册
查看>>
php 网页自适应屏幕,HTML如何将网页设计自动适应屏幕宽度
查看>>
php nslookup,linux下ping、nslookup均正常但是在php环境下gethostname却无法解析出ip
查看>>
java多线程霓虹灯,Android开发实现布局帧布局霓虹灯效果示例
查看>>
media add.php 漏洞,dedecms后台文件media_add.php任意上传漏洞的解决方法
查看>>
php 判断app浏览器打开,手机浏览器下判断是否安装某app,并判断是否打开该应用...
查看>>