API Documentation

Identity

Crypto / API Documentation / Identity / Address

Derive an address from a passphrase

public static function fromPassphrase(string $passphrase)

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This function returns an address derived from the given passphrase as a string.

use ArkEcosystem\Crypto\Identities\Address;

$address = Address::fromPassphrase('super secret passphrase');
>> '0x2289577F3784788784226Eb41E9B0ca9705C7C37'

Derive an address from a public key

public static function fromPublicKey(string $publicKey)

Parameters

TypeNameRequiredDescription
stringpublicKeyYesPublic key

Return Value

This function returns an address derived from the given public key as a string.

use ArkEcosystem\Crypto\Identities\Address;

$address = Address::fromPublicKey('023efc1da7f315f3c533a4080e491f32cd4219731cef008976c3876539e1f192d3');

>> '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22'

Derive an address from a private key

public static function fromPrivateKey(EccPrivateKey $privateKey)

Parameters

TypeNameRequiredDescription
EccPrivateKeyprivateKeyYesPrivate key

Return Value

This function returns an address derived from the given private key as a string.

use ArkEcosystem\Crypto\Identities\PrivateKey;

$privateKey = PrivateKey::fromPassphrase('this is a top secret passphrase');
$address = Address::fromPrivateKey($privateKey);

>> '0xb0FF9213f7226bBB72b84dE16af86e56f1f38B01'

Validate the given address

public static function validate(string $address)

Parameters

TypeNameRequiredDescription
stringaddressYesAddress to validate

Return Value

This method returns a boolean indicating the validity of the address.

use ArkEcosystem\Crypto\Identities\Address;

$isValid = Address::validate('0x2289577F3784788784226Eb41E9B0ca9705C7C37');

>> true
Crypto / API Documentation / Identity / PrivateKey

Derive a private key from a passphrase

public static function fromPassphrase(string $passphrase)

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This method returns a PrivateKey object derived from the given passphrase.

use ArkEcosystem\Crypto\Identities\PrivateKey;

$privateKey = PrivateKey::fromPassphrase('this is a top secret passphrase');

>> PrivateKey

Derive a private key from a hex string

public static function fromHex($privateKey)

Parameters

TypeNameRequiredDescription
stringprivateKeyYesHexadecimal representation of the private key

Return Value

This method returns a PrivateKey object derived from a hex string.

use ArkEcosystem\Crypto\Identities\PrivateKey;

$privateKey = PrivateKey::fromHex('d8839c2432bfd0a67ef10a804ba991eabba19f154a3d707917681d45822a5712');

>> PrivateKey

Derive a private key from a WIF

public static function fromWif(string $wif)

Parameters

TypeNameRequiredDescription
stringwifYesHexadecimal representation of the WIF string

Return Value

This method returns a PrivateKey object derived from a WIF string.

use ArkEcosystem\Crypto\Identities\PrivateKey;

$privateKey = PrivateKey::fromWif('UZYnRZ8qpeQWTLeCNzw93guWSdKLmr2vHEWGG4sNv7TJofL7TZvy');

>> PrivateKey

Signs a message using the private key

public function sign(BufferInterface $message): CompactSignature

Parameters

TypeNameRequiredDescription
stringmessageYesMessage to sign in hex (without 0x)

Return Value

This method signs a message using the private key.

use ArkEcosystem\Crypto\Identities\PrivateKey;

$privateKey = PrivateKey::fromPassphrase('this is a top secret passphrase');
$signature = $privateKey->sign(new Buffer('Hello World'));

>> CompactSignature
Crypto / API Documentation / Identity / PublicKey

Derive a public key from a passphrase

public static function fromPassphrase(string $passphrase): PublicKey

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase

Return Value

This function returns a public key instance object derived from the given passphrase.

use ArkEcosystem\Crypto\Identities\PublicKey;

$publicKey = PublicKey::fromPassphrase('this is a top secret passphrase');

>> PublicKey

Create a public key instance from a hex string

public static function fromHex($publicKey)

Parameters

TypeNameRequiredDescription
BitWaspBuffertoolsBufferInterface|stringpublicKeyYesPublic key in hex string or Buffer format

Return Value

This function returns a PublicKey object created from the given hex string.

use ArkEcosystem\Crypto\Identities\PublicKey;

$publicKey = PublicKey::fromHex('034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192');

>> PublicKey

Recover a public key from a signature

public static function recover(BufferInterface $message, CompactSignatureInterface $signature): self

Parameters

TypeNameRequiredDescription
BufferInterfacemessageYesMessage to use to recover
CompactSignatureInterfacesignatureYesSignature to use to recover

Return Value

This function returns a PublicKey object recovered from the given signature and message.

use ArkEcosystem\Crypto\Identities\PublicKey;

$publicKey = PublicKey::recover($message, $signature);

>> PublicKey
Crypto / API Documentation / Identity / WIF

Derive a WIF from a passphrase

public static function fromPassphrase(string $passphrase, AbstractNetwork $network = null)

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase
AbstractNetworknetworkNoNetwork WIF

Return Value

This method returns a WIF in string format.

use ArkEcosystem\Crypto\Identities\PrivateKey;

$privateKey = PrivateKey::fromWif('validWif');

>> EcPrivateKey

Network

Crypto / API Documentation / Configuration / Network

Set the network you want to use in the crypto library

public static function set(AbstractNetwork $network)

Parameters

TypeNameRequiredDescription
AbstractNetworknetworkYesNetwork object (Testnet, Mainnet, etc)

Return Value

This method returns void.

use ArkEcosystem\Crypto\Configuration\Network;
use ArkEcosystem\Crypto\Networks\Testnet;

Network::set(new Testnet());

>> void
// Custom network example

use ArkEcosystem\Crypto\Configuration\Network;
use ArkEcosystem\Crypto\Networks\AbstractNetwork;

class CustomNetwork extends AbstractNetwork {
  ...
}

Network::set(new CustomNetwork());

>> void

Get the currently used network

public static function get()

Return Value

This function returns the current network.

use ArkEcosystem\Crypto\Configuration\Network;

$network = Network::get();

>> AbstractNetwork

Get the chain ID of the Network

public function chainId()

Return Value

This method returns the chain ID as an integer.

use ArkEcosystem\Crypto\Networks\Testnet;

(new Testnet())->chainId();

>> 10000

Get the epoch time of the start of the Network

public function epoch()

Return Value

This method returns the epoch time in string format.

use ArkEcosystem\Crypto\Networks\Testnet;

(new Testnet())->epoch();

>> '2017-03-21T13:00:00.000Z'

Get the WIF of the Network as a hexadecimal string

public function wif()

Return Value

This method returns the WIF in hexadecimal string format.

use ArkEcosystem\Crypto\Networks\Testnet;

(new Testnet())->wif();

>> 'ba'

Transactions

Crypto / API Documentation / Transactions / AbstractTransactionBuilder

For full examples of using the Builders to create transactions, please have a look at the Examples page.

Create a new transaction instance

public static function new(?array $data = null): static

Parameters

TypeNameRequiredDescription
?arraydataNoOptional transaction data

Return Value

This method returns a new transaction instance in self format.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new();

>> TransferBuilder

Convert the transaction to its string representation

public function __toString(): string

Return Value

This method returns the string representation of the transaction.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
echo TransferBuilder::new()->__toString();

>> '{"gasPrice":"5000000000","network":30,"gasLimit":1000000,"nonce":"1","senderPublicKey":"","value":"0","data":""}'

Set the gas limit for the transaction

public function gasLimit(int | string $gas): static

Parameters

TypeNameRequiredDescription
int | stringgasLimitYesGas limit for the transaction

Return Value

This method returns the transaction instance in self format.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->gasLimit(1000000);

>> TransferBuilder

Set the recipient address for the transaction

public function to(string $to): static

Parameters

TypeNameRequiredDescription
stringtoYesRecipient address

Return Value

This method returns the transaction instance in self format.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22');

>> TransferBuilder

Set the gas price for the transaction

public function gasPrice(int | string $gasPrice): static

Parameters

TypeNameRequiredDescription
int | stringgasPriceYesGas price for the transaction

Return Value

This method returns the transaction instance in self format.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->gasPrice(5000000000);

>> TransferBuilder

Set the nonce for the transaction

public function nonce(string $nonce): static

Parameters

TypeNameRequiredDescription
stringnonceYesNonce for the transaction

Return Value

This method returns the transaction instance in self format.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->nonce('1');

>> TransferBuilder

Set the network for the transaction

public function network(int $network): static

Parameters

TypeNameRequiredDescription
intnetworkYesNetwork identifier (e.g., Mainnet, Testnet)

Return Value

This method returns the transaction instance in self format.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->network(23);

>> TransferBuilder

Sign the transaction using the given passphrase

public function sign(string $passphrase): static

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase for signing the transaction

Return Value

This method signs the transaction using the provided passphrase and returns the builder instance.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->sign('this is a top secret passphrase');

>> TransferBuilder

Sign the transaction with a second signature (only for legacy addresses)

public function legacySecondSign(string $passphrase, string $secondPasphrase): static

Parameters

TypeNameRequiredDescription
stringpassphraseYesPassphrase for signing the transaction
stringsecondPassphraseYesSecond Passphrase for signing the transaction

Return Value

This method signs the transaction using the provided passphrases and returns the builder instance.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$transaction = TransferBuilder::new()->legacySecondSign('this is a top secret passphrase',this is a top secret second passphrase');

>> TransferBuilder

Verify the transaction validity

public function verify(): bool

Return Value

This method returns the validity of the transaction as a boolean.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$isValid = TransferBuilder::new()
  ->to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  ->sign('this is a top secret passphrase')
  ->verify();

>> true

Convert the transaction to an array

public function toArray(): array

Return Value

This method returns the transaction data as an array.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$data = TransferBuilder::new()->toArray();

>> [
  "gasPrice" => "5000000000",
  "network" => 30,
  "gasLimit" => 1000000,
  "nonce" => 1,
  "senderPublicKey" => "",
  "value" => "0",
  "data" => "",
]

Convert the transaction to its JSON representation

public function toJson(): string

Return Value

This method returns the JSON representation of the transaction as a string.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

// Using TransferBuilder as an example of AbstractTransactionBuilder
$json = TransferBuilder::new()->toJson();

>> '{"value": "0", ..., "data": ""}'
Crypto / API Documentation / Transactions / EvmCallBuilder

Inherits from:AbstractTransactionBuilder

Set the payload for the EVM call

public function payload(string $payload): self

Parameters

TypeNameRequiredDescription
stringpayloadYesEVM code in hex format (without `0x` prefix)

Return Value

Returns the transaction instance with the payload set.

use ArkEcosystem\Crypto\Transactions\Builder\EvmCallBuilder;

$transaction = EvmCallBuilder::new()->payload('yourHexPayload');

>> EvmCallBuilder
Crypto / API Documentation / Transactions / MultipaymentBuilder

Inherits from:AbstractTransactionBuilder

Add recipient to the transaction

public function pay(string $address, BigDecimal $value): self

Parameters

TypeNameRequiredDescription
stringaddressYesAddress to recipient
BigDecimalvalueYesAmount to send to recipient

Return Value

This method adds a recipient to the transaction and returns the builder instance.

use ArkEcosystem\Crypto\Transactions\Builder\MultipaymentBuilder;

$transaction = MultipaymentBuilder::new()->pay('0x512F366D524157BcF734546eB29a6d687B762255', BigDecimal::of('100000'));

>> MultipaymentBuilder
Crypto / API Documentation / Transactions / TransferBuilder

Inherits from:AbstractTransactionBuilder

Set the value to transfer (in wei)

public function value(string $value): self

Parameters

TypeNameRequiredDescription
stringvalueYesAmount to transfer in wei

Return Value

This method returns the transaction instance with the value set.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;

$transaction = TransferBuilder::new()->value('1000000000000000000');

>> TransferBuilder
Crypto / API Documentation / Transactions / ValidatorRegistrationBuilder

Inherits from:AbstractTransactionBuilder

Set the validator BLS public key

public function validatorPublicKey(string $validatorPublicKey): self

Parameters

TypeNameRequiredDescription
stringvalidatorPublicKeyYesValidator BLS public key to register

Return Value

This method returns the transaction instance with the validator public key set.

use ArkEcosystem\Crypto\Transactions\Builder\ValidatorRegistrationBuilder;

$transaction = ValidatorRegistrationBuilder::new()->validatorPublicKey('954f46d6097a1d314e900e66e11e0dad0a57cd03e04ec99f0dedd1c765dcb11e6d7fa02e22cf40f9ee23d9cc1c0624bd');

>> ValidatorRegistrationBuilder

Set the locked value (in wei)

public function value(string $value): self

Parameters

TypeNameRequiredDescription
stringvalueYesAmount to lock in wei

Return Value

This method returns the transaction instance with the value set.

use ArkEcosystem\Crypto\Transactions\Builder\ValidatorRegistrationBuilder;

$transaction = ValidatorRegistrationBuilder::new()->value('250000000000000000000');

>> ValidatorRegistrationBuilder
Crypto / API Documentation / Transactions / ValidatorResignationBuilder

Inherits from:AbstractTransactionBuilder

Crypto / API Documentation / Transactions / UsernameRegistrationBuilder

Inherits from:AbstractTransactionBuilder

Set the Username

public function username(string $username): self

Parameters

TypeNameRequiredDescription
stringusernameYesUsername to register

Return Value

This method returns the transaction instance with the username set.

use ArkEcosystem\Crypto\Transactions\Builder\UsernameRegistrationBuilder;

$transaction = UsernameRegistrationBuilder::new()->username('myusername');

>> UsernameRegistrationBuilder
Crypto / API Documentation / Transactions / UsernameRegistrationBuilder

Inherits from:AbstractTransactionBuilder

Set the Username

public function username(string $username): self

Parameters

TypeNameRequiredDescription
stringusernameYesUsername to register

Return Value

This method returns the transaction instance with the username set.

use ArkEcosystem\Crypto\Transactions\Builder\UsernameRegistrationBuilder;

$transaction = UsernameRegistrationBuilder::new()->username('myusername');

>> UsernameRegistrationBuilder
Crypto / API Documentation / Transactions / UsernameResignationBuilder

Inherits from:AbstractTransactionBuilder

Crypto / API Documentation / Transactions / VoteBuilder

Inherits from:AbstractTransactionBuilder

Set the vote for the transaction

public function vote(string $vote): self

Parameters

TypeNameRequiredDescription
stringvoteYesPublic key of the validator to vote for

Return Value

This method returns the transaction instance with the vote set.

use ArkEcosystem\Crypto\Transactions\Builder\VoteBuilder;

$transaction = VoteBuilder::new()->vote('0xC3bBE9B1CeE1ff85Ad72b87414B0E9B7F2366763');

>> VoteBuilder
Crypto / API Documentation / Transactions / UnvoteBuilder

Inherits from:AbstractTransactionBuilder

Crypto / API Documentation / Transactions / AbstractTransaction

It is recommended to use the Transaction Builders instead of the AbstractTransaction class directly. You can also access the underlying transaction property of the Builder to work with the AbstractTransaction instance, e.g. TransferBuilder::new()->transaction.

Create a new transaction instance

public function __construct(?array $data = null)

Parameters

TypeNameRequiredDescription
?arraydataNoOptional transaction data

Return Value

This method initializes a new transaction instance.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Using Transfer as an example of AbstractTransaction
$transaction = new Transfer();

>> Transfer

Get the payload of the transaction

abstract public function getPayload(): string

Return Value

This method returns the payload of the transaction as a string.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Transactions\Types\Vote;
use ArkEcosystem\Crypto\Utils\AbiEncoder;

$payload = (new Transfer())->getPayload();
>> ''

$encoded = (new AbiEncoder())->encodeFunctionCall('vote', ['0xC3bBE9B1CeE1ff85Ad72b87414B0E9B7F2366763']);
$payload = (new Vote(['data' => $encoded]))->getPayload();

>> '0x6dd7d8ea000000000000000000000000c3bbe9b1cee1ff85ad72b87414b0e9b7f2366763'

Sign the transaction using the given private key

public function sign(PrivateKey $keys): static

Parameters

TypeNameRequiredDescription
PrivateKeykeysYesPrivate key for signing

Return Value

This method signs the transaction and returns the transaction instance.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Identities\PrivateKey;

// Using Transfer as an example of AbstractTransaction
(new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000
]))->sign(PrivateKey::fromPassphrase('this is a top secret passphrase'))

>> Transfer

Recover the sender public key and address

public function recoverSender(): void

Return Value

This method recovers the sender's public key and address from the transaction signature.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Identities\PrivateKey;

// Using Transfer as an example of AbstractTransaction
$transfer = (new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000
]))->sign(PrivateKey::fromPassphrase('this is a top secret passphrase'));

$transfer->recoverSender();

$transfer->toJson();

>> '{..., "to":"0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22"}'

Verify the transaction

public function verify(): bool

Return Value

This method verifies the transaction signature and returns a boolean indicating validity.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;
use ArkEcosystem\Crypto\Identities\PrivateKey;

// Using Transfer as an example of AbstractTransaction
$transfer = (new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000
]))->sign(PrivateKey::fromPassphrase('this is a top secret passphrase'));

$transfer->verify();

>> true

Serialize the transaction

public function serialize(array $options = []): Buffer

Parameters

TypeNameRequiredDescription
arrayoptionsNoSerialization options

Return Value

This method serializes the transaction and returns it as a Buffer.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Using Transfer as an example of AbstractTransaction
$serialized = (new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000
]))->serialize();

>> Buffer

Get the hash for the transaction

public function hash(bool $skipSignature = false): BufferInterface

Parameters

TypeNameRequiredDescription
boolskipSignatureNoSerialization options

Return Value

This method generates the hash for the transaction and returns it as a string.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Using Transfer as an example of AbstractTransaction
$hash = (new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000,
])).hash();

>> string

Refresh the payload of the transaction

public function refreshPayloadData(): static

Return Value

This method refreshes the payload data of the transaction and returns the transaction instance.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Using Transfer as an example of AbstractTransaction
$transaction = (new Transfer({
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000,
})).refreshPayloadData();

>> Transfer

Convert the transaction to an array

public function toArray(): array

Return Value

This method returns the transaction data as an array.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Using Transfer as an example of AbstractTransaction
$array =(new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000
]))->toArray();

>> ["gasPrice" => ..., "gasLimit" => ...]

Convert the transaction to its JSON representation

public function toJson(): string

Return Value

This method returns the JSON representation of the transaction as a string.

use ArkEcosystem\Crypto\Transactions\Types\Transfer;

// Using Transfer as an example of AbstractTransaction
$json = (new Transfer([
  'value' => '1',
  'nonce' => 1,
  'to' => '0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22',
  'gasPrice' => 5000000000,
  'gas' => 21000
]))->toJson();

>> '{"gasPrice":..., "hash":...}'
Crypto / API Documentation / Transactions / EvmCall

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Multipayment

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Transfer

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / ValidatorRegistration

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / ValidatorResignation

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / UsernameRegistration

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / UsernameResignation

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Vote

Inherits from:AbstractTransaction

Crypto / API Documentation / Transactions / Unvote

Inherits from:AbstractTransaction


Serializer

Crypto / API Documentation / Transactions / Serializer

Create a new serializer instance

public static function new($transaction)

Parameters

TypeNameRequiredDescription
AbstractTransactiontransactionYesTransaction to be serialized

Return Value

This method returns a new serializer instance.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;

$tx = TransferBuilder::new()
  ->value(0)
  ->gasPrice(5000000000)
  ->gasLimit(21000)
  ->to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  ->sign('this is a top secret passphrase')
  ->transaction;

$serializer = Serializer::new($transaction);

>> Serializer

Serialize a transaction

public function serialize(array $options = []): Buffer

Parameters

TypeNameRequiredDescription
arrayoptionsNoSerialization options

Return Value

This method serializes the transaction and returns it as a Buffer.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;
use ArkEcosystem\Crypto\Transactions\Serializer;

$tx = TransferBuilder::new()
  ->value(0)
  ->gasPrice(5000000000)
  ->gasLimit(21000)
  ->to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  ->sign('this is a top secret passphrase')
  ->transaction;

$serialized = (Serializer::new($tx))->serialize();

>> Buffer

Deserializer

Crypto / API Documentation / Transactions / Deserializer

Create a new deserializer instance

public static function new(string $serialized)

Parameters

TypeNameRequiredDescription
stringserializedYesSerialized transaction data

Return Value

This method returns a new deserializer instance.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;
use ArkEcosystem\Crypto\Transactions\Deserializer;

$tx = TransferBuilder::new()
  ->value(0)
  ->gasPrice(5000000000)
  ->gasLimit(21000)
  ->to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  ->sign('this is a top secret passphrase')
  ->transaction;

$deserializer = Deserializer::new($tx->serialize()->getHex());

>> Deserializer

Deserialize a transaction

public function deserialize(): AbstractTransaction

Return Value

This method deserializes the transaction and returns an instance of AbstractTransaction.

use ArkEcosystem\Crypto\Transactions\Builder\TransferBuilder;
use ArkEcosystem\Crypto\Transactions\Deserializer;

$tx = TransferBuilder::new()
  ->value(0)
  ->gasPrice(5000000000)
  ->gasLimit(21000)
  ->to('0x6F0182a0cc707b055322CcF6d4CB6a5Aff1aEb22')
  ->sign('this is a top secret passphrase')
  ->transaction;

$transaction = Deserializer::new($tx->serialize()->getHex())->deserialize();

>> AbstractTransaction

Decode the payload of a transaction

public static function decodePayload(array $data, ContractAbiType $abiType = ContractAbiType::CONSENSUS): ?array

Parameters

TypeNameRequiredDescription
arraydataYesThe array containing the payload data to decode.
ContractAbiTypeabiTypeNoContract ABI Type to decode against

Return Value

This method decodes the payload data of a transaction and returns it as an array. If decoding fails, it returns `null`.

use ArkEcosystem\Crypto\Transactions\Deserializer;
use ArkEcosystem\Crypto\Enums\ContractAbiType;

$payloadData = Deserializer::decodePayload($transactionData)

>> {'args': [...], ...}

$payloadData = Deserializer::decodePayload($transactionData, ContractAbiType::USERNAMES)

>> {'args': [...], ...}

Message

Crypto / API Documentation / Utils / Message

Create a new message instance

public static function new($message)

Parameters

TypeNameRequiredDescription
mixedmessageYesMessage data (object, array, or JSON string)

Return Value

This method returns a new message instance.

use ArkEcosystem\Crypto\Utils\Message;

$message = Message::new((object) array(
  'publicKey' => '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
  'signature' => '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
  'message' => 'Hello World'
));

>> Message

Sign a message using the given passphrase

public static function sign(string $message, string $passphrase)

Parameters

TypeNameRequiredDescription
stringmessageYesMessage to sign
stringpassphraseYesPassphrase for signing

Return Value

This method signs the message and returns a new Message instance.

use ArkEcosystem\Crypto\Utils\Message;

$signedMessage = Message::sign('Hello World', 'this is a top secret passphrase');

>> Message

Verify the message contents

public function verify()

Return Value

This method verifies the message and returns a boolean value indicating validity.

use ArkEcosystem\Crypto\Utils\Message;

$signedMessage = Message::sign('Hello World', 'this is a top secret passphrase');
$isValid = $signedMessage->verify();

>> true

Convert the message to an array

public function toArray()

Return Value

This method converts the message to an array and returns it.

use ArkEcosystem\Crypto\Utils\Message;

$signedMessage = Message::sign('Hello World', 'this is a top secret passphrase');
$arrayMessage = $signedMessage->toArray();

>> [
  'publicKey' => '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
  'signature' => '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
  'message' => 'Hello World'
]

Convert the message to its string representation

public function __toString()

Return Value

This method returns the string representation of the message in JSON format.

use ArkEcosystem\Crypto\Utils\Message;

$message = new Message((object) array(
  'publicKey' => '034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192',
  'signature' => '0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b',
  'message' => 'Hello World'
));

(string) $message;

>> '{"publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b","message":"Hello World"}'

Convert the message to its JSON representation

public function toJson()

Return Value

This method converts the message to a JSON string and returns it.

use ArkEcosystem\Crypto\Utils\Message;

$signedMessage = Message::sign('Hello World', 'this is a top secret passphrase');

$jsonMessage = $message->toJson();

>> '{"publickey":"034151a3ec46b5670a682b0a63394f863587d1bc97483b1b6c70eb58e7f0aed192","signature":"0x6fc765ffd60aec43dc5eff277173006c8902c4a044712f1c10fb6ea1707b0199092235829bceb59a55a83556e44599bcc5677c3a28d8532ca72db183281a48dc1b","message":"Hello World"}'

Slot

Crypto / API Documentation / Utils / Slot

Get the time difference between now and network start

public static function time()

Return Value

This method returns the time difference between now and the network start in int format.

use ArkEcosystem\Crypto\Utils\Slot;

$timeDiff = Slot::time();

>> 200366014

Get the network start epoch

public static function epoch()

Return Value

This method returns the network start epoch in int format.

use ArkEcosystem\Crypto\Utils\Slot;

$epoch = Slot::epoch();

>> 1490101200

ArgumentDecoder

Crypto / API Documentation / Utils / Abi / ArgumentDecoder

Create a new argument decoder instance

public function __construct(string $bytes)

Parameters

TypeNameRequiredDescription
stringbytesYesThe encoded bytes to be decoded

Return Value

This method returns a new instance of the ArgumentDecoder class.

use ArkEcosystem\Crypto\Utils\Abi\ArgumentDecoder;

$decoder = new ArgumentDecoder($argument);

>> ArgumentDecoder

Decode a string

public function decodeString(): string

Return Value

This method returns the decoded string.

use ArkEcosystem\Crypto\Utils\Abi\ArgumentDecoder;

$decoder = new ArgumentDecoder($argument);

$string = $decoder->decodeString();

>> string

Decode an Address

public function decodeAddress(): string

Return Value

This method returns the decoded address.

use ArkEcosystem\Crypto\Utils\Abi\ArgumentDecoder;

$decoder = new ArgumentDecoder($argument);

$address = $decoder->decodeAddress();

>> string

Decode an unsigned integer

public function decodeUnsignedInt(): string

Return Value

This method returns an unsigned integer as a string.

use ArkEcosystem\Crypto\Utils\Abi\ArgumentDecoder;

$decoder = new ArgumentDecoder($argument);

$value = $decoder->decodeUnsignedInt();

>> string

Decode a signed integer

public function decodeSignedInt(): string

Return Value

This method returns an signed integer as a string.

use ArkEcosystem\Crypto\Utils\Abi\ArgumentDecoder;

$decoder = new ArgumentDecoder($argument);

$value = $decoder->decodeSignedInt();

>> string

Decode a boolean

public function decodeBool(): bool

Return Value

This method returns a boolean.

use ArkEcosystem\Crypto\Utils\Abi\ArgumentDecoder;

$decoder = new ArgumentDecoder($argument);

$value = $decoder->decodeBool();

>> bool

UnitConverter

Crypto / API Documentation / Utils / UnitConverter

Parse a value into the appropriate units

public static function parseUnits($value, string $unit = 'ark'): string

Parameters

TypeNameRequiredDescription
float|int|stringvalueYesThe value to be converted
stringunitNoThe unit to convert to (default is 'ark')

Return Value

This method returns the value converted to the smallest unit as a string.

use ArkEcosystem\Crypto\Utils\UnitConverter;

$weiValue = UnitConverter::parseUnits(1, 'wei');

>> '1'

$gweiValue = UnitConverter::parseUnits(1, 'gwei');

>> '1000000000'

$arkValue = UnitConverter::parseUnits(1, 'ark');

>> '1000000000000000000'

$arkDecimalValue = UnitConverter::parseUnits(0.1, 'ark');

>> '100000000000000000'

Format a value from smaller units to a larger unit

public static function formatUnits(string $value, string $unit = 'ark'): float

Parameters

TypeNameRequiredDescription
stringvalueYesThe value in the smallest unit (as a string)
stringunitNoThe unit to format to (default is 'ark')

Return Value

This method returns the value converted from the smallest unit to the specified unit as a float.

use ArkEcosystem\Crypto\Utils\UnitConverter;

$formattedWei = UnitConverter::formatUnits('1', 'wei');

>> 1.0

$formattedGwei = UnitConverter::formatUnits('1000000000', 'gwei');

>> 1.0

$formattedArk = UnitConverter::formatUnits('1000000000000000000', 'ark');

>> 1.0

$formattedArkDecimal = UnitConverter::formatUnits('100000000000000000', 'ark');

>> 0.1

Format a wei value to an ark value

public static function weiToArk(string | int | float $value, ?string $suffix = null): string

Parameters

TypeNameRequiredDescription
string | int | floatvalueYesThe wei value (as a string, int or float)
stringsuffixNoThe suffix to show after the formatted value (default is null)

Return Value

This method returns the wei value formatted as an ARK value.

use ArkEcosystem\Crypto\Utils\UnitConverter;

$formattedValue = UnitConverter::weiToArk(1, 'DARK');

>> 0.000000000000000001 DARK

$formattedValue = UnitConverter::weiToArk(1);

>> 0.000000000000000001

$formattedValue = UnitConverter::weiToArk('1000000000000000000', 'DARK');

>> 1 DARK

$formattedValue = UnitConverter::weiToArk('1000000000000000000');

>> 1

Format a gwei value to an ark value

public static function gweiToArk(string | int | float $value, ?string $suffix = null): string

Parameters

TypeNameRequiredDescription
string | int | floatvalueYesThe gwei value (as a string, int or float)
stringsuffixNoThe suffix to show after the formatted value (default is null)

Return Value

This method returns the gwei value formatted as an ARK value.

use ArkEcosystem\Crypto\Utils\UnitConverter;

$formattedValue = UnitConverter::gweiToArk(1, 'DARK');

>> 0.000000001 DARK

$formattedValue = UnitConverter::gweiToArk(1);

>> 0.000000001

$formattedValue = UnitConverter::gweiToArk('1000000000', 'DARK');

>> 1 DARK

$formattedValue = UnitConverter::gweiToArk('1000000000');

>> 1