-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added examples and added setPrivateKeyWithWif method
- Loading branch information
Showing
5 changed files
with
169 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
require_once '../src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php'; | ||
|
||
use BitcoinPHP\BitcoinECDSA\BitcoinECDSA; | ||
|
||
$bitcoinECDSA = new BitcoinECDSA(); | ||
$bitcoinECDSA->generateRandomPrivateKey(); //generate new random private key | ||
$address = $bitcoinECDSA->getAddress(); //compressed Bitcoin address | ||
echo "Address: " . $address . PHP_EOL; | ||
|
||
//Validate an address (Verify the checksum) | ||
if($bitcoinECDSA->validateAddress($address)) { | ||
echo "The address is valid" . PHP_EOL; | ||
} else { | ||
echo "The address is invalid" . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
require_once '../src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php'; | ||
|
||
use BitcoinPHP\BitcoinECDSA\BitcoinECDSA; | ||
|
||
$bitcoinECDSA = new BitcoinECDSA(); | ||
$bitcoinECDSA->generateRandomPrivateKey(); //generate new random private key | ||
|
||
$message = "Test message"; | ||
$signedMessage = $bitcoinECDSA->signMessage($message); | ||
|
||
echo "signed message:" . PHP_EOL; | ||
echo $signedMessage . PHP_EOL; | ||
|
||
/** | ||
* Will print something like this: | ||
-----BEGIN BITCOIN SIGNED MESSAGE----- | ||
Test message | ||
-----BEGIN SIGNATURE----- | ||
1L56ndSQ1LfrAB2xyo3ZN7egiW4nSs8KWS | ||
HxTqM+b3xj2Qkjhhl+EoUpYsDUz+uTdz6RCY7Z4mV62yOXJ3XCAfkiHV+HGzox7Ba/OC6bC0y6zBX0GhB7UdEM0= | ||
-----END BITCOIN SIGNED MESSAGE----- | ||
*/ | ||
|
||
|
||
// If you only want the signature you can do this | ||
$signature = $bitcoinECDSA->signMessage($message, true); | ||
|
||
echo "signature:" . PHP_EOL; | ||
echo $signature . PHP_EOL; | ||
/** | ||
* Will print something like this: | ||
HxTqM+b3xj2Qkjhhl+EoUpYsDUz+uTdz6RCY7Z4mV62yOXJ3XCAfkiHV+HGzox7Ba/OC6bC0y6zBX0GhB7UdEM0= | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
require_once '../src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php'; | ||
|
||
use BitcoinPHP\BitcoinECDSA\BitcoinECDSA; | ||
|
||
$bitcoinECDSA = new BitcoinECDSA(); | ||
|
||
//To verify a message like this one | ||
$rawMessage = "-----BEGIN BITCOIN SIGNED MESSAGE----- | ||
Test message | ||
-----BEGIN SIGNATURE----- | ||
1L56ndSQ1LfrAB2xyo3ZN7egiW4nSs8KWS | ||
HxTqM+b3xj2Qkjhhl+EoUpYsDUz+uTdz6RCY7Z4mV62yOXJ3XCAfkiHV+HGzox7Ba/OC6bC0y6zBX0GhB7UdEM0= | ||
-----END BITCOIN SIGNED MESSAGE-----"; | ||
|
||
if($bitcoinECDSA->checkSignatureForRawMessage($rawMessage)) { | ||
echo "Message verified" . PHP_EOL; | ||
} else { | ||
echo "Couldn't verify message" . PHP_EOL; | ||
} | ||
|
||
// alternatively | ||
$signature = "HxTqM+b3xj2Qkjhhl+EoUpYsDUz+uTdz6RCY7Z4mV62yOXJ3XCAfkiHV+HGzox7Ba/OC6bC0y6zBX0GhB7UdEM0="; | ||
$address = "1L56ndSQ1LfrAB2xyo3ZN7egiW4nSs8KWS"; | ||
$message = "Test message"; | ||
|
||
if($bitcoinECDSA->checkSignatureForMessage($address, $signature, $message)) { | ||
echo "Message verified" . PHP_EOL; | ||
} else { | ||
echo "Couldn't verify message" . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
require_once '../src/BitcoinPHP/BitcoinECDSA/BitcoinECDSA.php'; | ||
|
||
use BitcoinPHP\BitcoinECDSA\BitcoinECDSA; | ||
|
||
$bitcoinECDSA = new BitcoinECDSA(); | ||
$bitcoinECDSA->generateRandomPrivateKey(); //generate new random private key | ||
|
||
$wif = $bitcoinECDSA->getWif(); | ||
$address = $bitcoinECDSA->getAddress(); | ||
echo "Address : " . $address . PHP_EOL; | ||
echo "WIF : " . $wif . PHP_EOL; | ||
|
||
unset($bitcoinECDSA); //destroy instance | ||
|
||
//import wif | ||
$bitcoinECDSA = new BitcoinECDSA(); | ||
if($bitcoinECDSA->validateWifKey($wif)) { | ||
$bitcoinECDSA->setPrivateKeyWithWif($wif); | ||
$address = $bitcoinECDSA->getAddress(); | ||
echo "imported address : " . $address . PHP_EOL; | ||
} else { | ||
echo "invalid WIF key" . PHP_EOL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters