2017-06-16 76 views
0

我是區塊鏈開發新手。目前,我正在學習以太坊平臺,這聽起來是一個非常好的環境。我測試了我的Android應用程序的web3j庫,它工作正常。我用下面的代碼連接到我的testrpc節點:Web3j新賬戶與testrpc交易

Web3j web3 = Web3jFactory.build(new HttpService("http://192.168.1.108:8545")); 
BigInteger gasPrice = BigInteger.valueOf(20000000000L); 
    BigInteger gasLimit = BigInteger.valueOf(500000L); 
    BigInteger nonce = null; 


    String contractAddress="0x0dd3b0efbce5c4eba2dc9b8500ecafb0b1cec28f"; 

    String from = "0x2d6fcee3c3435ebda9184bdddf8481a87b7d1948"; 
    List<Type> inputParameters = new ArrayList<>(); 
    String hash ="232131231232141231231231231232123123123"; 
    byte[] b =Arrays.copyOfRange(new BigInteger(hash, 16).toByteArray(), 1, 33); 

    Type _telNumber = new Bytes32(b); 
    Type _publicKey = new Bytes32(b); 
    inputParameters.add(_telNumber); 
    inputParameters.add(_publicKey); 
    Function function = new Function("addPerson", 
      inputParameters, 
      Collections.<TypeReference<?>>emptyList()); 
    String functionEncoder = FunctionEncoder.encode(function); 



    Transaction transaction = Transaction.createFunctionCallTransaction(from, nonce,gasPrice,gasLimit,contractAddress,new BigInteger("0"), functionEncoder); 

    try { 
     EthSendTransaction transactionResponse = web3.ethSendTransaction(transaction).sendAsync().get(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 

上面的代碼工作,我能夠與智能合同(調用一個函數)進行交互。

的主要問題是,參數是硬編碼(我從帳戶testrpc名單得到了它)。

我想實現的目標:我想創建一個應用程序,用戶可以在其中創建新的錢包(帳戶),並使用它們與網絡進行交易。我創建了錢包成功地使用下面的代碼:

String filePath = Environment.getExternalStorageDirectory().toString() + "/Pictures"; 


    Web3j web3 = Web3jFactory.build(new HttpService("http://192.168.1.108:8545")); 
    Web3ClientVersion web3ClientVersion = null; 

    try { 
     String fileName = WalletUtils.generateNewWalletFile("your password",new File(filePath),false); 
     Log.d("FILENAME",fileName); 

     Credentials credentials = WalletUtils.loadCredentials(
       "your password", 
       filePath+"/"+fileName); 

     myAddress = credentials.getAddress(); 
     Log.d("My address",credentials.getAddress()); 

    } catch (CipherException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (InvalidAlgorithmParameterException e) { 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } catch (NoSuchProviderException e) { 
     e.printStackTrace(); 
    } 

什麼是下一個步驟?我應該向網絡廣播我的地址嗎?

回答

0

主要問題是from參數是硬編碼的(我從testrpc帳戶列表中獲得)。

您可以調用您的文件並解密以獲取您的地址,每當您創建一個新帳戶時使用您的密鑰生成一個文件。

val credentialsOne = WalletUtils.loadCredentials("your password", "your path") 

當你與你的節點連接,它會自動檢測地址