Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Base58 Encode and Decode Interops on Neo #1833

Merged
merged 4 commits into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/neo/SmartContract/ApplicationEngine.Binary.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Neo.Cryptography;
using Neo.VM.Types;
using static System.Convert;

Expand All @@ -9,6 +10,8 @@ partial class ApplicationEngine
public static readonly InteropDescriptor System_Binary_Deserialize = Register("System.Binary.Deserialize", nameof(BinaryDeserialize), 0_00500000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base64Encode = Register("System.Binary.Base64Encode", nameof(Base64Encode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base64Decode = Register("System.Binary.Base64Decode", nameof(Base64Decode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base58Encode = Register("System.Binary.Base58Encode", nameof(Base58Encode), 0_00100000, CallFlags.None, true);
public static readonly InteropDescriptor System_Binary_Base58Decode = Register("System.Binary.Base58Decode", nameof(Base58Decode), 0_00100000, CallFlags.None, true);

protected internal byte[] BinarySerialize(StackItem item)
{
Expand All @@ -29,5 +32,15 @@ protected internal byte[] Base64Decode(string s)
{
return FromBase64String(s);
}

internal string Base58Encode(byte[] data)
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
return Base58.Encode(data);
}

internal byte[] Base58Decode(string s)
{
return Base58.Decode(s);
}
}
}
6 changes: 6 additions & 0 deletions tests/neo.UnitTests/SmartContract/UT_ApplicationEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,16 @@ public void TestBinary()
var data = new byte[0];
CollectionAssert.AreEqual(data, engine.Base64Decode(engine.Base64Encode(data)));

CollectionAssert.AreEqual(data, engine.Base58Decode(engine.Base58Encode(data)));
shargon marked this conversation as resolved.
Show resolved Hide resolved

data = new byte[] { 1, 2, 3 };
CollectionAssert.AreEqual(data, engine.Base64Decode(engine.Base64Encode(data)));

CollectionAssert.AreEqual(data, engine.Base58Decode(engine.Base58Encode(data)));

Assert.AreEqual("AQIDBA==", engine.Base64Encode(new byte[] { 1, 2, 3, 4 }));

Assert.AreEqual("2VfUX", engine.Base58Encode(new byte[] { 1, 2, 3, 4 }));
}

[TestMethod]
Expand Down