Skip to content

Commit

Permalink
refactor temporary command
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Jul 24, 2024
1 parent aa70f3b commit 08ceee1
Showing 1 changed file with 16 additions and 33 deletions.
49 changes: 16 additions & 33 deletions src/Transactions/Types/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,53 +188,36 @@ private function temporarySignerSign(Buffer $transaction, PrivateKey $keys)

$message = $transaction->getHex();

$scriptPath = __DIR__.'/../../../scripts';

$command = escapeshellcmd("npm start --prefix $scriptPath sign $privateKey $message");

exec($command, $output, $returnVar);
$command = "sign $privateKey $message";

if ($returnVar !== 0) {
$errorOutput = implode("\n", $output);
$result = $this->runTemporaryNodeCommand($command);

throw new \RuntimeException("Error running signer script: $errorOutput");
}

$jsonOutput = implode("\n", $output);

if (preg_match('/\{.*\}/s', $jsonOutput, $matches)) {
$json = $matches[0];
} else {
throw new \RuntimeException("Error: Could not find JSON output in: $jsonOutput");
}
return $result['signature'];
}

$result = json_decode($json, true);
private function temporarySignerVerify(Buffer $transaction, string $signature, string $publicKey)
{
$message = $transaction->getHex();

if (json_last_error() !== JSON_ERROR_NONE) {
throw new \RuntimeException('Error parsing JSON output: '.json_last_error_msg());
}
$command = "verify $publicKey $message $signature";

if ($result['status'] === 'success') {
return $result['signature'];
}
$result = $this->runTemporaryNodeCommand($command);

throw new \RuntimeException('Error signing message: '.$result['message']);
return $result['isValid'];
}

private function temporarySignerVerify(Buffer $transaction, string $signature, string $publicKey)
private function runTemporaryNodeCommand(string $command): array
{
$message = $transaction->getHex();

$scriptPath = __DIR__.'/../../../scripts';

$command = escapeshellcmd("npm start --prefix $scriptPath verify $publicKey $message $signature");
$command = escapeshellcmd("npm start --prefix $scriptPath $command");

exec($command, $output, $returnVar);

if ($returnVar !== 0) {
$errorOutput = implode("\n", $output);

throw new \RuntimeException("Error running verifier script: $errorOutput");
throw new \RuntimeException("Error running signer script: $errorOutput");
}

$jsonOutput = implode("\n", $output);
Expand All @@ -251,10 +234,10 @@ private function temporarySignerVerify(Buffer $transaction, string $signature, s
throw new \RuntimeException('Error parsing JSON output: '.json_last_error_msg());
}

if ($result['status'] === 'success') {
return $result['isValid'];
if ($result['status'] !== 'success') {
throw new \RuntimeException('Error: '.$result['message']);
}

throw new \RuntimeException('Error verifying signature: '.$result['message']);
return $result;
}
}

0 comments on commit 08ceee1

Please sign in to comment.