Skip to content

Commit eccfd47

Browse files
committed
replace bitwasp/secp256k1-php with simplito/elliptic-php
1 parent fa2d39f commit eccfd47

File tree

3 files changed

+80
-37
lines changed

3 files changed

+80
-37
lines changed

README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# php-graphene-node-client
2-
PHP client for connection to [VIZ](https://github.com/viz-blockchain)/[STEEM](https://github.com/steemit)/[GOLOS](https://github.com/goloschain)/[WHALESHARES](https://gitlab.com/beyondbitcoin) node
2+
PHP client for connection to [VIZ](https://github.com/viz-blockchain)/[STEEM](https://github.com/steemit)/[GOLOS](https://github.com/golos-blockchain/)/[WHALESHARES](https://gitlab.com/beyondbitcoin) node
33

44

55
## Install Via Composer
@@ -8,15 +8,12 @@ PHP client for connection to [VIZ](https://github.com/viz-blockchain)/[STEEM](ht
88
composer require t3ran13/php-graphene-node-client
99
```
1010
#### with broadcast (sending transactions to blockchain)
11-
\(details are [here](https://golos.io/ru--otkrytyij-kod/@php-node-client/podklyuchenie-secp256k1-php-k-php-dockerfile)\) and actual dockerfile and requests examples see in branch ["debug"]()https://github.com/t3ran13/php-graphene-node-client/tree/debug)
11+
actual dockerfile and requests examples see in branch ["debug"]()https://github.com/t3ran13/php-graphene-node-client/tree/debug)
1212

1313
install components
14-
- automake
15-
- libtool
1614
- libgmp-dev
1715

1816
install extensions
19-
- Bit-Wasp/secp256k1-php v0.2.1 \(how to install [secp256k1-php](https://github.com/Bit-Wasp/secp256k1-php)\)
2017
- gmp
2118

2219

Tools/Transaction.php

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44
namespace GrapheneNodeClient\Tools;
55

66

7+
use Elliptic\EC;
8+
use Elliptic\EC\Signature;
79
use GrapheneNodeClient\Commands\CommandQueryData;
810
use GrapheneNodeClient\Commands\CommandQueryDataInterface;
911
use GrapheneNodeClient\Commands\Single\GetBlockCommand;
1012
use GrapheneNodeClient\Commands\Single\GetDynamicGlobalPropertiesCommand;
11-
use GrapheneNodeClient\Commands\Single\LoginCommand;
1213
use GrapheneNodeClient\Connectors\ConnectorInterface;
1314
use GrapheneNodeClient\Tools\ChainOperations\OperationSerializer;
1415
use t3ran13\ByteBuffer\ByteBuffer;
@@ -152,6 +153,64 @@ public static function sign($chainName, CommandQueryDataInterface $trxData, $pri
152153
}
153154

154155

156+
// /**
157+
// * @param string $msg serialized Tx with prefix chain id
158+
// * @param string $privateWif
159+
// *
160+
// * @return string hex
161+
// * @throws \Exception
162+
// */
163+
// protected static function signOperation($msg, $privateWif)
164+
// {
165+
// $context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
166+
//
167+
// $msg32 = hash('sha256', hex2bin($msg), true);
168+
// $privateKey = Auth::PrivateKeyFromWif($privateWif);
169+
//
170+
// /** @var resource $signature */
171+
// $signatureRec = null;
172+
// $i = 0;
173+
// while (true) {
174+
// if ($i === 1) {
175+
// //sing always the same
176+
// throw new TransactionSignException("Can't to find canonical signature, {$i} ties");
177+
// }
178+
// $i++;
179+
//// echo "\n i=" . print_r($i, true) . '<pre>'; //FIXME delete it
180+
// if (secp256k1_ecdsa_sign_recoverable($context, $signatureRec, $msg32, $privateKey) !== 1) {
181+
// throw new TransactionSignException("Failed to create recoverable signature");
182+
// }
183+
//
184+
// $signature = null;
185+
// if (secp256k1_ecdsa_recoverable_signature_convert($context, $signature, $signatureRec) !== 1) {
186+
// throw new TransactionSignException("Failed to create signature");
187+
// }
188+
// $der = null;
189+
// if (secp256k1_ecdsa_signature_serialize_der($context, $der, $signature) !== 1) {
190+
// throw new TransactionSignException("Failed to create DER");
191+
// }
192+
//// echo "\n" . print_r(bin2hex($der), true) . '<pre>'; //FIXME delete it
193+
//
194+
// echo PHP_EOL . 'der 1: ' . print_r(bin2hex($der), true) . ''; //FIXME delete it
195+
// if (self::isSignatureCanonical($der)) {
196+
// break;
197+
// }
198+
// }
199+
//
200+
// $serializedSig = null;
201+
// $recid = 0;
202+
// secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $serializedSig, $recid, $signatureRec);
203+
// echo PHP_EOL . 'serializedSig 1: ' . print_r(bin2hex($serializedSig), true) . ''; //FIXME delete it
204+
// $serializedSig = hex2bin(base_convert($recid + 4 + 27, 10, 16)) . $serializedSig;
205+
// $length = strlen($serializedSig);
206+
// if ($length !== 65) {
207+
// throw new \Exception('Expecting 65 bytes for Tx signature, instead got ' . $length);
208+
// }
209+
//
210+
// return bin2hex($serializedSig);
211+
// }
212+
213+
155214
/**
156215
* @param string $msg serialized Tx with prefix chain id
157216
* @param string $privateWif
@@ -161,50 +220,41 @@ public static function sign($chainName, CommandQueryDataInterface $trxData, $pri
161220
*/
162221
protected static function signOperation($msg, $privateWif)
163222
{
164-
$context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY);
223+
$ec = new EC('secp256k1');
165224

166-
$msg32 = hash('sha256', hex2bin($msg), true);
167-
$privateKey = Auth::PrivateKeyFromWif($privateWif);
225+
$msg32Hex = hash('sha256', hex2bin($msg), false);
226+
$privateKeyHex = bin2hex(Auth::PrivateKeyFromWif($privateWif));
227+
$key = $ec->keyFromPrivate($privateKeyHex, 'hex');
168228

169-
/** @var resource $signature */
170-
$signatureRec = null;
171229
$i = 0;
172230
while (true) {
173231
if ($i === 1) {
174232
//sing always the same
175233
throw new TransactionSignException("Can't to find canonical signature, {$i} ties");
176234
}
177235
$i++;
178-
// echo "\n i=" . print_r($i, true) . '<pre>'; //FIXME delete it
179-
if (secp256k1_ecdsa_sign_recoverable($context, $signatureRec, $msg32, $privateKey) !== 1) {
180-
throw new TransactionSignException("Failed to create recoverable signature");
181-
}
182236

183-
$signature = null;
184-
if (secp256k1_ecdsa_recoverable_signature_convert($context, $signature, $signatureRec) !== 1) {
185-
throw new TransactionSignException("Failed to create signature");
186-
}
187-
$der = null;
188-
if (secp256k1_ecdsa_signature_serialize_der($context, $der, $signature) !== 1) {
189-
throw new TransactionSignException("Failed to create DER");
190-
}
191-
// echo "\n" . print_r(bin2hex($der), true) . '<pre>'; //FIXME delete it
192-
if (self::isSignatureCanonical($der)) {
237+
$signature = $key->sign($msg32Hex, 'hex', ['canonical' => true]);
238+
/** @var Signature $signature*/
239+
240+
241+
$der = $signature->toDER('hex');
242+
if (self::isSignatureCanonical(hex2bin($der))) {
193243
break;
194244
}
195245
}
196246

197-
$serializedSig = null;
198-
$recid = 0;
199-
secp256k1_ecdsa_recoverable_signature_serialize_compact($context, $serializedSig, $recid, $signatureRec);
247+
$recid = $ec->getKeyRecoveryParam($msg32Hex, $signature, $key->getPublic());
248+
249+
$compactSign = $signature->r->toString(16) . $signature->s->toString(16);
250+
$serializedSig = base_convert($recid + 4 + 27, 10, 16) . $compactSign;
200251

201-
$serializedSig = hex2bin(base_convert($recid + 4 + 27, 10, 16)) . $serializedSig;
202252
$length = strlen($serializedSig);
203-
if ($length !== 65) {
253+
if ($length !== 130) { //65 symbols
204254
throw new \Exception('Expecting 65 bytes for Tx signature, instead got ' . $length);
205255
}
206256

207-
return bin2hex($serializedSig);
257+
return $serializedSig;
208258
}
209259

210260

@@ -217,12 +267,8 @@ public static function isSignatureCanonical($der)
217267
{
218268
$buffer = new ByteBuffer();
219269
$buffer->write($der);
220-
// lenR = der[3];
221-
// lenS = der[5 + lenR];
222-
// if (lenR === 32 && lenS === 32) {
223270
$lenR = $buffer->readInt8(3);
224271
$lenS = $buffer->readInt8(5 + $lenR);
225-
// echo "\n" . var_dump($lenR, $lenS) . '<pre>'; //FIXME delete it
226272

227273
return $lenR === 32 && $lenS === 32;
228274
}

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
"require": {
1414
"php": "^7.0",
1515
"textalk/websocket": "^1.2",
16-
"bitwasp/secp256k1-php": "^0.1.2",
1716
"stephenhill/base58": "^1.1",
18-
"t3ran13/bytebuffer": "^1.0"
17+
"t3ran13/bytebuffer": "^1.0",
18+
"simplito/elliptic-php": "^1.0"
1919
},
2020
"autoload": {
2121
"psr-4": { "GrapheneNodeClient\\": "" }

0 commit comments

Comments
 (0)