Java Code Examples for org.fisco.bcos.web3j.crypto.Credentials#create()

The following examples show how to use org.fisco.bcos.web3j.crypto.Credentials#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GenCredential.java    From web3sdk with Apache License 2.0 6 votes vote down vote up
public static Credentials create() {
    try {
        ECKeyPair keyPair = createKeyPair();
        if (keyPair == null) return null;

        Credentials credentials = Credentials.create(keyPair);
        logger.debug(
                " privateKey: {}, publicKey: {}, address: {}",
                credentials.getEcKeyPair().getPrivateKey(),
                credentials.getEcKeyPair().getPrivateKey(),
                credentials.getAddress());
        return credentials;
    } catch (Exception e) {
        System.out.println("init credential failed");
        logger.error("init credential failed, error msg:" + e.getMessage());
        return null;
    }
}
 
Example 2
Source File: AbiTest.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateKey() throws Exception {
  Credentials credentials = Credentials.create("3bed914595c159cbce70ec5fb6aff3d6797e0c5ee5a7a9224a21cae8932d84a4");
  System.out.println( credentials.getAddress());
  System.out.println( credentials.getEcKeyPair().getPrivateKey());
  System.out.println(  credentials.getEcKeyPair().getPublicKey());

}
 
Example 3
Source File: KeyPairUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * get Credentials from privateKey by encrypt type
 * @param privateKey
 * @param encryptType 1: guomi, 0: standard
 * @return Credentials
 */
@Cacheable(cacheNames = "getCredentials")
public Credentials create(String privateKey, int encryptType) {
    try {
        ECKeyPair keyPair = createKeyPairByType(privateKey, encryptType);
        if (keyPair == null) {
            return null;
        }
        Credentials credentials = Credentials.create(keyPair);
        return credentials;
    } catch (Exception e) {
        log.error("init credential from private key failed, error msg:" + e.getMessage());
        return null;
    }
}
 
Example 4
Source File: KeyPairUtils.java    From WeBASE-Sign with Apache License 2.0 5 votes vote down vote up
/**
 * get Credentials from key pair by encrypt type
 * @param keyPair
 * @param encryptType 1: guomi, 0: standard
 * @return Credentials
 */
public Credentials create(ECKeyPair keyPair, int encryptType) {
    try {
        ECKeyPair newKeyPair = createKeyPairByType(keyPair.getPrivateKey().toString(16), encryptType);
        if (newKeyPair == null) {
            return null;
        }
        Credentials credentials = Credentials.create(newKeyPair);
        return credentials;
    } catch (Exception e) {
        log.error("KeyPairUtils init credential from private key failed, error msg:" + e.getMessage());
        return null;
    }
}
 
Example 5
Source File: GenCredential.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
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;
    }
}
 
Example 6
Source File: GenCredential.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static Credentials create(ECKeyPair keyPair) {
    try {
        ECKeyPair newKeyPair = createKeyPair(keyPair.getPrivateKey().toString(16));
        if (newKeyPair == null) return null;
        Credentials credentials = Credentials.create(newKeyPair);
        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;
    }
}
 
Example 7
Source File: AccountTest.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 8
Source File: OkClient.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // init the Service
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(Integer.parseInt(args[0]));
        service.run(); // run the daemon service
        // init the client keys
        keyPair = Keys.createEcKeyPair();
        credentials = Credentials.create(keyPair);

        logger.info("-----> start test !");
        logger.info("init AOMP ChannelEthereumService");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        try {
            web3j = Web3j.build(channelEthereumService, Integer.parseInt(args[0]));
        } catch (Exception e) {
            System.out.println("\nPlease provide groupID in the first paramters");
            System.exit(0);
        }

        if (args.length > 1) {
            if ("deploy".equals(args[1])) {
                deployOk();
            } else {
                String[] params = new String[args.length - 1];
                for (int i = 0; i < params.length; i++) params[i] = args[i + 1];
                testOk(params);
            }
        } else {
            System.out.println("\nPlease choose follow commands:\n deploy, trans or get");
        }
        System.exit(0);
    }
 
Example 9
Source File: MixContractClient.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {

        // init the Service
        ApplicationContext context =
                new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Service service = context.getBean(Service.class);
        service.setGroupId(Integer.parseInt(args[0]));
        service.run(); // run the daemon service
        // init the client keys
        keyPair = Keys.createEcKeyPair();
        credentials = Credentials.create(keyPair);

        logger.info("-----> start test !");
        logger.info("init AOMP ChannelEthereumService");
        ChannelEthereumService channelEthereumService = new ChannelEthereumService();
        channelEthereumService.setChannelService(service);
        try {
            web3j = Web3j.build(channelEthereumService, Integer.parseInt(args[0]));
        } catch (Exception e) {
            System.out.println("\nPlease provide groupID in the first paramters");
            System.exit(0);
        }

        if (args.length > 1) {
            if ("deploy".equals(args[1])) {
                deploymixContract();
            } else {
                String[] params = new String[args.length - 1];
                for (int i = 0; i < params.length; i++) params[i] = args[i + 1];
                testMixContract(params);
            }
        } else {
            System.out.println(
                    "\nPlease choose follow commands:\n deploy, create, insert, select, update or remove");
        }
        System.exit(0);
    }
 
Example 10
Source File: TestTxDecode.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static TransactionReceipt sentTx() throws Exception {
    // init the Service
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    service.setGroupId(1);
    Web3j web3j = Web3j.build(channelEthereumService, service.getGroupId());

    RemoteCall<TableTest> deploy =
            TableTest.deploy(
                    web3j,
                    credentials,
                    new StaticGasProvider(
                            new BigInteger("30000000"), new BigInteger("30000000")));
    TableTest tableTest = deploy.send();
    tableTest.create().send();

    String name = "fruit";
    int item_id = 1;
    String item_name = "apple";

    RemoteCall<TransactionReceipt> insert =
            tableTest.insert(name, BigInteger.valueOf(item_id), item_name);
    TransactionReceipt txReceipt = insert.send();

    return txReceipt;
}