Skip to content

Implement Base58 Encode and Decode Interops on Neo #33

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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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);
}

protected internal string Base58Encode(byte[] data)
{
return Base58.Encode(data);
}

protected internal byte[] Base58Decode(string s)
{
return Base58.Decode(s);
}
}
}
1 change: 1 addition & 0 deletions tests/neo.UnitTests/Cryptography/ECC/UT_ECDSA.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public void GenerateSignature()
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 2 }, sig[0], sig[1]));
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 1 }, sig[0] + 1, sig[1]));
Assert.IsFalse(ecdsa.VerifySignature(new byte[] { 1 }, sig[0], sig[1] + 1));
Assert.IsFalse(ecdsa.VerifySignature(new byte[33], sig[0], sig[1]));
}
}
}
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)));

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