diff --git a/wallet-test/src/test/scala/org/bitcoins/wallet/models/AddressDAOTest.scala b/wallet-test/src/test/scala/org/bitcoins/wallet/models/AddressDAOTest.scala index 7dbd7b1606..59a09c7ef8 100644 --- a/wallet-test/src/test/scala/org/bitcoins/wallet/models/AddressDAOTest.scala +++ b/wallet-test/src/test/scala/org/bitcoins/wallet/models/AddressDAOTest.scala @@ -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) + } + } }