Search code examples
bouncycastlecordapublic-key

Corda - Decoding Base58 public keys doesn't work


Decoding Base58 public keys back into PublicKey instances doesn't seem to work:

parsePublicKeyBase58("DLG9nscvKPbgagoLP7jr4oc5FygazKr7pdADZMGFizmpV8")

Exception in thread "main" java.lang.IllegalArgumentException: failed to construct sequence from byte[]: unknown tag 9 encountered at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source) at org.bouncycastle.asn1.x509.SubjectPublicKeyInfo.getInstance(Unknown Source) at net.corda.core.crypto.Crypto.decodePublicKey(Crypto.kt:343) at net.corda.core.utilities.EncodingUtils.parsePublicKeyBase58(EncodingUtils.kt:81) at test.Program$Companion.main(Program.kt:15) at test.Program.main(Program.kt)

What's wrong here?

What error are you getting with that – Sneha Damle?

parsePublicKeyBase58("G9nscvKPbgagoLP7jr4oc5FygazKr7pdADZMGFizmpV8")

Exception in thread "main" java.lang.IllegalArgumentException: failed to construct sequence from byte[]: DEF length 31 object truncated by 1 at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source) at org.bouncycastle.asn1.x509.SubjectPublicKeyInfo.getInstance(Unknown Source) at net.corda.core.crypto.Crypto.decodePublicKey(Crypto.kt:343) at net.corda.core.utilities.EncodingUtils.parsePublicKeyBase58(EncodingUtils.kt:81) at test.ProgramKt.main(Program.kt:6)


Solution

  • You are probably using an incorrect Base58 string. I did the following and it worked.

    Encoded a Public Key to Base58

    Base58.encode(party.getOwningKey().getEncoded()).toString();
    

    This gave me the String GfHq2tTVk9z4eXgyH7WThpV3Qn7zdCm4cKw8J5x8kVGnVkGtgAmh3KKE7EN4

    Decoded it:

    PublicKey publicKey = EncodingUtils.parsePublicKeyBase58("GfHq2tTVk9z4eXgyH7WThpV3Qn7zdCm4cKw8J5x8kVGnVkGtgAmh3KKE7EN4");