本文整理汇总了Java中org.bitcoinj.wallet.KeyChain类的典型用法代码示例。如果您正苦于以下问题:Java KeyChain类的具体用法?Java KeyChain怎么用?Java KeyChain使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
KeyChain类属于org.bitcoinj.wallet包,在下文中一共展示了KeyChain类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: derive
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void derive() throws Exception {
ECKey key1 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
ECKey key2 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
final Address address = new Address(UnitTestParams.get(), "n1bQNoEx8uhmCzzA5JPG6sFdtsUQhwiQJV");
assertEquals(address, key1.toAddress(UnitTestParams.get()));
assertEquals("mnHUcqUVvrfi5kAaXJDQzBb9HsWs78b42R", key2.toAddress(UnitTestParams.get()).toString());
assertEquals(key1, chain.findKeyFromPubHash(address.getHash160()));
assertEquals(key2, chain.findKeyFromPubKey(key2.getPubKey()));
key1.sign(Sha256Hash.ZERO_HASH);
ECKey key3 = chain.getKey(KeyChain.KeyPurpose.CHANGE);
assertEquals("mqumHgVDqNzuXNrszBmi7A2UpmwaPMx4HQ", key3.toAddress(UnitTestParams.get()).toString());
key3.sign(Sha256Hash.ZERO_HASH);
}
示例2: testRoundTripMarriedWallet
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void testRoundTripMarriedWallet() throws Exception {
// create 2-of-2 married wallet
myWallet = new Wallet(PARAMS);
final DeterministicKeyChain partnerChain = new DeterministicKeyChain(new SecureRandom());
DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, partnerChain.getWatchingKey().serializePubB58(PARAMS), PARAMS);
MarriedKeyChain chain = MarriedKeyChain.builder()
.random(new SecureRandom())
.followingKeys(partnerKey)
.threshold(2).build();
myWallet.addAndActivateHDChain(chain);
myAddress = myWallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
Wallet wallet1 = roundTrip(myWallet);
assertEquals(0, wallet1.getTransactions(true).size());
assertEquals(Coin.ZERO, wallet1.getBalance());
assertEquals(2, wallet1.getActiveKeyChain().getSigsRequiredToSpend());
assertEquals(myAddress, wallet1.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
}
示例3: encryption
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
public void encryption(SimpleHDKeyChain unencChain) throws UnreadableWalletException {
DeterministicKey key1 = unencChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
SimpleHDKeyChain encChain = unencChain.toEncrypted("open secret");
DeterministicKey encKey1 = encChain.findKeyFromPubKey(key1.getPubKey());
checkEncryptedKeyChain(encChain, key1);
// Round-trip to ensure de/serialization works and that we can store two chains and they both deserialize.
List<Protos.Key> serialized = encChain.toProtobuf();
System.out.println(protoToString(serialized));
encChain = SimpleHDKeyChain.fromProtobuf(serialized, encChain.getKeyCrypter());
checkEncryptedKeyChain(encChain, unencChain.findKeyFromPubKey(key1.getPubKey()));
DeterministicKey encKey2 = encChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
// Decrypt and check the keys match.
SimpleHDKeyChain decChain = encChain.toDecrypted("open secret");
DeterministicKey decKey1 = decChain.findKeyFromPubHash(encKey1.getPubKeyHash());
DeterministicKey decKey2 = decChain.findKeyFromPubHash(encKey2.getPubKeyHash());
assertEquals(decKey1.getPubKeyPoint(), encKey1.getPubKeyPoint());
assertEquals(decKey2.getPubKeyPoint(), encKey2.getPubKeyPoint());
assertFalse(decKey1.isEncrypted());
assertFalse(decKey2.isEncrypted());
assertNotEquals(encKey1.getParent(), decKey1.getParent()); // parts of a different hierarchy
// Check we can once again derive keys from the decrypted chain.
decChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS).sign(Sha256Hash.ZERO_HASH);
decChain.getKey(KeyChain.KeyPurpose.CHANGE).sign(Sha256Hash.ZERO_HASH);
}
示例4: issuedKeys
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void issuedKeys() throws Bip44KeyLookAheadExceededException {
List<BitAddress> issuedAddresses = new ArrayList<>();
assertEquals(0, pocket.getIssuedReceiveAddresses().size());
assertEquals(0, pocket.keys.getNumIssuedExternalKeys());
issuedAddresses.add(0, pocket.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
BitAddress freshAddress = pocket.getFreshReceiveAddress();
assertEquals(freshAddress, pocket.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
assertEquals(1, pocket.getIssuedReceiveAddresses().size());
assertEquals(1, pocket.keys.getNumIssuedExternalKeys());
assertEquals(issuedAddresses, pocket.getIssuedReceiveAddresses());
issuedAddresses.add(0, pocket.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
freshAddress = pocket.getFreshReceiveAddress();
assertEquals(freshAddress, pocket.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
assertEquals(2, pocket.getIssuedReceiveAddresses().size());
assertEquals(2, pocket.keys.getNumIssuedExternalKeys());
assertEquals(issuedAddresses, pocket.getIssuedReceiveAddresses());
}
示例5: bloom1
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void bloom1() {
DeterministicKey key2 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey key1 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
int numEntries =
(((chain.getLookaheadSize() + chain.getLookaheadThreshold()) * 2) // * 2 because of internal/external
+ chain.numLeafKeysIssued()
+ 4 // one root key + one account key + two chain keys (internal/external)
) * 2; // because the filter contains keys and key hashes.
assertEquals(numEntries, chain.numBloomFilterEntries());
BloomFilter filter = chain.getFilter(numEntries, 0.001, 1);
assertTrue(filter.contains(key1.getPubKey()));
assertTrue(filter.contains(key1.getPubKeyHash()));
assertTrue(filter.contains(key2.getPubKey()));
assertTrue(filter.contains(key2.getPubKeyHash()));
// The lookahead zone is tested in bloom2 and via KeyChainGroupTest.bloom
}
示例6: testRoundTripMarriedWallet
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void testRoundTripMarriedWallet() throws Exception {
// create 2-of-2 married wallet
myWallet = new Wallet(params);
final DeterministicKeyChain keyChain = new DeterministicKeyChain(new SecureRandom());
DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, keyChain.getWatchingKey().serializePubB58());
myWallet.addFollowingAccountKeys(ImmutableList.of(partnerKey), 2);
myAddress = myWallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
Wallet wallet1 = roundTrip(myWallet);
assertEquals(0, wallet1.getTransactions(true).size());
assertEquals(Coin.ZERO, wallet1.getBalance());
assertEquals(2, wallet1.getSigsRequiredToSpend());
assertEquals(myAddress, wallet1.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
}
示例7: testRoundTripMarriedWallet
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void testRoundTripMarriedWallet() throws Exception {
// create 2-of-2 married wallet
myWallet = new Wallet(params);
final DeterministicKeyChain partnerChain = new DeterministicKeyChain(new SecureRandom());
DeterministicKey partnerKey = DeterministicKey.deserializeB58(null, partnerChain.getWatchingKey().serializePubB58(params), params);
MarriedKeyChain chain = MarriedKeyChain.builder()
.random(new SecureRandom())
.followingKeys(partnerKey)
.threshold(2).build();
myWallet.addAndActivateHDChain(chain);
myAddress = myWallet.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS);
Wallet wallet1 = roundTrip(myWallet);
assertEquals(0, wallet1.getTransactions(true).size());
assertEquals(Coin.ZERO, wallet1.getBalance());
assertEquals(2, wallet1.getActiveKeychain().getSigsRequiredToSpend());
assertEquals(myAddress, wallet1.currentAddress(KeyChain.KeyPurpose.RECEIVE_FUNDS));
}
示例8: deriveKeyFromWallet
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
private ECKey deriveKeyFromWallet(final Wallet wallet, final int iteration, final KeyChain.KeyPurpose keyPurpose) throws UnreadableWalletException, IOException {
DeterministicKey key = null;
for (int i = 0; i <= iteration; i++) {
key = wallet.freshKey(keyPurpose);
}
if (key == null) {
throw new IOException("Unable to derive key");
}
return ECKey.fromPrivate(key.getPrivKey());
}
示例9: getKey
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void getKey() {
ECKey key1 = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
assertTrue(onKeysAddedRan.getAndSet(false));
assertEquals(key1, onKeysAdded.getAndSet(null).get(0));
ECKey key2 = chain.getKey(KeyChain.KeyPurpose.CHANGE);
assertFalse(onKeysAddedRan.getAndSet(false));
assertEquals(key2, key1);
}
示例10: cannotImportEncryptedKey
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test(expected = KeyCrypterException.class)
public void cannotImportEncryptedKey() {
final ECKey key1 = new ECKey();
chain.importKeys(ImmutableList.of(key1));
chain = chain.toEncrypted("foobar");
ECKey encryptedKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
assertTrue(encryptedKey.isEncrypted());
BasicKeyChain chain2 = new BasicKeyChain();
chain2.importKeys(ImmutableList.of(encryptedKey));
}
示例11: getLastIssuedKey
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void getLastIssuedKey() {
assertNull(chain.getLastIssuedKey(KeyChain.KeyPurpose.RECEIVE_FUNDS));
assertNull(chain.getLastIssuedKey(KeyChain.KeyPurpose.CHANGE));
DeterministicKey extKey = chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey intKey = chain.getKey(KeyChain.KeyPurpose.CHANGE);
assertEquals(extKey, chain.getLastIssuedKey(KeyChain.KeyPurpose.RECEIVE_FUNDS));
assertEquals(intKey, chain.getLastIssuedKey(KeyChain.KeyPurpose.CHANGE));
}
示例12: serializeUnencrypted
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
public void serializeUnencrypted(SimpleHDKeyChain keyChain, String expectedSerialization) throws UnreadableWalletException {
keyChain.setLookaheadSize(10);
keyChain.maybeLookAhead();
DeterministicKey key1 = keyChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey key2 = keyChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
DeterministicKey key3 = keyChain.getKey(KeyChain.KeyPurpose.CHANGE);
List<Protos.Key> keys = keyChain.toProtobuf();
// 1 master key, 1 account key, 2 internal keys, 3 derived, 20 lookahead and 5 lookahead threshold.
int numItems =
1 // master key/account key
+ 2 // ext/int parent keys
+ (keyChain.getLookaheadSize() + keyChain.getLookaheadThreshold()) * 2 // lookahead zone on each chain
;
assertEquals(numItems, keys.size());
// Get another key that will be lost during round-tripping, to ensure we can derive it again.
DeterministicKey key4 = keyChain.getKey(KeyChain.KeyPurpose.CHANGE);
String sb = protoToString(keys);
assertEquals(expectedSerialization, sb);
// Round trip the data back and forth to check it is preserved.
int oldLookaheadSize = keyChain.getLookaheadSize();
keyChain = SimpleHDKeyChain.fromProtobuf(keys, null);
assertEquals(expectedSerialization, protoToString(keyChain.toProtobuf()));
assertEquals(key1, keyChain.findKeyFromPubHash(key1.getPubKeyHash()));
assertEquals(key2, keyChain.findKeyFromPubHash(key2.getPubKeyHash()));
assertEquals(key3, keyChain.findKeyFromPubHash(key3.getPubKeyHash()));
assertEquals(key4, keyChain.getKey(KeyChain.KeyPurpose.CHANGE));
key1.sign(Sha256Hash.ZERO_HASH);
key2.sign(Sha256Hash.ZERO_HASH);
key3.sign(Sha256Hash.ZERO_HASH);
key4.sign(Sha256Hash.ZERO_HASH);
assertEquals(oldLookaheadSize, keyChain.getLookaheadSize());
}
示例13: checkEncryptedKeyChain
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
private void checkEncryptedKeyChain(SimpleHDKeyChain encChain, DeterministicKey key1) {
// Check we can look keys up and extend the chain without the AES key being provided.
DeterministicKey encKey1 = encChain.findKeyFromPubKey(key1.getPubKey());
DeterministicKey encKey2 = encChain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
assertFalse(key1.isEncrypted());
assertTrue(encKey1.isEncrypted());
assertEquals(encKey1.getPubKeyPoint(), key1.getPubKeyPoint());
final KeyParameter aesKey = checkNotNull(encChain.getKeyCrypter()).deriveKey("open secret");
encKey1.sign(Sha256Hash.ZERO_HASH, aesKey);
encKey2.sign(Sha256Hash.ZERO_HASH, aesKey);
assertTrue(encChain.checkAESKey(aesKey));
assertFalse(encChain.checkPassword("access denied"));
assertTrue(encChain.checkPassword("open secret"));
}
示例14: testSingleAddressWallet
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void testSingleAddressWallet() throws Exception {
ECKey key = pocket.keys.getCurrentUnusedKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
BitWalletSingleKey bitWalletSingleKey = new BitWalletSingleKey(DOGE, key);
bitWalletSingleKey.onConnection(getBlockchainConnection(DOGE));
assertEquals(DOGE.value("10"), bitWalletSingleKey.getBalance());
}
示例15: getKeys
import org.bitcoinj.wallet.KeyChain; //导入依赖的package包/类
@Test
public void getKeys() throws Exception {
chain.getKey(KeyChain.KeyPurpose.RECEIVE_FUNDS);
chain.getKey(KeyChain.KeyPurpose.CHANGE);
chain.maybeLookAhead();
assertEquals(2, chain.getKeys(false).size());
}