Implement importdescriptors RPC (#5534)

This commit is contained in:
Chris Stewart 2024-04-19 17:11:26 -05:00 committed by GitHub
parent 2b497c634f
commit c01793dc89
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 3 deletions

View File

@ -474,7 +474,7 @@ case class ListDescriptorsResult(
case class DescriptorsResult(
desc: Descriptor,
timestamp: ZonedDateTime,
timestamp: Long,
active: Boolean,
internal: Option[Boolean],
range: Option[Vector[Int]],
@ -505,3 +505,8 @@ final case class CreateWalletResult(
name: String,
warning: String
) extends WalletResult
case class ImportDescriptorResult(
success: Boolean,
warnings: Option[Vector[String]])
extends WalletResult

View File

@ -524,6 +524,9 @@ object JsonSerializers {
implicit val descriptorsResultReads: Reads[DescriptorsResult] =
Json.reads[DescriptorsResult]
implicit val descriptorResultWrites: Writes[DescriptorsResult] =
Json.writes[DescriptorsResult]
implicit val listDescriptorsReads: Reads[ListDescriptorsResult] =
Json.reads[ListDescriptorsResult]
@ -930,4 +933,8 @@ object JsonSerializers {
}
}
implicit val importDescriptorResultReads: Reads[ImportDescriptorResult] = {
Json.reads[ImportDescriptorResult]
}
}

View File

@ -4,7 +4,8 @@ import org.bitcoins.commons.jsonmodels.bitcoind.RpcOpts.AddressType
import org.bitcoins.commons.jsonmodels.bitcoind.{
AddressInfoResultPostV18,
AddressInfoResultPostV21,
AddressInfoResultPreV18
AddressInfoResultPreV18,
DescriptorsResult
}
import org.bitcoins.core.api.chain.db.BlockHeaderDbHelper
import org.bitcoins.core.currency._
@ -16,6 +17,8 @@ import org.bitcoins.rpc.client.v24.BitcoindV24RpcClient
import org.bitcoins.testkit.chain.BlockHeaderHelper
import org.bitcoins.testkit.rpc.BitcoindFixturesFundedCachedV24
import java.time.Instant
class BitcoindV24RpcClientTest extends BitcoindFixturesFundedCachedV24 {
val junkAddress: BitcoinAddress =
@ -154,4 +157,31 @@ class BitcoindV24RpcClientTest extends BitcoindFixturesFundedCachedV24 {
assert1
}
it must "importdescriptors" in { client =>
val str1 =
"wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)#kft60nuy"
val descriptor = Descriptor.fromString(str1)
val imp = DescriptorsResult(desc = descriptor,
timestamp = Instant.now().getEpochSecond,
active = true,
internal = None,
range = Some(Vector(0, 2)),
next = None)
val resultF = client.importDescriptors(Vector(imp))
for {
result <- resultF
_ = assert(result.forall(_.success))
firstAddress <- client.getNewAddress
secondAddress <- client.getNewAddress
//check it by deriving addresses externally
deriveAddresses <- client
.deriveAddresses(descriptor, Some(Vector(0, 1)))
.map(_.addresses)
} yield {
assert(Vector(firstAddress, secondAddress) == deriveAddresses)
}
}
}

View File

@ -2,7 +2,9 @@ package org.bitcoins.rpc.client.common
import org.bitcoins.commons.jsonmodels.bitcoind.{
DeriveAddressesResult,
GetDescriptorInfoResult
DescriptorsResult,
GetDescriptorInfoResult,
ImportDescriptorResult
}
import org.bitcoins.commons.serializers.JsonSerializers._
import org.bitcoins.commons.serializers.JsonWriters.DescriptorWrites
@ -34,4 +36,14 @@ trait DescriptorRpc {
"getdescriptorinfo",
List(DescriptorWrites.writes(descriptor)))
}
/** https://bitcoincore.org/en/doc/22.0.0/rpc/wallet/importdescriptors/
* @param imports
* @return
*/
def importDescriptors(imports: Vector[DescriptorsResult]): Future[
Vector[ImportDescriptorResult]] = {
bitcoindCall[Vector[ImportDescriptorResult]]("importdescriptors",
List(Json.toJson(imports)))
}
}