首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用web3j创建帐户

用web3j创建帐户
EN

Ethereum用户
提问于 2018-11-15 08:59:48
回答 2查看 2.9K关注 0票数 1

我试图使用web3j库创建一个帐户。但我找不到创建账户的方法。我的想法是做出这样的判断,但不是在Geth控制台,而是在使用web3j的Java应用程序中:

geth帐户新

有什么想法吗?

我是新来的,直到现在,我没有产生钱包,因为我没有使用聪明的合同。我的想法就是用de应用程序创建帐户,并在私有链中对eth进行简单的转换。

谢谢

EN

回答 2

Ethereum用户

回答已采纳

发布于 2018-11-15 09:20:52

使用Web3j (3.4.0),可以通过两个步骤实现这一点:

  1. 生成密钥对Keys.createEcKeyPair()
  2. 创建钱包Wallet.createStandard(seed, keyPair)

示例:

代码语言:javascript
复制
public static void main(String[] args) {

    try {
        String password = "secr3t";
        ECKeyPair keyPair = Keys.createEcKeyPair();
        WalletFile wallet = Wallet.createStandard(password, keyPair);

        System.out.println("Priate key: " + keyPair.getPrivateKey().toString(16));
        System.out.println("Account: " + wallet.getAddress());

    } catch(Exception e) {
        System.err.println("Error: " + e.getMessage());
    }

}

编辑:创建钱包文件

代码语言:javascript
复制
String fileName = WalletUtils.generateNewWalletFile(
        "secr3t",
        new File("/path/to/destination"));
票数 2
EN

Ethereum用户

发布于 2019-06-10 20:19:28

我做了这个:

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {
    private WalletFile wallet;
    private String password = "PASSWORD";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        setupBouncyCastle();


        try {
            wallet = createWallet();
        } catch (Exception e) {
            System.out.println("BIG RIP");
        }
    }


    public WalletFile createWallet() throws Exception {
        ECKeyPair keyPair = Keys.createEcKeyPair();
        return Wallet.createLight(password, keyPair);

    }


    private void setupBouncyCastle() {
        final Provider provider = Security.getProvider(BouncyCastleProvider.PROVIDER_NAME);
        if (provider == null) {
            // Web3j will set up the provider lazily when it's first used.
            return;
        }
        if (provider.getClass().equals(BouncyCastleProvider.class)) {
            // BC with same package name, shouldn't happen in real life.
            return;
        }
        // Android registers its own BC provider. As it might be outdated and might not include
        // all needed ciphers, we substitute it with a known BC bundled in the app.
        // Android's BC has its package rewritten to "com.android.org.bouncycastle" and because
        // of that it's possible to have another BC implementation loaded in VM.
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
        Security.insertProviderAt(new BouncyCastleProvider(), 1);
    }
}

由于某种原因,创建带有路径的钱包会不断抛出错误。我的部分代码是从:https://github.com/serso/web3a/blob/4dda74db948f8cbd9a79ba4b9ab456316ea52c4d/app/src/main/java/org/solovyev/android/web3a/App.java#L47复制的,因为新版本的web3j在创建ECKeyPair时会抛出错误。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/62399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档