Skip to content

Commit

Permalink
add soroban 11 support
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed Sep 18, 2023
1 parent 4c9b4e5 commit 8943098
Show file tree
Hide file tree
Showing 63 changed files with 1,033 additions and 1,175 deletions.
48 changes: 11 additions & 37 deletions Soneso/StellarSDK/InvokeContractHostFunction.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
use Exception;
use Soneso\StellarSDK\Soroban\Address;
use Soneso\StellarSDK\Xdr\XdrHostFunction;
use Soneso\StellarSDK\Xdr\XdrSCAddress;
use Soneso\StellarSDK\Xdr\XdrSCVal;
use Soneso\StellarSDK\Xdr\XdrInvokeContractArgs;

class InvokeContractHostFunction extends HostFunction
{
Expand All @@ -33,54 +32,29 @@ public function __construct(string $contractId, string $functionName, ?array $ar
}

public function toXdr() : XdrHostFunction {
$invokeContract = array();

// contractID
$contractIDVal = Address::fromContractId($this->contractId)->toXdrSCVal();
array_push($invokeContract, $contractIDVal);

// function name
$functionNameVal = XdrSCVal::forSymbol($this->functionName);
array_push($invokeContract, $functionNameVal);

// arguments if any
$args = array();
if ($this->arguments != null) {
$invokeContract = array_merge($invokeContract, $this->arguments);
$args = array_merge($args, $this->arguments);
}

return XdrHostFunction::forInvokingContractWithArgs($invokeContract);
$invokeArgs = new XdrInvokeContractArgs(Address::fromContractId($this->contractId)->toXdr(),
$this->functionName,$args);
return XdrHostFunction::forInvokingContractWithArgs($invokeArgs);
}

/**
* @throws Exception
*/
public static function fromXdr(XdrHostFunction $xdr) : InvokeContractHostFunction {
$invokeContract = $xdr->invokeContract;
if ($invokeContract == null || count($invokeContract) < 2) {
if ($invokeContract == null) {
throw new Exception("Invalid argument");
}

$contractId = null;
$functionName = null;
$fArgs = null;
$contractIdVal = $invokeContract[0];
$functionVal = $invokeContract[1];

if ($contractIdVal instanceof XdrSCVal && $contractIdVal->address != null) {
$contractId = Address::fromXdr($contractIdVal->address)->contractId;
}

if ($functionVal instanceof XdrSCVal && $functionVal->sym != null) {
$functionName = $functionVal->sym;
}
if(count($invokeContract) > 2) {
$fArgs = array_slice($invokeContract, 2);
}
if ($contractId == null || $functionName == null) {
throw new Exception("invalid argument");
}
$contractId = Address::fromXdr($invokeContract->contractAddress)->contractId;
$functionName = $invokeContract->functionName;
$args= $invokeContract->getArgs();

return new InvokeContractHostFunction($contractId, $functionName, $fArgs);
return new InvokeContractHostFunction($contractId, $functionName, $args);
}

/**
Expand Down
77 changes: 77 additions & 0 deletions Soneso/StellarSDK/Soroban/Responses/RestorePreamble.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php declare(strict_types=1);

// Copyright 2023 The Stellar PHP SDK Authors. All rights reserved.
// Use of this source code is governed by a license that can be
// found in the LICENSE file.

namespace Soneso\StellarSDK\Soroban\Responses;

use Soneso\StellarSDK\Xdr\XdrSorobanTransactionData;

/*
* It can only present on successful simulation (i.e. no error) of InvokeHostFunction operations.
* If present, it indicates the simulation detected expired ledger entries which requires restoring
* with the submission of a RestoreFootprint operation before submitting the InvokeHostFunction operation.
* The minResourceFee and transactionData fields should be used to construct the transaction
* containing the RestoreFootprint operation.
*/
class RestorePreamble
{

/// The recommended Soroban Transaction Data to use when submitting the RestoreFootprint operation.
public XdrSorobanTransactionData $transactionData;

/// Recommended minimum resource fee to add when submitting the RestoreFootprint operation. This fee is to be added on top of the Stellar network fee.
public int $minResourceFee;

/**
* @param XdrSorobanTransactionData $transactionData
* @param int $minResourceFee
*/
public function __construct(XdrSorobanTransactionData $transactionData, int $minResourceFee)
{
$this->transactionData = $transactionData;
$this->minResourceFee = $minResourceFee;
}


public static function fromJson(array $json) : RestorePreamble {
$transactionData = XdrSorobanTransactionData::fromBase64Xdr($json['transactionData']);
$minResourceFee = intval($json['minResourceFee']);

return new RestorePreamble($transactionData, $minResourceFee);
}

/**
* @return XdrSorobanTransactionData
*/
public function getTransactionData(): XdrSorobanTransactionData
{
return $this->transactionData;
}

/**
* @param XdrSorobanTransactionData $transactionData
*/
public function setTransactionData(XdrSorobanTransactionData $transactionData): void
{
$this->transactionData = $transactionData;
}

/**
* @return int
*/
public function getMinResourceFee(): int
{
return $this->minResourceFee;
}

/**
* @param int $minResourceFee
*/
public function setMinResourceFee(int $minResourceFee): void
{
$this->minResourceFee = $minResourceFee;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class SimulateTransactionResponse extends SorobanRpcResponse
/// Array of the events emitted during the contract invocation(s). The events are ordered by their emission time. (an array of serialized base64 strings)
public ?array $events = null; //[string xdr XdrDiagnosticEvent]

/// It can only present on successful simulation (i.e. no error) of InvokeHostFunction operations. If present, it indicates
/// the simulation detected expired ledger entries which requires restoring with the submission of a RestoreFootprint
/// operation before submitting the InvokeHostFunction operation. The restorePreamble.minResourceFee and restorePreamble.transactionData fields should
/// be used to construct the transaction containing the RestoreFootprint
public ?RestorePreamble $restorePreamble;

public static function fromJson(array $json) : SimulateTransactionResponse {
$result = new SimulateTransactionResponse($json);
if (isset($json['result'])) {
Expand Down Expand Up @@ -69,6 +75,9 @@ public static function fromJson(array $json) : SimulateTransactionResponse {
if (isset($json['result']['minResourceFee'])) {
$result->minResourceFee = intval($json['result']['minResourceFee']);
}
if (isset($json['result']['restorePreamble'])) {
$result->restorePreamble = RestorePreamble::fromJson($json['result']['restorePreamble']);
}
} else if (isset($json['error'])) {
$result->error = SorobanRpcErrorResponse::fromJson($json);
}
Expand Down Expand Up @@ -210,4 +219,20 @@ public function setEvents(?array $events): void
$this->events = $events;
}

/**
* @return RestorePreamble|null
*/
public function getRestorePreamble(): ?RestorePreamble
{
return $this->restorePreamble;
}

/**
* @param RestorePreamble|null $restorePreamble
*/
public function setRestorePreamble(?RestorePreamble $restorePreamble): void
{
$this->restorePreamble = $restorePreamble;
}

}
27 changes: 15 additions & 12 deletions Soneso/StellarSDK/Soroban/SorobanAddressCredentials.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Soneso\StellarSDK\Soroban;

use Soneso\StellarSDK\Xdr\XdrSCVal;
use Soneso\StellarSDK\Xdr\XdrSorobanAddressCredentials;


Expand All @@ -14,35 +15,36 @@ class SorobanAddressCredentials
public Address $address;
public int $nonce;
public int $signatureExpirationLedger;
public array $signatureArgs; // [XdrSCVal]
public XdrSCVal $signature;

/**
* @param Address $address
* @param int $nonce
* @param int $signatureExpirationLedger
* @param array $signatureArgs
* @param XdrSCVal $signature
*/
public function __construct(Address $address, int $nonce, int $signatureExpirationLedger, array $signatureArgs = array())
public function __construct(Address $address, int $nonce, int $signatureExpirationLedger, XdrSCVal $signature)
{
$this->address = $address;
$this->nonce = $nonce;
$this->signatureExpirationLedger = $signatureExpirationLedger;
$this->signatureArgs = $signatureArgs;
$this->signature = $signature;
}


/**
* @param XdrSorobanAddressCredentials $xdr
* @return SorobanAddressCredentials
*/
public static function fromXdr(XdrSorobanAddressCredentials $xdr) : SorobanAddressCredentials {
return new SorobanAddressCredentials(Address::fromXdr($xdr->address), $xdr->nonce, $xdr->signatureExpirationLedger, $xdr->signatureArgs);
return new SorobanAddressCredentials(Address::fromXdr($xdr->address), $xdr->nonce, $xdr->signatureExpirationLedger, $xdr->signature);
}

/**
* @return XdrSorobanAddressCredentials
*/
public function toXdr(): XdrSorobanAddressCredentials {
return new XdrSorobanAddressCredentials($this->address->toXdr(),$this->nonce, $this->signatureExpirationLedger, $this->signatureArgs);
return new XdrSorobanAddressCredentials($this->address->toXdr(),$this->nonce, $this->signatureExpirationLedger, $this->signature);
}

/**
Expand Down Expand Up @@ -94,18 +96,19 @@ public function setSignatureExpirationLedger(int $signatureExpirationLedger): vo
}

/**
* @return array
* @return XdrSCVal
*/
public function getSignatureArgs(): array
public function getSignature(): XdrSCVal
{
return $this->signatureArgs;
return $this->signature;
}

/**
* @param array $signatureArgs
* @param XdrSCVal $signature
*/
public function setSignatureArgs(array $signatureArgs): void
public function setSignature(XdrSCVal $signature): void
{
$this->signatureArgs = $signatureArgs;
$this->signature = $signature;
}

}
11 changes: 9 additions & 2 deletions Soneso/StellarSDK/Soroban/SorobanAuthorizationEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Soneso\StellarSDK\Xdr\XdrEnvelopeType;
use Soneso\StellarSDK\Xdr\XdrHashIDPreimage;
use Soneso\StellarSDK\Xdr\XdrHashIDPreimageSorobanAuthorization;
use Soneso\StellarSDK\Xdr\XdrSCVal;
use Soneso\StellarSDK\Xdr\XdrSorobanAuthorizationEntry;
use Soneso\StellarSDK\Xdr\XdrSorobanCredentialsType;

Expand Down Expand Up @@ -53,7 +54,7 @@ public function toBase64Xdr() : String {
}

/**
* Signs the authorization entry. The signature will be added to the signatureArgs of the soroban credentials
* Signs the authorization entry. The signature will be added to the signatures of the soroban credentials
* @param KeyPair $signer
* @param Network $network
*/
Expand All @@ -74,7 +75,13 @@ public function sign(KeyPair $signer, Network $network) {
$payload = Hash::generate($rootInvocationPreimage->encode()); // sha256
$signatureBytes = $signer->sign($payload);
$signature = new AccountEd25519Signature($signer->getPublicKey(), $signatureBytes);
array_push($this->credentials->addressCredentials->signatureArgs, $signature->toXdrSCVal());
$sigVal = $signature->toXdrSCVal();
if ($this->credentials->addressCredentials->signature->vec != null) {
array_push($this->credentials->addressCredentials->signature->vec, $sigVal);
} else {
$this->credentials->addressCredentials->signature = XdrSCVal::forVec([$sigVal]);
}
$signature->toXdrSCVal();
}

/**
Expand Down
Loading

0 comments on commit 8943098

Please sign in to comment.