Skip to content

Commit

Permalink
add constructor to init DirectSigningWallet with a privkey
Browse files Browse the repository at this point in the history
  • Loading branch information
luca992 committed Jan 17, 2023
1 parent 7336479 commit daf3802
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 13 deletions.
34 changes: 33 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ agp = "7.3.1"
##⬆ = "7.4.0-rc01"
##⬆ = "7.4.0-rc02"
##⬆ = "7.4.0-rc03"
##⬆ = "7.4.0"
##⬆ = "8.0.0-alpha01"
##⬆ = "8.0.0-alpha02"
##⬆ = "8.0.0-alpha03"
Expand All @@ -57,7 +58,7 @@ vanniktech-publish = "0.21.0"
## ⬆ = "0.23.1"
## ⬆ = "0.23.2-SNAPSHOT"
## ⬆ = "0.24.0-SNAPSHOT"
swift-package = "2.1.0"
swift-package = "2.1.1"
okio = "3.2.0"
##⬆ = "3.3.0-SNAPSHOT"
##⬆ = "3.3.0"
Expand All @@ -81,6 +82,37 @@ kermit = "1.2.2"
secretk = "0.1.2"

compose-jb = "1.2.2"
## ⬆ = "1.3.0-alpha01-dev824"
## ⬆ = "1.3.0-alpha01-dev827"
## ⬆ = "1.3.0-alpha01-dev831"
## ⬆ = "1.3.0-alpha01-dev832"
## ⬆ = "1.3.0-alpha01-dev834"
## ⬆ = "1.3.0-alpha01-dev849"
## ⬆ = "1.3.0-alpha01-dev853"
## ⬆ = "1.3.0-alpha01-dev861"
## ⬆ = "1.3.0-alpha01-dev862"
## ⬆ = "1.3.0-alpha01-dev866"
## ⬆ = "1.3.0-alpha01-dev868"
## ⬆ = "1.3.0-alpha01-dev869"
## ⬆ = "1.3.0-beta01"
## ⬆ = "1.3.0-beta02"
## ⬆ = "1.3.0-beta03"
## ⬆ = "1.3.0-beta04-dev871"
## ⬆ = "1.3.0-beta04-dev873"
## ⬆ = "1.3.0-beta04-dev875"
## ⬆ = "1.3.0-beta04-dev877"
## ⬆ = "1.3.0-beta04-dev879"
## ⬆ = "1.3.0-beta04-dev880"
## ⬆ = "1.3.0-beta04-dev882"
## ⬆ = "1.3.0-beta04-dev883"
## ⬆ = "1.3.0-beta04-dev885"
## ⬆ = "1.3.0-beta04-dev889"
## ⬆ = "1.3.0-beta04-dev897"
## ⬆ = "1.3.0-beta04-dev901"
## ⬆ = "1.3.0-beta04-dev903"
## ⬆ = "1.3.0-rc01"
## ⬆ = "1.3.0-rc02-dev906"
## ⬆ = "1.3.0-rc02"
bootstrap-compose = "0.1.10"

bignum = "0.3.7"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,32 @@ interface Wallet {
suspend fun signAmino(signerAddress: String, signDoc: StdSignDoc): AminoSignResponse
}

sealed class BaseWallet(
mnemonic: String?,
val hdPath: Array<Slip10RawIndex> = makeSecretNetworkPath(0.toUInt()),
val bech32Prefix: String = "secret"
) : Wallet {

sealed class BaseWallet : Wallet {
val hdPath: Array<Slip10RawIndex>
val bech32Prefix: String
val addressToAccountSigningData: MutableMap<String, AccountSigningData> = mutableMapOf()
val accountAddresses get() = addressToAccountSigningData.keys

init {
constructor(
mnemonic: String?,
hdPath: Array<Slip10RawIndex> = makeSecretNetworkPath(0.toUInt()),
bech32Prefix: String = "secret"
) {
this.hdPath = hdPath
this.bech32Prefix = bech32Prefix
addAccount(mnemonic)
}

constructor(
privkey: UByteArray,
hdPath: Array<Slip10RawIndex> = makeSecretNetworkPath(0.toUInt()),
bech32Prefix: String = "secret"
) {
this.hdPath = hdPath
this.bech32Prefix = bech32Prefix
addAccount(privkey)
}

fun addAccount(mnemonic: String? = null): AccountSigningData {
val seed = if (mnemonic != null) {
Mnemonics.MnemonicCode(mnemonic).toSeed().toUByteArray()
Expand All @@ -75,6 +88,14 @@ sealed class BaseWallet(
return addressToAccountSigningData[address]!!
}

fun addAccount(privkey: UByteArray): AccountSigningData {
val uncompressed = Secp256k1.makeKeypair(privkey).pubkey
val pubkey = Secp256k1.compressPubkey(uncompressed)
val address = pubkeyToAddress(encodeSecp256k1Pubkey(pubkey), bech32Prefix)
addressToAccountSigningData[address] = AccountSigningData(address, Algo.secp256k1, pubkey, privkey)
return addressToAccountSigningData[address]!!
}

/**
* Removes all account data for an address including private keys.
*
Expand Down Expand Up @@ -109,7 +130,8 @@ sealed class BaseWallet(
signBytes: UByteArray,
prehashType: PrehashType = PrehashType.SHA256
): StdSignature {
val account = addressToAccountSigningData[signerAddress] ?: throw Error("Address $signerAddress not found in wallet")
val account =
addressToAccountSigningData[signerAddress] ?: throw Error("Address $signerAddress not found in wallet")
val messageHash = prehash(signBytes, prehashType)
val signature = Secp256k1.createSignature(messageHash, account.privkey)
val fixedLengthSignature = signature.r.getPadded(32) + signature.s.getPadded(32)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import kotlinx.serialization.encodeToByteArray
import kotlinx.serialization.protobuf.ProtoBuf


class DirectSigningWallet(
mnemonic: String?,
) : BaseWallet(mnemonic) {
class DirectSigningWallet : BaseWallet {

constructor(mnemonic: String?) : super(mnemonic)
constructor(privkey: UByteArray) : super(privkey)

// helper constructor for swift
constructor() : this(null)
Expand Down
8 changes: 7 additions & 1 deletion secretk/src/commonTest/kotlin/io/eqoty/ClientTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ class ClientTests {
assertEquals(accAddress, wallet.getAccounts()[0].address)
}


@Test
fun testCorrectlyGeneratesAddressFromPrivKey() = runTest {
val privKey = wallet.addressToAccountSigningData[wallet.accountAddresses.first()]!!.privkey
val walletFromPrivKey = DirectSigningWallet(privKey)
assertEquals(walletFromPrivKey.getAccounts()[0].address, wallet.getAccounts()[0].address)
}

@Test
fun testCreateViewingKeyAndUseToQuery() = runTest {
val contractAddress = "secret1lz4m46vpdn8f2aj8yhtnexus40663udv7hhprm"
Expand Down

0 comments on commit daf3802

Please sign in to comment.