Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: init crypto #45

Merged
merged 2 commits into from
Dec 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/badges/branches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion .github/badges/jacoco.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,39 @@ cd sui/target/release

TODO

## Supported APIs
- [ ] sui_batchTransaction
- [x] sui_dryRunTransaction
- [ ] sui_executeTransaction
- [ ] sui_getCoinMetadata
- [x] sui_getCommitteeInfo
- [x] sui_getEvents
- [x] sui_getMoveFunctionArgTypes
- [x] sui_getNormalizedMoveFunction
- [x] sui_getNormalizedMoveModule
- [x] sui_getNormalizedMoveModulesByPackage
- [x] sui_getNormalizedMoveStruct
- [x] sui_getObject
- [x] sui_getObjectsOwnedByAddress
- [x] sui_getObjectsOwnedByObject
- [x] sui_getRawObject
- [x] sui_getTotalTransactionNumber
- [x] sui_getTransaction
- [ ] sui_getTransactions
- [x] sui_getTransactionsInRange
- [x] sui_mergeCoins
- [x] sui_moveCall
- [x] sui_pay
- [x] sui_payAllSui
- [x] sui_paySui
- [x] sui_publish
- [x] sui_splitCoin
- [x] sui_splitCoinEqual
- [ ] sui_subscribeEvent
- [x] sui_transferObject
- [x] sui_transferSui
- [ ] sui_tryGetPastObject

## Examples

### create client
Expand Down
8 changes: 8 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ dependencies {
implementation 'com.squareup.okhttp3:okhttp:4.10.0'
// https://mvnrepository.com/artifact/org.apache.commons/commons-lang3
implementation 'org.apache.commons:commons-lang3:3.12.0'
// https://mvnrepository.com/artifact/org.bouncycastle/bcprov-jdk18on
implementation 'org.bouncycastle:bcprov-jdk18on:1.72'
// https://mvnrepository.com/artifact/org.web3j/crypto
implementation('org.web3j:crypto:4.9.5') {
exclude group: 'org.bouncycastle', module: 'bcprov-jdk15on'
exclude group: 'com.fasterxml.jackson.core', module: 'jackson-databind'
exclude group: 'org.slf4j', module: 'slf4j-api'
}

// https://mvnrepository.com/artifact/com.squareup.okhttp3/mockwebserver
testImplementation 'com.squareup.okhttp3:mockwebserver:4.10.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.sui;


import io.sui.clients.ExecutionClient;
import io.sui.clients.ExecutionClientImpl;
import io.sui.jsonrpc.GsonJsonHandler;
import io.sui.jsonrpc.JsonHandler;
import io.sui.jsonrpc.JsonRpcClientProvider;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@


import com.google.common.collect.Lists;
import io.sui.clients.JsonRpcTransactionBuilder;
import io.sui.clients.TransactionBuilder;
import io.sui.jsonrpc.GsonJsonHandler;
import io.sui.jsonrpc.JsonHandler;
import io.sui.jsonrpc.JsonRpcClientProvider;
Expand Down
4 changes: 3 additions & 1 deletion src/integrationTest/java/io/sui/QueryClientImplIntTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import static org.junit.jupiter.api.Assertions.assertEquals;

import io.sui.clients.QueryClient;
import io.sui.clients.QueryClientImpl;
import io.sui.jsonrpc.GsonJsonHandler;
import io.sui.jsonrpc.JsonHandler;
import io.sui.jsonrpc.JsonRpc20Response.Error.ErrorCode;
Expand Down Expand Up @@ -317,7 +319,7 @@ void getTransactionsInRange() throws ExecutionException, InterruptedException {
@DisplayName("Test getEvents.")
void getEvents() throws ExecutionException, InterruptedException {
TransactionEventQuery query = new TransactionEventQuery();
query.setTransaction("ov1tDrhdOqRW2uFweTbSSTaQbBbnjHWmrsh675lwb0Q=");
query.setTransaction("9HF7ZAfdStA8d9eUuxfKBn4V2vWcvzT8tccs4CAVrFtj");
CompletableFuture<PaginatedEvents> res = client.getEvents(query, null, 1, false);
System.out.println(res.get());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* specific language governing permissions and limitations under the License.
*/

package io.sui;
package io.sui.clients;


import io.sui.models.transactions.TransactionEffects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* specific language governing permissions and limitations under the License.
*/

package io.sui;
package io.sui.clients;


import com.google.common.collect.Lists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* specific language governing permissions and limitations under the License.
*/

package io.sui;
package io.sui.clients;


import com.google.common.collect.Lists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* specific language governing permissions and limitations under the License.
*/

package io.sui;
package io.sui.clients;


import io.sui.models.CommitteeInfoResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* specific language governing permissions and limitations under the License.
*/

package io.sui;
package io.sui.clients;


import com.google.common.collect.Lists;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* specific language governing permissions and limitations under the License.
*/

package io.sui;
package io.sui.clients;


import io.sui.models.transactions.TransactionBytes;
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/io/sui/crypto/AbstractKeyStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2022 281165273grape@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.sui.crypto;


import java.util.concurrent.ConcurrentSkipListMap;

/**
* The type Abstract key store.
*
* @author grapebaba
* @since 2022.11
*/
public abstract class AbstractKeyStore implements KeyStore {

/** The Keys. */
protected final ConcurrentSkipListMap<String, SuiKeyPair<?>> keys = new ConcurrentSkipListMap<>();
}
72 changes: 72 additions & 0 deletions src/main/java/io/sui/crypto/ED25519KeyPair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 281165273grape@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.sui.crypto;


import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
import org.bouncycastle.crypto.params.Ed25519PrivateKeyParameters;
import org.bouncycastle.crypto.params.Ed25519PublicKeyParameters;
import org.bouncycastle.jcajce.provider.digest.SHA3.Digest256;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;

/**
* The type Secp256k1 key pair.
*
* @author grapebaba
* @since 2022.11
*/
@SuppressWarnings("checkstyle:AbbreviationAsWordInName")
public class ED25519KeyPair extends SuiKeyPair<AsymmetricCipherKeyPair> {

/**
* Instantiates a new Ed 25519 key pair.
*
* @param privateKeyParameters the private key parameters
* @param publicKeyParameters the public key parameters
*/
public ED25519KeyPair(
Ed25519PrivateKeyParameters privateKeyParameters,
Ed25519PublicKeyParameters publicKeyParameters) {
this.keyPair = new AsymmetricCipherKeyPair(publicKeyParameters, privateKeyParameters);
}

@Override
public String address() {
final Digest256 digest256 = new Digest256();
final byte[] hash =
digest256.digest(
Arrays.prepend(
((Ed25519PublicKeyParameters) keyPair.getPublic()).getEncoded(),
SignatureScheme.ED25519.getScheme()));
return "0x" + StringUtils.substring(Hex.toHexString(hash), 0, 40);
}

/**
* Decode base 64 sui key pair.
*
* @param encoded the encoded
* @return the sui key pair
*/
public static ED25519KeyPair decodeBase64(byte[] encoded) {
Ed25519PrivateKeyParameters privateKeyParameters =
new Ed25519PrivateKeyParameters(encoded, 1 + Ed25519PublicKeyParameters.KEY_SIZE);
Ed25519PublicKeyParameters publicKeyParameters = privateKeyParameters.generatePublicKey();
return new ED25519KeyPair(privateKeyParameters, publicKeyParameters);
}
}
72 changes: 72 additions & 0 deletions src/main/java/io/sui/crypto/FileBasedKeyStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022 281165273grape@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.sui.crypto;


import com.google.gson.Gson;
import com.google.gson.JsonArray;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

/**
* The type File based key store.
*
* @author grapebaba
* @since 2022.11
*/
public class FileBasedKeyStore extends AbstractKeyStore {

private final String path;

/**
* Instantiates a new File based key store.
*
* @param path the path
*/
public FileBasedKeyStore(String path) {
this.path = path;
if (Files.exists(Paths.get(this.path))) {
try {
JsonArray json =
new Gson().fromJson(Files.newBufferedReader(Paths.get(this.path)), JsonArray.class);
json.asList()
.forEach(
jsonElement -> {
try {
final SuiKeyPair<?> keyPair =
SuiKeyPair.decodeBase64(jsonElement.getAsString());
FileBasedKeyStore.super.keys.putIfAbsent(keyPair.address(), keyPair);
} catch (SignatureSchemeNotSupportedException e) {
throw new FileBasedKeyStoreInitException(e);
}
});
} catch (IOException e) {
throw new FileBasedKeyStoreInitException(e);
}
}
}

/**
* Gets path.
*
* @return the path
*/
public String getPath() {
return path;
}
}
35 changes: 35 additions & 0 deletions src/main/java/io/sui/crypto/FileBasedKeyStoreInitException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2022 281165273grape@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.sui.crypto;

/**
* The type FileBasedKeyStoreInitException.
*
* @author grapebaba
* @since 2022.11
*/
public class FileBasedKeyStoreInitException extends RuntimeException {

/**
* Instantiates a new File based key store init exception.
*
* @param cause the cause
*/
public FileBasedKeyStoreInitException(Throwable cause) {
super(cause);
}
}
25 changes: 25 additions & 0 deletions src/main/java/io/sui/crypto/KeyStore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright 2022 281165273grape@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package io.sui.crypto;

/**
* The interface Key store.
*
* @author grapebaba
* @since 2022.11
*/
public interface KeyStore {}
Loading