Skip to content

Commit

Permalink
Improve AnySigner output message (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
leoneparise authored Aug 7, 2019
1 parent 9e18c93 commit 174ee09
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
25 changes: 14 additions & 11 deletions src/Any/Signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,31 @@ Proto::SigningOutput Signer::sign() const noexcept {
case TWCoinTypeCosmos: {
Cosmos::Proto::SigningInput message;
parse(transaction, &message, output);
if (!output.has_error()) {
if (output.success()) {
message.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
auto signerOutput = Cosmos::Signer(std::move(message)).build();
output.set_json(signerOutput.json());
output.set_output(signerOutput.json());
}
break;
}
case TWCoinTypeBinance: {
Binance::Proto::SigningInput message;
parse(transaction, &message, output);
if (!output.has_error()) {
if (output.success()) {
message.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
auto signerOutput = Binance::Signer(std::move(message)).build();
output.set_encoded(hex(signerOutput.begin(), signerOutput.end()));
output.set_output(hex(signerOutput.begin(), signerOutput.end()));
}
break;
}
case TWCoinTypeEthereum: {
Ethereum::Proto::SigningInput message;
parse(transaction, &message, output);
if (!output.has_error()) {
if (output.success()) {
message.set_private_key(privateKey.bytes.data(), privateKey.bytes.size());
auto signerOutput = Ethereum::Signer(load(message.chain_id())).sign(message);
auto encoded = signerOutput.encoded();
output.set_encoded(hex(encoded.begin(), encoded.end()));
output.set_output(hex(encoded.begin(), encoded.end()));
}
break;
}
Expand All @@ -79,10 +79,13 @@ void Signer::parse(const std::string& transaction, Message* message,

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

if (!result.ok()) {
auto error = new Proto::SigningOutput_Error();
error->set_code(SignerErrorCodeInvalidJson);
error->set_description(result.error_message());
output.set_allocated_error(error);
if (result.ok()) {
output.set_success(true);
return;
}

auto error = new Proto::SigningOutput_Error();
error->set_code(SignerErrorCodeInvalidJson);
error->set_description(result.error_message());
output.set_allocated_error(error);
}
12 changes: 5 additions & 7 deletions src/proto/Any.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@ message SigningOutput {
string description = 2;
}

oneof output {
// Signing error
Error error = 1;
// Signing output as JSON
string json = 2;
// Signing output encoded
string encoded = 3;
oneof result {
Error error = 1;
bool success = 2;
}

string output = 4;
}
19 changes: 8 additions & 11 deletions tests/Any/SignerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ TEST(Signer, CosmosTransactionSign) {
auto signer = Signer(input);
auto output = signer.sign();

ASSERT_FALSE(output.has_error());
ASSERT_EQ("", output.encoded());
ASSERT_TRUE(output.success());
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\"}}",
output.json());
output.output());
}

TEST(Signer, BinanceTransactionSign) {
Expand All @@ -40,10 +39,9 @@ TEST(Signer, BinanceTransactionSign) {
auto signer = Signer(input);
auto output = signer.sign();

ASSERT_FALSE(output.has_error());
ASSERT_EQ("", output.json());
ASSERT_TRUE(output.success());
ASSERT_EQ("ca01f0625dee0a4a2a2c87fa0a210a1412e654edef9e508b833736a987d069da5a89aedb12090a03424e4210cb8d5212210a1433bbf307b98146f13d20693cf946c2d77a4caf2812090a03424e4210cb8d52126d0a26eb5ae9872102e58176f271a9796b4288908be85094a2ac978e25535fd59a37b58626e3a84d9e1240015b4beb3d6ef366a7a92fd283f66b8f0d8cdb6b152a9189146b27f84f507f047e248517cf691a36ebc2b7f3b7f64e27585ce1c40f1928d119c31af428efcf3e1882671a0754657374696e672002",
output.encoded());
output.output());
}

TEST(Signer, EthereumTransactionSign) {
Expand All @@ -56,10 +54,9 @@ TEST(Signer, EthereumTransactionSign) {
auto signer = Signer(input);
auto output = signer.sign();

ASSERT_FALSE(output.has_error());
ASSERT_EQ("", output.json());
ASSERT_TRUE(output.success());;
ASSERT_EQ("f86a8084d693a400825208947d8bf18c7ce84b3e175b339c4ca93aed1dd166f1870348bca5a160008025a0fe5802b49e04c6b1705088310e133605ed8b549811a18968ad409ea02ad79f21a05bf845646fb1e1b9365f63a7fd5eb5e984094e3ed35c3bed7361aebbcbf41f10",
output.encoded());
output.output());
}

TEST(Signer, NetworkNotSupported) {
Expand All @@ -72,7 +69,7 @@ TEST(Signer, NetworkNotSupported) {
auto signer = Signer(input);
auto output = signer.sign();

ASSERT_TRUE(output.has_error());
ASSERT_FALSE(output.success());
ASSERT_EQ(SignerErrorCodeNotSupported, output.error().code());
ASSERT_EQ("Network not supported", output.error().description());
}
Expand All @@ -87,6 +84,6 @@ TEST(Signer, InvalidJsonFormat) {
auto signer = Signer(input);
auto output = signer.sign();

ASSERT_TRUE(output.has_error());
ASSERT_FALSE(output.success());
ASSERT_EQ(SignerErrorCodeInvalidJson, output.error().code());
}

0 comments on commit 174ee09

Please sign in to comment.