Skip to content

Commit a721f3a

Browse files
author
Thomas Kerin
committed
use not_read branch of trezord-go
1 parent 0770d8a commit a721f3a

9 files changed

Lines changed: 86 additions & 63 deletions

File tree

src/Bridge/Client.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,18 @@ public function release(string $sessionId): bool
148148
return true;
149149
}
150150

151-
public function call(string $sessionId, MessageBase $message): MessageBase
151+
public function post(string $sessionId, MessageBase $message)
152+
{
153+
return $this->client->post($sessionId, $message);
154+
}
155+
156+
public function call(string $sessionId, MessageBase $message)
152157
{
153158
return $this->client->call($sessionId, $message);
154159
}
155160

156-
public function callAsync(string $sessionId, MessageBase $message, array $headers): PromiseInterface
161+
public function callAsync(string $sessionId, MessageBase $message): PromiseInterface
157162
{
158-
return $this->client->callAsync($sessionId, $message, $headers);
163+
return $this->client->callAsync($sessionId, $message);
159164
}
160165
}

src/Bridge/Http/HttpClient.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,25 +102,41 @@ public function release(string $sessionId): ResponseInterface
102102
]);
103103
}
104104

105+
public function post(string $sessionId, MessageBase $message)
106+
{
107+
$responsePromise = $this->postAsync($sessionId, $message);
108+
return $responsePromise->wait(true);
109+
}
110+
105111
public function call(string $sessionId, MessageBase $message): Message
106112
{
107113
$responsePromise = $this->callAsync($sessionId, $message);
108114
return $responsePromise->wait(true);
109115
}
110116

111-
public function callAsync(string $sessionId, MessageBase $message, array $headers = [])
117+
public function postAsync(string $sessionId, MessageBase $message)
118+
{
119+
static $prefixLen;
120+
if (null === $prefixLen) {
121+
$prefixLen = strlen("MessageType_");
122+
}
123+
124+
return $this->client->postAsync("/post/{$sessionId}", [
125+
'body' => $this->callCodec->encode($message->getType(), $message->getProto()),
126+
]);
127+
}
128+
129+
public function callAsync(string $sessionId, MessageBase $message)
112130
{
113131
static $prefixLen;
114132
if (null === $prefixLen) {
115133
$prefixLen = strlen("MessageType_");
116134
}
117135

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

126142
$messageType = MessageType::valueOf($type);

src/Bridge/Session.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,22 +99,20 @@ public function getDevice(): Device
9999
* @throws FailureException
100100
* @throws InactiveSessionException
101101
*/
102-
public function sendMessageAsync(MessageBase $request, array $headers = []): PromiseInterface
102+
public function sendMessageAsync(MessageBase $request): PromiseInterface
103103
{
104104
$this->assertSessionIsActive();
105-
fwrite(STDERR, "(sending {$request->getType()})\n");
106-
return $this->client->callAsync($this->getSessionId(), $request, $headers)
105+
return $this->client->callAsync($this->getSessionId(), $request)
107106
->then(function (Message $message) use ($request) {
108-
fwrite(STDERR, "(for {$request->getType()}) got message back {$message->getType()})\n");
109107
$proto = $message->getProto();
110108
if ($proto instanceof Failure) {
111-
fwrite(STDERR, "(failure {$request->getType()}) got message back {$message->getType()})\n");
112109
FailureException::handleFailure($proto);
113110
}
114111

115112
return $proto;
116113
});
117114
}
115+
118116
/**
119117
* @param MessageBase $message
120118
* @return ProtoMessage
@@ -127,4 +125,15 @@ public function sendMessage(MessageBase $message): ProtoMessage
127125
return $this->sendMessageAsync($message)
128126
->wait(true);
129127
}
128+
129+
/**
130+
* @param MessageBase $message
131+
* @throws FailureException
132+
* @throws InactiveSessionException
133+
*/
134+
public function postMessage(MessageBase $message)
135+
{
136+
$this->assertSessionIsActive();
137+
return $this->client->post($this->getSessionId(), $message);
138+
}
130139
}

src/Device/Button/DebugButtonAck.php

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,53 +10,44 @@
1010
use BitWasp\TrezorProto\ButtonRequest;
1111
use BitWasp\TrezorProto\ButtonRequestType;
1212
use BitWasp\TrezorProto\DebugLinkDecision;
13-
use BitWasp\TrezorProto\DebugLinkGetState;
14-
use BitWasp\TrezorProto\DebugLinkStop;
15-
use BitWasp\TrezorProto\Success;
1613

1714
class DebugButtonAck extends ButtonAck
1815
{
16+
/**
17+
* @var Session
18+
*/
1919
private $debug;
2020

21-
public function __construct(Session $debugSession)
22-
{
21+
/**
22+
* @var bool
23+
*/
24+
private $button;
25+
26+
public function __construct(
27+
Session $debugSession,
28+
bool $button
29+
) {
2330
$this->debug = $debugSession;
31+
$this->button = $button;
2432
}
2533

26-
public function acknowledge(Session $session, ButtonRequest $request, ButtonRequestType $expectedType)
27-
{
34+
public function acknowledge(
35+
Session $session,
36+
ButtonRequest $request,
37+
ButtonRequestType $expectedType
38+
): \Protobuf\Message {
2839
$theirType = $request->getCode();
2940
if ($theirType->value() !== $expectedType->value()) {
3041
throw new \RuntimeException("Unexpected button request (expected: {$expectedType->name()}, got {$theirType->name()})");
3142
}
3243

33-
fwrite(STDERR, microtime() . " - debugButtonAck.sending button ack (async)\n");
34-
$t1 = microtime(true);
3544
$ack = new \BitWasp\TrezorProto\ButtonAck();
3645

3746
$decision = new DebugLinkDecision();
38-
$decision->setYesNo(true);
39-
40-
fwrite(STDERR, microtime() . " - debugButtonAck.sending DECISION (async)\n");
41-
$t1 = microtime(true);
47+
$decision->setYesNo($this->button);
4248

4349
$success = $session->sendMessageAsync(Message::buttonAck($ack));
44-
$debug = $this->debug->sendMessageAsync(DebugMessage::decision($decision), [
45-
'Connection' => 'close',
46-
]);
47-
48-
fwrite(STDERR, microtime() . " - debugButtonAck.DECISION async took ".(microtime(true)-$t1).PHP_EOL);
49-
50-
fwrite(STDERR, "create promise");
51-
$val = null;
52-
$success->then(function (Success $success) use (&$val) {
53-
fwrite(STDERR, "success resolved");
54-
$val = $success;
55-
});
56-
fwrite(STDERR, "wait for success");
57-
$success->wait(true);
58-
fwrite(STDERR, "DONE waiting");
59-
60-
return $val;
50+
$this->debug->postMessage(DebugMessage::decision($decision));
51+
return $success->wait(true);
6152
}
6253
}

src/Device/Button/HumanButtonAck.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public function acknowledge(Session $session, ButtonRequest $request, ButtonRequ
1818
throw new \RuntimeException("Unexpected button request (expected: {$expectedType->name()}, got {$theirType->name()})");
1919
}
2020

21-
echo "sending button ack\n";
2221
return $session->sendMessage(Message::buttonAck(new \BitWasp\TrezorProto\ButtonAck()));
2322
}
2423
}

src/Device/Command/LoadDeviceService.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212

1313
class LoadDeviceService extends DeviceService
1414
{
15+
/**
16+
* @var ButtonAck
17+
*/
1518
private $ack;
1619

1720
public function __construct(ButtonAck $buttonAck)
@@ -23,21 +26,16 @@ public function call(
2326
Session $session,
2427
TrezorProto\LoadDevice $loadDevice
2528
): TrezorProto\Success {
26-
27-
fwrite(STDERR, "loadService.sending load device\n");
2829
$proto = $session->sendMessage(Message::loadDevice($loadDevice));
29-
fwrite(STDERR, "loadService.response for load device\n");
3030

3131
if ($proto instanceof TrezorProto\ButtonRequest) {
32-
fwrite(STDERR, "loadService.send button ack\n");
3332
$proto = $this->ack->acknowledge($session, $proto, TrezorProto\ButtonRequestType::ButtonRequest_ProtectCall());
34-
fwrite(STDERR, "loadService.response for ack device\n");
3533
}
3634

3735
if (!($proto instanceof TrezorProto\Success)) {
3836
throw new UnexpectedResultException("Unexpected response, expecting Success, got " . get_class($proto));
3937
}
40-
fwrite(STDERR, "done\n");
38+
4139
return $proto;
4240
}
4341
}

test/device/Device/Command/LoadDeviceServiceTest.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ class LoadDeviceServiceTest extends CommandTest
2323
$reqFactory = new RequestFactory();
2424
$getAddress = $reqFactory->loadDeviceWithMnemonic($mnemonic, $language);
2525
26-
$loadDeviceService = new LoadDeviceService();
26+
$debugSession = $this->client->acquire($this->devices[1]);
27+
$buttonAck = new DebugButtonAck($debugSession, true);
28+
$loadDeviceService = new LoadDeviceService($buttonAck);
29+
2730
$success = $loadDeviceService->call($this->session, $getAddress);
2831
}*/
2932

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

44-
$initService = new InitializeService();
45-
$features = $initService->call($this->session, new Initialize());
46-
47-
$buttonAck = new DebugButtonAck($debugSession);
46+
$debugSession = $this->client->acquire($this->devices[1]);
47+
$buttonAck = new DebugButtonAck($debugSession, true);
4848
$loadDeviceService = new LoadDeviceService($buttonAck);
49+
$initializeService = new InitializeService();
4950

50-
fwrite(STDERR, "firing load service\n");
51-
$l1 = microtime(true);
51+
$features = $initializeService->call($this->session, new Initialize());
52+
$this->assertFalse($features->getInitialized());
5253
$success = $loadDeviceService->call($this->session, $loadDevice);
5354
$this->assertInstanceOf(Success::class, $success);
54-
55-
$stop = new DebugLinkStop();
56-
$debugSession->sendMessageAsync(DebugMessage::stop($stop));
5755
}
5856
}

test/device/Device/Command/SetupDeviceTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

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

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

26-
$loadDeviceService = new LoadDeviceService();
27+
$debugSession = $this->client->acquire($this->devices[1]);
28+
$debugBtnAck = new DebugButtonAck($debugSession, true);
29+
$loadDeviceService = new LoadDeviceService($debugBtnAck);
2730
$success = $loadDeviceService->call($this->session, $loadDevice);
2831
$this->assertInstanceOf(Success::class, $success);
2932
}

tool/build_bridge.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ if [ ! -d $GOPATH/src/github.com/trezor/trezord-go ]; then
88
mkdir -p $GOPATH/src/github.com/trezor/trezord-go
99
cd $GOPATH/src/github.com/trezor/trezord-go
1010
git init
11-
git remote add bit-wasp https://github.com/bit-wasp/trezord-go
12-
git fetch bit-wasp nousb
13-
git checkout bit-wasp/nousb
11+
#git remote add bit-wasp https://github.com/bit-wasp/trezord-go
12+
#git fetch bit-wasp nousb
13+
#git checkout bit-wasp/nousb
14+
15+
git remote add origin https://github.com/trezor/trezord-go
16+
git fetch origin not_read
17+
git checkout not_read
1418
go install .
1519
fi

0 commit comments

Comments
 (0)