bitcoin-php icon indicating copy to clipboard operation
bitcoin-php copied to clipboard

How to do these basics actions ?

Open ZauChoco opened this issue 4 years ago • 1 comments

Hello, I'm new in the cryptocurrencies world, and i need help to understand your library. It seems huge, and i'm lost with all its features. I have some questions:

  1. Is it the good way to generate a wallet? I mean, can i create transactions with only these informations?
<?php
require "C:/xampp/vendor/autoload.php";

use BitWasp\Bitcoin\Address\PayToPubKeyHashAddress;
use BitWasp\Bitcoin\Address\SegwitAddress;
use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory;
use BitWasp\Bitcoin\Script\WitnessProgram;



  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));

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


    return $result;
  }

?>

I get these kind of keys: Public: 14h3sYm559vt7YiKsmNviyXNCqULaSKKCf Private: bc1q9paknt7d05tmzh2frcu8qxgk0ev0mtnpgcz4a3

  1. I would like to be able to list all the transactions and the balance bind to a key, is it possible? How?

  2. How to create a transaction simply and send it to the network? Like "Send 0.35 btc from [publicKey1] with [privateKey1] to [publicKey2]". Because I've read a lot of hard stuffs, like attach many inputs and outputs, does a simple way exist?

  3. How to choose an alternative network like Testnet?

I feel like you could think i didn't make any research at all, but I assure you that i searched a lot, and I'm desperate. I know that i could use an api, but i want to stay independent.

Thank you

ZauChoco avatar May 08 '20 11:05 ZauChoco

Is it the good way to generate a wallet?

No one really generates random private keys, they use deterministic keys because you can recover all the keys from a seed later. Look over BIP32 to understand how the paths work "m/0'/1". Then you should use BIP44 or similar to structure your BIP32 wallet.

I would like to be able to list all the transactions and the balance bind to a key, is it possible? How?

Wrong place to ask. This library is not a solution that comes with a database, or an integration with the bitcoin network. People do that in so many different ways it doesn't make sense for us to try. You'll need to a way that works for you.

How to create a transaction simply and send it to the network?

Two separate issues.

  • We have extensive examples around creating transactions.
  • Use bitcoind's sendrawtransaction? Or whatever solution you decide to use for connecting to the bitcoin network.

How to choose an alternative network like Testnet?

https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/src/Network/NetworkFactory.php

Create a new network object, and pass it into anywhere that produces network specific encodings. These methods (address creation, bip32 serialization) normally default to using Bitcoin, as it's the default. You can override the default (https://github.com/Bit-Wasp/bitcoin-php/blob/1.0/src/Bitcoin.php#L105) but trust me when I tell you the best way is to pass the $network object you want to use as an argument

Unfortunately, the library is pretty tough to work with if you've never used bitcoin before. Once you know the main topics in the bitcoin software space, you'll find it easier to use such libraries, because you're looking for a specific feature.. not trying to select from a tonne of features you don't understand!

To make it easier, I suggest you setup a testnet electrum wallet, and try to reproduce how it does things with code here. Electrum follows most of the major BIP's, so if you can interoperate with it, you're learning how most wallets work :) (hint: BIP39, BIP32, BIP44/BIP49/BIP84 or similar)

afk11 avatar May 08 '20 14:05 afk11