1
1
package io .iohk .atala .prism .node .crypto
2
2
3
- import io .iohk .atala .prism .crypto .EC
3
+ import io .iohk .atala .prism .crypto .signature .ECSignature
4
+ import io .iohk .atala .prism .crypto .{EC , Sha256 , Sha256Digest }
4
5
import io .iohk .atala .prism .node .crypto .CryptoUtils .{SecpECDSA , SecpPrivateKey , SecpPublicKey }
5
6
import org .scalatest .matchers .must .Matchers .convertToAnyMustWrapper
6
7
import org .scalatest .wordspec .AnyWordSpec
7
8
9
+ import scala .util .Try
10
+
11
+ // The end goal of this test suit is to be deleted
12
+ // Its purpose is to validate that the new implementation is
13
+ // equivalent to the old SDK it is replacing, Once the SDK is
14
+ // removed, then this tests should be deleted too
8
15
class CryptoTestsSpec extends AnyWordSpec {
9
16
10
- " crypto library" should {
17
+ // public key encoding/decoding (compressed and uncompressed)
18
+ // private key encoding/decoding
19
+ // Signature validation
20
+
21
+ " cryptoUtils library" should {
22
+
23
+ // HASHING
24
+ " Perform Sha256 hashing as the SDK" in {
25
+ val msg = EC .INSTANCE .generateKeyPair().getPublicKey.getEncoded
26
+ CryptoUtils .Sha256Hash .compute(msg).bytes mustBe Sha256 .compute(msg).getValue.toVector
27
+ }
28
+
29
+ " encode hashes as the SDK" in {
30
+ val msg = EC .INSTANCE .generateKeyPair().getPublicKey.getEncoded
31
+ CryptoUtils .Sha256Hash .compute(msg).hexEncoded mustBe Sha256 .compute(msg).getHexValue
32
+ }
33
+
34
+ " decode hex encoded hashes as the SDK" in {
35
+ val hexEncoded = " c489e391b64dc18273047935edcb5e90d97da88b58ed9c2fa5e48dd3cf878f56"
36
+ CryptoUtils .Sha256Hash .fromHex(hexEncoded).bytes mustBe Sha256Digest .fromHex(hexEncoded).getValue.toVector
37
+
38
+ val invalidHex = " W489e391b64dc18273047935edcb5e90d97da88b58ed9c2fa5e48dd3cf878f56"
39
+ val failed1 = Try (CryptoUtils .Sha256Hash .fromHex(invalidHex))
40
+ val failed2 = Try (Sha256Digest .fromHex(invalidHex))
41
+ (failed1.isFailure && failed2.isFailure) mustBe true
42
+ }
43
+
44
+ " decode hashes bytes as the SDK" in {
45
+ val bytes = Array [Byte ](- 60 , - 119 , - 29 , - 111 , - 74 , 77 , - 63 , - 126 , 115 , 4 , 121 , 53 , - 19 , - 53 , 94 , - 112 , - 39 , 125 ,
46
+ - 88 , - 117 , 88 , - 19 , - 100 , 47 , - 91 , - 28 , - 115 , - 45 , - 49 , - 121 , - 113 , 86 )
47
+ CryptoUtils .Sha256Hash .fromBytes(bytes).bytes mustBe Sha256Digest .fromBytes(bytes).getValue.toVector
48
+
49
+ val invalidBytes = Array [Byte ]()
50
+ val failed1 = Try (CryptoUtils .Sha256Hash .fromBytes(invalidBytes))
51
+ val failed2 = Try (Sha256Digest .fromBytes(invalidBytes))
52
+ (failed1.isFailure && failed2.isFailure) mustBe true
53
+ }
54
+
55
+ // SIGNING / VERIFICATION
11
56
" can verify what SDK signs" in {
12
57
val pair = EC .INSTANCE .generateKeyPair()
13
58
val pub = pair.getPublicKey
@@ -20,7 +65,7 @@ class CryptoTestsSpec extends AnyWordSpec {
20
65
SecpPublicKey .checkECDSASignature(msg, sig.getData, secp) mustBe true
21
66
}
22
67
23
- " can verify what it signs" in {
68
+ " can verify what it signs" in {
24
69
val pair = CryptoTestUtils .generateKeyPair()
25
70
val pub = pair.publicKey
26
71
val priv = pair.privateKey
@@ -31,8 +76,32 @@ class CryptoTestsSpec extends AnyWordSpec {
31
76
SecpPublicKey .checkECDSASignature(msg, sig, pub) mustBe true
32
77
}
33
78
34
- " public key uncompressed encoding decoding" in {
35
- // /
79
+ " can produce valid signatures according to SDK" in {
80
+ val pair = CryptoTestUtils .generateKeyPair()
81
+ val pub = CryptoTestUtils .getUnderlyingKey(pair.publicKey)
82
+ val priv = pair.privateKey
83
+
84
+ val msg = CryptoUtils .Sha256Hash .compute(pub.getEncodedCompressed).bytes.toArray
85
+ val sig = new ECSignature (SecpECDSA .signBytes(msg, priv).bytes)
86
+
87
+ EC .INSTANCE .verifyBytes(msg, pub, sig) mustBe true
88
+ }
89
+
90
+ // PUBLIC KEY ENCODING / DECODING
91
+ " public key uncompressed encoding us compatible with SDK" in {
92
+ val secpPublicKey = CryptoTestUtils .generateKeyPair().publicKey
93
+ val sdkPubKey = EC .INSTANCE .generateKeyPair().getPublicKey
94
+
95
+ val uSecp = secpPublicKey.unCompressed
96
+ val uSdk = sdkPubKey.getEncoded
97
+
98
+ // we parse the keys in the opposite library
99
+ val parsedSDKKey = SecpPublicKey .unsafetoPublicKeyFromUncompressed(uSdk)
100
+ val parsedSecpKey = EC .INSTANCE .toPublicKeyFromBytes(uSecp)
101
+
102
+ // we compare the encodings
103
+ uSdk.toVector mustBe parsedSDKKey.unCompressed.toVector
104
+ uSecp.toVector mustBe parsedSecpKey.getEncoded.toVector
36
105
}
37
106
38
107
" private key encoding decoding" in {
@@ -41,9 +110,6 @@ class CryptoTestsSpec extends AnyWordSpec {
41
110
val encodedPvKey = privK.getEncoded
42
111
val secp = SecpPrivateKey .unsafefromBytesCompressed(encodedPvKey)
43
112
44
- println(secp.bytes.toVector)
45
- println(secp.getEncoded.toVector)
46
-
47
113
encodedPvKey.toVector mustBe secp.getEncoded.toVector
48
114
}
49
115
0 commit comments