Examples
Initialization
1use ArkEcosystem\Crypto\Transactions\Builder\Transfer;
Transactions
A transaction is an object specifying the transfer of funds from the sender’s wallet to the recipient’s. Each transaction must be signed by the sender’s private key to prove authenticity and origin. After broadcasting through the client SDK , a transaction is permanently incorporated in the blockchain by a Delegate Node.
Sign
The crypto SDK can sign a transaction using your private key or passphrase (from which the private key is generated). Ensure you are familiar with digital signatures before using the crypto SDKs.
1$transaction = Transfer::new()2 ->recipient('DGihocTkwDygiFvmg6aG8jThYTic47GzU9')3 ->amount(1 * 10 ** 8)4 ->vendorField('This is a transaction from PHP')5 ->sign('This is a top secret passphrase');6 7echo gettype($transaction);8 9>>> Transfer
Serialize (AIP11)
Serialization of a transaction object ensures it is compact and properly formatted to be incorporated in the ARK blockchain. If you are using the crypto SDK in combination with the public API SDK, you should not need to serialize manually.
1use ArkEcosystem\Crypto\Transactions\Serializer;2 3$buffer = Serializer::new($transaction)->serialize();4 5echo gettype($buffer);6 7>>> String
Deserialize (AIP11)
A serialized transaction may be deserialized for inspection purposes. The public API does not return serialized transactions, so you should only need to deserialize in exceptional circumstances.
1use ArkEcosystem\Crypto\Transactions\Deserializer;2 3$transaction = Deserializer::new($serializedTransaction)->deserialize();4 5echo gettype($transaction);6 7>>> ArkTransaction
Message
The crypto SDK not only supports transactions but can also work with other arbitrary data (expressed as strings).
Sign
Signing a string works much like signing a transaction: in most implementations, the message is hashed, and the resulting hash is signed using the
private key
orpassphrase
.
1use ArkEcosystem\Crypto\Utils\Message;2 3$message = Message::sign('Hello World', 'this is a top secret passphrase');4 5echo gettype($message);6 7>>> self
Verify
A message’s signature can easily be verified by hash, without the private key that signed the message, by using the
verify
method.
1use ArkEcosystem\Crypto\Utils\Message; 2 3$message = Message::new([ 4 'publickey' => 'validPublicKey', 5 'signature' => 'validSignature', 6 'message' => 'Hello World' 7]); 8 9echo($message->verify() ? 'Valid' : 'Invalid');10 11>>> 'Valid' | 'Invalid'
Identities
The identities class allows for the creation and inspection of keyPairs from
passphrases
. Here you find vital functions when creating transactions and managing wallets.
Derive the Address from a Passphrase
1use ArkEcosystem\Crypto\Identities\Address;2 3$address = Address::fromPassphrase('this is a top secret passphrase');4 5echo gettype($address);6 7>>> string
Derive the Address from a Public Key
1use ArkEcosystem\Crypto\Identities\Address;2 3$address = Address::fromPublicKey('validPublicKey');4 5echo gettype($address);6 7>>> string
Derive the Address from a Private Key
1use ArkEcosystem\Crypto\Identities\Address;2 3$address = Address::fromPrivateKey('validPrivateKey');4 5echo gettype($address);6 7>>> string
Validate an Address
1use ArkEcosystem\Crypto\Identities\Address;2 3$address = Address::validate('validAddress');4 5echo gettype($address);6 7>>> bool
Private Key
As the name implies, private keys and passphrases are to remain private. Never store these unencrypted and minimize access to these secrets
Derive the Private Key from a Passphrase
1use ArkEcosystem\Crypto\Identities\PrivateKey;2 3$privateKey = PrivateKey::fromPassphrase('this is a top secret passphrase');4 5echo gettype($privateKey);6 7>>> EcPrivateKey
Derive the Private Key Instance Object from a Hexadecimal Encoded String
1use ArkEcosystem\Crypto\Identities\PrivateKey;2 3$privateKey = PrivateKey::fromHex('validHexString');4 5echo gettype($privateKey);6 7 8>>> EcPrivateKey
Derive the Private Key from a WIF
1use ArkEcosystem\Crypto\Identities\PrivateKey;2 3$privateKey = PrivateKey::fromWif('validWif');4 5echo gettype($privateKey);6 7>>> EcPrivateKey
Public Key
Public Keys may be freely shared, and are included in transaction objects to validate the authenticity.
Derive the Public Key from a Passphrase
1use ArkEcosystem\Crypto\Identities\PublicKey;2 3$publicKey = PublicKey::fromPassphrase('this is a top secret passphrase');4 5echo gettype($publicKey);6 7>>> EcPublicKey
Derive the Public Key Instance Object from a Hexadecimal Encoded String
1use ArkEcosystem\Crypto\Identities\PublicKey;2 3$publicKey = PublicKey::fromHex('validHexString');4 5echo gettype($publicKey);6 7>>> EcPublicKey
Validate a Public Key
1use ArkEcosystem\Crypto\Identities\PublicKey;2 3$publicKey = PublicKey::validate('validPublicKey');4 5echo gettype($publicKey);6 7>>> EcPublicKey
WIF
The WIF should remain secret, just like your
passphrase
andprivate key
.
Derive the WIF from a Passphrase
1use ArkEcosystem\Crypto\Identities\WIF;2 3$wif = WIF::fromPassphrase('this is a top secret passphrase');4 5echo gettype($wif);6 7>>> string