이더리움 ETH 개발 3 (조기어, 계정 가져오기, 계정 내보내기)

7389 단어 블록체인 개발
생성
    public void nem(String pwd) {
        StringBuilder sb = new StringBuilder();
        byte[] entropy = new byte[Words.TWELVE.byteLength()];
        new SecureRandom().nextBytes(entropy);
        new MnemonicGenerator(English.INSTANCE)
                .createMnemonic(entropy, sb::append);
        String mnemonic = sb.toString();
        System.out.println("mnemonic:" + mnemonic);
        List mnemonicList = Arrays.asList(mnemonic.split(" "));
        byte[] seed = new SeedCalculator()
                .withWordsFromWordList(English.INSTANCE)
                .calculateSeed(mnemonicList, pwd);
        ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
        String privateKey = ecKeyPair.getPrivateKey().toString(16);
        String publicKey = ecKeyPair.getPublicKey().toString(16);
        String address = Keys.getAddress(publicKey);
        System.out.println("privateKey:" + privateKey);
        System.out.println("publicKey:" + publicKey);
        System.out.println("address:" + address);
    }
  • 계정을 만들고 메모를 되돌려줍니다
  •   /**
         *     
         *
         * @param pwd
         * @return mnemonic :   
         * privateKey:  
         * publicKey:  
         * address:  
         * accountFilePath:      
         */
        public Map createAccount(String pwd) {
            Map resultMap = new LinkedHashMap();
            //String filePath = System.getProperty("user.home") + File.separator + "keystore";
            StringBuilder sb = new StringBuilder();
            byte[] entropy = new byte[Words.TWELVE.byteLength()];
            new SecureRandom().nextBytes(entropy);
            new MnemonicGenerator(English.INSTANCE)
                    .createMnemonic(entropy, sb::append);
            String mnemonic = sb.toString();
            System.out.println("mnemonic:" + mnemonic);
            List mnemonicList = Arrays.asList(mnemonic.split(" "));
            byte[] seed = new SeedCalculator()
                    .withWordsFromWordList(English.INSTANCE)
                    .calculateSeed(mnemonicList, pwd);
            ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
            String privateKey = ecKeyPair.getPrivateKey().toString(16);
            String publicKey = ecKeyPair.getPublicKey().toString(16);
            String address = "0x" + Keys.getAddress(publicKey);
            //         
            String fileName = null;
            try {
                fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (fileName == null) {
                return null;
            }
            String accountFilePath = filePath + File.separator + fileName;
            resultMap.put("mnemonic", mnemonic);
            resultMap.put("privateKey", privateKey);
            resultMap.put("publicKey", publicKey);
            resultMap.put("address", address);
            resultMap.put("accountFilePath", accountFilePath);
            return resultMap;
        }
    
  • 표기어에 따라 가져오기
  • /**
         *        
         *
         * @param pwd      :     
         * @param mnemonic :   
         * @return
         */
        public Map importByMnemonic(String pwd, String mnemonic) {
            Map resultMap = new LinkedHashMap();
            List mnemonicList = Arrays.asList(mnemonic.split(" "));
            byte[] seed = new SeedCalculator()
                    .withWordsFromWordList(English.INSTANCE)
                    .calculateSeed(mnemonicList, pwd);
            ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
            String privateKey = ecKeyPair.getPrivateKey().toString(16);
            String publicKey = ecKeyPair.getPublicKey().toString(16);
            String address = "0x" + Keys.getAddress(publicKey);
            //         
            String fileName = null;
            try {
                fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if (fileName == null) {
                return null;
            }
            String accountFilePath = filePath + File.separator + fileName;
            resultMap.put("mnemonic", mnemonic);
            resultMap.put("privateKey", privateKey);
            resultMap.put("publicKey", publicKey);
            resultMap.put("address", address);
            resultMap.put("accountFilePath", accountFilePath);
            return resultMap;
        }
    

    계정 내보내기
        /**
         *     
         * @param walletFilePath :       ,     
         * @param password :   
         * @return
         */
        public Map export(String walletFilePath, String password) {
            Map resultMap = new LinkedHashMap();
            Credentials credentials = loadAccount(walletFilePath, password);
            ECKeyPair ecKeyPair = credentials.getEcKeyPair();
            boolean useFullScrypt = false;
            WalletFile walletFile = null;
            try {
                if (useFullScrypt) {
                    walletFile = Wallet.createStandard(password, ecKeyPair);
                } else {
                    walletFile = Wallet.createLight(password, ecKeyPair);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            String fileNameEx = getWalletFileName(walletFile);
            System.out.println("walletFile:" + JSON.toJSONString(walletFile));
            System.out.println("fileNameEx:" + fileNameEx);
            resultMap.put("walletFile", walletFile);
            resultMap.put("fileName", fileNameEx);
            return resultMap;
        }
    

    maven 의존:
            
            
                io.github.novacrypto
                BIP39
                0.1.9
            
            
                io.github.novacrypto
                BIP32
                0.0.9
            
            
                io.github.novacrypto
                BIP44
                0.0.3
            
            
            
                io.github.novacrypto
                SHA256
                0.0.1
            
            
                com.lambdaworks
                scrypt
                1.4.0
            
            
            
                io.github.novacrypto
                ToRuntime
                0.9.0
            
            
            
                com.madgag.spongycastle
                core
                1.58.0.0
            
            
            
                org.web3j
                core
                3.5.0
            
    

    좋은 웹페이지 즐겨찾기