Skip to content

Commit 154cdfc

Browse files
limingxielimingxie
authored andcommitted
PubKeyInfo.getAddress method add PUBLIC_KEY_LENGTH_IN_HEX if condition
1 parent dec4746 commit 154cdfc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

key-core/src/main/java/com/webank/keygen/model/PubKeyInfo.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public class PubKeyInfo implements Cloneable {
2525
public String getAddress(){
2626
EccTypeEnums eccTypeEnums = EccTypeEnums.getEccByName(eccName);
2727
CryptoKeyPair cryptoKeyPair = KeyUtils.getCryptKeyPair(eccTypeEnums);
28-
return Numeric.toHexString(cryptoKeyPair.getAddress(Arrays.copyOfRange(publicKey,1,publicKey.length)));
28+
if (Numeric.toHexString(publicKey).length() == CryptoKeyPair.PUBLIC_KEY_LENGTH_IN_HEX){
29+
return Numeric.toHexString(cryptoKeyPair.getAddress(publicKey));
30+
} else {
31+
return Numeric.toHexString(cryptoKeyPair.getAddress(Arrays.copyOfRange(publicKey,1,publicKey.length)));
32+
}
2933
}
3034

3135
@Override

key-core/src/test/java/com/webank/keygen/service/PKeyByMnemonicServiceTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,31 @@ public void testGeneratePrivateKeyByMnemonic() throws Exception {
9090
Assert.assertTrue(KeyUtils.isAddressEquals("6f4da6efc551de988ce3f65bd9d21e9471d6f0ee",address2));
9191
Assert.assertTrue(Objects.equals("2579ab059f514514a04b0f941bd8c09b53b62cdbfa8cfdfa688869ce45fbd240", cc2));
9292
Assert.assertTrue(Objects.equals("secp256k1", pkeyInfo2.getEccName()));
93+
}
94+
95+
@Test
96+
public void testGeneratePrivateKeyByMnemonic2() throws Exception {
97+
String mnemonicStr = "common feel corn harvest dirt rapid potato verify enough sausage eye limb";
98+
PkeyInfo pkeyInfo1 = service.generatePrivateKeyByMnemonic(mnemonicStr, "123456", EccTypeEnums.SM2P256V1);
99+
PkeyInfo pkeyInfo2 = service.generatePrivateKeyByMnemonic(mnemonicStr, "123456", EccTypeEnums.SECP256K1);
100+
101+
String pkeyInfo1Str = Numeric.toHexString(pkeyInfo1.getPrivateKey());
102+
String address1 = pkeyInfo1.getAddress();
103+
String cc1 = Numeric.toHexString(pkeyInfo1.getChainCode());
104+
105+
Assert.assertTrue(Objects.equals("17370d3bb51ff24ef149e410dd9c07ba57bedeb509adbd4b21e07fbaa72a4301", pkeyInfo1Str));
106+
Assert.assertTrue(KeyUtils.isAddressEquals("493286567e6b18601fca2acd1d1af3b695fc3994", address1));
107+
Assert.assertTrue(Objects.equals("1f254a38fa868219824f68136727ee1fb2df638c31feabdba20de870ba484457", cc1));
108+
Assert.assertTrue(Objects.equals("sm2p256v1", pkeyInfo1.getEccName()));
109+
110+
String pkeyInfo2Str = Numeric.toHexString(pkeyInfo2.getPrivateKey());
111+
String address2 = pkeyInfo2.getAddress();
112+
String cc2 = Numeric.toHexString(pkeyInfo2.getChainCode());
93113

114+
Assert.assertTrue(Objects.deepEquals("17370d3bb51ff24ef149e410dd9c07ba57bedeb509adbd4b21e07fbaa72a4301", pkeyInfo2Str));
115+
Assert.assertTrue(KeyUtils.isAddressEquals("f80e95fb536ad12518737bc3dc1a7582ffe66768",address2));
116+
Assert.assertTrue(Objects.deepEquals("1f254a38fa868219824f68136727ee1fb2df638c31feabdba20de870ba484457", cc2));
117+
Assert.assertTrue(Objects.deepEquals("secp256k1", pkeyInfo2.getEccName()));
94118
}
95119
}
96120

0 commit comments

Comments
 (0)