1. 程式人生 > >Android開發以太坊錢包生成應用程序

Android開發以太坊錢包生成應用程序

運行時 try super 取地址 save ras parent sel cte

Android應用程序以太坊錢包生成,要做的工作不少,不過如果我們一步一步來應該也比較清楚:

1.在app/build.gradle中集成以下依賴項:

compile (‘org.web3j:core-android:2.2.1‘)

web3j核心是用於從服務器下載以太坊區塊鏈數據的核心類庫。它通常用於以太坊開發。

2.我們將設計一個Android UI示例,屏幕上將有文本編輯和按鈕。在EditText中,將要求用戶輸入錢包的密碼。然後在按鈕的單擊事件上,我們將開始發送密碼的過程。以下是layout.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/content"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">
    <EditText
        android:id="@+id/password"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:hint="@string/textview_password"
        android:padding="10dp"/>
    <Button
        android:id="@+id/generate_wallet_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/textview_generate_wallet"/>
</LinearLayout>

3.我們將創建一個FileOutputStream路徑,將創建的錢包文件保存在存儲中,這需要讀寫存儲權限。

4.對於Android用戶Api>26,需要擁有運行時權限以執行上述步驟。

5.然後有一個名為WalletUtils的類。在web3jcore中。在該類中,有一個方法generateWalletNewFile(password, path),它將接受密碼參數和錢包文件的路徑。 將可以創建錢包文件。

6.web3jcore中還有一個類憑據Credentials,它將使用WalletUtils.loadCredentials(password,path)方法加載文件的所有憑據。以下是用於生成錢包文件的一個類和接口:

public class EthereumGenerationPresenter implements EthereumGenerationContract.Presenter {
    private final EthereumGenerationContract.View mWalletGenerationView;
    private String mPassword;
    public EthereumGenerationPresenter(EthereumGenerationContract.View walletGenerationView, String password) {
        mWalletGenerationView = walletGenerationView;
        mPassword = password;
    }
    @Override
    public void generateWallet(final String password) {
        String fileName;
        try {
            File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
            if (!path.exists()) {
                path.mkdir();
            }
            fileName = WalletUtils.generateLightNewWalletFile(password, new File(String.valueOf(path)));
            Log.e("TAG", "generateWallet: " + path+ "/" + fileName);
            Credentials credentials =
                    WalletUtils.loadCredentials(
                            password,
                            path + "/" + fileName);
            mWalletGenerationView.showGeneratedWallet(credentials.getAddress());
            Log.e("TAG", "generateWallet: " + credentials.getAddress() + " " + credentials.getEcKeyPair().getPublicKey());
        } catch (NoSuchAlgorithmException
                | NoSuchProviderException
                | InvalidAlgorithmParameterException
                | IOException
                | CipherException e) {
            e.printStackTrace();
        }
    }
    @Override
    public void start() {
        generateWallet(mPassword);
    }
}

public interface EthereumGenerationContract {
    interface View extends BaseView<Presenter> {
        void showGeneratedWallet(String walletAddress);
    }
    interface Presenter extends BasePresenter {
        void generateWallet(String password);
    }
}

public interface BasePresenter {
    void start();
}

public interface BaseView<T> {
    void setPresenter(T presenter);
}

7.現在Credentials類將保存以太坊的錢包地址以及該文件的更多信息。

8.現在可以使用下面的函數獲取地址:

 credentials.getAddress()->

公鑰

 credentials.getPublicKey()

私鑰

credentials.getEcKeyPair()

9.錢包生成類Activity如下:

public class WalletGenerationActivity extends AppCompatActivity implements EthereumGenerationContract.View {

    private static final int REQUEST_PERMISSION_WRITE_STORAGE = 0;

    private EthereumGenerationContract.Presenter mWalletPresenter;

    private Button mGenerateWalletButton;

    private String mWalletAddress;

    private EditText mPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_generation);

        mGenerateWalletButton = (Button) findViewById(R.id.generate_wallet_button);
        mPassword = (EditText) findViewById(R.id.password);

        mGenerateWalletButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int permissionCheck = ContextCompat.checkSelfPermission(WalletGenerationActivity.this,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE);
                if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(
                            WalletGenerationActivity.this,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            REQUEST_PERMISSION_WRITE_STORAGE);
                } else {
                    mWalletPresenter = new EthereumGenerationPresenter(WalletGenerationActivity.this,
                            mPassword.getText().toString());
                    mWalletPresenter.generateWallet(mPassword.getText().toString());
                    Intent intent = new Intent(WalletGenerationActivity.this, WalletActivity.class);
                    intent.putExtra("WalletAddress", mWalletAddress);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    public void setPresenter(EthereumGenerationContract.Presenter presenter) {
        mWalletPresenter = presenter;
    }

    @Override
    public void showGeneratedWallet(String address) {
        mWalletAddress = address;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSION_WRITE_STORAGE: {
                if (grantResults.length == 0 || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                    finish();
                } else {
                    mWalletPresenter.generateWallet(mPassword.getText().toString());
                }
                break;
            }
        }
    }
}

10.具有textview的活動類,用於顯示錢包地址。

public class WalletActivity extends AppCompatActivity {

    private TextView mWalletAddress;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wallet);
        mWalletAddress = (TextView) findViewById(R.id.account_address);
        Bundle extras = getIntent().getExtras();
        mWalletAddress.setText(extras.getString("WalletAddress"));
    }
}

如果希望快速進行java以太坊開發,那請看我們精心打造的教程:
java以太坊開發教程,主要是針對java和android程序員進行區塊鏈以太坊開發的web3j詳解。

Android開發以太坊錢包生成應用程序