So generieren Sie Adressen in PHP

Ich brauche etwas PHP-Code, um Bitcoin-Adressen aus einem bestimmten öffentlichen Schlüssel zu generieren.

Ich kann nur Algorithmen finden, um Adressen aus dem privaten Master-Schlüssel zu generieren, aber laut https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses ist der private Schlüssel überhaupt nicht erforderlich.

Antworten (2)

Verwenden Sie die BitWasp Bitcoin-php-Bibliothek https://github.com/Bit-Wasp/bitcoin-php

<?php
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;

$network = Bitcoin::getNetwork();
$privateKey = PrivateKeyFactory::create(true);
$publicKey = $privateKey->getPublicKey();
$address = $publicKey->getAddress(); // returns AddressInterface
echo $address->getAddress($network); // prints address for $network as string

Wenn Sie eine Bitcoin-Adresse aus der Zeichenfolge des öffentlichen Schlüssels erstellen möchten, überprüfen Sie die Dokumentation für Factory-Klassen

oder neuere Versionen:

<?php
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Address\AddressCreator;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Key\KeyToScript\Factory\P2pkhScriptDataFactory; 

$network = Bitcoin::getNetwork();
$privateKey = PrivateKeyFactory::create(true);
$publicKey = $privateKey->getPublicKey();

$addrCreator = new AddressCreator();
$factory = new P2pkhScriptDataFactory();
$scriptPubKey = $factory->convertKey($publicKey)->getScriptPubKey();
$address = $addrCreator->fromOutputScript($scriptPubKey); // returns AddressInterface
echo $address->getAddress($network); // prints address for $network as string
Diese Antwort verwendet immer noch einen privaten Schlüssel und basiert nicht wie gewünscht auf einem rein öffentlichen Schlüssel.
Sie können PublicKeyFactory::fromHex ausführen und die Verwendung eines privaten Schlüssels vermeiden. Für neuere Versionen wurde getAddress entfernt, siehe dieses Beispiel github.com/Bit-Wasp/bitcoin-php/blob/…

Vollständige Anleitung basierend auf der Antwort von @Farghaly: Ubuntu 16

Abhängigkeiten installieren

sudo apt-get install php-bcmath php-gmp
composer require bitwasp/bitcoin

Und dann im Feldapp.php

<?php
require 'vendor/autoload.php';

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Address;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;

$network = Bitcoin::getNetwork();

$privateKey = PrivateKeyFactory::create(true);
$publicKey = $privateKey->getPublicKey();
$address = $publicKey->getAddress();
vendor/autoload.php wo finde ich diese Datei?
@makasci Ich glaube, es wird beim Ausführen automatisch vom Composer generiertcomposer install