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)
}
src091
  • 253
  • 1
  • 9

1 Answers1

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).

Nate Eldredge
  • 23,040
  • 3
  • 40
  • 80