1

I have a BigInteger that represents a private key. How can I get that private key as a base58 encoded string? The following pseudo code does not work properly:

base58.encode(bigInt.toByteArray) 
// yields a string that differs from given bigInt's private key

UPDATE

def fullDoubleSha(bytes: ArrayOfByte) = {
  val digestSha = MessageDigest getInstance "SHA-256"
  digestSha digest digestSha.digest(bytes)
}

def getPrivBytes(key: BigInteger) = key.toByteArray match { case keyBytes =>
  val res = if (keyBytes.head == 0) keyBytes drop 1 else keyBytes
  new Array(32 - res.length) ++ res
}

def privBytesToWif(vs: ArrayOfByte) = {
  val rawPrivKey = Array(0x80.toByte) ++ vs
  rawPrivKey ++ fullDoubleSha(rawPrivKey).take(4)
}
1

The base58 private key format includes an SHA256-based checksum. See this answer for details (disregard the part of the question where the private key itself is the SHA256 hash of some other data).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.