Created
October 1, 2020 16:48
-
-
Save fuyuton/79f782dfc247c756a107d4dd3cd2df48 to your computer and use it in GitHub Desktop.
bitcoin-phpからmonapartyを触る方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//脇Pサーバー | |
define("API_ENDPOINT", "https://monapa.electrum-mona.org/_api/"); | |
define("SECRET_KEY", ""); | |
require 'vendor/autoload.php'; | |
use BitWasp\Bitcoin\Bitcoin as bitcoin; | |
use BitWasp\Bitcoin\Network\NetworkFactory as networkfactory; | |
use BitWasp\Bitcoin\Key\Factory\PrivateKeyFactory as privatekeyfactory; | |
use BitWasp\Bitcoin\Script\ScriptFactory as scriptfactory; | |
use BitWasp\Bitcoin\Transaction\Factory\Signer as signer; | |
use BitWasp\Bitcoin\Transaction\TransactionFactory as transactionfactory; | |
use BitWasp\Bitcoin\Transaction\TransactionBuilder as transactionbuilder; | |
use BitWasp\Bitcoin\Transaction\TransactionOutput as transactionoutput; | |
class MonaParty | |
{ | |
function __construct($_addr = "") | |
{ | |
$this->addr = $_addr; | |
$this->secret_key = SECRET_KEY; | |
$this->counterPartyFlg = true; | |
$this->debug = false; | |
} | |
function getBalance($address = '1NFeBp9s5aQ1iZ26uWyiK2AYUXHxs7bFmB') | |
{ | |
$method = "get_chain_address_info"; | |
$params = array(); | |
$params["addresses"] = [$address]; | |
$params["with_uxtos"] = "True"; | |
$params['with_last_txn_hashes'] = "4"; | |
$result = json_decode($this->getAPI($method, $params, true), true); | |
return $result; | |
} | |
function getTokenBalance($address) | |
{ | |
$method = "get_normalized_balances"; | |
$params = array(); | |
$params["addresses"] = [$address]; | |
$result = json_decode($this->getAPI($method, $params, true), true); | |
return $result["result"]; | |
} | |
function createTx($destination, $asset, $quantity, $source = "", $memo = "") | |
{ | |
$method = "create_send"; | |
$params = array(); | |
//watanabe単位で | |
//1kbあたりなので、1バイトあたりの金額を1000倍にする | |
$params["fee_per_kb"] = 110 * 1000; | |
$params["encoding"] = "auto"; | |
$params["source"] = $source; | |
//$params["extended_tx_info"] = true; | |
$params["destination"] = $destination; | |
$params["asset"] = $asset; | |
//int型に明示的にキャストしないとAPIでエラーになる | |
$params["quantity"] = intval($quantity * 100000000); | |
$params["memo"] = $memo; | |
$params["use_enhanced_send"] = true; | |
$result = json_decode($this->getAPIByProxy($method, $params), true); | |
return $result["result"]; | |
} | |
function sign($unsigned_tx) | |
{ | |
/** | |
* http://bitcoin.stackexchange.com/questions/46764/sign-transaction-hex-with-php-library | |
**/ | |
$tx = transactionfactory::fromHex($unsigned_tx); | |
$transactionOutputs = []; | |
foreach ($tx->getInputs() as $idx => $input){ | |
var_dump("<br>input<br>"); | |
var_dump($input); | |
$transactionOutput = new transactionoutput(0, scriptfactory::fromHex($input->getScript()->getBuffer()->getHex())); | |
var_dump("<br>scriptfactory<br>"); | |
var_dump(scriptfactory::fromHex($input->getScript()->getBuffer()->getHex())); | |
var_dump("<br>transaction Output<br>"); | |
var_dump($transactionOutput); | |
array_push($transactionOutputs, $transactionOutput); | |
} | |
$monacoin = new Bitcoin(); | |
$monacoin_network = networkfactory::monacoin(); | |
$monacoin->setNetwork($monacoin_network); | |
$ecadapter = $monacoin->getEcAdapter(); | |
// privateKeyFactoryの使い方が新バージョンでは少し変わっていました。 | |
$privateKeyFactory = new privatekeyfactory($ecadapter); | |
$priv = $privateKeyFactory->fromWif($this->secret_key, $monacoin_network); | |
//signer(tx, ecadapter, counterparty_flg); ここにcounterparty用のflagを作りました | |
$signer = new signer($tx, $monacoin->getEcAdapter(), $this->counterPartyFlg); | |
foreach ($transactionOutputs as $idx => $transactionOutput){ | |
$signer->sign($idx, $priv, $transactionOutput); | |
} | |
$signed = $signer->get(); | |
$signed_tx = $signed->getHex(); | |
return $signed_tx; | |
} | |
function broadcast($signed_tx) | |
{ | |
if ($this->debug) { | |
$this->pr("デバッグモードなのでブロードキャストはしない", true); | |
return null; | |
} else { | |
$method = "broadcast_tx"; | |
$params = array(); | |
$params["signed_tx_hex"] = $signed_tx; | |
$result = json_decode($this->getAPIByPost($method, $params), true); | |
var_dump("broadcast result"); | |
var_dump($result); | |
} | |
return $result["result"]; | |
} | |
function getAPIByProxy($method, $params_proxy="") | |
{ | |
if(!is_array($params_proxy)) $params_proxy = array(); | |
$params = array(); | |
$params["method"] = $method; | |
$params["params"] = $params_proxy; | |
return $this->getAPIByPost("proxy_to_counterpartyd", $params); | |
} | |
function getAPIByPost($method, $params="") | |
{ | |
return $this->getAPI($method, $params, true); | |
} | |
function getAPI($method, $params="", $post_flg = false) | |
{ | |
if(!is_array($params)) $params = array(); | |
$datas = array(); | |
$datas["jsonrpc"] = "2.0"; | |
$datas["id"] = 0; | |
$ch = curl_init(API_ENDPOINT); | |
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // -k | |
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE); // --data-binary | |
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json;charset=utf-8', 'User-Agent: curl']); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); | |
$datas["method"] = $method; | |
$datas["params"] = $params; | |
if($post_flg) | |
{ | |
curl_setopt($ch, CURLOPT_POST, TRUE); //POST | |
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($datas)); | |
} | |
$response = curl_exec($ch); | |
curl_close($ch); | |
return $response; | |
} | |
function pr($param) | |
{ | |
echo "<pre>"; | |
print_r($param); | |
echo "</pre>"; | |
} | |
} | |
$address = ""; | |
$dest_address = ""; | |
$mona = new MonaParty(); | |
$balance = $mona->getBalance($address); | |
var_dump("<br>balance<br>"); | |
var_dump($balance); | |
$balance = $mona->getTokenBalance($address); | |
var_dump("<br>token_balance<br>"); | |
var_dump($balance); | |
$unsigned_tx = $mona->createTx($dest_address, "XMP", 1, $address); | |
var_dump("<br>unsigned_tx<br>"); | |
var_dump($unsigned_tx); | |
$signed_tx = $mona->sign($unsigned_tx); | |
var_dump("<br>signed_tx<br>"); | |
var_dump($signed_tx); | |
$txid = $mona->broadcast($signed_tx); | |
var_dump("<br>txid<br>"); | |
var_dump($txid); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment