11package net .cimadai .iroha
22
3- import java .security .{KeyPair , MessageDigest , PrivateKey , PublicKey }
4- import java .util .stream .{Collectors , StreamSupport }
3+ import java .security .{KeyPair , PrivateKey , PublicKey }
54
6- import iroha .protocol .Block .BlockVersion .BlockV1
75import iroha .protocol .Transaction .Payload
86import iroha .protocol .Transaction .Payload .BatchMeta .BatchType
97import iroha .protocol .{Block , Block_v1 , Query , Signature , Transaction , TxList , TxStatusRequest }
@@ -12,6 +10,7 @@ import javax.xml.bind.DatatypeConverter
1210import javax .xml .bind .DatatypeConverter .parseHexBinary
1311import jp .co .soramitsu .crypto .ed25519 .Ed25519Sha3
1412import jp .co .soramitsu .crypto .ed25519 .Ed25519Sha3 .{privateKeyFromBytes , publicKeyFromBytes }
13+ import org .spongycastle .jcajce .provider .digest .SHA3
1514
1615/**
1716 * Scala version of https://github.com/hyperledger/iroha-java/blob/master/client/src/main/java/jp/co/soramitsu/iroha/java/Utils.java
@@ -56,12 +55,12 @@ object Utils {
5655 /**
5756 * Calculate SHA3-256 hash of {@link iroha.protocol.Transaction.Payload.ReducedPayload}
5857 *
59- * @param reducedPayload Protobuf of ReducedPayload
58+ * @param rp Protobuf of ReducedPayload
6059 * @return 32 bytes hash
6160 */
62- def reducedHash (reducedPayload : ReducedPayload ): Array [Byte ] = {
63- MessageDigest .getInstance( " SHA-256 " )
64- .digest(reducedPayload .toByteArray)
61+ def reducedHash (rp : ReducedPayload ): Array [Byte ] = {
62+ val digest = new SHA3 . Digest256 ( )
63+ digest .digest(rp .toByteArray)
6564 }
6665
6766 /**
@@ -71,8 +70,8 @@ object Utils {
7170 * @return 32 bytes hash
7271 */
7372 def hash (p : Payload ): Array [Byte ] = {
74- MessageDigest .getInstance( " SHA-256 " )
75- .digest(p.toByteArray)
73+ val digest = new SHA3 . Digest256 ( )
74+ digest .digest(p.toByteArray)
7675 }
7776
7877 /**
@@ -82,8 +81,8 @@ object Utils {
8281 * @return 32 bytes hash
8382 */
8483 def hash (tx : Transaction ): Array [Byte ] = {
85- MessageDigest .getInstance( " SHA-256 " )
86- .digest(tx.getPayload.toByteArray)
84+ val digest = new SHA3 . Digest256 ( )
85+ digest .digest(tx.getPayload.toByteArray)
8786 }
8887
8988 /**
@@ -93,8 +92,8 @@ object Utils {
9392 * @return 32 bytes hash
9493 */
9594 def hash (b : Block_v1 ): Array [Byte ] = {
96- MessageDigest .getInstance( " SHA-256 " )
97- .digest(b.getPayload.toByteArray)
95+ val digest = new SHA3 . Digest256 ( )
96+ digest .digest(b.getPayload.toByteArray)
9897 }
9998
10099 /**
@@ -103,11 +102,11 @@ object Utils {
103102 * @param b Protobuf Block
104103 * @return 32 bytes hash
105104 */
106- def hash (b : Block ): Array [Byte ] = b.blockVersion.value match {
107- case Block .BlockVersion .BlockV1 =>
105+ def hash (b : Block ): Array [Byte ] = b.blockVersion match {
106+ case Block .BlockVersion .BlockV1 (_) =>
108107 hash(b.getBlockV1)
109108 case _ =>
110- throw new IllegalArgumentException (String .format(" Block has undefined version: %s" , b.blockVersion.value ))
109+ throw new IllegalArgumentException (String .format(" Block has undefined version: %s" , b.blockVersion.toString ))
111110 }
112111
113112 /**
@@ -117,13 +116,18 @@ object Utils {
117116 * @return 32 bytes hash
118117 */
119118 def hash (q : Query ): Array [Byte ] = {
120- MessageDigest .getInstance( " SHA-256 " )
121- .digest(q.getPayload.toByteArray)
119+ val digest = new SHA3 . Digest256 ( )
120+ digest .digest(q.getPayload.toByteArray)
122121 }
123122
124123 def sign (t : Payload , kp : KeyPair ): Signature = {
125124 val rawSignature = new Ed25519Sha3 ().rawSign(hash(t), kp)
126- Signature (Utils .toHex(rawSignature), Utils .toHex(kp.getPublic.getEncoded))
125+ Signature (toHex(kp.getPublic.getEncoded), toHex(rawSignature))
126+ }
127+
128+ def sign (t : Query , kp : KeyPair ): Signature = {
129+ val rawSignature = new Ed25519Sha3 ().rawSign(hash(t), kp)
130+ Signature (toHex(kp.getPublic.getEncoded), toHex(rawSignature))
127131 }
128132
129133 /**
@@ -202,7 +206,7 @@ object Utils {
202206 def getProtoBatchHashesHex (list : Seq [Transaction ]): Seq [String ] = list.map(reducedHash).map(toHex)
203207
204208 private def createBatch (list : Seq [Transaction ], batchType : BatchType , keyPair : KeyPair ) = {
205- val batchHashes = getBatchHashesHex (list)
209+ val batchHashes = getProtoBatchHashesHex (list)
206210 list.map { tx =>
207211 val transaction = tx.copy(
208212 tx.payload.map { p =>
0 commit comments