personal.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. require('./exampleBase.php');
  3. $personal = $web3->personal;
  4. $newAccount = '';
  5. echo 'Personal Create Account and Unlock Account' . PHP_EOL;
  6. // create account
  7. $personal->newAccount('123456', function ($err, $account) use (&$newAccount) {
  8. if ($err !== null) {
  9. echo 'Error: ' . $err->getMessage();
  10. return;
  11. }
  12. $newAccount = $account;
  13. echo 'New account: ' . $account . PHP_EOL;
  14. });
  15. $personal->unlockAccount($newAccount, '123456', function ($err, $unlocked) {
  16. if ($err !== null) {
  17. echo 'Error: ' . $err->getMessage();
  18. return;
  19. }
  20. if ($unlocked) {
  21. echo 'New account is unlocked!' . PHP_EOL;
  22. } else {
  23. echo 'New account isn\'t unlocked' . PHP_EOL;
  24. }
  25. });
  26. // get balance
  27. $web3->eth->getBalance($newAccount, function ($err, $balance) {
  28. if ($err !== null) {
  29. echo 'Error: ' . $err->getMessage();
  30. return;
  31. }
  32. echo 'Balance: ' . $balance->toString() . PHP_EOL;
  33. });
  34. // remember to lock account after transaction
  35. $personal->lockAccount($newAccount, function ($err, $locked) {
  36. if ($err !== null) {
  37. echo 'Error: ' . $err->getMessage();
  38. return;
  39. }
  40. if ($locked) {
  41. echo 'New account is locked!' . PHP_EOL;
  42. } else {
  43. echo 'New account isn\'t locked' . PHP_EOL;
  44. }
  45. });