Skip to content

Commit

Permalink
fix:fix format
Browse files Browse the repository at this point in the history
Signed-off-by: grapebaba <[email protected]>
  • Loading branch information
GrapeBaBa committed Feb 9, 2023
1 parent 3ed6049 commit f8410b4
Show file tree
Hide file tree
Showing 10 changed files with 221 additions and 59 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ checkstyle {
// configFile = project(":").file("config/checkstyle/google_checks.xml")
// SUN style (closest to modern Java styles) -- the basis for this project:
// configFile = project(":").file("config/checkstyle/sun_checks.xml")
ignoreFailures = false
maxWarnings = 0
}

spotless {
Expand Down
29 changes: 22 additions & 7 deletions src/main/java/io/sui/crypto/AbstractKeyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public NavigableSet<String> addresses() {
@Override
public abstract void addKey(String address, SuiKeyPair<?> keyPair);

/**
* Generate new key key response.
*
* @param schema the schema
* @return the key response
* @throws SignatureSchemeNotSupportedException the signature scheme not supported exception
*/
public KeyResponse generateNewKey(SignatureScheme schema)
throws SignatureSchemeNotSupportedException {

Expand All @@ -67,25 +74,33 @@ public KeyResponse generateNewKey(SignatureScheme schema)
}

byte[] seed = MnemonicCode.toSeed(mnemonic, "");
SuiKeyPair keyPair = genSuiKeyPair(seed, schema);
SuiKeyPair<?> keyPair = genSuiKeyPair(seed, schema);

this.addKey(keyPair.address(), keyPair);
return new KeyResponse(StringUtils.join(mnemonic, " "), keyPair.address());
}

/**
* Import from mnemonic string.
*
* @param mnemonic the mnemonic
* @param schema the schema
* @return the string
* @throws SignatureSchemeNotSupportedException the signature scheme not supported exception
*/
public String importFromMnemonic(String mnemonic, SignatureScheme schema)
throws SignatureSchemeNotSupportedException {
// todo check mnemonic

byte[] seed = MnemonicCode.toSeed(Arrays.asList(mnemonic.split(" ")), "");

SuiKeyPair keyPair = genSuiKeyPair(seed, schema);
SuiKeyPair<?> keyPair = genSuiKeyPair(seed, schema);

this.addKey(keyPair.address(), keyPair);
return keyPair.address();
}

private SuiKeyPair genSuiKeyPair(byte[] seed, SignatureScheme schema)
private SuiKeyPair<?> genSuiKeyPair(byte[] seed, SignatureScheme schema)
throws SignatureSchemeNotSupportedException {
switch (schema) {
case ED25519:
Expand All @@ -97,19 +112,19 @@ private SuiKeyPair genSuiKeyPair(byte[] seed, SignatureScheme schema)
}
}

@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
private ED25519KeyPair genED25519KeyPair(byte[] seed) {
ED25519KeyDerive key = ED25519KeyDerive.createKeyByDefaultPath(seed);
Ed25519PrivateKeyParameters parameters = new Ed25519PrivateKeyParameters(key.getKey());
Ed25519PublicKeyParameters publicKeyParameters = parameters.generatePublicKey();

ED25519KeyPair keyPair = new ED25519KeyPair(parameters, publicKeyParameters);
return keyPair;
return new ED25519KeyPair(parameters, publicKeyParameters);
}

@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
private SECP256K1KeyPair genSECP256K1KeyPair(byte[] seed) {
SECP256K1KeyDerive key = SECP256K1KeyDerive.createKeyByDefaultPath(seed);

SECP256K1KeyPair keyPair = new SECP256K1KeyPair(key.getKey());
return keyPair;
return new SECP256K1KeyPair(key.getKey());
}
}
63 changes: 50 additions & 13 deletions src/main/java/io/sui/crypto/ED25519KeyDerive.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 [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 @@ -33,41 +33,62 @@
* @author fearlessfe
* @since 2023.02
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public class ED25519KeyDerive {

private final String defaultDerivePath = "m/44H/784H/0H/0H/0H";
private byte[] key;
private byte[] chaincode;
private static final String DEFAULT_DERIVE_PATH = "m/44H/784H/0H/0H/0H";
private final byte[] key;
private final byte[] chaincode;

/**
* Create key by default path ed 25519 key derive.
*
* @param seed the seed
* @return the ed 25519 key derive
*/
public static ED25519KeyDerive createKeyByDefaultPath(byte[] seed) {
return createMasterKey(seed).deriveFromPath("");
}

/**
* Create master key ed 25519 key derive.
*
* @param seed the seed
* @return the ed 25519 key derive
*/
public static ED25519KeyDerive createMasterKey(byte[] seed) {
byte[] i = HDUtils.hmacSha512("ed25519 seed".getBytes(Charset.defaultCharset()), seed);
byte[] il = Arrays.copyOfRange(i, 0, 32);
byte[] ir = Arrays.copyOfRange(i, 32, 64);
return new ED25519KeyDerive(il, ir);
}

/**
* Instantiates a new Ed 25519 key derive.
*
* @param key the key
* @param chaincode the chaincode
*/
public ED25519KeyDerive(byte[] key, byte[] chaincode) {
this.key = key;
this.chaincode = chaincode;
}

/**
* Derive ed 25519 key derive.
*
* @param index the index
* @return the ed 25519 key derive
*/
public ED25519KeyDerive derive(int index) {
if (!hasHardenedBit(index)) {}
if (!hasHardenedBit(index)) {
// todo: create an exception
throw new RuntimeException();
}

byte[] indexBytes = new byte[4];
ByteBuffer.wrap(indexBytes).putInt(index);

// byte[] a = new byte[]{0x00};
//
// ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// outputStream.write(a);
// outputStream.write(this.key);
// outputStream.write(indexBytes);

byte[] data = Bytes.concat(new byte[] {0x00}, this.key, indexBytes);

byte[] i = HDUtils.hmacSha512(this.chaincode, data);
Expand All @@ -77,9 +98,15 @@ public ED25519KeyDerive derive(int index) {
return new ED25519KeyDerive(il, ir);
}

/**
* Derive from path ed 25519 key derive.
*
* @param path the path
* @return the ed 25519 key derive
*/
public ED25519KeyDerive deriveFromPath(String path) {
if (StringUtils.isAnyBlank(path)) {
path = defaultDerivePath;
path = DEFAULT_DERIVE_PATH;
}
HDPath hdPath = HDPath.parsePath(path);
Iterator<ChildNumber> it = hdPath.iterator();
Expand All @@ -94,10 +121,20 @@ private boolean hasHardenedBit(int a) {
return (a & ChildNumber.HARDENED_BIT) != 0;
}

/**
* Get key byte [ ].
*
* @return the byte [ ]
*/
public byte[] getKey() {
return key;
}

/**
* Get chaincode byte [ ].
*
* @return the byte [ ]
*/
public byte[] getChaincode() {
return chaincode;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/sui/crypto/ED25519KeyPair.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,5 @@ public String encodePrivateKey() {
Ed25519PrivateKeyParameters pair = (Ed25519PrivateKeyParameters) this.keyPair.getPrivate();
byte[] data = Bytes.concat(new byte[] {SignatureScheme.ED25519.getScheme()}, pair.getEncoded());
return Base64.toBase64String(data);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.sui.crypto;

/**
* The type File based key store save exception.
*
* @author fearlessfe
* @since 2023 02
*/
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/io/sui/crypto/KeyResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,41 @@
package io.sui.crypto;

/**
* The type Key response.
*
* @author fearlessfe
* @since 2023 02
*/
public class KeyResponse {
private String mnemonic;
private String address;

private final String mnemonic;
private final String address;

/**
* Instantiates a new Key response.
*
* @param mnemonic the mnemonic
* @param address the address
*/
public KeyResponse(String mnemonic, String address) {
this.mnemonic = mnemonic;
this.address = address;
}

/**
* Gets mnemonic.
*
* @return the mnemonic
*/
public String getMnemonic() {
return mnemonic;
}

/**
* Gets address.
*
* @return the address
*/
public String getAddress() {
return address;
}
Expand Down
Loading

0 comments on commit f8410b4

Please sign in to comment.