本文整理汇总了PHP中BitWasp\Bitcoin\Bitcoin::getMath方法的典型用法代码示例。如果您正苦于以下问题:PHP Bitcoin::getMath方法的具体用法?PHP Bitcoin::getMath怎么用?PHP Bitcoin::getMath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BitWasp\Bitcoin\Bitcoin
的用法示例。
在下文中一共展示了Bitcoin::getMath方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: services
public function services($int)
{
$math = Bitcoin::getMath();
$hex = $math->decHex($int);
$buffer = Buffer::hex($hex, 8);
return $buffer;
}
示例2: execute
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$config = (new ConfigLoader())->load();
// Create the child process
// All the code after pcntl_fork () will be performed by two processes: parent and child
if ($config->getItem('config', 'daemon', false)) {
$child_pid = pcntl_fork();
if ($child_pid) {
// Exit from the parent process that is bound to the console
exit;
}
// Make the child as the main process.
posix_setsid();
}
$math = Bitcoin::getMath();
$params = new Params($math);
$loop = \React\EventLoop\Factory::create();
$db = new DebugDb(Db::create($config));
$node = new BitcoinNode($config, $params, $db);
$container = new Container();
$container['debug'] = function (Container $c) use($node) {
$context = $c['zmq'];
return new ZmqDebug($node, $context);
};
$this->setupServices($container, $node, $loop, $config, $db);
$loop->run();
return 0;
}
示例3: fromHex
/**
* @param $string
* @param Math $math
* @return TransactionSignatureInterface
*/
public static function fromHex($string, Math $math = null)
{
$math = $math ?: Bitcoin::getMath();
$serializer = new TransactionSignatureSerializer(new DerSignatureSerializer($math));
$signature = $serializer->parse($string);
return $signature;
}
示例4: __construct
public function __construct(NodeInterface $node, Container $container)
{
$this->db = $container['db'];
$this->retargetDb = new RetargetDb($this->db->getPdo());
$this->math = Bitcoin::getMath();
$this->consensus = new Consensus(Bitcoin::getMath(), new Params(Bitcoin::getMath()));
$node->chains()->on('retarget', [$this, 'onRetarget']);
}
示例5: calculate
/**
* Calculate the hash of the current transaction, when you are looking to
* spend $txOut, and are signing $inputToSign. The SigHashType defaults to
* SIGHASH_ALL, though SIGHASH_SINGLE, SIGHASH_NONE, SIGHASH_ANYONECANPAY
* can be used.
*
* @param ScriptInterface $txOutScript
* @param $inputToSign
* @param int $sighashType
* @return Buffer
* @throws \Exception
*/
public function calculate(ScriptInterface $txOutScript, $inputToSign, $sighashType = SignatureHashInterface::SIGHASH_ALL)
{
$copy = $this->transaction->makeCopy();
$inputs = $copy->getInputs();
$outputs = $copy->getOutputs();
if ($inputToSign > count($inputs)) {
throw new \Exception('Input does not exist');
}
// Default SIGHASH_ALL procedure: null all input scripts
$inputCount = count($inputs);
for ($i = 0; $i < $inputCount; $i++) {
$inputs->getInput($i)->setScript(new Script());
}
$inputs->getInput($inputToSign)->setScript($txOutScript);
$math = Bitcoin::getMath();
if ($math->bitwiseAnd($sighashType, 31) == SignatureHashInterface::SIGHASH_NONE) {
// Set outputs to empty vector, and set sequence number of inputs to 0.
$copy->setOutputs(new TransactionOutputCollection());
// Let the others update at will. Set sequence of inputs we're not signing to 0.
$inputCount = count($inputs);
for ($i = 0; $i < $inputCount; $i++) {
if ($math->cmp($i, $inputToSign) !== 0) {
$inputs->getInput($i)->setSequence(0);
}
}
} elseif ($math->bitwiseAnd($sighashType, 31) == SignatureHashInterface::SIGHASH_SINGLE) {
// Resize output array to $inputToSign + 1, set remaining scripts to null,
// and set sequence's to zero.
$nOutput = $inputToSign;
if ($math->cmp($nOutput, count($outputs)) >= 0) {
return Buffer::hex('0100000000000000000000000000000000000000000000000000000000000000');
}
// Resize..
$outputs = $outputs->slice(0, $nOutput + 1)->getOutputs();
// Set to null
for ($i = 0; $i < $nOutput; $i++) {
$outputs[$i] = new TransactionOutput($math->getBinaryMath()->getTwosComplement(-1, 64), new Script());
}
$copy->setOutputs(new TransactionOutputCollection($outputs));
// Let the others update at will. Set sequence of inputs we're not signing to 0.
$inputCount = count($inputs);
for ($i = 0; $i < $inputCount; $i++) {
if ($math->cmp($i, $inputToSign) != 0) {
$inputs->getInput($i)->setSequence(0);
}
}
}
// This can happen regardless of whether it's ALL, NONE, or SINGLE
if ($math->bitwiseAnd($sighashType, SignatureHashInterface::SIGHASH_ANYONECANPAY)) {
$input = $inputs->getInput($inputToSign);
$copy->setInputs(new TransactionInputCollection([$input]));
}
// Serialize the TxCopy and append the 4 byte hashtype (little endian);
$txParser = new Parser($copy->getBuffer());
$txParser->writeInt(4, $sighashType, true);
return Hash::sha256d($txParser->getBuffer());
}
示例6: execute
/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$math = Bitcoin::getMath();
$params = new Params($math);
$loop = \React\EventLoop\Factory::create();
$app = new BitcoinNode($params, $loop);
$app->start();
$loop->run();
return 0;
}
示例7: testGetValueIn
public function testGetValueIn()
{
$utxo1 = new Utxo(new OutPoint(new Buffer('a', 32), 0), new TransactionOutput(2, new Script()));
$utxo2 = new Utxo(new OutPoint(new Buffer('a', 32), 1), new TransactionOutput(4, new Script()));
$utxo3 = new Utxo(new OutPoint(new Buffer('b', 32), 0), new TransactionOutput(1, new Script()));
$view = new UtxoView([$utxo1, $utxo2, $utxo3]);
$transaction = TransactionFactory::build()->spendOutPoint($utxo1->getOutPoint())->spendOutPoint($utxo2->getOutPoint())->spendOutPoint($utxo3->getOutPoint())->output(5, new Script())->get();
$this->assertEquals(7, $view->getValueIn(Bitcoin::getMath(), $transaction));
$this->assertEquals(2, $view->getFeePaid(Bitcoin::getMath(), $transaction));
}
示例8: __construct
/**
* Forks constructor.
* @param ParamsInterface $params
* @param BlockIndexInterface $index
* @param array[] $versions
*/
public function __construct(ParamsInterface $params, BlockIndexInterface $index, array $versions)
{
$this->versionCount = ['v1' => 0, 'v2' => 0, 'v3' => 0, 'v4' => 0, 'v5' => 0];
foreach ($versions as $value) {
$this->updateCount($value);
}
$this->math = Bitcoin::getMath();
$this->params = $params;
$this->index = $index;
$this->versions = $versions;
$this->update();
}
示例9: bitsToBuffers
/**
* @param array $bits
* @return array
*/
private function bitsToBuffers(array $bits)
{
$math = Bitcoin::getMath();
$vBuffers = str_split(str_pad('', (count($bits) + 7) / 8, '0', STR_PAD_LEFT));
$nBits = count($bits);
for ($p = 0; $p < $nBits; $p++) {
$index = (int) floor($p / 8);
$vBuffers[$index] |= $bits[$p] << $p % 8;
}
foreach ($vBuffers as &$value) {
$value = Buffer::int($value, null, $math);
}
unset($value);
return $vBuffers;
}
示例10: calculate
/**
* Calculate the hash of the current transaction, when you are looking to
* spend $txOut, and are signing $inputToSign. The SigHashType defaults to
* SIGHASH_ALL, though SIGHASH_SINGLE, SIGHASH_NONE, SIGHASH_ANYONECANPAY
* can be used.
*
* @param ScriptInterface $txOutScript
* @param int $inputToSign
* @param int $sighashType
* @return BufferInterface
* @throws \Exception
*/
public function calculate(ScriptInterface $txOutScript, $inputToSign, $sighashType = SigHash::ALL)
{
$math = Bitcoin::getMath();
$tx = new TxMutator($this->transaction);
$inputs = $tx->inputsMutator();
$outputs = $tx->outputsMutator();
// Default SIGHASH_ALL procedure: null all input scripts
foreach ($inputs as $input) {
$input->script(new Script());
}
$inputs[$inputToSign]->script($txOutScript);
if ($math->cmp($math->bitwiseAnd($sighashType, 31), SigHash::NONE) === 0) {
// Set outputs to empty vector, and set sequence number of inputs to 0.
$outputs->null();
// Let the others update at will. Set sequence of inputs we're not signing to 0.
foreach ($inputs as $i => $input) {
if ($i !== $inputToSign) {
$input->sequence(0);
}
}
} elseif ($math->cmp($math->bitwiseAnd($sighashType, 31), SigHash::SINGLE) === 0) {
// Resize output array to $inputToSign + 1, set remaining scripts to null,
// and set sequence's to zero.
$nOutput = $inputToSign;
if ($nOutput >= $this->nOutputs) {
return Buffer::hex('0100000000000000000000000000000000000000000000000000000000000000', 32, $math);
}
// Resize, set to null
$outputs->slice(0, $nOutput + 1);
for ($i = 0; $i < $nOutput; $i++) {
$outputs[$i]->null();
}
// Let the others update at will. Set sequence of inputs we're not signing to 0
foreach ($inputs as $i => $input) {
if ($i !== $inputToSign) {
$input->sequence(0);
}
}
}
// This can happen regardless of whether it's ALL, NONE, or SINGLE
if ($math->cmp($math->bitwiseAnd($sighashType, SigHash::ANYONECANPAY), 0) > 0) {
$input = $inputs[$inputToSign]->done();
$inputs->null()->add($input);
}
return Hash::sha256d(Buffertools::concat($tx->done()->getBuffer(), Buffertools::flipBytes(Buffer::int($sighashType, 4, $math))));
}
示例11: decode
/**
* Decode a base58 string
*
* @param string $base58
* @return BufferInterface
*/
public static function decode($base58)
{
$math = Bitcoin::getMath();
if ($base58 === '') {
return new Buffer('', 0, $math);
}
$original = $base58;
$length = strlen($base58);
$return = '0';
for ($i = 0; $i < $length; $i++) {
$return = $math->add($math->mul($return, 58), strpos(self::$base58chars, $base58[$i]));
}
$binary = $math->cmp($return, '0') === 0 ? '' : hex2bin($math->decHex($return));
for ($i = 0; $i < $length && $original[$i] === '1'; $i++) {
$binary = "" . $binary;
}
return new Buffer($binary);
}
示例12: decode
/**
* Decode a base58 string
*
* @param $base58
* @return Buffer
*/
public static function decode($base58)
{
if (strlen($base58) == 0) {
return new Buffer();
}
$original = $base58;
$strlen = strlen($base58);
$return = '0';
$math = Bitcoin::getMath();
for ($i = 0; $i < $strlen; $i++) {
$return = $math->add($math->mul($return, 58), strpos(self::$base58chars, $base58[$i]));
}
$hex = $return == '0' ? '' : $math->decHex($return);
for ($i = 0; $i < $strlen && $original[$i] == "1"; $i++) {
$hex = "00" . $hex;
}
$buffer = Buffer::hex($hex);
return $buffer;
}
示例13: buffer
/**
* @param BufferInterface $vch
* @param bool $fRequireMinimal
* @param int $maxNumSize
* @param Math|null $math
* @return self
*/
public static function buffer(BufferInterface $vch, $fRequireMinimal, $maxNumSize = self::MAX_NUM_SIZE, Math $math = null)
{
$size = $vch->getSize();
if ($size > $maxNumSize) {
throw new \RuntimeException('Script number overflow');
}
if ($fRequireMinimal && $size > 0) {
$binary = $vch->getBinary();
if (ord($binary[$size - 1]) & 0x7f === 0) {
if ($size <= 1 || ord($binary[$size - 2]) & 0x80 === 0) {
throw new \RuntimeException('Non-minimally encoded script number');
}
}
}
$math = $math ?: Bitcoin::getMath();
$number = new self(0, $math);
$number->number = $number->parseBuffer($vch);
return $number;
}
示例14: __construct
public function __construct()
{
$this->haveTx = [];
$this->math = Bitcoin::getMath();
$this->generator = Bitcoin::getGenerator();
$this->adapter = EcAdapterFactory::getPhpEcc($this->math, $this->generator);
$this->order = $this->adapter->getGenerator()->getOrder();
$this->loop = \React\EventLoop\Factory::create();
$factory = new \BitWasp\Bitcoin\Networking\Factory($this->loop);
$dns = $factory->getDns();
$peerFactory = $factory->getPeerFactory($dns);
$locator = $peerFactory->getLocator();
$server = new ReactServer($this->loop);
$listener = $peerFactory->getListener($server);
$this->manager = $peerFactory->getManager(true);
$this->manager->registerListener($listener);
$this->manager->on('outbound', function (Peer $peer) {
$this->setupPeer($peer);
});
$this->manager->on('inbound', function (Peer $peer) {
$this->setupPeer($peer);
});
$locator->queryDnsSeeds()->then(function (Locator $locator) {
$this->manager->connectToPeers($locator, 5);
$this->loop->addPeriodicTimer(30, function () {
echo "Have seen " . $this->inputs . " inputs and " . $this->counter . " high-S signatures \n";
echo "There are " . count($this->violators) . " violators \n";
$largest = 0;
$worstPeer = null;
foreach ($this->violators as $ip => $v) {
if ($v > $largest) {
$worstPeer = $ip;
$largest = $v;
}
}
if (!is_null($worstPeer)) {
echo "Worst peer: {$worstPeer} ({$largest})\n";
}
});
echo "Connecting..\n";
});
}
示例15: init
/**
* @param BlockInterface $genesisBlock
*/
public function init(BlockInterface $genesisBlock)
{
$hash = $genesisBlock->getHeader()->getHash();
$index = $this->db->fetchIndex($hash);
try {
$this->db->fetchBlock($hash);
} catch (\Exception $e) {
echo $e->getMessage() . PHP_EOL;
$this->db->insertBlock($index->getHash(), $genesisBlock, new BlockSerializer(Bitcoin::getMath(), new BlockHeaderSerializer(), new TransactionSerializer()));
}
}