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

gRPC classes #33

Merged
merged 15 commits into from
May 22, 2019
Merged
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.gitignore
.idea/*
wavesj.iml
.idea
target
*.iml
26 changes: 26 additions & 0 deletions examples/src/main/java/GRPCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import com.wavesplatform.api.grpc.TransactionsApiOuterClass.*;
import com.wavesplatform.api.grpc.TransactionsApiGrpc;
import com.wavesplatform.protobuf.transaction.RecipientOuterClass;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

import java.util.Iterator;

public class GRPCTest {
public static void main(String[] args) {
final ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 6870)
.usePlaintext()
.build();

final TransactionsApiGrpc.TransactionsApiBlockingStub blockingStub = TransactionsApiGrpc.newBlockingStub(channel);
final TransactionsRequest transactionsRequest = TransactionsRequest.newBuilder()
.setRecipient(RecipientOuterClass.Recipient.newBuilder().setAlias("test_recipient").build())
.build();

final Iterator<TransactionResponse> transactions = blockingStub.getTransactions(transactionsRequest);
for (int i = 0; i < 1000 && transactions.hasNext(); i++) {
final TransactionResponse txResponse = transactions.next();
System.out.printf("Transaction = %s, height = %d\n", txResponse.getTransaction(), txResponse.getHeight());
}
}
}
63 changes: 63 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,55 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>1.0</version>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchains>
<protobuf>
<version>[2.4,2.5)</version>
</protobuf>
</toolchains>
</configuration>
</plugin>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.6.1</version>
<extensions>true</extensions>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.20.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
</plugin>
</plugins>

<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.0</version>
</extension>
</extensions>
</build>

<dependencies>
Expand Down Expand Up @@ -177,5 +225,20 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.20.0</version>
</dependency>
</dependencies>
</project>
2 changes: 1 addition & 1 deletion src/main/java/com/wavesplatform/wavesj/ByteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static String putRecipient(ByteBuffer buffer, byte chainId, String recipi
}

public static String hash(byte[] bytes) {
return Base58.encode(Hash.hash(bytes, 0, bytes.length, Hash.BLAKE2B256));
return Base58.encode(Hash.blake2b(bytes, 0, bytes.length));
}

public static String sign(PrivateKeyAccount account, ByteBuffer buffer) {
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/wavesplatform/wavesj/GRPCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.wavesplatform.wavesj;

import com.wavesplatform.api.grpc.BlocksApiGrpc;
import com.wavesplatform.api.grpc.BlocksApiOuterClass;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;

public class GRPCTest {
public static void main(String[] args) {
final ManagedChannel channel = ManagedChannelBuilder.forAddress("mainnet-aws-fr-3.wavesnodes.com", 6870)
.usePlaintext()
.build();

final BlocksApiGrpc.BlocksApiBlockingStub blocksApiBlockingStub = BlocksApiGrpc.newBlockingStub(channel);

final BlocksApiOuterClass.BlockWithHeight block = blocksApiBlockingStub.getBlock(BlocksApiOuterClass.BlockRequest.newBuilder().setHeight(-1).build());
System.out.println(block);
}
}
50 changes: 29 additions & 21 deletions src/main/java/com/wavesplatform/wavesj/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,43 @@
import org.bouncycastle.crypto.digests.SHA256Digest;

public class Hash {
public static final ThreadLocal<Digest> BLAKE2B256 = new ThreadLocal<Digest>();
public static final ThreadLocal<Digest> KECCAK256 = new ThreadLocal<Digest>();
public static final ThreadLocal<Digest> SHA256 = new ThreadLocal<Digest>();
private static final ThreadLocal<Digest> BLAKE2B256 = new ThreadLocal<Digest>();
private static final ThreadLocal<Digest> KECCAK256 = new ThreadLocal<Digest>();
private static final ThreadLocal<Digest> SHA256 = new ThreadLocal<Digest>();

public static byte[] secureHash(byte[] message, int ofs, int len) {
final byte[] blake2b = hash(message, ofs, len, Hash.BLAKE2B256);
return hash(blake2b, 0, blake2b.length, Hash.KECCAK256);
}

public static byte[] blake2b(byte[] message, int ofs, int len) {
return hash(message, ofs, len, Hash.BLAKE2B256);
}

public static byte[] sha256(byte[] message, int ofs, int len) {
return hash(message, ofs, len, Hash.SHA256);
}

private static Digest digest(ThreadLocal<Digest> cache) {
Digest d = cache.get();
if (d == null) {
Digest digest = cache.get();
if (digest == null) {
if (cache == BLAKE2B256) {
d = new Blake2bDigest(256);
digest = new Blake2bDigest(256);
} else if (cache == KECCAK256) {
d = new KeccakDigest(256);
digest = new KeccakDigest(256);
} else if (cache == SHA256) {
d = new SHA256Digest();
digest = new SHA256Digest();
}
cache.set(d);
cache.set(digest);
}
return d;
}

protected static byte[] hash(byte[] message, int ofs, int len, ThreadLocal<Digest> alg) {
Digest d = digest(alg);
byte[] res = new byte[d.getDigestSize()];
d.update(message, ofs, len);
d.doFinal(res, 0);
return res;
return digest;
}

public static byte[] secureHash(byte[] message, int ofs, int len) {
byte[] blake2b = hash(message, ofs, len, Hash.BLAKE2B256);
return hash(blake2b, 0, blake2b.length, Hash.KECCAK256);
private static byte[] hash(byte[] message, int ofs, int len, ThreadLocal<Digest> alg) {
final Digest digest = digest(alg);
final byte[] result = new byte[digest.getDigestSize()];
digest.update(message, ofs, len);
digest.doFinal(result, 0);
return result;
}
}
8 changes: 3 additions & 5 deletions src/main/java/com/wavesplatform/wavesj/PrivateKeyAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import java.security.SecureRandom;
import java.util.Arrays;

import static com.wavesplatform.wavesj.Hash.*;

public class PrivateKeyAccount extends PublicKeyAccount {

private static final Curve25519 cipher = Curve25519.getInstance(Curve25519.BEST);
Expand Down Expand Up @@ -250,7 +248,7 @@ public String sign(byte[] bytes) {
public static String generateSeed() {
byte[] bytes = new byte[21];
new SecureRandom().nextBytes(bytes);
byte[] rhash = hash(bytes, 0, 20, SHA256);
byte[] rhash = Hash.sha256(bytes, 0, 20);
bytes[20] = rhash[0];
BigInteger rand = new BigInteger(bytes);
BigInteger mask = new BigInteger(new byte[]{0, 0, 7, -1}); // 11 lower bits
Expand All @@ -267,10 +265,10 @@ private static byte[] privateKey(String seed, int nonce) {
// account seed from seed & nonce
ByteBuffer buf = ByteBuffer.allocate(seed.getBytes().length + 4);
buf.putInt(nonce).put(seed.getBytes());
byte[] accountSeed = secureHash(buf.array(), 0, buf.array().length);
byte[] accountSeed = Hash.secureHash(buf.array(), 0, buf.array().length);

// private key from account seed & chainId
byte[] hashedSeed = hash(accountSeed, 0, accountSeed.length, SHA256);
byte[] hashedSeed = Hash.sha256(accountSeed, 0, accountSeed.length);
byte[] privateKey = Arrays.copyOf(hashedSeed, 32);
privateKey[0] &= 248;
privateKey[31] &= 127;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/wavesplatform/wavesj/PublicKeyAccount.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import java.nio.ByteBuffer;
import java.util.Arrays;

import static com.wavesplatform.wavesj.Hash.secureHash;

public class PublicKeyAccount implements Account {

private final byte chainId;
Expand Down Expand Up @@ -35,9 +33,9 @@ public final byte getChainId() {

private static byte[] address(byte[] publicKey, byte chainId) {
ByteBuffer buf = ByteBuffer.allocate(26);
byte[] hash = secureHash(publicKey, 0, publicKey.length);
byte[] hash = Hash.secureHash(publicKey, 0, publicKey.length);
buf.put((byte) 1).put((byte) chainId).put(hash, 0, 20);
byte[] checksum = secureHash(buf.array(), 0, 22);
byte[] checksum = Hash.secureHash(buf.array(), 0, 22);
buf.put(checksum, 0, 4);
return buf.array();
}
Expand Down
Loading