CardanoSharp.Wallet
CardanoSharp Wallet is a .NET library for Creating/Managing Wallets and Building/Signing Transactions.
#
Features- Generate Mnemonics
- Create Private and Public Keys
- Create Addresses
- Build Transactions
- Sign Transactions
#
Getting StartedCardanoSharp.Wallet is installed from NuGet.
Install-Package CardanoSharp.Wallet
#
Generate a Mnemonicusing CardanoSharp.Wallet;using CardanoSharp.Wallet.Enums;using CardanoSharp.Wallet.Models.Keys;
class Program{ static void Main(string[] args) { // The KeyServices exposes functions for Mnemonics and Deriving Keys var keyService = new KeyService(); // The AddressService allows us to create Addresses from our Public Keys var addressService = new AddressService(); // 24 represents the number of words we want for our Mnemonic int size = 24;
// This will generate a 24 English Mnemonic Mnemonic mnemonic = keyService.Generate(size, WordLists.English); System.Console.WriteLine(mnemonic.Words); }}
#
Create Private and Public KeysAdd powerful extensions to create and derive keys.
using CardanoSharp.Wallet.Extensions.Models;
// The masterKey is a PrivateKey made of up of the // - byte[] Key// - byte[] ChaincodePrivateKey masterKey = mnemonic.GetRootKey();
// This path will give us our Payment Key on index 0string paymentPath = $"m/1852'/1815'/0'/0/0";// The paymentPrv is another Tuple with the Private Key and Chain CodePrivateKey paymentPrv = masterKey.Derive(paymentPath);// Get the Public Key from the Payment Private KeyPublicKey paymentPub = paymentPrv.GetPublicKey(false);
// This path will give us our Stake Key on index 0string stakePath = $"m/1852'/1815'/0'/2/0";// The stakePrv is another Tuple with the Private Key and Chain Codevar stakePrv = masterKey.Derive(stakePath);// Get the Public Key from the Stake Private Keyvar stakePub = stakePrv.GetPublicKey(false);
#
Create Addresses// Creating Addresses require the Public Payment and Stake KeysAddress baseAddr = addressService.GetAddress( paymentPub, stakePub, NetworkType.Testnet, AddressType.Base);
#
NetworkTypenamespace CardanoSharp.Wallet.Enums{ public enum NetworkType { Testnet, Mainnet }}
#
AddressTypenamespace CardanoSharp.Wallet.Enums{ public enum AddressType { Base, Enterprise, Reward }}
#
Build and Sign TransactionsThis is just an example of how to start. You will need to Calculate Fees, compare with Protocol Parameters and re-serialize.
using CardanoSharp.Wallet.Models.Transactions;
// The Transaction Builder allows us to contruct and serialize our Transactionusing CardanoSharp.Wallet.Models.Transactions;using CardanoSharp.Wallet.Extensions.Models.Transactions;//For CBOR Utilitiesusing PeterO.Cbor2;
// Create the Transaction Body// The Transaction Body:// Supported// - Transaction Inputs// - Transaction Outputs// - Fee// - TTL// - Certificates// - Metadata Hash// Coming Soon// - Transaction Start Interval// - Withdrawls// - Mintvar transactionBody = new TransactionBody(){ TransactionInputs = new List<TransactionInput>() { new TransactionInput() { TransactionIndex = 0, TransactionId = new byte[32] } }, TransactionOutputs = new List<TransactionOutput>() { new TransactionOutput() { Address = baseAddr.GetBytes(), Value = new TransactionOutputValue() { Coin = 1 } } }, Ttl = 10, Fee = 0};
// Add our witness(es)// Currently we only support VKey Witnessesvar witnesses = new TransactionWitnessSet(){ VKeyWitnesses = new List<VKeyWitness>() { new VKeyWitness() { VKey = paymentPub.Key, SKey = paymentPrv.Key } }};
// Create Transactionvar transaction = new Transaction(){ TransactionBody = transactionBody, TransactionWitnessSet = witnesses};
// Serialize Transaction with Body and Witnesses// This results in a Signed Transactionvar signedTxSerialized = transaction.Serialize();
#
Calculate Fees// From Current Protocol Parameters// 44 = txFeePerByte// 155381 = txFeeFixedvar fee = transaction.CalculateFee(44, 155381);
#
Adding Metadatavar auxData = new AuxiliaryData(){ Metadata = new Dictionary<int, object>() { { 1234, new { name = "simple message" } } }};
var transaction = new Transaction(){ TransactionBody = transactionBody, TransactionWitnessSet = witnesses, AuxiliaryData = auxData};
#
More ExamplesPlease see the Transaction Tests