Skip to content

Commit b76b2a9

Browse files
hewigovensvikmeup
authored andcommitted
Refine Cosmos signing output (#568)
* return broadcast mode async, return signature instead of not used encoded data * Fix typo * set default to block mode
1 parent a3fa33d commit b76b2a9

File tree

13 files changed

+29
-50
lines changed

13 files changed

+29
-50
lines changed

android/app/src/androidTest/java/com/trustwallet/core/app/blockchains/cosmos/TestCosmosTransactions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class TestCosmosTransactions {
5757

5858
val jsonPayload = CosmosSigner.sign(signingInput).json
5959

60-
val expectedJsonPayload = """{"tx":{"fee":{"amount":[{"amount":"200","denom":"muon"}],"gas":"200000"},"memo":"","msg":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[{"amount":"1","denom":"muon"}],"from_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","to_address":"cosmos1zt50azupanqlfam5afhv3hexwyutnukeh4c573"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg=="}],"type":"cosmos-sdk/MsgSend"}}"""
60+
val expectedJsonPayload = """{"mode":"block","tx":{"fee":{"amount":[{"amount":"200","denom":"muon"}],"gas":"200000"},"memo":"","msg":[{"type":"cosmos-sdk/MsgSend","value":{"amount":[{"amount":"1","denom":"muon"}],"from_address":"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02","to_address":"cosmos1zt50azupanqlfam5afhv3hexwyutnukeh4c573"}}],"signatures":[{"pub_key":{"type":"tendermint/PubKeySecp256k1","value":"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F"},"signature":"/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg=="}],"type":"cosmos-sdk/MsgSend"}}"""
6161
assertEquals(expectedJsonPayload, jsonPayload)
6262

6363
}

js/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@ HDWallet.create(128);
4545

4646
- For general [`TrustWallet/wallet-core`](https://github.com/TrustWallet/wallet-core) documentation, please refer to [GitBooks](https://developer.trustwallet.com/wallet-core).
4747

48-
- For documentation on specific modules that are avaiable, please refer to [`TrustWallet/wallet-core`](https://github.com/TrustWallet/wallet-core) [exposed C headers](https://github.com/TrustWallet/wallet-core/tree/master/include/TrustWalletCore).
48+
- For documentation on specific modules that are available, please refer to [`TrustWallet/wallet-core`](https://github.com/TrustWallet/wallet-core) [exposed C headers](https://github.com/TrustWallet/wallet-core/tree/master/include/TrustWalletCore).

js/tests/blockchain/cosmos/CosmosTransactions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('CosmosTransactions', () => {
4747

4848
const jsonPayload = CosmosSigner.sign(signingInput).json;
4949

50-
const expectedJsonPayload = {'tx':{'fee':{'amount':[{'amount':'200','denom':'muon'}],'gas':'200000'},'memo':'','msg':[{'type':'cosmos-sdk/MsgSend','value':{'amount':[{'amount':'1','denom':'muon'}],'from_address':'cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02','to_address':'cosmos1zt50azupanqlfam5afhv3hexwyutnukeh4c573'}}],'signatures':[{'pub_key':{'type':'tendermint/PubKeySecp256k1','value':'AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F'},'signature':'/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg=='}],'type':'cosmos-sdk/MsgSend'}};
50+
const expectedJsonPayload = {'mode':'block','tx':{'fee':{'amount':[{'amount':'200','denom':'muon'}],'gas':'200000'},'memo':'','msg':[{'type':'cosmos-sdk/MsgSend','value':{'amount':[{'amount':'1','denom':'muon'}],'from_address':'cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02','to_address':'cosmos1zt50azupanqlfam5afhv3hexwyutnukeh4c573'}}],'signatures':[{'pub_key':{'type':'tendermint/PubKeySecp256k1','value':'AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F'},'signature':'/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg=='}],'type':'cosmos-sdk/MsgSend'}};
5151
expect(jsonPayload).to.equal(JSON.stringify(expectedJsonPayload));
5252
});
5353

src/Cosmos/Serialization.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ const std::string AMINO_PREFIX_WITHDRAW_STAKE_MESSAGE = "cosmos-sdk/MsgWithdrawD
2323
const std::string AMINO_PREFIX_TRANSACTION = "auth/StdTx";
2424
const std::string AMINO_PREFIX_PUBLIC_KEY = "tendermint/PubKeySecp256k1";
2525

26-
json higherWrapperJSON(json& jsonObj) {
26+
json broadcastJSON(json& jsonObj) {
2727
json jsonMsgWrapper;
2828

2929
jsonMsgWrapper["tx"] = jsonObj;
30+
jsonMsgWrapper["mode"] = "block";
3031

3132
return jsonMsgWrapper;
3233
}
@@ -176,5 +177,5 @@ json TW::Cosmos::transactionJSON(const Transaction& transaction, std::string typ
176177
jsonTx["msg"] = json::array({messageJSON(transaction)});
177178
jsonTx["signatures"] = json::array({signatureJSON(transaction.signature())});
178179

179-
return higherWrapperJSON(jsonTx);
180+
return broadcastJSON(jsonTx);
180181
}

src/Cosmos/Signer.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include "../Hash.h"
1111
#include "../HexCoding.h"
12-
#include "../Base64.h"
1312
#include "../PrivateKey.h"
1413
#include "../Data.h"
1514

@@ -62,11 +61,6 @@ std::vector<uint8_t> Signer::sign() const {
6261
return std::vector<uint8_t>(signature.begin(), signature.end() - 1);
6362
}
6463

65-
std::string Signer::signInBase64() const {
66-
auto signature = sign();
67-
return Base64::encode(Data(signature.begin(), signature.end()));
68-
}
69-
7064
std::string Signer::signaturePreimage() const {
7165
return signaturePreimageJSON(input).dump();
7266
}
@@ -107,10 +101,9 @@ Proto::SigningOutput Signer::build() const {
107101

108102
auto signature = sign();
109103
auto txJson = buildTransactionJSON(signature);
110-
auto txEncoded = json::to_cbor(txJson);
111104

112105
output.set_json(txJson.dump());
113-
output.set_encoded(txEncoded.data(), txEncoded.size());
106+
output.set_signature(signature.data(), signature.size());
114107

115108
return output;
116109
}

src/Cosmos/Signer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class Signer {
2626
///
2727
/// \returns the transaction signature or an empty vector if there is an error.
2828
Data sign() const;
29-
std::string signInBase64() const;
3029

3130
/// Builds the signed transaction.
3231
///

src/proto/Cosmos.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ message SigningInput {
8383

8484
// Transaction signing output.
8585
message SigningOutput {
86-
// Signed and encoded transaction bytes.
87-
bytes encoded = 1;
86+
// Signature
87+
bytes signature = 1;
8888
// Signed transaction in JSON.
8989
string json = 2;
9090
}

swift/Podfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ SPEC CHECKSUMS:
1717

1818
PODFILE CHECKSUM: cbcc5bab5b64fa9f7863e62cc106d25d3fc1dfc5
1919

20-
COCOAPODS: 1.7.3
20+
COCOAPODS: 1.7.5

swift/Tests/Blockchains/CosmosTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ class CosmosSignerTests: XCTestCase {
4646
}
4747

4848
let output = CosmosSigner.sign(input: signingInput)
49-
XCTAssertEqual("a1627478a563666565a266616d6f756e7481a266616d6f756e74633230306564656e6f6d646d756f6e6367617366323030303030646d656d6f60636d736781a2647479706572636f736d6f732d73646b2f4d736753656e646576616c7565a366616d6f756e7481a266616d6f756e7461316564656e6f6d646d756f6e6c66726f6d5f61646472657373782d636f736d6f733168736b366a727979716a6668703564686335357463396a74636b7967783065706836646430326a746f5f61646472657373782d636f736d6f73317a743530617a7570616e716c66616d356166687633686578777975746e756b656834633537336a7369676e61747572657381a2677075625f6b6579a26474797065781a74656e6465726d696e742f5075624b6579536563703235366b316576616c7565782c416c636f6273507a66544e56653775714141736e6445724a416a71706c6e79756461474230662b522b703346697369676e617475726578582f4437346d644947794942332f73517649626f4c54665339503945562f6659477267485a45322f764e6a395836654d36653537473361746c6a4e422b5041426e52773370546b353175586d6843466f70384f2f5a4a673d3d647479706572636f736d6f732d73646b2f4d736753656e64", output.encoded.hexString)
5049

5150
let expectedJSON: String =
5251
"""
5352
{
53+
"mode": "block",
5454
"tx": {
5555
"fee": {
5656
"amount": [
@@ -129,10 +129,10 @@ class CosmosSignerTests: XCTestCase {
129129
}
130130

131131
let output = CosmosSigner.sign(input: signingInput)
132-
XCTAssertEqual("a1627478a563666565a266616d6f756e7481a266616d6f756e7464313031386564656e6f6d646d756f6e6367617366313031373231646d656d6f60636d736781a2647479706576636f736d6f732d73646b2f4d736744656c65676174656576616c7565a366616d6f756e74a266616d6f756e746231306564656e6f6d646d756f6e7164656c656761746f725f61646472657373782d636f736d6f733168736b366a727979716a6668703564686335357463396a74636b7967783065706836646430327176616c696461746f725f616464726573737834636f736d6f7376616c6f706572317a6b757072383368727a6b6e33757035656c6b747a63713374756674386e78736d77647167706a7369676e61747572657381a2677075625f6b6579a26474797065781a74656e6465726d696e742f5075624b6579536563703235366b316576616c7565782c416c636f6273507a66544e56653775714141736e6445724a416a71706c6e79756461474230662b522b703346697369676e61747572657858774976666243734c52436a7a6558586f58544b66484c4758526241416d5570304f313334485666566336706664564e4a76767a49534d485255486759636a735369466c4c79523332686569612f794c674d44744959513d3d647479706572636f736d6f732d73646b2f4d736753656e64", output.encoded.hexString)
133132

134133
let expectedJSON = """
135134
{
135+
"mode": "block",
136136
"tx": {
137137
"fee": {
138138
"amount": [

swift/Tests/Blockchains/TerraTests.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,11 @@ class TerraTests: XCTestCase {
4848
}
4949

5050
let output = CosmosSigner.sign(input: signingInput)
51-
// swiftlint:disable:next line_length
52-
XCTAssertEqual(output.encoded.hexString, "a1627478a563666565a266616d6f756e7481a266616d6f756e746533303030306564656e6f6d65756c756e616367617366323030303030646d656d6f60636d736781a264747970656b7061792f4d736753656e646576616c7565a366616d6f756e7481a266616d6f756e74663130303030306564656e6f6d65756c756e616c66726f6d5f61646472657373782c7465727261316a663961616a396d79727a736e6d70647237747765636e6166747a6d6b75326d6873326866656a746f5f61646472657373782c746572726131737a7667647361736e6666756e77616b703977303430756e78647265736b3761333463666b616a7369676e61747572657381a2677075625f6b6579a26474797065781a74656e6465726d696e742f5075624b6579536563703235366b316576616c7565782c4131337868565a6c4964616e67434d5a376762686f6f3658743363742b312f64453870764258565269576a6b697369676e61747572657858754a59732b6f623147503235574f782b57434248304c56665050347273563277456b4c422f565462675670425a4f69574761662b514a432f4a2f52386245364c706356493356376343656a58344274794c6e78446a413d3d64747970656b7061792f4d736753656e64")
5351

5452
let expectedJSON: String =
5553
"""
5654
{
55+
"mode": "block",
5756
"tx": {
5857
"fee": {
5958
"amount": [{

tests/Any/SignerTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ TEST(Signer, CosmosTransactionSign) {
2626

2727
ASSERT_FALSE(output.has_error());
2828
ASSERT_EQ("", output.encoded());
29-
ASSERT_EQ("{\"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\"}}",
29+
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\"}}",
3030
output.json());
3131
}
3232

tests/Cosmos/SignerTests.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Coin.h"
88
#include "HDWallet.h"
99
#include "HexCoding.h"
10+
#include "Base64.h"
1011
#include "proto/Cosmos.pb.h"
1112
#include "Cosmos/Address.h"
1213
#include "Cosmos/Signer.h"
@@ -45,15 +46,15 @@ TEST(CosmosSigner, SignTx) {
4546

4647
auto signer = Cosmos::Signer(std::move(input));
4748
auto signature = signer.sign();
48-
auto signatureInBase64 = signer.signInBase64();
49+
auto signatureInBase64 = Base64::encode(signature);
4950

5051
ASSERT_EQ("/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg==", signatureInBase64);
5152

5253
auto output = signer.build();
5354

54-
ASSERT_EQ("{\"tx\":{\"fee\":{\"amount\":[{\"amount\":\"200\",\"denom\":\"muon\"}],\"gas\":\"200000\"},\"memo\":\"\",\"msg\":[{\"type\":\"cosmos-sdk/MsgSend\",\"value\":{\"amount\":[{\"amount\":\"1\",\"denom\":\"muon\"}],\"from_address\":\"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02\",\"to_address\":\"cosmos1zt50azupanqlfam5afhv3hexwyutnukeh4c573\"}}],\"signatures\":[{\"pub_key\":{\"type\":\"tendermint/PubKeySecp256k1\",\"value\":\"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F\"},\"signature\":\"/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg==\"}],\"type\":\"cosmos-sdk/MsgSend\"}}", output.json());
55+
ASSERT_EQ("{\"mode\":\"block\",\"tx\":{\"fee\":{\"amount\":[{\"amount\":\"200\",\"denom\":\"muon\"}],\"gas\":\"200000\"},\"memo\":\"\",\"msg\":[{\"type\":\"cosmos-sdk/MsgSend\",\"value\":{\"amount\":[{\"amount\":\"1\",\"denom\":\"muon\"}],\"from_address\":\"cosmos1hsk6jryyqjfhp5dhc55tc9jtckygx0eph6dd02\",\"to_address\":\"cosmos1zt50azupanqlfam5afhv3hexwyutnukeh4c573\"}}],\"signatures\":[{\"pub_key\":{\"type\":\"tendermint/PubKeySecp256k1\",\"value\":\"AlcobsPzfTNVe7uqAAsndErJAjqplnyudaGB0f+R+p3F\"},\"signature\":\"/D74mdIGyIB3/sQvIboLTfS9P9EV/fYGrgHZE2/vNj9X6eM6e57G3atljNB+PABnRw3pTk51uXmhCFop8O/ZJg==\"}],\"type\":\"cosmos-sdk/MsgSend\"}}", output.json());
5556

56-
ASSERT_EQ("a1627478a563666565a266616d6f756e7481a266616d6f756e74633230306564656e6f6d646d756f6e6367617366323030303030646d656d6f60636d736781a2647479706572636f736d6f732d73646b2f4d736753656e646576616c7565a366616d6f756e7481a266616d6f756e7461316564656e6f6d646d756f6e6c66726f6d5f61646472657373782d636f736d6f733168736b366a727979716a6668703564686335357463396a74636b7967783065706836646430326a746f5f61646472657373782d636f736d6f73317a743530617a7570616e716c66616d356166687633686578777975746e756b656834633537336a7369676e61747572657381a2677075625f6b6579a26474797065781a74656e6465726d696e742f5075624b6579536563703235366b316576616c7565782c416c636f6273507a66544e56653775714141736e6445724a416a71706c6e79756461474230662b522b703346697369676e617475726578582f4437346d644947794942332f73517649626f4c54665339503945562f6659477267485a45322f764e6a395836654d36653537473361746c6a4e422b5041426e52773370546b353175586d6843466f70384f2f5a4a673d3d647479706572636f736d6f732d73646b2f4d736753656e64", hex(output.encoded()));
57+
ASSERT_EQ(hex(output.signature()), "fc3ef899d206c88077fec42f21ba0b4df4bd3fd115fdf606ae01d9136fef363f57e9e33a7b9ec6ddab658cd07e3c0067470de94e4e75b979a1085a29f0efd926");
5758

5859

5960
/*

0 commit comments

Comments
 (0)