【问题标题】:Is there a bitcoin API that includes transaction priority or input age?是否有包含交易优先级或输入年龄的比特币 API?
【发布时间】:2014-07-11 23:32:43
【问题描述】:

我正在尝试为我的比特币支付处理增加一些额外的安全性。我希望允许即时交付数字商品而无需等待确认,但前提是交易是安全的,即不是双重支出并且具有足够高的费用或优先级。如果检测到异常,我想等待确认后再发货。

我可以使用 blockchain.info API 和 blockr.io 作为备份来提取所需的信息,以检查 tx 是否是双花或对其大小收取足够高的费用。我还想像比特币网络一样,允许无费用的高优先级交易被接受,就好像它们有费用一样。问题是我找不到提供所需信息的 API,无论是 tx 优先级还是 tx 输入的硬币年龄,这些信息用于优先级计算,如下所述: https://en.bitcoin.it/wiki/Transaction_fees#Technical_info

我可以使用额外的 API 调用来引用 tx 的输入并找到确认的数量,但显然单个 API 调用是理想的。如果不存在这样的 API,我可以访问一个完整的比特币节点,我认为它可以以某种方式用于设置我自己的 API。在这种情况下,我需要指出一些关于如何开始的文档。

【问题讨论】:

标签: bitcoin


【解决方案1】:

我使用我的比特币节点easybitcoin-php 和以下 PHP 代码解决了这个问题。
如果 tx 有标准或更高的费用,或者没有费用但足够高的优先级,则访问页面并传递 tx 哈希将输出“高优先级”,否则将输出“低优先级”或“错误”,如果有是个问题。

<?php
require_once('/var/www/easybitcoin.php');

$bitcoin = new Bitcoin('user', 'pass', '127.0.0.1', '8332');

$tx_hash = $_GET['tx'];
$tx = $bitcoin->getrawtransaction($tx_hash, 1);

$coin_age = 0;
$in_value = 0;
$out_value = 0;
$output_too_small = false;

foreach($tx['vin'] as $input) {
  $tx_in = $bitcoin->getrawtransaction($input['txid'], 1);
  $value = $tx_in['vout'][$input['vout']]['value'];
  $coin_age += $value * 1e8 * $tx_in['confirmations'];
  $in_value += $value;
}
foreach($tx['vout'] as $output) {
  $out_value += $output['value'];
  if ($output['value'] < 0.01) {
    $output_too_small = true;
  }
}

$size = strlen($tx['hex']) / 2;
$priority = $coin_age / $size;
$fee_per_byte = (($in_value - $out_value) * 1e8) / $size;

if (is_numeric($priority)) {
  if (($priority > 57600000 && $size < 1000 && $output_too_small == false) || $fee_per_byte >= 10) {
    echo 'high priority';
  } else {
    echo 'low priority';
  }
} else {
  echo 'error';
}
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-31
    • 2017-03-11
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2017-04-22
    相关资源
    最近更新 更多