簡體   English   中英

Web3j與testrpc進行新帳戶交易

[英]Web3j new account transacting with testrpc

我是區塊鏈開發的新手。 目前,我正在學習以太坊平台,這聽起來是一個非常好的環境。 我在Android應用程序上測試了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();
    }

上面的代碼可以正常工作,並且我能夠與智能合約進行交互(調用函數)。

主要問題是from參數是硬編碼的(我是從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();
    }

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

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

每次創建新帳戶時,都可以調用文件並將其解密以獲取地址,並使用密鑰生成文件。

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

與節點連接時,它將自動檢測地址

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM