@@ -14,9 +14,6 @@ | |||
<module>contract-framework</module> | |||
<module>contract-jvm</module> | |||
<module>contract-maven-plugin</module> | |||
<module>contract-compile</module> | |||
</modules> | |||
</project> |
@@ -49,12 +49,10 @@ public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes { | |||
@Override | |||
public CryptoAlgorithm getAlgorithm() { | |||
// return resolveAlgorithm(encodedBytes); | |||
return algorithm; | |||
} | |||
protected BytesSlice getRawCryptoBytes() { | |||
// return resolveRawCryptoBytes(encodedBytes); | |||
return new BytesSlice(getDirectBytes(), 2); | |||
} | |||
} |
@@ -181,4 +181,7 @@ public interface CryptoAlgorithm { | |||
return isEncryptionAlgorithm(algorithm) && hasAsymmetricKey(algorithm); | |||
} | |||
static boolean equals(CryptoAlgorithm algorithm1, CryptoAlgorithm algorithm2) { | |||
return algorithm1.code() == algorithm2.code(); | |||
} | |||
} |
@@ -129,6 +129,15 @@ public final class CryptoServiceProviders { | |||
Short code = names.get(name.toUpperCase()); | |||
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) { | |||
if (!CryptoAlgorithm.isRandomAlgorithm(algorithm)) { | |||
@@ -143,6 +152,15 @@ public final class CryptoServiceProviders { | |||
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) { | |||
if (!CryptoAlgorithm.isHashAlgorithm(algorithm)) { | |||
@@ -157,6 +175,15 @@ public final class CryptoServiceProviders { | |||
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) { | |||
if (!CryptoAlgorithm.isAsymmetricEncryptionAlgorithm(algorithm)) { | |||
@@ -171,6 +198,16 @@ public final class CryptoServiceProviders { | |||
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) { | |||
if (!CryptoAlgorithm.isSignatureAlgorithm(algorithm)) { | |||
@@ -185,6 +222,16 @@ public final class CryptoServiceProviders { | |||
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) { | |||
if (!CryptoAlgorithm.isSymmetricEncryptionAlgorithm(algorithm)) { | |||
@@ -199,6 +246,16 @@ public final class CryptoServiceProviders { | |||
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) { | |||
CryptoFunction func = functions.get(algorithm.code()); | |||
@@ -1,14 +1,19 @@ | |||
package com.jd.blockchain.crypto.asymmetric; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.PrivKey; | |||
import com.jd.blockchain.crypto.PubKey; | |||
public class CryptoKeyPair { | |||
private PubKey pubKey; | |||
private PrivKey privKey; | |||
public CryptoAlgorithm getAlgorithm() { | |||
return pubKey.getAlgorithm(); | |||
} | |||
public PubKey getPubKey() { | |||
return pubKey; | |||
} | |||
@@ -18,8 +23,11 @@ public class CryptoKeyPair { | |||
} | |||
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.privKey = privKey; | |||
} | |||
} |
@@ -11,46 +11,35 @@ | |||
<artifactId>ledger-model</artifactId> | |||
<dependencies> | |||
<dependency> | |||
<groupId>com.jd.blockchain</groupId> | |||
<artifactId>binary-proto</artifactId> | |||
<artifactId>utils-common</artifactId> | |||
<version>${project.version}</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.jd.blockchain</groupId> | |||
<artifactId>crypto-framework</artifactId> | |||
<artifactId>utils-web</artifactId> | |||
<version>${project.version}</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.jd.blockchain</groupId> | |||
<artifactId>crypto-impl</artifactId> | |||
<artifactId>binary-proto</artifactId> | |||
<version>${project.version}</version> | |||
<scope>test</scope> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.jd.blockchain</groupId> | |||
<artifactId>utils-common</artifactId> | |||
<artifactId>crypto-framework</artifactId> | |||
<version>${project.version}</version> | |||
</dependency> | |||
<dependency> | |||
<groupId>com.jd.blockchain</groupId> | |||
<artifactId>utils-web</artifactId> | |||
<artifactId>crypto-classic</artifactId> | |||
<version>${project.version}</version> | |||
<scope>test</scope> | |||
</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> | |||
</project> |
@@ -1,8 +1,9 @@ | |||
package com.jd.blockchain.ledger; | |||
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.SignatureFunction; | |||
/** | |||
* 区块链密钥生成器; | |||
@@ -11,6 +12,8 @@ import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
* | |||
*/ | |||
public class BlockchainKeyGenerator { | |||
public static final String DEFAULT_ALGORITHM = "ED25519"; | |||
private BlockchainKeyGenerator() { | |||
} | |||
@@ -20,11 +23,17 @@ public class BlockchainKeyGenerator { | |||
} | |||
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) { | |||
CryptoKeyPair cryptoKeyPair = CryptoUtils.sign(signatureAlgorithm).generateKeyPair(); | |||
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction(signatureAlgorithm); | |||
CryptoKeyPair cryptoKeyPair = signFunc.generateKeyPair(); | |||
return new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); | |||
} | |||
@@ -24,7 +24,7 @@ import com.jd.blockchain.ledger.MagicNumber; | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
public class DigitalSignatureBlob implements DigitalSignature { //, Externalizable | |||
public class DigitalSignatureBlob implements DigitalSignature { | |||
private PubKey pubKey; | |||
@@ -1,7 +1,7 @@ | |||
package com.jd.blockchain.ledger.data; | |||
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.asymmetric.CryptoKeyPair; | |||
import com.jd.blockchain.crypto.asymmetric.SignatureDigest; | |||
@@ -37,8 +37,7 @@ public class PreparedTx implements PreparedTransaction { | |||
@Override | |||
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(); | |||
byte[] content = BinaryEncodingUtils.encode(getTransactionContent(), TransactionContent.class); | |||
SignatureDigest signatureDigest = signatureFunction.sign(privKey, content); | |||
@@ -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 + "]!"); | |||
} | |||
} | |||
} |
@@ -2,8 +2,7 @@ package 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.CryptoUtils; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.hash.HashDigest; | |||
import com.jd.blockchain.ledger.TransactionBuilder; | |||
import com.jd.blockchain.ledger.TransactionContent; | |||
@@ -19,8 +18,8 @@ public class TxBuilder implements TransactionBuilder { | |||
private BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); | |||
private CryptoAlgorithm defaultHashAlgorithm = CryptoAlgorithm.SHA256; | |||
private static final String DEFAULT_HASH_ALGORITHM = "SHA256"; | |||
private HashDigest ledgerHash; | |||
public TxBuilder(HashDigest ledgerHash) { | |||
@@ -44,7 +43,7 @@ public class TxBuilder implements TransactionBuilder { | |||
txContent.addOperations(opFactory.getOperations()); | |||
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); | |||
return txContent; | |||
@@ -4,8 +4,7 @@ import java.util.ArrayList; | |||
import java.util.List; | |||
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.PubKey; | |||
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
@@ -19,6 +18,8 @@ import com.jd.blockchain.ledger.TransactionRequestBuilder; | |||
public class TxRequestBuilder implements TransactionRequestBuilder { | |||
private static final String DEFAULT_HASH_ALGORITHM = "SHA256"; | |||
private TransactionContent txContent; | |||
private List<DigitalSignature> endpointSignatures = new ArrayList<>(); | |||
@@ -70,11 +71,13 @@ public class TxRequestBuilder implements TransactionRequestBuilder { | |||
} | |||
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) { | |||
return CryptoUtils.sign(signDigest.getAlgorithm()).verify(signDigest, pubKey, txContent.getHash().toBytes()); | |||
return CryptoServiceProviders.getSignatureFunction(pubKey.getAlgorithm()).verify(signDigest, pubKey, | |||
txContent.getHash().toBytes()); | |||
} | |||
@Override | |||
@@ -84,7 +87,7 @@ public class TxRequestBuilder implements TransactionRequestBuilder { | |||
txMessage.addNodeSignatures(nodeSignatures); | |||
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); | |||
return txMessage; | |||
@@ -8,21 +8,25 @@ | |||
*/ | |||
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.DataContractRegistry; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
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.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 | |||
@@ -38,8 +42,8 @@ public class ContractCodeDeployOpTemplateTest { | |||
public void initContractCodeDeployOpTemplate() { | |||
DataContractRegistry.register(ContractCodeDeployOperation.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); | |||
byte[] chainCode = "jd-test".getBytes(); | |||
data = new ContractCodeDeployOpTemplate(contractID, chainCode); | |||
@@ -8,18 +8,21 @@ | |||
*/ | |||
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.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() { | |||
DataContractRegistry.register(DataAccountRegisterOperation.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); | |||
data = new DataAccountRegisterOpTemplate(contractID); | |||
@@ -16,8 +16,11 @@ import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
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.asymmetric.CryptoKeyPair; | |||
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.DigitalSignatureBody; | |||
import com.jd.blockchain.ledger.data.DigitalSignatureBlob; | |||
@@ -37,8 +40,11 @@ public class DigitalSignatureBlobTest { | |||
public void initDigitalSignatureBlob() throws Exception { | |||
DataContractRegistry.register(DigitalSignature.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); | |||
} | |||
@@ -2,25 +2,28 @@ package test.com.jd.blockchain.ledger.data; | |||
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; | |||
public class ED25519SignatureTest { | |||
public static void main(String[] args) { | |||
@Test | |||
public void perfomanceTest() { | |||
Random rand = new Random(); | |||
byte[] data = new byte[64]; | |||
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[] privKey = key.getPrivKey().getRawKeyBytes(); | |||
int count = 10000; | |||
System.out.println("=================== do sign test ==================="); | |||
byte[] sign = null; | |||
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, | |||
(count * 1000.00D) / elapsedTS)); | |||
} | |||
System.out.println("=================== do verify test ==================="); | |||
for (int r = 0; r < 5; r++) { | |||
System.out.println("------------- round[" + r + "] --------------"); | |||
@@ -6,15 +6,20 @@ import static org.junit.Assert.assertEquals; | |||
import java.io.IOException; | |||
import java.util.UUID; | |||
import com.jd.blockchain.ledger.*; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
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.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.TxContentBlob; | |||
import com.jd.blockchain.utils.io.ByteArray; | |||
@@ -29,32 +34,36 @@ public class TxContentBlobTest { | |||
DataContractRegistry.register(TransactionContent.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(); | |||
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); | |||
} | |||
@Test | |||
public void testSerialize_TransactionContentBody() throws IOException,InstantiationException,IllegalAccessException{ | |||
public void testSerialize_TransactionContentBody() | |||
throws IOException, InstantiationException, IllegalAccessException { | |||
byte[] bytesContent = BinaryEncodingUtils.encode(contentBlob, TransactionContentBody.class); | |||
TransactionContentBody resolvedContentBlob = BinaryEncodingUtils.decode(bytesContent); | |||
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); | |||
@@ -69,20 +78,23 @@ public class TxContentBlobTest { | |||
for (int j = 0; j < dataKv.length; j++) { | |||
assertEquals(dataKv[i].getKey(), resolvedKv[i].getKey()); | |||
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 | |||
public void testSerialize_TransactionContent() throws IOException,InstantiationException,IllegalAccessException{ | |||
public void testSerialize_TransactionContent() throws IOException, InstantiationException, IllegalAccessException { | |||
byte[] bytesContent = BinaryEncodingUtils.encode(contentBlob, TransactionContent.class); | |||
TransactionContentBody resolvedContentBlob = BinaryEncodingUtils.decode(bytesContent); | |||
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); | |||
@@ -97,7 +109,8 @@ public class TxContentBlobTest { | |||
for (int j = 0; j < dataKv.length; j++) { | |||
assertEquals(dataKv[i].getKey(), resolvedKv[i].getKey()); | |||
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()); | |||
} | |||
} | |||
} | |||
@@ -17,11 +17,12 @@ import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
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.SignatureFunction; | |||
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.BlockchainKeyPair; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
@@ -58,25 +59,27 @@ public class TxRequestMessageTest { | |||
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(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(node2); | |||
HashDigest hash = new HashDigest(CryptoAlgorithm.SHA256, "sunqi".getBytes()); | |||
HashDigest hash = CryptoServiceProviders.getHashFunction("SHA256").hash("DATA".getBytes()); | |||
data.setHash(hash); | |||
} | |||
@@ -192,12 +195,12 @@ public class TxRequestMessageTest { | |||
private TransactionContent initTransactionContent() throws Exception { | |||
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(); | |||
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.setSequenceNumber(1); | |||
DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress()) | |||
@@ -8,16 +8,19 @@ | |||
*/ | |||
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.DataContractRegistry; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
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 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 { | |||
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 ------"); | |||
} | |||
} |
@@ -8,20 +8,21 @@ | |||
*/ | |||
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.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() { | |||
DataContractRegistry.register(UserRegisterOperation.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); | |||
data = new UserRegisterOpTemplate(contractID); | |||
} | |||
@@ -5,10 +5,12 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.consensus.MessageService; | |||
import com.jd.blockchain.consensus.client.ConsensusClient; | |||
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.SignatureDigest; | |||
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
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.TransactionRequest; | |||
import com.jd.blockchain.ledger.TransactionResponse; | |||
@@ -64,13 +66,15 @@ public class NodeSigningAppender implements TransactionService { | |||
// 生成网关签名; | |||
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)); | |||
// 计算交易哈希; | |||
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); | |||
AsyncFuture<byte[]> result = messageService.sendOrdered(BinaryEncodingUtils.encode(txMessage, TransactionRequest.class)); | |||
@@ -4,7 +4,7 @@ import java.io.Closeable; | |||
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.asymmetric.SignatureDigest; | |||
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
@@ -139,16 +139,8 @@ public class GatewayServiceFactory implements BlockchainServiceFactory, Closeabl | |||
byte[] txContentBytes = BinaryEncodingUtils.encode(txRequest.getTransactionContent(), | |||
TransactionContent.class); | |||
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) { | |||
SignatureDigest signatureDigest = signatureFunction.sign(userPrivKey, txContentBytes); | |||
DigitalSignature signature = new DigitalSignatureBlob(userKey.getPubKey(), signatureDigest); | |||
@@ -1,8 +1,6 @@ | |||
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.SignatureFunction; | |||
import com.jd.blockchain.crypto.hash.HashDigest; | |||
@@ -24,9 +22,7 @@ import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; | |||
*/ | |||
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 | |||
*/ | |||
private static CryptoKeyPair getSponsorKey() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
@@ -1,8 +1,6 @@ | |||
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.SignatureFunction; | |||
import com.jd.blockchain.crypto.hash.HashDigest; | |||
@@ -16,9 +14,8 @@ import com.jd.blockchain.utils.net.NetworkAddress; | |||
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(); | |||
BlockchainKeyPair dataAccount = generator.generate(CryptoAlgorithm.ED25519); | |||
BlockchainKeyPair dataAccount = generator.generate("ED25519"); | |||
txTemp.dataAccounts().register(dataAccount.getIdentity()); | |||
@@ -77,7 +74,7 @@ public class SDKDemo_DataAccount { | |||
} | |||
private static CryptoKeyPair getSponsorKey() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
@@ -1,8 +1,6 @@ | |||
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.SignatureFunction; | |||
import com.jd.blockchain.crypto.hash.HashDigest; | |||
@@ -23,9 +21,8 @@ import com.jd.blockchain.utils.net.NetworkAddress; | |||
*/ | |||
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() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
@@ -1,7 +1,6 @@ | |||
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.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeyPair; | |||
@@ -21,9 +20,9 @@ import com.jd.blockchain.utils.net.NetworkAddress; | |||
*/ | |||
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()); | |||
/** | |||
@@ -1,8 +1,6 @@ | |||
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.SignatureFunction; | |||
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 AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto(); | |||
/** | |||
* 生成一个区块链用户,并注册到区块链; | |||
*/ | |||
@@ -67,7 +63,7 @@ public class SDKDemo_User { | |||
//BlockchainKeyGenerator generator = BlockchainKeyGenerator.getInstance(); | |||
//BlockchainKeyPair user = generator.generate(CryptoKeyType.PUBLIC); | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
CryptoKeyPair cryptoKeyPair = signatureFunction.generateKeyPair(); | |||
BlockchainKeyPair user = new BlockchainKeyPair(cryptoKeyPair.getPubKey(), cryptoKeyPair.getPrivKey()); | |||
@@ -92,7 +88,7 @@ public class SDKDemo_User { | |||
} | |||
private static CryptoKeyPair getSponsorKey() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
@@ -14,12 +14,11 @@ import org.junit.Before; | |||
import org.junit.Test; | |||
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.SignatureFunction; | |||
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.BlockchainKeyPair; | |||
import com.jd.blockchain.ledger.EndpointRequest; | |||
@@ -57,11 +56,10 @@ public class SDK_GateWay_BatchInsertData_Test_ { | |||
private BlockchainTransactionService service; | |||
private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto(); | |||
@Before | |||
public void init() { | |||
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519); | |||
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519"); | |||
GATEWAY_IPADDR = "127.0.0.1"; | |||
GATEWAY_PORT = 8000; | |||
SECURE = false; | |||
@@ -126,13 +124,14 @@ public class SDK_GateWay_BatchInsertData_Test_ { | |||
private CryptoKeyPair getSponsorKey() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
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; | |||
TxResponseMessage resp = new TxResponseMessage(contentHash); | |||
@@ -12,12 +12,11 @@ import org.junit.Before; | |||
import org.junit.Test; | |||
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.SignatureFunction; | |||
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.BlockchainKeyPair; | |||
import com.jd.blockchain.ledger.EndpointRequest; | |||
@@ -52,8 +51,6 @@ public class SDK_GateWay_DataAccount_Test_ { | |||
private BlockchainService service; | |||
private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto(); | |||
@Before | |||
public void init() { | |||
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() { | |||
HashDigest ledgerHash = new HashDigest(CryptoAlgorithm.SHA256, "jd-gateway".getBytes()); | |||
HashFunction hashFunc = CryptoServiceProviders.getHashFunction("SHA256");; | |||
HashDigest ledgerHash =hashFunc.hash("jd-gateway".getBytes()); | |||
return ledgerHash; | |||
} | |||
private SignatureFunction getSignatureFunction() { | |||
return asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
return CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
} | |||
private CryptoKeyPair getSponsorKey() { | |||
@@ -125,8 +123,9 @@ public class SDK_GateWay_DataAccount_Test_ { | |||
} | |||
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; | |||
TxResponseMessage resp = new TxResponseMessage(contentHash); | |||
@@ -14,12 +14,11 @@ import org.junit.Before; | |||
import org.junit.Test; | |||
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.SignatureFunction; | |||
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.BlockchainKeyPair; | |||
import com.jd.blockchain.ledger.EndpointRequest; | |||
@@ -54,11 +53,10 @@ public class SDK_GateWay_InsertData_Test_ { | |||
private BlockchainTransactionService service; | |||
private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto(); | |||
@Before | |||
public void init() { | |||
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate(CryptoAlgorithm.ED25519); | |||
CLIENT_CERT = BlockchainKeyGenerator.getInstance().generate("ED25519"); | |||
GATEWAY_IPADDR = "127.0.0.1"; | |||
GATEWAY_PORT = 8000; | |||
SECURE = false; | |||
@@ -113,19 +111,21 @@ public class SDK_GateWay_InsertData_Test_ { | |||
} | |||
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; | |||
} | |||
private CryptoKeyPair getSponsorKey() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
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; | |||
TxResponseMessage resp = new TxResponseMessage(contentHash); | |||
@@ -12,14 +12,13 @@ import org.junit.Before; | |||
import org.junit.Test; | |||
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.asymmetric.AsymmetricCryptography; | |||
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
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.HashFunction; | |||
import com.jd.blockchain.crypto.serialize.ByteArrayObjectDeserializer; | |||
import com.jd.blockchain.crypto.serialize.ByteArrayObjectSerializer; | |||
import com.jd.blockchain.ledger.AccountHeader; | |||
@@ -46,6 +45,7 @@ import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; | |||
/** | |||
* 插入数据测试 | |||
* | |||
* @author shaozhuguang | |||
* @create 2018/9/4 | |||
* @since 1.0.0 | |||
@@ -53,153 +53,155 @@ import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; | |||
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; | |||
} | |||
} |
@@ -14,14 +14,13 @@ import org.junit.Before; | |||
import org.junit.Test; | |||
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.PubKey; | |||
import com.jd.blockchain.crypto.asymmetric.AsymmetricCryptography; | |||
import com.jd.blockchain.crypto.asymmetric.CryptoKeyPair; | |||
import com.jd.blockchain.crypto.asymmetric.SignatureFunction; | |||
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.BlockchainKeyPair; | |||
import com.jd.blockchain.ledger.EndpointRequest; | |||
@@ -65,8 +64,6 @@ public class SDK_GateWay_User_Test_ { | |||
private BlockchainService service; | |||
private AsymmetricCryptography asymmetricCryptography = CryptoUtils.asymmCrypto(); | |||
@Before | |||
public void init() { | |||
@@ -141,13 +138,14 @@ public class SDK_GateWay_User_Test_ { | |||
// } | |||
private CryptoKeyPair getSponsorKey() { | |||
SignatureFunction signatureFunction = asymmetricCryptography.getSignatureFunction(CryptoAlgorithm.ED25519); | |||
SignatureFunction signatureFunction = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
return signatureFunction.generateKeyPair(); | |||
} | |||
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; | |||
TxResponseMessage resp = new TxResponseMessage(contentHash); | |||