mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-03-13 11:36:15 +01:00
Merge 0c4de4d48f
into 40b1ac351f
This commit is contained in:
commit
fb90ebfb97
1 changed files with 66 additions and 0 deletions
|
@ -22,8 +22,11 @@ import junitparams.JUnitParamsRunner;
|
|||
import junitparams.Parameters;
|
||||
import org.bitcoinj.base.BitcoinNetwork;
|
||||
import org.bitcoinj.base.ScriptType;
|
||||
import org.bitcoinj.base.Network;
|
||||
import org.bitcoinj.base.exceptions.AddressFormatException;
|
||||
import org.bitcoinj.base.internal.TimeUtils;
|
||||
import org.bitcoinj.crypto.AesKey;
|
||||
import org.bitcoinj.crypto.HDKeyDerivation;
|
||||
import org.bitcoinj.base.internal.ByteUtils;
|
||||
import org.bitcoinj.core.AbstractBlockChain;
|
||||
import org.bitcoinj.base.Address;
|
||||
|
@ -3534,4 +3537,67 @@ public class WalletTest extends TestWithWallet {
|
|||
Wallet wallet10 = Wallet.fromWatchingKeyB58(TESTNET, watchingKeyb58, Instant.ofEpochSecond(1415282801));
|
||||
assertEquals(TESTNET, wallet10.network());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromSpendingKey() {
|
||||
Network network = BitcoinNetwork.TESTNET;
|
||||
|
||||
DeterministicKey key1 = HDKeyDerivation.createMasterPrivateKey("satoshi lives!".getBytes());
|
||||
DeterministicKey spendingKey = HDKeyDerivation.deriveChildKey(key1, ChildNumber.ZERO_HARDENED);
|
||||
Instant creationTime = Instant.ofEpochSecond(1415282801);
|
||||
key1.setCreationTime(creationTime);
|
||||
Wallet wallet = Wallet.fromSpendingKey(network, spendingKey, ScriptType.P2PKH);
|
||||
|
||||
assertNotNull(wallet);
|
||||
assertEquals(network, wallet.network());
|
||||
assertTrue(wallet.isConsistent());
|
||||
assertEquals(creationTime, wallet.earliestKeyCreationTime());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
public void testFromSpendingKeyInvalidInput() {
|
||||
Network network = BitcoinNetwork.TESTNET;
|
||||
Wallet.fromSpendingKey(network, null, ScriptType.P2PKH); // Null key should throw an exception.
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromSpendingKeyB58() {
|
||||
Network network = BitcoinNetwork.TESTNET;
|
||||
DeterministicKey key1 = HDKeyDerivation.createMasterPrivateKey("satoshi lives!".getBytes());
|
||||
DeterministicKey key2 = HDKeyDerivation.deriveChildKey(key1, ChildNumber.ZERO_HARDENED);
|
||||
|
||||
final String priv58 = key2.serializePrivB58(network);
|
||||
Instant creationTime = Instant.ofEpochSecond(1415282801);
|
||||
|
||||
Wallet wallet = Wallet.fromSpendingKeyB58(network, priv58, creationTime);
|
||||
|
||||
assertNotNull(wallet);
|
||||
assertEquals(network, wallet.network());
|
||||
assertEquals(creationTime, wallet.earliestKeyCreationTime());
|
||||
assertTrue(wallet.isConsistent());
|
||||
assertEquals(DeterministicKey.deserializeB58(key1, priv58, network), key2);
|
||||
}
|
||||
|
||||
@Test(expected = AddressFormatException.class)
|
||||
public void testFromSpendingKeyB58InvalidBase58() {
|
||||
Network network = BitcoinNetwork.TESTNET;
|
||||
Wallet.fromSpendingKeyB58(network, "invalid_base58", Instant.now());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFromMasterKey() {
|
||||
Network network = BitcoinNetwork.TESTNET;
|
||||
DeterministicKey masterKey = HDKeyDerivation.createMasterPrivateKey(new byte[32]);
|
||||
Instant creationTime = Instant.ofEpochSecond(1415282801);
|
||||
masterKey.setCreationTime(creationTime);
|
||||
ChildNumber accountNumber = new ChildNumber(0, true);
|
||||
|
||||
Wallet wallet = Wallet.fromMasterKey(network, masterKey, ScriptType.P2PKH, accountNumber);
|
||||
|
||||
assertNotNull(wallet);
|
||||
assertEquals(network, wallet.network());
|
||||
assertTrue(wallet.isConsistent());
|
||||
assertEquals(creationTime, wallet.earliestKeyCreationTime());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue