Skip to content

Commit

Permalink
[Neo Core StdLib] Add Base64url (#3453)
Browse files Browse the repository at this point in the history
* add base64url

* active in

* update placehold hf height

* fix hf issue and move methods to proper place.

* fix test

* use identifymodel instead.
  • Loading branch information
Jim8y committed Aug 8, 2024
1 parent e63f10b commit 9d16c53
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.fs.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"HF_Basilisk": 4500000,
"HF_Cockatrice": 5800000,
"HF_Domovoi": 5800000,
"HF_Echidna": 0
"HF_Echidna": 5800001
},
"StandbyCommittee": [
"026fa34ec057d74c2fdf1a18e336d0bd597ea401a0b2ad57340d5c220d09f44086",
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"HF_Basilisk": 4120000,
"HF_Cockatrice": 5450000,
"HF_Domovoi": 5570000,
"HF_Echidna": 0
"HF_Echidna": 5570001
},
"InitialGasDistribution": 5200000000000000,
"ValidatorsCount": 7,
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.mainnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"HF_Basilisk": 4120000,
"HF_Cockatrice": 5450000,
"HF_Domovoi": 5570000,
"HF_Echidna": 0
"HF_Echidna": 5570001
},
"InitialGasDistribution": 5200000000000000,
"ValidatorsCount": 7,
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.CLI/config.testnet.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"HF_Basilisk": 2680000,
"HF_Cockatrice": 3967000,
"HF_Domovoi": 4144000,
"HF_Echidna": 0
"HF_Echidna": 4144001
},
"InitialGasDistribution": 5200000000000000,
"ValidatorsCount": 7,
Expand Down
1 change: 1 addition & 0 deletions src/Neo/Neo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="8.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.IdentityModel.Tokens" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
23 changes: 23 additions & 0 deletions src/Neo/SmartContract/Native/StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#pragma warning disable IDE0051

using Microsoft.IdentityModel.Tokens;
using Neo.Cryptography;
using Neo.Json;
using Neo.VM.Types;
Expand Down Expand Up @@ -131,6 +132,28 @@ public static byte[] Base64Decode([MaxLength(MaxInputLength)] string s)
return Convert.FromBase64String(s);
}

/// <summary>
/// Encodes a byte array into a base64Url string.
/// </summary>
/// <param name="data">The base64Url to be encoded.</param>
/// <returns>The encoded base64Url string.</returns>
[ContractMethod(Hardfork.HF_Echidna, CpuFee = 1 << 5)]
public static string Base64UrlEncode([MaxLength(MaxInputLength)] string data)
{
return Base64UrlEncoder.Encode(data);
}

/// <summary>
/// Decodes a byte array from a base64Url string.
/// </summary>
/// <param name="s">The base64Url string.</param>
/// <returns>The decoded base64Url string.</returns>
[ContractMethod(Hardfork.HF_Echidna, CpuFee = 1 << 5)]
public static string Base64UrlDecode([MaxLength(MaxInputLength)] string s)
{
return Base64UrlEncoder.Decode(s);
}

/// <summary>
/// Encodes a byte array into a base58 <see cref="string"/>.
/// </summary>
Expand Down

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions tests/Neo.UnitTests/SmartContract/Native/UT_StdLib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,5 +406,25 @@ public void TestRuntime_Deserialize()
Assert.AreEqual(engine.ResultStack.Pop<Integer>().GetInteger(), 100);
Assert.AreEqual(engine.ResultStack.Pop<ByteString>().GetString(), "test");
}

[TestMethod]
public void TestBase64Url()
{
var snapshotCache = TestBlockchain.GetTestSnapshotCache();
using (var script = new ScriptBuilder())
{
// Test encoding
script.EmitDynamicCall(NativeContract.StdLib.Hash, "base64UrlEncode", "[email protected]&Issuer=https://example.com");
script.EmitDynamicCall(NativeContract.StdLib.Hash, "base64UrlDecode", "U3ViamVjdD10ZXN0QGV4YW1wbGUuY29tJklzc3Vlcj1odHRwczovL2V4YW1wbGUuY29t");

using var engine = ApplicationEngine.Create(TriggerType.Application, null, snapshotCache, settings: TestBlockchain.TheNeoSystem.Settings);
engine.LoadScript(script.ToArray());

Assert.AreEqual(engine.Execute(), VMState.HALT);
Assert.AreEqual(2, engine.ResultStack.Count);
Assert.AreEqual("[email protected]&Issuer=https://example.com", engine.ResultStack.Pop<ByteString>());
Assert.AreEqual("U3ViamVjdD10ZXN0QGV4YW1wbGUuY29tJklzc3Vlcj1odHRwczovL2V4YW1wbGUuY29t", engine.ResultStack.Pop<ByteString>().GetString());
}
}
}
}

0 comments on commit 9d16c53

Please sign in to comment.