Skip to content

Commit 174ee09

Browse files
authored
Improve AnySigner output message (#574)
1 parent 9e18c93 commit 174ee09

File tree

3 files changed

+27
-29
lines changed

3 files changed

+27
-29
lines changed

src/Any/Signer.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,31 +33,31 @@ Proto::SigningOutput Signer::sign() const noexcept {
3333
case TWCoinTypeCosmos: {
3434
Cosmos::Proto::SigningInput message;
3535
parse(transaction, &message, output);
36-
if (!output.has_error()) {
36+
if (output.success()) {
3737
message.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
3838
auto signerOutput = Cosmos::Signer(std::move(message)).build();
39-
output.set_json(signerOutput.json());
39+
output.set_output(signerOutput.json());
4040
}
4141
break;
4242
}
4343
case TWCoinTypeBinance: {
4444
Binance::Proto::SigningInput message;
4545
parse(transaction, &message, output);
46-
if (!output.has_error()) {
46+
if (output.success()) {
4747
message.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
4848
auto signerOutput = Binance::Signer(std::move(message)).build();
49-
output.set_encoded(hex(signerOutput.begin(), signerOutput.end()));
49+
output.set_output(hex(signerOutput.begin(), signerOutput.end()));
5050
}
5151
break;
5252
}
5353
case TWCoinTypeEthereum: {
5454
Ethereum::Proto::SigningInput message;
5555
parse(transaction, &message, output);
56-
if (!output.has_error()) {
56+
if (output.success()) {
5757
message.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
5858
auto signerOutput = Ethereum::Signer(load(message.chain_id())).sign(message);
5959
auto encoded = signerOutput.encoded();
60-
output.set_encoded(hex(encoded.begin(), encoded.end()));
60+
output.set_output(hex(encoded.begin(), encoded.end()));
6161
}
6262
break;
6363
}
@@ -79,10 +79,13 @@ void Signer::parse(const std::string& transaction, Message* message,
7979

8080
auto result = JsonStringToMessage(transaction, message, options);
8181

82-
if (!result.ok()) {
83-
auto error = new Proto::SigningOutput_Error();
84-
error->set_code(SignerErrorCodeInvalidJson);
85-
error->set_description(result.error_message());
86-
output.set_allocated_error(error);
82+
if (result.ok()) {
83+
output.set_success(true);
84+
return;
8785
}
86+
87+
auto error = new Proto::SigningOutput_Error();
88+
error->set_code(SignerErrorCodeInvalidJson);
89+
error->set_description(result.error_message());
90+
output.set_allocated_error(error);
8891
}

src/proto/Any.proto

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,10 @@ message SigningOutput {
2020
string description = 2;
2121
}
2222

23-
oneof output {
24-
// Signing error
25-
Error error = 1;
26-
// Signing output as JSON
27-
string json = 2;
28-
// Signing output encoded
29-
string encoded = 3;
23+
oneof result {
24+
Error error = 1;
25+
bool success = 2;
3026
}
27+
28+
string output = 4;
3129
}

tests/Any/SignerTests.cpp

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ TEST(Signer, CosmosTransactionSign) {
2424
auto signer = Signer(input);
2525
auto output = signer.sign();
2626

27-
ASSERT_FALSE(output.has_error());
28-
ASSERT_EQ("", output.encoded());
27+
ASSERT_TRUE(output.success());
2928
ASSERT_EQ("{\"mode\":\"block\",\"tx\":{\"fee\":{\"amount\":[{\"amount\":\"5000\",\"denom\":\"uatom\"}],\"gas\":\"200000\"},\"memo\":\"Testing\",\"msg\":[{\"type\":\"cosmos-sdk/MsgSend\",\"value\":{\"amount\":[{\"amount\":\"995000\",\"denom\":\"uatom\"}],\"from_address\":\"cosmos1ufwv9ymhqaal6xz47n0jhzm2wf4empfqvjy575\",\"to_address\":\"cosmos135qla4294zxarqhhgxsx0sw56yssa3z0f78pm0\"}}],\"signatures\":[{\"pub_key\":{\"type\":\"tendermint/PubKeySecp256k1\",\"value\":\"A6EsukEXB53GhohQVeDpxtkeH8KQIayd/Co/ApYRYkTm\"},\"signature\":\"ULEpUqNzoAnYEx2x22F3ANAiPXquAU9+mqLWoAA/ZOUGTMsdb6vryzsW6AKX2Kqj1pGNdrTcQ58Z09JPyjpgEA==\"}],\"type\":\"cosmos-sdk/MsgSend\"}}",
30-
output.json());
29+
output.output());
3130
}
3231

3332
TEST(Signer, BinanceTransactionSign) {
@@ -40,10 +39,9 @@ TEST(Signer, BinanceTransactionSign) {
4039
auto signer = Signer(input);
4140
auto output = signer.sign();
4241

43-
ASSERT_FALSE(output.has_error());
44-
ASSERT_EQ("", output.json());
42+
ASSERT_TRUE(output.success());
4543
ASSERT_EQ("ca01f0625dee0a4a2a2c87fa0a210a1412e654edef9e508b833736a987d069da5a89aedb12090a03424e4210cb8d5212210a1433bbf307b98146f13d20693cf946c2d77a4caf2812090a03424e4210cb8d52126d0a26eb5ae9872102e58176f271a9796b4288908be85094a2ac978e25535fd59a37b58626e3a84d9e1240015b4beb3d6ef366a7a92fd283f66b8f0d8cdb6b152a9189146b27f84f507f047e248517cf691a36ebc2b7f3b7f64e27585ce1c40f1928d119c31af428efcf3e1882671a0754657374696e672002",
46-
output.encoded());
44+
output.output());
4745
}
4846

4947
TEST(Signer, EthereumTransactionSign) {
@@ -56,10 +54,9 @@ TEST(Signer, EthereumTransactionSign) {
5654
auto signer = Signer(input);
5755
auto output = signer.sign();
5856

59-
ASSERT_FALSE(output.has_error());
60-
ASSERT_EQ("", output.json());
57+
ASSERT_TRUE(output.success());;
6158
ASSERT_EQ("f86a8084d693a400825208947d8bf18c7ce84b3e175b339c4ca93aed1dd166f1870348bca5a160008025a0fe5802b49e04c6b1705088310e133605ed8b549811a18968ad409ea02ad79f21a05bf845646fb1e1b9365f63a7fd5eb5e984094e3ed35c3bed7361aebbcbf41f10",
62-
output.encoded());
59+
output.output());
6360
}
6461

6562
TEST(Signer, NetworkNotSupported) {
@@ -72,7 +69,7 @@ TEST(Signer, NetworkNotSupported) {
7269
auto signer = Signer(input);
7370
auto output = signer.sign();
7471

75-
ASSERT_TRUE(output.has_error());
72+
ASSERT_FALSE(output.success());
7673
ASSERT_EQ(SignerErrorCodeNotSupported, output.error().code());
7774
ASSERT_EQ("Network not supported", output.error().description());
7875
}
@@ -87,6 +84,6 @@ TEST(Signer, InvalidJsonFormat) {
8784
auto signer = Signer(input);
8885
auto output = signer.sign();
8986

90-
ASSERT_TRUE(output.has_error());
87+
ASSERT_FALSE(output.success());
9188
ASSERT_EQ(SignerErrorCodeInvalidJson, output.error().code());
9289
}

0 commit comments

Comments
 (0)