diff --git a/src/Account/Address.php b/src/Account/Address.php index b1522a8..767c64b 100644 --- a/src/Account/Address.php +++ b/src/Account/Address.php @@ -38,7 +38,10 @@ static function fromPublicKey( PublicKey $publicKey, ChainId $chainId = null ): $address = new Address; $wk = new \deemru\WavesKit( ( isset( $chainId ) ? $chainId : WavesConfig::chainId() )->asString() ); $wk->setPublicKey( $publicKey->bytes(), true ); - $address->address = Base58String::fromBytes( $wk->getAddress( true ) ); + $bytes = $wk->getAddress( true ); + if( !is_string( $bytes ) || strlen( $bytes ) !== Address::BYTE_LENGTH ) + throw new Exception( __FUNCTION__ . ' bad key', ExceptionCode::BAD_KEY ); + $address->address = Base58String::fromBytes( $bytes ); return $address; } diff --git a/src/Account/PrivateKey.php b/src/Account/PrivateKey.php index e4384ca..0d04005 100644 --- a/src/Account/PrivateKey.php +++ b/src/Account/PrivateKey.php @@ -2,7 +2,9 @@ namespace Waves\Account; +use Exception; use Waves\Common\Base58String; +use Waves\Common\ExceptionCode; class PrivateKey { @@ -16,7 +18,10 @@ private function __construct(){} static function fromSeed( string $seed, int $nonce = 0 ): PrivateKey { $privateKey = new PrivateKey; - $privateKey->key = Base58String::fromBytes( ( new \deemru\WavesKit )->getPrivateKey( true, $seed, pack( 'N', $nonce ) ) ); + $bytes = ( new \deemru\WavesKit )->getPrivateKey( true, $seed, pack( 'N', $nonce ) ); + if( !is_string( $bytes ) || strlen( $bytes ) !== PrivateKey::LENGTH ) + throw new Exception( __FUNCTION__ . ' bad key', ExceptionCode::BAD_KEY ); + $privateKey->key = Base58String::fromBytes( $bytes ); return $privateKey; } diff --git a/src/Account/PublicKey.php b/src/Account/PublicKey.php index 4716754..ff8e95e 100644 --- a/src/Account/PublicKey.php +++ b/src/Account/PublicKey.php @@ -2,7 +2,9 @@ namespace Waves\Account; +use Exception; use Waves\Common\Base58String; +use Waves\Common\ExceptionCode; use Waves\Model\ChainId; class PublicKey @@ -34,7 +36,10 @@ static function fromPrivateKey( PrivateKey $key ): PublicKey $publicKey = new PublicKey; $wk = new \deemru\WavesKit; $wk->setPrivateKey( $key->bytes(), true ); - $publicKey->key = Base58String::fromBytes( $wk->getPublicKey( true ) ); + $bytes = $wk->getPublicKey( true ); + if( !is_string( $bytes ) || strlen( $bytes ) !== PublicKey::BYTES_LENGTH ) + throw new Exception( __FUNCTION__ . ' bad key', ExceptionCode::BAD_KEY ); + $publicKey->key = Base58String::fromBytes( $bytes ); return $publicKey; } diff --git a/src/Common/ExceptionCode.php b/src/Common/ExceptionCode.php index 4107b22..7f1a75c 100644 --- a/src/Common/ExceptionCode.php +++ b/src/Common/ExceptionCode.php @@ -23,4 +23,5 @@ class ExceptionCode const BAD_CHAINID = ExceptionCode::BASE | 16; const TIMEOUT = ExceptionCode::BASE | 17; const UNEXPECTED = ExceptionCode::BASE | 18; + const BAD_KEY = ExceptionCode::BASE | 19; }