原始文献 / 论坛帖 / Sample account system using JSON-RPC needed

Sample account system using JSON-RPC needed

Bitcointalk · 2010-07-16 19:45:10 UTC

AuthorSatoshi Nakamoto
TitleSample account system using JSON-RPC needed
Date2010-07-16 19:45:10 UTC
ForumBitcointalk · 原文

我们需要有人写一份示例代码,最好是 Python 或 Java,展示用 JSON-RPC 接口搭建账户系统的推荐方式。大多数卖东西的网站都会需要这个。一直关注 JSON-RPC 相关帖子的人应该知道它大概该怎么写。

当用户登录自己的账户时,你向他展示一个可以发送比特币来充值的比特币地址。在展示之前,你先检查它是否被用过,如果用过就换成新的(getnewaddress <username>)。你的数据库里每个账户只需要保存最新的比特币地址。(我在之前某个帖子里贴过这段的示例代码片段,搜索 getnewaddress)

你用 getreceivedbylabel <username>,以用户名作为标签,来获取账户的"贷方"金额。你需要在数据库里保存一个"借方"金额。账户当前余额 =(贷方 - 借方)。当用户花钱时,你增加借方。

如果你要求大于 0 个确认,最好同时显示当前余额(0 个确认)和可用余额(1 个或更多确认),这样他们能立刻看到付款已被确认。不是所有网站都需要等确认,所以当前余额和可用余额的双显示应该是可选的。大多数卖数字商品的网站接受 0 确认没问题。

一个不错的示例应用是简单的银行网站,包含上面这些功能,外加向比特币地址发送付款的选项。示例代码应该尽可能简单,只保留让它成为一个能跑起来的网站的最少额外内容。

vekja.net 就是这类网站的一个例子。

英文原文

We need someone to write sample code, preferably Python or Java, showing the recommended way to use the JSON-RPC interface to create an account system. Most sites that sell things will need something like this. Someone who's kept up on the JSON-RPC threads here should have some idea how it should work.

When a user is logged in to their account, you show the bitcoin address they can send to to add funds. Before showing it, you check if it's been used, if it has then you replace it with a new one (getnewaddress <username>). You only need to keep the latest bitcoin address for the account in your database. (I posted a sample code fragment for this in an earlier thread somewhere, search on getnewaddress)

You use getreceivedbylabel <username> with the username as the label to get the "credit" amount of the account. You need to keep a "debit" amount in your database. The current balance of the account is (credit - debit). When the user spends money, you increase debit.

If you're requiring more than 0 confirmations, it's nice if you show the current balance (0 confirmations) and the available balance (1 or more confirmations), so they can immediately see that their payment is acknowledged. Not all sites need to wait for confirmations, so the dual current & available should be optional. Most sites selling digital goods are fine to accept 0 confirmations.

A nice sample app for this would be a simple bank site, which would have the above, plus the option to send a payment to a bitcoin address. The sample code should be the simplest possible with the minimum extra stuff to make it a working site.

vekja.net is an example of a site like this.