-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TSDK-733 Implement basic confirm redemption.
- Loading branch information
Showing
14 changed files
with
785 additions
and
175 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package co.topl.shared | ||
|
||
case class StartSessionRequest( | ||
pkey: String, | ||
sha256: String | ||
) | ||
|
||
case class StartSessionResponse( | ||
sessionID: String, | ||
script: String, | ||
escrowAddress: String, | ||
descriptor: String | ||
) | ||
|
||
case class ConfirmRedemptionRequest( | ||
sessionID: String, | ||
inputTxId: String, | ||
inputIndex: Int, | ||
feePerByte: Int, | ||
amount: Long, | ||
secret: String | ||
) | ||
|
||
case class ConfirmRedemptionResponse( | ||
tx: String | ||
) |
30 changes: 30 additions & 0 deletions
30
shared/src/main/scala/co/topl/shared/NetworkIdentifiers.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package co.topl.shared | ||
|
||
|
||
sealed abstract class BitcoinNetworkIdentifiers( | ||
val name: String | ||
) { | ||
override def toString: String = name | ||
def btcNetwork: org.bitcoins.core.config.BitcoinNetwork = | ||
this match { | ||
case Mainnet => org.bitcoins.core.config.MainNet | ||
case Testnet => org.bitcoins.core.config.TestNet3 | ||
case RegTest => org.bitcoins.core.config.RegTest | ||
} | ||
} | ||
|
||
case object Mainnet extends BitcoinNetworkIdentifiers("mainnet") | ||
case object Testnet extends BitcoinNetworkIdentifiers("testnet") | ||
case object RegTest extends BitcoinNetworkIdentifiers("regtest") | ||
case object BitcoinNetworkIdentifiers { | ||
|
||
def values = Set(Mainnet, Testnet, RegTest) | ||
|
||
def fromString(s: String): Option[BitcoinNetworkIdentifiers] = | ||
s match { | ||
case "mainnet" => Some(Mainnet) | ||
case "testnet" => Some(Testnet) | ||
case "regtest" => Some(RegTest) | ||
case _ => None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package co.topl.shared | ||
|
||
object ParamParser { | ||
|
||
implicit val networkRead: scopt.Read[BitcoinNetworkIdentifiers] = | ||
scopt.Read | ||
.reads(BitcoinNetworkIdentifiers.fromString(_)) | ||
.map(_ match { | ||
case Some(value) => value | ||
case None => | ||
throw new IllegalArgumentException( | ||
"Invalid network. Possible values: mainnet, testnet, regtest" | ||
) | ||
}) | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
shared/src/main/scala/co/topl/shared/utils/KeyGenerationUtils.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package co.topl.shared.utils | ||
|
||
import cats.effect.kernel.Sync | ||
import co.topl.shared.BitcoinNetworkIdentifiers | ||
import org.bitcoins.core.crypto.MnemonicCode | ||
import org.bitcoins.core.hd.BIP32Path | ||
import org.bitcoins.core.hd.HDAccount | ||
import org.bitcoins.core.hd.HDPath | ||
import org.bitcoins.core.hd.HDPurposes | ||
import org.bitcoins.core.wallet.keymanagement.KeyManagerParams | ||
import org.bitcoins.crypto.AesPassword | ||
import org.bitcoins.keymanager.bip39.BIP39KeyManager | ||
import scodec.bits.ByteVector | ||
import org.bitcoins.crypto.ECDigitalSignature | ||
import org.bitcoins.crypto.HashType | ||
|
||
object KeyGenerationUtils { | ||
|
||
def loadKeyAndSign[F[_]: Sync]( | ||
btcNetwork: BitcoinNetworkIdentifiers, | ||
seedFile: String, | ||
password: String, | ||
txBytes: ByteVector | ||
): F[String] = { | ||
import cats.implicits._ | ||
for { | ||
seedPath <- Sync[F].delay( | ||
new java.io.File(seedFile).getAbsoluteFile.toPath | ||
) | ||
purpose = HDPurposes.SegWit | ||
kmParams = KeyManagerParams(seedPath, purpose, btcNetwork.btcNetwork) | ||
aesPasswordOpt = Some(AesPassword.fromString(password)) | ||
// entropy = MnemonicCode.getEntropy256Bits | ||
// mnemonic = MnemonicCode.fromEntropy(entropy) | ||
km <- Sync[F].fromEither( | ||
BIP39KeyManager | ||
.fromParams( | ||
kmParams, | ||
aesPasswordOpt, | ||
None | ||
) | ||
.left | ||
.map(_ => new IllegalArgumentException("Invalid params")) | ||
) | ||
// hdAccount <- Sync[F].fromOption( | ||
// HDAccount.fromPath( | ||
// BIP32Path.fromString("m/84'/1'/0'") | ||
// ) // this is the standard account path for segwit | ||
// , | ||
// new IllegalArgumentException("Invalid account path") | ||
// ) | ||
signed <- Sync[F].delay( | ||
km.toSign(HDPath.fromString("m/84'/1'/0'/0/0")).sign(txBytes) | ||
) | ||
canonicalSignature <- Sync[F].delay(ECDigitalSignature( | ||
signed.bytes ++ ByteVector.fromByte(HashType.sigHashAll.byte) | ||
)) | ||
} yield canonicalSignature.hex | ||
} | ||
|
||
def generateKey[F[_]: Sync]( | ||
btcNetwork: BitcoinNetworkIdentifiers, | ||
seedFile: String, | ||
password: String | ||
): F[String] = { | ||
import cats.implicits._ | ||
for { | ||
seedPath <- Sync[F].delay( | ||
new java.io.File(seedFile).getAbsoluteFile.toPath | ||
) | ||
purpose = HDPurposes.SegWit | ||
kmParams = KeyManagerParams(seedPath, purpose, btcNetwork.btcNetwork) | ||
aesPasswordOpt = Some(AesPassword.fromString(password)) | ||
entropy = MnemonicCode.getEntropy256Bits | ||
mnemonic = MnemonicCode.fromEntropy(entropy) | ||
km <- Sync[F].fromEither( | ||
BIP39KeyManager.initializeWithMnemonic( | ||
aesPasswordOpt, | ||
mnemonic, | ||
None, | ||
kmParams | ||
) | ||
) | ||
hdAccount <- Sync[F].fromOption( | ||
HDAccount.fromPath( | ||
BIP32Path.fromString("m/84'/1'/0'") | ||
) // this is the standard account path for segwit | ||
, | ||
new IllegalArgumentException("Invalid account path") | ||
) | ||
pKey <- Sync[F].delay( | ||
km.deriveXPub(hdAccount) | ||
.get | ||
.deriveChildPubKey(BIP32Path.fromString("m/0/0")) | ||
.get | ||
.key | ||
.hex | ||
) | ||
} yield (pKey) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
topl-btc-bridge/src/main/scala/co/topl/bridge/BridgeParamsDescriptor.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package co.topl.bridge | ||
|
||
import co.topl.shared.BitcoinNetworkIdentifiers | ||
import scopt.OParser | ||
|
||
trait BridgeParamsDescriptor { | ||
|
||
import co.topl.shared.ParamParser._ | ||
|
||
val builder = OParser.builder[ToplBTCBridgeParamConfig] | ||
|
||
val parser = { | ||
import builder._ | ||
|
||
OParser.sequence( | ||
programName("topl-btc-bridge"), | ||
head("topl-btc-bridge", "0.1"), | ||
opt[BitcoinNetworkIdentifiers]('n', "network") | ||
.action((x, c) => c.copy(btcNetwork = x)) | ||
.text( | ||
"Network name: Possible values: mainnet, testnet, regtest. (mandatory)" | ||
) | ||
) | ||
} | ||
|
||
} |
Oops, something went wrong.