mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-19 05:43:51 +01:00
Implement importdescriptors RPC (#5534)
This commit is contained in:
parent
2b497c634f
commit
c01793dc89
@ -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
|
||||
|
@ -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]
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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)))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user