Activeledger - PHP SDK
The Activeledger PHP SDK has been built to provide an easy way to connect your php web application to an Activeledger Network
Activeledger
Visit Activeledger.io
Read Activeledgers documentation
Installation
$ composer require activeledger/sdk ~0.0.1
$ composer install -o
Usage
The SDK currently supports the following functionality
- Connection handling
- Key generation
- Key onboarding
- Transaction building
- Encrypted & unencrypted transaction posting
Once the SDK has been installed import the classes with the autoloader
require __DIR__ . '/vendor/autoload.php';
Connection
When sending a transaction, you must pass a connection that provides the information needed to establish a link to the network and specified node.
To do this a connection object must be created. This object must be passed the protocol, address, port, and the encryption flag.
Example
require __DIR__ . '/vendor/autoload.php';
$connection = new activeledger\sdk\Connection("http", "127.0.0.1", 5260, false);
$connection = new activeledger\sdk\Connection("http", "127.0.0.1", 5260, true);
Key
The Key class can be used to generate a key.
There are two key types that can be generated currently, more are planned and will be implemented into Activeledger first. These types are RSA and Elliptic Curve.
Generating a key
When generating a key the default is an Elliptic Curve key. The object returned will be a KeyIdentity class.
Example
require __DIR__ . '/vendor/autoload.php';
$identity = activeledger\sdk\Key::generateKeyIdentity();
$identity = activeledger\sdk\Key::generateKeyIdentity("rsa");
Onboarding a key
Once you have a key generated, to use it to sign transactions it must be onboarded to the ledger network
Example
require __DIR__ . '/vendor/autoload.php';
$connection = new activeledger\sdk\Connection("http", "127.0.0.1", 5260, false);
$identity = activeledger\sdk\Key::generateKeyIdentity();
try {
$stream = $identity->onBoard($connection);
}catch(Exception $error) {
die($error->getMessage());
}
Exporting Key
To save the key for later use you can use the php serialize function
require __DIR__ . '/vendor/autoload.php';
$identity = activeledger\sdk\Key::generateKeyIdentity();
$export = base64_encode(serialize($identity));
Importing Key
You can import a key using php unserialize function which restores the object
require __DIR__ . '/vendor/autoload.php';
$identity = serialize(base64_decode($export))
Transaction
The Transaction class contains a static function which helps build an Activeledger transaction object.
require __DIR__ . '/vendor/autoload.php';
$transaction = activeledger\sdk\Transaction::build(
"Contract Stream / Label",
"Namespace",
// Input Streams (Requires Signatures)
array("Identity Stream" => array()),
array("Outputs: Identity Stream" => array()),
array("Reads : Label" => "Idenity Stream")
);
Signing & sending a transaction
When signing a transaction you must send the finished version of it. No changes can be made after signing as this will cause the ledger to reject it.
The key must be one that has been successfully onboarded to the ledger which the transaction is being sent to.
Example
require __DIR__ . '/vendor/autoload.php';
$connection = new activeledger\sdk\Connection("http", "127.0.0.1", 5260, false);
$identity = activeledger\sdk\Key::generateKeyIdentity();
try {
$stream = $identity->onBoard($connection);
$transaction = activeledger\sdk\Transaction::build(
"0000482232bcca98e3f2b375e6209400e185fae26efad7812268d7d66b321131",
"example-namespace",
// Input Streams (Requires Signatures)
array($stream => array("text" => "Hello World"))
);
$signedTx = $identity->sign($transaction)
$response = $connection->send($signedTx);
$response->streams->new;
$response->streams->updated;
}catch(Exception $error) {
die($error->getMessage());
}
License
This project is licensed under the MIT License