GithubHelp home page GithubHelp logo

flxstn / bitcoin-php Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bit-wasp/bitcoin-php

0.0 2.0 0.0 3.63 MB

Bitcoin implementation in PHP

License: The Unlicense

PHP 99.76% Protocol Buffer 0.24%

bitcoin-php's Introduction

##Bitcoin

Build Status Code Coverage Scrutinizer Code Quality

Latest Stable Version Join the chat at https://gitter.im/Bit-Wasp/bitcoin-php

This repository contains an implementation of Bitcoin using mostly pure PHP.

As the saying goes - "never use a dynamically interpreted language to write consensus software" Now that you know this, please continue!

If the relevant extensions are installed, ie, secp256k1-php and bitcoinconsensus-php, it will be used instead of the native PHP implementation.

Besides classes for interfacing with Bitcoind's RPC, and Stratum servers, this library does NOT contain any code to interact with the network, or any API's.

See below, or here for examples.

Other repositories which are part of this project:

Requirements:

  • PHP 5.4+
  • Composer
  • ext-gmp
  • ext-json
  • ext-curl
  • ext-mcrypt

Optional:

Installation

You can install this library via Composer: composer require bitwasp/bitcoin

Contributing

All contributions are welcome. Please see [this page] before you get started

##Presently supported:

  • Bloom filters
  • Blocks, headers, and merkle blocks.
  • Regular/P2SH scripts.
  • An adaptable elliptic-curve library, using [PhpEcc] by default, or libsecp256k1 if the bindings are found.
  • Support for building, parsing, signing/validating transactions.
  • Deterministic signatures (RFC6979)
  • BIP32 and electrum (older type I) deterministic key algorithms.
  • ScriptFactory for common input/output types, parser, interpreter, and classifiers.
  • Supports bindings to libbitcoinconsensus.
  • RPC bindings to Bitcoin Core's RPC, getting OOP responses
  • Bindings to Stratum (electrum) servers
  • Easy serialization to binary representation of most classes
  • SIGHASH types when creating transactions
  • Payment Protocol (BIP70)
  • Blockchain classes utilizing the doctrine/cache package

##Known Issues:

The script interpreter has a modest set of test vectors, but these are mostly positive tests, that don't really exercise many of the edge cases. While it works, it's not bug-for-bug compatible yet and should not be relied on for consensus. Similarly, the secp256k1-php extension is a wrapper around an experimental library which has not yet been formally released. It's use should not be relied on until the upstream library has made a stable release.

Implemented BIPs

  • BIP0011 - M of N standard transactions
  • BIP0016 / BIP0013 - Pay to Script hash, and corresponding address format.
  • BIP0014 - Protocol Version and User Agent
  • BIP0031 - Pong message
  • BIP0032 - Hierarchical Deterministic Wallets
  • BIP0035 - Mempool Message
  • BIP0037 - Blooom Filtering
  • BIP0039 - Mnemonic code for generating deterministic keys
  • BIP0066 - Strict DER Signatures
  • BIP0067 - Deterministic P2SH multi-signature addresses
  • BIP0070 - Payment Protocol

Examples

Generate private keys

 // Create private keys
 use BitWasp\Bitcoin\Bitcoin;
 use BitWasp\Bitcoin\Key\PrivateKeyFactory;

 $network = Bitcoin::getNetwork();
 $private = PrivateKeyFactory::create(true);
 $public = $private->getPublicKey();
 echo $private->getHex() . "\n";
 echo $private->toWif($network) . "\n";
 echo $public->getHex() . "\n";
 echo $public->getAddress($network)->getAddress() . "\n";

Explore the blockchain using OOP bindings to the RPC

use BitWasp\Bitcoin\Rpc\RpcFactory;

$bitcoind = RpcFactory::bitcoind('127.0.0.1', 18332, 'bitcoinrpc', 'BBpsLqmCCx7Vp8sRd5ygDxFkHZBgWLTTi55QwWgN6Ng6');

$hash = $bitcoind->getbestblockhash();
$block = $bitcoind->getblock($hash);
$tx = $bitcoind->getTransactions()->getTransaction(10);
echo $tx->getTransactionId();

Create signed payment requests

use BitWasp\Bitcoin\Address\AddressFactory;
use BitWasp\Bitcoin\PaymentProtocol\PaymentRequestBuilder;
use BitWasp\Bitcoin\PaymentProtocol\PaymentRequestSigner;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Transaction\TransactionOutput;

$time = time();
$amount = 10000;
$destination = '18Ffckz8jsjU7YbhP9P44JMd33Hdkkojtc';
$paymentUrl = 'http://192.168.0.223:81/bitcoin-php/examples/bip70.fetch.php?time=' . $time;

// Create a signer for x509+sha256 - this requires a readable private key and certificate chain.
// $signer = new PaymentRequestSigner('none');
$signer = new PaymentRequestSigner('x509+sha256', '/var/www/git/paymentrequestold/.keys/ssl.key', '/var/www/git/paymentrequestold/.keys/ssl.pem');
$builder = new PaymentRequestBuilder($signer, 'main', time());

// PaymentRequests contain outputs that the wallet will fulfill
$address = AddressFactory::fromString($destination);
$builder->addAddressPayment($address, $amount);

// Create the request, send it + headers
$request = $builder->send();

Sign multi-signature transactions

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Transaction\Transaction;
use BitWasp\Bitcoin\Transaction\TransactionInput;
use BitWasp\Bitcoin\Transaction\TransactionOutput;
use BitWasp\Bitcoin\Transaction\TransactionBuilder;
use BitWasp\Bitcoin\Transaction\TransactionFactory;

$ecAdapter = Bitcoin::getEcAdapter();

// Two users independently create private keys.
$pk1 = PrivateKeyFactory::fromHex('421c76d77563afa1914846b010bd164f395bd34c2102e5e99e0cb9cf173c1d87');
$pk2 = PrivateKeyFactory::fromHex('f7225388c1d69d57e6251c9fda50cbbf9e05131e5adb81e5aa0422402f048162');

// They exchange public keys, and a multisignature address is made (sorted keys)
$redeemScript = ScriptFactory::multisig(2, [$pk1->getPublicKey(), $pk2->getPublicKey()], true);
$outputScript = $redeemScript->getOutputScript();

// The address is funded with a transaction (fake, for the purposes of this script).
// You would do getrawtransaction, or construct it elsewhere in your application
$spendTx = TransactionFactory::create()
    ->input('4141414141414141414141414141414141414141414141414141414141414141', 0)
    ->output(50, $outputScript)
    ->get();

// Someone wants to spend the funds. He creates a transaction spending them..
$new = TransactionFactory::create()
    ->spendOutput($spendTx, 0)
    ->payToAddress(50, $pk1->getAddress())
    ->get();
    
// And both parties sign
$signed = TransactionFactory::sign()
    ->signInputWithKey($pk1, $outputScript, 0, $redeemScript)
    ->signInputWithKey($pk2, $outputScript, 0, $redeemScript)
    ->get();

$rawTx = $signed->getHex();

echo "Fully signed transaction: " . $builder->getTransaction()->getHex() . "\n";

Create/verify signed Bitcoin messages

use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\MessageSigner\MessageSigner;
use BitWasp\Bitcoin\Key\PrivateKeyFactory;

$ec = Bitcoin::getEcAdapter();
$privateKey = PrivateKeyFactory::create(true);

$message = 'hi';

$signer = new MessageSigner($ec);
$signed = $signer->sign($message, $privateKey);

echo sprintf("Signed by %s\n%s\n", $privateKey->getAddress()->getAddress(), $signed->getBuffer()->getBinary());

if ($signer->verify($signed, $privateKey->getAddress())) {
    echo "Signature verified!\n";
} else {
    echo "Failed to verify signature!\n";
}

Create/use BIP39 mnemonics

use BitWasp\Bitcoin\Crypto\Random\Random;
use BitWasp\Bitcoin\Mnemonic\MnemonicFactory;
use BitWasp\Bitcoin\Mnemonic\Bip39\Bip39SeedGenerator;

// Generate a mnemonic
$random = new Random();
$entropy = $random->bytes(64);

$bip39 = MnemonicFactory::bip39();
$seedGenerator = new Bip39SeedGenerator($bip39);
$mnemonic = $bip39->entropyToMnemonic($entropy);

// Derive a seed from mnemonic/password
$seed = $seedGenerator->getSeed($mnemonic, 'password');
echo $seed->getHex() . "\n";

$bip32 = \BitWasp\Bitcoin\Key\HierarchicalKeyFactory::fromEntropy($seed);

Write/interpret bitcoin scripts

use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Flags;
use BitWasp\Bitcoin\Transaction\Transaction;

$ec = \BitWasp\Bitcoin\Bitcoin::getEcAdapter();

$script = ScriptFactory::create()->op('OP_1')->op('OP_1')->op('OP_ADD')->getScript();

echo "Formed script: " . $script->getHex() . "\n";
print_r($script->getScriptParser()->decode());

$i = new \BitWasp\Bitcoin\Script\Interpreter\Interpreter($ec, new Transaction(), new Flags(0));
$i->evaluate($script);

print_r($i);

bitcoin-php's People

Contributors

afk11 avatar flxstn avatar rubensayshi avatar scrutinizer-auto-fixer avatar willgriffin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.