如何从生成的 btc 地址建立交易?

问题描述

我正在尝试生成从比特币地址 A 到比特币地址 B 的交易。

我正在使用名为 bitwasp 的库,但是该库的文档并不适合初学者。

现在我的问题是 如何创建一个简单的交易并将其发送到网络? 就像“使用 [privateKey1] 从 [publicKey1] 向 [publicKey2] 发送 0.35 btc”。我读了很多我觉得很难理解的东西,比如附加许多输入和输出,是否存在一种简单的方法?

我认为我需要使用以下步骤

  1. 使用事务构建器。
  2. 使用验证器验证交易并将其发送到 btc 网络

这是否正确,您能否阐明我如何使用这些功能

这就是我现在要生成的地址,我被卡住的部分是构建交易,如果有人能告诉我下一步该怎么做那就太好了!

<?php
use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;

use BitWasp\Bitcoin\Script\Factory\OutputScriptFactory;
use BitWasp\Bitcoin\Script\Consensus\NativeConsensus;
use BitWasp\Bitcoin\Transaction\TransactionFactory;;

use BitWasp\Bitcoin\Script\WitnessProgram;
use BitWasp\Bitcoin\Address\SegwitAddress;

require __DIR__ . "/vendor/autoload.php";

$network = Bitcoin::getNetwork();

function generateBitcoinWallet()
{

    $result = array();

    $privKeyFactory = new PrivateKeyFactory();
    $rbg = new Random();
    $privateKey = $privKeyFactory->generateCompressed($rbg);
    $publicKey = $privateKey->getPublicKey();
    $pubKeyHash160 = $publicKey->getPubKeyHash();
    $pubKeyHashAddr = new PayToPubKeyHashAddress($pubKeyHash160);
    $witnessPubKeyHashAddr = new SegwitAddress(WitnessProgram::v0($pubKeyHash160));

    $hashAddress = new PayToPubKeyHashAddress($publicKey->getPubKeyHash());

    $result["publicKey"] = $pubKeyHashAddr->getAddress();
    $result["privateKey"] = $witnessPubKeyHashAddr->getAddress();
    $result["btcAddress"] = $hashAddress->getAddress();


    return $result;
}
$keys = generateBitcoinWallet();
// I get these kind of keys:
// Public: 1G7gaATj8psxvsHSpRBQwmJVzkUQheSv44
// Private: bc1q5hxk545uqzrrjxfzjy6cd65wpkpvp59nnuh55m
// btcAddress: 1G7gaATj8psxvsHSpRBQwmJVzkUQheSv44

$outputFactory = new OutputScriptFactory();
$script = $outputFactory->payToPubKey($publicKey); // 

// build a transaction/
$transactionFactory = new TransactionFactory();
$transactionBuilder = $transactionFactory->build(); 
// I don't know what to do with the transactionBuilder as it wants many outputs,inputs,witnesses etc.

$consensus = new NativeConsensus();
$bool = $consensus->verify($transaction,$script,$flags,$nInputToSign,$amount); // should return true if success

我觉得你可能认为我根本没有做任何研究,但我向你保证我搜索了很多,我很绝望。我知道我可以使用 api,但我不知道我可以使用什么 api 来处理所有这些事情, 我知道区块链有一个 api,但要获得 api 密钥,我需要等待多天,您可以向我推荐另一个 api 吗?

非常感谢您的帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)