Skip to content

Commit e194d70

Browse files
committed
Initial Commit
0 parents  commit e194d70

9 files changed

+606
-0
lines changed

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Path-based git attributes
2+
# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html
3+
4+
# Ignore all test and documentation with "export-ignore".
5+
/.gitattributes export-ignore
6+
/.gitignore export-ignore
7+
/.travis.yml export-ignore
8+
/phpunit.xml.dist export-ignore
9+
/.scrutinizer.yml export-ignore
10+
/tests export-ignore
11+
/docs export-ignore

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build
2+
composer.lock
3+
vendor

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
MIT License (MIT)
2+
Copyright (c) 2018 Activeledger
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
<img src="https://www.activeledger.io/wp-content/uploads/2018/09/Asset-23.png" alt="Activeledger" width="500"/>
3+
4+
Activeledger - PHP SDK
5+
=======================================
6+
7+
The Activeledger PHP SDK has been build to provide an easy way to connect your php web application to an Activeledger Network
8+
9+
### Activeledger
10+
11+
[Visit Activeledger.io](https://activeledger.io/)
12+
13+
[Read Activeledgers documentation](https://github.com/activeledger/activeledger)
14+
15+
## License
16+
***
17+
18+
This project is licensed under the [MIT](https://github.com/activeledger/activeledger/blob/master/LICENSE) License

composer.json

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
{
2+
"name": "activeledger/sdk",
3+
"type": "library",
4+
"description": "PHP package for use when integrating with Activeledger",
5+
"keywords": [
6+
"activeledger",
7+
"sdk",
8+
"blockchain",
9+
"dlt",
10+
"distributed ledger technology",
11+
"ledger",
12+
"smart contracts",
13+
"activeledger",
14+
"network",
15+
"node"
16+
],
17+
"homepage": "http://www.activeledger.io",
18+
"license": "MIT",
19+
"authors": [
20+
{
21+
"name": "Activeledger"
22+
}
23+
],
24+
"require": {
25+
"php" : "~5.6|~7.0"
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit" : ">=5.4.3",
29+
"squizlabs/php_codesniffer": "^2.3"
30+
},
31+
"autoload": {
32+
"psr-4": {
33+
"activeledger\\": "src"
34+
}
35+
},
36+
"autoload-dev": {
37+
"psr-4": {
38+
"activeledger\\": "tests"
39+
}
40+
},
41+
"scripts": {
42+
"test": "phpunit",
43+
"check-style": "phpcs -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests",
44+
"fix-style": "phpcbf -p --standard=PSR2 --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests"
45+
},
46+
"extra": {
47+
"branch-alias": {
48+
"dev-master": "1.0-dev"
49+
}
50+
},
51+
"config": {
52+
"sort-packages": true
53+
}
54+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="activeledger Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/connection.php

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
<?php
2+
/**
3+
* MIT License (MIT)
4+
* Copyright (c) 2018 Activeledger
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
namespace activeledger\sdk;
26+
27+
/**
28+
* Handles the Activeledger Node Connection
29+
*
30+
*/
31+
class Connection
32+
{
33+
/**
34+
* Node URL
35+
*
36+
* @var string
37+
*/
38+
private $_url;
39+
40+
/**
41+
* Node Public PEM
42+
*
43+
* @var string
44+
*/
45+
private $_pem;
46+
47+
/**
48+
* Request Headers (Encrypt or Normal)
49+
*
50+
* @var array
51+
*/
52+
private $_headers;
53+
54+
/**
55+
* Create Activeledger Connection Object
56+
*
57+
* @param string $protocol http | https
58+
* @param string $address location
59+
* @param int $port port
60+
* @param boolean $encrypt Encrypted Transactios?
61+
*/
62+
public function __construct($protocol, $address, $port, $encrypt)
63+
{
64+
// Build Connection Url
65+
$this->_url = $protocol."://".$address.":".$port;
66+
67+
// Setup Headers
68+
$this->_headers = array(
69+
"Content-type: application/json"
70+
);
71+
72+
if ($encrypt) {
73+
// Fetch Nodes Public Key
74+
$this->_pem = base64_decode($this->_getStatus()->pem);
75+
76+
// Modify Headers For Encrypt
77+
$this->_headers[0] = "Content-type: plain/text";
78+
$this->_headers[] = "X-Activeledger-Encrypt: 1";
79+
}
80+
}
81+
82+
/**
83+
* Get Nodes status
84+
*
85+
* @return object
86+
*/
87+
private function _getStatus()
88+
{
89+
return json_decode(
90+
file_get_contents($this->_url."/a/status")
91+
);
92+
}
93+
94+
/**
95+
* Makes the HTTP request to send transaction
96+
*
97+
* @param string $content string posted to Activeledger
98+
*
99+
* @return object
100+
*/
101+
private function _submit($content)
102+
{
103+
// Build Request Context
104+
$context = stream_context_create(
105+
array(
106+
'http' => array(
107+
'method' => 'POST',
108+
'header' => $this->_headers,
109+
'content' => $content,
110+
'timeout' => 60
111+
)
112+
)
113+
);
114+
115+
// Send Request & Get Response or Manage Error
116+
if (!$response = file_get_contents($this->_url, false, $context)) {
117+
// Get The Last Error
118+
$error = error_get_last();
119+
120+
// Make Friendly Message
121+
$message = substr($error['message'], strpos($error['message'], ": ") + 2);
122+
123+
// Throw Error
124+
throw new \Exception($message);
125+
}
126+
127+
// return response
128+
return json_decode($response);
129+
}
130+
131+
/**
132+
* Send Transaction to Activeledger Network
133+
*
134+
* @param object $txBody Activeledger Transaction Object
135+
*
136+
* @return object
137+
*/
138+
public function send($txBody)
139+
{
140+
// Encrypted Connection?
141+
if ($this->_pem) {
142+
// Encrypt with the nodes public address
143+
openssl_public_encrypt(
144+
json_encode($txBody, JSON_UNESCAPED_SLASHES),
145+
$encrypted,
146+
$this->_pem,
147+
OPENSSL_PKCS1_OAEP_PADDING
148+
);
149+
150+
// Submit Transaction as Base54 Encoded body
151+
return $this->_submit(base64_encode($encrypted));
152+
}
153+
154+
// Normal Transaction Send as JSON
155+
$response = $this->_submit(json_encode($txBody, JSON_UNESCAPED_SLASHES));
156+
157+
// Did we get a response?
158+
if ($response && $response->{'$umid'}) {
159+
// Transform response to be php friendly
160+
$transform = new \stdClass;
161+
$transform->umid = $response->{'$umid'};
162+
$transform->summary = $response->{'$summary'};
163+
164+
// Debug Mode Enabled?
165+
if ($response->{'$debug'}) {
166+
$transform->debug = $response->{'$debug'};
167+
}
168+
169+
// Do we have errors?
170+
// Without commits (Only 1 node may error)
171+
if (isset($transform->summary->errors) && $transform->summary->commit == 0) {
172+
// Throw First Error
173+
throw new \Exception($transform->summary->errors[0]);
174+
}
175+
176+
// Make sure we have streams
177+
if ($response->{'$streams'}) {
178+
$transform->streams = $response->{'$streams'};
179+
}
180+
181+
return $transform;
182+
}
183+
184+
return false;
185+
}
186+
}

0 commit comments

Comments
 (0)