diff --git a/src/Bridge/Client.php b/src/Bridge/Client.php index 561d4ac..9f64631 100644 --- a/src/Bridge/Client.php +++ b/src/Bridge/Client.php @@ -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); } } diff --git a/src/Bridge/Http/HttpClient.php b/src/Bridge/Http/HttpClient.php index 243ee96..3c47e32 100644 --- a/src/Bridge/Http/HttpClient.php +++ b/src/Bridge/Http/HttpClient.php @@ -102,13 +102,31 @@ 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) { @@ -116,11 +134,9 @@ public function callAsync(string $sessionId, MessageBase $message, array $header } 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); diff --git a/src/Bridge/Session.php b/src/Bridge/Session.php index 6863c73..7caa44a 100644 --- a/src/Bridge/Session.php +++ b/src/Bridge/Session.php @@ -11,7 +11,6 @@ use BitWasp\Trezor\Device\MessageBase; use BitWasp\TrezorProto\Failure; use GuzzleHttp\Promise\PromiseInterface; -use Protobuf\Message as ProtoMessage; class Session { @@ -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); + } } diff --git a/src/Device/Button/ButtonAck.php b/src/Device/Button/ButtonAck.php index 80efe42..e5756c4 100644 --- a/src/Device/Button/ButtonAck.php +++ b/src/Device/Button/ButtonAck.php @@ -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); } diff --git a/src/Device/Button/DebugButtonAck.php b/src/Device/Button/DebugButtonAck.php index d167dc0..84b8b85 100644 --- a/src/Device/Button/DebugButtonAck.php +++ b/src/Device/Button/DebugButtonAck.php @@ -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); } } diff --git a/src/Device/Button/HumanButtonAck.php b/src/Device/Button/HumanButtonAck.php index 3a7845d..e0a7176 100644 --- a/src/Device/Button/HumanButtonAck.php +++ b/src/Device/Button/HumanButtonAck.php @@ -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())); } } diff --git a/src/Device/Command/LoadDeviceService.php b/src/Device/Command/LoadDeviceService.php index 80a665a..5640ee9 100644 --- a/src/Device/Command/LoadDeviceService.php +++ b/src/Device/Command/LoadDeviceService.php @@ -12,6 +12,9 @@ class LoadDeviceService extends DeviceService { + /** + * @var ButtonAck + */ private $ack; public function __construct(ButtonAck $buttonAck) @@ -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; } } diff --git a/test/device/Device/Command/LoadDeviceServiceTest.php b/test/device/Device/Command/LoadDeviceServiceTest.php index f8aa579..26b4724 100644 --- a/test/device/Device/Command/LoadDeviceServiceTest.php +++ b/test/device/Device/Command/LoadDeviceServiceTest.php @@ -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; @@ -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); }*/ @@ -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)); } } diff --git a/test/device/Device/Command/SetupDeviceTest.php b/test/device/Device/Command/SetupDeviceTest.php index 8fd82f3..c9a4c73 100644 --- a/test/device/Device/Command/SetupDeviceTest.php +++ b/test/device/Device/Command/SetupDeviceTest.php @@ -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; @@ -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); } diff --git a/tool/build_bridge.sh b/tool/build_bridge.sh index 463278c..0023db8 100755 --- a/tool/build_bridge.sh +++ b/tool/build_bridge.sh @@ -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