Skip to content

Commit

Permalink
use not_read branch of trezord-go
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Kerin committed Apr 9, 2018
1 parent 0770d8a commit fa4116b
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 75 deletions.
11 changes: 8 additions & 3 deletions src/Bridge/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,13 +148,18 @@ public function release(string $sessionId): bool
return true;
}

public function call(string $sessionId, MessageBase $message): MessageBase
public function post(string $sessionId, MessageBase $message)
{
return $this->client->post($sessionId, $message);
}

public function call(string $sessionId, MessageBase $message)
{
return $this->client->call($sessionId, $message);
}

public function callAsync(string $sessionId, MessageBase $message, array $headers): PromiseInterface
public function callAsync(string $sessionId, MessageBase $message): PromiseInterface
{
return $this->client->callAsync($sessionId, $message, $headers);
return $this->client->callAsync($sessionId, $message);
}
}
24 changes: 20 additions & 4 deletions src/Bridge/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,25 +102,41 @@ public function release(string $sessionId): ResponseInterface
]);
}

public function post(string $sessionId, MessageBase $message)
{
$responsePromise = $this->postAsync($sessionId, $message);
return $responsePromise->wait(true);
}

public function call(string $sessionId, MessageBase $message): Message
{
$responsePromise = $this->callAsync($sessionId, $message);
return $responsePromise->wait(true);
}

public function callAsync(string $sessionId, MessageBase $message, array $headers = [])
public function postAsync(string $sessionId, MessageBase $message)
{
static $prefixLen;
if (null === $prefixLen) {
$prefixLen = strlen("MessageType_");
}

return $this->client->postAsync("/post/{$sessionId}", [
'body' => $this->callCodec->encode($message->getType(), $message->getProto()),
]);
}

public function callAsync(string $sessionId, MessageBase $message)
{
static $prefixLen;
if (null === $prefixLen) {
$prefixLen = strlen("MessageType_");
}

return $this->client->postAsync("/call/{$sessionId}", [
'headers' => $headers,
'body' => $this->callCodec->encode($message->getType(), $message->getProto()),
])
->then(function (Response $response) use ($prefixLen, $message) {
echo "got response back for ({$message->getType()})";
->then(function (Response $response) use ($prefixLen): Message {
list ($type, $result) = $this->callCodec->parsePayload($response->getBody());

$messageType = MessageType::valueOf($type);
Expand Down
33 changes: 20 additions & 13 deletions src/Bridge/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use BitWasp\Trezor\Device\MessageBase;
use BitWasp\TrezorProto\Failure;
use GuzzleHttp\Promise\PromiseInterface;
use Protobuf\Message as ProtoMessage;

class Session
{
Expand Down Expand Up @@ -93,38 +92,46 @@ public function getDevice(): Device
{
return $this->device;
}

/**
* @param MessageBase $message
* @return ProtoMessage
* @throws FailureException
* @throws InactiveSessionException
* @param MessageBase $request
* @return PromiseInterface
*/
public function sendMessageAsync(MessageBase $request, array $headers = []): PromiseInterface
public function sendMessageAsync(MessageBase $request): PromiseInterface
{
$this->assertSessionIsActive();
fwrite(STDERR, "(sending {$request->getType()})\n");
return $this->client->callAsync($this->getSessionId(), $request, $headers)
->then(function (Message $message) use ($request) {
fwrite(STDERR, "(for {$request->getType()}) got message back {$message->getType()})\n");
return $this->client->callAsync($this->getSessionId(), $request)
->then(function (Message $message): \Protobuf\Message {
$proto = $message->getProto();
if ($proto instanceof Failure) {
fwrite(STDERR, "(failure {$request->getType()}) got message back {$message->getType()})\n");
FailureException::handleFailure($proto);
}

return $proto;
});
}

/**
* @param MessageBase $message
* @return ProtoMessage
* @return \Protobuf\Message
* @throws FailureException
* @throws InactiveSessionException
*/
public function sendMessage(MessageBase $message): ProtoMessage
public function sendMessage(MessageBase $message): \Protobuf\Message
{
$this->assertSessionIsActive();
return $this->sendMessageAsync($message)
->wait(true);
}

/**
* @param MessageBase $message
* @throws FailureException
* @throws InactiveSessionException
*/
public function postMessage(MessageBase $message)
{
$this->assertSessionIsActive();
return $this->client->post($this->getSessionId(), $message);
}
}
2 changes: 1 addition & 1 deletion src/Device/Button/ButtonAck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@

abstract class ButtonAck
{
abstract function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $allowType);
abstract public function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $allowType);
}
51 changes: 21 additions & 30 deletions src/Device/Button/DebugButtonAck.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,53 +10,44 @@
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
{
/**
* @var Session
*/
private $debug;

public function __construct(Session $debugSession)
{
/**
* @var bool
*/
private $button;

public function __construct(
Session $debugSession,
bool $button
) {
$this->debug = $debugSession;
$this->button = $button;
}

public function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $expectedType)
{
public function acknowledge(
Session $session,
ButtonRequest $request,
ButtonRequestType $expectedType
): \Protobuf\Message {
$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);
$decision->setYesNo($this->button);

$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->debug->postMessage(DebugMessage::decision($decision));
return $success->wait(true);
}
}
1 change: 0 additions & 1 deletion src/Device/Button/HumanButtonAck.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public function acknowledge(Session $session, ButtonRequest $request, ButtonRequ
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()));
}
}
10 changes: 4 additions & 6 deletions src/Device/Command/LoadDeviceService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

class LoadDeviceService extends DeviceService
{
/**
* @var ButtonAck
*/
private $ack;

public function __construct(ButtonAck $buttonAck)
Expand All @@ -23,21 +26,16 @@ 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;
}
}
22 changes: 9 additions & 13 deletions test/device/Device/Command/LoadDeviceServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
use BitWasp\Trezor\Device\Button\DebugButtonAck;
use BitWasp\Trezor\Device\Command\InitializeService;
use BitWasp\Trezor\Device\Command\LoadDeviceService;
use BitWasp\Trezor\Device\DebugMessage;
use BitWasp\Trezor\Device\RequestFactory;
use BitWasp\TrezorProto\DebugLinkStop;
use BitWasp\TrezorProto\Initialize;
use BitWasp\TrezorProto\Success;

Expand All @@ -23,7 +21,10 @@ class LoadDeviceServiceTest extends CommandTest
$reqFactory = new RequestFactory();
$getAddress = $reqFactory->loadDeviceWithMnemonic($mnemonic, $language);
$loadDeviceService = new LoadDeviceService();
$debugSession = $this->client->acquire($this->devices[1]);
$buttonAck = new DebugButtonAck($debugSession, true);
$loadDeviceService = new LoadDeviceService($buttonAck);
$success = $loadDeviceService->call($this->session, $getAddress);
}*/

Expand All @@ -39,20 +40,15 @@ public function testLoadWithHdNode()
$reqFactory = new RequestFactory();
$hdNode = $reqFactory->privateHdNode($depth, $fingerprint, $numChild, $chainCode, $privateKey);
$loadDevice = $reqFactory->loadDeviceWithHdNode($hdNode, $language);
$debugSession = $this->client->acquire($this->devices[1]);

$initService = new InitializeService();
$features = $initService->call($this->session, new Initialize());

$buttonAck = new DebugButtonAck($debugSession);
$debugSession = $this->client->acquire($this->devices[1]);
$buttonAck = new DebugButtonAck($debugSession, true);
$loadDeviceService = new LoadDeviceService($buttonAck);
$initializeService = new InitializeService();

fwrite(STDERR, "firing load service\n");
$l1 = microtime(true);
$features = $initializeService->call($this->session, new Initialize());
$this->assertFalse($features->getInitialized());
$success = $loadDeviceService->call($this->session, $loadDevice);
$this->assertInstanceOf(Success::class, $success);

$stop = new DebugLinkStop();
$debugSession->sendMessageAsync(DebugMessage::stop($stop));
}
}
5 changes: 4 additions & 1 deletion test/device/Device/Command/SetupDeviceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace BitWasp\Test\Trezor\Device\Device\Command;

use BitWasp\Trezor\Device\Button\DebugButtonAck;
use BitWasp\Trezor\Device\Command\LoadDeviceService;
use BitWasp\Trezor\Device\RequestFactory;
use BitWasp\TrezorProto\Success;
Expand All @@ -23,7 +24,9 @@ public function testSetup()
$hdNode = $reqFactory->privateHdNode($depth, $fingerprint, $numChild, $chainCode, $privateKey);
$loadDevice = $reqFactory->loadDeviceWithHdNode($hdNode, $language);

$loadDeviceService = new LoadDeviceService();
$debugSession = $this->client->acquire($this->devices[1]);
$debugBtnAck = new DebugButtonAck($debugSession, true);
$loadDeviceService = new LoadDeviceService($debugBtnAck);
$success = $loadDeviceService->call($this->session, $loadDevice);
$this->assertInstanceOf(Success::class, $success);
}
Expand Down
10 changes: 7 additions & 3 deletions tool/build_bridge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@ if [ ! -d $GOPATH/src/github.com/trezor/trezord-go ]; then
mkdir -p $GOPATH/src/github.com/trezor/trezord-go
cd $GOPATH/src/github.com/trezor/trezord-go
git init
git remote add bit-wasp https://github.com/bit-wasp/trezord-go
git fetch bit-wasp nousb
git checkout bit-wasp/nousb
#git remote add bit-wasp https://github.com/bit-wasp/trezord-go
#git fetch bit-wasp nousb
#git checkout bit-wasp/nousb

git remote add origin https://github.com/trezor/trezord-go
git fetch origin not_read
git checkout not_read
go install .
fi

0 comments on commit fa4116b

Please sign in to comment.