├── .gitattributes
├── .github
└── workflows
│ └── dotnet.yml
├── .gitignore
├── DemoApiTest
├── DemoApiTest.csproj
├── Info.txt
├── JtonConnectFour.json
└── Program.cs
├── DemoWalletTest
├── DemoWalletTest.csproj
├── Program.cs
└── wallet.dat
├── IntegrationTest
├── IntegrationTest.csproj
└── LocalNodeTests.cs
├── LICENSE
├── README.md
├── Sandbox
├── Program.cs
├── Sandbox.csproj
└── dotmogMetaData.json
├── SubstrateNetApi.sln
├── SubstrateNetApi
├── CompactInteger.cs
├── Constants.cs
├── Doxyfile
├── Exceptions
│ ├── ClientNotConnectedException.cs
│ ├── ConverterAlreadyRegisteredException.cs
│ ├── MissingConverterException.cs
│ ├── MissingModuleOrItemException.cs
│ └── MissingParameterException.cs
├── HashExtension.cs
├── ITypeConverter.cs
├── MetaDataParser.cs
├── Mnemonic.cs
├── Model
│ ├── Calls
│ │ └── GenericExtrinsicCall.cs
│ ├── Custom
│ │ ├── DOTMog
│ │ │ ├── DOTMog.json
│ │ │ ├── DOTMogCalls.cs
│ │ │ └── DOTMogTypes.cs
│ │ ├── Kusama
│ │ │ ├── KusamaCalls.cs
│ │ │ └── KusamaTypes.cs
│ │ ├── Polkadot
│ │ │ ├── PolkadotCalls.cs
│ │ │ └── PolkadotTypes.cs
│ │ └── UniqueTestnet
│ │ │ ├── UniqueTestnet.json
│ │ │ ├── UniqueTestnetCalls.cs
│ │ │ └── UniqueTestnetTypes.cs
│ ├── Extrinsics
│ │ ├── Era.cs
│ │ ├── Extrinsic.cs
│ │ ├── Method.cs
│ │ ├── Payload.cs
│ │ ├── SignedExtensions.cs
│ │ └── UnCheckedExtrinsic.cs
│ ├── Meta
│ │ ├── Argument.cs
│ │ ├── Call.cs
│ │ ├── Const.cs
│ │ ├── Error.cs
│ │ ├── Event.cs
│ │ ├── Function.cs
│ │ ├── Item.cs
│ │ ├── MetaData.cs
│ │ ├── Module.cs
│ │ └── Storage.cs
│ ├── Rpc
│ │ ├── Block.cs
│ │ ├── BlockData.cs
│ │ ├── Digest.cs
│ │ ├── ExtrinsicStatus.cs
│ │ ├── Header.cs
│ │ ├── Health.cs
│ │ ├── RuntimeVersion.cs
│ │ └── StorageChangeSet.cs
│ └── Types
│ │ ├── Account.cs
│ │ ├── Base
│ │ ├── AccountId.cs
│ │ ├── Assets.cs
│ │ ├── Balance.cs
│ │ ├── BlockNumber.cs
│ │ ├── Bool.cs
│ │ ├── Hash.cs
│ │ ├── InheritedBaseTypes.cs
│ │ ├── U128.cs
│ │ ├── U16.cs
│ │ ├── U32.cs
│ │ ├── U64.cs
│ │ └── U8.cs
│ │ ├── BaseType.cs
│ │ ├── Enum
│ │ ├── BalanceStatus.cs
│ │ ├── DispatchClass.cs
│ │ ├── ElectionCompute.cs
│ │ ├── Pays.cs
│ │ ├── PhaseState.cs
│ │ ├── ProxyType.cs
│ │ └── VoteThreshold.cs
│ │ ├── EnumType.cs
│ │ ├── ExtEnumType.cs
│ │ ├── IEncodable.cs
│ │ ├── IType.cs
│ │ ├── Struct
│ │ ├── AccountData.cs
│ │ ├── AccountInfo.cs
│ │ ├── Assets.cs
│ │ ├── BaseEvent.cs
│ │ ├── Dispatchinfo.cs
│ │ ├── EventRecord.cs
│ │ ├── EventRecords.cs
│ │ ├── InheritedStructTypes.cs
│ │ ├── Option.cs
│ │ ├── Phase.cs
│ │ ├── RustTuple.cs
│ │ ├── Timepoint.cs
│ │ └── Vec.cs
│ │ ├── StructType.cs
│ │ ├── TypeUtil.cs
│ │ └── UnimplementedTypes.cs
├── Modules
│ ├── Author.cs
│ ├── Chain.cs
│ ├── State.cs
│ └── System.cs
├── RequestGenerator.cs
├── SubscriptionListener.cs
├── SubstrateClient.cs
├── SubstrateNetApi.csproj
├── TypeConverters
│ ├── ExtrinsicJsonConverter.cs
│ ├── ExtrinsicStatusJsonConverter.cs
│ └── GenericTypeConverter.cs
└── Utils.cs
├── SubstrateNetApiTest
├── ClientTests
│ ├── ClientTests.cs
│ └── GetStorageTests.cs
├── CompactIntegerTests.cs
├── CustomTests
│ ├── CustomExtrinsicTests.cs
│ ├── CustomStorageTests.cs
│ ├── CustomTypeConverters.cs
│ └── CustomTypes.cs
├── DOTMogNET.json
├── Extrinsic
│ ├── EraTest.cs
│ ├── MethodTest.cs
│ ├── PayloadTest.cs
│ ├── SignedExtensionsTest.cs
│ └── UnCheckedExtrinsicTest.cs
├── HashExtensionTests.cs
├── Keys
│ ├── Ed25519Tests.cs
│ └── Sr25519Tests.cs
├── MemonicTest.cs
├── RequestGeneratorTests.cs
├── Subscription
│ └── SubscriptionTest.cs
├── SubstrateNetApiTests.csproj
├── SystemInteraction.cs
├── TypeConverters
│ ├── EventDecodeTest.cs
│ ├── TypeConverterTest.cs
│ ├── TypeEncodingTest.cs
│ └── TypesMapperTest.cs
├── UtilsTests.cs
└── Values
│ └── ValueTests.cs
├── SubstrateNetWallet
├── Caching.cs
├── ChainInfo.cs
├── ManagedAes.cs
├── SubstrateNetWallet.csproj
├── SystemInteraction.cs
├── Wallet.cs
└── WalletFile.cs
├── SubstrateNetWalletTest
├── README.md
├── SubstrateNetWalletTests.csproj
├── WalletTest.cs
└── dev_wallet.dat
├── TestExtrinsic
├── Program.cs
└── TestExtrinsic.csproj
├── appveyor.yml
└── images
├── advanced_architecture.png
├── basic_architecture.png
└── dependencies.png
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.github/workflows/dotnet.yml:
--------------------------------------------------------------------------------
1 | name: .NET
2 |
3 | on:
4 | push:
5 | branches: [ origin ]
6 | pull_request:
7 | branches: [ origin ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 | strategy:
14 | matrix:
15 | dotnet-version: ["3.1.x"]
16 |
17 | steps:
18 | - uses: actions/checkout@v2
19 | - name: Setup .NET Core SDK ${{matrix.dotnet-version}}
20 | uses: actions/setup-dotnet@v1
21 | with:
22 | dotnet-version: ${{matrix.dotnet-version}}
23 | - name: Restore dependencies
24 | run: dotnet restore
25 | - name: Build
26 | run: dotnet build --no-restore
27 | - name: Test
28 | run: dotnet test --filter SubstrateNetApi\!~IntegrationTest --no-build --verbosity normal
29 | - name: Publish SubstrateNetApi
30 | uses: brandedoutcast/publish-nuget@v2.5.2
31 | with:
32 | PROJECT_FILE_PATH: SubstrateNetApi/SubstrateNetApi/SubstrateNetApi.csproj
33 | NUGET_KEY: ${{secrets.NUGET_API_KEY}}
34 |
--------------------------------------------------------------------------------
/DemoApiTest/DemoApiTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/DemoApiTest/Info.txt:
--------------------------------------------------------------------------------
1 | cargo test -p node-runtime -- --nocapture
2 |
3 | ##############################################
4 | Call: Call::Balances(transfer(Address::Id(d43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d (5GrwvaEF...)), 100))
5 | Call Encoded: [6, 0, 255, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 145, 1]
6 | Extra: (CheckSpecVersion, CheckTxVersion, CheckGenesis, CheckMortality(Era::Mortal(2048, 99)), CheckNonce(0), CheckWeight, ChargeTransactionPayment<0>)
7 | Extra Encoded: [58, 6, 0, 0]
8 | Additional Signed: (259, 1, 0x0000000000000000000000000000000000000000000000000000000000000000, 0x0000000000000000000000000000000000000000000000000000000000000000, (), (), ())
9 | Additional Signed Encoded: [3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
10 | ##############################################
11 | Raw Payload: [6, 0, 255, 212, 53, 147, 199, 21, 253, 211, 28, 97, 20, 26, 189, 4, 169, 159, 214, 130, 44, 133, 88, 133, 76, 205, 227, 154, 86, 132, 231, 165, 109, 162, 125, 145, 1, 58, 6, 0, 0, 3, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
--------------------------------------------------------------------------------
/DemoWalletTest/DemoWalletTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 | Always
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/DemoWalletTest/Program.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.IO;
3 | using System.Threading.Tasks;
4 | using SubstrateNetApi.Model.Types.Struct;
5 | using SubstrateNetWallet;
6 |
7 | namespace DemoWalletTest
8 | {
9 | internal class Program
10 | {
11 | private static async Task Main(string[] args)
12 | {
13 | SystemInteraction.ReadData = f => File.ReadAllText(Path.Combine(Environment.CurrentDirectory, f));
14 | SystemInteraction.DataExists = f => File.Exists(Path.Combine(Environment.CurrentDirectory, f));
15 | SystemInteraction.ReadPersistent = f => File.ReadAllText(Path.Combine(Environment.CurrentDirectory, f));
16 | SystemInteraction.PersistentExists = f => File.Exists(Path.Combine(Environment.CurrentDirectory, f));
17 | SystemInteraction.Persist = (f, c) => File.WriteAllText(Path.Combine(Environment.CurrentDirectory, f), c);
18 |
19 | // create new wallet with password and persist
20 | var wallet = new Wallet();
21 |
22 | wallet.ChainInfoUpdated += Wallet_ChainInfoUpdated;
23 |
24 | wallet.AccountInfoUpdated += Wallet_AccountInfoUpdated;
25 |
26 | await wallet.StartAsync();
27 |
28 | if (!wallet.IsConnected) return;
29 |
30 | if (wallet.Load())
31 | {
32 | Console.WriteLine("wallet unlocked");
33 | await wallet.UnlockAsync("Aa123456");
34 | }
35 | else if (!wallet.IsCreated)
36 | {
37 | Console.WriteLine("wallet created");
38 | await wallet.CreateAsync("Aa123456");
39 | }
40 |
41 | Console.WriteLine(wallet.Account.Value);
42 |
43 | Console.ReadKey();
44 |
45 | await wallet.StopAsync();
46 | }
47 |
48 | private static void Wallet_AccountInfoUpdated(object sender, AccountInfo accountInfo)
49 | {
50 | Console.WriteLine($"CallBack[AccountInfo]: {accountInfo}");
51 | }
52 |
53 | private static void Wallet_ChainInfoUpdated(object sender, ChainInfo chainInfo)
54 | {
55 | Console.WriteLine($"CallBack[ChainInfo]: {chainInfo}");
56 | }
57 | }
58 | }
--------------------------------------------------------------------------------
/DemoWalletTest/wallet.dat:
--------------------------------------------------------------------------------
1 | {"PublicKey":"E38P/IkE9Cqqv2PU7ICGL1CHqLBZJgwZFAHgPAODZ2c=","EncryptedSeed":"Eh8sWgjyZBa4bleAFQUyBr2nq5m6YP9rudzfNK45MA2y0h3IP7V8FuIBJs1D5Gla+nECMqbPacKPlEffyPVc6qbZfmRgNt2NtIquXD4Od+o=","Salt":"7JWPWnzW3xWqlkw7LGBGHQ=="}
--------------------------------------------------------------------------------
/IntegrationTest/IntegrationTest.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | netcoreapp3.1
5 |
6 | false
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SubstrateNetApi (NETStandard2.0)
2 | *Just another Substrate .NET API*
3 | [](https://ci.appveyor.com/project/darkfriend77/substratenetapi)
4 | [](https://ci.appveyor.com/project/darkfriend77/substratenetapi/build/artifacts)
5 | [](https://github.com/darkfriend77/SubstrateNetApi/issues)
6 | [](https://github.com/darkfriend77/SubstrateNetApi/blob/origin/LICENSE)
7 | [](https://github.com/darkfriend77/SubstrateNetApi/graphs/contributors)
8 |
9 | SubstrateNetApi is written in [NETStandard2.0](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) to provide maximum compatibility for [Unity3D](https://docs.unity3d.com/2020.2/Documentation/Manual/dotnetProfileSupport.html). Feedback, constructive critisme and disscussions are welcome and will help us to improve the API! (Telegram: @darkfriend77, Discord: darkfriend77#3753)
10 |
11 | If you enjoy using SubstrateNetApi consider supporting me at [buymeacoffee.com/darkfriend77](https://www.buymeacoffee.com/darkfriend77)
12 |
13 | ## Substrate Version
14 | **Important** This API is for the major release [**Substrate v3.0/0.9 – Apollo 14**](https://github.com/paritytech/substrate/releases/tag/v3.0.0), for releases [monthly-2021-10](https://github.com/paritytech/substrate/releases/tag/monthly-2021-10) and newer please refere to the [Ajuna.NetApi](https://github.com/ajuna-network/Ajuna.NetApi)
15 |
16 | ## General information
17 |
18 | This project has been started by darkfriend77 as a proove of concept in october'20, the goal was to proove Unity3D compatibility towards substrate. [Substrate](https://substrate.dev/) is a modular framework that enables you to create purpose-built blockchains by composing custom or pre-built components. After succeding every aspect crucial for developing mobile games on unity against substrate, the project decided to work further with the API and apply for a first [open-grant](https://github.com/w3f/Grants-Program/blob/master/applications/dotmog.md) from the Web3 Foundation.
19 |
20 | 
21 |
22 | The project behind the SubstrateNetApi, is [Ajuna Network](https://ajuna.io/) and the flagship game [DOTMog](dotmog.com), backed by the swiss company BloGa Tech AG.
23 |
24 | ## Table of Content
25 |
26 | 1. [Home](https://github.com/JetonNetwork/SubstrateNetApi/wiki)
27 | 1. [Requirements](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Requirements)
28 | 1. [Installation](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Installation)
29 | - [NuGet](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Installation#substrate-in-net)
30 | - [Unity3D](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Installation#substrate-in-unity3d)
31 | 1. [Usage](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Usage)
32 | 1. [Types](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Types#types)
33 | - [Base Types](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Types#base-types)
34 | - [Enum Types](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Types#enum-types)
35 | - [Struct Types](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Types#struct-types)
36 | 1. [Wallet](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Wallet)
37 | - [Create](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Wallet#create-a-new-wallet)
38 | - [Mnemonic / Restore](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Wallet#mnemonic--restore)
39 | - [Wallet File](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Wallet#wallet-file)
40 | 1. [Extension](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Extension#extension)
41 | 1. [Testing](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Testing)
42 | - [Guide](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Testing#guide)
43 | - [Node-Template](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Testing#node-template)
44 | 1. [Project](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Project#project)
45 | - [Substrate Version](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Project#substrate-version)
46 | - [Upgrading Substrate Version](https://github.com/JetonNetwork/SubstrateNetApi/wiki/Project#upgrading-substrate-version)
47 |
48 | ## Other Projects
49 |
50 | Those projects are related to the SubstrateNetApi, by either implementing the API or being supported in the API.
51 |
52 | - [SubstrateNetApiExt](https://github.com/JetonNetwork/SubstrateNetApiExt), SubstrateNetApi Extension Template a simple template for custom extension that integrate a pallet or set of pallets functionality for SubstrateNetApi.
53 | - [Unity3DExample](https://github.com/dotmog/Unity3DExample), basic example of two test scenes implementing the substrate net api.
54 | - [SubstrateUnityWalletSimple](https://github.com/dotmog/SubstrateUnityWalletSimple), simple test wallet integrating the substrate net wallet.
55 | - [JtonNodeTemplate](https://github.com/JetonNetwork/JtonNodeTemplate), basic node-template currently used for the integration test and as development base for pallets.
56 | - [jton-pallet-template](https://github.com/JetonNetwork/jton-pallet-template), Jeton Network Pallet Template, This is a template for a Jeton Network pallet which lives as its own crate so it can be imported into multiple runtimes. It is based on the "template" pallet that is included with the Substrate node template.
57 |
58 | ## Special Thanks
59 | - https://github.com/gautamdhameja/sr25519-dotnet
60 | - https://github.com/usetech-llc/polkadot_api_dotnet
61 |
--------------------------------------------------------------------------------
/Sandbox/Sandbox.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | Exe
5 | netcoreapp3.1
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Constants.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi
2 | {
3 | public class Constants
4 | {
5 | public const ulong ExtrinsicEraPeriodDefault = 64;
6 |
7 | /// The extrinsic version
8 | public const byte ExtrinsicVersion = 4; // aka the 2. TRANSACTION_VERSION
9 | // https://github.com/paritytech/substrate/blob/c0cb70419798eb7fd38806da668bec05f8cfd7f1/primitives/runtime/src/generic/unchecked_extrinsic.rs#L33
10 |
11 | // https://polkadot.js.org/docs/api/FAQ/
12 | // 0 = AccountId (no prefix), 1 = IndicesLookup (0xFF), 2 = MutiAddress (https://github.com/paritytech/substrate/pull/7380)
13 | public static uint AddressVersion = 2;
14 | }
15 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Exceptions/ClientNotConnectedException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Exceptions
4 | {
5 | public class ClientNotConnectedException : Exception
6 | {
7 | public ClientNotConnectedException(string message)
8 | : base(message)
9 | { }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Exceptions/ConverterAlreadyRegisteredException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Exceptions
4 | {
5 | public class ConverterAlreadyRegisteredException : Exception
6 | {
7 | public ConverterAlreadyRegisteredException(string message)
8 | : base(message)
9 | { }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Exceptions/MissingConverterException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Exceptions
4 | {
5 | public class MissingConverterException : Exception
6 | {
7 | public MissingConverterException(string message) :
8 | base(message)
9 | { }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Exceptions/MissingModuleOrItemException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Exceptions
4 | {
5 | public class MissingModuleOrItemException : Exception
6 | {
7 | public MissingModuleOrItemException(string message)
8 | : base(message)
9 | {
10 |
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Exceptions/MissingParameterException.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Exceptions
4 | {
5 | public class MissingParameterException : Exception
6 | {
7 | public MissingParameterException(string message)
8 | : base(message)
9 | { }
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/HashExtension.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 | using Blake2Core;
5 | using Extensions.Data;
6 | using SubstrateNetApi.Model.Meta;
7 |
8 | namespace SubstrateNetApi
9 | {
10 | public class HashExtension
11 | {
12 | public static byte[] Hash(Storage.Hasher hasher, byte[] bytes)
13 | {
14 | switch (hasher)
15 | {
16 | case Storage.Hasher.Identity:
17 | return bytes;
18 |
19 | case Storage.Hasher.BlakeTwo128:
20 | return Blake2(bytes, 128);
21 |
22 | case Storage.Hasher.BlakeTwo256:
23 | return Blake2(bytes, 256);
24 |
25 | case Storage.Hasher.BlakeTwo128Concat:
26 | return Blake2Concat(bytes, 128);
27 |
28 | case Storage.Hasher.Twox128:
29 | return Twox128(bytes);
30 |
31 | case Storage.Hasher.Twox256:
32 | return Twox256(bytes);
33 |
34 | case Storage.Hasher.Twox64Concat:
35 | return Twox64Concat(bytes);
36 |
37 | case Storage.Hasher.None:
38 | default:
39 | throw new NotSupportedException();
40 | }
41 | }
42 |
43 | /// Blake2 hashed the specified bytes.
44 | /// The bytes.
45 | /// The size.
46 | /// The key.
47 | ///
48 | ///
49 | ///
50 | public static byte[] Blake2(byte[] bytes, int size = 128, IReadOnlyList key = null)
51 | {
52 | var config = new Blake2BConfig {OutputSizeInBits = size, Key = null};
53 | return Blake2B.ComputeHash(bytes, config);
54 | }
55 |
56 | /// Blake2 hashed with bytes concated at the end.
57 | /// The bytes.
58 | /// The size.
59 | ///
60 | ///
61 | ///
62 | public static byte[] Blake2Concat(byte[] bytes, int size = 128)
63 | {
64 | var config = new Blake2BConfig {OutputSizeInBits = size, Key = null};
65 | return Blake2B.ComputeHash(bytes, config).Concat(bytes).ToArray();
66 | }
67 |
68 | /// Blake2 hashed the specified ss prefixed.
69 | /// The ss prefixed.
70 | /// The start.
71 | /// The count.
72 | ///
73 | ///
74 | ///
75 | internal static byte[] Blake2(byte[] ssPrefixed, int start, int count)
76 | {
77 | return Blake2B.ComputeHash(ssPrefixed, start, count);
78 | }
79 |
80 | /// XXHash 128 bytes.
81 | /// The bytes.
82 | ///
83 | ///
84 | ///
85 | public static byte[] Twox128(byte[] bytes)
86 | {
87 | return BitConverter.GetBytes(XXHash.XXH64(bytes, 0))
88 | .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 1)))
89 | .ToArray();
90 | }
91 |
92 | /// XXHash 256 bytes.
93 | /// The bytes.
94 | ///
95 | ///
96 | ///
97 | public static byte[] Twox256(byte[] bytes)
98 | {
99 | return BitConverter.GetBytes(XXHash.XXH64(bytes, 0))
100 | .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 1)))
101 | .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 2)))
102 | .Concat(BitConverter.GetBytes(XXHash.XXH64(bytes, 3)))
103 | .ToArray();
104 | }
105 |
106 | /// XXHashed 64 hashed with bytes concated at the end.
107 | /// The bytes.
108 | ///
109 | ///
110 | ///
111 | public static byte[] Twox64Concat(byte[] bytes)
112 | {
113 | return BitConverter.GetBytes(XXHash.XXH64(bytes)).Concat(bytes).ToArray().ToArray();
114 | }
115 | }
116 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/ITypeConverter.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi
2 | {
3 | /// Interface for type converter.
4 | /// 19.09.2020.
5 | public interface ITypeConverter
6 | {
7 | /// Gets the name of the type.
8 | /// The name of the type.
9 | string TypeName { get; }
10 |
11 | /// Creates a new object.
12 | /// The value.
13 | /// An object.
14 | object Create(string value);
15 | }
16 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Mnemonic.cs:
--------------------------------------------------------------------------------
1 | using System.Text;
2 | using System.Security.Cryptography;
3 | using Schnorrkel.Keys;
4 | using System;
5 | using dotnetstandard_bip39;
6 | using System.IO;
7 |
8 | namespace SubstrateNetApi
9 | {
10 | public static class Mnemonic
11 | {
12 | ///
13 | /// Rfc2898DeriveBytes, with HMACSHA512 usable for .NETStandard 2.0
14 | ///
15 | ///
16 | ///
17 | ///
18 | ///
19 | ///
20 | public static byte[] PBKDF2Sha512GetBytes(int dklen, byte[] password, byte[] salt, int iterationCount)
21 | {
22 | using (var hmac = new HMACSHA512(password))
23 | {
24 | int hashLength = hmac.HashSize / 8;
25 | if ((hmac.HashSize & 7) != 0)
26 | hashLength++;
27 | int keyLength = dklen / hashLength;
28 | if (dklen > (0xFFFFFFFFL * hashLength) || dklen < 0)
29 | {
30 | throw new ArgumentOutOfRangeException("dklen");
31 | }
32 | if (dklen % hashLength != 0)
33 | {
34 | keyLength++;
35 | }
36 | byte[] extendedkey = new byte[salt.Length + 4];
37 | Buffer.BlockCopy(salt, 0, extendedkey, 0, salt.Length);
38 | using (var ms = new MemoryStream())
39 | {
40 | for (int i = 0; i < keyLength; i++)
41 | {
42 | extendedkey[salt.Length] = (byte)(((i + 1) >> 24) & 0xFF);
43 | extendedkey[salt.Length + 1] = (byte)(((i + 1) >> 16) & 0xFF);
44 | extendedkey[salt.Length + 2] = (byte)(((i + 1) >> 8) & 0xFF);
45 | extendedkey[salt.Length + 3] = (byte)(((i + 1)) & 0xFF);
46 | byte[] u = hmac.ComputeHash(extendedkey);
47 | Array.Clear(extendedkey, salt.Length, 4);
48 | byte[] f = u;
49 | for (int j = 1; j < iterationCount; j++)
50 | {
51 | u = hmac.ComputeHash(u);
52 | for (int k = 0; k < f.Length; k++)
53 | {
54 | f[k] ^= u[k];
55 | }
56 | }
57 | ms.Write(f, 0, f.Length);
58 | Array.Clear(u, 0, u.Length);
59 | Array.Clear(f, 0, f.Length);
60 | }
61 | byte[] dk = new byte[dklen];
62 | ms.Position = 0;
63 | ms.Read(dk, 0, dklen);
64 | ms.Position = 0;
65 | for (long i = 0; i < ms.Length; i++)
66 | {
67 | ms.WriteByte(0);
68 | }
69 | Array.Clear(extendedkey, 0, extendedkey.Length);
70 | return dk;
71 | }
72 | }
73 | }
74 |
75 | ///
76 | /// Get seed from entropy bytes, make sure entropy is a byte array from a correctly recovered and checksumed BIP39.
77 | /// Following slices are supported:
78 | /// + 16 bytes for 12 words.
79 | /// + 20 bytes for 15 words.
80 | /// + 24 bytes for 18 words.
81 | /// + 28 bytes for 21 words.
82 | /// + 32 bytes for 24 words.
83 | /// Other slices will lead to a InvalidEntropy error.
84 | /// https://github.com/paritytech/substrate-bip39/blob/eef2f86337d2dab075806c12948e8a098aa59d59/src/lib.rs#L45
85 | ///
86 | ///
87 | ///
88 | ///
89 | public static byte[] SeedFromEntropy(byte[] entropyBytes, string password)
90 | {
91 | if (entropyBytes.Length < 16 || entropyBytes.Length > 32 || entropyBytes.Length % 4 != 0)
92 | {
93 | throw new Exception($"InvalidEntropy, length not allowed '{entropyBytes.Length}'");
94 | }
95 | var saltBytes = Encoding.UTF8.GetBytes("mnemonic" + password);
96 | return PBKDF2Sha512GetBytes(64, entropyBytes, saltBytes, 2048);
97 | }
98 |
99 | ///
100 | ///
101 | ///
102 | ///
103 | ///
104 | ///
105 | public static string GetEntropy(string mnemonic, BIP39Wordlist bIP39Wordlist)
106 | => new BIP39().MnemonicToEntropy(mnemonic, bIP39Wordlist);
107 |
108 | ///
109 | /// Get secret key from mnemonic
110 | ///
111 | ///
112 | ///
113 | ///
114 | ///
115 | public static byte[] GetSecretKeyFromMnemonic(string mnemonic, string password, BIP39Wordlist bIP39Wordlist)
116 | {
117 | var entropyBytes = Utils.HexToByteArray(GetEntropy(mnemonic, bIP39Wordlist));
118 | var seedBytes = SeedFromEntropy(entropyBytes, password);
119 | return seedBytes.AsMemory().Slice(0, 32).ToArray();
120 | }
121 |
122 | ///
123 | /// Get a key pair from mnemonic
124 | ///
125 | ///
126 | ///
127 | ///
128 | ///
129 | ///
130 | public static KeyPair GetKeyPairFromMnemonic(string mnemonic, string password, BIP39Wordlist bIP39Wordlist, ExpandMode expandMode)
131 | {
132 | var secretBytes = GetSecretKeyFromMnemonic(mnemonic, password, bIP39Wordlist);
133 | var miniSecret = new MiniSecret(secretBytes, expandMode);
134 | return new KeyPair(miniSecret.ExpandToPublic(), miniSecret.ExpandToSecret());
135 | }
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Calls/GenericExtrinsicCall.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types;
2 | using System.Collections.Generic;
3 | using System.Numerics;
4 | using SubstrateNetApi.Model.Types.Base;
5 |
6 | namespace SubstrateNetApi.Model.Calls
7 | {
8 | public class GenericExtrinsicCall : IEncodable
9 | {
10 | public string ModuleName { get; }
11 |
12 | public string CallName { get; }
13 |
14 | public List CallArguments;
15 |
16 | public GenericExtrinsicCall(string moduleName, string callName, params IType[] list)
17 | {
18 | ModuleName = moduleName;
19 |
20 | CallName = callName;
21 |
22 | CallArguments = new List();
23 |
24 | foreach (var element in list)
25 | {
26 | CallArguments.Add(element);
27 | }
28 | }
29 |
30 | public byte[] Encode()
31 | {
32 | var byteList = new List();
33 | foreach (var callArgument in CallArguments)
34 | {
35 | byteList.AddRange(callArgument.Encode());
36 | }
37 | return byteList.ToArray();
38 | }
39 | }
40 |
41 | public class ExtrinsicCall
42 | {
43 | public static GenericExtrinsicCall BalanceTransfer(string address, BigInteger balanceAmount)
44 | {
45 | var accountId = new AccountId();
46 | accountId.Create(Utils.GetPublicKeyFrom(address));
47 |
48 | var balance = new Balance();
49 | balance.Create(balanceAmount);
50 |
51 | return BalanceTransfer(accountId, balance);
52 | }
53 |
54 | public static GenericExtrinsicCall BalanceTransfer(AccountId dest, Balance value)
55 | {
56 | return new GenericExtrinsicCall("Balances", "transfer", dest, value);
57 | }
58 |
59 | public static GenericExtrinsicCall BalanceSetBalance(AccountId who, Balance newFree, Balance newReserved)
60 | {
61 | return new GenericExtrinsicCall("Balances", "set_balance", who, newFree, newReserved);
62 | }
63 |
64 | public static GenericExtrinsicCall BalanceForceTransfer(AccountId source, AccountId dest, Balance value)
65 | {
66 | return new GenericExtrinsicCall("Balances", "force_transfer", source, dest, value);
67 | }
68 |
69 | public static GenericExtrinsicCall BalanceTransferKeepAlive(AccountId dest, Balance value)
70 | {
71 | return new GenericExtrinsicCall("Balances", "transfer_keep_alive", dest, value);
72 | }
73 |
74 | public static GenericExtrinsicCall SystemSuicide()
75 | {
76 | return new GenericExtrinsicCall("System", "suicide");
77 | }
78 |
79 | public static GenericExtrinsicCall TimestampSet(Moment moment)
80 | {
81 | return new GenericExtrinsicCall("Timestamp", "set");
82 | }
83 |
84 |
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Custom/Kusama/KusamaCalls.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types;
2 | using SubstrateNetApi.Model.Types.Base;
3 | using SubstrateNetApi.Model.Types.Custom;
4 | using SubstrateNetApi.Model.Types.Enum;
5 | using SubstrateNetApi.Model.Types.Struct;
6 | using System.Linq;
7 | using System.Numerics;
8 | using System.Text;
9 |
10 | namespace SubstrateNetApi.Model.Calls
11 | {
12 | public class KusamaCall
13 | {
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Custom/Kusama/KusamaTypes.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types.Base;
2 | using SubstrateNetApi.Model.Types.Enum;
3 | using SubstrateNetApi.Model.Types.Struct;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Numerics;
7 | using System.Text;
8 |
9 | namespace SubstrateNetApi.Model.Types.Custom
10 | {
11 | #region BASE_TYPES
12 |
13 | #endregion
14 |
15 | #region ENUM_TYPES
16 |
17 | #endregion
18 |
19 | #region STRUCT_TYPES
20 |
21 | #endregion
22 |
23 | #region INHERITED_TYPES
24 |
25 | #endregion
26 |
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Custom/Polkadot/PolkadotCalls.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types;
2 | using SubstrateNetApi.Model.Types.Base;
3 | using SubstrateNetApi.Model.Types.Custom;
4 | using SubstrateNetApi.Model.Types.Enum;
5 | using SubstrateNetApi.Model.Types.Struct;
6 | using System.Linq;
7 | using System.Numerics;
8 | using System.Text;
9 |
10 | namespace SubstrateNetApi.Model.Calls
11 | {
12 | public class PolkaDotCall
13 | {
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Custom/Polkadot/PolkadotTypes.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types.Base;
2 | using SubstrateNetApi.Model.Types.Enum;
3 | using SubstrateNetApi.Model.Types.Struct;
4 | using System;
5 | using System.Collections.Generic;
6 | using System.Numerics;
7 | using System.Text;
8 |
9 | namespace SubstrateNetApi.Model.Types.Custom
10 | {
11 | #region BASE_TYPES
12 |
13 | #endregion
14 |
15 | #region ENUM_TYPES
16 |
17 | #endregion
18 |
19 | #region STRUCT_TYPES
20 |
21 | #endregion
22 |
23 | #region INHERITED_TYPES
24 |
25 | #endregion
26 |
27 |
28 | }
29 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Custom/UniqueTestnet/UniqueTestnetCalls.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types;
2 | using SubstrateNetApi.Model.Types.Base;
3 | using SubstrateNetApi.Model.Types.Custom;
4 | using SubstrateNetApi.Model.Types.Enum;
5 | using SubstrateNetApi.Model.Types.Struct;
6 | using System.Linq;
7 | using System.Numerics;
8 | using System.Text;
9 |
10 | namespace SubstrateNetApi.Model.Calls
11 | {
12 | public class UniqueCall
13 | {
14 |
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Extrinsics/Era.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using SubstrateNetApi.Model.Types;
3 | using System;
4 |
5 | namespace SubstrateNetApi.Model.Extrinsics
6 | {
7 | public class Era : IEncodable
8 | {
9 | public bool IsImmortal { get; }
10 |
11 | public ulong Period { get; }
12 |
13 | public ulong Phase { get; }
14 |
15 | public ulong EraStart(ulong currentBlockNumber) => IsImmortal ? 0 : (Math.Max(currentBlockNumber, Phase) - Phase) / Period * Period + Phase;
16 |
17 | ///
18 | /// Initializes a new instance of the class.
19 | ///
20 | /// The period.
21 | /// The phase.
22 | /// if set to true [is immortal].
23 | public Era(ulong period, ulong phase, bool isImmortal)
24 | {
25 | Period = period;
26 | Phase = phase;
27 | IsImmortal = isImmortal;
28 |
29 | }
30 |
31 | override
32 | public string ToString()
33 | {
34 | return JsonConvert.SerializeObject(this);
35 | }
36 |
37 | ///
38 | /// Creates the specified life time.
39 | ///
40 | /// The life time.
41 | /// The finalized header block number.
42 | ///
43 | public static Era Create(uint lifeTime, ulong finalizedHeaderBlockNumber)
44 | {
45 | if (lifeTime == 0)
46 | {
47 | return new Era(0, 0, true);
48 | }
49 |
50 | // NODE: { "IsImmortal":false,"Period":64,"Phase":49}
51 | // API: { "IsImmortal":false,"Period":64,"Phase":61}
52 |
53 | // RUST Implementation
54 | //let period = period.checked_next_power_of_two()
55 | // .unwrap_or(1 << 16)
56 | // .max(4)
57 | // .min(1 << 16);
58 | //let phase = current % period;
59 | //let quantize_factor = (period >> 12).max(1);
60 | //let quantized_phase = phase / quantize_factor * quantize_factor;
61 | //Era::Mortal(period, quantized_phase)
62 |
63 | ulong period = (ulong)Math.Pow(2, Math.Round(Math.Log(lifeTime, 2)));
64 | period = Math.Max(period, 4);
65 | period = Math.Min(period, 65536);
66 | ulong phase = finalizedHeaderBlockNumber % period;
67 | var quantize_factor = Math.Max(period >> 12, 1);
68 | var quantized_phase = phase / quantize_factor * quantize_factor;
69 |
70 | return new Era(period, quantized_phase, false);
71 | }
72 |
73 | ///
74 | /// Encodes this instance.
75 | ///
76 | ///
77 | public byte[] Encode()
78 | {
79 | if (IsImmortal)
80 | {
81 | return new byte[] { 0x00 };
82 | }
83 | var quantizeFactor = Math.Max(1, Period / 4096);
84 | var lastBit = Period & (ulong)-(long)Period;
85 | //var rest = _period;
86 | //var lastBit = 1;
87 | //while (rest % 2 == 0 && rest != 0)
88 | //{
89 | // rest /= 2;
90 | // lastBit *= 2;
91 | //}
92 | var logOf2 = lastBit != 0 ? Math.Log(lastBit, 2) : 64;
93 | var low = (ushort)Math.Min(15, Math.Max(1, logOf2 - 1));
94 | var high = (ushort)(Phase / quantizeFactor << 4);
95 | var encoded = (ushort)(low | high);
96 |
97 | return BitConverter.GetBytes(encoded);
98 | }
99 |
100 | ///
101 | /// Decodes the specified bytes.
102 | ///
103 | /// The bytes.
104 | ///
105 | ///
106 | /// 0, 0, true
107 | /// or
108 | /// 0, 0, true
109 | ///
110 | public static Era Decode(byte[] bytes)
111 | {
112 | if (bytes.Length == 1 && bytes[0] == 0x00)
113 | {
114 | return new Era(0, 0, true);
115 |
116 | }
117 | else if (bytes.Length == 2)
118 | {
119 | var ul0 = (ulong)bytes[0];
120 | var ul1 = (ulong)bytes[1];
121 | var encoded = ul0 + (ul1 << 8);
122 | var period = 2UL << (int)(encoded % (1 << 4));
123 | var quantizeFactor = Math.Max(1, period >> 12);
124 | var phase = (encoded >> 4) * quantizeFactor;
125 | if (period < 4 || phase >= period)
126 | {
127 | throw new ArgumentException($"{Utils.Bytes2HexString(new byte[] { bytes[0], bytes[1] })} is not a valid representation of Era.");
128 | }
129 | return new Era(period, phase, false);
130 | }
131 | else
132 | {
133 | throw new Exception("Invalid byte array to get era.");
134 | }
135 | }
136 | }
137 | }
138 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Extrinsics/Method.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using SubstrateNetApi.Model.Meta;
3 | using System.Collections.Generic;
4 |
5 | namespace SubstrateNetApi.Model.Extrinsics
6 | {
7 | public class Method
8 | {
9 | public string ModuleName;
10 |
11 | public byte ModuleIndex;
12 |
13 | public string CallName;
14 |
15 | public byte CallIndex;
16 |
17 | public Argument[] Arguments;
18 |
19 | public byte[] Parameters;
20 |
21 | ///
22 | /// Initializes a new instance of the class.
23 | ///
24 | /// Index of the module.
25 | /// Index of the call.
26 | /// The parameters.
27 | public Method(byte moduleIndex, byte callIndex, byte[] parameters)
28 | {
29 | ModuleIndex = moduleIndex;
30 | CallIndex = callIndex;
31 | Parameters = parameters ?? new byte[0];
32 | }
33 |
34 | ///
35 | /// Initializes a new instance of the class.
36 | ///
37 | /// Index of the module.
38 | /// Index of the call.
39 | public Method(byte moduleIndex, byte callIndex)
40 | {
41 | ModuleIndex = moduleIndex;
42 | CallIndex = callIndex;
43 | Parameters = new byte[0];
44 | }
45 |
46 | ///
47 | /// Initializes a new instance of the class.
48 | ///
49 | /// The module.
50 | /// The call.
51 | /// The parameters.
52 | public Method(Module module, Call call, byte[] parameters)
53 | {
54 | ModuleName = module.Name;
55 | ModuleIndex = module.Index;
56 | CallName = call.Name;
57 | CallIndex = module.IndexOf(call);
58 | Parameters = parameters;
59 | }
60 |
61 | ///
62 | /// Encodes this instance.
63 | ///
64 | ///
65 | public byte[] Encode()
66 | {
67 | var result = new List();
68 | result.Add(ModuleIndex);
69 | result.Add(CallIndex);
70 | result.AddRange(Parameters);
71 | return result.ToArray();
72 | }
73 |
74 | public override string ToString()
75 | {
76 | return JsonConvert.SerializeObject(this);
77 | }
78 | }
79 | }
80 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Extrinsics/Payload.cs:
--------------------------------------------------------------------------------
1 | using System.Linq;
2 |
3 | namespace SubstrateNetApi.Model.Extrinsics
4 | {
5 | public class Payload
6 | {
7 | private Method _call;
8 | private SignedExtensions _signedExtension;
9 |
10 | ///
11 | /// Initializes a new instance of the class.
12 | ///
13 | /// The call.
14 | /// The signed extensions.
15 | public Payload(Method call, SignedExtensions signedExtensions)
16 | {
17 | _call = call;
18 | _signedExtension = signedExtensions;
19 | }
20 |
21 | ///
22 | /// Encodes this instance.
23 | ///
24 | ///
25 | public byte[] Encode()
26 | {
27 | byte[] bytes = _call.Encode().Concat(_signedExtension.Encode()).ToArray();
28 | return bytes;
29 | }
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Extrinsics/SignedExtensions.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types;
2 | using System.Collections.Generic;
3 | using SubstrateNetApi.Model.Types.Base;
4 |
5 | namespace SubstrateNetApi.Model.Extrinsics
6 | {
7 | public class SignedExtensions
8 | {
9 | private uint _specVersion;
10 |
11 | private uint _txVersion;
12 |
13 | private Hash _genesis;
14 |
15 | private Hash _startEra;
16 |
17 | private Era _mortality;
18 |
19 | private CompactInteger _nonce;
20 |
21 | private CompactInteger _chargeTransactionPayment;
22 |
23 | ///
24 | /// Initializes a new instance of the class.
25 | ///
26 | /// The spec version.
27 | /// The tx version.
28 | /// The genesis.
29 | /// The start era.
30 | /// The mortality.
31 | /// The nonce.
32 | /// The charge transaction payment.
33 | public SignedExtensions(uint specVersion, uint txVersion, Hash genesis, Hash startEra, Era mortality, CompactInteger nonce, CompactInteger chargeTransactionPayment)
34 | {
35 | _specVersion = specVersion;
36 | _txVersion = txVersion;
37 | _genesis = genesis;
38 | _startEra = startEra;
39 | _mortality = mortality;
40 | _nonce = nonce;
41 | _chargeTransactionPayment = chargeTransactionPayment;
42 | }
43 |
44 | ///
45 | /// Gets the extra.
46 | ///
47 | ///
48 | public byte[] GetExtra()
49 | {
50 | var bytes = new List();
51 |
52 | // CheckMortality
53 | bytes.AddRange(_mortality.Encode());
54 |
55 | // CheckNonce
56 | bytes.AddRange(_nonce.Encode());
57 |
58 | // ChargeTransactionPayment
59 | bytes.AddRange(_chargeTransactionPayment.Encode());
60 |
61 | return bytes.ToArray();
62 | }
63 |
64 | ///
65 | /// Gets the additional signed.
66 | ///
67 | ///
68 | public byte[] GetAdditionalSigned()
69 | {
70 | var bytes = new List();
71 |
72 | // CheckSpecVersion
73 | bytes.AddRange(Utils.Value2Bytes(_specVersion));
74 |
75 | // CheckTxVersion
76 | bytes.AddRange(Utils.Value2Bytes(_txVersion));
77 |
78 | // CheckGenesis
79 | bytes.AddRange(_genesis.Bytes);
80 |
81 | // CheckMortality, Additional Blockhash check. Immortal = genesis_hash, Mortal = logic
82 | bytes.AddRange(_startEra.Bytes);
83 |
84 | return bytes.ToArray();
85 | }
86 |
87 | ///
88 | /// Encodes this instance.
89 | ///
90 | ///
91 | public byte[] Encode()
92 | {
93 | var bytes = new List();
94 |
95 | // Extra: Era, Nonce & Tip
96 | bytes.AddRange(GetExtra());
97 |
98 | // Additional Signed: SpecVersion, TxVersion, Genesis, Blockhash
99 | bytes.AddRange(GetAdditionalSigned());
100 |
101 | return bytes.ToArray();
102 | }
103 |
104 | }
105 | }
106 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Extrinsics/UnCheckedExtrinsic.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types;
2 | using System;
3 | using System.Collections.Generic;
4 | using SubstrateNetApi.Model.Rpc;
5 | using SubstrateNetApi.Model.Types.Base;
6 |
7 | namespace SubstrateNetApi.Model.Extrinsics
8 | {
9 | public class UnCheckedExtrinsic : Extrinsic
10 | {
11 | private readonly Hash _genesis;
12 |
13 | private readonly Hash _startEra;
14 |
15 | ///
16 | /// Initializes a new instance of the class.
17 | ///
18 | /// if set to true [signed].
19 | /// The account.
20 | /// The method.
21 | /// The era.
22 | /// The nonce.
23 | /// The tip.
24 | /// The genesis.
25 | /// The start era.
26 | public UnCheckedExtrinsic(bool signed, Account account, Method method, Era era, CompactInteger nonce, CompactInteger tip, Hash genesis, Hash startEra)
27 | : base(signed, account, nonce, method, era, tip)
28 | {
29 | _genesis = genesis;
30 | _startEra = startEra;
31 | }
32 |
33 | ///
34 | /// Gets the payload.
35 | ///
36 | /// The runtime.
37 | ///
38 | public Payload GetPayload(RuntimeVersion runtime)
39 | {
40 | return new Payload(Method, new SignedExtensions(runtime.SpecVersion, runtime.TransactionVersion, _genesis, _startEra, Era, Nonce, Tip));
41 | }
42 |
43 | ///
44 | /// Adds the payload signature.
45 | ///
46 | /// The signature.
47 | public void AddPayloadSignature(byte[] signature)
48 | {
49 | Signature = signature;
50 | }
51 |
52 | ///
53 | /// Encodes this instance.
54 | ///
55 | ///
56 | /// Missing payload signature for signed transaction.
57 | public byte[] Encode()
58 | {
59 | if (Signed && Signature == null)
60 | {
61 | throw new Exception("Missing payload signature for signed transaction.");
62 | }
63 |
64 | var list = new List();
65 |
66 | // 4 is the TRANSACTION_VERSION constant and it is 7 bits long, the highest bit 1 for signed transaction, 0 for unsigned.
67 | list.Add((byte)(Constants.ExtrinsicVersion | (Signed ? 0x80 : 0)));
68 |
69 | // 32 bytes + prefix depending on address encoding in chain, see Constants.Address_version
70 | list.AddRange(Account.Encode());
71 |
72 | // key type ed = 00 and sr = FF
73 | list.Add(Account.KeyTypeByte);
74 |
75 | // add signature if exists
76 | if (Signature != null)
77 | {
78 | list.AddRange(Signature);
79 | }
80 | else
81 | {
82 |
83 | }
84 |
85 | list.AddRange(Era.Encode());
86 |
87 | list.AddRange(Nonce.Encode());
88 |
89 | list.AddRange(Tip.Encode());
90 |
91 | list.AddRange(Method.Encode());
92 |
93 | return Utils.SizePrefixedByteArray(list);
94 | }
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Argument.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 |
3 | namespace SubstrateNetApi.Model.Meta
4 | {
5 | public class Argument
6 | {
7 | public string Name { get; set; }
8 | public string Type { get; set; }
9 |
10 | [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
11 | public object Value { get; set; }
12 | }
13 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Call.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Call
4 | {
5 | public string Name { get; set; }
6 | public Argument[] Arguments { get; set; }
7 | public string[] Documentations { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Const.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Const
4 | {
5 | public string Name { get; set; }
6 | public string Type { get; set; }
7 | public string Value { get; set; }
8 | public string[] Documentations { get; set; }
9 | }
10 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Error.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Error
4 | {
5 | public string[] Documentations { get; set; }
6 | public string Name { get; set; }
7 | }
8 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Event.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Event
4 | {
5 | public string[] Documentations { get; set; }
6 | public string Name { get; set; }
7 | public string[] EventArgs { get; set; }
8 | }
9 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Function.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Function
4 | {
5 | public Storage.Hasher Hasher { get; internal set; }
6 | public string Key1 { get; internal set; }
7 | public string Key2 { get; internal set; }
8 | public string Value { get; internal set; }
9 | public bool? IsLinked { get; internal set; }
10 | public Storage.Hasher Key2Hasher { get; internal set; }
11 | }
12 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Item.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Item
4 | {
5 | public string Name { get; set; }
6 | public Storage.Modifier Modifier { get; set; }
7 | public Storage.Type Type { get; set; }
8 | public Function Function { get; set; }
9 | public string FallBack { get; set; }
10 | public string[] Documentations { get; set; }
11 | }
12 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/MetaData.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using Newtonsoft.Json.Converters;
3 | using System;
4 |
5 | namespace SubstrateNetApi.Model.Meta
6 | {
7 | public class MetaData
8 | {
9 | public MetaData(string origin = "unknown")
10 | {
11 | Origin = origin;
12 | }
13 | public string Origin { get; set; }
14 | public string Magic { get; set; }
15 | public string Version { get; set; }
16 | public Module[] Modules { get; set; }
17 | public string[] ExtrinsicExtensions { get; set; }
18 |
19 | public bool TryGetModuleByName(string name, out Module result)
20 | {
21 | result = null;
22 | foreach (Module module in Modules)
23 | {
24 | if (module.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
25 | {
26 | result = module;
27 | return true;
28 | }
29 | }
30 |
31 | return false;
32 | }
33 |
34 | public string Serialize()
35 | {
36 | return JsonConvert.SerializeObject(this, new StringEnumConverter());
37 | }
38 |
39 |
40 | }
41 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Module.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Meta
4 | {
5 | public class Module
6 | {
7 | public string Name { get; set; }
8 |
9 | public byte Index { get; set; }
10 | public Storage Storage { get; set; }
11 | public Call[] Calls { get; set; }
12 | public Event[] Events { get; set; }
13 | public Const[] Consts { get; set; }
14 | public Error[] Errors { get; set; }
15 |
16 | public bool TryGetStorageItemByName(string name, out Item result)
17 | {
18 | result = null;
19 | if (Storage is null)
20 | {
21 | return false;
22 | }
23 |
24 | foreach (Item item in Storage.Items)
25 | {
26 | if (item.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
27 | {
28 | result = item;
29 | return true;
30 | }
31 | }
32 |
33 | return false;
34 | }
35 |
36 | internal bool TryGetCallByName(string name, out Call result)
37 | {
38 | result = null;
39 | foreach (Call call in Calls)
40 | {
41 | if (call.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase))
42 | {
43 | result = call;
44 | return true;
45 | }
46 | }
47 |
48 | return false;
49 | }
50 |
51 | public byte IndexOf(Call call)
52 | {
53 | return (byte)Array.IndexOf(Calls, call);
54 | }
55 | }
56 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Meta/Storage.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Meta
2 | {
3 | public class Storage
4 | {
5 | public enum Type
6 | {
7 | Plain, Map, DoubleMap
8 | }
9 |
10 | public enum Modifier
11 | {
12 | Optional,
13 | Default
14 | }
15 |
16 | public enum Hasher
17 | {
18 | None = -1,
19 | BlakeTwo128,
20 | BlakeTwo256,
21 | BlakeTwo128Concat,
22 | Twox128,
23 | Twox256,
24 | Twox64Concat,
25 | Identity
26 | }
27 |
28 | public string Prefix { get; set; }
29 | public Item[] Items { get; set; }
30 |
31 | }
32 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/Block.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Extrinsics;
2 |
3 | namespace SubstrateNetApi.Model.Rpc
4 | {
5 | public class Block
6 | {
7 | public Extrinsic[] Extrinsics { get; set; }
8 | public Header Header { get; set; }
9 |
10 | }
11 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/BlockData.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 |
3 | namespace SubstrateNetApi.Model.Rpc
4 | {
5 | public class BlockData
6 | {
7 |
8 | public BlockData(Block block, object justification)
9 | {
10 | Block = block;
11 | Justification = justification;
12 | }
13 |
14 | public Block Block { get; set; }
15 | public object Justification { get; set; }
16 |
17 | public override string ToString()
18 | {
19 | return JsonConvert.SerializeObject(this);
20 | }
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/Digest.cs:
--------------------------------------------------------------------------------
1 | using System.Collections.Generic;
2 |
3 | namespace SubstrateNetApi.Model.Rpc
4 | {
5 | public class Digest
6 | {
7 | public IList Logs { get; set; }
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/ExtrinsicStatus.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using SubstrateNetApi.Model.Types;
3 | using SubstrateNetApi.Model.Types.Base;
4 |
5 | namespace SubstrateNetApi.Model.Rpc
6 | {
7 | public enum ExtrinsicState
8 | {
9 | None, Future, Ready, Dropped, Invalid
10 | }
11 |
12 | public class ExtrinsicStatus
13 | {
14 | public ExtrinsicState ExtrinsicState { get; set; }
15 | public string[] Broadcast { get; set; }
16 | public Hash InBlock { get; set; }
17 | public Hash Retracted { get; set; }
18 | public Hash FinalityTimeout { get; set; }
19 | public Hash Finalized { get; set; }
20 | public Hash Usurped { get; set; }
21 |
22 | public override string ToString()
23 | {
24 | return JsonConvert.SerializeObject(this);
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/Header.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using SubstrateNetApi.Model.Types;
3 | using SubstrateNetApi.Model.Types.Base;
4 | using SubstrateNetApi.TypeConverters;
5 |
6 | namespace SubstrateNetApi.Model.Rpc
7 | {
8 | public class Header
9 | {
10 | public Digest Digest { get; set; }
11 |
12 | [JsonConverter(typeof(GenericTypeConverter))]
13 | public Hash ExtrinsicsRoot { get; set; }
14 |
15 | [JsonConverter(typeof(GenericTypeConverter))]
16 | public U64 Number { get; set; }
17 |
18 | [JsonConverter(typeof(GenericTypeConverter))]
19 | public Hash ParentHash { get; set; }
20 |
21 | [JsonConverter(typeof(GenericTypeConverter))]
22 | public Hash StateRoot { get; set; }
23 |
24 | public override string ToString()
25 | {
26 | return JsonConvert.SerializeObject(this);
27 | }
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/Health.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 |
3 | namespace SubstrateNetApi.Model.Rpc
4 | {
5 | public class Health
6 | {
7 | public bool IsSyncing { get; set; }
8 | public int Peers { get; set; }
9 | public bool ShouldHavePeers { get; set; }
10 | public override string ToString()
11 | {
12 | return JsonConvert.SerializeObject(this);
13 | }
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/RuntimeVersion.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Rpc
6 | {
7 | public class RuntimeVersion
8 | {
9 | public object[][] Apis { get; set; }
10 | public int AuthoringVersion { get; set; }
11 | public string ImplName { get; set; }
12 | public uint ImplVersion { get; set; }
13 | public string SpecName { get; set; }
14 | public uint SpecVersion { get; set; }
15 | public uint TransactionVersion { get; set; }
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Rpc/StorageChangeSet.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 | using SubstrateNetApi.Model.Types;
3 | using SubstrateNetApi.Model.Types.Base;
4 | using SubstrateNetApi.Model.Types.Struct;
5 | using SubstrateNetApi.TypeConverters;
6 |
7 | namespace SubstrateNetApi.Model.Rpc
8 | {
9 | public class StorageChangeSet
10 | {
11 | [JsonConverter(typeof(GenericTypeConverter))]
12 | public Hash Block { get; set; }
13 |
14 |
15 | public string[][] Changes { get; set; }
16 | //[JsonConverter(typeof(GenericTypeConverter>>))]
17 | //public Vec> Changes { get; set; }
18 |
19 | public override string ToString()
20 | {
21 | return JsonConvert.SerializeObject(this);
22 | }
23 | }
24 |
25 | ///
26 | /// TODO: This needs to be verified, if we can use it to convert straight
27 | ///
28 | /// pub struct StorageChangeSet {
29 | /// pub block: Hash,
30 | /// pub changes: Vec<(StorageKey, Option)>,
31 | /// }
32 | ///
33 | public class NewStorageChangeSet
34 | {
35 | [JsonConverter(typeof(GenericTypeConverter))]
36 | public Hash Block { get; set; }
37 |
38 | [JsonConverter(typeof(GenericTypeConverter>>))]
39 | public Vec> Changes { get; set; }
40 |
41 | public override string ToString()
42 | {
43 | return JsonConvert.SerializeObject(this);
44 | }
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Account.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Newtonsoft.Json;
3 | using SubstrateNetApi.Model.Types.Base;
4 |
5 | namespace SubstrateNetApi.Model.Types
6 | {
7 | public enum KeyType
8 | {
9 | Ed25519,
10 | Sr25519
11 | }
12 |
13 | public class Account : AccountId
14 | {
15 | public KeyType KeyType;
16 |
17 | [JsonIgnore]
18 | public byte KeyTypeByte
19 | {
20 | get
21 | {
22 | switch (KeyType)
23 | {
24 | case KeyType.Ed25519:
25 | return 0;
26 | case KeyType.Sr25519:
27 | return 1;
28 | default:
29 | throw new Exception($"Unknown key type found '{KeyType}'.");
30 | }
31 | }
32 | }
33 |
34 | [JsonIgnore] public byte[] PrivateKey { get; private set; }
35 |
36 | public void Create(KeyType keyType, byte[] privateKey, byte[] publicKey)
37 | {
38 | KeyType = keyType;
39 | PrivateKey = privateKey;
40 | base.Create(publicKey);
41 | }
42 |
43 | public void Create(KeyType keyType, byte[] publicKey)
44 | {
45 | Create(keyType, null, publicKey);
46 | }
47 |
48 | public static Account Build(KeyType keyType, byte[] privateKey, byte[] publicKey)
49 | {
50 | var account = new Account();
51 | account.Create(keyType, privateKey, publicKey);
52 | return account;
53 | }
54 | }
55 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/AccountId.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace SubstrateNetApi.Model.Types.Base
5 | {
6 | public class AccountId : BaseType
7 | {
8 | // TODO: ::Source -- RawAccountId is unprefixed Address
9 | public override string Name() => "T::AccountId";
10 |
11 | // TODO: might have to change this based on the address type.
12 | public override int Size() => 32;
13 |
14 | public override byte[] Encode()
15 | {
16 | var bytes = new List();
17 | switch (Constants.AddressVersion)
18 | {
19 | case 0:
20 | return Bytes;
21 | case 1:
22 | bytes.Add(0xFF);
23 | bytes.AddRange(Bytes);
24 | return bytes.ToArray();
25 | case 2:
26 | bytes.Add(0x00);
27 | bytes.AddRange(Bytes);
28 | return bytes.ToArray();
29 | default:
30 | throw new NotImplementedException("Unknown address version please refer to Constants.cs");
31 | }
32 | }
33 |
34 | public override void Create(byte[] byteArray)
35 | {
36 | Bytes = byteArray;
37 | Value = Utils.GetAddressFrom(byteArray);
38 | }
39 | }
40 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/Assets.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Types.Base
6 | {
7 | public class AssetId : U32
8 | {
9 | public override string Name() => "AssetId";
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/Balance.cs:
--------------------------------------------------------------------------------
1 | using System.Numerics;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class Balance : BaseType
6 | {
7 | public override string Name() => "T::Balance";
8 |
9 | public override int Size() => 16;
10 |
11 | public override byte[] Encode()
12 | {
13 | return new CompactInteger(Value).Encode();
14 | }
15 |
16 | public override void Create(byte[] byteArray)
17 | {
18 | Bytes = byteArray;
19 | Value = new BigInteger(byteArray);
20 | }
21 |
22 | public void Create(BigInteger value)
23 | {
24 | Bytes = value.ToByteArray();
25 | Value = value;
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/BlockNumber.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class BlockNumber : BaseType
6 | {
7 | public override string Name() => "T::BlockNumber";
8 |
9 | public override int Size() => 4;
10 |
11 | public override byte[] Encode()
12 | {
13 | var reversed = Bytes;
14 | Array.Reverse(reversed);
15 | return reversed;
16 | }
17 |
18 | public override void Create(byte[] byteArray)
19 | {
20 | Bytes = byteArray;
21 | Value = BitConverter.ToUInt32(byteArray, 0);
22 | }
23 |
24 | public void Create(uint value)
25 | {
26 | Bytes = BitConverter.GetBytes(value);
27 | Value = value;
28 | }
29 | }
30 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/Bool.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class Bool : BaseType
6 | {
7 | public override string Name() => "bool";
8 |
9 | public override int Size() => 1;
10 |
11 | public override byte[] Encode()
12 | {
13 | return Bytes;
14 | }
15 |
16 | public override void Create(byte[] byteArray)
17 | {
18 | Bytes = byteArray;
19 | Value = byteArray[0] > 0;
20 | }
21 |
22 | public void Create(bool value)
23 | {
24 | Bytes = new byte[] { (byte)(value ? 0x01 : 0x00) };
25 | Value = value;
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/Hash.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types.Base
2 | {
3 | public class Hash : BaseType
4 | {
5 | public override string Name() => "T::Hash";
6 |
7 | public override int Size() => 32;
8 |
9 | public override byte[] Encode()
10 | {
11 | return Bytes;
12 | }
13 |
14 | public override void Create(byte[] byteArray)
15 | {
16 | Bytes = byteArray;
17 | Value = Utils.Bytes2HexString(Bytes);
18 | }
19 | }
20 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/InheritedBaseTypes.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types.Base
2 | {
3 | public class AuthorityId : AccountId
4 | {
5 | public override string Name() => "AuthorityId";
6 | }
7 |
8 | public class AuthorityWeight : U64
9 | {
10 | public override string Name() => "AuthorityWeight";
11 | }
12 |
13 | public class Topic : Hash
14 | {
15 | public override string Name() => "Topic";
16 | }
17 |
18 | public class EraIndex : U32
19 | {
20 | public override string Name() => "EraIndex";
21 | }
22 |
23 | public class AccountIndex : U32
24 | {
25 | public override string Name() => "AccountIndex";
26 | }
27 |
28 | public class ApplyExtrinsic : U32
29 | {
30 | public override string Name() => "ApplyExtrinsic";
31 | }
32 |
33 | public class BountyIndex : U32
34 | {
35 | public override string Name() => "BountyIndex";
36 | }
37 |
38 | public class CallHash : Hash
39 | {
40 | public override string Name() => "CallHash";
41 | }
42 |
43 | public class MemberCount : U32
44 | {
45 | public override string Name() => "MemberCount";
46 | }
47 |
48 | public class PropIndex : U32
49 | {
50 | public override string Name() => "PropIndex";
51 | }
52 |
53 | public class SessionIndex : U32
54 | {
55 | public override string Name() => "SessionIndex";
56 | }
57 |
58 | public class ProposalIndex : U32
59 | {
60 | public override string Name() => "ProposalIndex";
61 | }
62 |
63 | public class ReferendumIndex : U32
64 | {
65 | public override string Name() => "ReferendumIndex";
66 | }
67 |
68 | public class RefCount : U32
69 | {
70 | public override string Name() => "RefCount";
71 | }
72 |
73 | public class RegistrarIndex : U32
74 | {
75 | public override string Name() => "RegistrarIndex";
76 | }
77 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/U128.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class U128 : BaseType
6 | {
7 | public override string Name() => "u128";
8 |
9 | public override int Size() => 16;
10 |
11 | public override byte[] Encode()
12 | {
13 | var reversed = Bytes;
14 | //Array.Reverse(reversed);
15 | return reversed;
16 | }
17 |
18 | public override void CreateFromJson(string str)
19 | {
20 | var bytes = Utils.HexToByteArray(str, true);
21 | Array.Reverse(bytes);
22 | Create(bytes);
23 | }
24 |
25 | public override void Create(byte[] byteArray)
26 | {
27 | if (byteArray.Length < Size())
28 | {
29 | var newByteArray = new byte[Size()];
30 | byteArray.CopyTo(newByteArray, 0);
31 | byteArray = newByteArray;
32 | }
33 |
34 | Bytes = byteArray;
35 | Value = BitConverter.ToUInt64(byteArray, 0);
36 | }
37 |
38 | public void Create(ulong value)
39 | {
40 | Bytes = BitConverter.GetBytes(value);
41 | Value = value;
42 | }
43 | }
44 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/U16.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class U16 : BaseType
6 | {
7 | public override string Name() => "u16";
8 |
9 | public override int Size() => 2;
10 |
11 | public override byte[] Encode()
12 | {
13 | var reversed = Bytes;
14 | //Array.Reverse(reversed);
15 | return reversed;
16 | }
17 |
18 | public override void CreateFromJson(string str)
19 | {
20 | var bytes = Utils.HexToByteArray(str, true);
21 | Array.Reverse(bytes);
22 | var result = new byte[Size()];
23 | bytes.CopyTo(result, 0);
24 | Create(result);
25 | }
26 |
27 | public override void Create(byte[] byteArray)
28 | {
29 | if (byteArray.Length < Size())
30 | {
31 | var newByteArray = new byte[Size()];
32 | byteArray.CopyTo(newByteArray, 0);
33 | byteArray = newByteArray;
34 | }
35 |
36 | Bytes = byteArray;
37 | Value = BitConverter.ToUInt16(byteArray, 0);
38 | }
39 |
40 | public void Create(ushort value)
41 | {
42 | Bytes = BitConverter.GetBytes(value);
43 | Value = value;
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/U32.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class U32 : BaseType
6 | {
7 | public override string Name() => "u32";
8 |
9 | public override int Size() => 4;
10 |
11 | public override byte[] Encode()
12 | {
13 | var reversed = Bytes;
14 | //Array.Reverse(reversed);
15 | return reversed;
16 | }
17 |
18 | public override void CreateFromJson(string str)
19 | {
20 | var bytes = Utils.HexToByteArray(str, true);
21 | Array.Reverse(bytes);
22 | var result = new byte[Size()];
23 | bytes.CopyTo(result, 0);
24 | Create(result);
25 | }
26 |
27 | public override void Create(byte[] byteArray)
28 | {
29 | if (byteArray.Length < Size())
30 | {
31 | var newByteArray = new byte[Size()];
32 | byteArray.CopyTo(newByteArray, 0);
33 | byteArray = newByteArray;
34 | }
35 |
36 | Bytes = byteArray;
37 | Value = BitConverter.ToUInt32(byteArray, 0);
38 | }
39 |
40 | public void Create(uint value)
41 | {
42 | Bytes = BitConverter.GetBytes(value);
43 | Value = value;
44 | }
45 | }
46 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/U64.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class U64 : BaseType
6 | {
7 | public override string Name() => "u64";
8 |
9 | public override int Size() => 8;
10 |
11 | public override byte[] Encode()
12 | {
13 | var reversed = Bytes;
14 | //Array.Reverse(reversed);
15 | return reversed;
16 | }
17 |
18 | public override void CreateFromJson(string str)
19 | {
20 | var bytes = Utils.HexToByteArray(str, true);
21 | Array.Reverse(bytes);
22 | Create(bytes);
23 | }
24 |
25 | public override void Create(byte[] byteArray)
26 | {
27 | if (byteArray.Length < Size())
28 | {
29 | var newByteArray = new byte[Size()];
30 | byteArray.CopyTo(newByteArray, 0);
31 | byteArray = newByteArray;
32 | }
33 |
34 | Bytes = byteArray;
35 | Value = BitConverter.ToUInt64(byteArray, 0);
36 | }
37 |
38 | public void Create(ulong value)
39 | {
40 | Bytes = BitConverter.GetBytes(value);
41 | Value = value;
42 | }
43 | }
44 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Base/U8.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types.Base
4 | {
5 | public class U8 : BaseType
6 | {
7 | public override string Name() => "u8";
8 |
9 | public override int Size() => 1;
10 |
11 | public override byte[] Encode()
12 | {
13 | return Bytes;
14 | }
15 |
16 | public override void Create(byte[] byteArray)
17 | {
18 | Bytes = byteArray;
19 | Value = byteArray[0];
20 | }
21 |
22 | public void Create(byte value)
23 | {
24 | Bytes = new byte[] { value };
25 | Value = value;
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/BaseType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Newtonsoft.Json;
3 |
4 | namespace SubstrateNetApi.Model.Types
5 | {
6 | public abstract class BaseType : IType
7 | {
8 | public abstract string Name();
9 | public abstract int Size();
10 |
11 | [JsonIgnore]
12 | public byte[] Bytes { get; internal set; }
13 |
14 | public abstract byte[] Encode();
15 |
16 | public void Decode(byte[] byteArray, ref int p)
17 | {
18 | var memory = byteArray.AsMemory();
19 | var result = memory.Span.Slice(p, Size()).ToArray();
20 | p += Size();
21 | Create(result);
22 | }
23 |
24 | public virtual void Create(string str) => Create(Utils.HexToByteArray(str));
25 |
26 | public virtual void CreateFromJson(string str) => Create(Utils.HexToByteArray(str));
27 |
28 | public abstract void Create(byte[] byteArray);
29 |
30 | public IType New() => this;
31 |
32 | public override string ToString() => JsonConvert.SerializeObject(Value);
33 |
34 | public T Value { get; internal set; }
35 |
36 | }
37 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/BalanceStatus.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Types.Enum
6 | {
7 | public enum BalanceStatus
8 | {
9 | Free, Reserved
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/DispatchClass.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types.Enum
2 | {
3 | public enum DispatchClass
4 | {
5 | Normal,
6 | Operational,
7 | Mandatory
8 | }
9 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/ElectionCompute.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Types.Enum
6 | {
7 | public enum ElectionCompute
8 | {
9 | OnChain, Signed, Unsigned
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/Pays.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types.Enum
2 | {
3 | public enum Pays
4 | {
5 | Yes,
6 | No
7 | }
8 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/PhaseState.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types.Enum
2 | {
3 | public enum PhaseState
4 | {
5 | None,
6 | Finalization,
7 | Initialization
8 | }
9 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/ProxyType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Types.Enum
6 | {
7 | public enum ProxyType
8 | {
9 | Any, NonTransfer, Governance, Staking
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Enum/VoteThreshold.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Types.Enum
6 | {
7 | public enum VoteThreshold
8 | {
9 | SuperMajorityApprove, SuperMajorityAgainst, SimpleMajority
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/EnumType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Newtonsoft.Json;
3 | using Newtonsoft.Json.Converters;
4 |
5 | namespace SubstrateNetApi.Model.Types
6 | {
7 | public class EnumType : IType where T : System.Enum
8 | {
9 | public string Name() => typeof(T).Name;
10 |
11 | public int Size() => 1;
12 |
13 | [JsonIgnore]
14 | public byte[] Bytes { get; internal set; }
15 |
16 | public byte[] Encode()
17 | {
18 | return Bytes;
19 | }
20 |
21 | public void Decode(byte[] byteArray, ref int p)
22 | {
23 | var memory = byteArray.AsMemory();
24 | var result = memory.Span.Slice(p, Size()).ToArray();
25 | p += Size();
26 | Create(result);
27 | }
28 |
29 | public virtual void Create(string str) => Create(Utils.HexToByteArray(str));
30 |
31 | public virtual void CreateFromJson(string str) => Create(Utils.HexToByteArray(str));
32 |
33 | public void Create(T t)
34 | {
35 | //var byteArray = BitConverter.GetBytes(Convert.ToInt32(t));
36 | //if (byteArray.Length < Size())
37 | //{
38 | // var newByteArray = new byte[Size()];
39 | // byteArray.CopyTo(newByteArray, 0);
40 | // byteArray = newByteArray;
41 | //}
42 | //Bytes = byteArray;
43 | Bytes = BitConverter.GetBytes(Convert.ToInt32(t));
44 | Value = t;
45 | }
46 |
47 | public void Create(byte[] byteArray)
48 | {
49 | Bytes = byteArray;
50 | Value = (T) System.Enum.Parse(typeof(T), byteArray[0].ToString(), true);
51 |
52 | //if (byteArray.Length < Size())
53 | //{
54 | // var newByteArray = new byte[Size()];
55 | // byteArray.CopyTo(newByteArray, 0);
56 | // byteArray = newByteArray;
57 | //}
58 |
59 | //Bytes = byteArray;
60 | //Value = (T)System.Enum.Parse(typeof(T), BitConverter.ToUInt32(byteArray, 0).ToString(), true);
61 | }
62 |
63 | public IType New() => this;
64 |
65 | public override string ToString() => JsonConvert.SerializeObject(Value);
66 |
67 | [JsonConverter(typeof(StringEnumConverter))]
68 | public T Value { get; internal set; }
69 |
70 | }
71 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/ExtEnumType.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using Newtonsoft.Json;
3 | using Newtonsoft.Json.Converters;
4 |
5 | namespace SubstrateNetApi.Model.Types
6 | {
7 | public class NullType : IType
8 | {
9 | public string Name() => "NullType";
10 | public int Size() => 0;
11 |
12 | public void Create(string str)
13 | {
14 | throw new NotImplementedException();
15 | }
16 |
17 | public void Create(byte[] byteArray)
18 | {
19 | throw new NotImplementedException();
20 | }
21 |
22 | public void CreateFromJson(string str)
23 | {
24 | throw new NotImplementedException();
25 | }
26 |
27 | public void Decode(byte[] byteArray, ref int p)
28 | {
29 | throw new NotImplementedException();
30 | }
31 |
32 | public byte[] Encode()
33 | {
34 | throw new NotImplementedException();
35 | }
36 |
37 | public IType New()
38 | {
39 | throw new NotImplementedException();
40 | }
41 | }
42 |
43 | public class ExtEnumType : IType
44 | where T0 : System.Enum
45 | where T1 : IType, new()
46 | where T2 : IType, new()
47 | where T3 : IType, new()
48 | where T4 : IType, new()
49 | where T5 : IType, new()
50 | where T6 : IType, new()
51 | where T7 : IType, new()
52 | where T8 : IType, new()
53 | where T9 : IType, new()
54 | {
55 | public virtual string Name() => typeof(T0).Name;
56 |
57 | private int _size;
58 | public int Size() => _size;
59 |
60 | [JsonIgnore]
61 | public byte[] Bytes { get; internal set; }
62 |
63 | public byte[] Encode()
64 | {
65 | return Bytes;
66 | }
67 |
68 | public void Decode(byte[] byteArray, ref int p)
69 | {
70 | var start = p;
71 | var enumByte = byteArray[p];
72 |
73 | Value = (T0)System.Enum.Parse(typeof(T0), enumByte.ToString(), true);
74 | p += 1;
75 |
76 | Value2 = DecodeOneOf(enumByte, byteArray, ref p);
77 |
78 | _size = p - start;
79 | }
80 |
81 | private IType DecodeOneOf(byte value, byte[] byteArray, ref int p)
82 | {
83 | IType result;
84 | switch (value)
85 | {
86 | case 0x00:
87 | result = new T1();
88 | if (result.GetType().Name == "NullType")
89 | return null;
90 | result.Decode(byteArray, ref p);
91 | return result;
92 | case 0x01:
93 | result = new T2();
94 | if (result.GetType().Name == "NullType")
95 | return null;
96 | result.Decode(byteArray, ref p);
97 | return result;
98 | case 0x02:
99 | result = new T3();
100 | if (result.GetType().Name == "NullType")
101 | return null;
102 | result.Decode(byteArray, ref p);
103 | return result;
104 | case 0x03:
105 | result = new T4();
106 | if (result.GetType().Name == "NullType")
107 | return null;
108 | result.Decode(byteArray, ref p);
109 | return result;
110 | case 0x04:
111 | result = new T5();
112 | if (result.GetType().Name == "NullType")
113 | return null;
114 | result.Decode(byteArray, ref p);
115 | return result;
116 | case 0x05:
117 | result = new T6();
118 | if (result.GetType().Name == "NullType")
119 | return null;
120 | result.Decode(byteArray, ref p);
121 | return result;
122 | case 0x06:
123 | result = new T7();
124 | if (result.GetType().Name == "NullType")
125 | return null;
126 | result.Decode(byteArray, ref p);
127 | return result;
128 | case 0x07:
129 | result = new T8();
130 | if (result.GetType().Name == "NullType")
131 | return null;
132 | result.Decode(byteArray, ref p);
133 | return result;
134 | case 0x08:
135 | result = new T9();
136 | if (result.GetType().Name == "NullType")
137 | return null;
138 | result.Decode(byteArray, ref p);
139 | return result;
140 | default:
141 | return null;
142 | }
143 | }
144 |
145 | public virtual void Create(string str) => Create(Utils.HexToByteArray(str));
146 |
147 | public virtual void CreateFromJson(string str) => Create(Utils.HexToByteArray(str));
148 |
149 | public void Create(T0 t, IType value2)
150 | {
151 | Bytes = BitConverter.GetBytes(Convert.ToInt32(t));
152 | Value = t;
153 | Value2 = value2;
154 | }
155 |
156 | public void Create(byte[] byteArray)
157 | {
158 | int p = 0;
159 | Decode(byteArray, ref p);
160 | }
161 |
162 | public IType New() => this;
163 |
164 | public override string ToString() => JsonConvert.SerializeObject(Value);
165 |
166 | [JsonConverter(typeof(StringEnumConverter))]
167 | public T0 Value { get; internal set; }
168 |
169 | public IType Value2 { get; internal set; }
170 |
171 | }
172 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/IEncodable.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types
2 | {
3 | public interface IEncodable
4 | {
5 | byte[] Encode();
6 | }
7 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/IType.cs:
--------------------------------------------------------------------------------
1 | namespace SubstrateNetApi.Model.Types
2 | {
3 | ///
4 | /// Reference to the polkadot js types implementation
5 | /// https://github.com/polkadot-js/api/tree/master/packages/types/src
6 | ///
7 | public interface IType
8 | {
9 | ///
10 | /// Names this instance.
11 | ///
12 | ///
13 | string Name();
14 |
15 | ///
16 | /// Sizes this instance.
17 | ///
18 | ///
19 | int Size();
20 |
21 | ///
22 | /// Encodes this instance.
23 | ///
24 | ///
25 | byte[] Encode();
26 |
27 | ///
28 | /// Decodes the specified byte array.
29 | ///
30 | /// The byte array.
31 | /// The p.
32 | void Decode(byte[] byteArray, ref int p);
33 |
34 | ///
35 | /// Creates the specified string.
36 | ///
37 | /// The string.
38 | void Create(string str);
39 |
40 | ///
41 | /// Creates from json.
42 | ///
43 | /// The string.
44 | void CreateFromJson(string str);
45 |
46 | ///
47 | /// Creates the specified byte array.
48 | ///
49 | /// The byte array.
50 | void Create(byte[] byteArray);
51 |
52 | ///
53 | /// News this instance.
54 | ///
55 | ///
56 | IType New();
57 | }
58 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/AccountData.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SubstrateNetApi.Model.Types.Base;
3 |
4 | namespace SubstrateNetApi.Model.Types.Struct
5 | {
6 | public class AccountData : StructType
7 | {
8 | public override string Name() => "AccountData";
9 |
10 | private int _size;
11 | public override int Size() => _size;
12 |
13 |
14 | public override byte[] Encode()
15 | {
16 | throw new NotImplementedException();
17 | }
18 |
19 | public override void Decode(byte[] byteArray, ref int p)
20 | {
21 | var start = p;
22 |
23 | Free = new Balance();
24 | Free.Decode(byteArray, ref p);
25 |
26 | Reserved = new Balance();
27 | Reserved.Decode(byteArray, ref p);
28 |
29 | MiscFrozen = new Balance();
30 | MiscFrozen.Decode(byteArray, ref p);
31 |
32 | FeeFrozen = new Balance();
33 | FeeFrozen.Decode(byteArray, ref p);
34 |
35 | _size = p - start;
36 | }
37 |
38 | public Balance Free { get; private set; }
39 | public Balance Reserved { get; private set; }
40 | public Balance MiscFrozen { get; private set; }
41 | public Balance FeeFrozen { get; private set; }
42 | }
43 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/AccountInfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SubstrateNetApi.Model.Types.Base;
3 |
4 | namespace SubstrateNetApi.Model.Types.Struct
5 | {
6 | public class AccountInfo : StructType
7 | {
8 | public override string Name() => "AccountInfo";
9 |
10 | private int _size;
11 | public override int Size() => _size;
12 |
13 | public override byte[] Encode()
14 | {
15 | throw new NotImplementedException();
16 | }
17 |
18 | public override void Decode(byte[] byteArray, ref int p)
19 | {
20 | var start = p;
21 |
22 | Nonce = new U32();
23 | Nonce.Decode(byteArray, ref p);
24 |
25 | Consumers = new RefCount();
26 | Consumers.Decode(byteArray, ref p);
27 |
28 | Providers = new RefCount();
29 | Providers.Decode(byteArray, ref p);
30 |
31 | AccountData = new AccountData();
32 | AccountData.Decode(byteArray, ref p);
33 |
34 | _size = p - start;
35 | }
36 |
37 | public U32 Nonce { get; private set; }
38 | public RefCount Consumers { get; private set; }
39 | public RefCount Providers { get; private set; }
40 | public AccountData AccountData { get; private set; }
41 | }
42 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/Assets.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 |
5 | namespace SubstrateNetApi.Model.Types.Base
6 | {
7 | public class AssetBalance : StructType
8 | {
9 | public override string Name() => "AssetBalance";
10 |
11 | private int _size;
12 | public override int Size() => _size;
13 |
14 | public override byte[] Encode()
15 | {
16 | throw new NotImplementedException();
17 | }
18 |
19 | public override void Decode(byte[] byteArray, ref int p)
20 | {
21 | var start = p;
22 |
23 | Balance = new U32();
24 | Balance.Decode(byteArray, ref p);
25 |
26 | IsFrozen = new Bool();
27 | IsFrozen.Decode(byteArray, ref p);
28 |
29 | Sufficient = new Bool();
30 | Sufficient.Decode(byteArray, ref p);
31 |
32 | //Extra = new Extra();
33 | //Extra.Decode(byteArray, ref p);
34 |
35 | _size = p - start;
36 | }
37 |
38 | public U32 Balance { get; private set; }
39 | public Bool IsFrozen { get; private set; }
40 | public Bool Sufficient { get; private set; }
41 | //public Extra Extra { get; private set; }
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/BaseEvent.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using Newtonsoft.Json;
4 | using SubstrateNetApi.Model.Meta;
5 | using SubstrateNetApi.Model.Types.Base;
6 | using SubstrateNetApi.Model.Types.Enum;
7 |
8 | namespace SubstrateNetApi.Model.Types.Struct
9 | {
10 | public partial class BaseEvent : StructType
11 | {
12 | public override string Name() => "Event";
13 |
14 | private int _size;
15 | public override int Size() => _size;
16 |
17 | private readonly MetaData _metaData;
18 | public BaseEvent() { }
19 |
20 | public BaseEvent(MetaData metaData)
21 | {
22 | _metaData = metaData;
23 | }
24 |
25 | public override byte[] Encode()
26 | {
27 | throw new NotImplementedException();
28 | }
29 |
30 |
31 |
32 | public override void Decode(byte[] byteArray, ref int p)
33 | {
34 | if (_metaData is null)
35 | {
36 | throw new NotImplementedException("Need MetaData in ctor to decode.");
37 | }
38 |
39 | ModuleIndex = new U8();
40 | ModuleIndex.Decode(byteArray, ref p);
41 |
42 | var module = _metaData.Modules[ModuleIndex.Value];
43 | ModuleName = module.Name;
44 |
45 | EventIndex = new U8();
46 | EventIndex.Decode(byteArray, ref p);
47 |
48 | var moduleEvent = module.Events[EventIndex.Value];
49 | EventName = moduleEvent.Name;
50 |
51 | EventArgs = new IType[moduleEvent.EventArgs.Length];
52 | for (var i = 0; i < moduleEvent.EventArgs.Length; i++)
53 | {
54 | var eventArgStr = moduleEvent.EventArgs[i];
55 | EventArgs[i] = TypeUtil.Mapper(eventArgStr, byteArray, ref p);
56 | }
57 | }
58 |
59 | [JsonIgnore]
60 | public U8 ModuleIndex;
61 | public string ModuleName;
62 | [JsonIgnore]
63 | public U8 EventIndex;
64 | public string EventName;
65 | public IType[] EventArgs;
66 | }
67 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/Dispatchinfo.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SubstrateNetApi.Model.Types.Base;
3 | using SubstrateNetApi.Model.Types.Enum;
4 |
5 | namespace SubstrateNetApi.Model.Types.Struct
6 | {
7 | public class DispatchInfo : StructType
8 | {
9 | public override string Name() => "DispatchInfo";
10 |
11 | private int _size;
12 | public override int Size() => _size;
13 |
14 | public override byte[] Encode()
15 | {
16 | throw new NotImplementedException();
17 | }
18 |
19 | public override void Decode(byte[] byteArray, ref int p)
20 | {
21 | var start = p;
22 |
23 | Weight = new U64();
24 | Weight.Decode(byteArray, ref p);
25 |
26 |
27 | DispatchClass = new EnumType();
28 | DispatchClass.Decode(byteArray, ref p);
29 |
30 | Pays = new EnumType();
31 | Pays.Decode(byteArray, ref p);
32 |
33 | _size = p - start;
34 | }
35 |
36 | public U64 Weight { get; set; }
37 | public EnumType DispatchClass { get; set; }
38 | public EnumType Pays { get; set; }
39 | }
40 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/EventRecord.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Text;
3 | using Newtonsoft.Json;
4 | using SubstrateNetApi.Model.Meta;
5 | using SubstrateNetApi.Model.Types.Base;
6 |
7 | namespace SubstrateNetApi.Model.Types.Struct
8 | {
9 | public class EventRecord : StructType
10 | {
11 | public override string Name() => "EventRecord";
12 |
13 | private int _size;
14 | public override int Size() => _size;
15 |
16 | private MetaData _metaData;
17 |
18 | public EventRecord()
19 | {
20 | }
21 |
22 | public EventRecord(MetaData metaData)
23 | {
24 | _metaData = metaData;
25 | }
26 |
27 | public override byte[] Encode()
28 | {
29 | throw new NotImplementedException();
30 | }
31 |
32 | public override void Decode(byte[] byteArray, ref int p)
33 | {
34 | if (_metaData is null)
35 | {
36 | throw new NotImplementedException("Need MetaData in ctor to decode.");
37 | }
38 |
39 | var start = p;
40 |
41 | Phase = new Phase();
42 | Phase.Decode(byteArray, ref p);
43 |
44 | BaseEvent = new BaseEvent(_metaData);
45 | BaseEvent.Decode(byteArray, ref p);
46 |
47 | Topics = new Vec();
48 | Topics.Decode(byteArray, ref p);
49 |
50 | _size = p - start;
51 | }
52 |
53 | public Phase Phase;
54 | public BaseEvent BaseEvent;
55 | public Vec Topics;
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/EventRecords.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using SubstrateNetApi.Model.Meta;
4 |
5 | namespace SubstrateNetApi.Model.Types.Struct
6 | {
7 | public class EventRecords : StructType
8 | {
9 | public override string Name() => $"Vec>";
10 |
11 | private int _size;
12 | public override int Size() => _size;
13 |
14 | private MetaData _metaData;
15 |
16 | public EventRecords()
17 | {
18 | }
19 |
20 | public EventRecords(MetaData metaData)
21 | {
22 | _metaData = metaData;
23 | }
24 |
25 | public override byte[] Encode()
26 | {
27 | throw new NotImplementedException();
28 | }
29 |
30 | public override void Decode(byte[] byteArray, ref int p)
31 | {
32 | if (_metaData is null)
33 | {
34 | throw new NotImplementedException("Need MetaData in ctor to decode.");
35 | }
36 |
37 | var start = p;
38 |
39 | var list = new List();
40 |
41 | var length = CompactInteger.Decode(byteArray, ref p);
42 | for (var i = 0; i < length; i++)
43 | {
44 | var t = new EventRecord(_metaData);
45 | t.Decode(byteArray, ref p);
46 | list.Add(t);
47 | }
48 |
49 | Bytes = byteArray;
50 | Value = list;
51 |
52 | _size = p - start;
53 | }
54 |
55 | public List Value { get; internal set; }
56 | }
57 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/InheritedStructTypes.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using SubstrateNetApi.Model.Types.Base;
5 |
6 | namespace SubstrateNetApi.Model.Types.Struct
7 | {
8 | public class AuthorityList : Vec>
9 | {
10 | public override string Name() => "AuthorityList";
11 | }
12 |
13 | public class StorageKey : Vec
14 | {
15 | public override string Name() => "StorageKey";
16 | }
17 |
18 | public class StorageData : Vec
19 | {
20 | public override string Name() => "StorageData";
21 | }
22 |
23 | public class OpaqueTimeSlot : Vec
24 | {
25 | public override string Name() => "OpaqueTimeSlot";
26 | }
27 |
28 | public class TaskAddress : RustTuple
29 | {
30 | public override string Name() => "TaskAddress";
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/Option.cs:
--------------------------------------------------------------------------------
1 | using SubstrateNetApi.Model.Types.Base;
2 | using System;
3 | using System.Collections.Generic;
4 | using System.Linq;
5 |
6 | namespace SubstrateNetApi.Model.Types.Struct
7 | {
8 | public class Option : StructType where T : IType, new()
9 | {
10 | public override string Name() => $"Option<{new T().Name()}>";
11 |
12 | private int _size;
13 | public override int Size() => _size;
14 |
15 | public bool OptionFlag { get; set; }
16 |
17 | public override byte[] Encode()
18 | {
19 | var bytes = new List();
20 | if (OptionFlag)
21 | {
22 | bytes.Add(1);
23 | bytes.AddRange(Value.Encode());
24 |
25 | } else
26 | {
27 | bytes.Add(0);
28 | }
29 |
30 | return bytes.ToArray();
31 | }
32 |
33 | public override void Decode(byte[] byteArray, ref int p)
34 | {
35 | var start = p;
36 |
37 | var optionByte = new U8();
38 | optionByte.Decode(byteArray, ref p);
39 |
40 | OptionFlag = optionByte.Value > 0;
41 |
42 | T t = default;
43 | if (optionByte.Value > 0)
44 | {
45 | t = new T();
46 | t.Decode(byteArray, ref p);
47 | }
48 |
49 | _size = p - start;
50 |
51 | var bytes = new byte[_size];
52 | Array.Copy(byteArray, start, bytes, 0, _size);
53 |
54 | Bytes = bytes;
55 | Value = t != null ? t : default;
56 | }
57 |
58 | public override void CreateFromJson(string str)
59 | {
60 | Create(Utils.HexToByteArray(str));
61 | }
62 |
63 | public T Value { get; internal set; }
64 |
65 | public void Create(T value)
66 | {
67 | OptionFlag = value != null;
68 | Value = value;
69 | Bytes = Encode();
70 | }
71 |
72 | }
73 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/Phase.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using SubstrateNetApi.Model.Types.Base;
3 | using SubstrateNetApi.Model.Types.Enum;
4 |
5 | namespace SubstrateNetApi.Model.Types.Struct
6 | {
7 | public class Phase : StructType
8 | {
9 | public override string Name() => "Phase";
10 |
11 | private int _size;
12 | public override int Size() => _size;
13 |
14 | public override byte[] Encode()
15 | {
16 | throw new NotImplementedException();
17 | }
18 |
19 | public override void Decode(byte[] byteArray, ref int p)
20 | {
21 | var start = p;
22 |
23 | PhaseState = new EnumType();
24 | PhaseState.Decode(byteArray, ref p);
25 |
26 | if (PhaseState.Value == Enum.PhaseState.None)
27 | {
28 | ApplyExtrinsic = new ApplyExtrinsic();
29 | ApplyExtrinsic.Decode(byteArray, ref p);
30 | }
31 |
32 | _size = p - start;
33 | }
34 |
35 | public ApplyExtrinsic ApplyExtrinsic { get; set; }
36 | public EnumType PhaseState { get; set; }
37 | }
38 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/RustTuple.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 |
4 | namespace SubstrateNetApi.Model.Types.Struct
5 | {
6 | public class RustTuple : StructType where T1 : IType, new()
7 | where T2 : IType, new()
8 | {
9 | public override string Name() => $"({new T1().Name()},{new T2().Name()})";
10 |
11 | private int _size;
12 | public override int Size() => _size;
13 |
14 | public override byte[] Encode()
15 | {
16 | throw new NotImplementedException();
17 | }
18 |
19 | public override void Decode(byte[] byteArray, ref int p)
20 | {
21 | var start = p;
22 |
23 | Value = new IType[2];
24 |
25 | var t1 = new T1();
26 | t1.Decode(byteArray, ref p);
27 | Value[0] = t1;
28 |
29 | var t2 = new T2();
30 | t2.Decode(byteArray, ref p);
31 | Value[1] = t2;
32 |
33 | _size = p - start;
34 |
35 | Bytes = new byte[_size];
36 | Array.Copy(byteArray, start, Bytes, 0, _size);
37 | }
38 |
39 | public IType[] Value { get; internal set; }
40 | }
41 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/Timepoint.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Text;
4 | using SubstrateNetApi.Model.Types.Base;
5 |
6 | namespace SubstrateNetApi.Model.Types.Struct
7 | {
8 | public class Timepoint : StructType
9 | {
10 | public override string Name() => "Timepoint";
11 |
12 | private int _size;
13 | public override int Size() => _size;
14 |
15 | public override byte[] Encode()
16 | {
17 | throw new NotImplementedException();
18 | }
19 |
20 | public override void Decode(byte[] byteArray, ref int p)
21 | {
22 | var start = p;
23 |
24 | Height = new BlockNumber();
25 | Height.Decode(byteArray, ref p);
26 |
27 | Index = new U32();
28 | Index.Decode(byteArray, ref p);
29 |
30 | _size = p - start;
31 | }
32 |
33 | public BlockNumber Height { get; private set; }
34 | public U32 Index { get; private set; }
35 | }
36 | }
37 |
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/Struct/Vec.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Linq;
4 |
5 | namespace SubstrateNetApi.Model.Types.Struct
6 | {
7 | public class Vec : StructType where T : IType, new()
8 | {
9 | public override string Name() => $"Vec<{new T().Name()}>";
10 |
11 | private int _size;
12 | public override int Size() => _size;
13 |
14 | public override byte[] Encode()
15 | {
16 | List result = new List();
17 | for(int i = 0; i < Value.Count; i++)
18 | {
19 | result.AddRange(Value[i].Encode());
20 | }
21 | return Utils.SizePrefixedByteArray(result);
22 | }
23 |
24 | public override void Decode(byte[] byteArray, ref int p)
25 | {
26 | var start = p;
27 |
28 | var list = new List();
29 |
30 | var length = CompactInteger.Decode(byteArray, ref p);
31 | for (var i = 0; i < length; i++)
32 | {
33 | var t = new T();
34 | t.Decode(byteArray, ref p);
35 | list.Add(t);
36 | }
37 |
38 | _size = p - start;
39 |
40 | var bytes = new byte[_size];
41 | Array.Copy(byteArray, start, bytes, 0, _size);
42 |
43 | Bytes = bytes;
44 | Value = list;
45 | }
46 |
47 | public override void CreateFromJson(string str)
48 | {
49 | Create(Utils.HexToByteArray(str));
50 | }
51 |
52 | public List Value { get; internal set; }
53 |
54 | public void Create(List list)
55 | {
56 | Value = list;
57 | Bytes = Encode();
58 | }
59 |
60 | }
61 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/StructType.cs:
--------------------------------------------------------------------------------
1 | using Newtonsoft.Json;
2 |
3 | namespace SubstrateNetApi.Model.Types
4 | {
5 | public abstract class StructType : IType
6 | {
7 | public abstract string Name();
8 |
9 | public abstract int Size();
10 |
11 | [JsonIgnore]
12 | public byte[] Bytes { get; internal set; }
13 |
14 | public abstract byte[] Encode();
15 |
16 | public abstract void Decode(byte[] byteArray, ref int p);
17 |
18 | public virtual void Create(string str) => Create(Utils.HexToByteArray(str));
19 |
20 | public virtual void CreateFromJson(string str) => Create(Utils.HexToByteArray(str));
21 |
22 | public void Create(byte[] byteArray)
23 | {
24 | var p = 0;
25 | Decode(byteArray, ref p);
26 | }
27 |
28 |
29 | public IType New() => this;
30 |
31 | public override string ToString() => JsonConvert.SerializeObject(this);
32 | }
33 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Model/Types/UnimplementedTypes.cs:
--------------------------------------------------------------------------------
1 | using System;
2 |
3 | namespace SubstrateNetApi.Model.Types
4 | {
5 | public class DispatchError
6 | {
7 | public static DispatchError Decode(Memory byteArray, ref int p)
8 | {
9 | throw new NotImplementedException("DispatchError");
10 | }
11 | }
12 |
13 | public class DispatchResult
14 | {
15 | public static DispatchResult Decode(Memory byteArray, ref int p)
16 | {
17 | throw new NotImplementedException("DispatchResult");
18 | }
19 | }
20 |
21 | public class Kind
22 | {
23 | public static Kind Decode(Memory byteArray, ref int p)
24 | {
25 | throw new NotImplementedException("Kind");
26 | }
27 | }
28 |
29 | public class Moment
30 | {
31 | public byte[] Encode()
32 | {
33 | throw new NotImplementedException("Moment");
34 | }
35 | }
36 |
37 | }
--------------------------------------------------------------------------------
/SubstrateNetApi/Modules/State.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Threading;
3 | using System.Threading.Tasks;
4 | using Newtonsoft.Json.Linq;
5 | using SubstrateNetApi.Model.Rpc;
6 |
7 | namespace SubstrateNetApi.Modules
8 | {
9 | /// A state.
10 | /// 19.09.2020.
11 | public class State
12 | {
13 | /// The client.
14 | private readonly SubstrateClient _client;
15 |
16 | /// Constructor.
17 | /// 19.09.2020.
18 | /// The client.
19 | internal State(SubstrateClient client)
20 | {
21 | _client = client;
22 | }
23 |
24 | public async Task GetMetaDataAsync()
25 | {
26 | return await GetMetaDataAsync(CancellationToken.None);
27 | }
28 |
29 | public async Task GetMetaDataAsync(CancellationToken token)
30 | {
31 | return await _client.InvokeAsync("state_getMetadata", null, token);
32 | }
33 |
34 | public async Task GetPairsAsync(byte[] keyPrefix)
35 | {
36 | return await GetPairsAsync(keyPrefix, CancellationToken.None);
37 | }
38 |
39 | public async Task GetPairsAsync(byte[] keyPrefix, CancellationToken token)
40 | {
41 | return await _client.InvokeAsync("state_getPairs", new object[] {Utils.Bytes2HexString(keyPrefix)}, token);
42 | }
43 |
44 | public async Task GetKeysPagedAsync(byte[] keyPrefix, uint pageCount, byte[] startKey)
45 | {
46 | return await GetKeysPagedAsync(keyPrefix, pageCount, startKey, CancellationToken.None);
47 | }
48 |
49 | public async Task GetKeysPagedAsync(byte[] keyPrefix, uint pageCount, byte[] startKey,
50 | CancellationToken token)
51 | {
52 | return startKey.Length == 0
53 | ? await _client.InvokeAsync("state_getKeysPaged",
54 | new object[] {Utils.Bytes2HexString(keyPrefix), pageCount}, token)
55 | : await _client.InvokeAsync("state_getKeysPaged",
56 | new object[] {Utils.Bytes2HexString(keyPrefix), pageCount, Utils.Bytes2HexString(startKey)}, token);
57 | }
58 |
59 | public async Task