-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thomas Kerin
committed
Apr 8, 2018
1 parent
45e2ec7
commit 0770d8a
Showing
26 changed files
with
652 additions
and
86 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,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
require "vendor/autoload.php"; | ||
|
||
use BitWasp\Trezor\Bridge\Client; | ||
use BitWasp\Trezor\Bridge\Http\HttpClient; | ||
use BitWasp\Trezor\Device\Button\DebugButtonAck; | ||
use BitWasp\Trezor\Device\Command\LoadDeviceService; | ||
use BitWasp\Trezor\Device\RequestFactory; | ||
|
||
$depth = 0; | ||
$fingerprint = 0; | ||
$numChild = 0; | ||
$chainCode = \Protobuf\Stream::fromString(hex2bin('a86d0945bd342199a130b65255df75199fe09e539d60053003cc1c0e999982a5')); | ||
$privateKey = \Protobuf\Stream::fromString(hex2bin('874c62f2c98f7c94f1a691492825a71e8e9b9251f03c208f37d1ec9c9cda2b24')); | ||
$language = "EN"; | ||
|
||
$reqFactory = new RequestFactory(); | ||
$hdNode = $reqFactory->privateHdNode($depth, $fingerprint, $numChild, $chainCode, $privateKey); | ||
$loadDevice = $reqFactory->loadDeviceWithHdNode($hdNode, $language); | ||
|
||
$httpClient = HttpClient::forUri("http://localhost:21325"); | ||
$client = new Client($httpClient); | ||
$devices = $client->listDevices()->devices(); | ||
if (empty($devices)) { | ||
throw new \RuntimeException("No devices returned"); | ||
} | ||
|
||
$session = $client->acquire($devices[0]); | ||
$dbgSession = $client->acquire($devices[1]); | ||
|
||
$dbgBtnAck = new DebugButtonAck($dbgSession); | ||
$loadService = new LoadDeviceService($dbgBtnAck); | ||
$loaded = $loadService->call($session, $loadDevice); | ||
var_dump($loaded); |
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
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
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
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,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\Trezor\Device\Button; | ||
|
||
use BitWasp\Trezor\Bridge\Session; | ||
use BitWasp\TrezorProto\ButtonRequest; | ||
use BitWasp\TrezorProto\ButtonRequestType; | ||
|
||
abstract class ButtonAck | ||
{ | ||
abstract function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $allowType); | ||
} |
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,62 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\Trezor\Device\Button; | ||
|
||
use BitWasp\Trezor\Bridge\Session; | ||
use BitWasp\Trezor\Device\DebugMessage; | ||
use BitWasp\Trezor\Device\Message; | ||
use BitWasp\TrezorProto\ButtonRequest; | ||
use BitWasp\TrezorProto\ButtonRequestType; | ||
use BitWasp\TrezorProto\DebugLinkDecision; | ||
use BitWasp\TrezorProto\DebugLinkGetState; | ||
use BitWasp\TrezorProto\DebugLinkStop; | ||
use BitWasp\TrezorProto\Success; | ||
|
||
class DebugButtonAck extends ButtonAck | ||
{ | ||
private $debug; | ||
|
||
public function __construct(Session $debugSession) | ||
{ | ||
$this->debug = $debugSession; | ||
} | ||
|
||
public function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $expectedType) | ||
{ | ||
$theirType = $request->getCode(); | ||
if ($theirType->value() !== $expectedType->value()) { | ||
throw new \RuntimeException("Unexpected button request (expected: {$expectedType->name()}, got {$theirType->name()})"); | ||
} | ||
|
||
fwrite(STDERR, microtime() . " - debugButtonAck.sending button ack (async)\n"); | ||
$t1 = microtime(true); | ||
$ack = new \BitWasp\TrezorProto\ButtonAck(); | ||
|
||
$decision = new DebugLinkDecision(); | ||
$decision->setYesNo(true); | ||
|
||
fwrite(STDERR, microtime() . " - debugButtonAck.sending DECISION (async)\n"); | ||
$t1 = microtime(true); | ||
|
||
$success = $session->sendMessageAsync(Message::buttonAck($ack)); | ||
$debug = $this->debug->sendMessageAsync(DebugMessage::decision($decision), [ | ||
'Connection' => 'close', | ||
]); | ||
|
||
fwrite(STDERR, microtime() . " - debugButtonAck.DECISION async took ".(microtime(true)-$t1).PHP_EOL); | ||
|
||
fwrite(STDERR, "create promise"); | ||
$val = null; | ||
$success->then(function (Success $success) use (&$val) { | ||
fwrite(STDERR, "success resolved"); | ||
$val = $success; | ||
}); | ||
fwrite(STDERR, "wait for success"); | ||
$success->wait(true); | ||
fwrite(STDERR, "DONE waiting"); | ||
|
||
return $val; | ||
} | ||
} |
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,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\Trezor\Device\Button; | ||
|
||
use BitWasp\Trezor\Bridge\Session; | ||
use BitWasp\Trezor\Device\Message; | ||
use BitWasp\TrezorProto\ButtonRequest; | ||
use BitWasp\TrezorProto\ButtonRequestType; | ||
|
||
class HumanButtonAck extends ButtonAck | ||
{ | ||
public function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $expectedType) | ||
{ | ||
$theirType = $request->getCode(); | ||
if ($theirType->value() !== $expectedType->value()) { | ||
throw new \RuntimeException("Unexpected button request (expected: {$expectedType->name()}, got {$theirType->name()})"); | ||
} | ||
|
||
echo "sending button ack\n"; | ||
return $session->sendMessage(Message::buttonAck(new \BitWasp\TrezorProto\ButtonAck())); | ||
} | ||
} |
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
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,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\Trezor\Device\Command; | ||
|
||
use BitWasp\Trezor\Bridge\Session; | ||
use BitWasp\Trezor\Device\Button\ButtonAck; | ||
use BitWasp\Trezor\Device\Exception\UnexpectedResultException; | ||
use BitWasp\Trezor\Device\Message; | ||
use BitWasp\TrezorProto; | ||
|
||
class LoadDeviceService extends DeviceService | ||
{ | ||
private $ack; | ||
|
||
public function __construct(ButtonAck $buttonAck) | ||
{ | ||
$this->ack = $buttonAck; | ||
} | ||
|
||
public function call( | ||
Session $session, | ||
TrezorProto\LoadDevice $loadDevice | ||
): TrezorProto\Success { | ||
|
||
fwrite(STDERR, "loadService.sending load device\n"); | ||
$proto = $session->sendMessage(Message::loadDevice($loadDevice)); | ||
fwrite(STDERR, "loadService.response for load device\n"); | ||
|
||
if ($proto instanceof TrezorProto\ButtonRequest) { | ||
fwrite(STDERR, "loadService.send button ack\n"); | ||
$proto = $this->ack->acknowledge($session, $proto, TrezorProto\ButtonRequestType::ButtonRequest_ProtectCall()); | ||
fwrite(STDERR, "loadService.response for ack device\n"); | ||
} | ||
|
||
if (!($proto instanceof TrezorProto\Success)) { | ||
throw new UnexpectedResultException("Unexpected response, expecting Success, got " . get_class($proto)); | ||
} | ||
fwrite(STDERR, "done\n"); | ||
return $proto; | ||
} | ||
} |
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace BitWasp\Trezor\Device; | ||
|
||
use BitWasp\TrezorProto\DebugLinkDecision; | ||
use BitWasp\TrezorProto\DebugLinkGetState; | ||
use BitWasp\TrezorProto\DebugLinkMemoryRead; | ||
use BitWasp\TrezorProto\DebugLinkStop; | ||
use BitWasp\TrezorProto\MessageType; | ||
|
||
class DebugMessage extends MessageBase | ||
{ | ||
public static function getState(DebugLinkGetState $getState): self | ||
{ | ||
return new self( | ||
MessageType::MessageType_DebugLinkGetState(), | ||
$getState | ||
); | ||
} | ||
public static function stop(DebugLinkStop $stop) | ||
{ | ||
return new self( | ||
MessageType::MessageType_DebugLinkStop(), | ||
$stop | ||
); | ||
} | ||
public static function decision(DebugLinkDecision $decision) | ||
{ | ||
return new self( | ||
MessageType::MessageType_DebugLinkDecision(), | ||
$decision | ||
); | ||
} | ||
|
||
public static function memoryRead(DebugLinkMemoryRead $memoryRead): self | ||
{ | ||
return new self( | ||
MessageType::MessageType_DebugLinkMemoryRead(), | ||
$memoryRead | ||
); | ||
} | ||
} |
Oops, something went wrong.