当我用PHP编写半分散式加密货币时。(第2部分-开发)

前言


在之前的一篇文章中,我谈到了哪些库对我们有用,并将这种想法与失败的库进行了比较。在这一部分中,我们将开发加密货币本身并配置跟踪器。我们不要犹豫,走吧。

文章清单


  1. 当我用PHP编写半分散式加密货币时。(第1部分-馆藏)
  2. 当我用PHP编写半分散式加密货币时。(第2部分-开发)

追踪器安装


在开始安装Web跟踪器之前,我将告诉您-该库支持两种类型的跟踪器-本机和Web。Native-tracker可以在各种VDS上使用,它们将轻松维护网络上的用户列表。
在这里,我将使用可以轻松安装在站点上的Web跟踪器。
我们转到上一部分中讨论过的GitHub存储库。主分支对我们不感兴趣,我们选择网络。使用此线程下载档案,将其解压缩并在索引板或您喜欢的其他地方打开index.php文件。这是文件的内容:

index.php
<?php

/**
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * @package     Bit Points Network
 * @copyright   2020 Podvirnyy Nikita (Observer KRypt0n_)
 * @license     GNU GPLv3 <https://www.gnu.org/licenses/gpl-3.0.html>
 * @author      Podvirnyy Nikita (Observer KRypt0n_)
 * 
 * Contacts:
 *
 * Email: <suimin.tu.mu.ga.mi@gmail.com>
 * VK:    <https://vk.com/technomindlp>
 *        <https://vk.com/hphp_convertation>
 * 
 */

namespace BPN;

const TRACKER_KEY = 'EFJI#*$&(*#WEF(@Q#)(DFJSAO(@#*$)REFSKE)UJ*#@(&$';

require 'ext/DH-Generator/Generator.php';
require 'php/User.php';
require 'php/Tracker.php';

$tracker = new Tracker;
$tracker->update ();

if (isset ($_GET['r']))
    $tracker->processRequest ($_GET['r']);

您必须将TRACKER_KEY常量更改为许多不同的字符,以便它与原始文件不匹配。

这么说吧:


const TRACKER_KEY = 'sdT*&Y&*YO*&T*OGTGYFIYYUFGKUHLIYIj9sfhysdphphdpsfhBLABLABLAhsidlfhlisd';

这只是一个例子。您必须以自己的方式进行更改。我们将其全部发布到主机上。Web跟踪器设置完成。

这个追踪器是做什么用的?

发展历程


一般


由于这是我们的应用程序,因此您需要连接所有软件包和组件,即Composer和非Composer软件包,建立帐户结构和帐户本地数据库,在应用程序和结构之间进行交互,并初始化应用程序本身。

General.php
<?php

declare (strict_types=1);

require_once __DIR__ . '/vendor/autoload.php'; #   

require_once 'PHP/Structure.php';              # 
require_once 'PHP/UserAPI.php';                # 

require_once 'NotComposer/vendor/autoload.php';#  - 

require_once 'PHP/App.php';                    # 


BIP39的库要求严格输入;我们在代码的第一行中对其进行声明。
BIP39和BIP44库本身已随Composer的标准autoload.php文件加载。
我们将在Structure.php中声明数据结构。

使用钱包的功能将在UserAPI.php文件中。

BPN和phpblockchain库与Composer不兼容,我们创建一个名称类似于Composer的文件。

我们从App.php中取出应用程序本身-这将是那里最后一个连接的文件,因为我们必须首先从上方加载所有内容。

NotComposer /供应商/ autoload.php


我已经说过,这两个库与Composer不兼容,有些文件不是它们本机的,或者根本不存在,而Packagist也不存在。您必须手动下载它们并连接它们。该文件是为此目的而设计的。

结构体


此处的数据结构旨在简化处理钱包的过程。UserAPI.php需要使用该库来处理电子钱包,即注册和还原电子钱包,而App.php需要使用该库来使用这些电子钱包。
Structure.php
<?php

class Account
{
	public $address;
	public $publicKey;
	protected $privateKey;

	public function getPrivateKey ()
	{
		return $this->privateKey;
	}

	public function setPrivateKey ($key)
	{
		$this->privateKey = $key;
	}
}

class AccountChain
{
	public $accounts;
}


原始代码,对不对?

UserAPI.php


Structure.php和App.php之间的中介是UserAPI.php。通过它,我们已经可以使用钱包,注册新钱包并还原现有的钱包。

该文件的代码:

UserAPI.php
<?php

use \FurqanSiddiqui\BIP39\BIP39;
use BIP\BIP44;

class User
{
	public $account;

	public function registerNewAccount ($password)
	{
		$mnemonic = BIP39::Generate (18);
		$seed = $mnemonic->generateSeed ($password, 256);

		$HDKey = BIP44::fromMasterSeed ($seed)->derive ("m/44'/0'/0'/0/1");

		$newAccount = new Account;
		$newAccount->address = '1x' . hash ('ripemd160', hash ('sha512', $HDKey->publicKey));
		$newAccount->publicKey = $HDKey->publicKey;
		$newAccount->setPrivateKey ($HDKey->privateKey);

		$accountChain = new AccountChain;
		$accountChain->accounts = [];
		$accountChain->accounts[] = $newAccount;
		
		return ['words' => $mnemonic->words, 'chain' => $accountChain];
	}

	public function registerNewAccountInChain (&$chain, $password, $prevAccountMnemonic)
	{
		$mnemonic = BIP39::Words ($prevAccountMnemonic);
		$seed = $mnemonic->generateSeed ($password, 256);

		$accountsCount = sizeof ($chain->accounts) + 1;

		$HDKey = BIP44::fromMasterSeed ($seed)->derive ("m/44'/0'/0'/0/{$accountsCount}");

		$newAccount = new Account;
		$newAccount->address = '1x' . hash ('ripemd160', hash ('sha512', $HDKey->publicKey));
		$newAccount->publicKey = $HDKey->publicKey;
		$newAccount->setPrivateKey ($HDKey->privateKey);

		$chain->accounts[] = $newAccount;

		return ['chain' => $chain];
	}

	public function restoreAccount ($mnemonic, $password, $accountNo)
	{
		$mnemonic = BIP39::Words ($mnemonic);
		$seed = $mnemonic->generateSeed ($password, 256);

		$HDKey = BIP44::fromMasterSeed ($seed)->derive ("m/44'/0'/0'/0/{$accountNo}");

		$newAccount = new Account;
		$newAccount->address = '1x' . hash ('ripemd160', hash ('sha512', $HDKey->publicKey));
		$newAccount->publicKey = $HDKey->publicKey;
		$newAccount->setPrivateKey ($HDKey->privateKey);

		$accountChain = new AccountChain;
		$accountChain->accounts = [];
		$accountChain->accounts[] = $newAccount;

		return $accountChain;
	}
}


该应用程序


我们客户的逻辑。她负责所有工作,包括作为矿工,保持平衡,发送交易和自制交易。

App.php
所以停下来,我们已经谈论了很多。在第3部分-逻辑中,我们将讨论程序逻辑代码。

总结一下


我们谈论了很多事情,关于结构,关于钱包和很多事情。在下一部分中,我们将继续显示货币代码。

TL; DR
— — FlyCoin FLC

All Articles