use of org.fisco.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.
the class GenCredential method create.
public static Credentials create(String privKey) {
try {
ECKeyPair keyPair = createKeyPair(privKey);
if (keyPair == null)
return null;
Credentials credentials = Credentials.create(keyPair);
return credentials;
} catch (Exception e) {
System.out.println("init credential from private key failed ");
logger.error("init credential from private key failed, error msg:" + e.getMessage());
return null;
}
}
use of org.fisco.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.
the class GenCredential method genEcPairFromKeyPair.
private static ECKeyPair genEcPairFromKeyPair(KeyPair keyPairData) {
try {
SM2PrivateKey vk = (SM2PrivateKey) keyPairData.getPrivate();
SM2PublicKey pk = (SM2PublicKey) keyPairData.getPublic();
final byte[] publicKey = pk.getEncoded();
final byte[] privateKey = vk.getEncoded();
BigInteger biPublic = new BigInteger(Hex.toHexString(publicKey), 16);
BigInteger biPrivate = new BigInteger(Hex.toHexString(privateKey), 16);
ECKeyPair keyPair = new ECKeyPair(biPrivate, biPublic);
return keyPair;
} catch (Exception e) {
logger.error("create ec_keypair of guomi failed, error msg:" + e.getMessage());
return null;
}
}
use of org.fisco.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.
the class PerformanceEvidenceVerify method main.
public static void main(String[] args) throws Exception {
try {
String groupId = args[3];
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
Service service = context.getBean(Service.class);
service.setGroupId(Integer.parseInt(groupId));
service.run();
System.out.println("Start Evidence test...");
System.out.println("===================================================================");
ChannelEthereumService channelEthereumService = new ChannelEthereumService();
channelEthereumService.setChannelService(service);
Web3AsyncThreadPoolSize.web3AsyncCorePoolSize = 3000;
Web3AsyncThreadPoolSize.web3AsyncPoolSize = 2000;
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);
Web3j web3 = Web3j.build(channelEthereumService, 15 * 100, scheduledExecutorService, Integer.parseInt(groupId));
Credentials credentials = GenCredential.create();
BigInteger gasPrice = new BigInteger("30000000");
BigInteger gasLimit = new BigInteger("30000000");
String command = args[0];
Integer count = 0;
Integer qps = 0;
switch(command) {
case "insert":
count = Integer.parseInt(args[1]);
qps = Integer.parseInt(args[2]);
break;
default:
System.out.println("Args: <insert> <Total> <QPS>");
}
ThreadPoolTaskExecutor threadPool = new ThreadPoolTaskExecutor();
threadPool.setCorePoolSize(200);
threadPool.setMaxPoolSize(500);
threadPool.setQueueCapacity(count);
threadPool.initialize();
System.out.println("Deploying Evidence contract...");
EvidenceVerify evidence = EvidenceVerify.deploy(web3, credentials, gasPrice, gasLimit).send();
PerformanceCollector collector = new PerformanceCollector();
collector.setTotal(count);
RateLimiter limiter = RateLimiter.create(qps);
Integer area = count / 10;
final Integer total = count;
ECDSASign signHandler = new ECDSASign();
ECKeyPair keyPair = Keys.createEcKeyPair();
System.out.println("Start test,total:" + count);
System.out.println("address:" + credentials.getAddress());
String signAddr = Keys.getAddress(keyPair);
System.out.println("standardCredential address:" + signAddr);
for (Integer i = 0; i < count; ++i) {
threadPool.execute(new Runnable() {
@Override
public void run() {
limiter.acquire();
PerformanceOkCallback callback = new PerformanceOkCallback();
callback.setCollector(collector);
try {
String evi = "test";
String evInfo = "test_info";
int random = new SecureRandom().nextInt(50000);
String eviId = String.valueOf(random);
// sign to evi
byte[] message = Hash.sha3(evi.getBytes());
Sign.SignatureData sign = signHandler.signMessage(evi.getBytes(), keyPair);
int v = sign.getV();
evidence.insertEvidence(evi, evInfo, eviId, signAddr, message, BigInteger.valueOf(v), sign.getR(), sign.getS(), callback);
} catch (Exception e) {
TransactionReceipt receipt = new TransactionReceipt();
receipt.setStatus("-1");
callback.onResponse(receipt);
logger.info(e.getMessage());
}
int current = sended.incrementAndGet();
if (current >= area && ((current % area) == 0)) {
System.out.println("Already sended: " + current + "/" + total + " transactions");
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
use of org.fisco.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.
the class AccountTest method accountTest.
@Test
public void accountTest() throws UnrecoverableKeyException, KeyStoreException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-keystore-sample.xml");
// test p12
P12Manager p12 = context.getBean(P12Manager.class);
ECKeyPair p12KeyPair = p12.getECKeyPair();
assertEquals(p12KeyPair.getPrivateKey().toString(16), PRIVATE_KEY);
assertEquals(p12KeyPair.getPublicKey().toString(16), PUBLIC_KEY);
ECPublicKey publicKey = (ECPublicKey) p12.getPublicKey();
byte[] publicKeyBytes = publicKey.getQ().getEncoded(false);
BigInteger publicKeyValue = new BigInteger(1, Arrays.copyOfRange(publicKeyBytes, 1, publicKeyBytes.length));
assertEquals(publicKeyValue.toString(16), PUBLIC_KEY);
Credentials credentials = Credentials.create(p12KeyPair);
assertEquals(credentials.getAddress(), ADDRESS);
// test pem
PEMManager pem = context.getBean(PEMManager.class);
ECKeyPair pemKeyPair = pem.getECKeyPair();
assertEquals(pemKeyPair.getPrivateKey().toString(16), PRIVATE_KEY);
assertEquals(pemKeyPair.getPublicKey().toString(16), PUBLIC_KEY);
Credentials credentialsPEM = Credentials.create(pemKeyPair);
assertEquals(credentialsPEM.getAddress(), ADDRESS);
}
use of org.fisco.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.
the class GenGmAccount method GenGuoMiKeyAndStore.
/**
* @author: fisco-dev
* @function: generate private/public key with GuoMi algorithm
* @param keyFile: file name used to store private/public/account info
*/
private static void GenGuoMiKeyAndStore(String keyFile) {
System.out.println("-------------------------------------------------------------------------------");
System.out.println("==========Generate (private key, public key, account) For Guomi randomly =======");
ECKeyPair keyPair = GenCredential.createGuomiKeyPair();
if (keyPair != null) {
// deduce account according to public key
String account = deduceAccountFromPublic(keyPair.getPublicKey());
while (account == null || keyPair == null) {
keyPair = GenCredential.createGuomiKeyPair();
account = deduceAccountFromPublic(keyPair.getPublicKey());
}
System.out.println("===Generated Account:");
System.out.println("* public key:" + (keyPair.getPublicKey().toString(16)));
System.out.println("* private key :" + (keyPair.getPrivateKey().toString(16)));
System.out.println("* account :" + account);
// save private/public/account result
System.out.println("");
System.out.println("==== SAVE PRIVATE/PUBLIC/ACCOUNT INFO ===");
KeyInfo keyInfo = new KeyInfo(keyPair.getPublicKey().toString(16), keyPair.getPrivateKey().toString(16), account);
int result = keyInfo.storeKeyInfo(keyFile);
if (result != RetCode.success)
System.out.println("xxx STORE PRIVATE/PUBLIC/ACCOUNT INFO FAILED xxx");
else
System.out.println("=== STORE PIVATE/PUBLIC/ACCOUNT SUCCEED ===");
System.out.println("-------------------------------------------------------------------------------");
} else {
System.out.println("==== generate private/public key with GuoMi algorithm failed ====");
}
}
Aggregations