コード例 #1
0
ファイル: ScriptFactory.php プロジェクト: nmarley/bitcoin-php
 /**
  * @param int|null $flags
  * @param EcAdapterInterface|null $ecAdapter
  * @return NativeConsensus
  */
 public static function getNativeConsensus($flags = null, EcAdapterInterface $ecAdapter = null)
 {
     if ($flags === null) {
         $flags = self::defaultFlags();
     }
     return new NativeConsensus($ecAdapter ?: Bitcoin::getEcAdapter(), $flags);
 }
コード例 #2
0
 /**
  * @param \BitWasp\Buffertools\Buffer|string $string
  * @param EcAdapterInterface $ecAdapter
  * @return SignatureInterface
  */
 public static function fromHex($string, EcAdapterInterface $ecAdapter = null)
 {
     $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
     $serializer = EcSerializer::getSerializer($ecAdapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface');
     /** @var DerSignatureSerializerInterface $serializer */
     return $serializer->parse($string);
 }
コード例 #3
0
 /**
  * @param string $hex
  * @param bool $compressed
  * @param EcAdapterInterface|null $ecAdapter
  * @return PrivateKey
  */
 public static function fromHex($hex, $compressed = false, EcAdapterInterface $ecAdapter = null)
 {
     $hex = Buffer::hex($hex);
     $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
     $hexSerializer = new HexPrivateKeySerializer($ecAdapter);
     return $hexSerializer->parse($hex)->setCompressed($compressed);
 }
コード例 #4
0
 public function __construct()
 {
     parent::__construct(function () {
         $consensus = new ConsensusFactory(Bitcoin::getEcAdapter());
         $loop = LoopFactory::create();
         $context = new ZmqContext($loop);
         $control = $context->getSocket(\ZMQ::SOCKET_SUB);
         $control->connect('tcp://127.0.0.1:5594');
         $control->subscribe('control');
         $control->on('messages', function ($msg) use($loop) {
             if ($msg[1] == 'shutdown') {
                 $loop->stop();
             }
         });
         $results = $context->getSocket(\ZMQ::SOCKET_PUSH);
         $results->connect("tcp://127.0.0.1:5593");
         $workers = $context->getSocket(\ZMQ::SOCKET_PULL);
         $workers->connect('tcp://127.0.0.1:5592');
         $workers->on('message', function ($message) use($consensus, $results) {
             $details = json_decode($message, true);
             $txid = $details['txid'];
             $flags = $details['flags'];
             $vin = $details['vin'];
             $scriptPubKey = new Script(Buffer::hex($details['scriptPubKey']));
             $tx = TransactionFactory::fromHex($details['tx']);
             $results->send(json_encode(['txid' => $txid, 'vin' => $vin, 'result' => $consensus->getConsensus(new Flags($flags))->verify($tx, $scriptPubKey, $vin)]));
         });
         $loop->run();
         exit(0);
     });
 }
コード例 #5
0
 /**
  * @param Parser $parser
  * @return Alert
  */
 public function fromParser(Parser $parser)
 {
     $detail = $this->detail->fromParser($parser);
     list($sigBuffer) = $this->getSigBuf()->parse($parser);
     $adapter = Bitcoin::getEcAdapter();
     $serializer = EcSerializer::getSerializer('BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface', true, $adapter);
     $sig = $serializer->parse($sigBuffer);
     return new Alert($detail, $sig);
 }
コード例 #6
0
 /**
  * @param \BitWasp\Buffertools\BufferInterface|string $hex
  * @param bool $compressed
  * @param EcAdapterInterface|null $ecAdapter
  * @return PrivateKey
  */
 public static function fromHex($hex, $compressed = false, EcAdapterInterface $ecAdapter = null)
 {
     $ecAdapter = $ecAdapter ?: Bitcoin::getEcAdapter();
     /** @var PrivateKeySerializerInterface $serializer */
     $serializer = EcSerializer::getSerializer($ecAdapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Key\\PrivateKeySerializerInterface');
     $parsed = $serializer->parse($hex);
     if ($compressed) {
         $parsed = $ecAdapter->getPrivateKey($parsed->getSecretMultiplier(), $compressed);
     }
     return $parsed;
 }
コード例 #7
0
 protected function discover_wallet_addrs_multisig($xpub_list)
 {
     foreach ($xpub_list as $pk) {
         $k[] = HierarchicalKeyFactory::fromExtended($pk, Bitcoin::getNetwork());
     }
     $params = $this->get_params();
     $num_signers = $params['numsig'];
     $ec = \BitWasp\Bitcoin\Bitcoin::getEcAdapter();
     $sequences = new \BitWasp\Bitcoin\Key\Deterministic\HierarchicalKeySequence($ec->getMath());
     $hd = new \BitWasp\Bitcoin\Key\Deterministic\MultisigHD($num_signers, 'm/44', $k, $sequences, true);
     return $this->discover_addrs($hd);
 }
コード例 #8
0
ファイル: Signer.php プロジェクト: toneloc/php-client
 /**
  * Sign hex data deterministically using deterministic k.
  *
  * @param string $hexDataToSign
  * @param PrivateKeyInterface|string $privateKey
  * @return string
  */
 public static function sign($hexDataToSign, $privateKey)
 {
     if (is_string($privateKey)) {
         $privateKey = PrivateKeyManipulator::importPrivateKey($privateKey);
     }
     // Convert hex data to buffer
     $data = Buffer::hex($hexDataToSign);
     /** @var EcAdapterInterface $ecAdapter */
     $ecAdapter = Bitcoin::getEcAdapter();
     // Deterministic digital signature generation
     $k = new Rfc6979($ecAdapter, $privateKey, $data, 'sha256');
     $sig = $ecAdapter->sign($data, $privateKey, $k);
     // DEBUG
     //echo "hexDataToSign: <br/>";
     //var_dump($hexDataToSign);
     //echo "sig: <br/>";
     //var_dump($sig->getHex());
     return $sig->getHex();
 }
コード例 #9
0
ファイル: BitcoinNode.php プロジェクト: Bit-Wasp/node-php
 /**
  * BetterNode constructor.
  * @param ConfigProviderInterface $config
  * @param ParamsInterface $params
  * @param DbInterface $db
  */
 public function __construct(ConfigProviderInterface $config, ParamsInterface $params, DbInterface $db)
 {
     $math = Bitcoin::getMath();
     $adapter = Bitcoin::getEcAdapter($math);
     $this->chains = new ChainContainer($math, $params);
     $consensus = new Consensus($math, $params);
     $pow = new ProofOfWork($math, $params);
     $this->headers = new Index\Headers($db, $adapter, $this->chains, $consensus, $pow);
     $this->blocks = new Index\Blocks($db, $config, $adapter, $this->chains, $consensus);
     $this->transactions = new Index\Transactions($db);
     $genesis = $params->getGenesisBlock();
     $this->headers->init($genesis->getHeader());
     $this->blocks->init($genesis);
     $this->db = $db;
     $segments = $this->db->fetchChainSegments();
     foreach ($segments as $segment) {
         $this->chains->addSegment($segment);
     }
     $this->chains->initialize($this->db);
 }
コード例 #10
0
 /**
  * @param Parser $parser
  * @return Alert
  */
 public function fromParser(Parser &$parser)
 {
     $detail = $this->detail->fromParser($parser);
     list($sigR, $sigS) = $this->getSigTemplate()->parse($parser);
     $adapter = Bitcoin::getEcAdapter();
     if (!$adapter instanceof \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter) {
         // We need to serialize this as DER, and deserialize it using the correct serializer...
         $temporary = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Adapter\EcAdapter($adapter->getMath(), $adapter->getGenerator());
         $sig = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\Signature($temporary, $sigR, $sigS);
         $serializer = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Serializer\Signature\DerSignatureSerializer($temporary);
         $serialized = $serializer->serialize($sig);
         // Parse using native EcAdapter
         /** @var \BitWasp\Bitcoin\Crypto\EcAdapter\Serializer\Signature\DerSignatureSerializerInterface $serializer */
         $serializer = EcSerializer::getSerializer($adapter, 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\DerSignatureSerializerInterface');
         $sig = $serializer->parse($serialized);
     } else {
         $sig = new \BitWasp\Bitcoin\Crypto\EcAdapter\Impl\PhpEcc\Signature\Signature($adapter, $sigR, $sigS);
     }
     return new Alert($detail, $sig);
 }
コード例 #11
0
ファイル: HeaderCheckTest.php プロジェクト: Bit-Wasp/node-php
 public function testCreateInstance()
 {
     $ecAdapter = Bitcoin::getEcAdapter();
     $params = new Params($ecAdapter->getMath());
     $consensus = new Consensus($ecAdapter->getMath(), $params);
     $proofOfWork = new ProofOfWork($ecAdapter->getMath(), $params);
     $check = new HeaderCheck($consensus, $ecAdapter, $proofOfWork);
     $header = BlockHeaderFactory::fromHex($this->getGenesisHex());
     $hash = $header->getHash();
     $this->assertNoException(function () use($check, $hash, $header) {
         $check->check($hash, $header, false);
     });
     $this->assertNoException(function () use($check, $hash, $header) {
         $check->check($hash, $header, true);
     });
     $badHeader = new BlockHeader($header->getVersion(), $header->getPrevBlock(), $header->getMerkleRoot(), $header->getTimestamp(), $header->getBits(), 1);
     $this->assertException(function () use($check, $badHeader) {
         $hash = $badHeader->getHash();
         $check->check($hash, $badHeader, true);
     });
 }
コード例 #12
0
 /**
  * @param PointInterface $point
  * @param bool $compressed
  * @param EcAdapterInterface $ecAdapter
  * @return PublicKey
  */
 public static function fromPoint(PointInterface $point, $compressed = false, EcAdapterInterface $ecAdapter = null)
 {
     return new PublicKey($ecAdapter ?: Bitcoin::getEcAdapter(), $point, $compressed);
 }
コード例 #13
0
 /**
  * @param TransactionInterface $transaction
  * @param EcAdapterInterface|null $ecAdapter
  * @return TxSigner
  */
 public static function sign(TransactionInterface $transaction, EcAdapterInterface $ecAdapter = null)
 {
     return new TxSigner($ecAdapter ?: Bitcoin::getEcAdapter(), $transaction);
 }
コード例 #14
0
<?php

require_once "../vendor/autoload.php";
use BitWasp\Bitcoin\Script\ScriptFactory;
use BitWasp\Bitcoin\Transaction\Transaction;
use BitWasp\Bitcoin\Script\Script;
$ec = \BitWasp\Bitcoin\Bitcoin::getEcAdapter();
$scriptSig = ScriptFactory::create()->int(1)->int(1)->getScript();
$scriptPubKey = ScriptFactory::create()->op('OP_ADD')->int(2)->op('OP_EQUAL')->getScript();
echo "Formed script: " . $scriptSig->getHex() . " " . $scriptPubKey->getHex() . "\n";
$flags = new \BitWasp\Bitcoin\Flags(0);
$i = new \BitWasp\Bitcoin\Script\Interpreter\Interpreter($ec, new Transaction(), $flags);
$result = $i->verify($scriptSig, $scriptPubKey, 0);
echo "Script result: " . ($result ? 'true' : 'false') . "\n";
コード例 #15
0
<?php

require "../vendor/autoload.php";
use BitWasp\Bitcoin\Bitcoin;
use BitWasp\Bitcoin\MessageSigner\MessageSigner;
use BitWasp\Bitcoin\Serializer\MessageSigner\SignedMessageSerializer;
use BitWasp\Bitcoin\Serializer\Signature\CompactSignatureSerializer;
Bitcoin::setNetwork(\BitWasp\Bitcoin\Network\NetworkFactory::bitcoinTestnet());
$address = 'n2Z2DFCxG6vktyX1MFkKAQPQFsrmniGKj5';
$sig = '-----BEGIN BITCOIN SIGNED MESSAGE-----
hi
-----BEGIN SIGNATURE-----
IBpGR29vEbbl4kmpK0fcDsT75GPeH2dg5O199D3iIkS3VcDoQahJMGJEDozXot8JGULWjN9Llq79aF+FogOoz/M=
-----END BITCOIN SIGNED MESSAGE-----';
$ec = Bitcoin::getEcAdapter();
$addr = \BitWasp\Bitcoin\Address\AddressFactory::fromString($address);
$serializer = new SignedMessageSerializer(new CompactSignatureSerializer(Bitcoin::getMath()));
$signedMessage = $serializer->parse($sig);
$signer = new MessageSigner($ec);
if ($signer->verify($signedMessage, $addr)) {
    echo "Signature verified!\n";
} else {
    echo "Failed to verify signature!\n";
}
コード例 #16
0
ファイル: BitcoinNode.php プロジェクト: sbwdlihao/node-php
 /**
  * @param ParamsInterface $params
  * @param LoopInterface $loop
  */
 public function __construct(ParamsInterface $params, LoopInterface $loop)
 {
     echo ' [App] start ' . PHP_EOL;
     $start = microtime(true);
     $math = Bitcoin::getMath();
     $adapter = Bitcoin::getEcAdapter($math);
     $zmq = new ZMQContext($loop);
     $this->initControl($zmq)->initConfig();
     $this->loop = $loop;
     $this->params = $params;
     $this->adapter = $adapter;
     $this->chains = new Chains($adapter);
     $this->inventory = new KnownInventory();
     $this->peerState = new PeerStateCollection();
     $this->peersInbound = new Peers();
     $this->peersOutbound = new Peers();
     $this->netFactory = new NetworkingFactory($loop);
     $this->db = new Db($this->config, false);
     $consensus = new Consensus($math, $params);
     $zmqScript = new ZmqScriptCheck(new \ZMQContext());
     $this->headers = new Index\Headers($this->db, $consensus, $math, new HeaderCheck($consensus, $adapter, new ProofOfWork($math, $params)));
     $this->blocks = new Index\Blocks($this->db, $adapter, $consensus, new BlockCheck($consensus, $adapter, $zmqScript));
     $genesis = $params->getGenesisBlock();
     $this->headers->init($genesis->getHeader());
     $this->blocks->init($genesis);
     $this->initChainState();
     $this->utxo = new Index\UtxoIdx($this->chains, $this->db);
     $this->blockDownload = new BlockDownloader($this->chains, $this->peerState, $this->peersOutbound);
     $this->on('blocks.syncing', function () {
         echo ' [App] ... BLOCKS: syncing' . PHP_EOL;
     });
     $this->on('headers.syncing', function () {
         echo ' [App] ... HEADERS: syncing' . PHP_EOL;
     });
     $this->on('headers.synced', function () {
         echo ' [App] ... HEADERS: synced!' . PHP_EOL;
     });
     echo ' [App] Startup took: ' . (microtime(true) - $start) . ' seconds ' . PHP_EOL;
 }
コード例 #17
0
 /**
  * @param EcAdapterInterface $ecAdapter
  * @return PublicKeySerializerInterface
  */
 public static function getSerializer(EcAdapterInterface $ecAdapter = null)
 {
     return EcSerializer::getSerializer($ecAdapter ?: Bitcoin::getEcAdapter(), 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Key\\PublicKeySerializerInterface');
 }
コード例 #18
0
 /**
  * @param $extendedKey
  * @param NetworkInterface $network
  * @param EcAdapterInterface $ecAdapter
  * @return HierarchicalKey
  */
 public static function fromExtended($extendedKey, NetworkInterface $network, EcAdapterInterface $ecAdapter = null)
 {
     $extSerializer = self::getSerializer($ecAdapter ?: Bitcoin::getEcAdapter(), $network);
     return $extSerializer->parse($extendedKey);
 }
コード例 #19
0
 /**
  * @return \BitWasp\Buffertools\Buffer
  */
 public function getBuffer()
 {
     $serializer = new SignedMessageSerializer(EcSerializer::getSerializer(Bitcoin::getEcAdapter(), 'BitWasp\\Bitcoin\\Crypto\\EcAdapter\\Serializer\\Signature\\CompactSignatureSerializerInterface'));
     return $serializer->serialize($this);
 }
コード例 #20
0
 protected function signTx($private_key_wif, $tx_builder, $transaction_outputs)
 {
     $private_key = PrivateKeyFactory::fromWif($private_key_wif);
     $transaction = $tx_builder->get();
     $signer = new Signer($transaction, Bitcoin::getEcAdapter());
     // sign each input script
     foreach ($transaction_outputs as $offset => $transaction_output) {
         $signer->sign($offset, $private_key, $transaction_output);
     }
     $signed_transaction = $signer->get();
     return $signed_transaction;
 }
コード例 #21
0
 /**
  * @param \BitWasp\Bitcoin\Mnemonic\Bip39\Bip39WordListInterface $wordList
  * @param EcAdapterInterface $ecAdapter
  * @return Bip39Mnemonic
  */
 public static function bip39(Bip39WordListInterface $wordList = null, EcAdapterInterface $ecAdapter = null)
 {
     return new Bip39Mnemonic($ecAdapter ?: Bitcoin::getEcAdapter(), $wordList ?: new Bip39\Wordlist\EnglishWordList());
 }
コード例 #22
0
 /**
  * Takes a key which is assumed to be either the master private key or master public key.
  *
  * @param KeyInterface $key
  * @param EcAdapterInterface $ecAdapter
  * @return ElectrumKey
  */
 public static function fromKey(KeyInterface $key, EcAdapterInterface $ecAdapter = null)
 {
     return new ElectrumKey($ecAdapter ?: Bitcoin::getEcAdapter(), $key);
 }