1
0
Fork 0
mirror of https://github.com/bitcoin-s/bitcoin-s.git synced 2025-03-26 21:42:48 +01:00

Increase Key Manager test coverage ()

This commit is contained in:
Ben Carman 2020-05-26 06:13:01 -05:00 committed by GitHub
parent aa885688fd
commit eb37e551e0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 50 additions and 4 deletions
key-manager-test/src/test/scala/org/bitcoins/keymanager

View file

@ -6,7 +6,7 @@ import org.bitcoins.core.config.{MainNet, RegTest}
import org.bitcoins.core.crypto.MnemonicCode
import org.bitcoins.core.hd._
import org.bitcoins.core.util.TimeUtil
import org.bitcoins.crypto.DoubleSha256DigestBE
import org.bitcoins.crypto.{AesPassword, DoubleSha256DigestBE}
import org.bitcoins.keymanager._
import org.bitcoins.testkit.keymanager.{KeyManagerTestUtil, KeyManagerUnitTest}
import scodec.bits.BitVector
@ -104,6 +104,52 @@ class BIP39KeyManagerTest extends KeyManagerUnitTest {
assert(api.deriveXPub(hdAccount) == direct.deriveXPub(hdAccount))
}
it must "write a key manager to disk then initialize a key manager from a params" in {
val kmParams = buildParams()
val bip39Pw = KeyManagerTestUtil.bip39Password
val direct =
BIP39KeyManager(mnemonic, kmParams, Some(bip39Pw), TimeUtil.now)
val decryptedMnemonic = DecryptedMnemonic(mnemonic, direct.creationTime)
val password = AesPassword.fromNonEmptyString("password")
WalletStorage.writeMnemonicToDisk(kmParams.seedPath,
decryptedMnemonic.encrypt(password))
val directXpub = direct.getRootXPub
val api =
BIP39KeyManager.fromParams(kmParams, password, Some(bip39Pw)).right.get
val apiXpub = api.getRootXPub
assert(apiXpub == directXpub,
s"We don't have initialization symmetry between our constructors!")
//we should be able to derive the same child xpub
assert(api.deriveXPub(hdAccount) == direct.deriveXPub(hdAccount))
}
it must "initialize a key manager from a mnemonic to the same xpub if we call constructor directly or use CreateKeyManagerApi" in {
val kmParams = buildParams()
val bip39Pw = KeyManagerTestUtil.bip39Password
val direct =
BIP39KeyManager(mnemonic, kmParams, Some(bip39Pw), TimeUtil.now)
val directXpub = direct.getRootXPub
val api = BIP39KeyManager
.initializeWithMnemonic(mnemonic, Some(bip39Pw), kmParams)
.right
.get
val apiXpub = api.getRootXPub
assert(apiXpub == directXpub,
s"We don't have initialization symmetry between our constructors!")
//we should be able to derive the same child xpub
assert(api.deriveXPub(hdAccount) == direct.deriveXPub(hdAccount))
}
it must "NOT initialize a key manager to the same xpub if one has a password and one does not" in {
val kmParams = buildParams()
val bip39Pw = KeyManagerTestUtil.bip39PasswordNonEmpty

View file

@ -35,7 +35,7 @@ class HdUtilTest extends KeyManagerUnitTest {
HDUtil.getXpubVersion(HDPurposes.NestedSegWit, TestNet3) == NestedSegWitTestNet3Pub)
assertThrows[IllegalArgumentException] {
HDUtil.getXpubVersion(HDPurpose(-1), TestNet3)
HDUtil.getXpubVersion(HDPurpose(1), TestNet3)
}
}
@ -54,11 +54,11 @@ class HdUtilTest extends KeyManagerUnitTest {
HDUtil.getXprivVersion(HDPurposes.NestedSegWit, TestNet3) == NestedSegWitTestNet3Priv)
assertThrows[IllegalArgumentException] {
HDUtil.getXprivVersion(HDPurpose(-1), MainNet)
HDUtil.getXprivVersion(HDPurpose(1), MainNet)
}
assertThrows[IllegalArgumentException] {
HDUtil.getXprivVersion(HDPurpose(-1), TestNet3)
HDUtil.getXprivVersion(HDPurpose(1), TestNet3)
}
}