@@ -5,6 +5,7 @@ import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE; | |||
import java.util.Arrays; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoBytes; | |||
import com.jd.blockchain.crypto.CryptoException; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.HashFunction; | |||
@@ -57,4 +58,5 @@ public class RIPEMD160HashFunction implements HashFunction { | |||
throw new CryptoException("digestBytes is invalid!"); | |||
} | |||
} | |||
} |
@@ -5,6 +5,7 @@ import static com.jd.blockchain.crypto.CryptoBytes.ALGORYTHM_CODE_SIZE; | |||
import java.util.Arrays; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoBytes; | |||
import com.jd.blockchain.crypto.CryptoException; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.HashFunction; | |||
@@ -57,4 +58,5 @@ public class SHA256HashFunction implements HashFunction { | |||
throw new CryptoException("digestBytes is invalid!"); | |||
} | |||
} | |||
} |
@@ -4,7 +4,6 @@ import java.util.Arrays; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.BytesSlice; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes { | |||
@@ -15,44 +14,23 @@ public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes { | |||
} | |||
public BaseCryptoBytes(short algorithm, byte[] rawCryptoBytes) { | |||
super(encodeBytes(algorithm, rawCryptoBytes)); | |||
super(CryptoBytesEncoding.encodeBytes(algorithm, rawCryptoBytes)); | |||
this.algorithm = algorithm; | |||
} | |||
public BaseCryptoBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | |||
super(encodeBytes(algorithm, rawCryptoBytes)); | |||
super(CryptoBytesEncoding.encodeBytes(algorithm, rawCryptoBytes)); | |||
this.algorithm = algorithm.code(); | |||
} | |||
public BaseCryptoBytes(byte[] cryptoBytes) { | |||
super(cryptoBytes); | |||
short algorithm = decodeAlgorithm(cryptoBytes); | |||
short algorithm = CryptoBytesEncoding.decodeAlgorithm(cryptoBytes); | |||
if (!support(algorithm)) { | |||
throw new CryptoException("Not supported algorithm [code:" + algorithm + "]!"); | |||
} | |||
this.algorithm = algorithm; | |||
} | |||
static byte[] encodeBytes(short algorithm, byte[] rawCryptoBytes) { | |||
return BytesUtils.concat(BytesUtils.toBytes(algorithm), rawCryptoBytes); | |||
} | |||
static byte[] encodeBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | |||
return BytesUtils.concat(CryptoAlgorithm.toBytes(algorithm), rawCryptoBytes); | |||
} | |||
static short decodeAlgorithm(byte[] cryptoBytes) { | |||
return CryptoAlgorithm.resolveCode(cryptoBytes); | |||
} | |||
// static CryptoAlgorithm decodeAlgorithm(byte[] cryptoBytes) { | |||
// short algorithmCode = BytesUtils.toShort(cryptoBytes, 0); | |||
// CryptoAlgorithm algorithm = CryptoServiceProviders.getAlgorithm(algorithmCode); | |||
// if (algorithm == null) { | |||
// throw new CryptoException("The algorithm with code[" + algorithmCode + "] is not supported!"); | |||
// } | |||
// return algorithm; | |||
// } | |||
protected abstract boolean support(short algorithm); | |||
@@ -66,6 +44,6 @@ public abstract class BaseCryptoBytes extends Bytes implements CryptoBytes { | |||
} | |||
protected BytesSlice getRawCryptoBytes() { | |||
return new BytesSlice(getDirectBytes(), 2); | |||
return new BytesSlice(getDirectBytes(), CryptoAlgorithm.CODE_SIZE); | |||
} | |||
} |
@@ -2,42 +2,31 @@ package com.jd.blockchain.crypto; | |||
import java.io.Serializable; | |||
import com.jd.blockchain.utils.io.BytesSlice; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
public abstract class BaseCryptoKey extends BaseCryptoBytes implements CryptoKey, Serializable { | |||
public static final int KEY_TYPE_BYTES = 1; | |||
private static final long serialVersionUID = 4543074827807908363L; | |||
// public BaseCryptoKey() { | |||
// super(); | |||
// } | |||
// public BaseCryptoKey() { | |||
// super(); | |||
// } | |||
protected BaseCryptoKey(short algorithm, byte[] rawKeyBytes, CryptoKeyType keyType) { | |||
super(algorithm, encodeKeyBytes(rawKeyBytes, keyType)); | |||
super(algorithm, CryptoBytesEncoding.encodeKeyBytes(rawKeyBytes, keyType)); | |||
} | |||
protected BaseCryptoKey(CryptoAlgorithm algorithm, byte[] rawKeyBytes, CryptoKeyType keyType) { | |||
super(algorithm, encodeKeyBytes(rawKeyBytes, keyType)); | |||
super(algorithm, CryptoBytesEncoding.encodeKeyBytes(rawKeyBytes, keyType)); | |||
} | |||
public BaseCryptoKey(byte[] cryptoBytes) { | |||
super(cryptoBytes); | |||
CryptoKeyType keyType = decodeKeyType(getRawCryptoBytes()); | |||
CryptoKeyType keyType = CryptoBytesEncoding.decodeKeyType(getRawCryptoBytes()); | |||
if (getKeyType() != keyType) { | |||
throw new CryptoException("CryptoKey doesn't support keyType[" + keyType + "]!"); | |||
} | |||
} | |||
private static byte[] encodeKeyBytes(byte[] rawKeyBytes, CryptoKeyType keyType) { | |||
return BytesUtils.concat(new byte[] { keyType.CODE }, rawKeyBytes); | |||
} | |||
private static CryptoKeyType decodeKeyType(BytesSlice cryptoBytes) { | |||
return CryptoKeyType.valueOf(cryptoBytes.getByte()); | |||
} | |||
@Override | |||
protected boolean support(short algorithm) { | |||
return CryptoAlgorithm.hasAsymmetricKey(algorithm) || CryptoAlgorithm.hasSymmetricKey(algorithm); | |||
@@ -0,0 +1,27 @@ | |||
package com.jd.blockchain.crypto; | |||
import com.jd.blockchain.utils.io.BytesSlice; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
public final class CryptoBytesEncoding { | |||
static byte[] encodeBytes(short algorithm, byte[] rawCryptoBytes) { | |||
return BytesUtils.concat(BytesUtils.toBytes(algorithm), rawCryptoBytes); | |||
} | |||
static byte[] encodeBytes(CryptoAlgorithm algorithm, byte[] rawCryptoBytes) { | |||
return BytesUtils.concat(CryptoAlgorithm.toBytes(algorithm), rawCryptoBytes); | |||
} | |||
public static short decodeAlgorithm(byte[] cryptoBytes) { | |||
return CryptoAlgorithm.resolveCode(cryptoBytes); | |||
} | |||
static byte[] encodeKeyBytes(byte[] rawKeyBytes, CryptoKeyType keyType) { | |||
return BytesUtils.concat(new byte[] { keyType.CODE }, rawKeyBytes); | |||
} | |||
static CryptoKeyType decodeKeyType(BytesSlice cryptoBytes) { | |||
return CryptoKeyType.valueOf(cryptoBytes.getByte()); | |||
} | |||
} |
@@ -1,7 +1,30 @@ | |||
package com.jd.blockchain.crypto; | |||
/** | |||
* CryptoFunction represents the cryptographic function of a particular | |||
* algorithm; | |||
* | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
public interface CryptoFunction { | |||
/** | |||
* The cryptographic algorithm supported by this CryptoFunction; | |||
* | |||
* @return | |||
*/ | |||
CryptoAlgorithm getAlgorithm(); | |||
// /** | |||
// * Resolve ciphertext from byte array to CyptoBytes object, and check if its | |||
// * algorithm matches this function. | |||
// * | |||
// * @param bytes | |||
// * ciphertext | |||
// * @return Return {@link CryptoBytes} object, or throw {@link CryptoException} | |||
// * if its algorithm does not match. | |||
// */ | |||
// CryptoBytes resolveCryptoBytes(byte[] bytes); | |||
} |
@@ -15,8 +15,9 @@ import java.util.concurrent.RecursiveTask; | |||
import java.util.concurrent.atomic.AtomicLong; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoUtils; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.HashFunction; | |||
import com.jd.blockchain.ledger.CryptoSetting; | |||
import com.jd.blockchain.storage.service.ExPolicyKVStorage; | |||
import com.jd.blockchain.storage.service.ExPolicyKVStorage.ExPolicy; | |||
@@ -488,6 +489,8 @@ public class MerkleTree implements Transactional { | |||
*/ | |||
private static class RehashTask extends RecursiveTask<List<PathNode>> { | |||
private static final long serialVersionUID = -9165021733321713070L; | |||
private MerkleTree tree; | |||
private PathNode[] pathNodes; | |||
@@ -1082,7 +1085,7 @@ public class MerkleTree implements Transactional { | |||
/** | |||
* 当前节点采用的 hash 算法; | |||
*/ | |||
private CryptoAlgorithm hashAlgorithm; | |||
private short hashAlgorithm; | |||
/** | |||
* 节点的起始序列号; | |||
@@ -1132,6 +1135,8 @@ public class MerkleTree implements Transactional { | |||
/** | |||
* 创建一个路径节点; | |||
* | |||
* @param hashAlgorithm | |||
* 生成节点采用的哈希算法; | |||
* @param startingSN | |||
* 路径节点表示的子树的起始序列号; | |||
* @param level | |||
@@ -1143,12 +1148,21 @@ public class MerkleTree implements Transactional { | |||
this(hashAlgorithm, startingSN, level, dataCount, new HashDigest[TREE_DEGREE], null); | |||
} | |||
private PathNode(short hashAlgorithm, long startingSN, int level, long dataCount) { | |||
this(hashAlgorithm, startingSN, level, dataCount, new HashDigest[TREE_DEGREE], null); | |||
} | |||
private PathNode(long startingSN, int level, long dataCount, HashDigest[] childrenHashes, HashDigest nodeHash) { | |||
this(nodeHash.getAlgorithm(), startingSN, level, dataCount, childrenHashes, nodeHash); | |||
} | |||
private PathNode(CryptoAlgorithm defaultHashAlgorithm, long startingSN, int level, long dataCount, | |||
HashDigest[] childrenHashes, HashDigest nodeHash) { | |||
this(defaultHashAlgorithm.code(), startingSN, level, dataCount, childrenHashes, nodeHash); | |||
} | |||
private PathNode(short hashAlgorithm, long startingSN, int level, long dataCount, HashDigest[] childrenHashes, | |||
HashDigest nodeHash) { | |||
if (startingSN < 0) { | |||
throw new IllegalArgumentException("The specified starting sn of PathNode is negative!"); | |||
} | |||
@@ -1158,7 +1172,7 @@ public class MerkleTree implements Transactional { | |||
if (dataCount < 0) { | |||
throw new IllegalArgumentException("The specified data count of PathNode is negative!"); | |||
} | |||
this.hashAlgorithm = defaultHashAlgorithm; | |||
this.hashAlgorithm = hashAlgorithm; | |||
this.startingSN = startingSN; | |||
this.level = level; | |||
@@ -1344,7 +1358,7 @@ public class MerkleTree implements Transactional { | |||
h = new byte[hashSize]; | |||
System.arraycopy(bytes, offset, h, 0, hashSize); | |||
offset += hashSize; | |||
childrenHashes[i] = CryptoUtils.hashCrypto().resolveHashDigest(h); | |||
childrenHashes[i] = new HashDigest(h); | |||
} | |||
int hashSize = NumberMask.TINY.resolveMaskedNumber(bytes, offset); | |||
@@ -1354,7 +1368,7 @@ public class MerkleTree implements Transactional { | |||
System.arraycopy(bytes, offset, nodeHashBytes, 0, hashSize); | |||
offset += hashSize; | |||
HashDigest nodeHash = CryptoUtils.hashCrypto().resolveHashDigest(nodeHashBytes); | |||
HashDigest nodeHash = new HashDigest(nodeHashBytes); | |||
PathNode node = new PathNode(startingSN, level, dataCount, childrenHashes, nodeHash); | |||
if (checkHash) { | |||
@@ -1393,7 +1407,8 @@ public class MerkleTree implements Transactional { | |||
int totalSize = getBodySize(); | |||
byte[] bodyBytes = new byte[totalSize]; | |||
generateBodyBytes(bodyBytes); | |||
return CryptoUtils.hash(hashAlgorithm).hash(bodyBytes); | |||
HashFunction hashFunc = CryptoServiceProviders.getHashFunction(hashAlgorithm); | |||
return hashFunc.hash(bodyBytes); | |||
} | |||
} | |||
@@ -1450,7 +1465,8 @@ public class MerkleTree implements Transactional { | |||
byte[] dataBytes = BytesUtils.concat(bodyBytes, hashedData); | |||
HashDigest dataHash = CryptoUtils.hash(hashAlgorithm).hash(dataBytes); | |||
HashFunction hashFunc = CryptoServiceProviders.getHashFunction(hashAlgorithm); | |||
HashDigest dataHash = hashFunc.hash(dataBytes); | |||
int hashMaskSize = NumberMask.TINY.getMaskLength(dataHash.size()); | |||
int dataNodeSize = bodySize + hashMaskSize + dataHash.size(); | |||
@@ -1573,7 +1589,7 @@ public class MerkleTree implements Transactional { | |||
byte[] dataHashBytes = new byte[hashSize]; | |||
System.arraycopy(bytes, offset, dataHashBytes, 0, hashSize); | |||
offset += hashSize; | |||
HashDigest dataHash = CryptoUtils.hashCrypto().resolveHashDigest(dataHashBytes); | |||
HashDigest dataHash = new HashDigest(dataHashBytes); | |||
return new DataNode(sn, key, version, dataHash, bytes); | |||
} | |||
@@ -1,6 +1,5 @@ | |||
package com.jd.blockchain.ledger.core; | |||
import com.jd.blockchain.crypto.CryptoUtils; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.ledger.UserInfo; | |||
@@ -45,7 +44,7 @@ public class UserAccount implements UserInfo { | |||
if (pkBytes == null) { | |||
return null; | |||
} | |||
return CryptoUtils.crypto().asymmetricCryptography().resolvePubKey(pkBytes); | |||
return new PubKey(pkBytes); | |||
} | |||
public long setDataPubKey(PubKey pubKey) { | |||
@@ -75,8 +74,8 @@ public class UserAccount implements UserInfo { | |||
} | |||
private Bytes encodePropertyKey(Bytes key) { | |||
// return key.concatTo(USER_INFO_PREFIX); | |||
return USER_INFO_PREFIX.concat(key); | |||
// return key.concatTo(USER_INFO_PREFIX); | |||
return USER_INFO_PREFIX.concat(key); | |||
} | |||
} |
@@ -2,7 +2,6 @@ package com.jd.blockchain.ledger.core.impl; | |||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.CryptoUtils; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.HashFunction; | |||
import com.jd.blockchain.ledger.BlockBody; | |||
@@ -72,7 +71,7 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
private volatile LedgerState latestState; | |||
private volatile LedgerEditor nextBlockEditor; | |||
private volatile boolean closed = false; | |||
public LedgerRepositoryImpl(HashDigest ledgerHash, String keyPrefix, ExPolicyKVStorage exPolicyStorage, | |||
@@ -178,7 +177,7 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
if (hashBytes == null || hashBytes.length == 0) { | |||
return null; | |||
} | |||
return CryptoUtils.hashCrypto().resolveHashDigest(hashBytes); | |||
return new HashDigest(hashBytes); | |||
} | |||
@Override | |||
@@ -402,8 +401,7 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
@Override | |||
public synchronized LedgerEditor createNextBlock() { | |||
if (closed) { | |||
throw new LedgerException( | |||
"Ledger repository has been closed!"); | |||
throw new LedgerException("Ledger repository has been closed!"); | |||
} | |||
if (this.nextBlockEditor != null) { | |||
throw new LedgerException( | |||
@@ -422,15 +420,14 @@ public class LedgerRepositoryImpl implements LedgerRepository { | |||
public LedgerEditor getNextBlockEditor() { | |||
return nextBlockEditor; | |||
} | |||
@Override | |||
public synchronized void close() { | |||
if (closed) { | |||
return; | |||
} | |||
if (this.nextBlockEditor != null) { | |||
throw new LedgerException( | |||
"A new block is in process, cann't close the ledger repository!"); | |||
throw new LedgerException("A new block is in process, cann't close the ledger repository!"); | |||
} | |||
closed = true; | |||
} | |||
@@ -6,9 +6,8 @@ import static org.junit.Assert.assertTrue; | |||
import org.junit.Test; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.core.AccountSet; | |||
@@ -22,32 +21,32 @@ public class AccountSetTest { | |||
@Test | |||
public void test() { | |||
OpeningAccessPolicy accessPolicy = new OpeningAccessPolicy(); | |||
MemoryKVStorage storage = new MemoryKVStorage(); | |||
CryptoConfig cryptoConf = new CryptoConfig(); | |||
cryptoConf.setAutoVerifyHash(true); | |||
cryptoConf.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
String keyPrefix = ""; | |||
AccountSet accset = new AccountSet(cryptoConf,keyPrefix, storage, storage, accessPolicy); | |||
AccountSet accset = new AccountSet(cryptoConf, keyPrefix, storage, storage, accessPolicy); | |||
BlockchainKeypair userKey = BlockchainKeyGenerator.getInstance().generate(); | |||
accset.register(userKey.getAddress(), userKey.getPubKey()); | |||
BaseAccount userAcc = accset.getAccount(userKey.getAddress()); | |||
assertNotNull(userAcc); | |||
assertTrue(accset.contains(userKey.getAddress())); | |||
accset.commit(); | |||
HashDigest rootHash = accset.getRootHash(); | |||
assertNotNull(rootHash); | |||
AccountSet reloadAccSet = new AccountSet(rootHash, cryptoConf, keyPrefix,storage, storage, true, accessPolicy); | |||
AccountSet reloadAccSet = new AccountSet(rootHash, cryptoConf, keyPrefix, storage, storage, true, accessPolicy); | |||
BaseAccount reloadUserAcc = reloadAccSet.getAccount(userKey.getAddress()); | |||
assertNotNull(reloadUserAcc); | |||
assertTrue(reloadAccSet.contains(userKey.getAddress())); | |||
assertEquals(userAcc.getAddress(), reloadUserAcc.getAddress()); | |||
assertEquals(userAcc.getPubKey(), reloadUserAcc.getPubKey()); | |||
} | |||
@@ -4,10 +4,8 @@ import static org.junit.Assert.assertEquals; | |||
import static org.junit.Assert.assertFalse; | |||
import org.junit.Test; | |||
import org.springframework.util.StringUtils; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.core.BaseAccount; | |||
@@ -31,7 +29,7 @@ public class BaseAccountTest { | |||
CryptoConfig cryptoConf = new CryptoConfig(); | |||
cryptoConf.setAutoVerifyHash(true); | |||
cryptoConf.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConf.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
OpeningAccessPolicy accPlc = new OpeningAccessPolicy(); | |||
@@ -1,60 +1,61 @@ | |||
package test.com.jd.blockchain.ledger; | |||
import static org.junit.Assert.assertEquals; | |||
import java.util.Random; | |||
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.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.sm.SMCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.crypto.service.sm.SMAlgorithm; | |||
import com.jd.blockchain.ledger.AccountHeader; | |||
import com.jd.blockchain.ledger.UserInfo; | |||
import com.jd.blockchain.ledger.core.AccountSet; | |||
import com.jd.blockchain.utils.Bytes; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import java.util.Random; | |||
import static org.junit.Assert.assertEquals; | |||
/** | |||
* Created by zhangshuang3 on 2018/9/3. | |||
*/ | |||
public class LedgerAccountTest { | |||
byte[] seed = null; | |||
byte[] settingValue = null; | |||
byte[] rawDigestBytes = null; | |||
@Before | |||
public void initCfg() throws Exception{ | |||
Random rand = new Random(); | |||
seed = new byte[8]; | |||
settingValue = new byte[8]; | |||
rawDigestBytes = new byte[8]; | |||
rand.nextBytes(seed); | |||
rand.nextBytes(settingValue); | |||
rand.nextBytes(rawDigestBytes); | |||
DataContractRegistry.register(AccountHeader.class); | |||
DataContractRegistry.register(UserInfo.class); | |||
} | |||
@Test | |||
public void testSerialize_AccountHeader() { | |||
String address = "xxxxxxxxxxxx"; | |||
PubKey pubKey = new PubKey(SMCryptoService.SM2_ALGORITHM, rawDigestBytes); | |||
HashDigest hashDigest = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, rawDigestBytes); | |||
AccountSet.AccountHeaderData accountHeaderData = new AccountSet.AccountHeaderData(Bytes.fromString(address), pubKey, hashDigest); | |||
//encode and decode | |||
byte[] encodeBytes = BinaryEncodingUtils.encode(accountHeaderData, AccountHeader.class); | |||
AccountHeader deAccountHeaderData = BinaryEncodingUtils.decode(encodeBytes); | |||
//verify start | |||
assertEquals(accountHeaderData.getAddress(), deAccountHeaderData.getAddress()); | |||
assertEquals(accountHeaderData.getPubKey(), deAccountHeaderData.getPubKey()); | |||
assertEquals(accountHeaderData.getRootHash(), deAccountHeaderData.getRootHash()); | |||
} | |||
byte[] seed = null; | |||
byte[] settingValue = null; | |||
byte[] rawDigestBytes = null; | |||
@Before | |||
public void initCfg() throws Exception { | |||
Random rand = new Random(); | |||
seed = new byte[8]; | |||
settingValue = new byte[8]; | |||
rawDigestBytes = new byte[8]; | |||
rand.nextBytes(seed); | |||
rand.nextBytes(settingValue); | |||
rand.nextBytes(rawDigestBytes); | |||
DataContractRegistry.register(AccountHeader.class); | |||
DataContractRegistry.register(UserInfo.class); | |||
} | |||
@Test | |||
public void testSerialize_AccountHeader() { | |||
String address = "xxxxxxxxxxxx"; | |||
PubKey pubKey = new PubKey(SMAlgorithm.SM2, rawDigestBytes); | |||
HashDigest hashDigest = new HashDigest(ClassicAlgorithm.SHA256, rawDigestBytes); | |||
AccountSet.AccountHeaderData accountHeaderData = new AccountSet.AccountHeaderData(Bytes.fromString(address), | |||
pubKey, hashDigest); | |||
// encode and decode | |||
byte[] encodeBytes = BinaryEncodingUtils.encode(accountHeaderData, AccountHeader.class); | |||
AccountHeader deAccountHeaderData = BinaryEncodingUtils.decode(encodeBytes); | |||
// verify start | |||
assertEquals(accountHeaderData.getAddress(), deAccountHeaderData.getAddress()); | |||
assertEquals(accountHeaderData.getPubKey(), deAccountHeaderData.getPubKey()); | |||
assertEquals(accountHeaderData.getRootHash(), deAccountHeaderData.getRootHash()); | |||
} | |||
} |
@@ -12,9 +12,8 @@ import java.util.Random; | |||
import org.junit.Test; | |||
import com.jd.blockchain.crypto.AddressEncoding; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.ParticipantNode; | |||
@@ -58,7 +57,7 @@ public class LedgerAdminAccountTest { | |||
CryptoConfig cryptoSetting = new CryptoConfig(); | |||
cryptoSetting.setAutoVerifyHash(true); | |||
cryptoSetting.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
initSetting.setCryptoSetting(cryptoSetting); | |||
byte[] ledgerSeed = new byte[16]; | |||
@@ -157,7 +156,7 @@ public class LedgerAdminAccountTest { | |||
assertNotNull(rlmeta.getSetting()); | |||
assertTrue(expMeta.getSetting().getConsensusSetting().equals(rlmeta.getSetting().getConsensusSetting())); | |||
assertEquals(expMeta.getSetting().getConsensusProvider(), rlmeta.getSetting().getConsensusProvider()); | |||
assertEquals(expMeta.getSetting().getCryptoSetting().getAutoVerifyHash(), | |||
rlmeta.getSetting().getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(expMeta.getSetting().getCryptoSetting().getHashAlgorithm(), | |||
@@ -172,7 +171,8 @@ public class LedgerAdminAccountTest { | |||
ParticipantNode rlParti = actualPaticipants[i]; | |||
assertEquals(expParties[i].getAddress(), rlParti.getAddress()); | |||
assertEquals(expParties[i].getName(), rlParti.getName()); | |||
// assertEquals(expParties[i].getConsensusAddress(), rlParti.getConsensusAddress()); | |||
// assertEquals(expParties[i].getConsensusAddress(), | |||
// rlParti.getConsensusAddress()); | |||
assertEquals(expParties[i].getPubKey(), rlParti.getPubKey()); | |||
} | |||
} | |||
@@ -16,7 +16,7 @@ import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.LedgerBlock; | |||
import com.jd.blockchain.ledger.LedgerDataSnapshot; | |||
import com.jd.blockchain.ledger.core.impl.LedgerBlockData; | |||
@@ -31,84 +31,90 @@ import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | |||
public class LedgerBlockImplTest { | |||
private LedgerBlockData data; | |||
@Before | |||
public void initLedgerBlockImpl() { | |||
DataContractRegistry.register(LedgerBlock.class); | |||
DataContractRegistry.register(LedgerDataSnapshot.class); | |||
long height = 9999L; | |||
HashDigest ledgerHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes()); | |||
HashDigest previousHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes()); | |||
data = new LedgerBlockData(height, ledgerHash, previousHash); | |||
data.setHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes())); | |||
data.setTransactionSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes())); | |||
// 设置LedgerDataSnapshot相关属性 | |||
data.setAdminAccountHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd1".getBytes())); | |||
data.setDataAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd2".getBytes())); | |||
data.setUserAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd3".getBytes())); | |||
data.setContractAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd4".getBytes())); | |||
} | |||
@Test | |||
public void testSerialize_LedgerBlock() throws Exception { | |||
byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerBlock.class); | |||
LedgerBlock resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
System.out.println("------Assert start ------"); | |||
assertEquals(resolvedData.getHash(), data.getHash()); | |||
assertEquals(resolvedData.getHeight(), data.getHeight()); | |||
assertEquals(resolvedData.getLedgerHash(), data.getLedgerHash()); | |||
assertEquals(resolvedData.getPreviousHash(), data.getPreviousHash()); | |||
assertEquals(resolvedData.getTransactionSetHash(), data.getTransactionSetHash()); | |||
assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
System.out.println("------Assert OK ------"); | |||
} | |||
//notice: LedgerBlock interface has more field info than LedgerDataSnapshot interface, so cannot deserialize LedgerBlock | |||
// with LedgerDataSnapshot encode | |||
//@Test | |||
//public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
//byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerDataSnapshot.class); | |||
//LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes, null, | |||
//LedgerBlockData.class); | |||
//System.out.println("------Assert start ------"); | |||
//assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
//assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
//assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
//assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
//assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
//System.out.println("------Assert OK ------"); | |||
//} | |||
@Test | |||
public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
TransactionStagedSnapshot transactionStagedSnapshot = new TransactionStagedSnapshot(); | |||
HashDigest admin = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "alice".getBytes()); | |||
HashDigest contract = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "bob".getBytes()); | |||
HashDigest data = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jerry".getBytes()); | |||
HashDigest user = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "tom".getBytes()); | |||
transactionStagedSnapshot.setAdminAccountHash(admin); | |||
transactionStagedSnapshot.setContractAccountSetHash(contract); | |||
transactionStagedSnapshot.setDataAccountSetHash(data); | |||
transactionStagedSnapshot.setUserAccountSetHash(user); | |||
byte[] serialBytes = BinaryEncodingUtils.encode(transactionStagedSnapshot, LedgerDataSnapshot.class); | |||
LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
//verify start | |||
assertEquals(resolvedData.getAdminAccountHash(), transactionStagedSnapshot.getAdminAccountHash()); | |||
assertEquals(resolvedData.getContractAccountSetHash(), transactionStagedSnapshot.getContractAccountSetHash()); | |||
assertEquals(resolvedData.getDataAccountSetHash(), transactionStagedSnapshot.getDataAccountSetHash()); | |||
assertEquals(resolvedData.getUserAccountSetHash(), transactionStagedSnapshot.getUserAccountSetHash()); | |||
//verify succeed | |||
} | |||
private LedgerBlockData data; | |||
@Before | |||
public void initLedgerBlockImpl() { | |||
DataContractRegistry.register(LedgerBlock.class); | |||
DataContractRegistry.register(LedgerDataSnapshot.class); | |||
long height = 9999L; | |||
HashDigest ledgerHash = new HashDigest(ClassicAlgorithm.SHA256, "zhangsan".getBytes()); | |||
HashDigest previousHash = new HashDigest(ClassicAlgorithm.SHA256, "lisi".getBytes()); | |||
data = new LedgerBlockData(height, ledgerHash, previousHash); | |||
data.setHash(new HashDigest(ClassicAlgorithm.SHA256, "wangwu".getBytes())); | |||
data.setTransactionSetHash(new HashDigest(ClassicAlgorithm.SHA256, "zhaoliu".getBytes())); | |||
// 设置LedgerDataSnapshot相关属性 | |||
data.setAdminAccountHash(new HashDigest(ClassicAlgorithm.SHA256, "jd1".getBytes())); | |||
data.setDataAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "jd2".getBytes())); | |||
data.setUserAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "jd3".getBytes())); | |||
data.setContractAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "jd4".getBytes())); | |||
} | |||
@Test | |||
public void testSerialize_LedgerBlock() throws Exception { | |||
byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerBlock.class); | |||
LedgerBlock resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
System.out.println("------Assert start ------"); | |||
assertEquals(resolvedData.getHash(), data.getHash()); | |||
assertEquals(resolvedData.getHeight(), data.getHeight()); | |||
assertEquals(resolvedData.getLedgerHash(), data.getLedgerHash()); | |||
assertEquals(resolvedData.getPreviousHash(), data.getPreviousHash()); | |||
assertEquals(resolvedData.getTransactionSetHash(), data.getTransactionSetHash()); | |||
assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
System.out.println("------Assert OK ------"); | |||
} | |||
// notice: LedgerBlock interface has more field info than LedgerDataSnapshot | |||
// interface, so cannot deserialize LedgerBlock | |||
// with LedgerDataSnapshot encode | |||
// @Test | |||
// public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
// byte[] serialBytes = BinaryEncodingUtils.encode(data, | |||
// LedgerDataSnapshot.class); | |||
// LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes, | |||
// null, | |||
// LedgerBlockData.class); | |||
// System.out.println("------Assert start ------"); | |||
// assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
// assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
// assertEquals(resolvedData.getContractAccountSetHash(), | |||
// data.getContractAccountSetHash()); | |||
// assertEquals(resolvedData.getDataAccountSetHash(), | |||
// data.getDataAccountSetHash()); | |||
// assertEquals(resolvedData.getUserAccountSetHash(), | |||
// data.getUserAccountSetHash()); | |||
// System.out.println("------Assert OK ------"); | |||
// } | |||
@Test | |||
public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
TransactionStagedSnapshot transactionStagedSnapshot = new TransactionStagedSnapshot(); | |||
HashDigest admin = new HashDigest(ClassicAlgorithm.SHA256, "alice".getBytes()); | |||
HashDigest contract = new HashDigest(ClassicAlgorithm.SHA256, "bob".getBytes()); | |||
HashDigest data = new HashDigest(ClassicAlgorithm.SHA256, "jerry".getBytes()); | |||
HashDigest user = new HashDigest(ClassicAlgorithm.SHA256, "tom".getBytes()); | |||
transactionStagedSnapshot.setAdminAccountHash(admin); | |||
transactionStagedSnapshot.setContractAccountSetHash(contract); | |||
transactionStagedSnapshot.setDataAccountSetHash(data); | |||
transactionStagedSnapshot.setUserAccountSetHash(user); | |||
byte[] serialBytes = BinaryEncodingUtils.encode(transactionStagedSnapshot, LedgerDataSnapshot.class); | |||
LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
// verify start | |||
assertEquals(resolvedData.getAdminAccountHash(), transactionStagedSnapshot.getAdminAccountHash()); | |||
assertEquals(resolvedData.getContractAccountSetHash(), transactionStagedSnapshot.getContractAccountSetHash()); | |||
assertEquals(resolvedData.getDataAccountSetHash(), transactionStagedSnapshot.getDataAccountSetHash()); | |||
assertEquals(resolvedData.getUserAccountSetHash(), transactionStagedSnapshot.getUserAccountSetHash()); | |||
// verify succeed | |||
} | |||
} |
@@ -12,7 +12,7 @@ import com.jd.blockchain.crypto.AddressEncoding; | |||
import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.SignatureFunction; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.LedgerBlock; | |||
import com.jd.blockchain.ledger.LedgerInitSetting; | |||
@@ -59,6 +59,7 @@ public class LedgerEditerTest { | |||
AsymmetricKeypair cryptoKeyPair = signatureFunction.generateKeypair(); | |||
@SuppressWarnings("unused") | |||
@Test | |||
public void testWriteDataAccoutKvOp() { | |||
@@ -108,10 +109,10 @@ public class LedgerEditerTest { | |||
private LedgerInitSetting createLedgerInitSetting() { | |||
SignatureFunction signFunc = CryptoServiceProviders.getSignatureFunction("ED25519"); | |||
CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
defCryptoSetting.setAutoVerifyHash(true); | |||
defCryptoSetting.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
LedgerInitSettingData initSetting = new LedgerInitSettingData(); | |||
@@ -1,10 +1,18 @@ | |||
package test.com.jd.blockchain.ledger; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
import java.util.Arrays; | |||
import java.util.Random; | |||
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.AddressEncoding; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.LedgerInitOperation; | |||
@@ -16,112 +24,116 @@ import com.jd.blockchain.ledger.data.LedgerInitOpTemplate; | |||
import com.jd.blockchain.ledger.data.LedgerInitSettingData; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.net.NetworkAddress; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import java.util.Arrays; | |||
import java.util.Random; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
public class LedgerInitOperationTest { | |||
byte[] seed = null; | |||
byte[] csSysSettingBytes = null; | |||
LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
@Before | |||
public void initCfg() { | |||
DataContractRegistry.register(LedgerInitSetting.class); | |||
DataContractRegistry.register(LedgerInitOperation.class); | |||
Random rand = new Random(); | |||
seed = new byte[8]; | |||
rand.nextBytes(seed); | |||
csSysSettingBytes = new byte[64]; | |||
rand.nextBytes(csSysSettingBytes); | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
ledgerInitSettingData.setConsensusSettings(new Bytes(csSysSettingBytes)); | |||
ledgerInitSettingData.setConsensusProvider("cons-provider"); | |||
ledgerInitSettingData.setLedgerSeed(seed); | |||
ledgerInitSettingData.setCryptoSetting(cryptoConfig); | |||
} | |||
@Test | |||
public void test_LedgerInitOperation_ConsensusParticipantData() { | |||
ConsensusParticipantData[] parties = new ConsensusParticipantData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ConsensusParticipantData(); | |||
// parties[i].setId(i); | |||
parties[i].setAddress(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58()); | |||
parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
parties[i].setName("Participant[" + i + "]"); | |||
parties[i].setPubKey(keys[i].getPubKey()); | |||
} | |||
ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
LedgerInitOpTemplate template = new LedgerInitOpTemplate(ledgerInitSettingData); | |||
byte[] encode = BinaryEncodingUtils.encode(template, LedgerInitOperation.class); | |||
LedgerInitOperation decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0 ; i < template.getInitSetting().getConsensusParticipants().length; i++) { | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getAddress(), decode.getInitSetting().getConsensusParticipants()[i].getAddress()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getName(), decode.getInitSetting().getConsensusParticipants()[i].getName()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getPubKey(), decode.getInitSetting().getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(template.getInitSetting().getLedgerSeed(), decode.getInitSetting().getLedgerSeed()); | |||
assertArrayEquals(template.getInitSetting().getConsensusSettings().toBytes(), decode.getInitSetting().getConsensusSettings().toBytes()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getHashAlgorithm(), decode.getInitSetting().getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getAutoVerifyHash(), decode.getInitSetting().getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(template.getInitSetting().getConsensusProvider(), decode.getInitSetting().getConsensusProvider()); | |||
} | |||
@Test | |||
public void test_LedgerInitOperation_ParticipantCertData() { | |||
ParticipantCertData[] parties = new ParticipantCertData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58(), "Participant[" + i + "]", keys[i].getPubKey()); | |||
} | |||
ParticipantCertData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
LedgerInitOpTemplate template = new LedgerInitOpTemplate(ledgerInitSettingData); | |||
byte[] encode = BinaryEncodingUtils.encode(template, LedgerInitOperation.class); | |||
LedgerInitOperation decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0 ; i < template.getInitSetting().getConsensusParticipants().length; i++) { | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getAddress(), decode.getInitSetting().getConsensusParticipants()[i].getAddress()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getName(), decode.getInitSetting().getConsensusParticipants()[i].getName()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getPubKey(), decode.getInitSetting().getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(template.getInitSetting().getLedgerSeed(), decode.getInitSetting().getLedgerSeed()); | |||
assertArrayEquals(template.getInitSetting().getConsensusSettings().toBytes(), decode.getInitSetting().getConsensusSettings().toBytes()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getHashAlgorithm(), decode.getInitSetting().getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getAutoVerifyHash(), decode.getInitSetting().getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(template.getInitSetting().getConsensusProvider(), decode.getInitSetting().getConsensusProvider()); | |||
} | |||
byte[] seed = null; | |||
byte[] csSysSettingBytes = null; | |||
LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
@Before | |||
public void initCfg() { | |||
DataContractRegistry.register(LedgerInitSetting.class); | |||
DataContractRegistry.register(LedgerInitOperation.class); | |||
Random rand = new Random(); | |||
seed = new byte[8]; | |||
rand.nextBytes(seed); | |||
csSysSettingBytes = new byte[64]; | |||
rand.nextBytes(csSysSettingBytes); | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
ledgerInitSettingData.setConsensusSettings(new Bytes(csSysSettingBytes)); | |||
ledgerInitSettingData.setConsensusProvider("cons-provider"); | |||
ledgerInitSettingData.setLedgerSeed(seed); | |||
ledgerInitSettingData.setCryptoSetting(cryptoConfig); | |||
} | |||
@Test | |||
public void test_LedgerInitOperation_ConsensusParticipantData() { | |||
ConsensusParticipantData[] parties = new ConsensusParticipantData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ConsensusParticipantData(); | |||
// parties[i].setId(i); | |||
parties[i].setAddress(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58()); | |||
parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
parties[i].setName("Participant[" + i + "]"); | |||
parties[i].setPubKey(keys[i].getPubKey()); | |||
} | |||
ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
LedgerInitOpTemplate template = new LedgerInitOpTemplate(ledgerInitSettingData); | |||
byte[] encode = BinaryEncodingUtils.encode(template, LedgerInitOperation.class); | |||
LedgerInitOperation decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0; i < template.getInitSetting().getConsensusParticipants().length; i++) { | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getAddress(), | |||
decode.getInitSetting().getConsensusParticipants()[i].getAddress()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getName(), | |||
decode.getInitSetting().getConsensusParticipants()[i].getName()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getPubKey(), | |||
decode.getInitSetting().getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(template.getInitSetting().getLedgerSeed(), decode.getInitSetting().getLedgerSeed()); | |||
assertArrayEquals(template.getInitSetting().getConsensusSettings().toBytes(), | |||
decode.getInitSetting().getConsensusSettings().toBytes()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getHashAlgorithm(), | |||
decode.getInitSetting().getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getAutoVerifyHash(), | |||
decode.getInitSetting().getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(template.getInitSetting().getConsensusProvider(), decode.getInitSetting().getConsensusProvider()); | |||
} | |||
@Test | |||
public void test_LedgerInitOperation_ParticipantCertData() { | |||
ParticipantCertData[] parties = new ParticipantCertData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58(), | |||
"Participant[" + i + "]", keys[i].getPubKey()); | |||
} | |||
ParticipantCertData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
LedgerInitOpTemplate template = new LedgerInitOpTemplate(ledgerInitSettingData); | |||
byte[] encode = BinaryEncodingUtils.encode(template, LedgerInitOperation.class); | |||
LedgerInitOperation decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0; i < template.getInitSetting().getConsensusParticipants().length; i++) { | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getAddress(), | |||
decode.getInitSetting().getConsensusParticipants()[i].getAddress()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getName(), | |||
decode.getInitSetting().getConsensusParticipants()[i].getName()); | |||
assertEquals(template.getInitSetting().getConsensusParticipants()[i].getPubKey(), | |||
decode.getInitSetting().getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(template.getInitSetting().getLedgerSeed(), decode.getInitSetting().getLedgerSeed()); | |||
assertArrayEquals(template.getInitSetting().getConsensusSettings().toBytes(), | |||
decode.getInitSetting().getConsensusSettings().toBytes()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getHashAlgorithm(), | |||
decode.getInitSetting().getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(template.getInitSetting().getCryptoSetting().getAutoVerifyHash(), | |||
decode.getInitSetting().getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(template.getInitSetting().getConsensusProvider(), decode.getInitSetting().getConsensusProvider()); | |||
} | |||
} |
@@ -1,13 +1,20 @@ | |||
package test.com.jd.blockchain.ledger; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
import java.util.Arrays; | |||
import java.util.Random; | |||
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.AddressEncoding; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.LedgerInitOperation; | |||
import com.jd.blockchain.ledger.LedgerInitSetting; | |||
import com.jd.blockchain.ledger.core.CryptoConfig; | |||
import com.jd.blockchain.ledger.core.ParticipantCertData; | |||
@@ -16,120 +23,119 @@ import com.jd.blockchain.ledger.data.LedgerInitOpTemplate; | |||
import com.jd.blockchain.ledger.data.LedgerInitSettingData; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.net.NetworkAddress; | |||
import org.junit.Before; | |||
import org.junit.Test; | |||
import java.util.Arrays; | |||
import java.util.Random; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertEquals; | |||
public class LedgerInitSettingTest { | |||
byte[] seed = null; | |||
byte[] csSysSettingBytes = null; | |||
LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
LedgerInitOpTemplate template = new LedgerInitOpTemplate(); | |||
@Before | |||
public void initCfg() { | |||
DataContractRegistry.register(LedgerInitSetting.class); | |||
Random rand = new Random(); | |||
seed = new byte[8]; | |||
rand.nextBytes(seed); | |||
csSysSettingBytes = new byte[64]; | |||
rand.nextBytes(csSysSettingBytes); | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
ledgerInitSettingData.setConsensusSettings(new Bytes(csSysSettingBytes)); | |||
ledgerInitSettingData.setConsensusProvider("cons-provider"); | |||
ledgerInitSettingData.setLedgerSeed(seed); | |||
byte[] seed = null; | |||
byte[] csSysSettingBytes = null; | |||
LedgerInitSettingData ledgerInitSettingData = new LedgerInitSettingData(); | |||
LedgerInitOpTemplate template = new LedgerInitOpTemplate(); | |||
ledgerInitSettingData.setCryptoSetting(cryptoConfig); | |||
} | |||
@Test | |||
public void test_ledgerinitsetting_ConsensusParticipantData() { | |||
ConsensusParticipantData[] parties = new ConsensusParticipantData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ConsensusParticipantData(); | |||
// parties[i].setId(i); | |||
parties[i].setAddress(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58()); | |||
parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
parties[i].setName("Participant[" + i + "]"); | |||
parties[i].setPubKey(keys[i].getPubKey()); | |||
} | |||
ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
byte[] encode = BinaryEncodingUtils.encode(ledgerInitSettingData, LedgerInitSetting.class); | |||
LedgerInitSetting decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0 ; i < ledgerInitSettingData.getConsensusParticipants().length; i++) { | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getAddress(), decode.getConsensusParticipants()[i].getAddress()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getName(), decode.getConsensusParticipants()[i].getName()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getPubKey(), decode.getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(ledgerInitSettingData.getLedgerSeed(), decode.getLedgerSeed()); | |||
assertArrayEquals(ledgerInitSettingData.getConsensusSettings().toBytes(), decode.getConsensusSettings().toBytes()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getHashAlgorithm(), decode.getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getAutoVerifyHash(), decode.getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(ledgerInitSettingData.getConsensusProvider(), decode.getConsensusProvider()); | |||
} | |||
// @Test | |||
// public void test_ledgerinitsetting_ConsensusParticipantConfig() { | |||
// } | |||
@Test | |||
public void test_ledgerinitsetting_ParticipantCertData() { | |||
ParticipantCertData[] parties = new ParticipantCertData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58(), "Participant[" + i + "]", keys[i].getPubKey()); | |||
} | |||
ParticipantCertData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
byte[] encode = BinaryEncodingUtils.encode(ledgerInitSettingData, LedgerInitSetting.class); | |||
LedgerInitSetting decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0 ; i < ledgerInitSettingData.getConsensusParticipants().length; i++) { | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getAddress(), decode.getConsensusParticipants()[i].getAddress()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getName(), decode.getConsensusParticipants()[i].getName()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getPubKey(), decode.getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(ledgerInitSettingData.getLedgerSeed(), decode.getLedgerSeed()); | |||
assertArrayEquals(ledgerInitSettingData.getConsensusSettings().toBytes(), decode.getConsensusSettings().toBytes()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getHashAlgorithm(), decode.getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getAutoVerifyHash(), decode.getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(ledgerInitSettingData.getConsensusProvider(), decode.getConsensusProvider()); | |||
} | |||
@Before | |||
public void initCfg() { | |||
DataContractRegistry.register(LedgerInitSetting.class); | |||
Random rand = new Random(); | |||
seed = new byte[8]; | |||
rand.nextBytes(seed); | |||
csSysSettingBytes = new byte[64]; | |||
rand.nextBytes(csSysSettingBytes); | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
ledgerInitSettingData.setConsensusSettings(new Bytes(csSysSettingBytes)); | |||
ledgerInitSettingData.setConsensusProvider("cons-provider"); | |||
ledgerInitSettingData.setLedgerSeed(seed); | |||
ledgerInitSettingData.setCryptoSetting(cryptoConfig); | |||
} | |||
@Test | |||
public void test_ledgerinitsetting_ConsensusParticipantData() { | |||
ConsensusParticipantData[] parties = new ConsensusParticipantData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ConsensusParticipantData(); | |||
// parties[i].setId(i); | |||
parties[i].setAddress(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58()); | |||
parties[i].setHostAddress(new NetworkAddress("192.168.10." + (10 + i), 10010 + 10 * i)); | |||
parties[i].setName("Participant[" + i + "]"); | |||
parties[i].setPubKey(keys[i].getPubKey()); | |||
} | |||
ConsensusParticipantData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
byte[] encode = BinaryEncodingUtils.encode(ledgerInitSettingData, LedgerInitSetting.class); | |||
LedgerInitSetting decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0; i < ledgerInitSettingData.getConsensusParticipants().length; i++) { | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getAddress(), | |||
decode.getConsensusParticipants()[i].getAddress()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getName(), | |||
decode.getConsensusParticipants()[i].getName()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getPubKey(), | |||
decode.getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(ledgerInitSettingData.getLedgerSeed(), decode.getLedgerSeed()); | |||
assertArrayEquals(ledgerInitSettingData.getConsensusSettings().toBytes(), | |||
decode.getConsensusSettings().toBytes()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getHashAlgorithm(), | |||
decode.getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getAutoVerifyHash(), | |||
decode.getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(ledgerInitSettingData.getConsensusProvider(), decode.getConsensusProvider()); | |||
} | |||
// @Test | |||
// public void test_ledgerinitsetting_ConsensusParticipantConfig() { | |||
// } | |||
@Test | |||
public void test_ledgerinitsetting_ParticipantCertData() { | |||
ParticipantCertData[] parties = new ParticipantCertData[4]; | |||
BlockchainKeypair[] keys = new BlockchainKeypair[parties.length]; | |||
for (int i = 0; i < parties.length; i++) { | |||
keys[i] = BlockchainKeyGenerator.getInstance().generate(); | |||
parties[i] = new ParticipantCertData(AddressEncoding.generateAddress(keys[i].getPubKey()).toBase58(), | |||
"Participant[" + i + "]", keys[i].getPubKey()); | |||
} | |||
ParticipantCertData[] parties1 = Arrays.copyOf(parties, 4); | |||
ledgerInitSettingData.setConsensusParticipants(parties1); | |||
byte[] encode = BinaryEncodingUtils.encode(ledgerInitSettingData, LedgerInitSetting.class); | |||
LedgerInitSetting decode = BinaryEncodingUtils.decode(encode); | |||
for (int i = 0; i < ledgerInitSettingData.getConsensusParticipants().length; i++) { | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getAddress(), | |||
decode.getConsensusParticipants()[i].getAddress()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getName(), | |||
decode.getConsensusParticipants()[i].getName()); | |||
assertEquals(ledgerInitSettingData.getConsensusParticipants()[i].getPubKey(), | |||
decode.getConsensusParticipants()[i].getPubKey()); | |||
} | |||
assertArrayEquals(ledgerInitSettingData.getLedgerSeed(), decode.getLedgerSeed()); | |||
assertArrayEquals(ledgerInitSettingData.getConsensusSettings().toBytes(), | |||
decode.getConsensusSettings().toBytes()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getHashAlgorithm(), | |||
decode.getCryptoSetting().getHashAlgorithm()); | |||
assertEquals(ledgerInitSettingData.getCryptoSetting().getAutoVerifyHash(), | |||
decode.getCryptoSetting().getAutoVerifyHash()); | |||
assertEquals(ledgerInitSettingData.getConsensusProvider(), decode.getConsensusProvider()); | |||
} | |||
} | |||
@@ -15,7 +15,7 @@ import com.jd.blockchain.crypto.AsymmetricKeypair; | |||
import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.SignatureFunction; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockBody; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
@@ -172,7 +172,7 @@ public class LedgerManagerTest { | |||
private LedgerInitSetting createLedgerInitSetting() { | |||
CryptoConfig defCryptoSetting = new CryptoConfig(); | |||
defCryptoSetting.setAutoVerifyHash(true); | |||
defCryptoSetting.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
defCryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
LedgerInitSettingData initSetting = new LedgerInitSettingData(); | |||
@@ -15,7 +15,7 @@ import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.crypto.AddressEncoding; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.CryptoSetting; | |||
import com.jd.blockchain.ledger.ParticipantNode; | |||
import com.jd.blockchain.ledger.core.CryptoConfig; | |||
@@ -58,7 +58,7 @@ public class LedgerMetaDataTest { | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
LedgerConfiguration ledgerConfiguration = new LedgerConfiguration(consensusProvider, | |||
new Bytes(consensusSettingBytes), cryptoConfig); | |||
@@ -67,7 +67,7 @@ public class LedgerMetaDataTest { | |||
ledgerMetadata.setSeed(seed); | |||
ledgerMetadata.setSetting(ledgerConfiguration); | |||
HashDigest hashDigest = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, rawDigestBytes); | |||
HashDigest hashDigest = new HashDigest(ClassicAlgorithm.SHA256, rawDigestBytes); | |||
ledgerMetadata.setParticipantsHash(hashDigest); | |||
// encode and decode | |||
@@ -95,9 +95,10 @@ public class LedgerMetaDataTest { | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
LedgerConfiguration ledgerConfiguration = new LedgerConfiguration(consensusProvider, new Bytes(csSettingsBytes), cryptoConfig); | |||
LedgerConfiguration ledgerConfiguration = new LedgerConfiguration(consensusProvider, new Bytes(csSettingsBytes), | |||
cryptoConfig); | |||
byte[] encodeBytes = BinaryEncodingUtils.encode(ledgerConfiguration, LedgerSetting.class); | |||
LedgerSetting deLedgerConfiguration = BinaryEncodingUtils.decode(encodeBytes); | |||
// verify start | |||
@@ -134,7 +135,7 @@ public class LedgerMetaDataTest { | |||
// LedgerCodes.METADATA_LEDGER_SETTING_CRYPTO | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
byte[] encodeBytes = BinaryEncodingUtils.encode(cryptoConfig, CryptoSetting.class); | |||
CryptoSetting deCryptoConfig = BinaryEncodingUtils.decode(encodeBytes); | |||
@@ -150,7 +151,7 @@ public class LedgerMetaDataTest { | |||
// prepare work | |||
int id = 1; | |||
// String address = "xxxxxxxxxxxxxx"; | |||
PubKey pubKey = new PubKey(ClassicCryptoService.ED25519_ALGORITHM, rawDigestBytes); | |||
PubKey pubKey = new PubKey(ClassicAlgorithm.ED25519, rawDigestBytes); | |||
// ParticipantInfo info = new ParticipantCertData.ParticipantInfoData(1, "yyy"); | |||
// SignatureDigest signature = new SignatureDigest(CryptoAlgorithm.SM2, | |||
// rawDigestBytes); | |||
@@ -7,7 +7,7 @@ import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.crypto.SignatureFunction; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainIdentityData; | |||
import com.jd.blockchain.ledger.CryptoSetting; | |||
import com.jd.blockchain.ledger.PreparedTransaction; | |||
@@ -78,7 +78,7 @@ public class LedgerTestUtils { | |||
public static CryptoSetting createDefaultCryptoSetting() { | |||
CryptoConfig cryptoSetting = new CryptoConfig(); | |||
cryptoSetting.setAutoVerifyHash(true); | |||
cryptoSetting.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoSetting.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
return cryptoSetting; | |||
} | |||
@@ -21,7 +21,7 @@ import com.jd.blockchain.crypto.CryptoServiceProviders; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.crypto.SignatureDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.BlockchainKeyGenerator; | |||
import com.jd.blockchain.ledger.BlockchainKeypair; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
@@ -70,11 +70,11 @@ public class LedgerTransactionDataTest { | |||
data = new LedgerTransactionData(blockHeight, txRequestMessage, TransactionState.SUCCESS, | |||
initTransactionStagedSnapshot()); | |||
HashDigest hash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes()); | |||
HashDigest adminAccountHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes()); | |||
HashDigest userAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes()); | |||
HashDigest dataAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes()); | |||
HashDigest contractAccountSetHash = new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "sunqi".getBytes()); | |||
HashDigest hash = new HashDigest(ClassicAlgorithm.SHA256, "zhangsan".getBytes()); | |||
HashDigest adminAccountHash = new HashDigest(ClassicAlgorithm.SHA256, "lisi".getBytes()); | |||
HashDigest userAccountSetHash = new HashDigest(ClassicAlgorithm.SHA256, "wangwu".getBytes()); | |||
HashDigest dataAccountSetHash = new HashDigest(ClassicAlgorithm.SHA256, "zhaoliu".getBytes()); | |||
HashDigest contractAccountSetHash = new HashDigest(ClassicAlgorithm.SHA256, "sunqi".getBytes()); | |||
data.setHash(hash); | |||
// data.setBlockHeight(blockHeight); | |||
@@ -215,35 +215,31 @@ public class LedgerTransactionDataTest { | |||
private TransactionStagedSnapshot initTransactionStagedSnapshot() { | |||
TransactionStagedSnapshot transactionStagedSnapshot = new TransactionStagedSnapshot(); | |||
transactionStagedSnapshot | |||
.setAdminAccountHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes())); | |||
transactionStagedSnapshot | |||
.setContractAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes())); | |||
transactionStagedSnapshot | |||
.setDataAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes())); | |||
transactionStagedSnapshot | |||
.setUserAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes())); | |||
transactionStagedSnapshot.setAdminAccountHash(new HashDigest(ClassicAlgorithm.SHA256, "zhangsan".getBytes())); | |||
transactionStagedSnapshot.setContractAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "lisi".getBytes())); | |||
transactionStagedSnapshot.setDataAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "wangwu".getBytes())); | |||
transactionStagedSnapshot.setUserAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "zhaoliu".getBytes())); | |||
return transactionStagedSnapshot; | |||
} | |||
private TxRequestMessage initTxRequestMessage() throws Exception { | |||
TxRequestMessage txRequestMessage = new TxRequestMessage(initTransactionContent()); | |||
SignatureDigest digest1 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "zhangsan".getBytes()); | |||
SignatureDigest digest2 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "lisi".getBytes()); | |||
SignatureDigest digest1 = new SignatureDigest(ClassicAlgorithm.ED25519, "zhangsan".getBytes()); | |||
SignatureDigest digest2 = new SignatureDigest(ClassicAlgorithm.ED25519, "lisi".getBytes()); | |||
DigitalSignatureBlob endPoint1 = new DigitalSignatureBlob( | |||
new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd1.com".getBytes()), digest1); | |||
new PubKey(ClassicAlgorithm.ED25519, "jd1.com".getBytes()), digest1); | |||
DigitalSignatureBlob endPoint2 = new DigitalSignatureBlob( | |||
new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd2.com".getBytes()), digest2); | |||
new PubKey(ClassicAlgorithm.ED25519, "jd2.com".getBytes()), digest2); | |||
txRequestMessage.addEndpointSignatures(endPoint1); | |||
txRequestMessage.addEndpointSignatures(endPoint2); | |||
SignatureDigest digest3 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "wangwu".getBytes()); | |||
SignatureDigest digest4 = new SignatureDigest(ClassicCryptoService.ED25519_ALGORITHM, "zhaoliu".getBytes()); | |||
SignatureDigest digest3 = new SignatureDigest(ClassicAlgorithm.ED25519, "wangwu".getBytes()); | |||
SignatureDigest digest4 = new SignatureDigest(ClassicAlgorithm.ED25519, "zhaoliu".getBytes()); | |||
DigitalSignatureBlob node1 = new DigitalSignatureBlob( | |||
new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd3.com".getBytes()), digest3); | |||
new PubKey(ClassicAlgorithm.ED25519, "jd3.com".getBytes()), digest3); | |||
DigitalSignatureBlob node2 = new DigitalSignatureBlob( | |||
new PubKey(ClassicCryptoService.ED25519_ALGORITHM, "jd4.com".getBytes()), digest4); | |||
new PubKey(ClassicAlgorithm.ED25519, "jd4.com".getBytes()), digest4); | |||
txRequestMessage.addNodeSignatures(node1); | |||
txRequestMessage.addNodeSignatures(node2); | |||
@@ -252,12 +248,12 @@ public class LedgerTransactionDataTest { | |||
private TransactionContent initTransactionContent() throws Exception { | |||
TxContentBlob contentBlob = null; | |||
BlockchainKeypair id = BlockchainKeyGenerator.getInstance().generate(ClassicCryptoService.ED25519_ALGORITHM); | |||
BlockchainKeypair id = BlockchainKeyGenerator.getInstance().generate(ClassicAlgorithm.ED25519); | |||
HashDigest ledgerHash = CryptoServiceProviders.getHashFunction("SHA256") | |||
.hash(UUID.randomUUID().toString().getBytes("UTF-8")); | |||
BlockchainOperationFactory opFactory = new BlockchainOperationFactory(); | |||
contentBlob = new TxContentBlob(ledgerHash); | |||
contentBlob.setHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "jd.com".getBytes())); | |||
contentBlob.setHash(new HashDigest(ClassicAlgorithm.SHA256, "jd.com".getBytes())); | |||
// contentBlob.setSubjectAccount(id.getAddress()); | |||
// contentBlob.setSequenceNumber(1); | |||
DataAccountKVSetOperation kvsetOP = opFactory.dataAccount(id.getAddress()) | |||
@@ -15,7 +15,7 @@ import java.util.Set; | |||
import org.junit.Test; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.core.CryptoConfig; | |||
import com.jd.blockchain.ledger.core.MerkleDataSet; | |||
import com.jd.blockchain.ledger.core.MerkleProof; | |||
@@ -33,7 +33,7 @@ public class MerkleDataSetTest { | |||
public void testStorageIncreasement() { | |||
String keyPrefix = ""; | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
MemoryKVStorage storage = new MemoryKVStorage(); | |||
@@ -64,7 +64,7 @@ public class MerkleDataSetTest { | |||
mds.commit(); | |||
// New key added, include 1 versioning kv, 1 sn key, 2 merkle nodes; | |||
// String[] keys = StringUtils.toStringArray(storage.keySet()); | |||
// String[] keys = StringUtils.toStringArray(storage.keySet()); | |||
Bytes[] keys = storage.keySet().toArray(new Bytes[0]); | |||
expStorageCount = expStorageCount + 1 + 1 + 2; | |||
assertEquals(expStorageCount, storage.getStorageCount()); | |||
@@ -117,7 +117,7 @@ public class MerkleDataSetTest { | |||
Random rand = new Random(); | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
MemoryKVStorage storage = new MemoryKVStorage(); | |||
@@ -282,7 +282,7 @@ public class MerkleDataSetTest { | |||
Random rand = new Random(); | |||
CryptoConfig cryptoConfig = new CryptoConfig(); | |||
cryptoConfig.setHashAlgorithm(ClassicCryptoService.SHA256_ALGORITHM); | |||
cryptoConfig.setHashAlgorithm(ClassicAlgorithm.SHA256); | |||
cryptoConfig.setAutoVerifyHash(true); | |||
MemoryKVStorage storage = new MemoryKVStorage(); | |||
@@ -15,7 +15,7 @@ import org.junit.Test; | |||
import org.mockito.Mockito; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.CryptoSetting; | |||
import com.jd.blockchain.ledger.core.MerkleDataNode; | |||
import com.jd.blockchain.ledger.core.MerkleNode; | |||
@@ -34,7 +34,7 @@ public class MerkleTreeTest { | |||
Random rand = new Random(); | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 测试从空的树开始,顺序增加数据节点; | |||
@@ -85,7 +85,7 @@ public class MerkleTreeTest { | |||
@Test | |||
public void testSequenceInsert_OneCommit() { | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 测试从空的树开始,顺序增加数据节点; | |||
@@ -139,7 +139,7 @@ public class MerkleTreeTest { | |||
@Test | |||
public void testSequenceInsert_MultiCommit() { | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 测试从空的树开始,顺序增加数据节点; | |||
@@ -319,7 +319,7 @@ public class MerkleTreeTest { | |||
@Test | |||
public void testRandomInsert_MultiCommit() { | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 保存所有写入的数据节点的 SN-Hash 映射表; | |||
@@ -409,7 +409,7 @@ public class MerkleTreeTest { | |||
@Test | |||
public void testDataModify() { | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 保存所有写入的数据节点的 SN-Hash 映射表; | |||
@@ -492,7 +492,7 @@ public class MerkleTreeTest { | |||
@Test | |||
public void testDataVersionModify() { | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 保存所有写入的数据节点的 SN-Hash 映射表; | |||
@@ -559,7 +559,7 @@ public class MerkleTreeTest { | |||
@Test | |||
public void testMerkleReload() { | |||
CryptoSetting setting = Mockito.mock(CryptoSetting.class); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicCryptoService.SHA256_ALGORITHM); | |||
when(setting.getHashAlgorithm()).thenReturn(ClassicAlgorithm.SHA256); | |||
when(setting.getAutoVerifyHash()).thenReturn(true); | |||
// 保存所有写入的数据节点的 SN-Hash 映射表; | |||
@@ -737,6 +737,7 @@ public class MerkleTreeTest { | |||
} | |||
} | |||
@SuppressWarnings("unused") | |||
private static int getLevel(long dataCount) { | |||
if (dataCount < 0) { | |||
throw new IllegalArgumentException("The specified data count is negative!"); | |||
@@ -16,7 +16,7 @@ import org.junit.Test; | |||
import com.jd.blockchain.binaryproto.BinaryEncodingUtils; | |||
import com.jd.blockchain.binaryproto.DataContractRegistry; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.service.classic.ClassicCryptoService; | |||
import com.jd.blockchain.crypto.service.classic.ClassicAlgorithm; | |||
import com.jd.blockchain.ledger.LedgerDataSnapshot; | |||
import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | |||
@@ -29,27 +29,27 @@ import com.jd.blockchain.ledger.core.impl.TransactionStagedSnapshot; | |||
public class TransactionStagedSnapshotTest { | |||
private TransactionStagedSnapshot data; | |||
@Before | |||
public void initTransactionStagedSnapshot() { | |||
DataContractRegistry.register(LedgerDataSnapshot.class); | |||
data = new TransactionStagedSnapshot(); | |||
data.setAdminAccountHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhangsan".getBytes())); | |||
data.setContractAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "lisi".getBytes())); | |||
data.setDataAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "wangwu".getBytes())); | |||
data.setUserAccountSetHash(new HashDigest(ClassicCryptoService.SHA256_ALGORITHM, "zhaoliu".getBytes())); | |||
} | |||
@Test | |||
public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerDataSnapshot.class); | |||
LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
System.out.println("------Assert start ------"); | |||
assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
System.out.println("------Assert OK ------"); | |||
} | |||
private TransactionStagedSnapshot data; | |||
@Before | |||
public void initTransactionStagedSnapshot() { | |||
DataContractRegistry.register(LedgerDataSnapshot.class); | |||
data = new TransactionStagedSnapshot(); | |||
data.setAdminAccountHash(new HashDigest(ClassicAlgorithm.SHA256, "zhangsan".getBytes())); | |||
data.setContractAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "lisi".getBytes())); | |||
data.setDataAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "wangwu".getBytes())); | |||
data.setUserAccountSetHash(new HashDigest(ClassicAlgorithm.SHA256, "zhaoliu".getBytes())); | |||
} | |||
@Test | |||
public void testSerialize_LedgerDataSnapshot() throws Exception { | |||
byte[] serialBytes = BinaryEncodingUtils.encode(data, LedgerDataSnapshot.class); | |||
LedgerDataSnapshot resolvedData = BinaryEncodingUtils.decode(serialBytes); | |||
System.out.println("------Assert start ------"); | |||
assertEquals(resolvedData.getAdminAccountHash(), data.getAdminAccountHash()); | |||
assertEquals(resolvedData.getContractAccountSetHash(), data.getContractAccountSetHash()); | |||
assertEquals(resolvedData.getDataAccountSetHash(), data.getDataAccountSetHash()); | |||
assertEquals(resolvedData.getUserAccountSetHash(), data.getUserAccountSetHash()); | |||
System.out.println("------Assert OK ------"); | |||
} | |||
} |
@@ -15,11 +15,10 @@ import com.jd.blockchain.binaryproto.DConstructor; | |||
import com.jd.blockchain.binaryproto.FieldSetter; | |||
import com.jd.blockchain.crypto.AddressEncoding; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoKey; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.ledger.data.CryptoKeyEncoding; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.ByteArray; | |||
import com.jd.blockchain.utils.io.BytesEncoding; | |||
import com.jd.blockchain.utils.io.BytesReader; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
import com.jd.blockchain.utils.io.BytesWriter; | |||
@@ -67,19 +66,16 @@ public class BlockchainIdentityData implements BytesWriter, BytesReader, Externa | |||
@Override | |||
public void resolvFrom(InputStream in) throws IOException { | |||
Bytes addr = AddressEncoding.readAddress(in); | |||
CryptoKey pubKey = CryptoKeyEncoding.readKey(in); | |||
if (!(pubKey instanceof PubKey)) { | |||
throw new IllegalArgumentException( | |||
"Expected public key but private key was resolved from the InputStream!"); | |||
} | |||
byte[] value = BytesEncoding.readInShort(in); | |||
PubKey pk = new PubKey(value); | |||
this.address = addr; | |||
this.pubKey = (PubKey) pubKey; | |||
this.pubKey = pk; | |||
} | |||
@Override | |||
public void writeTo(OutputStream out) throws IOException { | |||
AddressEncoding.writeAddress(address, out); | |||
CryptoKeyEncoding.writeKey(pubKey, out); | |||
BytesEncoding.writeInShort(pubKey.toBytes(), out); | |||
} | |||
/* (non-Javadoc) | |||
@@ -1,46 +1,46 @@ | |||
package com.jd.blockchain.ledger; | |||
/** | |||
* Hash 算法的代码常量; | |||
* | |||
* @author zhaoming9 | |||
* | |||
*/ | |||
public enum HashAlgorithm { | |||
RIPE160((byte) 1), | |||
SHA256((byte) 2), | |||
SM3((byte) 4); | |||
public final byte CODE; | |||
private HashAlgorithm(byte algorithm) { | |||
CODE = algorithm; | |||
} | |||
public byte getAlgorithm() { | |||
return CODE; | |||
} | |||
public static HashAlgorithm valueOf(byte algorithm) { | |||
for (HashAlgorithm hashAlgorithm : HashAlgorithm.values()) { | |||
if (hashAlgorithm.CODE == algorithm) { | |||
return hashAlgorithm; | |||
} | |||
} | |||
throw new IllegalArgumentException("Unsupported hash algorithm [" + algorithm + "]!"); | |||
} | |||
public static void checkHashAlgorithm(HashAlgorithm algorithm) { | |||
switch (algorithm) { | |||
case RIPE160: | |||
break; | |||
case SHA256: | |||
break; | |||
default: | |||
throw new IllegalArgumentException("Unsupported hash algorithm [" + algorithm + "]!"); | |||
} | |||
} | |||
} | |||
//package com.jd.blockchain.ledger; | |||
// | |||
///** | |||
// * Hash 算法的代码常量; | |||
// * | |||
// * @author zhaoming9 | |||
// * | |||
// */ | |||
//public enum HashAlgorithm { | |||
// | |||
// RIPE160((byte) 1), | |||
// | |||
// SHA256((byte) 2), | |||
// | |||
// SM3((byte) 4); | |||
// | |||
// public final byte CODE; | |||
// | |||
// private HashAlgorithm(byte algorithm) { | |||
// CODE = algorithm; | |||
// } | |||
// | |||
// public byte getAlgorithm() { | |||
// return CODE; | |||
// } | |||
// | |||
// public static HashAlgorithm valueOf(byte algorithm) { | |||
// for (HashAlgorithm hashAlgorithm : HashAlgorithm.values()) { | |||
// if (hashAlgorithm.CODE == algorithm) { | |||
// return hashAlgorithm; | |||
// } | |||
// } | |||
// throw new IllegalArgumentException("Unsupported hash algorithm [" + algorithm + "]!"); | |||
// } | |||
// | |||
// public static void checkHashAlgorithm(HashAlgorithm algorithm) { | |||
// switch (algorithm) { | |||
// case RIPE160: | |||
// break; | |||
// case SHA256: | |||
// break; | |||
// default: | |||
// throw new IllegalArgumentException("Unsupported hash algorithm [" + algorithm + "]!"); | |||
// } | |||
// } | |||
//} |
@@ -1,12 +1,11 @@ | |||
package com.jd.blockchain.ledger; | |||
import com.jd.blockchain.binaryproto.DataContract; | |||
import com.jd.blockchain.binaryproto.DataField; | |||
import com.jd.blockchain.consts.TypeCodes; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
/** | |||
* HashObject 由一个哈希摘要唯一地标识对象; | |||
* HashObject 表示以“哈希值”作为唯一标识的对象; | |||
* | |||
* @author huanghaiquan | |||
* | |||
@@ -1,7 +1,6 @@ | |||
package com.jd.blockchain.ledger; | |||
import java.io.ByteArrayInputStream; | |||
import java.io.InputStream; | |||
import java.io.UnsupportedEncodingException; | |||
import java.math.BigInteger; | |||
import java.util.Date; | |||
@@ -51,15 +51,15 @@ public class MagicNumber { | |||
*/ | |||
public static final byte SIGNATURE = 0x20; | |||
/** | |||
* 公钥标识; | |||
*/ | |||
public static final byte PUB_KEY = 0x21; | |||
/** | |||
* 私钥标识; | |||
*/ | |||
public static final byte PRIV_KEY = 0x22; | |||
// /** | |||
// * 公钥标识; | |||
// */ | |||
// public static final byte PUB_KEY = 0x21; | |||
// | |||
// /** | |||
// * 私钥标识; | |||
// */ | |||
// public static final byte PRIV_KEY = 0x22; | |||
@@ -3,7 +3,6 @@ package com.jd.blockchain.ledger; | |||
import com.jd.blockchain.binaryproto.DataContract; | |||
import com.jd.blockchain.binaryproto.DataField; | |||
import com.jd.blockchain.consts.TypeCodes; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
@DataContract(code = TypeCodes.REQUEST_NODE) | |||
public interface NodeRequest extends EndpointRequest { | |||
@@ -1,44 +0,0 @@ | |||
package com.jd.blockchain.ledger; | |||
import com.jd.blockchain.ledger.data.CryptoKeyEncoding; | |||
import com.jd.blockchain.utils.io.ByteArray; | |||
import java.io.Serializable; | |||
/** | |||
* 密钥; | |||
* | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
//public class PrivKey implements CryptoKey,Serializable { | |||
// | |||
// private CryptoKeyType type; | |||
// | |||
// private ByteArray value; | |||
// | |||
// public PrivKey(CryptoKeyType type, ByteArray value) { | |||
// this.type = type; | |||
// this.value = value; | |||
// } | |||
// | |||
// public CryptoKeyType getType() { | |||
// return type; | |||
// } | |||
// | |||
// public ByteArray getValue() { | |||
// return value; | |||
// } | |||
// | |||
// public ByteArray toBytes() { | |||
// return CryptoKeyEncoding.toBytes(this); | |||
// } | |||
// | |||
// @Override | |||
// public String toString() { | |||
// return toBytes().toString(); | |||
// } | |||
// | |||
// | |||
// | |||
//} |
@@ -1,50 +0,0 @@ | |||
package com.jd.blockchain.ledger; | |||
import com.jd.blockchain.ledger.data.CryptoKeyEncoding; | |||
import com.jd.blockchain.utils.io.ByteArray; | |||
/** | |||
* 密钥; | |||
* | |||
* @author huanghaiquan | |||
* | |||
*/ | |||
//public class PubKey implements CryptoKey { | |||
// | |||
// private CryptoKeyType type; | |||
// | |||
// private ByteArray value; | |||
// | |||
// public PubKey(CryptoKeyType type, ByteArray value) { | |||
// this.type = type; | |||
// this.value = value; | |||
// } | |||
// | |||
// public CryptoKeyType getType() { | |||
// return type; | |||
// } | |||
// | |||
// public ByteArray getValue() { | |||
// return value; | |||
// } | |||
// | |||
// public ByteArray toBytes() { | |||
// return CryptoKeyEncoding.toBytes(this); | |||
// } | |||
// | |||
// @Override | |||
// public String toString() { | |||
// return toBytes().toString(); | |||
// } | |||
// | |||
// @Override | |||
// public boolean equals(Object o) { | |||
// if (this == o) return true; | |||
// if (!(o instanceof PubKey)) return false; | |||
// | |||
// PubKey pubKey = (PubKey) o; | |||
// | |||
// if (getType() != pubKey.getType()) return false; | |||
// return getValue().equals(pubKey.getValue()); | |||
// } | |||
//} |
@@ -4,7 +4,6 @@ import java.util.ArrayList; | |||
import java.util.Collection; | |||
import java.util.List; | |||
import com.alibaba.fastjson.JSON; | |||
import com.jd.blockchain.ledger.BlockchainIdentity; | |||
import com.jd.blockchain.ledger.ContractCodeDeployOperation; | |||
import com.jd.blockchain.ledger.ContractEventSendOperation; | |||
@@ -1,129 +0,0 @@ | |||
package com.jd.blockchain.ledger.data; | |||
import java.io.ByteArrayOutputStream; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.OutputStream; | |||
import com.jd.blockchain.crypto.CryptoAlgorithm; | |||
import com.jd.blockchain.crypto.CryptoKey; | |||
import com.jd.blockchain.crypto.PrivKey; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.ledger.MagicNumber; | |||
import com.jd.blockchain.utils.io.ByteArray; | |||
import com.jd.blockchain.utils.io.BytesEncoding; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
import com.jd.blockchain.utils.io.NumberMask; | |||
import com.jd.blockchain.utils.io.RuntimeIOException; | |||
public class CryptoKeyEncoding { | |||
/** | |||
* @param key | |||
* @param out | |||
* @return 写入的字节数; | |||
*/ | |||
public static int writeKey(CryptoKey key, OutputStream out) { | |||
try { | |||
byte magicNum; | |||
if (key instanceof PubKey) { | |||
magicNum = MagicNumber.PUB_KEY; | |||
} else if (key instanceof PrivKey) { | |||
magicNum = MagicNumber.PRIV_KEY; | |||
} else { | |||
throw new IllegalArgumentException("Unsupported key type[" + key.getClass().getName() + "]!"); | |||
} | |||
out.write(magicNum); | |||
BytesUtils.writeShort(key.getAlgorithm(), out); | |||
int size = 3;// 已经写入 3 字节; | |||
size += BytesEncoding.write(key.getRawKeyBytes(), NumberMask.SHORT, out); | |||
return size; | |||
} catch (IOException e) { | |||
throw new RuntimeIOException(e.getMessage(), e); | |||
} | |||
} | |||
/** | |||
* @param in | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static CryptoKey readKey(InputStream in) { | |||
try { | |||
byte magicNum = (byte) in.read(); | |||
if (magicNum != MagicNumber.PUB_KEY && magicNum != MagicNumber.PRIV_KEY) { | |||
throw new IllegalArgumentException("The CryptoKey MagicNumber read from the InputStream is Illegal!"); | |||
} | |||
short algorithm = CryptoAlgorithm.resolveCode(in); | |||
ByteArray value = BytesEncoding.readAsByteArray(NumberMask.SHORT, in); | |||
if (magicNum == MagicNumber.PUB_KEY) { | |||
return new PubKey(algorithm, value.bytes()); | |||
} else { | |||
return new PrivKey(algorithm, value.bytes()); | |||
} | |||
} catch (IOException e) { | |||
throw new RuntimeIOException(e.getMessage(), e); | |||
} | |||
} | |||
/** | |||
* @param in | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static PubKey readPubKey(InputStream in) { | |||
try { | |||
byte magicNum = (byte) in.read(); | |||
if (magicNum != MagicNumber.PUB_KEY) { | |||
throw new IllegalArgumentException("The PubKey MagicNumber read from the InputStream is Illegal!"); | |||
} | |||
short algorithm = CryptoAlgorithm.resolveCode(in); | |||
ByteArray value = BytesEncoding.readAsByteArray(NumberMask.SHORT, in); | |||
return new PubKey(algorithm, value.bytes()); | |||
} catch (IOException e) { | |||
throw new RuntimeIOException(e.getMessage(), e); | |||
} | |||
} | |||
/** | |||
* @param in | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static PrivKey readPrivKey(InputStream in) { | |||
try { | |||
byte magicNum = (byte) in.read(); | |||
if (magicNum != MagicNumber.PRIV_KEY) { | |||
throw new IllegalArgumentException("The PrivKey MagicNumber read from the InputStream is Illegal!"); | |||
} | |||
short algorithm = CryptoAlgorithm.resolveCode(in); | |||
ByteArray value = BytesEncoding.readAsByteArray(NumberMask.SHORT, in); | |||
return new PrivKey(algorithm, value.bytes()); | |||
} catch (IOException e) { | |||
throw new RuntimeIOException(e.getMessage(), e); | |||
} | |||
} | |||
public static ByteArray toBytes(CryptoKey key) { | |||
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |||
writeKey(key, out); | |||
byte[] keyBytes = out.toByteArray(); | |||
return ByteArray.wrap(keyBytes); | |||
} | |||
public static String toBase64(CryptoKey key) { | |||
return toBytes(key).toBase64(); | |||
} | |||
public static CryptoKey fromBase64(String hexKeyString) { | |||
return readKey(ByteArray.parseBase64(hexKeyString).asInputStream()); | |||
} | |||
public static CryptoKey fromBase58(String base58String) { | |||
return readKey(ByteArray.parseBase58(base58String).asInputStream()); | |||
} | |||
} |
@@ -1,12 +1,8 @@ | |||
package com.jd.blockchain.ledger.data; | |||
import com.alibaba.fastjson.JSON; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.ledger.DataType; | |||
import com.jd.blockchain.utils.Bytes; | |||
import java.util.Date; | |||
public interface DataAccountKVSetOperationBuilder { | |||
/** | |||
@@ -1,13 +1,12 @@ | |||
package com.jd.blockchain.ledger.data; | |||
import com.alibaba.fastjson.JSON; | |||
import com.jd.blockchain.ledger.BytesValue; | |||
import com.jd.blockchain.ledger.BytesValueImpl; | |||
import com.jd.blockchain.ledger.DataAccountKVSetOperation; | |||
import com.jd.blockchain.ledger.DataType; | |||
import com.jd.blockchain.utils.Bytes; | |||
import com.jd.blockchain.utils.io.BytesUtils; | |||
import com.jd.blockchain.utils.serialize.json.JSONSerializeUtils; | |||
public class DataAccountKVSetOperationBuilderImpl implements DataAccountKVSetOperationBuilder{ | |||
@@ -22,17 +21,6 @@ public class DataAccountKVSetOperationBuilderImpl implements DataAccountKVSetOpe | |||
return operation; | |||
} | |||
public static boolean isJson(String str) { | |||
boolean result = false; | |||
try { | |||
Object obj=JSON.parse(str); | |||
result = true; | |||
} catch (Exception e) { | |||
result=false; | |||
} | |||
return result; | |||
} | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, byte[] value, long expVersion) { | |||
BytesValue bytesValue = new BytesValueImpl(DataType.BYTES, value); | |||
@@ -43,7 +31,7 @@ public class DataAccountKVSetOperationBuilderImpl implements DataAccountKVSetOpe | |||
@Override | |||
public DataAccountKVSetOperationBuilder set(String key, String value, long expVersion) { | |||
BytesValue bytesValue; | |||
if (isJson(value)) { | |||
if (JSONSerializeUtils.isJSON(value)) { | |||
bytesValue = new BytesValueImpl(DataType.JSON, value.getBytes()); | |||
} | |||
else { | |||
@@ -1,7 +1,6 @@ | |||
package com.jd.blockchain.ledger.data; | |||
import com.jd.blockchain.crypto.HashDigest; | |||
import com.jd.blockchain.crypto.PubKey; | |||
import com.jd.blockchain.crypto.SignatureDigest; | |||
import com.jd.blockchain.ledger.DigitalSignature; | |||
@@ -1,58 +0,0 @@ | |||
package com.jd.blockchain.ledger.data; | |||
import java.io.IOException; | |||
import java.io.InputStream; | |||
import java.io.OutputStream; | |||
import com.jd.blockchain.ledger.HashAlgorithm; | |||
import com.jd.blockchain.utils.io.ByteArray; | |||
import com.jd.blockchain.utils.io.BytesEncoding; | |||
import com.jd.blockchain.utils.io.NumberMask; | |||
import com.jd.blockchain.utils.security.RipeMD160Utils; | |||
import com.jd.blockchain.utils.security.ShaUtils; | |||
public class HashEncoding { | |||
public static int write(byte[] hash, OutputStream out) { | |||
BytesEncoding.write(hash, NumberMask.TINY, out); | |||
return hash.length; | |||
} | |||
public static int write(ByteArray hash, OutputStream out) { | |||
BytesEncoding.write(hash, NumberMask.TINY, out); | |||
return hash.size(); | |||
} | |||
/** | |||
* @param in | |||
* @return | |||
* @throws IOException | |||
*/ | |||
public static ByteArray read(InputStream in) throws IOException { | |||
return BytesEncoding.readAsByteArray(NumberMask.TINY, in); | |||
} | |||
public static ByteArray computeHash(ByteArray content, HashAlgorithm algorithm) { | |||
return ByteArray.wrap(hash(content.bytes(), algorithm)); | |||
} | |||
public static ByteArray computeHash(byte[] contentBytes, HashAlgorithm algorithm) { | |||
return ByteArray.wrap(hash(contentBytes, algorithm)); | |||
} | |||
public static byte[] hash(byte[] contentBytes, HashAlgorithm algorithm) { | |||
byte[] hashBytes; | |||
switch (algorithm) { | |||
case RIPE160: | |||
hashBytes = RipeMD160Utils.hash(contentBytes); | |||
break; | |||
case SHA256: | |||
hashBytes = ShaUtils.hash_256(contentBytes); | |||
break; | |||
default: | |||
throw new IllegalArgumentException("Unsupported hash algorithm [" + algorithm + "]!"); | |||
} | |||
return hashBytes; | |||
} | |||
} |
@@ -156,16 +156,16 @@ public class BytesEncoding { | |||
} | |||
} | |||
/** | |||
* 读取头部和内容; | |||
* @param headerMask headerMask | |||
* @param in in | |||
* @return byteArray | |||
* @throws IOException exception | |||
*/ | |||
public static ByteArray readAsByteArray(NumberMask headerMask, InputStream in) throws IOException { | |||
byte[] data = read(headerMask, in); | |||
return ByteArray.wrap(data); | |||
} | |||
// /** | |||
// * 读取头部和内容; | |||
// * @param headerMask headerMask | |||
// * @param in in | |||
// * @return byteArray | |||
// * @throws IOException exception | |||
// */ | |||
// public static ByteArray readAsByteArray(NumberMask headerMask, InputStream in) throws IOException { | |||
// byte[] data = read(headerMask, in); | |||
// return ByteArray.wrap(data); | |||
// } | |||
} |