Skip to content

Commit

Permalink
fix:adapte sui keystore format change.
Browse files Browse the repository at this point in the history
Signed-off-by: grapebaba <[email protected]>
  • Loading branch information
GrapeBaBa committed Jan 18, 2023
1 parent d8d2bf7 commit ca4e967
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 51 deletions.
5 changes: 2 additions & 3 deletions src/main/java/io/sui/crypto/ED25519KeyPair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 [email protected]
* Copyright 2022-2023 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
Expand Down Expand Up @@ -92,8 +92,7 @@ public String sign(String msg) throws SigningException {
* @return the sui key pair
*/
public static ED25519KeyPair decodeBase64(byte[] encoded) {
Ed25519PrivateKeyParameters privateKeyParameters =
new Ed25519PrivateKeyParameters(encoded, 1 + Ed25519PublicKeyParameters.KEY_SIZE);
Ed25519PrivateKeyParameters privateKeyParameters = new Ed25519PrivateKeyParameters(encoded, 1);
Ed25519PublicKeyParameters publicKeyParameters = privateKeyParameters.generatePublicKey();
return new ED25519KeyPair(privateKeyParameters, publicKeyParameters);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/io/sui/crypto/SECP256K1KeyPair.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 [email protected]
* Copyright 2022-2023 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
Expand Down Expand Up @@ -53,9 +53,7 @@ public SECP256K1KeyPair(byte[] privateKey) {
* @return the sui key pair
*/
public static SECP256K1KeyPair decodeBase64(byte[] encoded) {
final int compressedPublicKeySize = 33;
return new SECP256K1KeyPair(
Arrays.copyOfRange(encoded, 1 + compressedPublicKeySize, encoded.length));
return new SECP256K1KeyPair(Arrays.copyOfRange(encoded, 1, encoded.length));
}

@Override
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/io/sui/crypto/ED25519KeyPairTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 [email protected]
* Copyright 2022-2023 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
Expand Down Expand Up @@ -40,20 +40,16 @@ class ED25519KeyPairTest {
/** Address. */
@Test
void address() {
final String base64 =
"AOSqUUDaiPGYESoI/G13YwT2qyWW/RRvsW2G7IoCG"
+ "URYYbRLmbwt4NZ9m9x7s8taYhCJg9OQdkrmSTVioUVpCSM=";
final String base64 = "ADfbVnAT2QLG7W+bM+1ENzEKAxnoUx10+WfGg5zx8VRm";
final ED25519KeyPair keyPair = ED25519KeyPair.decodeBase64(Base64.decode(base64));

assertEquals("0x1e7752f22228753e5745f5ac8ad4ef1bbc502845", keyPair.address());
assertEquals("0x0a7421363a1f6a82800f7c9340ac02b5905798cb", keyPair.address());
}

/** Decode base 64. */
@Test
void decodeBase64() throws CryptoException {
final String base64 =
"AOSqUUDaiPGYESoI/G13YwT2qyWW/RRvsW2G7IoCG"
+ "URYYbRLmbwt4NZ9m9x7s8taYhCJg9OQdkrmSTVioUVpCSM=";
final String base64 = "ADfbVnAT2QLG7W+bM+1ENzEKAxnoUx10+WfGg5zx8VRm";
final ED25519KeyPair ed25519KeyPair = ED25519KeyPair.decodeBase64(Base64.decode(base64));

Signer signer = new Ed25519Signer();
Expand Down
22 changes: 9 additions & 13 deletions src/test/java/io/sui/crypto/FileBasedKeyStoreTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 [email protected]
* Copyright 2022-2023 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
Expand Down Expand Up @@ -48,19 +48,15 @@ void initKeyPairs() {
new FileBasedKeyStore(
Paths.get("src/test/resources/config/sui.keystore").toAbsolutePath().toString());

assertEquals(11, fileBasedKeyStore.keys.size());
assertEquals(7, fileBasedKeyStore.keys.size());
String expected =
"0x1e7752f22228753e5745f5ac8ad4ef1bbc502845\n"
+ "0x207f2c9f08472b1ff68644fdfc7a70df10cb3d4e\n"
+ "0x49ef9b602b76a37e0f9177783755c1a190866e72\n"
+ "0x51972acc644b8c6dd81d6088780b40e842a0a10c\n"
+ "0x51de405091c9f971fc6085d384f9ba764f268fca\n"
+ "0x63485e00efc944d62349b79f88a11b7cacc2a764\n"
+ "0x78cec6011e9d0690d5fbbfa4d25987a087a88ee7\n"
+ "0x887ddfbf2bc37d757eabb08d62bf725a04922bde\n"
+ "0xca21af1b5b347d315d7355ff9e6e73cc79d0a4d0\n"
+ "0xe8da3f038048e2cd6339e916a926874d0d0604b7\n"
+ "0xea79464d86786b7a7a63e3f13f798f29f5e65947\n";
"0x0a7421363a1f6a82800f7c9340ac02b5905798cb\n"
+ "0x64e1714eb06c22dff8ac7dab7eb302d4abededf9\n"
+ "0xa0fd2a6814cff90d4463fb7e5b5b81d01d763472\n"
+ "0xd456963facd4c40adfc3858d1a33c93d4d759664\n"
+ "0xee3752e38e25a52c5d35cff23695d1115d3a0149\n"
+ "0xf76f8c0e679c55a9d17229b9cbda702645836aa7\n"
+ "0xfa423b6448e5e83d03e0d98ce00b5be32da5ee86\n";
StringBuilder actual = new StringBuilder();
for (String key : fileBasedKeyStore.keys.navigableKeySet()) {
actual.append(key).append("\n");
Expand Down
12 changes: 4 additions & 8 deletions src/test/java/io/sui/crypto/SECP256K1KeyPairTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 [email protected]
* Copyright 2022-2023 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
Expand Down Expand Up @@ -37,20 +37,16 @@ class SECP256K1KeyPairTest {
/** Address. */
@Test
void address() {
final String base64 =
"AQLE7fDdDt4nrbGgCX8umsFscJRFY4t3Bkrk3MaB"
+ "b1nnA6dD5QHIFrPAdPQtdDyfoJNjiN/ghxuVLxfHxehcwec0";
final String base64 = "ABw1fhWdG+Ni9eFfyLdfdmsiMWyirHCV/UVC9jcloBm8";
final SuiKeyPair<ECKey> secp256K1KeyPair = SECP256K1KeyPair.decodeBase64(Base64.decode(base64));

assertEquals("0xe8da3f038048e2cd6339e916a926874d0d0604b7", secp256K1KeyPair.address());
assertEquals("0xea650e4199befc011d2373decabc4c3deb0275d8", secp256K1KeyPair.address());
}

/** Decode base 64. */
@Test
void decodeBase64() {
final String base64 =
"AQLE7fDdDt4nrbGgCX8umsFscJRFY4t3Bkrk3MaBb1nnA6dD5QH"
+ "IFrPAdPQtdDyfoJNjiN/ghxuVLxfHxehcwec0";
final String base64 = "ABw1fhWdG+Ni9eFfyLdfdmsiMWyirHCV/UVC9jcloBm8";
final SuiKeyPair<ECKey> secp256K1KeyPair = SECP256K1KeyPair.decodeBase64(Base64.decode(base64));
final String msg = "test";
final ECDSASignature signature = secp256K1KeyPair.keyPair.sign(Sha256Hash.of(msg.getBytes()));
Expand Down
6 changes: 2 additions & 4 deletions src/test/java/io/sui/crypto/SuiKeyPairTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 [email protected]
* Copyright 2022-2023 [email protected]
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
Expand Down Expand Up @@ -34,9 +34,7 @@ class SuiKeyPairTest {
/** Decode base 64. */
@Test
void decodeBase64() {
final String base64 =
"AQLE7fDdDt4nrbGgCX8umsFscJRFY4t3Bkrk3MaB"
+ "b1nnA6dD5QHIFrPAdPQtdDyfoJNjiN/ghxuVLxfHxehcwec0";
final String base64 = "ADfbVnAT2QLG7W+bM+1ENzEKAxnoUx10+WfGg5zx8VRm";
try {
SuiKeyPair.decodeBase64(base64);
} catch (SignatureSchemeNotSupportedException e) {
Expand Down
18 changes: 7 additions & 11 deletions src/test/resources/config/sui.keystore
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
[
"AOSqUUDaiPGYESoI/G13YwT2qyWW/RRvsW2G7IoCGURYYbRLmbwt4NZ9m9x7s8taYhCJg9OQdkrmSTVioUVpCSM=",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