Add unit test for address creation when we are already watching the script (#4152)

* Add unit test for address creation when we are already watching the script

* revert wallet

* Add upsert test too

* revert test again
This commit is contained in:
Chris Stewart 2022-03-03 07:36:44 -06:00 committed by GitHub
parent 374c1d7b9f
commit 4cefa56c99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,17 @@
package org.bitcoins.wallet.models
import org.bitcoins.core.api.wallet.db.AddressRecord
import org.bitcoins.core.api.wallet.db.{
AddressRecord,
ScriptPubKeyDb,
SegWitAddressDb
}
import org.bitcoins.core.hd.SegWitHDPath
import org.bitcoins.core.protocol.Bech32Address
import org.bitcoins.core.protocol.script.{
EmptyScriptWitness,
P2WPKHWitnessSPKV0
}
import org.bitcoins.crypto.ECPublicKey
import org.bitcoins.testkit.fixtures.WalletDAOFixture
import org.bitcoins.testkit.wallet.WalletTestUtil
@ -79,4 +90,70 @@ class AddressDAOTest extends WalletDAOFixture {
assert(!found.contains(created3))
}
}
it must "insert an address into the database whose script is already being watched" in {
daos =>
val spkDAO = daos.scriptPubKeyDAO
val addressDAO = daos.addressDAO
val addrStr = "bc1qfjex5a4m5w0atqrpwad3zj4vkfkuhun46tge9c"
val address = Bech32Address.fromString(addrStr)
val spk = address.scriptPubKey.asInstanceOf[P2WPKHWitnessSPKV0]
//insert the script first
val spkDb = ScriptPubKeyDb(address.scriptPubKey)
val createdSpkF = spkDAO.create(spkDb)
//now try to insert the address in the database
val segwitHdPath: SegWitHDPath =
SegWitHDPath.fromString("m/84'/0'/0'/0/0")
val pubKey: ECPublicKey = ECPublicKey.freshPublicKey
val addressDb = SegWitAddressDb.apply(segwitHdPath,
pubKey,
spk.pubKeyHash,
address,
EmptyScriptWitness,
spk)
for {
createdSpk <- createdSpkF
_ <- addressDAO.create(addressDb)
//make sure we can find it now
foundOpt <- addressDAO.read(address)
} yield {
assert(foundOpt.isDefined)
assert(foundOpt.get.scriptPubKeyId == createdSpk.id.get)
assert(foundOpt.get.address == address)
}
}
it must "upsert an address into the database whose script is already being watched" in {
daos =>
val spkDAO = daos.scriptPubKeyDAO
val addressDAO = daos.addressDAO
val addrStr = "bc1qfjex5a4m5w0atqrpwad3zj4vkfkuhun46tge9c"
val address = Bech32Address.fromString(addrStr)
val spk = address.scriptPubKey.asInstanceOf[P2WPKHWitnessSPKV0]
//insert the script first
val spkDb = ScriptPubKeyDb(address.scriptPubKey)
val createdSpkF = spkDAO.create(spkDb)
//now try to insert the address in the database
val segwitHdPath: SegWitHDPath =
SegWitHDPath.fromString("m/84'/0'/0'/0/0")
val pubKey: ECPublicKey = ECPublicKey.freshPublicKey
val addressDb = SegWitAddressDb.apply(segwitHdPath,
pubKey,
spk.pubKeyHash,
address,
EmptyScriptWitness,
spk)
for {
createdSpk <- createdSpkF
_ <- addressDAO.upsert(addressDb)
//make sure we can find it now
foundOpt <- addressDAO.read(address)
} yield {
assert(foundOpt.isDefined)
assert(foundOpt.get.scriptPubKeyId == createdSpk.id.get)
assert(foundOpt.get.address == address)
}
}
}