· 3 min read
使用门罗币XMR进行监控收款(入门篇)
使用USDT收款相对比较简单,只需监控特定地址在USDT智能合约上的交易记录,只统计向该地址转账的交易并与订单信息进行比对,即可完成收款。然而,门罗币的匿名性导致第三方监控无法获取发送方地址和金额,交易信息只能通过接收方使用私钥进行监控。

原理
使用USDT收款相对比较简单,只需监控特定地址在USDT智能合约上的交易记录,只统计向该地址转账的交易并与订单信息进行比对,即可完成收款。然而,门罗币的匿名性导致第三方监控无法获取发送方地址和金额,交易信息只能通过接收方使用私钥进行监控。
下载程序
教程的以下内容均使用命令行程序进行演示。
创建钱包
./monero-wallet-cli --generate-new-wallet mywallet
输入密码(牢记),选择助记词语言并生成助记词(做好备份,丢失助记词将无法找回钱包和资产).

使用以下命令同步和管理钱包
./monero-wallet-cli --password your-wallet-password --wallet-file ./mywallet.wallet --trusted-daemon --daemon-address https://node.monerod.org:443

并将viewkey保存好,后边会用到。这里作为测试的钱包的View key: a2be3d11a0e07a8ca3c4bcbc4e1a516e7ba92a933f0bf6591cbb1ea31a8c1004
使用View Key创建观察钱包,钱包权限只限于接收资产,无转出权限。
A view-only wallet is a special type of wallet that can only see incoming transactions. Since it doesn’t hold your mnemonic seed and private spend key, it can’t sign transactions and it can’t see outgoing transactions.
钱包观察功能在收款场景中非常实用,可以保证资产安全的同时监控资产的转入情况。 使用以下命令创建观察钱包
./monero-wallet-cli --generate-from-view-key myviewwallet

启动RPC服务器
将配置写入配置文件rpc.conf,省去在命令行输入的繁琐。
trusted-daemon=1
daemon-address=https://node.monerod.org:443
rpc-bind-port=18088
# restricted-rpc=1
rpc-login=login-user:login-password
rpc服务启动命令
./monero-wallet-rpc --config-file ./rpc.conf --wallet-file ./myviewwallet.wallet --password my-viewpass
本地18088端口将监听rpc调用。
使用Python SDK调用rpc服务
使用pip安装monero依赖
pip install monero
在Python代码中初始化钱包
from monero.wallet import Wallet
from monero.backends.jsonrpc import JSONRPCWallet
import config
wallet = Wallet(JSONRPCWallet(
port=18088, user='login-user', password='login-password'))
监听收款
from monero_rpc.wallet import wallet
from monero.transaction import IncomingPayment
def format(inc: IncomingPayment):
return {
'txid': inc.transaction.hash,
'block': inc.transaction.height,
'symbol': 'XMR',
'from': 'unknown',
'to': str(inc.local_address),
'amount': inc.amount,
'timestamp': int(inc.timestamp.timestamp()) * 1000
}
def txes(min_height=0):
ts = []
for incoming in wallet.incoming(min_height=min_height):
tx = format(incoming)
ts.append(tx)
return ts
从以上代码可以看出,我们只能获取收款金额,而无法获取到发送方地址。这正是门罗币的匿名性所在。


