[강좌] 이더소셜 PHP API 서버 만들기 #8. 트랜잭션 전송하기

in #topmining6 years ago

안녕하세요.
쌩광부입니다.

지난 강좌
https://steemit.com/@topmining
https://www.ddengle.com/@TopMining

전체 소스
https://github.com/topmining/ethersocial-php-api

이번 강좌는 이더소셜 PHP API 서버 만들기의 마지막 강좌인데요.
코인 전송을 위한 트랜잭션 전송 API 입니다.

send_rawtransaction.php 파일을 만들고 아래와 같이 코딩합니다.

<?php
require "json-rpc.php";
require "api-config.php";

header('Content-type: application/json');

$rawdata = array_key_exists('data', $_GET) ? $_GET['data'] : '';

if(strlen($rawdata) > 0) {
  $id = 1;
  $json = getEtherRpc($api_host, $api_port, 'eth_sendRawTransaction', '"'.$rawdata.'"', $id++);

  $data = json_decode($json, true);
  $error = array_key_exists('error', $data) ? $data['error'] : null;

  $result = array();
  if($error) {
    $result['error'] = $error;
  }
  else {
    $txhash = $data["result"];
    $result['error'] = null;
    $result['result']['txhash'] = $txhash;
  }

  echo json_encode($result);
}
else {
  $result = array();
  $result['error']['code'] = '-1';
  $result['error']['message'] = 'Invalid data';

  echo json_encode($result);
}

매우 간단하죠.
rawdata을 받아서 JSON RPC만 호출하면 됩니다.

$rawdata = array_key_exists('data', $_GET) ? $_GET['data'] : '';

GET 인자값으로 rawdata를 읽습니다.
이 rawdata는 트랜잭션 내용을 지정된 규칙에 따라 Hex값으로 변경하고 그것을 개인키로 사인한 Byte 배열입니다.
rawdata에 대한 자세한 내용은 아래 링크를 참고하세요.
https://github.com/ethereum/wiki/wiki/JavaScript-API#web3ethsendrawtransaction

$json = getEtherRpc($api_host, $api_port, 'eth_sendRawTransaction', '"'.$rawdata.'"', $id++);

eth_sendRawTransaction RPC를 호출합니다.
만약 전송에 성공한다면 결과값에는 트랜잭션 해시값이 들어옵니다.
이 해시값을 이용해 블록 익스플로어에서 실제 전송됬는지 여부를 확인할 수 있겠죠.

이렇게 이번 강좌도 끝~~~!

실제 호출하는 과정은 생략하도록 하겠습니다.
차후에 이더소셜 모바일 지갑 만들기 강좌에서 이 API를 이용할 예정인데요.
그때 확인하시면 되겠습니다. ^^

이더소셜 PHP API 서버 만들기 강좌는 모바일 지갑을 만들기 위해서 제작된 API 서버를 소개하기 위한 강좌였었는데요.
조금이나마 이더리움 블록체인 구조를 이해하는데 도움이 되셨으면 좋겠네요.
곧 모바일 지갑을 만들기 강좌도 다시 이어질 예정인데요.
많은 응원 부탁드립니다.
감사합니다.

Sort:  

Congratulations @topmining! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!