mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-01-19 05:43:51 +01:00
Remove CompatEither, it was needed for historical purposes to support… (#2394)
* Remove CompatEither, it was needed for historical purposes to support Scala 2.11.x * Revert files from another change
This commit is contained in:
parent
2c25057fd6
commit
97d4ec8490
@ -1,6 +1,5 @@
|
||||
package org.bitcoins.keymanager
|
||||
|
||||
import org.bitcoins.core.compat.CompatEither
|
||||
import org.bitcoins.core.crypto._
|
||||
import org.bitcoins.crypto.{AesCrypt, AesEncryptedData, AesPassword, AesSalt}
|
||||
import scodec.bits.ByteVector
|
||||
@ -48,7 +47,7 @@ case class EncryptedSeed(
|
||||
private def decryptStr(password: AesPassword): Try[String] = {
|
||||
val key = password.toKey(salt)
|
||||
val either = AesCrypt.decrypt(value, key)
|
||||
CompatEither(either).toTry.flatMap { decrypted =>
|
||||
either.toTry.flatMap { decrypted =>
|
||||
decrypted.decodeUtf8 match {
|
||||
case Left(_) =>
|
||||
// when failing to decode this to a UTF-8 string
|
||||
|
@ -1,6 +1,5 @@
|
||||
package org.bitcoins.keymanager
|
||||
|
||||
import org.bitcoins.core.compat._
|
||||
import org.bitcoins.core.crypto._
|
||||
import org.bitcoins.crypto._
|
||||
import scodec.bits.ByteVector
|
||||
@ -156,22 +155,22 @@ object WalletStorage extends KeyManagerLogger {
|
||||
}
|
||||
|
||||
private def readJsonFromDisk(
|
||||
seedPath: Path): CompatEither[ReadMnemonicError, Value] = {
|
||||
seedPath: Path): Either[ReadMnemonicError, Value] = {
|
||||
if (Files.isRegularFile(seedPath)) {
|
||||
val rawJson = Files.readAllLines(seedPath).asScala.mkString("\n")
|
||||
logger.debug(s"Read raw mnemonic from $seedPath")
|
||||
|
||||
Try(ujson.read(rawJson)) match {
|
||||
case Failure(ujson.ParseException(clue, _, _, _)) =>
|
||||
CompatLeft(ReadMnemonicError.JsonParsingError(clue))
|
||||
Left(ReadMnemonicError.JsonParsingError(clue))
|
||||
case Failure(exception) => throw exception
|
||||
case Success(value) =>
|
||||
logger.debug(s"Parsed $seedPath into valid json")
|
||||
CompatRight(value)
|
||||
Right(value)
|
||||
}
|
||||
} else {
|
||||
logger.error(s"Mnemonic not found at $seedPath")
|
||||
CompatLeft(ReadMnemonicError.NotFoundError)
|
||||
Left(ReadMnemonicError.NotFoundError)
|
||||
}
|
||||
}
|
||||
|
||||
@ -179,14 +178,14 @@ object WalletStorage extends KeyManagerLogger {
|
||||
* performing no decryption
|
||||
*/
|
||||
private def readEncryptedMnemonicFromDisk(
|
||||
seedPath: Path): CompatEither[ReadMnemonicError, EncryptedSeed] = {
|
||||
seedPath: Path): Either[ReadMnemonicError, EncryptedSeed] = {
|
||||
|
||||
val jsonE = readJsonFromDisk(seedPath)
|
||||
|
||||
import MnemonicJsonKeys._
|
||||
import ReadMnemonicError._
|
||||
|
||||
val readJsonTupleEither: CompatEither[
|
||||
val readJsonTupleEither: Either[
|
||||
ReadMnemonicError,
|
||||
(String, String, String, Long)] = jsonE.flatMap { json =>
|
||||
logger.trace(s"Read encrypted mnemonic JSON: $json")
|
||||
@ -197,13 +196,13 @@ object WalletStorage extends KeyManagerLogger {
|
||||
val rawSaltString = json(SALT).str
|
||||
(ivString, cipherTextString, rawSaltString, creationTimeNum)
|
||||
} match {
|
||||
case Success(value) => CompatRight(value)
|
||||
case Success(value) => Right(value)
|
||||
case Failure(exception) =>
|
||||
CompatLeft(JsonParsingError(exception.getMessage))
|
||||
Left(JsonParsingError(exception.getMessage))
|
||||
}
|
||||
}
|
||||
|
||||
val encryptedEither: CompatEither[ReadMnemonicError, EncryptedSeed] =
|
||||
val encryptedEither: Either[ReadMnemonicError, EncryptedSeed] =
|
||||
readJsonTupleEither.flatMap {
|
||||
case (rawIv, rawCipherText, rawSalt, rawCreationTime) =>
|
||||
val encryptedOpt = for {
|
||||
@ -217,39 +216,38 @@ object WalletStorage extends KeyManagerLogger {
|
||||
salt,
|
||||
Instant.ofEpochSecond(rawCreationTime))
|
||||
}
|
||||
val toRight: Option[CompatRight[ReadMnemonicError, EncryptedSeed]] =
|
||||
val toRight: Option[Right[ReadMnemonicError, EncryptedSeed]] =
|
||||
encryptedOpt
|
||||
.map(CompatRight(_))
|
||||
.map(Right(_))
|
||||
|
||||
toRight.getOrElse(
|
||||
CompatLeft(JsonParsingError("JSON contents was not hex strings")))
|
||||
Left(JsonParsingError("JSON contents was not hex strings")))
|
||||
}
|
||||
encryptedEither
|
||||
}
|
||||
|
||||
/** Reads the raw unencrypted mnemonic from disk */
|
||||
private def readUnencryptedMnemonicFromDisk(
|
||||
seedPath: Path): CompatEither[ReadMnemonicError, DecryptedMnemonic] = {
|
||||
seedPath: Path): Either[ReadMnemonicError, DecryptedMnemonic] = {
|
||||
|
||||
val jsonE = readJsonFromDisk(seedPath)
|
||||
|
||||
import MnemonicJsonKeys._
|
||||
import ReadMnemonicError._
|
||||
|
||||
val readJsonTupleEither: CompatEither[
|
||||
ReadMnemonicError,
|
||||
(Vector[String], Long)] = jsonE.flatMap { json =>
|
||||
logger.trace(s"Read mnemonic JSON: Masked(json)")
|
||||
Try {
|
||||
val creationTimeNum = parseCreationTime(json)
|
||||
val words = json(MNEMONIC_SEED).arr.toVector.map(_.str)
|
||||
(words, creationTimeNum)
|
||||
} match {
|
||||
case Success(value) => CompatRight(value)
|
||||
case Failure(exception) =>
|
||||
CompatLeft(JsonParsingError(exception.getMessage))
|
||||
val readJsonTupleEither: Either[ReadMnemonicError, (Vector[String], Long)] =
|
||||
jsonE.flatMap { json =>
|
||||
logger.trace(s"Read mnemonic JSON: Masked(json)")
|
||||
Try {
|
||||
val creationTimeNum = parseCreationTime(json)
|
||||
val words = json(MNEMONIC_SEED).arr.toVector.map(_.str)
|
||||
(words, creationTimeNum)
|
||||
} match {
|
||||
case Success(value) => Right(value)
|
||||
case Failure(exception) =>
|
||||
Left(JsonParsingError(exception.getMessage))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readJsonTupleEither.flatMap {
|
||||
case (words, rawCreationTime) =>
|
||||
@ -261,25 +259,25 @@ object WalletStorage extends KeyManagerLogger {
|
||||
Instant.ofEpochSecond(rawCreationTime))
|
||||
}
|
||||
|
||||
val toRight: Try[CompatRight[ReadMnemonicError, DecryptedMnemonic]] =
|
||||
val toRight: Try[Right[ReadMnemonicError, DecryptedMnemonic]] =
|
||||
decryptedMnemonicT
|
||||
.map(CompatRight(_))
|
||||
.map(Right(_))
|
||||
|
||||
toRight.getOrElse(
|
||||
CompatLeft(JsonParsingError("JSON contents was correctly formatted")))
|
||||
Left(JsonParsingError("JSON contents was correctly formatted")))
|
||||
}
|
||||
}
|
||||
|
||||
/** Reads the raw unencrypted xprv from disk */
|
||||
private def readUnencryptedSeedFromDisk(
|
||||
seedPath: Path): CompatEither[ReadMnemonicError, DecryptedExtPrivKey] = {
|
||||
seedPath: Path): Either[ReadMnemonicError, DecryptedExtPrivKey] = {
|
||||
|
||||
val jsonE = readJsonFromDisk(seedPath)
|
||||
|
||||
import MnemonicJsonKeys._
|
||||
import ReadMnemonicError._
|
||||
|
||||
val readJsonTupleEither: CompatEither[ReadMnemonicError, (String, Long)] =
|
||||
val readJsonTupleEither: Either[ReadMnemonicError, (String, Long)] =
|
||||
jsonE.flatMap { json =>
|
||||
logger.trace(s"Read mnemonic JSON: Masked(json)")
|
||||
Try {
|
||||
@ -287,9 +285,9 @@ object WalletStorage extends KeyManagerLogger {
|
||||
val xprvStr = json(XPRV).str
|
||||
(xprvStr, creationTimeNum)
|
||||
} match {
|
||||
case Success(value) => CompatRight(value)
|
||||
case Success(value) => Right(value)
|
||||
case Failure(exception) =>
|
||||
CompatLeft(JsonParsingError(exception.getMessage))
|
||||
Left(JsonParsingError(exception.getMessage))
|
||||
}
|
||||
}
|
||||
|
||||
@ -300,12 +298,12 @@ object WalletStorage extends KeyManagerLogger {
|
||||
DecryptedExtPrivKey(xprv, Instant.ofEpochSecond(rawCreationTime))
|
||||
}
|
||||
|
||||
val toRight: Try[CompatRight[ReadMnemonicError, DecryptedExtPrivKey]] =
|
||||
val toRight: Try[Right[ReadMnemonicError, DecryptedExtPrivKey]] =
|
||||
decryptedExtPrivKeyT
|
||||
.map(CompatRight(_))
|
||||
.map(Right(_))
|
||||
|
||||
toRight.getOrElse(
|
||||
CompatLeft(JsonParsingError("JSON contents was correctly formatted")))
|
||||
Left(JsonParsingError("JSON contents was correctly formatted")))
|
||||
}
|
||||
}
|
||||
|
||||
@ -318,7 +316,7 @@ object WalletStorage extends KeyManagerLogger {
|
||||
passphraseOpt: Option[AesPassword]): Either[
|
||||
ReadMnemonicError,
|
||||
DecryptedSeedState] = {
|
||||
val decryptedEither: CompatEither[ReadMnemonicError, DecryptedSeedState] =
|
||||
val decryptedEither: Either[ReadMnemonicError, DecryptedSeedState] =
|
||||
passphraseOpt match {
|
||||
case Some(passphrase) =>
|
||||
val encryptedEither = readEncryptedMnemonicFromDisk(seedPath)
|
||||
@ -329,32 +327,29 @@ object WalletStorage extends KeyManagerLogger {
|
||||
encrypted.toExtPrivKey(passphrase) match {
|
||||
case Failure(exc) =>
|
||||
logger.error(s"Error when decrypting $encrypted: $exc")
|
||||
CompatLeft(ReadMnemonicError.DecryptionError)
|
||||
Left(ReadMnemonicError.DecryptionError)
|
||||
case Success(xprv) =>
|
||||
logger.debug(s"Decrypted $encrypted successfully")
|
||||
val decryptedExtPrivKey =
|
||||
DecryptedExtPrivKey(xprv, encrypted.creationTime)
|
||||
CompatRight(decryptedExtPrivKey)
|
||||
Right(decryptedExtPrivKey)
|
||||
}
|
||||
case Success(mnemonic) =>
|
||||
logger.debug(s"Decrypted $encrypted successfully")
|
||||
val decryptedMnemonic =
|
||||
DecryptedMnemonic(mnemonic, encrypted.creationTime)
|
||||
CompatRight(decryptedMnemonic)
|
||||
Right(decryptedMnemonic)
|
||||
}
|
||||
}
|
||||
case None =>
|
||||
readUnencryptedMnemonicFromDisk(seedPath) match {
|
||||
case CompatLeft(_) =>
|
||||
case Left(_) =>
|
||||
readUnencryptedSeedFromDisk(seedPath)
|
||||
case CompatRight(mnemonic) => CompatRight(mnemonic)
|
||||
case Right(mnemonic) => Right(mnemonic)
|
||||
}
|
||||
}
|
||||
|
||||
decryptedEither match {
|
||||
case CompatLeft(value) => Left(value)
|
||||
case CompatRight(value) => Right(value)
|
||||
}
|
||||
decryptedEither
|
||||
}
|
||||
|
||||
def changeAesPassword(
|
||||
|
@ -5,7 +5,6 @@ import org.bitcoins.core.api.keymanager.{
|
||||
BIP39KeyManagerCreateApi,
|
||||
KeyManagerApi
|
||||
}
|
||||
import org.bitcoins.core.compat.{CompatEither, CompatLeft, CompatRight}
|
||||
import org.bitcoins.core.crypto._
|
||||
import org.bitcoins.core.hd.{HDAccount, HDPath}
|
||||
import org.bitcoins.core.util.{BitcoinSLogger, HDUtil, TimeUtil}
|
||||
@ -99,26 +98,24 @@ object BIP39KeyManager
|
||||
|
||||
val time = TimeUtil.now
|
||||
|
||||
val writtenToDiskE: CompatEither[KeyManagerInitializeError, KeyManagerApi] =
|
||||
val writtenToDiskE: Either[KeyManagerInitializeError, KeyManagerApi] =
|
||||
if (Files.notExists(seedPath)) {
|
||||
logger.info(
|
||||
s"Seed path parent directory does not exist, creating ${seedPath.getParent}")
|
||||
Files.createDirectories(seedPath.getParent)
|
||||
|
||||
val mnemonicT = Try(MnemonicCode.fromEntropy(entropy))
|
||||
val mnemonicE: CompatEither[KeyManagerInitializeError, MnemonicCode] =
|
||||
val mnemonicE: Either[KeyManagerInitializeError, MnemonicCode] =
|
||||
mnemonicT match {
|
||||
case Success(mnemonic) =>
|
||||
logger.info(s"Created mnemonic from entropy")
|
||||
CompatEither(Right(mnemonic))
|
||||
Right(mnemonic)
|
||||
case Failure(err) =>
|
||||
logger.error(s"Could not create mnemonic from entropy! $err")
|
||||
CompatEither(Left(InitializeKeyManagerError.BadEntropy))
|
||||
Left(InitializeKeyManagerError.BadEntropy)
|
||||
}
|
||||
|
||||
val writableMnemonicE: CompatEither[
|
||||
KeyManagerInitializeError,
|
||||
SeedState] =
|
||||
val writableMnemonicE: Either[KeyManagerInitializeError, SeedState] =
|
||||
mnemonicE.map { mnemonic =>
|
||||
val decryptedMnemonic = DecryptedMnemonic(mnemonic, time)
|
||||
aesPasswordOpt match {
|
||||
@ -148,16 +145,16 @@ object BIP39KeyManager
|
||||
WalletStorage.decryptSeedFromDisk(kmParams.seedPath,
|
||||
aesPasswordOpt) match {
|
||||
case Right(mnemonic: DecryptedMnemonic) =>
|
||||
CompatRight(
|
||||
Right(
|
||||
fromMnemonic(mnemonic = mnemonic.mnemonicCode,
|
||||
kmParams = kmParams,
|
||||
bip39PasswordOpt = bip39PasswordOpt,
|
||||
creationTime = mnemonic.creationTime))
|
||||
case Right(xprv: DecryptedExtPrivKey) =>
|
||||
val km = new BIP39KeyManager(xprv.xprv, kmParams, xprv.creationTime)
|
||||
CompatRight(km)
|
||||
Right(km)
|
||||
case Left(err) =>
|
||||
CompatLeft(
|
||||
Left(
|
||||
InitializeKeyManagerError.FailedToReadWrittenSeed(
|
||||
JsonParsingError(err.toString)))
|
||||
}
|
||||
@ -169,7 +166,7 @@ object BIP39KeyManager
|
||||
bip39PasswordOpt,
|
||||
kmParams = kmParams)
|
||||
|
||||
val biasedFinalE: CompatEither[KeyManagerInitializeError, BIP39KeyManager] =
|
||||
val biasedFinalE: Either[KeyManagerInitializeError, BIP39KeyManager] =
|
||||
for {
|
||||
kmBeforeWrite <- writtenToDiskE
|
||||
invariant <- unlocked match {
|
||||
@ -178,20 +175,20 @@ object BIP39KeyManager
|
||||
unlockedKeyManager == kmBeforeWrite,
|
||||
s"We could not read the key manager we just wrote! $kmBeforeWrite != $unlockedKeyManager"
|
||||
)
|
||||
CompatRight(unlockedKeyManager)
|
||||
Right(unlockedKeyManager)
|
||||
|
||||
case Left(err) =>
|
||||
CompatLeft(InitializeKeyManagerError.FailedToReadWrittenSeed(err))
|
||||
Left(InitializeKeyManagerError.FailedToReadWrittenSeed(err))
|
||||
}
|
||||
} yield {
|
||||
invariant
|
||||
}
|
||||
|
||||
biasedFinalE match {
|
||||
case CompatRight(initSuccess) =>
|
||||
case Right(initSuccess) =>
|
||||
logger.info(s"Successfully initialized wallet")
|
||||
Right(initSuccess)
|
||||
case CompatLeft(err) =>
|
||||
case Left(err) =>
|
||||
logger.error(s"Failed to initialize key manager with err=$err")
|
||||
Left(err)
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import org.bitcoins.core.api.wallet.{
|
||||
AddUtxoResult,
|
||||
AddUtxoSuccess
|
||||
}
|
||||
import org.bitcoins.core.compat._
|
||||
import org.bitcoins.core.consensus.Consensus
|
||||
import org.bitcoins.core.hd.HDAccount
|
||||
import org.bitcoins.core.number.UInt32
|
||||
@ -22,7 +21,7 @@ import org.bitcoins.core.protocol.transaction.{
|
||||
TransactionOutPoint,
|
||||
TransactionOutput
|
||||
}
|
||||
import org.bitcoins.core.util.{EitherUtil, FutureUtil}
|
||||
import org.bitcoins.core.util.FutureUtil
|
||||
import org.bitcoins.core.wallet.utxo._
|
||||
import org.bitcoins.crypto.DoubleSha256DigestBE
|
||||
import org.bitcoins.wallet.{Wallet, WalletLogger}
|
||||
@ -164,14 +163,14 @@ private[wallet] trait UtxoHandling extends WalletLogger {
|
||||
* it in our address table
|
||||
*/
|
||||
private def findAddress(
|
||||
spk: ScriptPubKey): Future[CompatEither[AddUtxoError, AddressDb]] =
|
||||
spk: ScriptPubKey): Future[Either[AddUtxoError, AddressDb]] =
|
||||
BitcoinAddress.fromScriptPubKeyT(spk, networkParameters) match {
|
||||
case Success(address) =>
|
||||
addressDAO.findAddress(address).map {
|
||||
case Some(addrDb) => CompatRight(addrDb)
|
||||
case None => CompatLeft(AddUtxoError.AddressNotFound)
|
||||
case Some(addrDb) => Right(addrDb)
|
||||
case None => Left(AddUtxoError.AddressNotFound)
|
||||
}
|
||||
case Failure(_) => Future.successful(CompatLeft(AddUtxoError.BadSPK))
|
||||
case Failure(_) => Future.successful(Left(AddUtxoError.BadSPK))
|
||||
}
|
||||
|
||||
/** Constructs a DB level representation of the given UTXO, and persist it to disk */
|
||||
@ -257,12 +256,13 @@ private[wallet] trait UtxoHandling extends WalletLogger {
|
||||
|
||||
// second check: do we have an address associated with the provided
|
||||
// output in our DB?
|
||||
def addressDbEitherF: Future[CompatEither[AddUtxoError, AddressDb]] =
|
||||
def addressDbEitherF: Future[Either[AddUtxoError, AddressDb]] = {
|
||||
findAddress(output.scriptPubKey)
|
||||
}
|
||||
|
||||
// insert the UTXO into the DB
|
||||
addressDbEitherF.flatMap { addressDbE =>
|
||||
def biasedE: CompatEither[AddUtxoError, Future[SpendingInfoDb]] =
|
||||
addressDbEitherF
|
||||
.map { addressDbE =>
|
||||
for {
|
||||
addressDb <- addressDbE
|
||||
} yield writeUtxo(tx = transaction,
|
||||
@ -271,12 +271,11 @@ private[wallet] trait UtxoHandling extends WalletLogger {
|
||||
outPoint = outPoint,
|
||||
addressDb = addressDb,
|
||||
blockHash = blockHash)
|
||||
|
||||
EitherUtil.liftRightBiasedFutureE(biasedE)
|
||||
} map {
|
||||
case CompatRight(utxo) => AddUtxoSuccess(utxo)
|
||||
case CompatLeft(e) => e
|
||||
}
|
||||
}
|
||||
.flatMap {
|
||||
case Right(utxoF) => utxoF.map(AddUtxoSuccess(_))
|
||||
case Left(e) => Future.successful(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user