This repository has been archived by the owner on May 1, 2024. It is now read-only.
generated from tighten/php-package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from exzachlyvv/main
Port of Javascript Solana library
- Loading branch information
Showing
37 changed files
with
2,821 additions
and
126 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk; | ||
|
||
use Tighten\SolanaPhpSdk\Util\Buffer; | ||
use Tighten\SolanaPhpSdk\Util\HasPublicKey; | ||
use Tighten\SolanaPhpSdk\Util\HasSecretKey; | ||
|
||
class Account implements HasPublicKey, HasSecretKey | ||
{ | ||
protected Keypair $keypair; | ||
|
||
/** | ||
* @param $secretKey | ||
*/ | ||
public function __construct($secretKey = null) | ||
{ | ||
if ($secretKey) { | ||
$secretKeyString = Buffer::from($secretKey)->toString(); | ||
|
||
$this->keypair = Keypair::fromSecretKey($secretKeyString); | ||
} else { | ||
$this->keypair = Keypair::generate(); | ||
} | ||
} | ||
|
||
/** | ||
* @return PublicKey | ||
*/ | ||
public function getPublicKey(): PublicKey | ||
{ | ||
return $this->keypair->getPublicKey(); | ||
} | ||
|
||
/** | ||
* @return Buffer | ||
*/ | ||
public function getSecretKey(): Buffer | ||
{ | ||
return $this->keypair->getSecretKey(); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk; | ||
|
||
use Tighten\SolanaPhpSdk\Exceptions\AccountNotFoundException; | ||
use Tighten\SolanaPhpSdk\Util\Commitment; | ||
|
||
class Connection extends Program | ||
{ | ||
/** | ||
* @param string $pubKey | ||
* @return array | ||
*/ | ||
public function getAccountInfo(string $pubKey): array | ||
{ | ||
$accountResponse = $this->client->call('getAccountInfo', [$pubKey, ["encoding" => "jsonParsed"]])['value']; | ||
|
||
if (! $accountResponse) { | ||
throw new AccountNotFoundException("API Error: Account {$pubKey} not found."); | ||
} | ||
|
||
return $accountResponse; | ||
} | ||
|
||
/** | ||
* @param string $pubKey | ||
* @return float | ||
*/ | ||
public function getBalance(string $pubKey): float | ||
{ | ||
return $this->client->call('getBalance', [$pubKey])['value']; | ||
} | ||
|
||
/** | ||
* @param string $transactionSignature | ||
* @return array | ||
*/ | ||
public function getConfirmedTransaction(string $transactionSignature): array | ||
{ | ||
return $this->client->call('getConfirmedTransaction', [$transactionSignature]); | ||
} | ||
|
||
/** | ||
* NEW: This method is only available in solana-core v1.7 or newer. Please use getConfirmedTransaction for solana-core v1.6 | ||
* | ||
* @param string $transactionSignature | ||
* @return array | ||
*/ | ||
public function getTransaction(string $transactionSignature): array | ||
{ | ||
return $this->client->call('getTransaction', [$transactionSignature]); | ||
} | ||
|
||
/** | ||
* @param Commitment|null $commitment | ||
* @return array | ||
* @throws Exceptions\GenericException|Exceptions\MethodNotFoundException|Exceptions\InvalidIdResponseException | ||
*/ | ||
public function getRecentBlockhash(?Commitment $commitment = null): array | ||
{ | ||
return $this->client->call('getRecentBlockhash', array_filter([$commitment]))['value']; | ||
} | ||
|
||
/** | ||
* @param Transaction $transaction | ||
* @param Keypair $signer | ||
* @param array $params | ||
* @return array|\Illuminate\Http\Client\Response | ||
* @throws Exceptions\GenericException | ||
* @throws Exceptions\InvalidIdResponseException | ||
* @throws Exceptions\MethodNotFoundException | ||
*/ | ||
public function sendTransaction(Transaction $transaction, Keypair $signer, $params = []) | ||
{ | ||
if (! $transaction->recentBlockhash) { | ||
$transaction->recentBlockhash = $this->getRecentBlockhash()['blockhash']; | ||
} | ||
|
||
$transaction->sign($signer); | ||
|
||
$rawBinaryString = $transaction->serialize(false); | ||
|
||
$hashString = sodium_bin2base64($rawBinaryString, SODIUM_BASE64_VARIANT_ORIGINAL); | ||
|
||
return $this->client->call('sendTransaction', [ | ||
$hashString, | ||
[ | ||
'encoding' => 'base64', | ||
'preflightCommitment' => 'confirmed', | ||
], | ||
]); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Exceptions; | ||
|
||
use Exception; | ||
|
||
class BaseSolanaPhpSdkException extends Exception | ||
{ | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Exceptions; | ||
|
||
class InputValidationException extends BaseSolanaPhpSdkException | ||
{ | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk\Exceptions; | ||
|
||
use Exception; | ||
use Throwable; | ||
|
||
class TodoException extends Exception | ||
{ | ||
public function __construct($message = "", $code = 0, Throwable $previous = null) | ||
{ | ||
parent::__construct($message . " | Help is appreciated: https://github.com/tighten/solana-php-sdk", $code, $previous); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Tighten\SolanaPhpSdk; | ||
|
||
use SodiumException; | ||
use Tighten\SolanaPhpSdk\Util\Buffer; | ||
use Tighten\SolanaPhpSdk\Util\HasPublicKey; | ||
use Tighten\SolanaPhpSdk\Util\HasSecretKey; | ||
|
||
/** | ||
* An account keypair used for signing transactions. | ||
*/ | ||
class Keypair implements HasPublicKey, HasSecretKey | ||
{ | ||
public Buffer $publicKey; | ||
public Buffer $secretKey; | ||
|
||
/** | ||
* @param array|string $publicKey | ||
* @param array|string $secretKey | ||
*/ | ||
public function __construct($publicKey = null, $secretKey = null) | ||
{ | ||
if ($publicKey == null && $secretKey == null) { | ||
$keypair = sodium_crypto_sign_keypair(); | ||
|
||
$publicKey = sodium_crypto_sign_publickey($keypair); | ||
$secretKey = sodium_crypto_sign_secretkey($keypair); | ||
} | ||
|
||
$this->publicKey = Buffer::from($publicKey); | ||
$this->secretKey = Buffer::from($secretKey); | ||
} | ||
|
||
/** | ||
* @return Keypair | ||
* @throws SodiumException | ||
*/ | ||
public static function generate(): Keypair | ||
{ | ||
$keypair = sodium_crypto_sign_keypair(); | ||
|
||
return static::from($keypair); | ||
} | ||
|
||
/** | ||
* @param string $keypair | ||
* @return Keypair | ||
* @throws SodiumException | ||
*/ | ||
public static function from(string $keypair): Keypair | ||
{ | ||
return new static( | ||
sodium_crypto_sign_publickey($keypair), | ||
sodium_crypto_sign_secretkey($keypair) | ||
); | ||
} | ||
|
||
/** | ||
* Create a keypair from a raw secret key byte array. | ||
* | ||
* This method should only be used to recreate a keypair from a previously | ||
* generated secret key. Generating keypairs from a random seed should be done | ||
* with the {@link Keypair.fromSeed} method. | ||
* | ||
* @param $secretKey | ||
* @return Keypair | ||
*/ | ||
static public function fromSecretKey($secretKey): Keypair | ||
{ | ||
$secretKey = Buffer::from($secretKey)->toString(); | ||
|
||
$publicKey = sodium_crypto_sign_publickey_from_secretkey($secretKey); | ||
|
||
return new static( | ||
$publicKey, | ||
$secretKey | ||
); | ||
} | ||
|
||
/** | ||
* Generate a keypair from a 32 byte seed. | ||
* | ||
* @param string|array $seed | ||
* @return Keypair | ||
* @throws SodiumException | ||
*/ | ||
static public function fromSeed($seed): Keypair | ||
{ | ||
$seed = Buffer::from($seed)->toString(); | ||
|
||
$keypair = sodium_crypto_sign_seed_keypair($seed); | ||
|
||
return static::from($keypair); | ||
} | ||
|
||
/** | ||
* The public key for this keypair | ||
* | ||
* @return PublicKey | ||
*/ | ||
public function getPublicKey(): PublicKey | ||
{ | ||
return new PublicKey($this->publicKey); | ||
} | ||
|
||
/** | ||
* The raw secret key for this keypair | ||
* | ||
* @return Buffer | ||
*/ | ||
public function getSecretKey(): Buffer | ||
{ | ||
return Buffer::from($this->secretKey); | ||
} | ||
} |
Oops, something went wrong.