• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Java ECKeyPair类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Java中org.web3j.crypto.ECKeyPair的典型用法代码示例。如果您正苦于以下问题:Java ECKeyPair类的具体用法?Java ECKeyPair怎么用?Java ECKeyPair使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



ECKeyPair类属于org.web3j.crypto包,在下文中一共展示了ECKeyPair类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: testCreateAccountFromScratch

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
@Test
public void testCreateAccountFromScratch() throws Exception {
	
	// create new private/public key pair
	ECKeyPair keyPair = Keys.createEcKeyPair();
	
	BigInteger publicKey = keyPair.getPublicKey();
	String publicKeyHex = Numeric.toHexStringWithPrefix(publicKey);
	
	BigInteger privateKey = keyPair.getPrivateKey();
	String privateKeyHex = Numeric.toHexStringWithPrefix(privateKey);
	
	// create credentials + address from private/public key pair
	Credentials credentials = Credentials.create(new ECKeyPair(privateKey, publicKey));
	String address = credentials.getAddress();
	
	// print resulting data of new account
	System.out.println("private key: '" + privateKeyHex + "'");
	System.out.println("public key: '" + publicKeyHex + "'");
	System.out.println("address: '" + address + "'\n");
	
	// test (1) check if it's possible to transfer funds to new address
	BigInteger amountWei = Convert.toWei("0.131313", Convert.Unit.ETHER).toBigInteger();
	transferWei(getCoinbase(), address, amountWei);

	BigInteger balanceWei = getBalanceWei(address);
	BigInteger nonce = getNonce(address);
	
	assertEquals("Unexpected nonce for 'to' address", BigInteger.ZERO, nonce);
	assertEquals("Unexpected balance for 'to' address", amountWei, balanceWei);

	// test (2) funds can be transferred out of the newly created account
	BigInteger txFees = Web3jConstants.GAS_LIMIT_ETHER_TX.multiply(Web3jConstants.GAS_PRICE);
	RawTransaction txRaw = RawTransaction
			.createEtherTransaction(
					nonce, 
					Web3jConstants.GAS_PRICE, 
					Web3jConstants.GAS_LIMIT_ETHER_TX, 
					getCoinbase(), 
					amountWei.subtract(txFees));

	// sign raw transaction using the sender's credentials
	byte[] txSignedBytes = TransactionEncoder.signMessage(txRaw, credentials);
	String txSigned = Numeric.toHexString(txSignedBytes);

	// send the signed transaction to the ethereum client
	EthSendTransaction ethSendTx = web3j
			.ethSendRawTransaction(txSigned)
			.sendAsync()
			.get();

	Error error = ethSendTx.getError();
	String txHash = ethSendTx.getTransactionHash();
	assertNull(error);		
	assertFalse(txHash.isEmpty());
	
	waitForReceipt(txHash);

	assertEquals("Unexpected nonce for 'to' address", BigInteger.ONE, getNonce(address));
	assertTrue("Balance for 'from' address too large: " + getBalanceWei(address), getBalanceWei(address).compareTo(txFees) < 0);
}
 
开发者ID:matthiaszimmermann,项目名称:web3j_demo,代码行数:62,代码来源:CreateAccountTest.java


示例2: generateWalletFile

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
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 objectMapper = ObjectMapperFactory.getObjectMapper();
    objectMapper.writeValue(destination, walletFile);

    return fileName;
}
 
开发者ID:manuelsc,项目名称:Lunary-Ethereum-Wallet,代码行数:20,代码来源:OwnWalletUtils.java


示例3: generateWallet

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public static Wallet generateWallet(Context context, ECKeyPair ecKeyPair,
                                    final String password)
        throws WalletNotGeneratedException
{
    try {
        final String address = Numeric.prependHexPrefix(Keys.getAddress(ecKeyPair));
        final File destDirectory = Environment.getExternalStorageDirectory().getAbsoluteFile();
        Log.d(TAG, Environment.getExternalStorageState());
        final String fileName = WalletUtils.generateWalletFile(password, ecKeyPair, destDirectory, false);
        final String destFilePath = destDirectory + "/" + fileName;

        return new Wallet(ecKeyPair, destFilePath, address);
    } catch (CipherException | IOException e)
    {
        e.printStackTrace();
        throw new WalletNotGeneratedException();
    }
}
 
开发者ID:humaniq,项目名称:humaniq-android,代码行数:19,代码来源:Wallet.java


示例4: decodeKeyPair

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public static KeyPair decodeKeyPair(ECKeyPair ecKeyPair) {
    byte[] bytes = Numeric.toBytesPadded(ecKeyPair.getPublicKey(), 64);
    BigInteger x = Numeric.toBigInt(Arrays.copyOfRange(bytes, 0, 32));
    BigInteger y = Numeric.toBigInt(Arrays.copyOfRange(bytes, 32, 64));
    ECPoint q = curve.createPoint(x, y);
    BCECPublicKey publicKey = new BCECPublicKey(ALGORITHM, new ECPublicKeyParameters(q, dp), BouncyCastleProvider.CONFIGURATION);
    BCECPrivateKey privateKey = new BCECPrivateKey(ALGORITHM, new ECPrivateKeyParameters(ecKeyPair.getPrivateKey(), dp), publicKey, p, BouncyCastleProvider.CONFIGURATION);
    return new KeyPair(publicKey, privateKey);
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:10,代码来源:CryptoUtil.java


示例5: importPrivateKey

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
@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));
}
 
开发者ID:TrustWallet,项目名称:trust-wallet-android,代码行数:10,代码来源:GethKeystoreAccountService.java


示例6: configureSsl

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
private void configureSsl(NettyServerBuilder builder) throws NoSuchAlgorithmException, CertificateEncodingException, NoSuchProviderException, InvalidKeyException, SignatureException, SSLException {
    NativeLibraryLoader.loadFirstAvailable(ClassLoader.getSystemClassLoader(),
        "netty_tcnative_osx_x86_64",
        "netty_tcnative_linux_x86_64",
        "netty_tcnative_windows_x86_64"
    );
    ECKeyPair ecKeyPair = ethereumConfig.getMainCredentials().getEcKeyPair();
    KeyPair keyPair = CryptoUtil.decodeKeyPair(ecKeyPair);
    SslContextBuilder contextBuilder = SslContextBuilder.forServer(
        keyPair.getPrivate(),
        CryptoUtil.genCert(keyPair)
    );

    builder.sslContext(GrpcSslContexts.configure(contextBuilder).build());
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:16,代码来源:NodeServer.java


示例7: test

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
@Test
public void test() throws IOException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, SignatureException {
    long channelId = 1;
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Address sender = new Address(Keys.getAddress(keyPair));
    int blockNumber = 1;
    File file = File.createTempFile("state_channels", ".data");
    BlockId blockId = new BlockId(channelId, blockNumber);
    FileBlockWriter store = new FileBlockWriter(blockId, file);
    Assert.assertEquals(0, store.length());

    byte[] data = {1, 2, 3};
    ChannelTransaction transaction = new ChannelTransaction(channelId, blockNumber, data);
    transaction.sign(keyPair);
    store.append(transaction);

    byte[] data1 = {1, 2, 3};
    transaction = new ChannelTransaction(channelId, blockNumber, data1);
    transaction.sign(keyPair);
    store.append(transaction);
    store.close();

    Iterator<ChannelTransaction> iterator = new FileBlockReader(blockId, file).readAll();

    Assert.assertTrue(iterator.hasNext());
    ChannelTransaction transaction1 = iterator.next();
    Assert.assertEquals(channelId, transaction1.getChannelId());
    Assert.assertEquals(blockNumber, transaction1.getBlockNumber());
    Assert.assertArrayEquals(data, transaction1.getData());
    Assert.assertEquals(sender, transaction1.getSignerAddress());

    Assert.assertTrue(iterator.hasNext());
    transaction1 = iterator.next();
    Assert.assertEquals(channelId, transaction1.getChannelId());
    Assert.assertEquals(blockNumber, transaction1.getBlockNumber());
    Assert.assertArrayEquals(data1, transaction1.getData());
    Assert.assertEquals(sender, transaction1.getSignerAddress());
    
    Assert.assertFalse(iterator.hasNext());
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:41,代码来源:BlockWriteReadTest.java


示例8: generateNewWalletFile

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public static String generateNewWalletFile(
        String password, File destinationDirectory, boolean useFullScrypt)
        throws CipherException, IOException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException, NoSuchProviderException {

    ECKeyPair ecKeyPair = Keys.createEcKeyPair();
    return generateWalletFile(password, ecKeyPair, destinationDirectory, useFullScrypt);
}
 
开发者ID:manuelsc,项目名称:Lunary-Ethereum-Wallet,代码行数:9,代码来源:OwnWalletUtils.java


示例9: MyWallet

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public MyWallet(String privateKey) throws Exception {
  if (!WalletUtils.isValidPrivateKey(privateKey)) {
    throw new Exception("Private Key error");
  }
  credentials = Credentials.create(ECKeyPair.create(hexStringToByteArray(privateKey)));
}
 
开发者ID:AtlantPlatform,项目名称:atlant-android,代码行数:7,代码来源:MyWallet.java


示例10: sign

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public static byte[] sign(ECKeyPair keyPair, byte[] hash) {
    Sign.SignatureData signatureData = Sign.signMessage(hash, keyPair);
    return signatureEncode(signatureData);
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:5,代码来源:CryptoUtil.java


示例11: main

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public static void main(String[] args) throws InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException {
    ECKeyPair ecKeyPair = Keys.createEcKeyPair();
    KeyPair keyPair1 = decodeKeyPair(ecKeyPair);
    System.out.println(toString(keyPair1));
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:6,代码来源:CryptoUtil.java


示例12: sign

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public void sign(ECKeyPair keyPair) {
    byte[] hash = hash();
    signature = CryptoUtil.sign(keyPair, hash);
}
 
开发者ID:papyrusglobal,项目名称:state-channels,代码行数:5,代码来源:SignedObject.java


示例13: getEcKeyPair

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
public ECKeyPair getEcKeyPair() {
    return ecKeyPair;
}
 
开发者ID:humaniq,项目名称:humaniq-android,代码行数:4,代码来源:Wallet.java


示例14: Wallet

import org.web3j.crypto.ECKeyPair; //导入依赖的package包/类
private Wallet(ECKeyPair ecKeyPair, final String walletFile, final String address) {
    this.walletFile = walletFile;
    this.address = address;
    this.ecKeyPair = ecKeyPair;
}
 
开发者ID:humaniq,项目名称:humaniq-android,代码行数:6,代码来源:Wallet.java



注:本文中的org.web3j.crypto.ECKeyPair类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Java TByteCharMap类代码示例发布时间:2022-05-23
下一篇:
Java TermSpans类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap