get_address($public_key)); } elseif ($q == "base58") { /** * @api {get} /api.php?q=base58 03. base58 * @apiName base58 * @apiGroup API * @apiDescription Converts a string to base58. * * @apiParam {string} data Input string * * @apiSuccess {string} data Output string */ api_echo(base58_encode($data['data'])); } elseif ($q == "getBalance") { /** * @api {get} /api.php?q=getBalance 04. getBalance * @apiName getBalance * @apiGroup API * @apiDescription Returns the balance of a specific account or public key. * * @apiParam {string} [public_key] Public key * @apiParam {string} [account] Account id / address * * @apiSuccess {string} data The ARO balance */ $public_key = $data['public_key']; $account = $data['account']; if (!empty($public_key) && strlen($public_key) < 32) { api_err("Invalid public key"); } if (!empty($public_key)) { $account = $acc->get_address($public_key); } if (empty($account)) { api_err("Invalid account id"); } $account = san($account); api_echo($acc->balance($account)); } elseif ($q == "getPendingBalance") { /** * @api {get} /api.php?q=getPendingBalance 05. getPendingBalance * @apiName getPendingBalance * @apiGroup API * @apiDescription Returns the pending balance, which includes pending transactions, of a specific account or public key. * * @apiParam {string} [public_key] Public key * @apiParam {string} [account] Account id / address * * @apiSuccess {string} data The ARO balance */ $account = $data['account']; if (!empty($public_key) && strlen($public_key) < 32) { api_err("Invalid public key"); } if (!empty($public_key)) { $account = $acc->get_address($public_key); } if (empty($account)) { api_err("Invalid account id"); } $account = san($account); api_echo($acc->pending_balance($account)); } elseif ($q == "getTransactions") { /** * @api {get} /api.php?q=getTransactions 06. getTransactions * @apiName getTransactions * @apiGroup API * @apiDescription Returns the latest transactions of an account. * * @apiParam {string} [public_key] Public key * @apiParam {string} [account] Account id / address * @apiParam {numeric} [limit] Number of confirmed transactions, max 1000, min 1 * * @apiSuccess {string} block Block ID * @apiSuccess {numeric} confirmation Number of confirmations * @apiSuccess {numeric} date Transaction's date in UNIX TIMESTAMP format * @apiSuccess {string} dst Transaction destination * @apiSuccess {numeric} fee The transaction's fee * @apiSuccess {numeric} height Block height * @apiSuccess {string} id Transaction ID/HASH * @apiSuccess {string} message Transaction's message * @apiSuccess {string} signature Transaction's signature * @apiSuccess {string} public_key Account's public_key * @apiSuccess {string} src Sender's address * @apiSuccess {string} type "debit", "credit" or "mempool" * @apiSuccess {numeric} val Transaction value * @apiSuccess {numeric} version Transaction version */ $account = san($data['account']); if (!empty($public_key) && strlen($public_key) < 32) { api_err("Invalid public key"); } if (!empty($public_key)) { $account = $acc->get_address($public_key); } if (empty($account)) { api_err("Invalid account id"); } $limit = intval($data['limit']); $transactions = $acc->get_mempool_transactions($account); $transactions = array_merge($transactions, $acc->get_transactions($account, $limit)); api_echo($transactions); } elseif ($q == "getTransaction") { /** * @api {get} /api.php?q=getTransaction 07. getTransaction * @apiName getTransaction * @apiGroup API * @apiDescription Returns one transaction. * * @apiParam {string} transaction Transaction ID * * @apiSuccess {string} block Block ID * @apiSuccess {numeric} confirmation Number of confirmations * @apiSuccess {numeric} date Transaction's date in UNIX TIMESTAMP format * @apiSuccess {string} dst Transaction destination * @apiSuccess {numeric} fee The transaction's fee * @apiSuccess {numeric} height Block height * @apiSuccess {string} id Transaction ID/HASH * @apiSuccess {string} message Transaction's message * @apiSuccess {string} signature Transaction's signature * @apiSuccess {string} public_key Account's public_key * @apiSuccess {string} src Sender's address * @apiSuccess {string} type "debit", "credit" or "mempool" * @apiSuccess {numeric} val Transaction value * @apiSuccess {numeric} version Transaction version */ $id = san($data['transaction']); $res = $trx->get_transaction($id); if ($res === false) { $res = $trx->get_mempool_transaction($id); if ($res === false) { api_err("invalid transaction"); } } api_Echo($res); } elseif ($q == "getPublicKey") { /** * @api {get} /api.php?q=getPublicKey 08. getPublicKey * @apiName getPublicKey * @apiGroup API * @apiDescription Returns the public key of a specific account. * * @apiParam {string} account Account id / address * * @apiSuccess {string} data The public key */ $account = san($data['account']); if (empty($account)) { api_err("Invalid account id"); } $public_key = $acc->public_key($account); if ($public_key === false) { api_err("No public key found for this account"); } else { api_echo($public_key); } } elseif ($q == "generateAccount") { /** * @api {get} /api.php?q=generateAccount 09. generateAccount * @apiName generateAccount * @apiGroup API * @apiDescription Generates a new account. This function should only be used when the node is on the same host or over a really secure network. * * @apiSuccess {string} address Account address * @apiSuccess {string} public_key Public key * @apiSuccess {string} private_key Private key */ $acc = new Account(); $res = $acc->generate_account(); api_echo($res); } elseif ($q == "currentBlock") { /** * @api {get} /api.php?q=currentBlock 10. currentBlock * @apiName currentBlock * @apiGroup API * @apiDescription Returns the current block. * * @apiSuccess {string} id Blocks id * @apiSuccess {string} generator Block Generator * @apiSuccess {numeric} height Height * @apiSuccess {numeric} date Block's date in UNIX TIMESTAMP format * @apiSuccess {string} nonce Mining nonce * @apiSuccess {string} signature Signature signed by the generator * @apiSuccess {numeric} difficulty The base target / difficulty * @apiSuccess {string} argon Mining argon hash */ $current = $block->current(); api_echo($current); } elseif ($q == "getBlock") { /** * @api {get} /api.php?q=getBlock 11. getBlock * @apiName getBlock * @apiGroup API * @apiDescription Returns the block. * * @apiParam {numeric} height Block Height * * @apiSuccess {string} id Block id * @apiSuccess {string} generator Block Generator * @apiSuccess {numeric} height Height * @apiSuccess {numeric} date Block's date in UNIX TIMESTAMP format * @apiSuccess {string} nonce Mining nonce * @apiSuccess {string} signature Signature signed by the generator * @apiSuccess {numeric} difficulty The base target / difficulty * @apiSuccess {string} argon Mining argon hash */ $height = san($data['height']); $ret = $block->get($height); if ($ret == false) { api_err("Invalid block"); } else { api_echo($ret); } } elseif ($q == "getBlockTransactions") { /** * @api {get} /api.php?q=getBlockTransactions 12. getBlockTransactions * @apiName getBlockTransactions * @apiGroup API * @apiDescription Returns the transactions of a specific block. * * @apiParam {numeric} [height] Block Height * @apiParam {string} [block] Block id * * @apiSuccess {string} block Block ID * @apiSuccess {numeric} confirmations Number of confirmations * @apiSuccess {numeric} date Transaction's date in UNIX TIMESTAMP format * @apiSuccess {string} dst Transaction destination * @apiSuccess {numeric} fee The transaction's fee * @apiSuccess {numeric} height Block height * @apiSuccess {string} id Transaction ID/HASH * @apiSuccess {string} message Transaction's message * @apiSuccess {string} signature Transaction's signature * @apiSuccess {string} public_key Account's public_key * @apiSuccess {string} src Sender's address * @apiSuccess {string} type "debit", "credit" or "mempool" * @apiSuccess {numeric} val Transaction value * @apiSuccess {numeric} version Transaction version */ $height = san($data['height']); $block = san($data['block']); $ret = $trx->get_transactions($height, $block); if ($ret === false) { api_err("Invalid block"); } else { api_echo($ret); } } elseif ($q == "version") { /** * @api {get} /api.php?q=version 13. version * @apiName version * @apiGroup API * @apiDescription Returns the node's version. * * * @apiSuccess {string} data Version */ api_echo(VERSION); } elseif ($q == "send") { /** * @api {get} /api.php?q=send 14. send * @apiName send * @apiGroup API * @apiDescription Sends a transaction. * * @apiParam {numeric} val Transaction value (without fees) * @apiParam {string} dst Destination address * @apiParam {string} public_key Sender's public key * @apiParam {string} [signature] Transaction signature. It's recommended that the transaction is signed before being sent to the node to avoid sending your private key to the node. * @apiParam {string} [private_key] Sender's private key. Only to be used when the transaction is not signed locally. * @apiParam {numeric} [date] Transaction's date in UNIX TIMESTAMP format. Requried when the transaction is pre-signed. * @apiParam {string} [message] A message to be included with the transaction. Maximum 128 chars. * @apiParam {numeric} [version] The version of the transaction. 1 to send coins. * * @apiSuccess {string} data Transaction id */ $current = $block->current(); if ($current['height'] > 10790 && $current['height'] < 10810) { api_err("Hard fork in progress. Please retry the transaction later!"); //10800 } $acc = new Account(); $block = new Block(); $trx = new Transaction(); $dst = san($data['dst']); if (!$acc->valid($dst)) { api_err("Invalid destination address"); } $dst_b = base58_decode($dst); if (strlen($dst_b) != 64) { api_err("Invalid destination address"); } $public_key = san($data['public_key']); if (!$acc->valid_key($public_key)) { api_err("Invalid public key"); } $private_key = san($data['private_key']); if (!$acc->valid_key($private_key)) { api_err("Invalid private key"); } $signature = san($data['signature']); if (!$acc->valid_key($signature)) { api_err("Invalid signature"); } $date = $data['date'] + 0; if ($date == 0) { $date = time(); } if ($date < time() - (3600 * 24 * 48)) { api_err("The date is too old"); } if ($date > time() + 86400) { api_err("Invalid Date"); } $version = intval($data['version']); $message = $data['message']; if (strlen($message) > 128) { api_err("The message must be less than 128 chars"); } $val = $data['val'] + 0; $fee = $val * 0.0025; if ($fee < 0.00000001) { $fee = 0.00000001; } if ($fee > 10 && $current['height'] > 10800) { $fee = 10; //10800 } if ($val < 0.00000001) { api_err("Invalid value"); } if ($version < 1) { $version = 1; } $val = number_format($val, 8, '.', ''); $fee = number_format($fee, 8, '.', ''); if (empty($public_key) && empty($private_key)) { api_err("Either the private key or the public key must be sent"); } if (empty($private_key) && empty($signature)) { api_err("Either the private_key or the signature must be sent"); } if (empty($public_key)) { $pk = coin2pem($private_key, true); $pkey = openssl_pkey_get_private($pk); $pub = openssl_pkey_get_details($pkey); $public_key = pem2coin($pub['key']); } $transaction = [ "val" => $val, "fee" => $fee, "dst" => $dst, "public_key" => $public_key, "date" => $date, "version" => $version, "message" => $message, "signature" => $signature, ]; if (!empty($private_key)) { $signature = $trx->sign($transaction, $private_key); $transaction['signature'] = $signature; } $hash = $trx->hash($transaction); $transaction['id'] = $hash; if (!$trx->check($transaction)) { api_err("Transaction signature failed"); } $res = $db->single("SELECT COUNT(1) FROM mempool WHERE id=:id", [":id" => $hash]); if ($res != 0) { api_err("The transaction is already in mempool"); } $res = $db->single("SELECT COUNT(1) FROM transactions WHERE id=:id", [":id" => $hash]); if ($res != 0) { api_err("The transaction is already in a block"); } $src = $acc->get_address($public_key); $transaction['src'] = $src; $balance = $db->single("SELECT balance FROM accounts WHERE id=:id", [":id" => $src]); if ($balance < $val + $fee) { api_err("Not enough funds"); } $memspent = $db->single("SELECT SUM(val+fee) FROM mempool WHERE src=:src", [":src" => $src]); if ($balance - $memspent < $val + $fee) { api_err("Not enough funds (mempool)"); } $trx->add_mempool($transaction, "local"); system("php propagate.php transaction $hash > /dev/null 2>&1 &"); api_echo($hash); } elseif ($q == "mempoolSize") { /** * @api {get} /api.php?q=mempoolSize 15. mempoolSize * @apiName mempoolSize * @apiGroup API * @apiDescription Returns the number of transactions in mempool. * * @apiSuccess {numeric} data Number of mempool transactions */ $res = $db->single("SELECT COUNT(1) FROM mempool"); api_echo($res); } elseif ($q == 'randomNumber') { /** * @api {get} /api.php?q=randomNumber 16. randomNumber * @apiName randomNumber * @apiGroup API * @apiDescription Returns a random number based on an ARO block id. * * @apiParam {numeric} height The height of the block on which the random number will be based on (should be a future block when starting) * @apiParam {numeric} min Minimum number (default 1) * @apiParam {numeric} max Maximum number * @apiParam {string} seed A seed to generate different numbers for each use cases. * @apiSuccess {numeric} data The random number */ $height = san($_GET['height']); $max = intval($_GET['max']); if (empty($_GET['min'])) { $min = 1; } else { $min = intval($_GET['min']); } $blk = $db->single("SELECT id FROM blocks WHERE height=:h", [":h" => $height]); if ($blk === false) { api_err("Unknown block. Future?"); } $base = hash("sha256", $blk.$_GET['seed']); $seed1 = hexdec(substr($base, 0, 12)); // generate random numbers based on the seed mt_srand($seed1, MT_RAND_MT19937); $res = mt_rand($min, $max); api_echo($res); } else { api_err("Invalid request"); }