org.web3j.crypto.Wallet类的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(9.0k)|赞(0)|评价(0)|浏览(95)

本文整理了Java中org.web3j.crypto.Wallet类的一些代码示例,展示了Wallet类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Wallet类的具体详情如下:
包路径:org.web3j.crypto.Wallet
类名称:Wallet

Wallet介绍

[英]Ethereum wallet file management. For reference, refer to Web3 Secret Storage Definition or the Go Ethereum client implementation.

Note: the Bouncy Castle Scrypt implementation SCrypt, fails to comply with the following Ethereum reference Scrypt test vector:

// Only value of r that cost (as an int) could be exceeded for is 1}

[中]以太坊钱包文件管理。如需参考,请参考{$0$}或{$1$}。
注意:Bouncy Castle Scrypt实现Scrypt未能符合以下以太坊引用{Scrypt test vector

// Only value of r that cost (as an int) could be exceeded for is 1}

代码示例

代码示例来源:origin: web3j/web3j

public static Credentials loadCredentials(String password, File source)
    throws IOException, CipherException {
  WalletFile walletFile = objectMapper.readValue(source, WalletFile.class);
  return Credentials.create(Wallet.decrypt(password, walletFile));
}

代码示例来源:origin: web3j/web3j

public static String generateWalletFile(
    String password, ECKeyPair ecKeyPair, File destinationDirectory, boolean useFullScrypt)
    throws CipherException, IOException {
  WalletFile walletFile;
  if (useFullScrypt) {
    walletFile = Wallet.createStandard(password, ecKeyPair);
  } else {
    walletFile = Wallet.createLight(password, ecKeyPair);
  }
  String fileName = getWalletFileName(walletFile);
  File destination = new File(destinationDirectory, fileName);
  objectMapper.writeValue(destination, walletFile);
  return fileName;
}

代码示例来源:origin: web3j/web3j

public static WalletFile createStandard(String password, ECKeyPair ecKeyPair)
    throws CipherException {
  return create(password, ecKeyPair, N_STANDARD, P_STANDARD);
}

代码示例来源:origin: web3j/web3j

public static WalletFile create(String password, ECKeyPair ecKeyPair, int n, int p)
    throws CipherException {
  byte[] salt = generateRandomBytes(32);
  byte[] derivedKey = generateDerivedScryptKey(
      password.getBytes(UTF_8), salt, n, R, p, DKLEN);
  byte[] encryptKey = Arrays.copyOfRange(derivedKey, 0, 16);
  byte[] iv = generateRandomBytes(16);
  byte[] privateKeyBytes =
      Numeric.toBytesPadded(ecKeyPair.getPrivateKey(), Keys.PRIVATE_KEY_SIZE);
  byte[] cipherText = performCipherOperation(
        Cipher.ENCRYPT_MODE, iv, encryptKey, privateKeyBytes);
  byte[] mac = generateMac(derivedKey, cipherText);
  return createWalletFile(ecKeyPair, cipherText, iv, salt, mac, n, p);
}

代码示例来源:origin: ethjava/web3j-sample

System.out.println("助记词种子 " + Arrays.toString(mnemonicSeedBytes));
ECKeyPair mnemonicKeyPair = ECKeyPair.create(mnemonicSeedBytes);
WalletFile walletFile = Wallet.createLight(password, mnemonicKeyPair);
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
ECKeyPair ecKeyPair = Wallet.decrypt(password, checkWalletFile);
byte[] checkMnemonicSeedBytes = Numeric.hexStringToByteArray(ecKeyPair.getPrivateKey().toString(16));
System.out.println("验证助记词种子 "
WalletFile walletFile = Wallet.createLight(password, keyPair);
System.out.println("eth address " + "0x" + walletFile.getAddress());
ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();

代码示例来源:origin: web3j/web3j

@Test
public void testCreateLight() throws Exception {
  testCreate(Wallet.createLight(SampleKeys.PASSWORD, SampleKeys.KEY_PAIR));
}

代码示例来源:origin: web3j/web3j

@Test
public void testCreateStandard() throws Exception {
  testCreate(Wallet.createStandard(SampleKeys.PASSWORD, SampleKeys.KEY_PAIR));
}

代码示例来源:origin: web3j/web3j

@Test
public void testGenerateRandomBytes() {
  assertThat(Wallet.generateRandomBytes(0), is(new byte[]{}));
  assertThat(Wallet.generateRandomBytes(10).length, is(10));
}

代码示例来源:origin: web3j/web3j

@Test
public void testEncryptDecryptLight() throws Exception {
  testEncryptDecrypt(Wallet.createLight(SampleKeys.PASSWORD, SampleKeys.KEY_PAIR));
}

代码示例来源:origin: web3j/web3j

@Test
public void testEncryptDecryptStandard() throws Exception {
  testEncryptDecrypt(Wallet.createStandard(SampleKeys.PASSWORD, SampleKeys.KEY_PAIR));
}

代码示例来源:origin: web3j/web3j

private void testEncryptDecrypt(WalletFile walletFile) throws Exception {
  assertThat(Wallet.decrypt(SampleKeys.PASSWORD, walletFile), equalTo(SampleKeys.KEY_PAIR));
}

代码示例来源:origin: uncleleonfan/FunWallet

@Override
  public void run() {
    try {
      File walletDir = contextWrapper.getDir("eth", Context.MODE_PRIVATE);
      if (walletDir.exists() && walletDir.listFiles().length > 0) {
        File[] files = walletDir.listFiles();
        wallet = objectMapper.readValue(files[0], WalletFile.class);
      } else {
        ECKeyPair ecKeyPair = Keys.createEcKeyPair();
        wallet  = Wallet.createLight(PASSWORD, ecKeyPair);
        String walletFileName = getWalletFileName(wallet);
        File destination = new File(walletDir, walletFileName);
        objectMapper.writeValue(destination, wallet);
      }
      if (listener != null && wallet != null) {
        listener.onWalletLoaded(wallet);
      }
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    } catch (NoSuchProviderException e) {
      e.printStackTrace();
    } catch (InvalidAlgorithmParameterException e) {
      e.printStackTrace();
    } catch (CipherException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
});

代码示例来源:origin: ethjava/web3j-sample

/**
 * 创建钱包
 *
 * @param password 密码
 */
public static void createWallet(String password) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, CipherException, JsonProcessingException {
  WalletFile walletFile;
  ECKeyPair ecKeyPair = Keys.createEcKeyPair();
  walletFile = Wallet.createStandard(password, ecKeyPair);
  System.out.println("address " + walletFile.getAddress());
  ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
  String jsonStr = objectMapper.writeValueAsString(walletFile);
  System.out.println("keystore json file " + jsonStr);
}

代码示例来源:origin: web3j/web3j

public static WalletFile createLight(String password, ECKeyPair ecKeyPair)
    throws CipherException {
  return create(password, ecKeyPair, N_LIGHT, P_LIGHT);
}

代码示例来源:origin: web3j/web3j

@Test
public void testDecryptScrypt() throws Exception {
  WalletFile walletFile = load(SCRYPT);
  ECKeyPair ecKeyPair = Wallet.decrypt(PASSWORD, walletFile);
  assertThat(Numeric.toHexStringNoPrefix(ecKeyPair.getPrivateKey()), is(SECRET));
}

代码示例来源:origin: uncleleonfan/FunWallet

private WalletFile createWalletFile(List<String> words) throws MnemonicException.MnemonicLengthException, MnemonicException.MnemonicWordException, MnemonicException.MnemonicChecksumException, CipherException {
  byte[] seeds = MnemonicCode.INSTANCE.toEntropy(words);
  DeterministicKey masterPrivateKey = HDKeyDerivation.createMasterPrivateKey(seeds);
  DeterministicHierarchy deterministicHierarchy = new DeterministicHierarchy(masterPrivateKey);
  DeterministicKey child = deterministicHierarchy.deriveChild(DeterministicKeyChain.BIP44_ACCOUNT_ZERO_PATH, true,
      true, ChildNumber.ZERO);
  ECKeyPair ecKeyPair = ECKeyPair.create(child.getPrivKeyBytes());
  return Wallet.createLight(PASSWORD, ecKeyPair);
}

代码示例来源:origin: TrustWallet/trust-wallet-android-source

@Override
public Single<Wallet> importPrivateKey(String privateKey, String newPassword) {
  return Single.fromCallable(() -> {
    BigInteger key = new BigInteger(privateKey, PRIVATE_KEY_RADIX);
    ECKeyPair keypair = ECKeyPair.create(key);
    WalletFile walletFile = create(newPassword, keypair, N, P);
    return new ObjectMapper().writeValueAsString(walletFile);
  }).compose(upstream -> importKeystore(upstream.blockingGet(), newPassword, newPassword));
}

代码示例来源:origin: web3j/web3j

@Test
public void testDecryptAes128Ctr() throws Exception {
  WalletFile walletFile = load(AES_128_CTR);
  ECKeyPair ecKeyPair = Wallet.decrypt(PASSWORD, walletFile);
  assertThat(Numeric.toHexStringNoPrefix(ecKeyPair.getPrivateKey()), is(SECRET));
}

代码示例来源:origin: terryjiao/BitcoinWallet

private static List<String> generateKeyPairs(String mnemonic) throws InvalidKeySpecException, NoSuchAlgorithmException, CipherException {
  // 1. we just need eth wallet for now
  AddressIndex ethAddressIndex = BIP44.m().purpose44().coinType(60).account(0).external().address(0);
  AddressIndex btcAddressIndex = BIP44.m().purpose44().coinType(0).account(0).external().address(0);
  // 2. calculate seed from mnemonics , then get master/root key ; Note that the bip39 passphrase we set "" for common
  String seed;
  String salt = "mnemonic";
  seed = getSeed(mnemonic, salt);
  System.out.println(seed);
  ExtendedPrivateKey rootKey = ExtendedPrivateKey.fromSeed(hexStringToByteArray(seed), Bitcoin.MAIN_NET);
  // 3. get child private key deriving from master/root key
  ExtendedPrivateKey childPrivateKey = rootKey.derive(ethAddressIndex, AddressIndex.DERIVATION);
  // 4. get key pair
  byte[] privateKeyBytes = childPrivateKey.getKey(); //child private key
  ECKeyPair keyPair = ECKeyPair.create(privateKeyBytes);
  walletFile = Wallet.createLight(password, keyPair);
  List<String> returnList = EthAddress(childPrivateKey, keyPair);
  childPrivateKey = rootKey.derive(btcAddressIndex, AddressIndex.DERIVATION);
  bitcoinAddress(childPrivateKey);
  return returnList;
}

代码示例来源:origin: uncleleonfan/FunWallet

@Override
  public void run() {
    try {
      String to = mTokenToAddressEdit.getText().toString();
      String amount = mTokenAmountEdit.getText().toString();
      Function transfer = transfer(to, new BigInteger(amount));
      ECKeyPair ecKeyPair = Wallet.decrypt("a12345678", mWalletFile);
      Credentials credentials = Credentials.create(ecKeyPair);
      String transactionHash = execute(credentials, transfer, CONTRACT_ADDRESS);
      Log.d(TAG, "onSendMET: " + transactionHash);
      runOnUiThread(new Runnable() {
        @Override
        public void run() {
          Toast.makeText(EthereumWalletActivity.this, transactionHash, Toast.LENGTH_SHORT).show();
        }
      });
    } catch (CipherException e) {
      e.printStackTrace();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
});

相关文章

微信公众号

最新文章

更多