Android开发以太坊钱包生成应用程序

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详解。

猜你喜欢

转载自blog.51cto.com/13697184/2178366