public string Export(Wallet wallet, byte[] publicKey) { Contract.Assert(wallet != null); Contract.Assert(publicKey != null && publicKey.Length > 0); // Take a private key var privateKey = wallet.GetPrivateKeyByAddress(publicKey); Contract.Assert(privateKey != null); // Add a 0x80 byte in front of it for mainnet addresses or 0xef for testnet addresses. // Also add a 0x01 byte at the end if the private key will correspond to a compressed public key var header = new byte[] { 0x80 }; var footer = new byte[] { 0x01 }; var extendedKey = new byte[header.Length + privateKey.Length + footer.Length]; Buffer.BlockCopy(header, 0, extendedKey, 0, header.Length); Buffer.BlockCopy(privateKey, 0, extendedKey, header.Length, privateKey.Length); Buffer.BlockCopy(footer, 0, extendedKey, header.Length + privateKey.Length, footer.Length); // Perform SHA-256 hash on the extended key // Perform SHA-256 hash on result of SHA-256 hash // Take the first 4 bytes of the second SHA-256 hash, this is the checksum byte[] checksum = extendedKey.Sha256().Sha256().Take(4).ToArray(); // Add the 4 checksum bytes from point 5 at the end of the extended key from point 2 byte[] exportKey = new byte[extendedKey.Length + checksum.Length]; Buffer.BlockCopy(extendedKey, 0, exportKey, 0, extendedKey.Length); Buffer.BlockCopy(checksum, 0, exportKey, extendedKey.Length, checksum.Length); // Convert the result from a byte string into a base58 string using Base58Check encoding return(Base58Check.EncodePlain(exportKey)); }
static Base58CheckTests() { testValue = Utils.HexStringToBytes("000111D38E5FC9071FFCD20B4A763CC9AE4F252BB4E48FD66A835E252ADA93FF480D6DD43DC62A641155A5"); // Should match the base58 alphabet when encoded testValueEncoded = Base58Check.Alphabet; var testValueChecksum = DoubleSHA256.ComputeChecksum(testValue); testValueWithChecksumEncoded = Base58Check.EncodePlain(testValue.Concat(testValueChecksum).ToArray()); }
public void TestEncodeDecode() { // Without checksum string encoded = Base58Check.EncodePlain(testValue); Assert.AreEqual(encoded, testValueEncoded); byte[] decoded = Base58Check.DecodePlain(encoded); CollectionAssert.AreEqual(decoded, testValue); // With checksum encoded = Base58Check.Encode(testValue); Assert.AreEqual(encoded, testValueWithChecksumEncoded); decoded = Base58Check.Decode(encoded); CollectionAssert.AreEqual(decoded, testValue); }
public void TestEncodingBigArray() { Base58Check.EncodePlain(Enumerable.Repeat <byte>(0xFF, 1024).ToArray()); }