Search in sources :

Example 1 with ECKeyPair

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;
    }
}
Also used : ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Example 2 with ECKeyPair

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;
    }
}
Also used : SM2PrivateKey(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PrivateKey) SM2PublicKey(org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2PublicKey) ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) BigInteger(java.math.BigInteger)

Example 3 with ECKeyPair

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);
    }
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Service(org.fisco.bcos.channel.client.Service) SecureRandom(java.security.SecureRandom) ChannelEthereumService(org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService) RateLimiter(com.google.common.util.concurrent.RateLimiter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.fisco.bcos.web3j.protocol.Web3j) ECDSASign(org.fisco.bcos.web3j.crypto.ECDSASign) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) Credentials(org.fisco.bcos.web3j.crypto.Credentials)

Example 4 with ECKeyPair

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);
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ECPublicKey(org.bouncycastle.jce.interfaces.ECPublicKey) PEMManager(org.fisco.bcos.channel.client.PEMManager) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) P12Manager(org.fisco.bcos.channel.client.P12Manager) ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair) BigInteger(java.math.BigInteger) Credentials(org.fisco.bcos.web3j.crypto.Credentials) Test(org.junit.Test)

Example 5 with ECKeyPair

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 ====");
    }
}
Also used : ECKeyPair(org.fisco.bcos.web3j.crypto.ECKeyPair)

Aggregations

ECKeyPair (org.fisco.bcos.web3j.crypto.ECKeyPair)13 Credentials (org.fisco.bcos.web3j.crypto.Credentials)6 BigInteger (java.math.BigInteger)5 KeyPair (java.security.KeyPair)4 ApplicationContext (org.springframework.context.ApplicationContext)3 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)3 PrivateKey (java.security.PrivateKey)2 PublicKey (java.security.PublicKey)2 ECPrivateKey (java.security.interfaces.ECPrivateKey)2 Service (org.fisco.bcos.channel.client.Service)2 SM2KeyGenerator (org.fisco.bcos.web3j.crypto.gm.sm2.crypto.asymmetric.SM2KeyGenerator)2 Web3j (org.fisco.bcos.web3j.protocol.Web3j)2 ChannelEthereumService (org.fisco.bcos.web3j.protocol.channel.ChannelEthereumService)2 TransactionReceipt (org.fisco.bcos.web3j.protocol.core.methods.response.TransactionReceipt)2 RateLimiter (com.google.common.util.concurrent.RateLimiter)1 SecureRandom (java.security.SecureRandom)1 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ECPublicKey (org.bouncycastle.jce.interfaces.ECPublicKey)1 P12Manager (org.fisco.bcos.channel.client.P12Manager)1