SolanaService.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * @Created by PhpStorm
  4. * @author: Kirin
  5. * @day: 2024/6/27
  6. * @time: 19:16
  7. */
  8. namespace qiniu\services\blockchain;
  9. use BitWasp\Bitcoin\Base58;
  10. use BitWasp\Buffertools\Buffer;
  11. use think\Exception;
  12. class SolanaService
  13. {
  14. private static $node = 'https://solana-mainnet.core.chainstack.com/867a25e325f29ac68e3b5185af1abb68';
  15. public static function signCheck($sign, $address, $message)
  16. {
  17. if (!is_array($sign)) {
  18. $sign = explode(',', $sign);
  19. }
  20. if (!count($sign) == 64) {
  21. throw new Exception('Sign fail');
  22. }
  23. $sign_str = self::uint8ArrayToByteString($sign);
  24. $sign_buffer = new Buffer($sign_str, 64);
  25. $key_buffer2 = Base58::decode($address);
  26. return sodium_crypto_sign_verify_detached($sign_buffer->getBinary(), $message, $key_buffer2->getBinary());
  27. }
  28. private static function uint8ArrayToByteString(array $uint8Array): string
  29. {
  30. return call_user_func_array('pack', array_merge(array('C*'), $uint8Array));
  31. }
  32. private static function byteStringToUint8Array(string $byteString): array
  33. {
  34. $uint8Array = unpack('C*', $byteString);
  35. return array_values($uint8Array);
  36. }
  37. public static function getTransaction($tx)
  38. {
  39. $data = [
  40. 'jsonrpc' => '2.0',
  41. 'id' => 1,
  42. 'method' => 'getTransaction',
  43. 'params' => [
  44. $tx,
  45. [
  46. 'maxSupportedTransactionVersion' => 0,
  47. 'encoding' => 'json'
  48. ],
  49. ],
  50. ];
  51. $res = do_request(self::$node, [], $data, ['Content-Type: application/json'], true, true);
  52. return json_decode($res, true);
  53. }
  54. }