web3j-根据keystore密码解析keystore从而得到账户信息

    //根据keystore解锁账号返回私钥
    public static String getPrivatekeybyKeystore(String keyStorePass) {
        try {
            ClassPathResource resource = new ClassPathResource("keystore.json");
            File files = asFile(resource.getInputStream());
              
            org.web3j.crypto.Credentials credentials = WalletUtils.loadCredentials(keyStorePass,files);
            String privatekey = credentials.getEcKeyPair().getPrivateKey().toString(16);
            
            return privatekey;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
    public static File asFile(InputStream inputStream) throws IOException{
        File tmp = File.createTempFile("keystore", ".json", new File("/"));
        OutputStream os = new FileOutputStream(tmp);
        int bytesRead = 0;
        byte[] buffer = new byte[8192];
        while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
          os.write(buffer, 0, bytesRead);
        }
        inputStream.close();
        return tmp;
     }

关于keystore密码的输入,最开始是通过启动项目的时候输入-D******来得到,后面发现linux通过linux命令history可以直接看到密码;后面就通过System.in的方式,在启动项目过程中通过交互形式输入来获取,然后Controller中根据applicationArguments获取密码。

 

你可能感兴趣的:(web3j)