Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Sep 3rd, 2016 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. // ------
  4. // CONFIG
  5. // ------
  6. $version      = 60002;
  7. $node         = array('85.119.83.25', 8333); // node you want to connect to
  8. $local        = array('90.213.66.225', 8333); // this node
  9. $start_height = 0; // starting block height for this node
  10.  
  11. list($node_ip, $node_port) = $node;
  12. list($local_ip, $local_port) = $local;
  13.  
  14. echo 'Config'.PHP_EOL.'------'.PHP_EOL;
  15. echo 'version:      '.$version.PHP_EOL;
  16. echo 'node:         '.implode($node, ':').PHP_EOL;
  17. echo 'local:        '.implode($local, ':').PHP_EOL;
  18. echo 'start_height: '.$start_height.PHP_EOL.PHP_EOL;
  19.  
  20.  
  21. // ------------------
  22. // 1. VERSION MESSAGE
  23. // ------------------
  24.  
  25. // General Functions
  26. function fieldsize($field, $bytes = 1) {
  27.     $length = $bytes * 2;
  28.     $result = str_pad($field, $length, '0', STR_PAD_LEFT);
  29.     return $result;
  30. }
  31.  
  32. function swapEndian($hex) {
  33.     return implode('', array_reverse(str_split($hex, 2)));
  34. }
  35.  
  36. function byteSpaces($bytes) { // add spaces between bytes
  37.     $bytes = implode(str_split(strtoupper($bytes), 2), ' ');
  38.     return $bytes;
  39. }
  40.  
  41. // Version Message Functions
  42. function timestamp($time) { // convert timestamp to network byte order
  43.     $time = dechex($time);
  44.     $time = fieldsize($time, 8);
  45.     $time = swapEndian($time);
  46.     return byteSpaces($time);
  47. }
  48.  
  49. function networkaddress($ip, $port = '8333') { // convert ip address to network byte order
  50.     $services = '01 00 00 00 00 00 00 00'; // 1 = NODE_NETWORK
  51.    
  52.     $ipv6_prefix = '00 00 00 00 00 00 00 00 00 00 FF FF';
  53.    
  54.     $ip = explode('.', $ip);
  55.     $ip = array_map("dechex", $ip);
  56.     $ip = array_map("fieldsize", $ip);
  57.     $ip = array_map("strtoupper", $ip);
  58.     $ip = implode($ip, ' ');
  59.    
  60.     $port = dechex($port); // for some fucking reason this is big-endian
  61.     $port = byteSpaces($port);
  62.    
  63.     return "$services $ipv6_prefix $ip $port";
  64. }
  65.  
  66. function checksum($string) {
  67.     $string = hex2bin($string);
  68.     $hash = hash('sha256', hash('sha256', $string, true));
  69.     $checksum = substr($hash, 0, 8);
  70.     return byteSpaces($checksum);
  71. }
  72.  
  73.  
  74. // VERSION MESSAGE
  75. $version = bytespaces(swapEndian(fieldsize(dechex($version), 4)));
  76. $timestamp = timestamp(time()); // 73 43 c9 57 00 00 00 00
  77. $recv = networkaddress($node_ip, $node_port);
  78. $from = networkaddress($local_ip, $local_port);
  79. $nonce = bytespaces(swapEndian(fieldsize(dechex(mt_rand()), 8)));
  80. $start_height = bytespaces(swapEndian(fieldsize(dechex($start_height), 4)));
  81.  
  82. $version_array = [ // hexadecimal, network byte order
  83.     'version'       => $version,                    // 4 bytes (60002)
  84.     'services'      => '01 00 00 00 00 00 00 00',   // 8 bytes (1 = NODE_NETORK)
  85.     'timestamp'     => $timestamp,                  // 8 bytes
  86.     'addr_recv'     => $recv,                       // 26 bytes
  87.     'addr_from'     => $from,                       // 26 bytes
  88.     'nonce'         => $nonce,                      // 8 bytes
  89.     'user_agent'    => '00',                        // varint
  90.     'start_height'  => $start_height                // 4 bytes
  91. ];
  92.  
  93. $version_message = str_replace(' ', '', implode($version_array));
  94.  
  95. // VERSION MESSAGE HEADER
  96. $payload = swapEndian(fieldsize(dechex(strlen($version_message) / 2), 4));
  97. $checksum = checksum($version_message);
  98.  
  99. $header_array = [
  100.     'magicbytes'    => 'F9 BE B4 D9',
  101.     'command'       => '76 65 72 73 69 6F 6E 00 00 00 00 00',
  102.     'payload_size'  => $payload,
  103.     'checksum'      => $checksum,
  104. ];
  105.  
  106. $message_header = str_replace(' ', '', implode($header_array));
  107.  
  108.  
  109. // Print Results
  110. echo PHP_EOL.'VERSION MESSAGE'.PHP_EOL.'---------------'.PHP_EOL;
  111. echo 'Header:'; print_r ($header_array).PHP_EOL;
  112. echo 'Payload:'; print_r ($version_array).PHP_EOL;
  113.  
  114. $message = $message_header.$version_message;
  115. echo PHP_EOL.'Serialized:'.PHP_EOL.$message.PHP_EOL.PHP_EOL;
  116.  
  117.  
  118. // -----------------
  119. // 2. SOCKET CONNECT
  120. // -----------------
  121.  
  122. // Print socket error
  123. function error() {
  124.     $error = socket_strerror(socket_last_error());
  125.     return $error.PHP_EOL;
  126. }
  127.  
  128.  
  129. // HANDSHAKE
  130.  
  131. echo PHP_EOL.'SOCKET'.PHP_EOL.'------'.PHP_EOL;
  132.  
  133. $socket = socket_create(AF_INET, SOCK_STREAM, 6); // IPv4, TCP uses this type, TCP protocol
  134. socket_connect($socket, $node_ip, $node_port);              echo "Connect: ".error();
  135. socket_send($socket, $message, strlen($message) / 2, 0);    echo "Send:    ".error();
  136. socket_recv($socket, $buf, 84, MSG_WAITALL);                echo "Receive: ".error();
  137.  
  138. var_dump($buf);
  139.  
  140.  
  141. // https://en.bitcoin.it/wiki/Protocol_documentation
  142. // https://coinlogic.wordpress.com/2014/03/09/the-bitcoin-protocol-4-network-messages-1-version/
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top