Skip to content

Commit

Permalink
feat(signers): add support for creating random PrivateKeySigner
Browse files Browse the repository at this point in the history
additionally, re-use cached SecureRandom instance
  • Loading branch information
ArtificialPB committed Dec 11, 2024
1 parent fc040ad commit dc75456
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 10 additions & 0 deletions ethers-crypto/src/main/kotlin/io/ethers/crypto/Hashing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ import org.bouncycastle.crypto.macs.HMac
import org.bouncycastle.crypto.params.KeyParameter
import org.bouncycastle.jcajce.provider.digest.Keccak
import org.bouncycastle.jcajce.provider.digest.SHA256
import java.security.SecureRandom

object Hashing {
private val MESSAGE_PREFIX = "\u0019Ethereum Signed Message:\n".toByteArray()
private const val VERSIONED_HASH_VERSION_KZG = 0x01.toByte()
private val secureRandom by lazy { SecureRandom() }

/**
* Get cached [SecureRandom] instance. Instance is created on first access.
* */
@JvmStatic
fun secureRandom(): SecureRandom {
return secureRandom
}

/**
* Construct and hash [message] based on [EIP-191](https://eips.ethereum.org/EIPS/eip-191) standard (version 0x01).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package io.ethers.crypto.bip39

import io.ethers.crypto.Hashing
import org.bouncycastle.crypto.digests.SHA512Digest
import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator
import org.bouncycastle.crypto.params.KeyParameter
import org.bouncycastle.jcajce.provider.digest.SHA256
import java.nio.charset.StandardCharsets
import java.security.SecureRandom
import java.util.Collections

/**
Expand Down Expand Up @@ -130,17 +130,16 @@ class MnemonicCode @JvmOverloads constructor(
private const val SEED_ITERATIONS = 2048

/**
* Create a new mnemonic code from random entropy, using [SecureRandom].
* Create a new mnemonic code from random entropy, using [Hashing.secureRandom].
* */
@JvmStatic
@JvmOverloads
fun fromRandomEntropy(
bitsOfEntropy: Int = 256,
wordList: MnemonicWordList = MnemonicWordListEnglish,
): MnemonicCode {
val rand = SecureRandom()
val entropy = ByteArray(bitsOfEntropy / 8)
rand.nextBytes(entropy)
Hashing.secureRandom().nextBytes(entropy)

return fromEntropy(entropy, wordList)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import io.ethers.core.FastHex
import io.ethers.core.types.Address
import io.ethers.core.types.Bytes
import io.ethers.core.types.Signature
import io.ethers.crypto.Hashing
import io.ethers.crypto.Secp256k1

/**
Expand Down Expand Up @@ -59,5 +60,15 @@ class PrivateKeySigner(val signingKey: Secp256k1.SigningKey) : Signer {

return Secp256k1.SigningKey(bytes)
}

/**
* Create a new [PrivateKeySigner] from random entropy, using [Hashing.secureRandom].
* */
@JvmStatic
fun random(): PrivateKeySigner {
val privateKey = ByteArray(32)
Hashing.secureRandom().nextBytes(privateKey)
return PrivateKeySigner(privateKey)
}
}
}

0 comments on commit dc75456

Please sign in to comment.