Skip to content

Commit

Permalink
Extract Blob::str to static toBinaryString
Browse files Browse the repository at this point in the history
Signed-off-by: Kitsu <[email protected]>
  • Loading branch information
l4l authored and kamilsa committed Dec 27, 2017
1 parent 4009744 commit 06fe3b4
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 30 deletions.
4 changes: 2 additions & 2 deletions shared_model/backend/protobuf/queries/proto_query.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ namespace shared_model {
}

auto sig = proto_->mutable_signature();
sig->set_pubkey(signature->publicKey().str());
sig->set_signature(signature->signedData().str());
sig->set_pubkey(crypto::toBinaryString(signature->publicKey()));
sig->set_signature(crypto::toBinaryString(signature->signedData()));
return true;
}

Expand Down
4 changes: 2 additions & 2 deletions shared_model/backend/protobuf/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ namespace shared_model {
return false;
}
auto sig = proto_->add_signature();
sig->set_pubkey(signature->publicKey().str());
sig->set_signature(signature->signedData().str());
sig->set_pubkey(crypto::toBinaryString(signature->publicKey()));
sig->set_signature(crypto::toBinaryString(signature->signedData()));
signatures_.invalidate();
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions shared_model/builders/protobuf/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ namespace shared_model {
return addCommand([&](auto proto_command) {
auto command = proto_command->mutable_add_peer();
command->set_address(address);
command->set_peer_key(peer_key.str());
command->set_peer_key(crypto::toBinaryString(peer_key));
});
}

Expand All @@ -133,7 +133,7 @@ namespace shared_model {
return addCommand([&](auto proto_command) {
auto command = proto_command->mutable_add_signatory();
command->set_account_id(account_id);
command->set_public_key(public_key.str());
command->set_public_key(crypto::toBinaryString(public_key));
});
}

Expand All @@ -143,7 +143,7 @@ namespace shared_model {
return addCommand([&](auto proto_command) {
auto command = proto_command->mutable_remove_sign();
command->set_account_id(account_id);
command->set_public_key(public_key.str());
command->set_public_key(crypto::toBinaryString(public_key));
});
}

Expand Down Expand Up @@ -175,7 +175,7 @@ namespace shared_model {
auto command = proto_command->mutable_create_account();
command->set_account_name(account_name);
command->set_domain_id(domain_id);
command->set_main_pubkey(main_pubkey.str());
command->set_main_pubkey(crypto::toBinaryString(main_pubkey));
});
}

Expand Down
4 changes: 2 additions & 2 deletions shared_model/builders/protobuf/unsigned_proto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ namespace shared_model {
auto signedBlob = shared_model::crypto::CryptoSigner<>::sign(
shared_model::crypto::Blob(unsigned_.payload()), keypair);
iroha::protocol::Signature protosig;
protosig.set_pubkey(keypair.publicKey().str());
protosig.set_signature(signedBlob.str());
protosig.set_pubkey(crypto::toBinaryString(keypair.publicKey()));
protosig.set_signature(crypto::toBinaryString(signedBlob));
auto *s1 = new Signature(protosig);
unsigned_.addSignature(detail::PolymorphicWrapper<Signature>(
s1)); // TODO: 05.12.2017 luckychess think about false case
Expand Down
15 changes: 7 additions & 8 deletions shared_model/cryptography/blob.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
namespace shared_model {
namespace crypto {

class Blob;
static inline std::string toBinaryString(const Blob &b);
/**
* Blob class present user-friendly blob for working with low-level
* binary stuff. Its length is not fixed in compile time.
Expand Down Expand Up @@ -63,13 +65,6 @@ namespace shared_model {
*/
virtual const Bytes &blob() const { return blob_; }

/**
* @return provides raw representation of blob as string
*/
virtual const std::string str() const {
return std::string(blob_.begin(), blob_.end());
}

/**
* @return provides human-readable representation of blob without leading
* 0x
Expand Down Expand Up @@ -104,14 +99,18 @@ namespace shared_model {

template <typename BlobType>
DEPRECATED BlobType makeOldModel() const {
return BlobType::from_string(str());
return BlobType::from_string(toBinaryString(*this));
}

private:
// TODO: 17/11/2017 luckychess use improved Lazy with references support
Bytes blob_;
std::string hex_;
};

static inline std::string toBinaryString(const Blob &b) {
return std::string(b.blob().begin(), b.blob().end());
}
} // namespace crypto
} // namespace shared_model
#endif // IROHA_SHARED_MODEL_BLOB_HPP
2 changes: 1 addition & 1 deletion shared_model/cryptography/ed25519_sha3_impl/signer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace shared_model {
Signed Signer::sign(const Blob &blob, const Keypair &keypair) {
return Signed(
iroha::sign(
blob.str(),
crypto::toBinaryString(blob),
keypair.publicKey().makeOldModel<PublicKey::OldPublicKeyType>(),
keypair.privateKey()
.makeOldModel<PrivateKey::OldPrivateKeyType>())
Expand Down
2 changes: 1 addition & 1 deletion shared_model/cryptography/ed25519_sha3_impl/verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace shared_model {
const Blob &orig,
const PublicKey &publicKey) {
return iroha::verify(
orig.str(),
crypto::toBinaryString(orig),
publicKey.makeOldModel<PublicKey::OldPublicKeyType>(),
signedData.makeOldModel<Signed::OldSignatureType>());
}
Expand Down
4 changes: 3 additions & 1 deletion shared_model/cryptography/public_key.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ namespace shared_model {
.finalize();
}

PublicKey *copy() const override { return new PublicKey(str()); }
PublicKey *copy() const override {
return new PublicKey(crypto::toBinaryString(*this));
}
};
} // namespace crypto
} // namespace shared_model
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ namespace shared_model {
OldModelType *makeOldModel() const override {
auto response = boost::apply_visitor(
detail::OldModelCreatorVisitor<OldModelType *>(), get());
response->tx_hash = transactionHash().str();
response->tx_hash = crypto::toBinaryString(transactionHash());
return response;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ TEST(ProtoQueryBuilder, Builder) {
keypair);

auto sig = proto_query.mutable_signature();
sig->set_pubkey(keypair.publicKey().str());
sig->set_signature(signedProto.str());
sig->set_pubkey(shared_model::crypto::toBinaryString(keypair.publicKey()));
sig->set_signature(shared_model::crypto::toBinaryString(signedProto));

auto query = shared_model::proto::QueryBuilder()
.createdTime(created_time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ TEST(ProtoTransaction, Builder) {
keypair);

auto sig = proto_tx.add_signature();
sig->set_pubkey(keypair.publicKey().str());
sig->set_signature(signedProto.str());
sig->set_pubkey(shared_model::crypto::toBinaryString(keypair.publicKey()));
sig->set_signature(shared_model::crypto::toBinaryString(signedProto));

auto tx = shared_model::proto::TransactionBuilder()
.txCounter(tx_counter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

using namespace iroha;
using namespace shared_model::proto;
using shared_model::crypto::toBinaryString;

/**
* @given some protobuf object
Expand All @@ -33,6 +34,6 @@ TEST(UtilTest, StringFromMakeBlob) {
base.set_created_time(100);
auto blob = make_blob(base);

ASSERT_TRUE(deserialized.ParseFromString(blob.str()));
ASSERT_TRUE(deserialized.ParseFromString(toBinaryString(blob)));
ASSERT_EQ(deserialized.created_time(), base.created_time());
}
6 changes: 3 additions & 3 deletions test/module/shared_model/cryptography/blob_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ TEST(BlobTest, HexConversionTest) {
*/
TEST(BlobTest, BlobIsString) {
Blob blob(data);
auto str = blob.str();
auto bin_str = toBinaryString(blob);
auto binary = blob.blob();
size_t sz = binary.size();

ASSERT_EQ(str.size(), sz);
ASSERT_EQ(bin_str.size(), sz);
for (size_t i = 0; i < sz; ++i) {
ASSERT_EQ(binary[i], str[i]);
ASSERT_EQ(binary[i], bin_str[i]);
}
}

0 comments on commit 06fe3b4

Please sign in to comment.