Java Code Examples for org.fisco.bcos.web3j.crypto.gm.GenCredential

The following examples show how to use org.fisco.bcos.web3j.crypto.gm.GenCredential. These examples are extracted from open source projects. 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 Project: WeBASE-Node-Manager   Source File: PublicKeyGmTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void checkPubCorrect() throws IOException {
     String defaultUserPrivateKey = "SzK9KCjpyVCW0T9K9r/MSlmcpkeckYKVn/D1X7fzzp18MM7yHhUHQugTxKXVJJY5XWOb4zZ79IXMBu77zmXsr0mCRnATZTUqFfWLX6tUBIw=";
     String defaultPub = "0xc5d877bff9923af55f248fb48b8907dc7d00cac3ba19b4259aebefe325510af7bd0a75e9a8e8234aa7aa58bc70510ee4bef02201a86006196da4e771c47b71b4";
     String defaultAddress = "0xf1585b8d0e08a0a00fff662e24d67ba95a438256";

    Credentials credential = GenCredential.create(defaultUserPrivateKey);
    System.out.println("private: ");
    System.out.println(credential.getEcKeyPair().getPrivateKey());
    System.out.println(Numeric.toHexStringNoPrefix(credential.getEcKeyPair().getPrivateKey()));
    System.out.println("pub: ");
    System.out.println(credential.getEcKeyPair().getPublicKey());
    System.out.println("address: ");
    System.out.println(credential.getAddress());
    System.out.println(Keys.getAddress(credential.getEcKeyPair().getPublicKey()));
}
 
Example 2
Source Project: WeBASE-Front   Source File: KeyStoreService.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * create key store locally and save
 * with private key
 */
public KeyStoreInfo createKeyStoreLocally(String userName) {
    log.info("start createKeyStore. userName:{}", userName);
    checkUserNameAndTypeNotExist(userName, KeyTypes.LOCALUSER.getValue());
    // create keyPair(support guomi)
    KeyStoreInfo keyStoreInfo;
    try {
        ECKeyPair keyPair = GenCredential.createKeyPair();
        keyStoreInfo = keyPair2KeyStoreInfo(keyPair, userName);
    } catch (Exception e) {
        log.error("fail createKeyStore.", e);
        throw new FrontException("create keyInfo failed");
    }
    keyStoreInfo.setType(KeyTypes.LOCALUSER.getValue());
    String realPrivateKey = keyStoreInfo.getPrivateKey();
    keyStoreInfo.setPrivateKey(aesUtils.aesEncrypt(realPrivateKey));
    return keystoreRepository.save(keyStoreInfo);
}
 
Example 3
Source Project: WeBASE-Front   Source File: ConsensusServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConsensus() throws Exception {
    nodeId = "224e6ee23e8a02d371298b9aec828f77cc2711da3a981684896715a3711885a3177b3cf7906bf9d1b84e597fad1e0049511139332c04edfe3daddba5ed60cffa";

    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //链管理员私钥加载
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));

    ConsensusService consensusService = new ConsensusService(web3j, credentialsPEM);
    System.out.println(consensusService.addSealer(nodeId));
    assertNotNull(consensusService.addSealer(nodeId));
    System.out.println(consensusService.addObserver(nodeId));
    assertNotNull(consensusService.addObserver(nodeId));
    System.out.println(consensusService.removeNode(nodeId));
    assertNotNull(consensusService.removeNode(nodeId));
}
 
Example 4
@Test
public void testSystemConfig() throws Exception {

    key = "tx_count_limit"; // key: tx_count_limit, tx_gas_limit
    value = "300000001";

    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //链管理员私钥加载
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));

    SystemConfigService systemConfigService = new SystemConfigService(web3j, credentialsPEM);

    System.out.println(web3j.getSystemConfigByKey(key).send().getResult());
    System.out.println(systemConfigService.setValueByKey(key, value));
    assertNotNull(systemConfigService.setValueByKey(key, value));
    System.out.println(web3j.getSystemConfigByKey(key).send().getResult());
    assertNotNull(web3j.getSystemConfigByKey(key));
}
 
Example 5
Source Project: WeBASE-Front   Source File: PermissionServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testPermissionManager() throws Exception{
    address = "0x832f299af98c215f7ada010a966b12126483cd00";

    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //生成web3sdk使用的Credentials
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
    PermissionService permissionService = new PermissionService(web3j, credentialsPEM);

    System.out.println(permissionService.grantPermissionManager(address));
    assertNotNull(permissionService.grantPermissionManager(address));
    System.out.println(permissionService.listPermissionManager().get(0).getAddress());
    assertTrue(permissionService.listPermissionManager().size() != 0);
    System.out.println(permissionService.revokePermissionManager(address));
    assertNotNull(permissionService.revokePermissionManager(address));
}
 
Example 6
Source Project: WeBASE-Front   Source File: PermissionServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
    public void testDeployAndCreateManager() throws Exception{
        // 普通账户
        address = "0x832f299af98c215f7ada010a966b12126483cd00";

        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        PEMManager pem = context.getBean(PEMManager.class);
        ECKeyPair pemKeyPair = pem.getECKeyPair();
        //生成web3sdk使用的Credentials
        Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
        // 链管理员账户签名的PermissionService
        PermissionService permissionService = new PermissionService(web3j, credentialsPEM);

        // TablePermission
        System.out.println(permissionService.grantDeployAndCreateManager(address));
        System.out.println(permissionService.grantDeployAndCreateManager(address));

        assertNotNull(permissionService.grantDeployAndCreateManager(address));
        System.out.println(permissionService.listDeployAndCreateManager().get(0).getAddress());
        assertTrue(permissionService.listDeployAndCreateManager().size() != 0);
//        System.out.println(permissionService.revokeDeployAndCreateManager(address));
//        assertNotNull(permissionService.revokeDeployAndCreateManager(address));
    }
 
Example 7
Source Project: WeBASE-Front   Source File: PermissionServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
    public void testUserTableManager() throws Exception{
        String tableName = "t_test";
        address = "0xd5bba8fe456fce310f529edecef902e4b63129b1";

        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        PEMManager pem = context.getBean(PEMManager.class);
        ECKeyPair pemKeyPair = pem.getECKeyPair();
        //生成web3sdk使用的Credentials
        Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
        PermissionService permissionService = new PermissionService(web3j, credentialsPEM);

        // TablePermission
        System.out.println(permissionService.grantUserTableManager(tableName, address));
        assertNotNull(permissionService.grantUserTableManager(tableName, address));
//        List<PermissionInfo> list = permissionService.listUserTableManager(tableName);
//        assertTrue(permissionService.listUserTableManager(tableName).size() != 0);
//        System.out.println(permissionService.revokeUserTableManager(tableName, address));
//        assertNotNull(permissionService.revokeUserTableManager(tableName, address));
    }
 
Example 8
Source Project: WeBASE-Front   Source File: PermissionServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testNodeManger() throws Exception {
    address = "0x832f299af98c215f7ada010a966b12126483cd00";
    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //生成web3sdk使用的Credentials
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
    PermissionService permissionService = new PermissionService(web3j, credentialsPEM);

    // TablePermission
    System.out.println(permissionService.grantNodeManager(address));
    assertNotNull(permissionService.grantNodeManager(address));
    System.out.println(permissionService.listNodeManager().get(0).getAddress());
    assertTrue(permissionService.listNodeManager().size() != 0);
    System.out.println(permissionService.revokeNodeManager(address));
    assertNotNull(permissionService.revokeNodeManager(address));
}
 
Example 9
Source Project: WeBASE-Front   Source File: PermissionServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCNSManager() throws Exception {
    address = "0x832f299af98c215f7ada010a966b12126483cd00";
    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //生成web3sdk使用的Credentials
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
    PermissionService permissionService = new PermissionService(web3j, credentialsPEM);

    // TablePermission
    System.out.println(permissionService.grantCNSManager(address));
    assertNotNull(permissionService.grantCNSManager(address));
    System.out.println(permissionService.listCNSManager().get(0).getAddress());
    assertTrue(permissionService.listCNSManager().size() != 0);
    System.out.println(permissionService.revokeCNSManager(address));
    assertNotNull(permissionService.revokeCNSManager(address));
}
 
Example 10
Source Project: WeBASE-Front   Source File: PermissionServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConfigManager() throws Exception {
    address = "0x832f299af98c215f7ada010a966b12126483cd00";
    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //生成web3sdk使用的Credentials
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
    PermissionService permissionService = new PermissionService(web3j, credentialsPEM);

    // TablePermission
    System.out.println(permissionService.grantSysConfigManager(address));
    assertNotNull(permissionService.grantSysConfigManager(address));
    System.out.println(permissionService.listSysConfigManager().get(0).getAddress());
    assertTrue(permissionService.listSysConfigManager().size() != 0);
    System.out.println(permissionService.revokeSysConfigManager(address));
    assertNotNull(permissionService.revokeSysConfigManager(address));
}
 
Example 11
Source Project: WeBASE-Front   Source File: SignDataTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * key pair from sign differs from local credential
 * front or sign change @param encryptType: 1 and 0 to switch from guomi and standard
 */
@Test
public void testKeyFromSign() {
    KeyStoreInfo keyStoreInfo = getKeyStoreInfoFromSign();
    Assert.assertNotNull(keyStoreInfo);
    System.out.println("keyStoreInfo: ");
    System.out.println(keyStoreInfo.getAddress());
    System.out.println(keyStoreInfo.getPublicKey());
    System.out.println(keyStoreInfo.getPrivateKey());
    // local guomi
    Credentials credentials = GenCredential.create(keyStoreInfo.getPrivateKey());
    String pub = Numeric.toHexStringWithPrefix(credentials.getEcKeyPair().getPublicKey());
    String addr = credentials.getAddress();
    System.out.println("local transfer: ");
    System.out.println(pub);
    System.out.println(addr);
    Assert.assertTrue(keyStoreInfo.getPublicKey().equals(pub));
    Assert.assertTrue(keyStoreInfo.getAddress().equals(addr));
}
 
Example 12
Source Project: WeBASE-Front   Source File: PublicKeyTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void checkPubCorrect() throws IOException {
    String defaultUserPrivateKeyAfterAes = "SzK9KCjpyVCW0T9K9r/MSlmcpkeckYKVn/D1X7fzzp18MM7yHhUHQugTxKXVJJY5XWOb4zZ79IXMBu77zmXsr0mCRnATZTUqFfWLX6tUBIw=";
    String defaultPub = "0xc5d877bff9923af55f248fb48b8907dc7d00cac3ba19b4259aebefe325510af7bd0a75e9a8e8234aa7aa58bc70510ee4bef02201a86006196da4e771c47b71b4";
    String defaultAddress = "0xf1585b8d0e08a0a00fff662e24d67ba95a438256";

    String defaultUserPrivateKey = aesUtils.aesDecrypt(defaultUserPrivateKeyAfterAes);
    System.out.println("decrypt aes");
    System.out.println(defaultUserPrivateKey);
    Credentials credential = GenCredential.create(defaultUserPrivateKey);
    System.out.println("private: ");
    System.out.println(credential.getEcKeyPair().getPrivateKey());
    System.out.println(Numeric.toHexStringNoPrefix(credential.getEcKeyPair().getPrivateKey()));
    System.out.println("pub: ");
    System.out.println(credential.getEcKeyPair().getPublicKey());
    System.out.println("address: ");
    System.out.println(credential.getAddress());
    System.out.println(Keys.getAddress(credential.getEcKeyPair().getPublicKey()));
}
 
Example 13
Source Project: WeBASE-Front   Source File: TransactionEncoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGMCrendetial() {

    for (int i = 0; i < 10; i++) {

        Instant systartTime = Instant.now();
                GenCredential.create(
                        "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f"+i);
        System.out.println("       siyao  useTime: " + Duration.between(systartTime, Instant.now()).toMillis());



        // gm createTransaction!
        EncryptType encryptType = new EncryptType(1);
        assertSame(encryptType.getEncryptType(),1);
        Instant gmsystartTime = Instant.now();

        GenCredential.create(
                        "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f"+i);
        //     System.out.println(gmcredentials.getEcKeyPair().getPublicKey().toString(16));
        System.out.println(encryptType+ "      gmsiyao  useTime: " + Duration.between(gmsystartTime, Instant.now()).toMillis());

    }
}
 
Example 14
public boolean loadConfig() throws Exception {
    System.out.println("please wait ...");
    Service service;
    try {
        context = new ClassPathXmlApplicationContext("classpath:node/application.xml");
        service = context.getBean(Service.class);
        service.run();
    } catch (Exception e) {
        logger.error("load config failed, error msg: " + e.getMessage());
        throw new Exception("load config failed, error msg: " + e.getMessage());
    }

    credentials = GenCredential.create();

    // channel eth service
    ChannelEthereumService channelService = new ChannelEthereumService();
    channelService.setChannelService(service);
    channelService.setTimeout(5000);
    web3j = Web3j.build(channelService, service.getGroupId());
    return true;
}
 
Example 15
Source Project: web3sdk   Source File: AccountUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static Account newAccount(boolean flag)
        throws InvalidAlgorithmParameterException, NoSuchAlgorithmException,
                NoSuchProviderException {
    // guomi
    Account account = new Account();
    if (flag) {
        EncryptType.encryptType = 1;
        account.setEncryptType("guomi");
    } else {
        EncryptType.encryptType = 0;
        account.setEncryptType("standard");
    }
    Credentials credentials = GenCredential.create();

    String address = credentials.getAddress();
    String privateKey = credentials.getEcKeyPair().getPrivateKey().toString(16);
    String publicKey = credentials.getEcKeyPair().getPublicKey().toString(16);

    account.setAddress(address);
    account.setPrivateKey(privateKey);
    account.setPublicKey(publicKey);
    return account;
}
 
Example 16
Source Project: web3sdk   Source File: TestBase.java    License: Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void setUpBeforeClass() throws Exception {

	context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

  Service service = context.getBean(Service.class);
  service.run();

  ChannelEthereumService channelEthereumService = new ChannelEthereumService();
  channelEthereumService.setChannelService(service);

  System.out.println("EncryptType =>  " + EncryptType.getEncryptType());

  web3j = Web3j.build(channelEthereumService, service.getGroupId());
  credentials = GenCredential.create();
  Ok ok = Ok.deploy(web3j, credentials, new StaticGasProvider(gasPrice, gasLimit)).send();
  blockNumber = ok.getTransactionReceipt().get().getBlockNumber();
  blockHash = ok.getTransactionReceipt().get().getBlockHash();
  txHash = ok.getTransactionReceipt().get().getTransactionHash();
}
 
Example 17
Source Project: web3sdk   Source File: PerformanceDTTest.java    License: Apache License 2.0 6 votes vote down vote up
public void initialize(String groupId) throws Exception {
    ApplicationContext context =
            new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.setGroupId(Integer.parseInt(groupId));
    service.run();

    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);

    Web3AsyncThreadPoolSize.web3AsyncCorePoolSize = 3000;
    Web3AsyncThreadPoolSize.web3AsyncPoolSize = 2000;

    ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(500);

    web3 =
            Web3j.build(
                    channelEthereumService,
                    15 * 100,
                    scheduledExecutorService,
                    Integer.parseInt(groupId));
    credentials = GenCredential.create();
    transactionManager = Contract.getTheTransactionManager(web3, credentials);
}
 
Example 18
Source Project: web3sdk   Source File: TransactionEncoderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Ignore
@Test
public void testGMSignMessage() {
    EncryptType encryptType = new EncryptType(1);

    Credentials credentials =
            GenCredential.create(
                    "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");
    System.out.println(credentials.getEcKeyPair().getPublicKey().toString(16));

    Credentials credentials1 =
            Credentials.create(
                    "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");
    System.out.println(credentials1.getEcKeyPair().getPublicKey().toString(16));

    byte[] signedMessage =
            TransactionEncoder.signMessage(createContractTransaction(), credentials);
    String hexMessage = Numeric.toHexString(signedMessage);
    assertThat(
            hexMessage,
            is(
                    "0xf8948201f4010a8201f5800a850000000000b8408234c544a9f3ce3b401a92cc7175602ce2a1e29b1ec135381c7d2a9e8f78f3edc9c06ee55252857c9a4560cb39e9d70d40f4331cace4d2b3121b967fa7a829f0a03d8627050f6688f27e2b5b89c9c141d3a48603029849e088486d1c7ea079ea7fa037024ed35d2c099d7eb68fb133e57735b03605ec32ded39ab305c3b56e5d99e7"));
}
 
Example 19
Source Project: WeBASE-Node-Manager   Source File: UserControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGenerateKey() throws Exception {
    // guomi use GenCredential
   Credentials credentials = GenCredential.create("3bed914595c159cbce70ec5fb6aff3d6797e0c5ee5a7a9224a21cae8932d84a4");
    System.out.println( credentials.getAddress());
    System.out.println( credentials.getEcKeyPair().getPrivateKey().toString(16));
    System.out.println(  credentials.getEcKeyPair().getPublicKey().toString(16));
}
 
Example 20
Source Project: WeBASE-Front   Source File: KeyStoreService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * get random credential to call transaction(not execute)
 * 2019/11/26 support guomi
 */
public Credentials getCredentialsForQuery() {
    log.debug("start getCredentialsForQuery. ");
    // create keyPair(support guomi)
    KeyStoreInfo keyStoreInfo;
    try {
        ECKeyPair keyPair = GenCredential.createKeyPair();
        keyStoreInfo = keyPair2KeyStoreInfo(keyPair, "");
    } catch (Exception e) {
        log.error("fail getCredentialsForQuery.", e);
        throw new FrontException("create random Credentials for query failed");
    }
    return GenCredential.create(keyStoreInfo.getPrivateKey());
}
 
Example 21
Source Project: WeBASE-Front   Source File: KeyStoreService.java    License: Apache License 2.0 5 votes vote down vote up
public KeyStoreInfo getKeyStoreInfoForQuery() {
    log.debug("start getKeyStoreInfoForQuery. ");
    // create keyPair(support guomi)
    KeyStoreInfo keyStoreInfo;
    try {
        ECKeyPair keyPair = GenCredential.createKeyPair();
        return keyPair2KeyStoreInfo(keyPair, "");
    } catch (Exception e) {
        log.error("fail getKeyStoreInfoForQuery.", e);
        throw new FrontException("create random keyInfo for query failed");
    }
}
 
Example 22
Source Project: WeBASE-Front   Source File: KeyStoreService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * save LOCAL_USER key store by private key
 * @param privateKey
 * @param userName
 * @return KeyStoreInfo local user
 */
public KeyStoreInfo importFromPrivateKey(String privateKey, String userName) {
    // check name
    checkUserNameAndTypeNotExist(userName, KeyTypes.LOCALUSER.getValue());
    // to store locally
    ECKeyPair keyPair = GenCredential.createKeyPair(privateKey);
    if (keyPair == null) {
        throw new FrontException(ConstantCode.PARAM_ERROR);
    }
    KeyStoreInfo keyStoreInfo = keyPair2KeyStoreInfo(keyPair, userName);
    keyStoreInfo.setType(KeyTypes.LOCALUSER.getValue());
    String realPrivateKey = keyStoreInfo.getPrivateKey();
    keyStoreInfo.setPrivateKey(aesUtils.aesEncrypt(realPrivateKey));
    return keystoreRepository.save(keyStoreInfo);
}
 
Example 23
Source Project: WeBASE-Front   Source File: CnsServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
    public void testRegCns() throws Exception {
        contractName = "Evidence1";
        version = "1.0";
        address = "0x8acf30e511c885163b8b9d85f34b806c216da6cc";
        abi = "{\"constant\":true,\"inputs\":[{\"name\":\"id\",\"type\":\"bytes32\"}],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"id\",\"type\":\"bytes32\"},{\"name\":\"decription\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"bytes32\"}],\"name\":\"evidence\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]";
        contractNameAndVersion = "Evidencee:1.0";

        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        PEMManager pem = context.getBean(PEMManager.class);
        ECKeyPair pemKeyPair = pem.getECKeyPair();
        //链管理员私钥加载
        Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));

        CnsService cnsService = new CnsService(web3j, credentialsPEM);
//        System.out.println(cnsService.registerCns(contractName, version, address, abi));
//        assertNotNull(cnsService.registerCns(contractName, version, address, abi));

        // 默认获取最新版本
//        String res = cnsService.getAddressByContractNameAndVersion("contractNameAndVersion");
        List<CnsInfo> list = new ArrayList<>();
                list = cnsService.queryCnsByName(contractName);
        System.out.println(contractNameAndVersion);
//        System.out.println(cnsService.getAddressByContractNameAndVersion(contractName));
//        assertNotNull(cnsService.getAddressByContractNameAndVersion(contractName));
//        System.out.println(cnsService.queryCnsByName(contractName).size());
//        System.out.println(cnsService.queryCnsByName(contractName).get(0).getAddress());
//        System.out.println(cnsService.queryCnsByName(contractName).get(1).getAddress());
//        assertTrue(cnsService.queryCnsByName(contractName).size() != 0);

    }
 
Example 24
Source Project: WeBASE-Front   Source File: SysConfigDbTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * System config related
 */
@Test
public void setSysConfigValueByKey() throws Exception {
    context = new ClassPathXmlApplicationContext("applicationContext.xml");
    PEMManager pem = context.getBean(PEMManager.class);
    ECKeyPair pemKeyPair = pem.getECKeyPair();
    //链管理员私钥加载
    Credentials credentialsPEM = GenCredential.create(pemKeyPair.getPrivateKey().toString(16));
    SystemConfigService systemConfigService = new SystemConfigService(web3j, credentialsPEM);

    systemConfigService.setValueByKey(key, value);
}
 
Example 25
Source Project: WeBASE-Front   Source File: SendGMTxTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testSystemConfigService() throws Exception {
    Credentials credentials2 =
            GenCredential.create("a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f6");

    // web3j
    SystemConfigService systemConfigSerivce = new SystemConfigService(web3ApiService.getWeb3j(1), credentials2);
    systemConfigSerivce.setValueByKey("tx_count_limit", "2000");
    String value = web3ApiService.getWeb3j(1).getSystemConfigByKey("tx_count_limit").send().getSystemConfigByKey();
    System.out.println(value);
    assertTrue("2000".equals(value));
}
 
Example 26
Source Project: WeBASE-Front   Source File: TransactionEncoderTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGMSignMessage() {


        Credentials credentials =
                GenCredential.create(
                        "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f");

        Instant startTime = Instant.now();

        byte[] signedMessage = TransactionEncoder.signMessage(createContractTransaction(), credentials);
        System.out.println("       sign  useTime: " + Duration.between(startTime, Instant.now()).toMillis());
     //   String hexMessage = Numeric.toHexString(signedMessage);


        // gm createTransaction!
        EncryptType encryptType = new EncryptType(1);
         assertSame(encryptType.getEncryptType(),1);
        Credentials gmcredentials =
                GenCredential.create(
                        "a392604efc2fad9c0b3da43b5f698a2e3f270f170d859912be0d54742275c5f");

        Instant startTime1 = Instant.now();
         TransactionEncoder.signMessage(createContractTransaction(), gmcredentials);
        System.out.println(" guomi sign  useTime: " + Duration.between(startTime1, Instant.now()).toMillis());
  //      String hexMessage1 = Numeric.toHexString(signedMessage1);
}
 
Example 27
Source Project: WeBASE-Front   Source File: KeyStoreServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void loadPrivateKeyTest() throws Exception {
    String privateKey = "71f1479d9051e8d6b141a3b3ef9c01a7756da823a0af280c6bf62d18ee0cc978";
    Credentials credentials = GenCredential.create(privateKey);
    // private key 实例
    BigInteger privateKeyInstance = credentials.getEcKeyPair().getPrivateKey();
    System.out.println(Numeric.toHexStringNoPrefix(privateKeyInstance));
    // public key 实例
    BigInteger publicKeyInstance = credentials.getEcKeyPair().getPublicKey();
    System.out.println(Numeric.toHexString(publicKeyInstance.toByteArray()));
    String address = credentials.getAddress();
    System.out.println(address);
}
 
Example 28
Source Project: WeBASE-Front   Source File: KeyStoreServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadP12() throws UnrecoverableKeyException, InvalidKeySpecException, NoSuchAlgorithmException, KeyStoreException, NoSuchProviderException, CertificateException, IOException {
	P12Manager p12Manager = new P12Manager();
	p12Manager.setP12File("0x6399bda67f0ae8d1fdd997a885b8aee32a0c9696.p12");
	p12Manager.setPassword("123");
	p12Manager.load();
	// c5658bbb9b905345e7c057690ec6f50c06dada711d1086820980496b4954fbc7
	String privateKey = Numeric.toHexStringNoPrefix(p12Manager.getECKeyPair().getPrivateKey());
	System.out.println("load private key: " + privateKey);
	String address = GenCredential.create(privateKey).getAddress();
	System.out.println("address: " + address);
	Assert.assertTrue("pri error", address.equals("0x6399bda67f0ae8d1fdd997a885b8aee32a0c9696"));
}
 
Example 29
Source Project: WeBASE-Transaction   Source File: KeyStoreService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * get user Address.
 * 
 * @return
 */
public String getAddress() throws BaseException {
    try {
        String privateKey = properties.getPrivateKey();
        Credentials credentials = GenCredential.create(privateKey);
        return credentials.getAddress();
    } catch (Exception e) {
        log.error("getAddress fail.");
        throw new BaseException(ConstantCode.SYSTEM_ERROR);
    }
}
 
Example 30
Source Project: WeBASE-Transaction   Source File: KeyStoreService.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * get random Address.
 * 
 * @return
 */
public String getRandomAddress() throws BaseException {
    try {
        Credentials credentials = GenCredential.create();
        return credentials.getAddress();
    } catch (Exception e) {
        log.error("getRandomAddress fail.");
        throw new BaseException(ConstantCode.SYSTEM_ERROR);
    }
}