Skip to content

Commit

Permalink
Insert submodule codes
Browse files Browse the repository at this point in the history
  • Loading branch information
farukterzioglu committed Feb 22, 2021
1 parent e56cab6 commit 4dc2d50
Show file tree
Hide file tree
Showing 277 changed files with 2,398,776 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/ripple-dot-net/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2015, 2016 Ripple Labs Inc.

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
54 changes: 54 additions & 0 deletions src/ripple-dot-net/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Ripple.NET [![Build status](https://ci.appveyor.com/api/projects/status/w1jnx38oreo35a6b/branch/master?svg=true)](https://ci.appveyor.com/project/sublimator/ripple-dot-net)

The purpose of this code is primarily for signing transactions, and while the
code can do a bit more than what is shown here, at this stage, it may change
without warning. Only the Transaction signing api shown below has any
guarantees. While this is currently a low priority project, feel free to register
your interest at support@ripple.com

## Requirements

* Visual Studio 2015 or equivalent compiler with c#6 support
* .NET framework 4.5.0+

## Transaction signing

Signing is done with either ecdsa/rfc6979 or ed25519. See [ripple-keypairs](https://github.com/ripple/ripple-keypairs) for how to generate a seed/secret, encoded in base58.

```c#

// using Ripple.TxSigning
// using Newtonsoft.Json.Linq;
var secret = "sEd7rBGm5kxzauRTAV2hbsNz7N45X91";
var unsignedTxJson = @"{
'Account': 'rJZdUusLDtY9NEsGea7ijqhVrXv98rYBYN',
'Amount': '1000',
'Destination': 'rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh',
'Fee': '10',
'Flags': 2147483648,
'Sequence': 1,
'TransactionType' : 'Payment'
}";

var signed = TxSigner.SignJson(JObject.Parse(unsignedTxJson), secret);

Console.WriteLine(signed.Hash);
Console.WriteLine(signed.TxJson);
Console.WriteLine(signed.TxBlob);

// A8A9C869671D35A18DFB69AFB7741062DF43F73C8A5942AD94EE58ED31477AC6
// {
// "Account": "rJZdUusLDtY9NEsGea7ijqhVrXv98rYBYN",
// "Amount": "1000",
// "Destination": "rHb9CJAWyB4rj91VRWn96DkukG4bwdtyTh",
// "Fee": "10",
// "Flags": 2147483648,
// "Sequence": 1,
// "TransactionType": "Payment",
// "SigningPubKey": "EDD3993CDC6647896C455F136648B7750723B011475547AF60691AA3D7438E021D",
// "TxnSignature": "C3646313B08EED6AF4392261A31B961F10C66CB733DB7F6CD9EAB079857834C8B0334270A2C037E63CDCCC1932E0832882B7B7066ECD2FAEDEB4A83DF8AE6303"
// }
// 120000228000000024000000016140000000000003E868400000000000000A7321EDD3993CDC6647896C455F136648B7750723B011475547AF60691AA3D7438E021D7440C3646313B08EED6AF4392261A31B961F10C66CB733DB7F6CD9EAB079857834C8B0334270A2C037E63CDCCC1932E0832882B7B7066ECD2FAEDEB4A83DF8AE63038114C0A5ABEF242802EFED4B041E8F2D4A8CC86AE3D18314B5F762798A53D543A014CAF8B297CFF8F2F937E8
```
72 changes: 72 additions & 0 deletions src/ripple-dot-net/Ripple.Address.Tests/AddressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Ripple.Address.Tests
{
using static AddressCodec;

[TestClass]
public class AddressTests
{
[TestMethod]
public void NodePublicTest()
{
AssertEncodes("NodePublic",
("0388E5BA87A000CB807240DF8C848EB0B" +
"5FFA5C8E5A521BC8E105C0F0A44217828"),
"n9MXXueo837zYH36DvMc13BwHcqtfAWNJY5czWVbp7uYTj7x17TH");
}

[TestMethod]
public void SeedTest()
{
var decodedKoblitz = DecodeSeed("sn259rEFXrQrWyx3Q7XneWcwV6dfL");
Assert.AreEqual(decodedKoblitz.Bytes.Length, 16);
Assert.AreEqual(decodedKoblitz.Type, "secp256k1");

AssertEncodes("K256Seed",
"CF2DE378FBDD7E2EE87D486DFB5A7BFF",
"sn259rEFXrQrWyx3Q7XneWcwV6dfL");

var decoded = DecodeSeed("sEdTM1uX8pu2do5XvTnutH6HsouMaM2");
Assert.AreEqual(decoded.Bytes.Length, 16);
Assert.AreEqual(decoded.Type, "ed25519");

AssertEncodes(
"EdSeed",
"4C3A1D213FBDFB14C7C28D609469B341",
"sEdTM1uX8pu2do5XvTnutH6HsouMaM2");
}

[TestMethod]
public void AddressTest()
{
AssertEncodes(
"Address",
"0000000000000000000000000000000000000000",
"rrrrrrrrrrrrrrrrrrrrrhoLvTp");

AssertEncodes(
"Address",
"BA8E78626EE42C41B46D46C3048DF3A1C3C87072",
"rJrRMgiRgrU6hDF4pgu5DXQdWyPbY35ErN");
}

public void AssertEncodes(string type, string hex, string base58)
{
Type kls = typeof(AddressCodec);
var encode = kls.GetMethod("Encode" + type);
var decode = kls.GetMethod("Decode" + type);
var validate = kls.GetMethod("IsValid" + type);
var encoded = Helpers.Invoke<string>(encode, Helpers.DecodeHex(hex));
var decoded = Helpers.Invoke<byte[]>(decode, base58);
var valid = Helpers.Invoke<bool>(validate, base58);
var hexIsValidB58 = Helpers.Invoke<bool>(validate, hex);

Assert.AreEqual(base58, encoded);
Assert.AreEqual(hex, Helpers.EncodeHex(decoded));
Assert.IsTrue(valid);
Assert.IsFalse(hexIsValidB58);
}
}
}
55 changes: 55 additions & 0 deletions src/ripple-dot-net/Ripple.Address.Tests/Helpers.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using System.Reflection;

namespace Ripple.Address.Tests
{
internal class B16
{
public static string Encode(byte[] data)
{
if (data == null)
return null;
char[] c = new char[data.Length * 2];
int b;
for (int i = 0; i < data.Length; i++)
{
b = data[i] >> 4;
c[i * 2] = (char)(55 + b + (((b - 10) >> 31) & -7));
b = data[i] & 0xF;
c[i * 2 + 1] = (char)(55 + b + (((b - 10) >> 31) & -7));
}
return new string(c);
}


public static byte[] Decode(string hexString)
{
if (hexString == null)
return null;
if (hexString.Length % 2 != 0)
throw new FormatException("The hex string is invalid because it has an odd length");
var result = new byte[hexString.Length / 2];
for (int i = 0; i < result.Length; i++)
result[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
return result;
}
}

internal class Helpers
{
public static T Invoke<T>(MethodInfo info, params object[] objects)
{
return (T)info.Invoke(null, objects);
}

public static byte[] DecodeHex(string hex)
{
return B16.Decode(hex);
}

public static string EncodeHex(byte[] buffer)
{
return B16.Encode(buffer);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170923-02" />
<PackageReference Include="MSTest.TestAdapter" Version="1.2.0-beta3" />
<PackageReference Include="MSTest.TestFramework" Version="1.2.0-beta3" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Ripple.Address\Ripple.Address.csproj" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

</Project>
121 changes: 121 additions & 0 deletions src/ripple-dot-net/Ripple.Address/AddressCodec.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
using System;

namespace Ripple.Address
{
public class AddressCodec
{
public const string Alphabet = "rpshnaf39wBUDNEGHJKLM4PQRST7VWXYZ2bcdeCg65jkm8oFqi1tuvAxyz";

// ReSharper disable RedundantArgumentNameForLiteralExpression, RedundantArgumentName
public static B58.Version AccountId = B58.Version.With(versionByte: 0, expectedLength: 20);

public static B58.Version NodePublic = B58.Version.With(versionByte: 28, expectedLength: 33);
public static B58.Version NodePrivate = B58.Version.With(versionByte: 32, expectedLength: 32);

public static B58.Version AccountPublic = B58.Version.With(versionByte: 35, expectedLength: 33);
public static B58.Version AccountPrivate = B58.Version.With(versionByte: 34, expectedLength: 32);

public static B58.Version FamilyGenerator = B58.Version.With(versionByte: 41, expectedLength: 33);

public static B58.Version K256Seed = B58.Version.With(versionByte: 33, expectedLength: 16);
public static B58.Version Ed25519Seed = B58.Version.With(versionBytes: new byte[]{ 0x1, 0xe1, 0x4b }, expectedLength: 16);

public static B58.Versions AnySeed = B58.Versions
.With("secp256k1", K256Seed)
.And("ed25519", Ed25519Seed);
// ReSharper restore RedundantArgumentNameForLiteralExpression, RedundantArgumentName
private static readonly B58 B58;
static AddressCodec()
{
B58 = new B58(Alphabet);
}

public class DecodedSeed
{
public readonly string Type;
public readonly byte[] Bytes;

public DecodedSeed(string type, byte[] payload)
{
Type = type;
Bytes = payload;
}
}

public static DecodedSeed DecodeSeed(string seed)
{
var decoded = B58.Decode(seed, AnySeed);
return new DecodedSeed(decoded.Type, decoded.Payload);
}

public static string EncodeSeed(byte[] bytes, string type)
{
return B58.Encode(bytes, type, AnySeed);
}

public static byte[] DecodeK256Seed(string seed)
{
return B58.Decode(seed, K256Seed);
}

public static string EncodeK256Seed(byte[] bytes)
{
return B58.Encode(bytes, K256Seed);
}

public static byte[] DecodeEdSeed(string base58)
{
return B58.Decode(base58, Ed25519Seed);
}

public static string EncodeEdSeed(byte[] bytes)
{
return B58.Encode(bytes, Ed25519Seed);
}

public static string EncodeAddress(byte[] bytes)
{
return B58.Encode(bytes, AccountId);
}

public static string EncodeNodePublic(byte[] bytes)
{
return B58.Encode(bytes, NodePublic);
}

public static byte[] DecodeNodePublic(string publicKey)
{
return B58.Decode(publicKey, NodePublic);
}

public static byte[] DecodeAddress(string address)
{
return B58.Decode(address, AccountId);
}

public static bool IsValidAddress(string address)
{
return B58.IsValid(address, AccountId);
}

public static bool IsValidNodePublic(string nodePublic)
{
return B58.IsValid(nodePublic, NodePublic);
}

public static bool IsValidSeed(string seed)
{
return B58.IsValid(seed, AnySeed);
}

public static bool IsValidEdSeed(string edSeed)
{
return B58.IsValid(edSeed, Ed25519Seed);
}

public static bool IsValidK256Seed(string k256Seed)
{
return B58.IsValid(k256Seed, K256Seed);
}
}
}
Loading

0 comments on commit 4dc2d50

Please sign in to comment.