Browse Source

Fixed compilation errors in sdk and ledger-model ;

tags/1.0.0
huanghaiquan 6 years ago
parent
commit
7780fb64a4
32 changed files with 470 additions and 443 deletions
  1. +0
    -3
      source/contract/pom.xml
  2. +0
    -2
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java
  3. +3
    -0
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java
  4. +57
    -0
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoServiceProviders.java
  5. +11
    -3
      source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/asymmetric/CryptoKeyPair.java
  6. +8
    -19
      source/ledger/ledger-model/pom.xml
  7. +12
    -3
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/BlockchainKeyGenerator.java
  8. +1
    -1
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/DigitalSignatureBlob.java
  9. +2
    -3
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/PreparedTx.java
  10. +0
    -50
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/SignatureUtils.java
  11. +4
    -5
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/TxBuilder.java
  12. +8
    -5
      source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/TxRequestBuilder.java
  13. +15
    -11
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/ContractCodeDeployOpTemplateTest.java
  14. +13
    -10
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/DataAccountRegisterOpTemplateTest.java
  15. +8
    -2
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/DigitalSignatureBlobTest.java
  16. +11
    -8
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/ED25519SignatureTest.java
  17. +32
    -19
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/TxContentBlobTest.java
  18. +23
    -20
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/TxRequestMessageTest.java
  19. +38
    -35
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/TxResponseMessageTest.java
  20. +13
    -12
      source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/UserRegisterOpTemplateTest.java
  21. +8
    -4
      source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/service/NodeSigningAppender.java
  22. +3
    -11
      source/sdk/sdk-client/src/main/java/com/jd/blockchain/sdk/client/GatewayServiceFactory.java
  23. +3
    -7
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_Contract.java
  24. +4
    -7
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_DataAccount.java
  25. +3
    -6
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_InsertData.java
  26. +3
    -4
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_Query.java
  27. +3
    -7
      source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_User.java
  28. +7
    -8
      source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_BatchInsertData_Test_.java
  29. +8
    -9
      source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_DataAccount_Test_.java
  30. +9
    -9
      source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_InsertData_Test_.java
  31. +154
    -152
      source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Query_Test_.java
  32. +6
    -8
      source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_User_Test_.java

+ 0
- 3
source/contract/pom.xml View File

@@ -14,9 +14,6 @@
<module>contract-framework</module> <module>contract-framework</module>
<module>contract-jvm</module> <module>contract-jvm</module>
<module>contract-maven-plugin</module> <module>contract-maven-plugin</module>
<module>contract-compile</module>
</modules> </modules>
</project> </project>

+ 0
- 2
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/BaseCryptoBytes.java View File

@@ -49,12 +49,10 @@ public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes {


@Override @Override
public CryptoAlgorithm getAlgorithm() { public CryptoAlgorithm getAlgorithm() {
// return resolveAlgorithm(encodedBytes);
return algorithm; return algorithm;
} }


protected BytesSlice getRawCryptoBytes() { protected BytesSlice getRawCryptoBytes() {
// return resolveRawCryptoBytes(encodedBytes);
return new BytesSlice(getDirectBytes(), 2); return new BytesSlice(getDirectBytes(), 2);
} }
} }

+ 3
- 0
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoAlgorithm.java View File

@@ -181,4 +181,7 @@ public interface CryptoAlgorithm {
return isEncryptionAlgorithm(algorithm) && hasAsymmetricKey(algorithm); return isEncryptionAlgorithm(algorithm) && hasAsymmetricKey(algorithm);
} }


static boolean equals(CryptoAlgorithm algorithm1, CryptoAlgorithm algorithm2) {
return algorithm1.code() == algorithm2.code();
}
} }

+ 57
- 0
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/CryptoServiceProviders.java View File

@@ -129,6 +129,15 @@ public final class CryptoServiceProviders {
Short code = names.get(name.toUpperCase()); Short code = names.get(name.toUpperCase());
return code == null ? null : algorithms.get(code); return code == null ? null : algorithms.get(code);
} }
public static RandomFunction getRandomFunction(String algorithmName) {
CryptoAlgorithm algorithm = getAlgorithm(algorithmName);
if (algorithm == null) {
throw new CryptoException(
"Algorithm " + algorithmName + " has no service provider!");
}
return getRandomFunction(algorithm);
}


public static RandomFunction getRandomFunction(CryptoAlgorithm algorithm) { public static RandomFunction getRandomFunction(CryptoAlgorithm algorithm) {
if (!CryptoAlgorithm.isRandomAlgorithm(algorithm)) { if (!CryptoAlgorithm.isRandomAlgorithm(algorithm)) {
@@ -143,6 +152,15 @@ public final class CryptoServiceProviders {


return (RandomFunction) func; return (RandomFunction) func;
} }
public static HashFunction getHashFunction(String algorithmName) {
CryptoAlgorithm algorithm = getAlgorithm(algorithmName);
if (algorithm == null) {
throw new CryptoException(
"Algorithm " + algorithmName + " has no service provider!");
}
return getHashFunction(algorithm);
}


public static HashFunction getHashFunction(CryptoAlgorithm algorithm) { public static HashFunction getHashFunction(CryptoAlgorithm algorithm) {
if (!CryptoAlgorithm.isHashAlgorithm(algorithm)) { if (!CryptoAlgorithm.isHashAlgorithm(algorithm)) {
@@ -157,6 +175,15 @@ public final class CryptoServiceProviders {


return (HashFunction) func; return (HashFunction) func;
} }
public static AsymmetricEncryptionFunction getAsymmetricEncryptionFunction(String algorithmName) {
CryptoAlgorithm algorithm = getAlgorithm(algorithmName);
if (algorithm == null) {
throw new CryptoException(
"Algorithm " + algorithmName + " has no service provider!");
}
return getAsymmetricEncryptionFunction(algorithm);
}


public static AsymmetricEncryptionFunction getAsymmetricEncryptionFunction(CryptoAlgorithm algorithm) { public static AsymmetricEncryptionFunction getAsymmetricEncryptionFunction(CryptoAlgorithm algorithm) {
if (!CryptoAlgorithm.isAsymmetricEncryptionAlgorithm(algorithm)) { if (!CryptoAlgorithm.isAsymmetricEncryptionAlgorithm(algorithm)) {
@@ -171,6 +198,16 @@ public final class CryptoServiceProviders {


return (AsymmetricEncryptionFunction) func; return (AsymmetricEncryptionFunction) func;
} }

public static SignatureFunction getSignatureFunction(String algorithmName) {
CryptoAlgorithm algorithm = getAlgorithm(algorithmName);
if (algorithm == null) {
throw new CryptoException(
"Algorithm " + algorithmName + " has no service provider!");
}
return getSignatureFunction(algorithm);
}


public static SignatureFunction getSignatureFunction(CryptoAlgorithm algorithm) { public static SignatureFunction getSignatureFunction(CryptoAlgorithm algorithm) {
if (!CryptoAlgorithm.isSignatureAlgorithm(algorithm)) { if (!CryptoAlgorithm.isSignatureAlgorithm(algorithm)) {
@@ -185,6 +222,16 @@ public final class CryptoServiceProviders {


return (SignatureFunction) func; return (SignatureFunction) func;
} }

public static SymmetricEncryptionFunction getSymmetricEncryptionFunction(String algorithmName) {
CryptoAlgorithm algorithm = getAlgorithm(algorithmName);
if (algorithm == null) {
throw new CryptoException(
"Algorithm " + algorithmName + " has no service provider!");
}
return getSymmetricEncryptionFunction(algorithm);
}


public static SymmetricEncryptionFunction getSymmetricEncryptionFunction(CryptoAlgorithm algorithm) { public static SymmetricEncryptionFunction getSymmetricEncryptionFunction(CryptoAlgorithm algorithm) {
if (!CryptoAlgorithm.isSymmetricEncryptionAlgorithm(algorithm)) { if (!CryptoAlgorithm.isSymmetricEncryptionAlgorithm(algorithm)) {
@@ -199,6 +246,16 @@ public final class CryptoServiceProviders {


return (SymmetricEncryptionFunction) func; return (SymmetricEncryptionFunction) func;
} }

public static CryptoFunction getCryptoFunction(String algorithmName) {
CryptoAlgorithm algorithm = getAlgorithm(algorithmName);
if (algorithm == null) {
throw new CryptoException(
"Algorithm " + algorithmName + " has no service provider!");
}
return getCryptoFunction(algorithm);
}


public static CryptoFunction getCryptoFunction(CryptoAlgorithm algorithm) { public static CryptoFunction getCryptoFunction(CryptoAlgorithm algorithm) {
CryptoFunction func = functions.get(algorithm.code()); CryptoFunction func = functions.get(algorithm.code());


+ 11
- 3
source/crypto/crypto-framework/src/main/java/com/jd/blockchain/crypto/asymmetric/CryptoKeyPair.java View File

@@ -1,14 +1,19 @@
package com.jd.blockchain.crypto.asymmetric; package com.jd.blockchain.crypto.asymmetric;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.PrivKey; import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.PubKey;


public class CryptoKeyPair { public class CryptoKeyPair {
private PubKey pubKey; private PubKey pubKey;
private PrivKey privKey; private PrivKey privKey;


public CryptoAlgorithm getAlgorithm() {
return pubKey.getAlgorithm();
}

public PubKey getPubKey() { public PubKey getPubKey() {
return pubKey; return pubKey;
} }
@@ -18,8 +23,11 @@ public class CryptoKeyPair {
} }


public CryptoKeyPair(PubKey pubKey, PrivKey privKey) { public CryptoKeyPair(PubKey pubKey, PrivKey privKey) {
if (!CryptoAlgorithm.equals(pubKey.getAlgorithm(), privKey.getAlgorithm())) {
throw new IllegalArgumentException("The algorithms of PubKey and PrivKey don't match!");
}
this.pubKey = pubKey; this.pubKey = pubKey;
this.privKey = privKey; this.privKey = privKey;
} }
} }

+ 8
- 19
source/ledger/ledger-model/pom.xml View File

@@ -11,46 +11,35 @@
<artifactId>ledger-model</artifactId> <artifactId>ledger-model</artifactId>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.jd.blockchain</groupId> <groupId>com.jd.blockchain</groupId>
<artifactId>binary-proto</artifactId>
<artifactId>utils-common</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jd.blockchain</groupId> <groupId>com.jd.blockchain</groupId>
<artifactId>crypto-framework</artifactId>
<artifactId>utils-web</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jd.blockchain</groupId> <groupId>com.jd.blockchain</groupId>
<artifactId>crypto-impl</artifactId>
<artifactId>binary-proto</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<scope>test</scope>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jd.blockchain</groupId> <groupId>com.jd.blockchain</groupId>
<artifactId>utils-common</artifactId>
<artifactId>crypto-framework</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.jd.blockchain</groupId> <groupId>com.jd.blockchain</groupId>
<artifactId>utils-web</artifactId>
<artifactId>crypto-classic</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
<scope>test</scope>
</dependency> </dependency>
<!-- <dependency> <groupId>net.i2p.crypto</groupId> <artifactId>eddsa</artifactId>
<version>0.1.0</version> </dependency> -->
<!-- <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId>
<version>1.9.0</version> </dependency> <dependency> <groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId> <version>1.9.0</version>$NO-MVN-MAN-VER$
</dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId>
<version>1.9.0</version> </dependency> -->
</dependencies> </dependencies>
</project> </project>

+ 12
- 3
source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/BlockchainKeyGenerator.java View File

@@ -1,8 +1,9 @@
package com.jd.blockchain.ledger; package com.jd.blockchain.ledger;
import com.jd.blockchain.crypto.CryptoAlgorithm; import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
/** /**
* 区块链密钥生成器; * 区块链密钥生成器;
@@ -11,6 +12,8 @@ import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
* *
*/ */
public class BlockchainKeyGenerator { public class BlockchainKeyGenerator {
public static final String DEFAULT_ALGORITHM = "ED25519";
private BlockchainKeyGenerator() { private BlockchainKeyGenerator() {
} }
@@ -20,11 +23,17 @@ public class BlockchainKeyGenerator {
} }
public BlockchainKeyPair generate() { public BlockchainKeyPair generate() {
return generate(CryptoAlgorithm.ED25519);
return generate(DEFAULT_ALGORITHM);
} }
public BlockchainKeyPair generate(String algorithmName) {
CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm(algorithmName);
return generate(algorithm);
}
public BlockchainKeyPair generate(CryptoAlgorithm signatureAlgorithm) { public BlockchainKeyPair generate(CryptoAlgorithm signatureAlgorithm) {
CryptoKeyPair cryptoKeyPair = CryptoUtils.sign(signatureAlgorithm).generateKeyPair();
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction(signatureAlgorithm);
CryptoKeyPair cryptoKeyPair = signFunc.generateKeyPair();
return new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); return new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
} }


+ 1
- 1
source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/DigitalSignatureBlob.java View File

@@ -24,7 +24,7 @@ import com.jd.blockchain.ledger.MagicNumber;
* @author huanghaiquan * @author huanghaiquan
* *
*/ */
public class DigitalSignatureBlob implements DigitalSignature { //, Externalizable
public class DigitalSignatureBlob implements DigitalSignature {
private PubKey pubKey; private PubKey pubKey;


+ 2
- 3
source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/PreparedTx.java View File

@@ -1,7 +1,7 @@
package com.jd.blockchain.ledger.data; package com.jd.blockchain.ledger.data;


import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PrivKey; import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
@@ -37,8 +37,7 @@ public class PreparedTx implements PreparedTransaction {


@Override @Override
public DigitalSignature sign(CryptoKeyPair keyPair) { public DigitalSignature sign(CryptoKeyPair keyPair) {
// SignatureFunction signatureFunction = new ED25519SignatureFunction();
SignatureFunction signatureFunction = CryptoUtils.sign(keyPair.getPubKey().getAlgorithm());
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(keyPair.getAlgorithm());
PrivKey privKey = keyPair.getPrivKey(); PrivKey privKey = keyPair.getPrivKey();
byte[] content = BinaryEncodingUtils.encode(getTransactionContent(), TransactionContent.class); byte[] content = BinaryEncodingUtils.encode(getTransactionContent(), TransactionContent.class);
SignatureDigest signatureDigest = signatureFunction.sign(privKey, content); SignatureDigest signatureDigest = signatureFunction.sign(privKey, content);


+ 0
- 50
source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/SignatureUtils.java View File

@@ -1,50 +0,0 @@
package com.jd.blockchain.ledger.data;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.DigitalSignature;
import com.jd.blockchain.utils.io.ByteArray;
import com.jd.blockchain.utils.security.Ed25519Utils;
public class SignatureUtils {
public static DigitalSignature sign(ByteArray data, BlockchainKeyPair keyPair) {
return sign(data.bytes(), keyPair);
}
public static DigitalSignature sign(byte[] data, BlockchainKeyPair keyPair) {
// 对交易内容的hash进行签名;
CryptoAlgorithm algorithm = keyPair.getPrivKey().getAlgorithm();
switch (algorithm) {
case ED25519:
byte[] digest = Ed25519Utils.sign_512(data, keyPair.getPrivKey().getRawKeyBytes());
DigitalSignatureBlob signature = new DigitalSignatureBlob(keyPair.getPubKey(), new SignatureDigest(digest));
return signature;
case SM2:
throw new IllegalArgumentException("Unsupported KeyType[" + algorithm + "]!");
// case CA:
// throw new IllegalArgumentException("Unsupported KeyType[" + keyType + "]!");
default:
throw new IllegalArgumentException("Unsupported KeyType[" + algorithm + "]!");
}
}
public static boolean verify(ByteArray data, DigitalSignature signature) {
return verify(data.bytes(), signature);
}
public static boolean verify(byte[] data, DigitalSignature signature) {
CryptoAlgorithm algorithm = signature.getPubKey().getAlgorithm();
switch (algorithm) {
case ED25519:
return Ed25519Utils.verify(data, signature.getPubKey().getRawKeyBytes(), signature.getDigest().toBytes());
case SM2:
throw new IllegalArgumentException("Unsupported KeyType[" + algorithm + "]!");
// case CA:
// throw new IllegalArgumentException("Unsupported KeyType[" + keyType + "]!");
default:
throw new IllegalArgumentException("Unsupported KeyType[" + algorithm + "]!");
}
}
}

+ 4
- 5
source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/TxBuilder.java View File

@@ -2,8 +2,7 @@ package com.jd.blockchain.ledger.data;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.ledger.TransactionBuilder; import com.jd.blockchain.ledger.TransactionBuilder;
import com.jd.blockchain.ledger.TransactionContent; import com.jd.blockchain.ledger.TransactionContent;
@@ -19,8 +18,8 @@ public class TxBuilder implements TransactionBuilder {
private BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); private BlockchainOperationFactory opFactory = new BlockchainOperationFactory();
private CryptoAlgorithm defaultHashAlgorithm = CryptoAlgorithm.SHA256;
private static final String DEFAULT_HASH_ALGORITHM = "SHA256";
private HashDigest ledgerHash; private HashDigest ledgerHash;
public TxBuilder(HashDigest ledgerHash) { public TxBuilder(HashDigest ledgerHash) {
@@ -44,7 +43,7 @@ public class TxBuilder implements TransactionBuilder {
txContent.addOperations(opFactory.getOperations()); txContent.addOperations(opFactory.getOperations());
byte[] contentBodyBytes = BinaryEncodingUtils.encode(txContent, TransactionContentBody.class); byte[] contentBodyBytes = BinaryEncodingUtils.encode(txContent, TransactionContentBody.class);
HashDigest contentHash = CryptoUtils.hash(defaultHashAlgorithm).hash(contentBodyBytes);
HashDigest contentHash = CryptoServiceProviders.getHashFunction(DEFAULT_HASH_ALGORITHM).hash(contentBodyBytes);
txContent.setHash(contentHash); txContent.setHash(contentHash);
return txContent; return txContent;


+ 8
- 5
source/ledger/ledger-model/src/main/java/com/jd/blockchain/ledger/data/TxRequestBuilder.java View File

@@ -4,8 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PrivKey; import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
@@ -19,6 +18,8 @@ import com.jd.blockchain.ledger.TransactionRequestBuilder;
public class TxRequestBuilder implements TransactionRequestBuilder { public class TxRequestBuilder implements TransactionRequestBuilder {
private static final String DEFAULT_HASH_ALGORITHM = "SHA256";
private TransactionContent txContent; private TransactionContent txContent;
private List<DigitalSignature> endpointSignatures = new ArrayList<>(); private List<DigitalSignature> endpointSignatures = new ArrayList<>();
@@ -70,11 +71,13 @@ public class TxRequestBuilder implements TransactionRequestBuilder {
} }
public static SignatureDigest sign(TransactionContent txContent, PrivKey privKey) { public static SignatureDigest sign(TransactionContent txContent, PrivKey privKey) {
return CryptoUtils.sign(privKey.getAlgorithm()).sign(privKey, txContent.getHash().toBytes());
return CryptoServiceProviders.getSignatureFunction(privKey.getAlgorithm()).sign(privKey,
txContent.getHash().toBytes());
} }
public static boolean verifySignature(TransactionContent txContent, SignatureDigest signDigest, PubKey pubKey) { public static boolean verifySignature(TransactionContent txContent, SignatureDigest signDigest, PubKey pubKey) {
return CryptoUtils.sign(signDigest.getAlgorithm()).verify(signDigest, pubKey, txContent.getHash().toBytes());
return CryptoServiceProviders.getSignatureFunction(pubKey.getAlgorithm()).verify(signDigest, pubKey,
txContent.getHash().toBytes());
} }
@Override @Override
@@ -84,7 +87,7 @@ public class TxRequestBuilder implements TransactionRequestBuilder {
txMessage.addNodeSignatures(nodeSignatures); txMessage.addNodeSignatures(nodeSignatures);
byte[] reqBytes = BinaryEncodingUtils.encode(txMessage, NodeRequest.class); byte[] reqBytes = BinaryEncodingUtils.encode(txMessage, NodeRequest.class);
HashDigest reqHash = CryptoUtils.hash(CryptoAlgorithm.SHA256).hash(reqBytes);
HashDigest reqHash = CryptoServiceProviders.getHashFunction(DEFAULT_HASH_ALGORITHM).hash(reqBytes);
txMessage.setHash(reqHash); txMessage.setHash(reqHash);
return txMessage; return txMessage;


+ 15
- 11
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/ContractCodeDeployOpTemplateTest.java View File

@@ -8,21 +8,25 @@
*/ */
package test.com.jd.blockchain.ledger.data; package test.com.jd.blockchain.ledger.data;


import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import org.junit.Before;
import org.junit.Test;

import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.ledger.*;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.ledger.BlockchainIdentity;
import com.jd.blockchain.ledger.BlockchainIdentityData;
import com.jd.blockchain.ledger.ContractCodeDeployOperation;
import com.jd.blockchain.ledger.Operation;
import com.jd.blockchain.ledger.data.ContractCodeDeployOpTemplate; import com.jd.blockchain.ledger.data.ContractCodeDeployOpTemplate;
import com.jd.blockchain.utils.io.BytesUtils; import com.jd.blockchain.utils.io.BytesUtils;


import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/** /**
* *
* @author shaozhuguang * @author shaozhuguang
@@ -38,8 +42,8 @@ public class ContractCodeDeployOpTemplateTest {
public void initContractCodeDeployOpTemplate() { public void initContractCodeDeployOpTemplate() {
DataContractRegistry.register(ContractCodeDeployOperation.class); DataContractRegistry.register(ContractCodeDeployOperation.class);
DataContractRegistry.register(Operation.class); DataContractRegistry.register(Operation.class);
String pubKeyVal = "jd.com";
PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, pubKeyVal.getBytes());
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519");
PubKey pubKey = signFunc.generateKeyPair().getPubKey();
BlockchainIdentity contractID = new BlockchainIdentityData(pubKey); BlockchainIdentity contractID = new BlockchainIdentityData(pubKey);
byte[] chainCode = "jd-test".getBytes(); byte[] chainCode = "jd-test".getBytes();
data = new ContractCodeDeployOpTemplate(contractID, chainCode); data = new ContractCodeDeployOpTemplate(contractID, chainCode);


+ 13
- 10
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/DataAccountRegisterOpTemplateTest.java View File

@@ -8,18 +8,21 @@
*/ */
package test.com.jd.blockchain.ledger.data; package test.com.jd.blockchain.ledger.data;


import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.ledger.*;
import com.jd.blockchain.ledger.data.DataAccountRegisterOpTemplate;
import com.jd.blockchain.utils.io.ByteArray;
import static org.junit.Assert.assertEquals;


import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import static org.junit.Assert.assertEquals;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.ledger.BlockchainIdentity;
import com.jd.blockchain.ledger.BlockchainIdentityData;
import com.jd.blockchain.ledger.DataAccountRegisterOperation;
import com.jd.blockchain.ledger.Operation;
import com.jd.blockchain.ledger.data.DataAccountRegisterOpTemplate;


/** /**
* *
@@ -36,8 +39,8 @@ public class DataAccountRegisterOpTemplateTest {
public void initDataAccountRegisterOpTemplate() { public void initDataAccountRegisterOpTemplate() {
DataContractRegistry.register(DataAccountRegisterOperation.class); DataContractRegistry.register(DataAccountRegisterOperation.class);
DataContractRegistry.register(Operation.class); DataContractRegistry.register(Operation.class);
String pubKeyVal = "jd.com";
PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, pubKeyVal.getBytes());
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519");
PubKey pubKey = signFunc.generateKeyPair().getPubKey();
BlockchainIdentity contractID = new BlockchainIdentityData(pubKey); BlockchainIdentity contractID = new BlockchainIdentityData(pubKey);
data = new DataAccountRegisterOpTemplate(contractID); data = new DataAccountRegisterOpTemplate(contractID);




+ 8
- 2
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/DigitalSignatureBlobTest.java View File

@@ -16,8 +16,11 @@ import org.junit.Test;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm; import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.ledger.DigitalSignature; import com.jd.blockchain.ledger.DigitalSignature;
import com.jd.blockchain.ledger.DigitalSignatureBody; import com.jd.blockchain.ledger.DigitalSignatureBody;
import com.jd.blockchain.ledger.data.DigitalSignatureBlob; import com.jd.blockchain.ledger.data.DigitalSignatureBlob;
@@ -37,8 +40,11 @@ public class DigitalSignatureBlobTest {
public void initDigitalSignatureBlob() throws Exception { public void initDigitalSignatureBlob() throws Exception {
DataContractRegistry.register(DigitalSignature.class); DataContractRegistry.register(DigitalSignature.class);
DataContractRegistry.register(DigitalSignatureBody.class); DataContractRegistry.register(DigitalSignatureBody.class);
PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, "jd.com".getBytes());
SignatureDigest digest = new SignatureDigest(CryptoAlgorithm.ED25519, "zhangsan".getBytes());
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519");
CryptoKeyPair kp = signFunc.generateKeyPair();
PubKey pubKey = kp.getPubKey();
SignatureDigest digest = signFunc.sign(kp.getPrivKey(), "zhangsan".getBytes());
data = new DigitalSignatureBlob(pubKey, digest); data = new DigitalSignatureBlob(pubKey, digest);
} }




+ 11
- 8
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/ED25519SignatureTest.java View File

@@ -2,25 +2,28 @@ package test.com.jd.blockchain.ledger.data;


import java.util.Random; import java.util.Random;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair;
import org.junit.Test;

import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.utils.security.Ed25519Utils; import com.jd.blockchain.utils.security.Ed25519Utils;


public class ED25519SignatureTest { public class ED25519SignatureTest {


public static void main(String[] args) {
@Test
public void perfomanceTest() {
Random rand = new Random(); Random rand = new Random();
byte[] data = new byte[64]; byte[] data = new byte[64];
rand.nextBytes(data); rand.nextBytes(data);


BlockchainKeyPair key = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519");
CryptoKeyPair key = signFunc.generateKeyPair();
byte[] pubKey = key.getPubKey().getRawKeyBytes(); byte[] pubKey = key.getPubKey().getRawKeyBytes();
byte[] privKey = key.getPrivKey().getRawKeyBytes(); byte[] privKey = key.getPrivKey().getRawKeyBytes();


int count = 10000; int count = 10000;
System.out.println("=================== do sign test ==================="); System.out.println("=================== do sign test ===================");
byte[] sign = null; byte[] sign = null;
for (int r = 0; r < 5; r++) { for (int r = 0; r < 5; r++) {
@@ -33,7 +36,7 @@ public class ED25519SignatureTest {
System.out.println(String.format("Siging Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS, System.out.println(String.format("Siging Count=%s; Elapsed Times=%s; TPS=%.2f", count, elapsedTS,
(count * 1000.00D) / elapsedTS)); (count * 1000.00D) / elapsedTS));
} }
System.out.println("=================== do verify test ==================="); System.out.println("=================== do verify test ===================");
for (int r = 0; r < 5; r++) { for (int r = 0; r < 5; r++) {
System.out.println("------------- round[" + r + "] --------------"); System.out.println("------------- round[" + r + "] --------------");


+ 32
- 19
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/TxContentBlobTest.java View File

@@ -6,15 +6,20 @@ import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.util.UUID; import java.util.UUID;
import com.jd.blockchain.ledger.*;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.DataAccountKVSetOperation;
import com.jd.blockchain.ledger.HashObject;
import com.jd.blockchain.ledger.Operation;
import com.jd.blockchain.ledger.TransactionContent;
import com.jd.blockchain.ledger.TransactionContentBody;
import com.jd.blockchain.ledger.data.BlockchainOperationFactory; import com.jd.blockchain.ledger.data.BlockchainOperationFactory;
import com.jd.blockchain.ledger.data.TxContentBlob; import com.jd.blockchain.ledger.data.TxContentBlob;
import com.jd.blockchain.utils.io.ByteArray; import com.jd.blockchain.utils.io.ByteArray;
@@ -29,32 +34,36 @@ public class TxContentBlobTest {
DataContractRegistry.register(TransactionContent.class); DataContractRegistry.register(TransactionContent.class);
DataContractRegistry.register(HashObject.class); DataContractRegistry.register(HashObject.class);
BlockchainKeyPair id = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
BlockchainKeyPair id = BlockchainKeyGenerator.getInstance().generate("ED25519");
HashDigest ledgerHash = CryptoUtils.hash(CryptoAlgorithm.SHA256).hash(UUID.randomUUID().toString().getBytes("UTF-8"));
HashDigest ledgerHash = CryptoServiceProviders.getHashFunction("SHA256")
.hash(UUID.randomUUID().toString().getBytes("UTF-8"));
BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); BlockchainOperationFactory opFactory = new BlockchainOperationFactory();
contentBlob = new TxContentBlob(ledgerHash); contentBlob = new TxContentBlob(ledgerHash);
contentBlob.setHash(new HashDigest(CryptoAlgorithm.SHA256, "jd.com".getBytes()));
HashDigest contentHash = CryptoServiceProviders.getHashFunction("SHA256")
.hash("jd.com".getBytes());
contentBlob.setHash(contentHash);
// contentBlob.setSubjectAccount(id.getAddress());
// contentBlob.setSequenceNumber(1);
DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress()).set("Name", ByteArray.fromString("AAA", "UTF-8"), -1).getOperation();
DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress())
.set("Name", ByteArray.fromString("AAA", "UTF-8"), -1).getOperation();
contentBlob.addOperation(kvsetOP); contentBlob.addOperation(kvsetOP);
} }
@Test @Test
public void testSerialize_TransactionContentBody() throws IOException,InstantiationException,IllegalAccessException{
public void testSerialize_TransactionContentBody()
throws IOException, InstantiationException, IllegalAccessException {
byte[] bytesContent = BinaryEncodingUtils.encode(contentBlob, TransactionContentBody.class); byte[] bytesContent = BinaryEncodingUtils.encode(contentBlob, TransactionContentBody.class);
TransactionContentBody resolvedContentBlob = BinaryEncodingUtils.decode(bytesContent); TransactionContentBody resolvedContentBlob = BinaryEncodingUtils.decode(bytesContent);
assertEquals(contentBlob.getLedgerHash(), resolvedContentBlob.getLedgerHash()); assertEquals(contentBlob.getLedgerHash(), resolvedContentBlob.getLedgerHash());
// assertEquals(contentBlob.getSubjectAccount(), resolvedContentBlob.getSubjectAccount());
// assertEquals(contentBlob.getSequenceNumber(), resolvedContentBlob.getSequenceNumber());
// assertEquals(contentBlob.getSubjectAccount(),
// resolvedContentBlob.getSubjectAccount());
// assertEquals(contentBlob.getSequenceNumber(),
// resolvedContentBlob.getSequenceNumber());
assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length); assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length);
assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length); assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length);
@@ -69,20 +78,23 @@ public class TxContentBlobTest {
for (int j = 0; j < dataKv.length; j++) { for (int j = 0; j < dataKv.length; j++) {
assertEquals(dataKv[i].getKey(), resolvedKv[i].getKey()); assertEquals(dataKv[i].getKey(), resolvedKv[i].getKey());
assertEquals(dataKv[i].getExpectedVersion(), resolvedKv[i].getExpectedVersion()); assertEquals(dataKv[i].getExpectedVersion(), resolvedKv[i].getExpectedVersion());
assertArrayEquals(dataKv[i].getValue().getValue().toBytes(), resolvedKv[i].getValue().getValue().toBytes());
assertArrayEquals(dataKv[i].getValue().getValue().toBytes(),
resolvedKv[i].getValue().getValue().toBytes());
} }
} }
} }
@Test @Test
public void testSerialize_TransactionContent() throws IOException,InstantiationException,IllegalAccessException{
public void testSerialize_TransactionContent() throws IOException, InstantiationException, IllegalAccessException {
byte[] bytesContent = BinaryEncodingUtils.encode(contentBlob, TransactionContent.class); byte[] bytesContent = BinaryEncodingUtils.encode(contentBlob, TransactionContent.class);
TransactionContentBody resolvedContentBlob = BinaryEncodingUtils.decode(bytesContent); TransactionContentBody resolvedContentBlob = BinaryEncodingUtils.decode(bytesContent);
assertEquals(contentBlob.getLedgerHash(), resolvedContentBlob.getLedgerHash()); assertEquals(contentBlob.getLedgerHash(), resolvedContentBlob.getLedgerHash());
// assertEquals(contentBlob.getSubjectAccount(), resolvedContentBlob.getSubjectAccount());
// assertEquals(contentBlob.getSequenceNumber(), resolvedContentBlob.getSequenceNumber());
// assertEquals(contentBlob.getSubjectAccount(),
// resolvedContentBlob.getSubjectAccount());
// assertEquals(contentBlob.getSequenceNumber(),
// resolvedContentBlob.getSequenceNumber());
assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length); assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length);
assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length); assertEquals(contentBlob.getOperations().length, resolvedContentBlob.getOperations().length);
@@ -97,7 +109,8 @@ public class TxContentBlobTest {
for (int j = 0; j < dataKv.length; j++) { for (int j = 0; j < dataKv.length; j++) {
assertEquals(dataKv[i].getKey(), resolvedKv[i].getKey()); assertEquals(dataKv[i].getKey(), resolvedKv[i].getKey());
assertEquals(dataKv[i].getExpectedVersion(), resolvedKv[i].getExpectedVersion()); assertEquals(dataKv[i].getExpectedVersion(), resolvedKv[i].getExpectedVersion());
assertArrayEquals(dataKv[i].getValue().getValue().toBytes(), resolvedKv[i].getValue().getValue().toBytes());
assertArrayEquals(dataKv[i].getValue().getValue().toBytes(),
resolvedKv[i].getValue().getValue().toBytes());
} }
} }
} }


+ 23
- 20
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/TxRequestMessageTest.java View File

@@ -17,11 +17,12 @@ import org.junit.Test;


import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair; import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.DataAccountKVSetOperation; import com.jd.blockchain.ledger.DataAccountKVSetOperation;
@@ -58,25 +59,27 @@ public class TxRequestMessageTest {


data = new TxRequestMessage(initTransactionContent()); data = new TxRequestMessage(initTransactionContent());


SignatureDigest digest1 = new SignatureDigest(CryptoAlgorithm.ED25519, "zhangsan".getBytes());
SignatureDigest digest2 = new SignatureDigest(CryptoAlgorithm.ED25519, "lisi".getBytes());
DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob(
new PubKey(CryptoAlgorithm.ED25519, "jd1.com".getBytes()), digest1);
DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob(
new PubKey(CryptoAlgorithm.ED25519, "jd2.com".getBytes()), digest2);
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519");
CryptoKeyPair key1 = signFunc.generateKeyPair();
CryptoKeyPair key2 = signFunc.generateKeyPair();
CryptoKeyPair key3 = signFunc.generateKeyPair();
CryptoKeyPair key4 = signFunc.generateKeyPair();

SignatureDigest digest1 = signFunc.sign(key1.getPrivKey(), "zhangsan".getBytes());
SignatureDigest digest2 = signFunc.sign(key2.getPrivKey(), "lisi".getBytes());
DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob(key1.getPubKey(), digest1);
DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob(key2.getPubKey(), digest2);
data.addEndpointSignatures(endPoint1); data.addEndpointSignatures(endPoint1);
data.addEndpointSignatures(endPoint2); data.addEndpointSignatures(endPoint2);


SignatureDigest digest3 = new SignatureDigest(CryptoAlgorithm.ED25519, "wangwu".getBytes());
SignatureDigest digest4 = new SignatureDigest(CryptoAlgorithm.ED25519, "zhaoliu".getBytes());
DigitalSignatureBlob node1 = new DigitalSignatureBlob(new PubKey(CryptoAlgorithm.ED25519, "jd3.com".getBytes()),
digest3);
DigitalSignatureBlob node2 = new DigitalSignatureBlob(new PubKey(CryptoAlgorithm.ED25519, "jd4.com".getBytes()),
digest4);
SignatureDigest digest3 = signFunc.sign(key3.getPrivKey(), "wangwu".getBytes());
SignatureDigest digest4 = signFunc.sign(key4.getPrivKey(), "zhaoliu".getBytes());
DigitalSignatureBlob node1 = new DigitalSignatureBlob(key3.getPubKey(), digest3);
DigitalSignatureBlob node2 = new DigitalSignatureBlob(key4.getPubKey(), digest4);
data.addNodeSignatures(node1); data.addNodeSignatures(node1);
data.addNodeSignatures(node2); data.addNodeSignatures(node2);


HashDigest hash = new HashDigest(CryptoAlgorithm.SHA256, "sunqi".getBytes());
HashDigest hash = CryptoServiceProviders.getHashFunction("SHA256").hash("DATA".getBytes());
data.setHash(hash); data.setHash(hash);
} }


@@ -192,12 +195,12 @@ public class TxRequestMessageTest {


private TransactionContent initTransactionContent() throws Exception { private TransactionContent initTransactionContent() throws Exception {
TxContentBlob contentBlob = null; TxContentBlob contentBlob = null;
BlockchainKeyPair id = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
HashDigest ledgerHash = CryptoUtils.hash(CryptoAlgorithm.SHA256)
.hash(UUID.randomUUID().toString().getBytes("UTF-8"));
BlockchainKeyPair id = BlockchainKeyGenerator.getInstance().generate("ED25519");
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");
HashDigest ledgerHash = hashFunc.hash(UUID.randomUUID().toString().getBytes("UTF-8"));
BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); BlockchainOperationFactory opFactory = new BlockchainOperationFactory();
contentBlob = new TxContentBlob(ledgerHash); contentBlob = new TxContentBlob(ledgerHash);
contentBlob.setHash(new HashDigest(CryptoAlgorithm.SHA256, "jd.com".getBytes()));
contentBlob.setHash(hashFunc.hash("jd.com".getBytes()));
// contentBlob.setSubjectAccount(id.getAddress()); // contentBlob.setSubjectAccount(id.getAddress());
// contentBlob.setSequenceNumber(1); // contentBlob.setSequenceNumber(1);
DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress()) DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress())


+ 38
- 35
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/TxResponseMessageTest.java View File

@@ -8,16 +8,19 @@
*/ */
package test.com.jd.blockchain.ledger.data; package test.com.jd.blockchain.ledger.data;


import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.ledger.*;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.TransactionResponse;
import com.jd.blockchain.ledger.TransactionState;
import com.jd.blockchain.ledger.data.TxResponseMessage; import com.jd.blockchain.ledger.data.TxResponseMessage;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;


/** /**
* *
@@ -28,33 +31,33 @@ import static org.junit.Assert.assertEquals;


public class TxResponseMessageTest { public class TxResponseMessageTest {


private TxResponseMessage data;
@Before
public void initTxRequestMessage() throws Exception {
DataContractRegistry.register(TransactionResponse.class);
HashDigest contentHash = new HashDigest(CryptoAlgorithm.SHA256, "jd-content".getBytes());
HashDigest blockHash = new HashDigest(CryptoAlgorithm.SHA256, "jd-block".getBytes());
long blockHeight = 9999L;
data = new TxResponseMessage(contentHash);
data.setBlockHash(blockHash);
data.setBlockHeight(blockHeight);
data.setExecutionState(TransactionState.SUCCESS);
}
@Test
public void testSerialize_TransactionResponse() {
byte[] serialBytes = BinaryEncodingUtils.encode(data, TransactionResponse.class);
TransactionResponse resolvedData = BinaryEncodingUtils.decode(serialBytes);
System.out.println("------Assert start ------");
assertEquals(resolvedData.getBlockHash(), data.getBlockHash());
assertEquals(resolvedData.getBlockHeight(), data.getBlockHeight());
assertEquals(resolvedData.getContentHash(), data.getContentHash());
assertEquals(resolvedData.getExecutionState(), data.getExecutionState());
assertEquals(resolvedData.isSuccess(), data.isSuccess());
System.out.println("------Assert OK ------");
}
private TxResponseMessage data;
@Before
public void initTxRequestMessage() throws Exception {
DataContractRegistry.register(TransactionResponse.class);
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");
HashDigest contentHash = hashFunc.hash("jd-content".getBytes());
HashDigest blockHash = hashFunc.hash("jd-block".getBytes());
long blockHeight = 9999L;
data = new TxResponseMessage(contentHash);
data.setBlockHash(blockHash);
data.setBlockHeight(blockHeight);
data.setExecutionState(TransactionState.SUCCESS);
}
@Test
public void testSerialize_TransactionResponse() {
byte[] serialBytes = BinaryEncodingUtils.encode(data, TransactionResponse.class);
TransactionResponse resolvedData = BinaryEncodingUtils.decode(serialBytes);
System.out.println("------Assert start ------");
assertEquals(resolvedData.getBlockHash(), data.getBlockHash());
assertEquals(resolvedData.getBlockHeight(), data.getBlockHeight());
assertEquals(resolvedData.getContentHash(), data.getContentHash());
assertEquals(resolvedData.getExecutionState(), data.getExecutionState());
assertEquals(resolvedData.isSuccess(), data.isSuccess());
System.out.println("------Assert OK ------");
}
} }

+ 13
- 12
source/ledger/ledger-model/src/test/java/test/com/jd/blockchain/ledger/data/UserRegisterOpTemplateTest.java View File

@@ -8,20 +8,21 @@
*/ */
package test.com.jd.blockchain.ledger.data; package test.com.jd.blockchain.ledger.data;


import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.ledger.*;
import com.jd.blockchain.ledger.data.ContractEventSendOpTemplate;
import com.jd.blockchain.ledger.data.DataAccountRegisterOpTemplate;
import com.jd.blockchain.ledger.data.UserRegisterOpTemplate;
import com.jd.blockchain.utils.io.ByteArray;
import static org.junit.Assert.assertEquals;


import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import static org.junit.Assert.assertEquals;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.ledger.BlockchainIdentity;
import com.jd.blockchain.ledger.BlockchainIdentityData;
import com.jd.blockchain.ledger.Operation;
import com.jd.blockchain.ledger.UserRegisterOperation;
import com.jd.blockchain.ledger.data.UserRegisterOpTemplate;


/** /**
* *
@@ -38,8 +39,8 @@ public class UserRegisterOpTemplateTest {
public void initUserRegisterOpTemplate() { public void initUserRegisterOpTemplate() {
DataContractRegistry.register(UserRegisterOperation.class); DataContractRegistry.register(UserRegisterOperation.class);
DataContractRegistry.register(Operation.class); DataContractRegistry.register(Operation.class);
String pubKeyVal = "jd.com";
PubKey pubKey = new PubKey(CryptoAlgorithm.ED25519, pubKeyVal.getBytes());
CryptoKeyPair key = CryptoServiceProviders.getSignatureFunction("ED25519").generateKeyPair();
PubKey pubKey = key.getPubKey();
BlockchainIdentity contractID = new BlockchainIdentityData(pubKey); BlockchainIdentity contractID = new BlockchainIdentityData(pubKey);
data = new UserRegisterOpTemplate(contractID); data = new UserRegisterOpTemplate(contractID);
} }


+ 8
- 4
source/sdk/sdk-base/src/main/java/com/jd/blockchain/sdk/service/NodeSigningAppender.java View File

@@ -5,10 +5,12 @@ import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.consensus.MessageService; import com.jd.blockchain.consensus.MessageService;
import com.jd.blockchain.consensus.client.ConsensusClient; import com.jd.blockchain.consensus.client.ConsensusClient;
import com.jd.blockchain.crypto.CryptoAlgorithm; import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.NodeRequest; import com.jd.blockchain.ledger.NodeRequest;
import com.jd.blockchain.ledger.TransactionRequest; import com.jd.blockchain.ledger.TransactionRequest;
import com.jd.blockchain.ledger.TransactionResponse; import com.jd.blockchain.ledger.TransactionResponse;
@@ -64,13 +66,15 @@ public class NodeSigningAppender implements TransactionService {
// 生成网关签名; // 生成网关签名;
byte[] endpointRequestBytes = BinaryEncodingUtils.encode(txMessage, TransactionRequest.class); byte[] endpointRequestBytes = BinaryEncodingUtils.encode(txMessage, TransactionRequest.class);


CryptoAlgorithm signAlgorithm = nodeKeyPair.getPrivKey().getAlgorithm();
SignatureDigest signDigest = CryptoUtils.sign(signAlgorithm).sign(nodeKeyPair.getPrivKey(), endpointRequestBytes);
CryptoAlgorithm signAlgorithm = nodeKeyPair.getAlgorithm();
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction(nodeKeyPair.getAlgorithm());
SignatureDigest signDigest = signFunc.sign(nodeKeyPair.getPrivKey(), endpointRequestBytes);
txMessage.addNodeSignatures(new DigitalSignatureBlob(nodeKeyPair.getPubKey(), signDigest)); txMessage.addNodeSignatures(new DigitalSignatureBlob(nodeKeyPair.getPubKey(), signDigest));


// 计算交易哈希; // 计算交易哈希;
byte[] nodeRequestBytes = BinaryEncodingUtils.encode(txMessage, TransactionRequest.class); byte[] nodeRequestBytes = BinaryEncodingUtils.encode(txMessage, TransactionRequest.class);
HashDigest txHash = CryptoUtils.hash(hashAlgorithm).hash(nodeRequestBytes);
HashFunction hashFunc = CryptoServiceProviders.getHashFunction(nodeKeyPair.getAlgorithm());
HashDigest txHash = hashFunc.hash(nodeRequestBytes);
txMessage.setHash(txHash); txMessage.setHash(txHash);


AsyncFuture<byte[]> result = messageService.sendOrdered(BinaryEncodingUtils.encode(txMessage, TransactionRequest.class)); AsyncFuture<byte[]> result = messageService.sendOrdered(BinaryEncodingUtils.encode(txMessage, TransactionRequest.class));


+ 3
- 11
source/sdk/sdk-client/src/main/java/com/jd/blockchain/sdk/client/GatewayServiceFactory.java View File

@@ -4,7 +4,7 @@ import java.io.Closeable;
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; import com.jd.blockchain.binaryproto.BinaryEncodingUtils;
import com.jd.blockchain.crypto.CryptoAlgorithm; import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PrivKey; import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
@@ -139,16 +139,8 @@ public class GatewayServiceFactory implements BlockchainServiceFactory, Closeabl
byte[] txContentBytes = BinaryEncodingUtils.encode(txRequest.getTransactionContent(), byte[] txContentBytes = BinaryEncodingUtils.encode(txRequest.getTransactionContent(),
TransactionContent.class); TransactionContent.class);
PrivKey userPrivKey = userKey.getPrivKey(); PrivKey userPrivKey = userKey.getPrivKey();
CryptoAlgorithm userAlgorithm = userPrivKey.getAlgorithm();
SignatureFunction signatureFunction = null;
switch (userAlgorithm) {
case ED25519:
signatureFunction = CryptoUtils.asymmCrypto().getSignatureFunction(CryptoAlgorithm.ED25519);
break;
default:
signatureFunction = CryptoUtils.asymmCrypto().getSignatureFunction(CryptoAlgorithm.ED25519);
break;
}
CryptoAlgorithm userAlgorithm = userKey.getAlgorithm();
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction(userAlgorithm);
if (signatureFunction != null) { if (signatureFunction != null) {
SignatureDigest signatureDigest = signatureFunction.sign(userPrivKey, txContentBytes); SignatureDigest signatureDigest = signatureFunction.sign(userPrivKey, txContentBytes);
DigitalSignature signature = new DigitalSignatureBlob(userKey.getPubKey(), signatureDigest); DigitalSignature signature = new DigitalSignatureBlob(userKey.getPubKey(), signatureDigest);


+ 3
- 7
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_Contract.java View File

@@ -1,8 +1,6 @@
package com.jd.blockchain.sdk.samples; package com.jd.blockchain.sdk.samples;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
@@ -24,9 +22,7 @@ import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils;
*/ */
public class SDKDemo_Contract { public class SDKDemo_Contract {


public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);

public static AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();
public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");


/** /**
* 演示合约执行的过程; * 演示合约执行的过程;
@@ -112,7 +108,7 @@ public class SDKDemo_Contract {
* @return * @return
*/ */
private static CryptoKeyPair getSponsorKey() { private static CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }




+ 4
- 7
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_DataAccount.java View File

@@ -1,8 +1,6 @@
package com.jd.blockchain.sdk.samples; package com.jd.blockchain.sdk.samples;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
@@ -16,9 +14,8 @@ import com.jd.blockchain.utils.net.NetworkAddress;


public class SDKDemo_DataAccount { public class SDKDemo_DataAccount {


public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");


public static AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();


/** /**
* 生成一个区块链用户,并注册到区块链; * 生成一个区块链用户,并注册到区块链;
@@ -54,7 +51,7 @@ public class SDKDemo_DataAccount {
// 在本地产生要注册的账户的秘钥; // 在本地产生要注册的账户的秘钥;
BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance(); BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance();


BlockchainKeyPair dataAccount = generator.generate(CryptoAlgorithm.ED25519);
BlockchainKeyPair dataAccount = generator.generate("ED25519");


txTemp.dataAccounts().register(dataAccount.getIdentity()); txTemp.dataAccounts().register(dataAccount.getIdentity());


@@ -77,7 +74,7 @@ public class SDKDemo_DataAccount {
} }


private static CryptoKeyPair getSponsorKey() { private static CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }




+ 3
- 6
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_InsertData.java View File

@@ -1,8 +1,6 @@
package com.jd.blockchain.sdk.samples; package com.jd.blockchain.sdk.samples;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
@@ -23,9 +21,8 @@ import com.jd.blockchain.utils.net.NetworkAddress;
*/ */
public class SDKDemo_InsertData { public class SDKDemo_InsertData {


public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");


public static AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();


/** /**
* 演示数据写入的调用过程; * 演示数据写入的调用过程;
@@ -83,7 +80,7 @@ public class SDKDemo_InsertData {
} }


private static CryptoKeyPair getSponsorKey() { private static CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }




+ 3
- 4
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_Query.java View File

@@ -1,7 +1,6 @@
package com.jd.blockchain.sdk.samples; package com.jd.blockchain.sdk.samples;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair; import com.jd.blockchain.ledger.BlockchainKeyPair;
@@ -21,9 +20,9 @@ import com.jd.blockchain.utils.net.NetworkAddress;
*/ */
public class SDKDemo_Query { public class SDKDemo_Query {
public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");
public static final HashDigest LEDGER_HASH = CryptoUtils.hash(CryptoAlgorithm.SHA256)
public static final HashDigest LEDGER_HASH = CryptoServiceProviders.getHashFunction("SHA256")
.hash("xkxjcioewfqwe".getBytes()); .hash("xkxjcioewfqwe".getBytes());
/** /**


+ 3
- 7
source/sdk/sdk-samples/src/main/java/com/jd/blockchain/sdk/samples/SDKDemo_User.java View File

@@ -1,8 +1,6 @@
package com.jd.blockchain.sdk.samples; package com.jd.blockchain.sdk.samples;


import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
@@ -18,8 +16,6 @@ public class SDKDemo_User {


public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(); public static BlockchainKeyPair CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate();


public static AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();

/** /**
* 生成一个区块链用户,并注册到区块链; * 生成一个区块链用户,并注册到区块链;
*/ */
@@ -67,7 +63,7 @@ public class SDKDemo_User {
//BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance(); //BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance();
//BlockchainKeyPair user = generator.generate(CryptoKeyType.PUBLIC); //BlockchainKeyPair user = generator.generate(CryptoKeyType.PUBLIC);


SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair(); CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair();
BlockchainKeyPair user = new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); BlockchainKeyPair user = new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());


@@ -92,7 +88,7 @@ public class SDKDemo_User {
} }


private static CryptoKeyPair getSponsorKey() { private static CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }




+ 7
- 8
source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_BatchInsertData_Test_.java View File

@@ -14,12 +14,11 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair; import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.EndpointRequest; import com.jd.blockchain.ledger.EndpointRequest;
@@ -57,11 +56,10 @@ public class SDK_GateWay_BatchInsertData_Test_ {


private BlockchainTransactionService service; private BlockchainTransactionService service;


private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();


@Before @Before
public void init() { public void init() {
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");
GATEWAY_IPADDR = "127.0.0.1"; GATEWAY_IPADDR = "127.0.0.1";
GATEWAY_PORT = 8000; GATEWAY_PORT = 8000;
SECURE = false; SECURE = false;
@@ -126,13 +124,14 @@ public class SDK_GateWay_BatchInsertData_Test_ {




private CryptoKeyPair getSponsorKey() { private CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }
private TransactionResponse initResponse() { private TransactionResponse initResponse() {
HashDigest contentHash = new HashDigest(CryptoAlgorithm.SHA256, "contentHash".getBytes());
HashDigest blockHash = new HashDigest(CryptoAlgorithm.SHA256, "blockHash".getBytes());
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");;
HashDigest contentHash = hashFunc.hash("contentHash".getBytes());
HashDigest blockHash = hashFunc.hash("blockHash".getBytes());
long blockHeight = 9998L; long blockHeight = 9998L;


TxResponseMessage resp = new TxResponseMessage(contentHash); TxResponseMessage resp = new TxResponseMessage(contentHash);


+ 8
- 9
source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_DataAccount_Test_.java View File

@@ -12,12 +12,11 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair; import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.EndpointRequest; import com.jd.blockchain.ledger.EndpointRequest;
@@ -52,8 +51,6 @@ public class SDK_GateWay_DataAccount_Test_ {


private BlockchainService service; private BlockchainService service;


private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();

@Before @Before
public void init() { public void init() {
CLIENT_CERT = new BlockchainKeyPair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0); CLIENT_CERT = new BlockchainKeyPair(SDK_GateWay_KeyPair_Para.pubKey0, SDK_GateWay_KeyPair_Para.privkey0);
@@ -112,12 +109,13 @@ public class SDK_GateWay_DataAccount_Test_ {
} }


private HashDigest getLedgerHash() { private HashDigest getLedgerHash() {
HashDigest ledgerHash = new HashDigest(CryptoAlgorithm.SHA256, "jd-gateway".getBytes());
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");;
HashDigest ledgerHash =hashFunc.hash("jd-gateway".getBytes());
return ledgerHash; return ledgerHash;
} }


private SignatureFunction getSignatureFunction() { private SignatureFunction getSignatureFunction() {
return asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
return CryptoServiceProviders.getSignatureFunction("ED25519");
} }


private CryptoKeyPair getSponsorKey() { private CryptoKeyPair getSponsorKey() {
@@ -125,8 +123,9 @@ public class SDK_GateWay_DataAccount_Test_ {
} }


private TransactionResponse initResponse() { private TransactionResponse initResponse() {
HashDigest contentHash = new HashDigest(CryptoAlgorithm.SHA256, "contentHash".getBytes());
HashDigest blockHash = new HashDigest(CryptoAlgorithm.SHA256, "blockHash".getBytes());
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");;
HashDigest contentHash = hashFunc.hash("contentHash".getBytes());
HashDigest blockHash = hashFunc.hash("blockHash".getBytes());
long blockHeight = 9998L; long blockHeight = 9998L;


TxResponseMessage resp = new TxResponseMessage(contentHash); TxResponseMessage resp = new TxResponseMessage(contentHash);


+ 9
- 9
source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_InsertData_Test_.java View File

@@ -14,12 +14,11 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair; import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.EndpointRequest; import com.jd.blockchain.ledger.EndpointRequest;
@@ -54,11 +53,10 @@ public class SDK_GateWay_InsertData_Test_ {


private BlockchainTransactionService service; private BlockchainTransactionService service;


private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();


@Before @Before
public void init() { public void init() {
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");
GATEWAY_IPADDR = "127.0.0.1"; GATEWAY_IPADDR = "127.0.0.1";
GATEWAY_PORT = 8000; GATEWAY_PORT = 8000;
SECURE = false; SECURE = false;
@@ -113,19 +111,21 @@ public class SDK_GateWay_InsertData_Test_ {
} }


private HashDigest getLedgerHash() { private HashDigest getLedgerHash() {
HashDigest ledgerHash = new HashDigest(CryptoAlgorithm.SHA256, "jd-gateway".getBytes());
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");
HashDigest ledgerHash = hashFunc.hash("jd-gateway".getBytes());
return ledgerHash; return ledgerHash;
} }




private CryptoKeyPair getSponsorKey() { private CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }
private TransactionResponse initResponse() { private TransactionResponse initResponse() {
HashDigest contentHash = new HashDigest(CryptoAlgorithm.SHA256, "contentHash".getBytes());
HashDigest blockHash = new HashDigest(CryptoAlgorithm.SHA256, "blockHash".getBytes());
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");
HashDigest contentHash = hashFunc.hash("contentHash".getBytes());
HashDigest blockHash = hashFunc.hash("blockHash".getBytes());
long blockHeight = 9998L; long blockHeight = 9998L;


TxResponseMessage resp = new TxResponseMessage(contentHash); TxResponseMessage resp = new TxResponseMessage(contentHash);


+ 154
- 152
source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_Query_Test_.java View File

@@ -12,14 +12,13 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; import com.jd.blockchain.crypto.asymmetric.SignatureDigest;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.crypto.serialize.ByteArrayObjectDeserializer; import com.jd.blockchain.crypto.serialize.ByteArrayObjectDeserializer;
import com.jd.blockchain.crypto.serialize.ByteArrayObjectSerializer; import com.jd.blockchain.crypto.serialize.ByteArrayObjectSerializer;
import com.jd.blockchain.ledger.AccountHeader; import com.jd.blockchain.ledger.AccountHeader;
@@ -46,6 +45,7 @@ import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils;


/** /**
* 插入数据测试 * 插入数据测试
*
* @author shaozhuguang * @author shaozhuguang
* @create 2018/9/4 * @create 2018/9/4
* @since 1.0.0 * @since 1.0.0
@@ -53,153 +53,155 @@ import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils;


public class SDK_GateWay_Query_Test_ { public class SDK_GateWay_Query_Test_ {


private static Class<?>[] byteArrayClasss = new Class<?>[]{HashDigest.class, PubKey.class, SignatureDigest.class};

static {
for (Class<?> byteArrayClass : byteArrayClasss) {
JSONSerializeUtils.configSerialization(byteArrayClass,
ByteArrayObjectSerializer.getInstance(byteArrayClass),
ByteArrayObjectDeserializer.getInstance(byteArrayClass));
}
}

private BlockchainKeyPair CLIENT_CERT = null;

private String GATEWAY_IPADDR = null;

private int GATEWAY_PORT;

private boolean SECURE;

private BlockchainService service;

private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();

@Before
public void init() {
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519);
GATEWAY_IPADDR = "127.0.0.1";
GATEWAY_PORT = 11000;
SECURE = false;
GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE,
CLIENT_CERT);
service = serviceFactory.getBlockchainService();

DataContractRegistry.register(TransactionContent.class);
DataContractRegistry.register(TransactionContentBody.class);
DataContractRegistry.register(TransactionRequest.class);
DataContractRegistry.register(NodeRequest.class);
DataContractRegistry.register(EndpointRequest.class);
DataContractRegistry.register(TransactionResponse.class);
}

@Test
public void query_Test() {

HashDigest ledgerHash = service.getLedgerHashs()[0];;
// ParserConfig.global.setAutoTypeSupport(true);

LedgerInfo ledgerInfo = service.getLedger(ledgerHash);
long ledgerNumber = ledgerInfo.getLatestBlockHeight();
System.out.println(ledgerNumber);
HashDigest hashDigest = ledgerInfo.getHash();
System.out.println(hashDigest);
// 最新区块;
LedgerBlock latestBlock = service.getBlock(ledgerHash, ledgerNumber);
System.out.println("latestBlock.Hash=" + latestBlock.getHash());
long count = service.getContractCount(ledgerHash, 3L);
System.out.println("contractCount=" + count);
count = service.getContractCount(ledgerHash, hashDigest);
System.out.println("contractCount=" + count);
AccountHeader contract = service.getContract(ledgerHash, "12345678");
System.out.println(contract);

LedgerBlock block = service.getBlock(ledgerHash, hashDigest);
System.out.println("block.Hash=" + block.getHash());

count = service.getDataAccountCount(ledgerHash, 123456);
System.out.println("dataAccountCount=" + count);
count = service.getDataAccountCount(ledgerHash, hashDigest);
System.out.println("dataAccountCount=" + count);

AccountHeader dataAccount = service.getDataAccount(ledgerHash, "1245633");
System.out.println(dataAccount.getAddress());

count = service.getTransactionCount(ledgerHash, hashDigest);
System.out.println("transactionCount=" + count);
count = service.getTransactionCount(ledgerHash, 12456);
System.out.println("transactionCount=" + count);

LedgerTransaction[] txList = service.getTransactions(ledgerHash, ledgerNumber, 0, 100);
for (LedgerTransaction ledgerTransaction : txList) {
System.out.println("ledgerTransaction.Hash=" + ledgerTransaction.getHash());
}

txList = service.getTransactions(ledgerHash, hashDigest, 0, 100);
for (LedgerTransaction ledgerTransaction : txList) {
System.out.println("ledgerTransaction.Hash=" + ledgerTransaction.getHash());
}

Transaction tx = service.getTransactionByContentHash(ledgerHash, hashDigest);
DigitalSignature[] signatures = tx.getEndpointSignatures();
for (DigitalSignature signature : signatures) {
System.out.println(signature.getDigest().getAlgorithm());
}
System.out.println("transaction.blockHeight=" + tx.getBlockHeight());
System.out.println("transaction.executionState=" + tx.getExecutionState());


ParticipantNode[] participants = service.getConsensusParticipants(ledgerHash);
for (ParticipantNode participant : participants) {
System.out.println("participant.name=" + participant.getName());
// System.out.println(participant.getConsensusAddress());
// System.out.println("participant.host=" + participant.getConsensusAddress().getHost());
System.out.println("participant.getPubKey=" + participant.getPubKey());
System.out.println("participant.getKeyType=" + participant.getPubKey().getKeyType());
System.out.println("participant.getRawKeyBytes=" + participant.getPubKey().getRawKeyBytes());
System.out.println("participant.algorithm=" + participant.getPubKey().getAlgorithm());
}
String commerceAccount = "GGhhreGeasdfasfUUfehf9932lkae99ds66jf==";
String[] objKeys = new String[] { "x001", "x002" };
KVDataEntry[] kvData = service.getDataEntries(ledgerHash, commerceAccount, objKeys);
for (KVDataEntry kvDatum : kvData) {
System.out.println("kvData.key=" + kvDatum.getKey());
System.out.println("kvData.version=" + kvDatum.getVersion());
System.out.println("kvData.value=" + kvDatum.getValue());
}

HashDigest[] hashs = service.getLedgerHashs();
for (HashDigest hash : hashs) {
System.out.println("hash.toBase58=" + hash.toBase58());
}
}

private HashDigest getLedgerHash() {
HashDigest ledgerHash = new HashDigest(CryptoAlgorithm.SHA256, "jd-gateway".getBytes());
return ledgerHash;
}

private SignatureFunction getSignatureFunction() {
return asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
}

private BlockchainKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair();
BlockchainKeyPair blockchainKeyPair = new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey());
return blockchainKeyPair;
}

private TransactionResponse initResponse() {
HashDigest contentHash = new HashDigest(CryptoAlgorithm.SHA256, "contentHash".getBytes());
HashDigest blockHash = new HashDigest(CryptoAlgorithm.SHA256, "blockHash".getBytes());
long blockHeight = 9998L;

TxResponseMessage resp = new TxResponseMessage(contentHash);
resp.setBlockHash(blockHash);
resp.setBlockHeight(blockHeight);
resp.setExecutionState(TransactionState.SUCCESS);
return resp;
}
private static Class<?>[] byteArrayClasss = new Class<?>[] { HashDigest.class, PubKey.class,
SignatureDigest.class };

static {
for (Class<?> byteArrayClass : byteArrayClasss) {
JSONSerializeUtils.configSerialization(byteArrayClass,
ByteArrayObjectSerializer.getInstance(byteArrayClass),
ByteArrayObjectDeserializer.getInstance(byteArrayClass));
}
}

private BlockchainKeyPair CLIENT_CERT = null;

private String GATEWAY_IPADDR = null;

private int GATEWAY_PORT;

private boolean SECURE;

private BlockchainService service;

@Before
public void init() {
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519");
GATEWAY_IPADDR = "127.0.0.1";
GATEWAY_PORT = 11000;
SECURE = false;
GatewayServiceFactory serviceFactory = GatewayServiceFactory.connect(GATEWAY_IPADDR, GATEWAY_PORT, SECURE,
CLIENT_CERT);
service = serviceFactory.getBlockchainService();

DataContractRegistry.register(TransactionContent.class);
DataContractRegistry.register(TransactionContentBody.class);
DataContractRegistry.register(TransactionRequest.class);
DataContractRegistry.register(NodeRequest.class);
DataContractRegistry.register(EndpointRequest.class);
DataContractRegistry.register(TransactionResponse.class);
}

@Test
public void query_Test() {

HashDigest ledgerHash = service.getLedgerHashs()[0];
;
// ParserConfig.global.setAutoTypeSupport(true);

LedgerInfo ledgerInfo = service.getLedger(ledgerHash);
long ledgerNumber = ledgerInfo.getLatestBlockHeight();
System.out.println(ledgerNumber);
HashDigest hashDigest = ledgerInfo.getHash();
System.out.println(hashDigest);
// 最新区块;
LedgerBlock latestBlock = service.getBlock(ledgerHash, ledgerNumber);
System.out.println("latestBlock.Hash=" + latestBlock.getHash());
long count = service.getContractCount(ledgerHash, 3L);
System.out.println("contractCount=" + count);
count = service.getContractCount(ledgerHash, hashDigest);
System.out.println("contractCount=" + count);
AccountHeader contract = service.getContract(ledgerHash, "12345678");
System.out.println(contract);

LedgerBlock block = service.getBlock(ledgerHash, hashDigest);
System.out.println("block.Hash=" + block.getHash());

count = service.getDataAccountCount(ledgerHash, 123456);
System.out.println("dataAccountCount=" + count);
count = service.getDataAccountCount(ledgerHash, hashDigest);
System.out.println("dataAccountCount=" + count);

AccountHeader dataAccount = service.getDataAccount(ledgerHash, "1245633");
System.out.println(dataAccount.getAddress());

count = service.getTransactionCount(ledgerHash, hashDigest);
System.out.println("transactionCount=" + count);
count = service.getTransactionCount(ledgerHash, 12456);
System.out.println("transactionCount=" + count);

LedgerTransaction[] txList = service.getTransactions(ledgerHash, ledgerNumber, 0, 100);
for (LedgerTransaction ledgerTransaction : txList) {
System.out.println("ledgerTransaction.Hash=" + ledgerTransaction.getHash());
}

txList = service.getTransactions(ledgerHash, hashDigest, 0, 100);
for (LedgerTransaction ledgerTransaction : txList) {
System.out.println("ledgerTransaction.Hash=" + ledgerTransaction.getHash());
}

Transaction tx = service.getTransactionByContentHash(ledgerHash, hashDigest);
DigitalSignature[] signatures = tx.getEndpointSignatures();
for (DigitalSignature signature : signatures) {
System.out.println(signature.getDigest().getAlgorithm());
}
System.out.println("transaction.blockHeight=" + tx.getBlockHeight());
System.out.println("transaction.executionState=" + tx.getExecutionState());

ParticipantNode[] participants = service.getConsensusParticipants(ledgerHash);
for (ParticipantNode participant : participants) {
System.out.println("participant.name=" + participant.getName());
// System.out.println(participant.getConsensusAddress());
// System.out.println("participant.host=" +
// participant.getConsensusAddress().getHost());
System.out.println("participant.getPubKey=" + participant.getPubKey());
System.out.println("participant.getKeyType=" + participant.getPubKey().getKeyType());
System.out.println("participant.getRawKeyBytes=" + participant.getPubKey().getRawKeyBytes());
System.out.println("participant.algorithm=" + participant.getPubKey().getAlgorithm());
}

String commerceAccount = "GGhhreGeasdfasfUUfehf9932lkae99ds66jf==";
String[] objKeys = new String[] { "x001", "x002" };
KVDataEntry[] kvData = service.getDataEntries(ledgerHash, commerceAccount, objKeys);
for (KVDataEntry kvDatum : kvData) {
System.out.println("kvData.key=" + kvDatum.getKey());
System.out.println("kvData.version=" + kvDatum.getVersion());
System.out.println("kvData.value=" + kvDatum.getValue());
}

HashDigest[] hashs = service.getLedgerHashs();
for (HashDigest hash : hashs) {
System.out.println("hash.toBase58=" + hash.toBase58());
}
}

private HashDigest getLedgerHash() {
HashDigest ledgerHash = CryptoServiceProviders.getHashFunction("SHA256").hash("jd-gateway".getBytes());
return ledgerHash;
}

private SignatureFunction getSignatureFunction() {
return CryptoServiceProviders.getSignatureFunction("ED25519");
}

private BlockchainKeyPair getSponsorKey() {
SignatureFunction signatureFunction = getSignatureFunction();
CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair();
BlockchainKeyPair blockchainKeyPair = new BlockchainKeyPair(cryptoKeyPair.getPubKey(),
cryptoKeyPair.getPrivKey());
return blockchainKeyPair;
}

private TransactionResponse initResponse() {
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");
HashDigest contentHash = hashFunc.hash("contentHash".getBytes());
HashDigest blockHash = hashFunc.hash("blockHash".getBytes());
long blockHeight = 9998L;

TxResponseMessage resp = new TxResponseMessage(contentHash);
resp.setBlockHash(blockHash);
resp.setBlockHeight(blockHeight);
resp.setExecutionState(TransactionState.SUCCESS);
return resp;
}
} }

+ 6
- 8
source/sdk/sdk-samples/src/test/java/test/com/jd/blockchain/sdk/test/SDK_GateWay_User_Test_.java View File

@@ -14,14 +14,13 @@ import org.junit.Before;
import org.junit.Test; import org.junit.Test;


import com.jd.blockchain.binaryproto.DataContractRegistry; import com.jd.blockchain.binaryproto.DataContractRegistry;
import com.jd.blockchain.crypto.CryptoAlgorithm;
import com.jd.blockchain.crypto.CryptoUtils;
import com.jd.blockchain.crypto.CryptoServiceProviders;
import com.jd.blockchain.crypto.PrivKey; import com.jd.blockchain.crypto.PrivKey;
import com.jd.blockchain.crypto.PubKey; import com.jd.blockchain.crypto.PubKey;
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography;
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair;
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; import com.jd.blockchain.crypto.asymmetric.SignatureFunction;
import com.jd.blockchain.crypto.hash.HashDigest; import com.jd.blockchain.crypto.hash.HashDigest;
import com.jd.blockchain.crypto.hash.HashFunction;
import com.jd.blockchain.ledger.BlockchainKeyGenerator; import com.jd.blockchain.ledger.BlockchainKeyGenerator;
import com.jd.blockchain.ledger.BlockchainKeyPair; import com.jd.blockchain.ledger.BlockchainKeyPair;
import com.jd.blockchain.ledger.EndpointRequest; import com.jd.blockchain.ledger.EndpointRequest;
@@ -65,8 +64,6 @@ public class SDK_GateWay_User_Test_ {


private BlockchainService service; private BlockchainService service;


private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto();

@Before @Before
public void init() { public void init() {


@@ -141,13 +138,14 @@ public class SDK_GateWay_User_Test_ {
// } // }


private CryptoKeyPair getSponsorKey() { private CryptoKeyPair getSponsorKey() {
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519);
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519");
return signatureFunction.generateKeyPair(); return signatureFunction.generateKeyPair();
} }


private TransactionResponse initResponse() { private TransactionResponse initResponse() {
HashDigest contentHash = new HashDigest(CryptoAlgorithm.SHA256, "contentHash".getBytes());
HashDigest blockHash = new HashDigest(CryptoAlgorithm.SHA256, "blockHash".getBytes());
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");;
HashDigest contentHash = hashFunc.hash("contentHash".getBytes());
HashDigest blockHash = hashFunc.hash("blockHash".getBytes());
long blockHeight = 9998L; long blockHeight = 9998L;


TxResponseMessage resp = new TxResponseMessage(contentHash); TxResponseMessage resp = new TxResponseMessage(contentHash);


Loading…
Cancel
Save